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

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

import java.util.*;

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

	private Long in_id;
	private String in_code;
	private Date in_date;
	private String in_recorder;
	private Long in_recorderuu;
	private String in_auditor;
	private Date in_recorddate;
	private String in_environment;//环保要求
	private Date in_enddate;
	private String in_remark;
	private String in_pricetype;
	private List<InquiryDetail> details;
	private String in_kind; //询价类型
	private Long in_b2bid; // b2b存储的id信息
	/**
	 * 主动报价时，平台的主动报价单SaleQuotation的ID
	 */
	private Long b2b_qu_id;
	private Long in_buyeruu;
	private List<Attach> attaches;

	public Long getIn_id() {
		return in_id;
	}

	public void setIn_id(Long in_id) {
		this.in_id = in_id;
	}

	public String getIn_code() {
		return in_code;
	}

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

	public Date getIn_date() {
		return in_date;
	}
	
	public String getIn_pricetype() {
		return in_pricetype;
	}

	public void setIn_pricetype(String in_pricetype) {
		this.in_pricetype = in_pricetype;
	}

	public void setIn_date(Date in_date) {
		this.in_date = in_date;
	}

	public String getIn_recorder() {
		return in_recorder;
	}

	public void setIn_recorder(String in_recorder) {
		this.in_recorder = in_recorder;
	}

	public String getIn_auditor() {
		return in_auditor;
	}

	public void setIn_auditor(String in_auditor) {
		this.in_auditor = in_auditor;
	}

	public Date getIn_recorddate() {
		return in_recorddate;
	}

	public void setIn_recorddate(Date in_recorddate) {
		this.in_recorddate = in_recorddate;
	}

	public Date getIn_enddate() {
		return in_enddate;
	}

	public void setIn_enddate(Date in_enddate) {
		this.in_enddate = in_enddate;
	}

	public String getIn_remark() {
		return in_remark;
	}

	public void setIn_remark(String in_remark) {
		this.in_remark = in_remark;
	}

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

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

	public Long getB2b_qu_id() {
		return b2b_qu_id;
	}

	public void setB2b_qu_id(Long b2b_qu_id) {
		this.b2b_qu_id = b2b_qu_id;
	}

	public Long getIn_buyeruu() {
		return in_buyeruu;
	}

	public void setIn_buyeruu(Long in_buyeruu) {
		this.in_buyeruu = in_buyeruu;
	}

	public Long getIn_recorderuu() {
		return in_recorderuu;
	}

	public void setIn_recorderuu(Long in_recorderuu) {
		this.in_recorderuu = in_recorderuu;
	}

	public String getIn_environment() {
		return in_environment;
	}

	public void setIn_environment(String in_environment) {
		this.in_environment = in_environment;
	}

	public List<Attach> getAttaches() {
		return attaches;
	}

	public void setAttaches(List<Attach> attaches) {
		this.attaches = attaches;
	}

	public String getIn_kind() {
		return in_kind;
	}

	public void setIn_kind(String in_kind) {
		this.in_kind = in_kind;
	}

	public Long getIn_b2bid() {
		return in_b2bid;
	}

	public void setIn_b2bid(Long in_b2bid) {
		this.in_b2bid = in_b2bid;
	}

	/**
	 * 转为平台的采购询价
	 * 
	 * @return
	 */
	public PurchaseInquiry convert() {
		PurchaseInquiry inquiry = new PurchaseInquiry();
		inquiry.setCode(this.in_code);
		inquiry.setDate(this.in_date);
		inquiry.setEndDate(this.in_enddate);
		inquiry.setEnUU(SystemSession.getUser().getEnterprise().getUu());
		inquiry.setEnterprise(new EnterpriseInfo(SystemSession.getUser().getEnterprise()));
		inquiry.setRecorder(this.in_recorder);
		inquiry.setRecorderUU(this.in_recorderuu);
		inquiry.setRemark(this.in_remark);
		inquiry.setEnvironment(this.in_environment);
		inquiry.setSourceId(this.in_id);
		inquiry.setPriceType(this.in_pricetype);
		inquiry.setInquirytype(this.in_kind);
		inquiry.setIsOpen(Constant.NO); // erp传过来的询价单，默认为非公开
		if (!CollectionUtils.isEmpty(this.details)) {
			Set<PurchaseInquiryItem> inquiryItems = new HashSet<PurchaseInquiryItem>();
			for (InquiryDetail detail : details) {
				inquiryItems.add(detail.convert());
			}
			inquiry.setInquiryItems(inquiryItems);
		}
		if (!CollectionUtils.isEmpty(this.attaches)) {
			Set<com.uas.platform.b2b.model.Attach> b2bAttaches = new HashSet<com.uas.platform.b2b.model.Attach>();
			for(Attach attach : this.attaches) {
				com.uas.platform.b2b.model.Attach b2bAttach = attach.convertToB2bAttach("客户采购询价单附件");
				b2bAttaches.add(b2bAttach);
			}
			inquiry.setAttachs(b2bAttaches);
		}
		return inquiry;
	}

	public Inquiry() {

	}

	/**
	 * 平台的主动报价，转为买家的主动报价
	 * 
	 * @param quotation
	 */
	public Inquiry(SaleQuotation quotation) {
		this.in_code = quotation.getCode();
		this.in_date = quotation.getDate();
		this.in_enddate = quotation.getEndDate();
		this.in_recorddate = quotation.getDate();
		this.in_remark = quotation.getRemark();
		this.b2b_qu_id = quotation.getId();
		this.in_buyeruu = quotation.getCustUserUU();
		this.in_environment = quotation.getEnvironment();
		if (!CollectionUtils.isEmpty(quotation.getQuotationItems())) {
			this.details = new ArrayList<InquiryDetail>();
			for (SaleQuotationItem item : quotation.getQuotationItems()) {
				this.details.add(new InquiryDetail(item));
			}
		}
	}

}
