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

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

import org.apache.commons.collections.CollectionUtils;

import com.uas.platform.b2b.model.Product;
import com.uas.platform.b2b.model.PurchaseInquiryItem;
import com.uas.platform.b2b.model.PurchaseInquiryReply;
import com.uas.platform.b2b.model.SaleQuotationItem;
import com.uas.platform.b2b.model.SaleQuotationPrice;
import com.uas.platform.b2b.support.SystemSession;
import com.uas.platform.core.model.Constant;
import com.uas.platform.core.model.Status;

/**
 * 买家ERP系统的采购询价单明细
 * 
 * @author yingp
 * 
 */
public class InquiryDetail {

	private Long b2b_id_id;
	private short id_detno;
	private String id_prodcode;
	private String id_currency;
	private Float id_rate;
	private Long ve_uu;
	private String ve_contact;
	private Long ve_contactuu;
	private String id_remark;
	private Long id_id;
	private Date id_myfromdate;
	private Date id_mytodate;
	private Date id_fromdate;
	private Date id_todate;
	private Double id_minbuyqty;
	private Double id_minqty;
	private List<InquiryDetailDet> dets;
	private Long ve_buyeruu;

	public Long getB2b_id_id() {
		return b2b_id_id;
	}

	public void setB2b_id_id(Long b2b_id_id) {
		this.b2b_id_id = b2b_id_id;
	}

	public short getId_detno() {
		return id_detno;
	}

	public void setId_detno(short id_detno) {
		this.id_detno = id_detno;
	}

	public String getId_prodcode() {
		return id_prodcode;
	}

	public void setId_prodcode(String id_prodcode) {
		this.id_prodcode = id_prodcode;
	}

	public String getId_currency() {
		return id_currency;
	}

	public void setId_currency(String id_currency) {
		this.id_currency = id_currency;
	}

	public Float getId_rate() {
		return id_rate;
	}

	public void setId_rate(Float id_rate) {
		this.id_rate = id_rate;
	}

	public Long getVe_uu() {
		return ve_uu;
	}

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

	public String getVe_contact() {
		return ve_contact;
	}

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

	public Long getVe_contactuu() {
		return ve_contactuu;
	}

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

	public String getId_remark() {
		return id_remark;
	}

	public void setId_remark(String id_remark) {
		this.id_remark = id_remark;
	}

	public Long getId_id() {
		return id_id;
	}

	public void setId_id(Long id_id) {
		this.id_id = id_id;
	}

	public List<InquiryDetailDet> getDets() {
		return dets;
	}

	public void setDets(List<InquiryDetailDet> dets) {
		this.dets = dets;
	}

	public Date getId_myfromdate() {
		return id_myfromdate;
	}

	public void setId_myfromdate(Date id_myfromdate) {
		this.id_myfromdate = id_myfromdate;
	}

	public Date getId_mytodate() {
		return id_mytodate;
	}

	public void setId_mytodate(Date id_mytodate) {
		this.id_mytodate = id_mytodate;
	}

	public Date getId_fromdate() {
		return id_fromdate;
	}

	public void setId_fromdate(Date id_fromdate) {
		this.id_fromdate = id_fromdate;
	}

	public Date getId_todate() {
		return id_todate;
	}

	public void setId_todate(Date id_todate) {
		this.id_todate = id_todate;
	}

	public Double getId_minbuyqty() {
		return id_minbuyqty;
	}

	public void setId_minbuyqty(Double id_minbuyqty) {
		this.id_minbuyqty = id_minbuyqty;
	}

	public Double getId_minqty() {
		return id_minqty;
	}

	public void setId_minqty(Double id_minqty) {
		this.id_minqty = id_minqty;
	}

	public Long getVe_buyeruu() {
		return ve_buyeruu;
	}

	public void setVe_buyeruu(Long ve_buyeruu) {
		this.ve_buyeruu = ve_buyeruu;
	}

	/**
	 * 转为平台的采购询价明细
	 * 
	 * @return
	 */
	public PurchaseInquiryItem convert() {
		PurchaseInquiryItem inquiryItem = new PurchaseInquiryItem();
		inquiryItem.setCurrency(this.id_currency);
		inquiryItem.setNumber(this.id_detno);
		inquiryItem.setRemark(this.id_remark);
		inquiryItem.setTaxrate(this.id_rate);
		inquiryItem.setUserUU(this.ve_buyeruu);
		inquiryItem.setVendUU(this.ve_uu);
		inquiryItem.setVendUserUU(this.ve_contactuu);
		Product product = new Product();
		product.setCode(this.id_prodcode);
		product.setEnUU(SystemSession.getUser().getEnterprise().getUu());
		inquiryItem.setProduct(product);
		inquiryItem.setSourceId(this.id_id);
		inquiryItem.setStatus((short) Status.NOT_REPLY.value());
		inquiryItem.setSendStatus((short) Status.NOT_UPLOAD.value());
		if (!CollectionUtils.isEmpty(this.dets)) {// 都不会为空
			Set<PurchaseInquiryReply> replies = new HashSet<PurchaseInquiryReply>();
			for (InquiryDetailDet det : this.dets) {
				replies.add(det.convert());
			}
			inquiryItem.setReplies(replies);
			inquiryItem.setCustLap(replies.size() > 1 ? Constant.YES : Constant.NO);
		}
		return inquiryItem;
	}

	public InquiryDetail() {

	}

	public InquiryDetail(PurchaseInquiryItem item) {
		this.b2b_id_id = item.getId();
		this.id_minbuyqty = item.getMinOrderQty();
		this.id_minqty = item.getMinPackQty();
		this.id_fromdate = item.getVendFromDate();
		this.id_todate = item.getVendToDate();
		this.id_id = item.getSourceId();
		this.id_detno = item.getNumber();
		if (!CollectionUtils.isEmpty(item.getReplies())) {
			dets = new ArrayList<InquiryDetailDet>();
			for (PurchaseInquiryReply reply : item.getReplies()) {
				dets.add(new InquiryDetailDet(reply));
			}
		}
	}

	public InquiryDetail(SaleQuotationItem item) {
		this.id_currency = item.getQuotation().getCurrency();
		this.id_detno = item.getNumber();
		this.id_fromdate = item.getQuotation().getDate();
		this.id_minbuyqty = item.getMinOrderQty();
		this.id_minqty = item.getMinPackQty();
		this.id_myfromdate = item.getQuotation().getDate();
		this.id_mytodate = item.getQuotation().getEndDate();
		this.id_prodcode = item.getCustProductCode();
		this.id_rate = item.getQuotation().getRate();
		this.id_remark = item.getRemark();
		this.id_todate = item.getQuotation().getEndDate();
		this.ve_uu = item.getQuotation().getEnUU();
		if (!CollectionUtils.isEmpty(item.getPrices())) {
			dets = new ArrayList<InquiryDetailDet>();
			for (SaleQuotationPrice price : item.getPrices()) {
				dets.add(new InquiryDetailDet(price));
			}
		}
	}

}
