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.PurchaseInquiry;
import com.uas.platform.b2b.model.PurchaseInquiryItem;
import com.uas.platform.b2b.model.SaleQuotation;
import com.uas.platform.b2b.model.SaleQuotationItem;
import com.uas.platform.b2b.support.SystemSession;

/**
 * 买家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 Date in_enddate;
	private String in_remark;
	private List<InquiryDetail> details;
	/**
	 * 主动报价时，平台的主动报价单SaleQuotation的ID
	 */
	private Long b2b_qu_id;
	private Long in_buyeruu;

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

	/**
	 * 转为平台的采购询价
	 * 
	 * @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(SystemSession.getUser().getEnterprise());
		inquiry.setRecorder(this.in_recorder);
		inquiry.setRecorderUU(this.in_recorderuu);
		inquiry.setRemark(this.in_remark);
		inquiry.setSourceId(this.in_id);
		if (!CollectionUtils.isEmpty(this.details)) {
			Set<PurchaseInquiryItem> inquiryItems = new HashSet<PurchaseInquiryItem>();
			for (InquiryDetail detail : details) {
				inquiryItems.add(detail.convert());
			}
			inquiry.setInquiryItems(inquiryItems);
		}
		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();
		if (!CollectionUtils.isEmpty(quotation.getQuotationItems())) {
			this.details = new ArrayList<InquiryDetail>();
			for (SaleQuotationItem item : quotation.getQuotationItems()) {
				this.details.add(new InquiryDetail(item));
			}
		}
	}

}
