package com.uas.platform.b2b.model;

import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.OptimisticLockType;
import org.hibernate.annotations.OptimisticLocking;

import javax.persistence.*;
import java.util.*;

/**
 * 平台里面，以供应商的角度来查看采购订单明细
 * 
 * @author yingp
 * 
 */
@Table(name = "purc$orderitems")
@Entity
@OptimisticLocking(type = OptimisticLockType.ALL)
@DynamicUpdate(value = true)
public class PurchaseOrderItem {

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

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

	/**
	 * 采购订单
	 */
	@ManyToOne(cascade = { CascadeType.PERSIST, CascadeType.MERGE })
	@JoinColumn(name = "pd_puid")
	private PurchaseOrder order;

	/**
	 * 产品ID
	 */
	@Column(name = "pd_prid")
	private Long productId;

	/**
	 * 产品
	 */
	@OneToOne(cascade = { CascadeType.REFRESH })
	@JoinColumn(name = "pd_prid", insertable = false, updatable = false)
	private Product product;

	/**
	 * 产品的附件
	 */
	@OneToMany(fetch = FetchType.EAGER, cascade = {CascadeType.ALL})
	@JoinTable(name = "purc$prod$attach", joinColumns = @JoinColumn(name = "pd_id", referencedColumnName = "pd_id"), inverseJoinColumns = @JoinColumn(name="at_id", referencedColumnName = "at_id"))
	private Set<Attach> attachs;

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

	/**
	 * 回复数量
	 */
	@Column(name = "pd_replyqty")
	private Double replyQty;

	/**
	 * (最近一次)回复的交期
	 */
	@Column(name = "pd_replydelivery")
	private Date replyDelivery;

	/**
	 * (最近一次)回复的备注
	 */
	@Column(name = "pd_replyremark", length = 1000)
	private String replyRemark;

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

	/**
	 * 送货工厂
	 */
	@Column(name = "pd_factory")
	private String factory;

	/**
	 * 供应商规格
	 */
	@Column(name = "pd_vendspec")
	private String vendspec;

	public String getVendspec() {
		return vendspec;
	}

	public void setVendspec(String vendspec) {
		this.vendspec = vendspec;
	}

	/**
	 * 含税单价
	 */
	@Column(name = "pd_price")
	private Double price;

	/**
	 * 税率
	 */
	@Column(name = "pd_taxrate")
	private Float taxrate;

	/**
	 * 含税金额
	 */
	@Column(name = "pd_amount", insertable = false, updatable = false)
	private Double amount;

	/**
	 * 不含税单价
	 */
	@Column(name = "pd_notaxprice", insertable = false, updatable = false)
	private Double noTaxPrice;

	/**
	 * 不含税金额
	 */
	@Column(name = "pd_notaxamount", insertable = false, updatable = false)
	private Double noTaxAmount;

	/**
	 * 交货日期
	 */
	@Column(name = "pd_delivery")
	private Date delivery;

	/**
	 * 虚拟列，不可修改，自动根据qty, replyqty生成值<br>
	 * 处理状态（已回复、未回复），全部回复后改为已回复
	 */
	@Column(name = "pd_status", insertable = false, updatable = false)
	private Short status;

	/**
	 * 是否已结案
	 */
	@Column(name = "pd_end")
	private Short end;

	/**
	 * 客户已验收数量
	 */
	@Column(name = "pd_acceptqty")
	private Double acceptQty;

	/**
	 * 客户验退数量
	 */
	@Column(name = "pd_returnqty")
	private Double returnQty;

	/**
	 * 结案状态是否已传输到卖家ERP的情况
	 */
	@Column(name = "pd_endstatus")
	private Short endStatus;

	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 PurchaseOrder getOrder() {
		return order;
	}

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

	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 Set<Attach> getAttachs() {
		return attachs;
	}

	public void setAttachs(Set<Attach> attachs) {
		this.attachs = attachs;
	}

	public Double getQty() {
		return qty;
	}

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

	public String getRemark() {
		return remark;
	}

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

	public Double getPrice() {
		return price;
	}

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

	public Float getTaxrate() {
		return taxrate;
	}

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

	public Double getAmount() {
		return amount;
	}

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

	public Double getNoTaxPrice() {
		return noTaxPrice;
	}

	public void setNoTaxPrice(Double noTaxPrice) {
		this.noTaxPrice = noTaxPrice;
	}

	public Double getNoTaxAmount() {
		return noTaxAmount;
	}

	public void setNoTaxAmount(Double noTaxAmount) {
		this.noTaxAmount = noTaxAmount;
	}

	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 Date getDelivery() {
		return delivery;
	}

	public void setDelivery(Date delivery) {
		this.delivery = delivery;
	}

	public Date getReplyDelivery() {
		return replyDelivery;
	}

	public void setReplyDelivery(Date replyDelivery) {
		this.replyDelivery = replyDelivery;
	}

	public Short getStatus() {
		return status;
	}

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

	public Short getEnd() {
		return end;
	}

	public void setEnd(Short end) {
		this.end = end;
	}

	public Short getEndStatus() {
		return endStatus;
	}

	public void setEndStatus(Short endStatus) {
		this.endStatus = endStatus;
	}

	public Double getAcceptQty() {
		return acceptQty;
	}

	public void setAcceptQty(Double acceptQty) {
		this.acceptQty = acceptQty;
	}

	public Double getReturnQty() {
		return returnQty;
	}

	public void setReturnQty(Double returnQty) {
		this.returnQty = returnQty;
	}

	public String getFactory() {
		return factory;
	}

	public void setFactory(String factory) {
		this.factory = factory;
	}

	public static List<PurchaseOrderItem> distinctItem(List<PurchaseOrderItem> items) {
		List<PurchaseOrderItem> orderItems = new ArrayList<>();
		Set<Long> keys = new HashSet<>();
		for (PurchaseOrderItem item : items) {
			if (!keys.contains(item.getOrder().getId())) {
				orderItems.add(item);
				keys.add(item.getOrder().getId());
			}
		}
		return orderItems;
	}

	public static List<PurchaseOrder> distinct(List<PurchaseOrderItem> items) {
		List<PurchaseOrder> orders = new ArrayList<>();
		Set<Long> keys = new HashSet<>();
		for (PurchaseOrderItem item : items) {
			if (!keys.contains(item.getOrder().getId())) {
				orders.add(item.getOrder());
				keys.add(item.getOrder().getId());
			}
		}
		return orders;
	}

}
