package com.uas.platform.b2b.model;

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

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

/**
 * 平台里面，供应商发起的发货单明细（明细表角度）
 * 
 * @author yingp
 * 
 */
@Table(name = "sale$senditem")
@Entity
public class SaleSendItem {

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

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

	/**
	 * 客户送货提醒单
	 */
	@OneToOne(cascade = { CascadeType.REFRESH })
	@JoinColumn(name = "si_noticeid", insertable = false, updatable = false, nullable = false)
	private PurchaseNotice notice;

	@Column(name = "si_noticeid")
	private Long noticeId;

	/**
	 * 客户采购单明细（可能存在不通过送货提醒发货的情况，所以需要能直接关联客户采购单明细）
	 */
	@OneToOne(cascade = { CascadeType.REFRESH })
	@JoinColumn(name = "si_pdid", insertable = false, updatable = false, nullable = false)
	private PurchaseOrderItem orderItem;

	@Column(name = "si_pdid")
	private Long orderItemId;

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

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

	/**
	 * 发货单
	 */
	@ManyToOne(cascade = CascadeType.ALL, optional = true)
	@JoinColumn(name = "si_ssid", nullable = false)
	private SaleSend send;

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

	/**
	 * 客户确认信息
	 */
	@Column(name = "si_replyremark")
	private String replyRemark;
	
	/**
	 * 客户收料数
	 */
	@Column(name = "si_verifyqty")
	private Double verifyQty;
	
	/**
	 * 客户收料合格数
	 */
	@Column(name = "si_okqty")
	private Double okQty;
	
	/**
	 * 客户收料不合格数
	 */
	@Column(name = "si_notokqty")
	private Double notokQty;
	
	/**
	 * 最小包装数
	 */
	@Column(name = "si_packageqty")
	private Double packageQty;
	
	/**
	 * 外箱容量
	 */
	@Column(name = "si_outboxqty")
	private Double outBoxQty;

	/**
	 * 是否勾选生成外箱号
	 */
	@Transient
	private Boolean isOutboxChecked;
	
	/**
	 * 条码明细
	 */
	@OneToMany(mappedBy = "saleSendItem", cascade = { CascadeType.REFRESH, CascadeType.PERSIST, CascadeType.REMOVE, CascadeType.MERGE }, fetch = FetchType.EAGER)
	private Set<BarSendNotify> barSendNotify;
	
	/**
	 * 明细行中的条码打印状态【0:未打印，1:已打印】
	 */
	@Column(name = "si_barprintstatus")
	private Long barPrintStatus;
	
	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;
	}

	@JsonIgnore
	@JSONField(serialize = false)
	public PurchaseNotice getNotice() {
		return notice;
	}

	public void setNotice(PurchaseNotice notice) {
		this.notice = notice;
	}

	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 SaleSend getSend() {
		return send;
	}

	public void setSend(SaleSend send) {
		this.send = send;
	}

	public String getRemark() {
		return remark;
	}

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

	public Long getNoticeId() {
		return noticeId;
	}

	public void setNoticeId(Long noticeId) {
		this.noticeId = noticeId;
	}

	public PurchaseOrderItem getOrderItem() {
		return orderItem;
	}

	public void setOrderItem(PurchaseOrderItem orderItem) {
		this.orderItem = orderItem;
	}

	public Long getOrderItemId() {
		return orderItemId;
	}

	public void setOrderItemId(Long orderItemId) {
		this.orderItemId = orderItemId;
	}

	public String getReplyRemark() {
		return replyRemark;
	}

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

	public Double getVerifyQty() {
		return verifyQty;
	}

	public void setVerifyQty(Double verifyQty) {
		this.verifyQty = verifyQty;
	}

	public Double getOkQty() {
		return okQty;
	}

	public void setOkQty(Double okQty) {
		this.okQty = okQty;
	}

	public Double getNotokQty() {
		return notokQty;
	}

	public void setNotokQty(Double notokQty) {
		this.notokQty = notokQty;
	}

	public Double getPackageQty() {
		return packageQty;
	}

	public void setPackageQty(Double packageQty) {
		this.packageQty = packageQty;
	}

	public Double getOutBoxQty() {
		return outBoxQty;
	}

	public void setOutBoxQty(Double outBoxQty) {
		this.outBoxQty = outBoxQty;
	}

	public void modify(double oldQty, double newQty) {
		setReplyRemark("客户修改数量：" + oldQty + "改为" + newQty);
		setQty(newQty);
	}

	public Boolean getIsOutboxChecked() {
		return isOutboxChecked;
	}

	public void setIsOutboxChecked(Boolean isOutboxChecked) {
		this.isOutboxChecked = isOutboxChecked;
	}

	/**
	 * @return the barSendNotify
	 */
	public Set<BarSendNotify> getBarSendNotify() {
		return barSendNotify;
	}

	/**
	 * @param barSendNotify the barSendNotify to set
	 */
	public void setBarSendNotify(Set<BarSendNotify> barSendNotify) {
		this.barSendNotify = barSendNotify;
	}

	/**
	 * @return the barPrintStatus
	 */
	public Long getBarPrintStatus() {
		return barPrintStatus;
	}

	/**
	 * @param barPrintStatus the barPrintStatus to set
	 */
	public void setBarPrintStatus(Long barPrintStatus) {
		this.barPrintStatus = barPrintStatus;
	}

    public static List<SaleSend> distinct(List<SaleSendItem> sendItems) {
		List<SaleSend> list = new ArrayList<>();
		Set<Long> keys = new HashSet<Long>();
		for (SaleSendItem item : sendItems) {
			if (!keys.contains(item.getSend().getId())) {
				list.add(item.getSend());
				keys.add(item.getSend().getId());
			}
		}
		return list;
    }
}
