package com.uas.platform.b2b.model;

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

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;

import org.codehaus.jackson.annotate.JsonIgnore;

import com.alibaba.fastjson.annotation.JSONField;

/**
 * 卖家应收（买家应付）对账单明细
 * 
 * @author suntg
 * @date 2015年10月10日16:42:14
 */
@Table(name = "sale$archeckitems")
@Entity
public class SaleARCheckItem {

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	@Column(name = "saci_id")
	private Long id;

	/**
	 * 明细序号
	 */
	@Column(name = "saci_number")
	private Short number;

	/**
	 * 来源单号
	 */
	@Column(name = "saci_sourcecode")
	private String sourceCode;

	/**
	 * 来源单据序号
	 */
	@Column(name = "saci_sourcenumber")
	private Short sourceNumber;

	/**
	 * 来源单据类型
	 */
	@Column(name = "saci_sourcetype")
	private String sourceType;

	/**
	 * 出入库单号
	 */
	@Column(name = "saci_inoutcode")
	private String inoutCode;

	/**
	 * 本次对账数量
	 */
	@Column(name = "saci_qty")
	private Double qty;

	/**
	 * 单价
	 */
	@Column(name = "saci_price")
	private Double price;

	/**
	 * 本次对账金额
	 */
	@Column(name = "saci_amount")
	private Double amount;

	/**
	 * 税率
	 */
	@Column(name = "saci_taxrate")
	private Double taxrate;

	/**
	 * 已收款金额
	 */
	@Column(name = "saci_paidamount")
	private Double paidAmount;

	/**
	 * 客户采购单号
	 */
	@Column(name = "saci_pocode")
	private String poCode;

	/**
	 * （客户）物料编号
	 */
	@Column(name = "saci_prodcode")
	private String prodCode;

	/**
	 * （客户）物料名称
	 */
	@Column(name = "saci_prodtitle")
	private String prodTitle;

	/**
	 * （客户）物料规格
	 */
	@Column(name = "saci_prodspec")
	private String prodSpec;

	/**
	 * （客户）是否同意 是 1， 否 0
	 */
	@Column(name = "saci_agreed")
	private Short agreed;

	/**
	 * 状态
	 */
	@Column(name = "saci_status")
	private Short status;

	/**
	 * 来源单据明细id
	 */
	@Column(name = "saci_sourceid")
	private Long sourceId;

	/**
	 * 客户确认数量
	 */
	@Column(name = "saci_replyqty")
	private Double replyQty;

	/**
	 * 客户确认备注
	 */
	@Column(name = "saci_replyremark")
	private String replyRemark;

	/**
	 * 是否等待供应商确认（1，0）
	 */
	@Column(name = "saci_waiting")
	private Short waiting;

	/**
	 * 应付对账单主记录
	 */
	@ManyToOne(cascade = CascadeType.ALL, optional = true)
	@JoinColumn(name = "saci_sacid", nullable = false)
	private SaleARCheck arCheck;

	/**
	 * 保存erp传入数据的时间
	 * 
	 * @return
	 */

	@Column(name = "saci_erpdate")
	private Date erpDate;

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public Short getNumber() {
		return number;
	}

	public void setNumber(Short number) {
		this.number = number;
	}

	public String getSourceCode() {
		return sourceCode;
	}

	public void setSourceCode(String sourceCode) {
		this.sourceCode = sourceCode;
	}

	public Short getSourceNumber() {
		return sourceNumber;
	}

	public void setSourceNumber(Short sourceNumber) {
		this.sourceNumber = sourceNumber;
	}

	public String getSourceType() {
		return sourceType;
	}

	public void setSourceType(String sourceType) {
		this.sourceType = sourceType;
	}

	public String getInoutCode() {
		return inoutCode;
	}

	public void setInoutCode(String inoutCode) {
		this.inoutCode = inoutCode;
	}

	public Double getQty() {
		return qty;
	}

	public void setQty(Double qty) {
		this.qty = qty;
	}

	public Double getPrice() {
		return price;
	}

	public void setPrice(Double price) {
		this.price = price;
	}

	public Double getAmount() {
		return amount;
	}

	public void setAmount(Double amount) {
		this.amount = amount;
	}

	public Double getTaxrate() {
		return taxrate;
	}

	public void setTaxrate(Double taxrate) {
		this.taxrate = taxrate;
	}

	public String getPoCode() {
		return poCode;
	}

	public void setPoCode(String poCode) {
		this.poCode = poCode;
	}

	public String getProdCode() {
		return prodCode;
	}

	public void setProdCode(String prodCode) {
		this.prodCode = prodCode;
	}

	public String getProdTitle() {
		return prodTitle;
	}

	public void setProdTitle(String prodTitle) {
		this.prodTitle = prodTitle;
	}

	public String getProdSpec() {
		return prodSpec;
	}

	public void setProdSpec(String prodSpec) {
		this.prodSpec = prodSpec;
	}

	public Short getAgreed() {
		return agreed;
	}

	public void setAgreed(Short agreed) {
		this.agreed = agreed;
	}

	@JsonIgnore
	@JSONField(serialize = false)
	public SaleARCheck getArCheck() {
		return arCheck;
	}

	public void setArCheck(SaleARCheck arCheck) {
		this.arCheck = arCheck;
	}

	public Double getPaidAmount() {
		return paidAmount;
	}

	public void setPaidAmount(Double paidAmount) {
		this.paidAmount = paidAmount;
	}

	public Short getStatus() {
		return status;
	}

	public void setStatus(Short status) {
		this.status = status;
	}

	public Long getSourceId() {
		return sourceId;
	}

	public void setSourceId(Long sourceId) {
		this.sourceId = sourceId;
	}

	public Double getReplyQty() {
		return replyQty;
	}

	public void setReplyQty(Double replyQty) {
		this.replyQty = replyQty;
	}

	public String getReplyRemark() {
		return replyRemark;
	}

	public void setReplyRemark(String replyRemark) {
		this.replyRemark = replyRemark;
	}

	public Short getWaiting() {
		return waiting;
	}

	public void setWaiting(Short waiting) {
		this.waiting = waiting;
	}

	public Date getErpDate() {
		return erpDate;
	}

	public void setErpDate(Date erpDate) {
		this.erpDate = erpDate;
	}

	public static List<SaleARCheck> distinct(List<SaleARCheckItem> items) {
		List<SaleARCheck> saleArChecks = new ArrayList<SaleARCheck>();
		Set<Long> keys = new HashSet<Long>();
		for (SaleARCheckItem item : items) {
			if (!keys.contains(item.arCheck.getId())) {
				saleArChecks.add(item.arCheck);
				keys.add(item.arCheck.getId());
			}
		}
		return saleArChecks;
	}
}
