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.OneToOne;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;

import org.codehaus.jackson.annotate.JsonIgnore;

import com.alibaba.fastjson.annotation.JSONField;

/**
 * 买家委外验收明细
 * 
 * @author aof
 * @date 2015年8月18日
 */
@Table(name = "make$acceptitem")
@Entity
public class MakeAcceptItem {

	/**
	 * 主键ID
	 */
	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	@Column(name = "mai_id")
	private Long id;

	/**
	 * 序号
	 */
	@Column(name = "mai_number")
	private Short number;

	/**
	 * 数量
	 */
	@Column(name = "mai_qty")
	private Double qty;

	/**
	 * 委外采购单价
	 */
	@Column(name = "mai_orderpice")
	private Double orderPrice;

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

	/**
	 * 验收单主记录
	 */
	@ManyToOne(cascade = CascadeType.ALL, optional = true)
	@JoinColumn(name = "mai_paid", nullable = false)
	private MakeAccept accept;

	/**
	 * 委外订单
	 */
	@OneToOne(cascade = { CascadeType.REFRESH })
	@JoinColumn(name = "mai_odid", insertable = false, updatable = false)
	private MakeOrder order;

	/**
	 * 委外加工单id
	 */
	@Column(name = "mai_odid")
	private Long orderId;

	/**
	 * 入库批号
	 */
	@Column(name = "mai_batchcode")
	private String batchCode;

	/**
	 * 备注
	 */
	@Column(name = "mai_remark")
	private String remark;

	/**
	 * 仓库名称
	 */
	@Column(name = "mai_whname")
	private String whname;

	/**
	 * 存储erp传入数据时间
	 */
	@Column(name = "mai_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 Double getQty() {
		return qty;
	}

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

	public Double getOrderPrice() {
		return orderPrice;
	}

	public void setOrderPrice(Double orderPrice) {
		this.orderPrice = orderPrice;
	}

	public Double getTaxrate() {
		return taxrate;
	}

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

	@JsonIgnore
	@JSONField(serialize = false)
	public MakeAccept getAccept() {
		return accept;
	}

	public void setAccept(MakeAccept accept) {
		this.accept = accept;
	}

	public MakeOrder getOrder() {
		return order;
	}

	public void setOrder(MakeOrder order) {
		this.order = order;
	}

	public Long getOrderId() {
		return orderId;
	}

	public void setOrderId(Long orderId) {
		this.orderId = orderId;
	}

	public String getBatchCode() {
		return batchCode;
	}

	public void setBatchCode(String batchCode) {
		this.batchCode = batchCode;
	}

	public String getRemark() {
		return remark;
	}

	public void setRemark(String remark) {
		this.remark = remark;
	}

	public String getWhname() {
		return whname;
	}

	public void setWhname(String whname) {
		this.whname = whname;
	}

	public Date getErpDate() {
		return erpDate;
	}

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

	/**
	 * 将明细的重复数据去除
	 * 
	 * @param items
	 * @return
	 */
	public static List<MakeAccept> distinct(List<MakeAcceptItem> items) {
		List<MakeAccept> accepts = new ArrayList<MakeAccept>();
		Set<Long> keys = new HashSet<Long>();
		for (MakeAcceptItem item : items) {
			if (!keys.contains(item.getAccept().getId())) {
				accepts.add(item.getAccept());
				keys.add(item.getAccept().getId());
			}
		}
		return accepts;
	}

}
