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 yingp
 * 
 */
@Table(name = "purc$changeitems")
@Entity
public class PurchaseOrderChangeItem {

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

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

	/**
	 * 采购订单变更单
	 */
	@ManyToOne(cascade = CascadeType.ALL, optional = true)
	@JoinColumn(name = "pcd_pcid", nullable = false)
	private PurchaseOrderChange orderChange;

	/**
	 * 采购订单明细ID
	 */
	@Column(name = "pcd_pdid")
	private Long orderItemId;

	/**
	 * 采购订单明细序号
	 */
	@Column(name = "pcd_pdnumber")
	private Short orderItemNumber;

	/**
	 * 新产品
	 */
	@OneToOne(cascade = { CascadeType.REFRESH })
	@JoinColumn(name = "pcd_newprid", insertable = false, updatable = false)
	private Product newProduct;

	/**
	 * 原产品
	 */
	@OneToOne(cascade = { CascadeType.REFRESH })
	@JoinColumn(name = "pcd_oldprid", insertable = false, updatable = false)
	private Product oldProduct;

	/**
	 * 新产品id
	 */
	@Column(name = "pcd_newprid")
	private Long newProductId;

	/**
	 * 原产品id
	 */
	@Column(name = "pcd_oldprid")
	private Long oldProductId;

	/**
	 * 新数量
	 */
	@Column(name = "pcd_newqty")
	private Double newQty;

	/**
	 * 原数量
	 */
	@Column(name = "pcd_oldqty")
	private Double oldQty;

	/**
	 * 新含税单价
	 */
	@Column(name = "pcd_newprice")
	private Double newPrice;

	/**
	 * 原含税单价
	 */
	@Column(name = "pcd_oldprice")
	private Double oldPrice;

	/**
	 * 新税率
	 */
	@Column(name = "pcd_newtaxrate")
	private Float newTaxrate;

	/**
	 * 原税率
	 */
	@Column(name = "pcd_oldtaxrate")
	private Float oldTaxrate;

	/**
	 * 新交货日期
	 */
	@Column(name = "pcd_newdelivery")
	private Date newDelivery;

	/**
	 * 原交货日期
	 */
	@Column(name = "pcd_olddelivery")
	private Date oldDelivery;

	/**
	 * 备注
	 */
	@Column(name = "pcd_remark", length = 500)
	private String remark;

	/**
	 * 保存erp传入数据的存储时间
	 */
	@Column(name = "pcd_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;
	}

	@JsonIgnore
	@JSONField(serialize = false)
	public PurchaseOrderChange getOrderChange() {
		return orderChange;
	}

	public void setOrderChange(PurchaseOrderChange orderChange) {
		this.orderChange = orderChange;
	}

	public Product getNewProduct() {
		return newProduct;
	}

	public void setNewProduct(Product newProduct) {
		this.newProduct = newProduct;
	}

	public Double getNewQty() {
		return newQty;
	}

	public void setNewQty(Double newQty) {
		this.newQty = newQty;
	}

	public Double getNewPrice() {
		return newPrice;
	}

	public Long getOrderItemId() {
		return orderItemId;
	}

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

	public Long getNewProductId() {
		return newProductId;
	}

	public void setNewProductId(Long newProductId) {
		this.newProductId = newProductId;
	}

	public void setNewPrice(Double newPrice) {
		this.newPrice = newPrice;
	}

	public Float getNewTaxrate() {
		return newTaxrate;
	}

	public void setNewTaxrate(Float newTaxrate) {
		this.newTaxrate = newTaxrate;
	}

	public Date getNewDelivery() {
		return newDelivery;
	}

	public void setNewDelivery(Date newDelivery) {
		this.newDelivery = newDelivery;
	}

	public String getRemark() {
		return remark;
	}

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

	public Short getOrderItemNumber() {
		return orderItemNumber;
	}

	public void setOrderItemNumber(Short orderItemNumber) {
		this.orderItemNumber = orderItemNumber;
	}

	public Product getOldProduct() {
		return oldProduct;
	}

	public void setOldProduct(Product oldProduct) {
		this.oldProduct = oldProduct;
	}

	public Long getOldProductId() {
		return oldProductId;
	}

	public void setOldProductId(Long oldProductId) {
		this.oldProductId = oldProductId;
	}

	public Double getOldQty() {
		return oldQty;
	}

	public void setOldQty(Double oldQty) {
		this.oldQty = oldQty;
	}

	public Double getOldPrice() {
		return oldPrice;
	}

	public void setOldPrice(Double oldPrice) {
		this.oldPrice = oldPrice;
	}

	public Float getOldTaxrate() {
		return oldTaxrate;
	}

	public void setOldTaxrate(Float oldTaxrate) {
		this.oldTaxrate = oldTaxrate;
	}

	public Date getOldDelivery() {
		return oldDelivery;
	}

	public void setOldDelivery(Date oldDelivery) {
		this.oldDelivery = oldDelivery;
	}

	public Date getErpDate() {
		return erpDate;
	}

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

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

}
