package com.uas.platform.b2b.model;

import com.alibaba.fastjson.annotation.JSONField;
import org.codehaus.jackson.annotate.JsonIgnore;

import javax.persistence.*;
import java.util.*;

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

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

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

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

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

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

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

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

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

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

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

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

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

	/**
	 * 本次对账数量
	 */
	@Column(name = "pbi_ycheckqty")
	private Integer checkQty;

	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;
	}

	@JsonIgnore
	@JSONField(serialize = false)
	public PurchaseBadIn getBadIn() {
		return badIn;
	}

	public void setBadIn(PurchaseBadIn badIn) {
		this.badIn = badIn;
	}

	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;
	}

	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 Integer getCheckQty() {
		return checkQty;
	}

	public void setCheckQty(Integer checkQty) {
		this.checkQty = checkQty;
	}

	public static List<PurchaseBadIn> distinct(List<PurchaseBadInItem> items) {
		List<PurchaseBadIn> list = new ArrayList<PurchaseBadIn>();
		Set<Long> keys = new HashSet<Long>();
		for (PurchaseBadInItem item : items) {
			if (!keys.contains(item.getBadIn().getId())) {
				list.add(item.getBadIn());
				keys.add(item.getBadIn().getId());
			}
		}
		return list;
	}

}
