package com.uas.platform.b2b.model;

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

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

@Table(name = "purc$orderitems")
@Entity
public class PurchaseOrderTodoItem {

	@Id
	@Column(name = "pd_id")
	private Long id;

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

	/**
	 * 采购订单
	 */
	@ManyToOne(cascade = CascadeType.ALL, optional = true)
	@JoinColumn(name = "pd_puid", nullable = false)
	private PurchaseOrderTodo 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")
	private String replyRemark;

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

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

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

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

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

	/**
	 * 处理状态（已回复、未回复），全部回复后改为已回复
	 */
	@Column(name = "pd_status", insertable = false, updatable = false)
	private Short status;

	/**
	 * 含税金额
	 */
	@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;

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

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

	/**
	 * 关键词查询相关
	 */
	@Transient
	private Boolean key;

    /**
     * 最后一次回复数量
     */
	@Transient
	private Double latestReplyQty;

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

	public void setOrder(PurchaseOrderTodo 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 Double getReplyQty() {
		return replyQty;
	}

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

	public String getRemark() {
		return remark;
	}

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

	public String getVendspec() {
		return vendspec;
	}

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

	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 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 String getReplyRemark() {
		return replyRemark;
	}

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

	public Short getStatus() {
		return status;
	}

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

	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 Boolean getKey() {
		return key;
	}

	public void setKey(Boolean key) {
		this.key = key;
	}

	public String getFactory() {
		return factory;
	}

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

    public Double getLatestReplyQty() {
        return latestReplyQty;
    }

    public void setLatestReplyQty(Double latestReplyQty) {
        this.latestReplyQty = latestReplyQty;
    }

    public double getBalance() {
		double qtyNum, returnQtyNum, acceptQtyNum;
		qtyNum = qty == null ? 0 : qty;
		returnQtyNum = returnQty == null ? 0 : returnQty;
		acceptQtyNum = acceptQty == null ? 0 : acceptQty;
		return qtyNum + returnQtyNum - acceptQtyNum;
	}
}
