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.PurchaseOrderAll;
import com.uas.platform.b2b.model.PurchaseOrderAllItem;
import com.uas.platform.b2b.model.User;
import com.uas.platform.b2b.support.SystemSession;
import com.uas.platform.core.model.Status;
import com.uas.platform.core.util.encry.Md5Utils;

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

	private String pu_code;
	private Date pu_date;
	private Long em_uu;
	private String em_name;
	private String em_sex;
	private String em_mobile;
	private String em_email;
	private Long ve_uu;
	private Long ve_contactuu;
	private String ve_contact;
	private String pu_currency;
	private Float pu_rate;
	private String pu_kind;
	private String pu_payments;
	private String pu_remark;
	private String pu_recordman;
	private String pu_auditman;
	private Date pu_indate;
	private String pu_shipaddresscode;
	private List<PurchaseDetail> purchaseDetails;

	public String getPu_code() {
		return pu_code;
	}

	public void setPu_code(String pu_code) {
		this.pu_code = pu_code;
	}

	public Date getPu_date() {
		return pu_date;
	}

	public void setPu_date(Date pu_date) {
		this.pu_date = pu_date;
	}

	public Long getEm_uu() {
		return em_uu;
	}

	public void setEm_uu(Long em_uu) {
		this.em_uu = em_uu;
	}

	public String getEm_name() {
		return em_name;
	}

	public void setEm_name(String em_name) {
		this.em_name = em_name;
	}

	public String getEm_sex() {
		return em_sex;
	}

	public void setEm_sex(String em_sex) {
		this.em_sex = em_sex;
	}

	public String getEm_mobile() {
		return em_mobile;
	}

	public void setEm_mobile(String em_mobile) {
		this.em_mobile = em_mobile;
	}

	public String getEm_email() {
		return em_email;
	}

	public void setEm_email(String em_email) {
		this.em_email = em_email;
	}

	public Long getVe_uu() {
		return ve_uu;
	}

	public void setVe_uu(Long ve_uu) {
		this.ve_uu = ve_uu;
	}

	public Long getVe_contactuu() {
		return ve_contactuu;
	}

	public void setVe_contactuu(Long ve_contactuu) {
		this.ve_contactuu = ve_contactuu;
	}

	public String getVe_contact() {
		return ve_contact;
	}

	public void setVe_contact(String ve_contact) {
		this.ve_contact = ve_contact;
	}

	public String getPu_currency() {
		return pu_currency;
	}

	public void setPu_currency(String pu_currency) {
		this.pu_currency = pu_currency;
	}

	public String getPu_kind() {
		return pu_kind;
	}

	public void setPu_kind(String pu_kind) {
		this.pu_kind = pu_kind;
	}

	public Float getPu_rate() {
		return pu_rate;
	}

	public void setPu_rate(Float pu_rate) {
		this.pu_rate = pu_rate;
	}

	public String getPu_payments() {
		return pu_payments;
	}

	public void setPu_payments(String pu_payments) {
		this.pu_payments = pu_payments;
	}

	public String getPu_remark() {
		return pu_remark;
	}

	public void setPu_remark(String pu_remark) {
		this.pu_remark = pu_remark;
	}

	public String getPu_recordman() {
		return pu_recordman;
	}

	public void setPu_recordman(String pu_recordman) {
		this.pu_recordman = pu_recordman;
	}

	public String getPu_auditman() {
		return pu_auditman;
	}

	public void setPu_auditman(String pu_auditman) {
		this.pu_auditman = pu_auditman;
	}

	public Date getPu_indate() {
		return pu_indate;
	}

	public void setPu_indate(Date pu_indate) {
		this.pu_indate = pu_indate;
	}

	public String getPu_shipaddresscode() {
		return pu_shipaddresscode;
	}

	public void setPu_shipaddresscode(String pu_shipaddresscode) {
		this.pu_shipaddresscode = pu_shipaddresscode;
	}

	public List<PurchaseDetail> getPurchaseDetails() {
		return purchaseDetails;
	}

	public void setPurchaseDetails(List<PurchaseDetail> purchaseDetails) {
		this.purchaseDetails = purchaseDetails;
	}

	/**
	 * 转为平台的采购订单
	 * 
	 * @return
	 */
	public PurchaseOrderAll convert() {
		PurchaseOrderAll order = new PurchaseOrderAll();
		order.setCode(this.pu_code);
		order.setCurrency(this.pu_currency);
		order.setRate(this.pu_rate);
		order.setDate(this.pu_date);
		order.setEnUU(SystemSession.getUser().getEnterprise().getUu());
		order.setEnterprise(SystemSession.getUser().getEnterprise());
		User user = new User();
		user.setUserUU(this.em_uu);
		user.setUserTel(this.em_mobile);
		user.setUserSex(("女".equals(this.em_sex) || "F".equals(this.em_sex)) ? "F" : "M");
		user.setUserPwd(Md5Utils.encode("111111", this.em_uu));
		user.setUserName(this.em_name);
		user.setUserEmail(this.em_email);
		order.setUser(user);
		if (this.em_uu != null) {
			order.setUserUU(this.em_uu);
		}
		order.setPayments(this.pu_payments);
		order.setRemark(this.pu_remark);
		order.setShipAddress(this.pu_shipaddresscode);
		order.setRecorder(this.pu_recordman);
		order.setStatus((short) Status.NOT_REPLY.value());
		order.setSendStatus((short) Status.NOT_UPLOAD.value());
		order.setType(this.pu_kind);
		order.setVendUU(this.ve_uu);
		order.setVendUserUU(this.ve_contactuu);
		order.setAuditor(this.pu_auditman);
		if (!CollectionUtils.isEmpty(this.purchaseDetails)) {
			Set<PurchaseOrderAllItem> items = new HashSet<PurchaseOrderAllItem>();
			for (PurchaseDetail detail : this.purchaseDetails) {
				items.add(detail.convert());
			}
			order.setOrderItems(items);
		}
		return order;
	}

}
