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

import com.uas.platform.b2b.model.Attach;
import com.uas.platform.b2b.model.*;
import com.uas.platform.b2b.support.SystemSession;
import com.uas.platform.core.model.Constant;
import com.uas.platform.core.model.Status;
import org.apache.commons.collections.CollectionUtils;

import java.util.*;

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

    /**
     * 自动询价
     */
    private static String AUTO_INQUIRY = "自动询价";

    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 String id_brand;//供应商物料品牌
	private String id_vendorprodcode;//供应商物料编号
	private Long id_leadtime;//供应商交互周期
	private List<InquiryDetailDet> dets;
	private List<RemoteFile> files;
	private String id_attach;
	private Long ve_buyeruu;
	private String sourceApp;
	private String id_vendname;// 供应商名称
	private String id_vendyyzzh;// 供应商营业执照号
	private String in_code;// 公共询价时epr没有表不同，这时，需要新建数据
	private String in_inquirytype;// 询价类型，公共询价传回到ERP时进行特殊处理
    private Integer id_quto;//给是否自动询价赋值

	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;
	}

	public String getId_brand() {
		return id_brand;
	}

	public void setId_brand(String id_brand) {
		this.id_brand = id_brand;
	}

	public String getId_vendorprodcode() {
		return id_vendorprodcode;
	}

	public void setId_vendorprodcode(String id_vendorprodcode) {
		this.id_vendorprodcode = id_vendorprodcode;
	}

	public Long getId_leadtime() {
		return id_leadtime;
	}

	public void setId_leadtime(Long id_leadtime) {
		this.id_leadtime = id_leadtime;
	}

	public String getSourceApp() {
		return sourceApp;
	}

	public void setSourceApp(String sourceApp) {
		this.sourceApp = sourceApp;
	}

	public List<RemoteFile> getFiles() {
		return files;
	}

	public void setFiles(List<RemoteFile> files) {
		this.files = files;
	}

	public String getId_attach() {
		return id_attach;
	}

	public void setId_attach(String id_attach) {
		this.id_attach = id_attach;
	}

	public String getId_vendname() {
		return id_vendname;
	}

	public void setId_vendname(String id_vendname) {
		this.id_vendname = id_vendname;
	}

	public String getId_vendyyzzh() {
		return id_vendyyzzh;
	}

	public void setId_vendyyzzh(String id_vendyyzzh) {
		this.id_vendyyzzh = id_vendyyzzh;
	}

	public String getIn_code() {
		return in_code;
	}

	public void setIn_code(String in_code) {
		this.in_code = in_code;
	}

	public String getIn_inquirytype() {
		return in_inquirytype;
	}

	public void setIn_inquirytype(String in_inquirytype) {
		this.in_inquirytype = in_inquirytype;
	}

    public Integer getId_quto() {
        return id_quto;
    }

    public void setId_quto(Integer id_quto) {
        this.id_quto = id_quto;
    }

    /**
	 * 转为平台的采购询价明细
	 * 
	 * @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());
		inquiryItem.setFromDate(this.id_myfromdate);
		inquiryItem.setToDate(this.id_mytodate);
		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();
		this.id_brand = item.getBrand();
		this.id_vendorprodcode = item.getVendorprodcode();
		this.id_leadtime = item.getLeadtime();
		this.sourceApp = item.getSourceApp();
		this.id_rate = item.getTaxrate();
		this.id_vendname = item.getEnterprise().getEnName();
		this.id_vendyyzzh = item.getEnterprise().getBusinesscode();
		this.in_code = item.getInquiry().getCode();
		this.id_prodcode = item.getProduct().getCode();
		this.id_currency = item.getCurrency();
		this.ve_uu = item.getVendUU();
		this.in_inquirytype = item.getInquiry().getInquirytype();
		if (!CollectionUtils.isEmpty(item.getReplies())) {
			dets = new ArrayList<InquiryDetailDet>();
			for (PurchaseInquiryReply reply : item.getReplies()) {
				dets.add(new InquiryDetailDet(reply));
			}
		}
		if (!CollectionUtils.isEmpty(item.getAttaches())) {
			files = new ArrayList<>();
			for (Attach attach : item.getAttaches()) {
				files.add(new RemoteFile(attach));
			}
		}
		if (AUTO_INQUIRY.equals(item.getInquiry().getRemark())) {
            this.id_quto = 1;
        }
	}

	public InquiryDetail(SaleQuotationItem item) {
		this.id_currency = item.getQuotation().getCurrency();
		this.id_detno = item.getNumber();
		this.id_minbuyqty = item.getMinOrderQty();
		this.id_minqty = item.getMinPackQty();
		this.id_prodcode = item.getProduct() != null ? item.getProduct().getCode(): item.getCustProductCode();
		this.id_rate = item.getQuotation().getTaxrate();
		this.id_remark = item.getRemark();
		this.ve_uu = item.getQuotation().getEnUU();
		this.id_brand = item.getBrand();
		this.id_leadtime = item.getLeadtime();
		this.id_vendorprodcode = item.getVendorprodcode();
		if (item.getQuotation().getUser() != null) {
			this.ve_contact = item.getQuotation().getUser().getUserName();
		}
		this.ve_contactuu = item.getQuotation().getUserUU();
		this.ve_buyeruu = item.getQuotation().getCustUserUU();
		if (!CollectionUtils.isEmpty(item.getPrices())) {
			dets = new ArrayList<InquiryDetailDet>();
			for (SaleQuotationPrice price : item.getPrices()) {
				dets.add(new InquiryDetailDet(price));
			}
		}
	}

	/**
	 * 将平台的公共询价信息转成ERP对应的字段
     *
	 * @param item 平台公共询价已转报价的报价单明细
	 */
	public InquiryDetail(PublicInquiryItemInfo 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();
        this.id_brand = item.getBrand();
        this.id_vendorprodcode = item.getVendorprodcode();
        this.id_leadtime = item.getLeadtime();
        this.sourceApp = item.getSourceApp();
        this.id_rate = item.getTaxrate();
        this.id_vendname = item.getEnterprise().getEnName();
        this.id_vendyyzzh = item.getEnterprise().getBusinesscode();
        this.in_code = item.getInquiry().getCode();
        this.id_prodcode = item.getProduct().getCode();
        this.id_currency = item.getCurrency();
        this.ve_uu = item.getVendUU();
        this.in_inquirytype = item.getInquiry().getInquirytype();
        if (!CollectionUtils.isEmpty(item.getReplies())) {
            dets = new ArrayList<InquiryDetailDet>();
            for (PublicInquiryReply reply : item.getReplies()) {
                dets.add(new InquiryDetailDet(reply));
            }
        }
        if (!CollectionUtils.isEmpty(item.getAttaches())) {
            files = new ArrayList<>();
            for (Attach attach : item.getAttaches()) {
                files.add(new RemoteFile(attach));
            }
        }
        if (AUTO_INQUIRY.equals(item.getInquiry().getRemark())) {
            this.id_quto = 1;
        }
	}

}
