package com.uas.platform.b2b.model;

import com.alibaba.fastjson.annotation.JSONField;
import org.codehaus.jackson.annotate.JsonIgnore;

import javax.persistence.*;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * 平台里面，以供应商的角度来查看委外加工单用料明细
 * 
 * @author suntg
 *
 */
@Table(name = "make$orderitems")
@Entity
public class MakeOrderItem implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

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

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

	/**
	 * 用料ID
	 */
	@Column(name = "mai_prid")
	private Long productId;

	/**
	 * 用料
	 */
	@OneToOne(cascade = { CascadeType.REFRESH })
	@JoinColumn(name = "mai_prid", insertable = false, updatable = false)
	private Product product;
	
	/**
	 * 单位用量
	 */
	@Column(name = "mai_perqty")
	private Double perQty;

	/**
	 * 订单用量
	 */
	@Column(name = "mai_qty")
	private Double qty;
	
	/**
	 * 是否可以替代
	 * 1 可以，0 不可
	 */
	@Column(name = "mai_ifreplace")
	private Short ifReplace;

	/**
	 * 替代料ID
	 */
	@Column(name = "mai_repprid")
	private Long repProductId;

	/**
	 * 替代料
	 */
	@OneToOne(cascade = { CascadeType.REFRESH })
	@JoinColumn(name = "mai_repprid", insertable = false, updatable = false)
	private Product repProduct;

	/**
	 * 委外加工单主记录
	 */
	@ManyToOne(cascade = CascadeType.ALL, optional = true)
	@JoinColumn(name = "mai_maid", nullable = false)
	private MakeOrder make;

	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 Long getProductId() {
		return productId;
	}

	public void setProductId(Long productId) {
		this.productId = productId;
	}

	public Product getProduct() {
		return product;
	}

	public void setProduct(Product product) {
		this.product = product;
	}

	public Double getPerQty() {
		return perQty;
	}

	public void setPerQty(Double perQty) {
		this.perQty = perQty;
	}

	public Double getQty() {
		return qty;
	}

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

	public Short getIfReplace() {
		return ifReplace;
	}

	public void setIfReplace(Short ifReplace) {
		this.ifReplace = ifReplace;
	}

	public Long getRepProductId() {
		return repProductId;
	}

	public void setRepProductId(Long repProductId) {
		this.repProductId = repProductId;
	}

	public Product getRepProduct() {
		return repProduct;
	}

	public void setRepProduct(Product repProduct) {
		this.repProduct = repProduct;
	}

	@JsonIgnore
	@JSONField(serialize = false)
	public MakeOrder getMake() {
		return make;
	}

	public void setMake(MakeOrder make) {
		this.make = make;
	}
	
	public static List<MakeOrder> distinct(List<MakeOrderItem> items) {
		List<MakeOrder> orders = new ArrayList<MakeOrder>();
		Set<Long> keys = new HashSet<Long>();
		for (MakeOrderItem item : items) {
			if (!keys.contains(item.getMake().getId())) {
				orders.add(item.getMake());
				keys.add(item.getMake().getId());
			}
		}
		return orders;
	}
	


}
