package com.uas.platform.b2b.model;

import java.util.Date;

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;

/**
 * 平台里面的订单变更单明细
 * 
 * @author yingp
 *
 */
@Table(name = "purc$changeitems")
@Entity
public class PurchaseOrderChangeItem {

	@Id
	@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "purc$changeitems_gen")
	@SequenceGenerator(name = "purc$changeitems_gen", sequenceName = "purc$changeitems_seq", allocationSize = 1)
	@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;

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

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

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

	@Column(name = "pcd_newprid")
	private Long newProductId;

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

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

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

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

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

	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
	public PurchaseOrderChange getOrderChange() {
		return orderChange;
	}

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

	public PurchaseOrderItem getOrderItem() {
		return orderItem;
	}

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

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

	/**
	 * 变更明细转入到采购订单明细
	 * 
	 * @return
	 */
	public PurchaseOrderItem convert() {
		if (this.newDelivery != null)
			this.orderItem.setDelivery(this.newDelivery);
		if (this.newPrice != null)
			this.orderItem.setPrice(this.newPrice);
		if (this.newQty != null && this.newQty > 0)
			this.orderItem.setQty(this.newQty);
		if (this.newTaxrate != null)
			this.orderItem.setTaxrate(this.newTaxrate);
		if (this.newProduct != null) {
			this.orderItem.setProduct(this.newProduct);
			this.orderItem.setProductId(this.newProductId);
		}
		return this.orderItem;
	}

}
