package com.uas.platform.b2b.history.model;

import java.io.Serializable;
import java.util.Date;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.OrderBy;
import javax.persistence.Table;

import org.codehaus.jackson.annotate.JsonIgnore;
import org.springframework.util.StringUtils;

import com.uas.platform.core.model.Constant;

/**
 * 客户采购订单变更单 （历史数据）<br>
 * 级联变更明细
 * 
 * @author yingp
 *
 */
@Table(name = "purc$changes$his")
@Entity(name = "PurchaseOrderChangeHistory")
public class PurchaseOrderChange implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = -8957353551959748654L;

	@Id
	@Column(name = "pc_id")
	private Long id;

	/**
	 * 采购变更单所属企业UU
	 */
	@Column(name = "pc_enuu")
	private Long enUU;

	/**
	 * 采购变更单号
	 */
	@Column(name = "pc_code")
	private String code;

	/**
	 * 录单人
	 */
	@Column(name = "pc_recorder")
	private String recorder;

	/**
	 * 单据录入日期
	 */
	@Column(name = "pc_indate")
	private Date date;

	/**
	 * 变更内容描述
	 */
	@Column(name = "pc_description")
	private String description;

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

	/**
	 * 从平台传到供应商ERP的状态
	 */
	@Column(name = "pc_sendstatus")
	private Short sendStatus;

	/**
	 * 从平台传回复内容到供应商ERP的状态
	 */
	@Column(name = "pc_replysendstatus")
	private Short replySendStatus;

	/**
	 * 状态（未回复、已回复）
	 */
	@Column(name = "pc_status")
	private Short status;

	/**
	 * 卖家是否同意了买家的变更请求(1,0)
	 */
	@Column(name = "pc_agreed")
	private Short agreed;

	/**
	 * 回复备注
	 */
	@Column(name = "pc_replyremark")
	private String replyRemark;

	/**
	 * 从平台传回复信息到买方ERP的状态
	 */
	@Column(name = "pc_backstatus")
	private Short backStatus;

	/**
	 * 新付款方式
	 */
	@Column(name = "pc_newpayments")
	private String newPayments;

	/**
	 * 新付款币种
	 */
	@Column(name = "pc_newcurrency")
	private String newCurrency;

	/**
	 * 新汇率
	 */
	@Column(name = "pc_newrate")
	private Float newRate;

	/**
	 * 采购订单
	 */
	@OneToOne(cascade = { CascadeType.REFRESH })
	@JoinColumn(name = "pc_puid", insertable = false, updatable = false)
	private PurchaseOrder order;

	@Column(name = "pc_puid")
	private Long orderId;

	/**
	 * 采购变更单明细
	 */
	@OneToMany(mappedBy = "orderChange", cascade = { CascadeType.REFRESH, CascadeType.PERSIST, CascadeType.REMOVE, CascadeType.MERGE }, fetch = FetchType.EAGER)
	@OrderBy("number")
	private Set<PurchaseOrderChangeItem> orderChangeItems;

	public Long getId() {
		return id;
	}

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

	public Long getEnUU() {
		return enUU;
	}

	public void setEnUU(Long enUU) {
		this.enUU = enUU;
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public String getRecorder() {
		return recorder;
	}

	public void setRecorder(String recorder) {
		this.recorder = recorder;
	}

	public Date getDate() {
		return date;
	}

	public void setDate(Date date) {
		this.date = date;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public String getRemark() {
		return remark;
	}

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

	public Short getSendStatus() {
		return sendStatus;
	}

	public void setSendStatus(Short sendStatus) {
		this.sendStatus = sendStatus;
	}

	public String getNewPayments() {
		return newPayments;
	}

	public void setNewPayments(String newPayments) {
		this.newPayments = newPayments;
	}

	public String getNewCurrency() {
		return newCurrency;
	}

	public void setNewCurrency(String newCurrency) {
		this.newCurrency = newCurrency;
	}

	public Float getNewRate() {
		return newRate;
	}

	public void setNewRate(Float newRate) {
		this.newRate = newRate;
	}

	public PurchaseOrder getOrder() {
		return order;
	}

	public void setOrder(PurchaseOrder order) {
		this.order = order;
	}

	public Set<PurchaseOrderChangeItem> getOrderChangeItems() {
		return orderChangeItems;
	}

	public void setOrderChangeItems(Set<PurchaseOrderChangeItem> orderChangeItems) {
		this.orderChangeItems = orderChangeItems;
	}

	public Short getStatus() {
		return status;
	}

	public void setStatus(Short status) {
		this.status = status;
	}

	public Short getAgreed() {
		return agreed;
	}

	public void setAgreed(Short agreed) {
		this.agreed = agreed;
	}

	public String getReplyRemark() {
		return replyRemark;
	}

	public void setReplyRemark(String replyRemark) {
		this.replyRemark = replyRemark;
	}

	public Short getBackStatus() {
		return backStatus;
	}

	public void setBackStatus(Short backStatus) {
		this.backStatus = backStatus;
	}

	public Short getReplySendStatus() {
		return replySendStatus;
	}

	public void setReplySendStatus(Short replySendStatus) {
		this.replySendStatus = replySendStatus;
	}

	public Long getOrderId() {
		return orderId;
	}

	public void setOrderId(Long orderId) {
		this.orderId = orderId;
	}

	@JsonIgnore
	public boolean isAgreed() {
		return this.agreed != null && Constant.YES == this.agreed;
	}

	/**
	 * 变更信息转入到采购订单
	 * 
	 * @return
	 */
	public PurchaseOrder convert() {
		if (StringUtils.hasText(this.newPayments))
			this.order.setPayments(this.newPayments);
		if (StringUtils.hasText(this.newCurrency)) {
			this.order.setCurrency(this.newCurrency);
			this.order.setRate(this.newRate);
		}
		return this.order;
	}

}
