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

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

import org.springframework.util.CollectionUtils;

import com.uas.platform.b2b.model.PurchaseOrderChange;
import com.uas.platform.b2b.model.PurchaseOrderChangeItem;
import com.uas.platform.b2b.support.SystemSession;
import com.uas.platform.core.model.Status;

/**
 * ERP系统的采购订单变更单
 * 
 * @author yingp
 * 
 */
public class PurchaseChange {

	private String pc_code;
	private String pc_purccode;
	private Date pc_indate;
	private String pc_recorder;
	private String pc_newpayments;
	private String pc_newcurrency;
	private Float pc_newrate;
	private String pc_description;
	private String pc_remark;
	private Short pc_agreed;
	private List<PurchaseChangeDetail> changeDetails;

	public String getPc_code() {
		return pc_code;
	}

	public void setPc_code(String pc_code) {
		this.pc_code = pc_code;
	}

	public String getPc_purccode() {
		return pc_purccode;
	}

	public void setPc_purccode(String pc_purccode) {
		this.pc_purccode = pc_purccode;
	}

	public Date getPc_indate() {
		return pc_indate;
	}

	public void setPc_indate(Date pc_indate) {
		this.pc_indate = pc_indate;
	}

	public String getPc_recorder() {
		return pc_recorder;
	}

	public void setPc_recorder(String pc_recorder) {
		this.pc_recorder = pc_recorder;
	}

	public String getPc_newpayments() {
		return pc_newpayments;
	}

	public void setPc_newpayments(String pc_newpayments) {
		this.pc_newpayments = pc_newpayments;
	}

	public String getPc_newcurrency() {
		return pc_newcurrency;
	}

	public void setPc_newcurrency(String pc_newcurrency) {
		this.pc_newcurrency = pc_newcurrency;
	}

	public Float getPc_newrate() {
		return pc_newrate;
	}

	public void setPc_newrate(Float pc_newrate) {
		this.pc_newrate = pc_newrate;
	}

	public String getPc_description() {
		return pc_description;
	}

	public void setPc_description(String pc_description) {
		this.pc_description = pc_description;
	}

	public String getPc_remark() {
		return pc_remark;
	}

	public void setPc_remark(String pc_remark) {
		this.pc_remark = pc_remark;
	}

	public Short getPc_agreed() {
		return pc_agreed != null ? ((short) Math.abs(pc_agreed)) : null;
	}

	public void setPc_agreed(Short pc_agreed) {
		this.pc_agreed = pc_agreed;
	}

	public List<PurchaseChangeDetail> getChangeDetails() {
		return changeDetails;
	}

	public void setChangeDetails(List<PurchaseChangeDetail> changeDetails) {
		this.changeDetails = changeDetails;
	}

	/**
	 * 转为平台的采购订单变更单
	 * 
	 * @return
	 */
	public PurchaseOrderChange convert() {
		PurchaseOrderChange change = new PurchaseOrderChange();
		change.setBackStatus((short) Status.NOT_UPLOAD.value());
		change.setCode(this.pc_code);
		change.setDate(this.pc_indate);
		change.setDescription(this.pc_description);
		change.setEnUU(SystemSession.getUser().getEnterprise().getUu());
		change.setNewCurrency(this.pc_newcurrency);
		change.setNewPayments(this.pc_newpayments);
		change.setNewRate(this.pc_newrate);
		change.setRecorder(this.pc_recorder);
		change.setRemark(this.pc_remark);
		change.setSendStatus((short) Status.NOT_UPLOAD.value());
		change.setStatus((short) Status.NOT_REPLY.value());
		change.setAgreed(getPc_agreed());
		if (change.isAgreed()) {
			// 买家已同意的，无需再传回到买家ERP
			change.setStatus((short) Status.REPLIED.value());
			change.setBackStatus((short) Status.DOWNLOADED.value());
			// 买家已同意的，卖家ERP无需回复
			change.setReplySendStatus((short) Status.DOWNLOADED.value());
		}
		if (!CollectionUtils.isEmpty(this.changeDetails)) {
			Set<PurchaseOrderChangeItem> changeItems = new HashSet<PurchaseOrderChangeItem>();
			for (PurchaseChangeDetail detail : changeDetails) {
				changeItems.add(detail.convert());
			}
			change.setOrderChangeItems(changeItems);
		}
		return change;
	}

}