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

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

import org.apache.commons.collections.CollectionUtils;

import com.uas.platform.b2b.model.SaleSend;
import com.uas.platform.b2b.model.SaleSendItem;
import com.uas.platform.b2b.support.SystemSession;
import com.uas.platform.core.model.Status;

/**
 * 卖家ERP系统的发货单
 * 
 * @author yingp
 * 
 */
public class SaleOut {

	private Long b2b_ss_id;
	private String pi_inoutno;
	private String pi_currency;
	private Float pi_rate;
	private long cu_uu;
	private long cu_contactuu;
	private String pi_payment;
	private String pi_remark;
	private String pi_recordman;
	private String pi_auditman;
	private List<SaleOutDetail> details;

	public String getPi_inoutno() {
		return pi_inoutno;
	}

	public void setPi_inoutno(String pi_inoutno) {
		this.pi_inoutno = pi_inoutno;
	}

	public long getCu_uu() {
		return cu_uu;
	}

	public void setCu_uu(long cu_uu) {
		this.cu_uu = cu_uu;
	}

	public String getPi_currency() {
		return pi_currency;
	}

	public void setPi_currency(String pi_currency) {
		this.pi_currency = pi_currency;
	}

	public List<SaleOutDetail> getDetails() {
		return details;
	}

	public void setDetails(List<SaleOutDetail> details) {
		this.details = details;
	}

	public Long getB2b_ss_id() {
		return b2b_ss_id;
	}

	public void setB2b_ss_id(Long b2b_ss_id) {
		this.b2b_ss_id = b2b_ss_id;
	}

	public long getCu_contactuu() {
		return cu_contactuu;
	}

	public void setCu_contactuu(long cu_contactuu) {
		this.cu_contactuu = cu_contactuu;
	}

	public Float getPi_rate() {
		return pi_rate;
	}

	public void setPi_rate(Float pi_rate) {
		this.pi_rate = pi_rate;
	}

	public String getPi_payment() {
		return pi_payment;
	}

	public void setPi_payment(String pi_payment) {
		this.pi_payment = pi_payment;
	}

	public String getPi_remark() {
		return pi_remark;
	}

	public void setPi_remark(String pi_remark) {
		this.pi_remark = pi_remark;
	}

	public String getPi_recordman() {
		return pi_recordman;
	}

	public void setPi_recordman(String pi_recordman) {
		this.pi_recordman = pi_recordman;
	}

	public String getPi_auditman() {
		return pi_auditman;
	}

	public void setPi_auditman(String pi_auditman) {
		this.pi_auditman = pi_auditman;
	}

	public SaleOut() {
	}

	/**
	 * 平台的发货单，传到卖家平台
	 * 
	 * @param item
	 */
	public SaleOut(SaleSend send) {
		this.b2b_ss_id = send.getId();
		this.details = new ArrayList<SaleOutDetail>();
		if (!CollectionUtils.isEmpty(send.getSendItems())) {
			for (SaleSendItem item : send.getSendItems()) {
				this.details.add(new SaleOutDetail(item));
			}
		}
	}

	public SaleSend convert() {
		SaleSend send = new SaleSend();
		send.setBackStatus((short) Status.DOWNLOADED.value());
		send.setCode(this.pi_inoutno);
		send.setCurrency(this.pi_currency);
		send.setCustUserUU(this.cu_contactuu);
		send.setCustUU(this.cu_uu);
		send.setEnUU(SystemSession.getUser().getEnterprise().getUu());
		send.setPayments(this.pi_payment);
		send.setRate(this.pi_rate);
		send.setRemark(this.pi_remark);
		send.setRecorder(this.pi_recordman);
		send.setSendStatus((short) Status.DOWNLOADED.value());// 先暂置为已下载
		Set<SaleSendItem> items = new HashSet<SaleSendItem>();
		if (!CollectionUtils.isEmpty(this.details)) {
			for (SaleOutDetail detail : this.details) {
				SaleSendItem item = detail.convert();
				// 如果存在关联的送货提醒，则需要下达到买家
				if (item.getNoticeId() != null) {
                    send.setSendStatus((short) Status.NOT_UPLOAD.value());
                }
				items.add(item);
			}
		}
		send.setSendItems(items);
		return send;
	}

}
