package com.uas.platform.b2b.model;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToOne;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;

import org.codehaus.jackson.annotate.JsonIgnore;

import com.alibaba.fastjson.annotation.JSONField;

/**
 * 买家的不良品入库明细
 * 
 * @author suntg
 * 
 */
@Table(name = "purc$badoutitem")
@Entity
public class PurchaseBadOutItem {

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	@Column(name = "poi_id")
	private Long id;

	/**
	 * 序号
	 */
	@Column(name = "poi_number")
	private Short number;

	/**
	 * 数量
	 */
	@Column(name = "poi_qty")
	private Double qty;

	/**
	 * 采购单价
	 */
	@Column(name = "poi_orderprice")
	private Double orderPrice;

	/**
	 * 税率
	 */
	@Column(name = "poi_taxrate")
	private Double taxrate;

	/**
	 * 不良品入库单主记录
	 */
	@ManyToOne(cascade = CascadeType.ALL, optional = true)
	@JoinColumn(name = "poi_poid", nullable = false)
	private PurchaseBadOut badOut;

	/**
	 * 采购订单明细
	 */
	@OneToOne(cascade = { CascadeType.REFRESH })
	@JoinColumn(name = "poi_pdid", insertable = false, updatable = false)
	private PurchaseOrderItem orderItem;

	@Column(name = "poi_pdid")
	private Long orderItemId;

	/**
	 * 入库批号
	 */
	@Column(name = "poi_batchcode")
	private String batchCode;

	/**
	 * 备注
	 */
	@Column(name = "poi_remark")
	private String remark;

	/**
	 * 仓库名称
	 */
	@Column(name = "poi_whname")
	private String whname;

	/**
	 * 保存erp传入数据的时间
	 * 
	 * @return
	 */
	@Column(name = "poi_erpdate")
	private Date erpDate;

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public Short getNumber() {
		return number;
	}

	public void setNumber(Short number) {
		this.number = number;
	}

	public Double getQty() {
		return qty;
	}

	public void setQty(Double qty) {
		this.qty = qty;
	}

	public Double getOrderPrice() {
		return orderPrice;
	}

	public void setOrderPrice(Double orderPrice) {
		this.orderPrice = orderPrice;
	}

	public String getRemark() {
		return remark;
	}

	public void setRemark(String remark) {
		this.remark = remark;
	}

	public PurchaseOrderItem getOrderItem() {
		return orderItem;
	}

	public void setOrderItem(PurchaseOrderItem orderItem) {
		this.orderItem = orderItem;
	}

	public Long getOrderItemId() {
		return orderItemId;
	}

	public void setOrderItemId(Long orderItemId) {
		this.orderItemId = orderItemId;
	}

	public String getBatchCode() {
		return batchCode;
	}

	public void setBatchCode(String batchCode) {
		this.batchCode = batchCode;
	}

	public Double getTaxrate() {
		return taxrate;
	}

	public void setTaxrate(Double taxrate) {
		this.taxrate = taxrate;
	}

	@JsonIgnore
	@JSONField(serialize = false)
	public PurchaseBadOut getBadOut() {
		return badOut;
	}

	public void setBadOut(PurchaseBadOut badOut) {
		this.badOut = badOut;
	}

	public String getWhname() {
		return whname;
	}

	public void setWhname(String whname) {
		this.whname = whname;
	}

	public Date getErpDate() {
		return erpDate;
	}

	public void setErpDate(Date erpDate) {
		this.erpDate = erpDate;
	}

	public static List<PurchaseBadOut> distinct(List<PurchaseBadOutItem> items) {
		List<PurchaseBadOut> accepts = new ArrayList<PurchaseBadOut>();
		Set<Long> keys = new HashSet<Long>();
		for (PurchaseBadOutItem item : items) {
			if (!keys.contains(item.getBadOut().getId())) {
				accepts.add(item.getBadOut());
				keys.add(item.getBadOut().getId());
			}
		}
		return accepts;
	}

}
