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;

/**
 * 买家应付（卖家应收）对账单明细
 */
@Table(name = "purc$apbilloutitem")
@Entity
public class PurchaseApBillOutItem implements Serializable {

	/**
	 * 序列号
	 */
	private static final long serialVersionUID = 1L;

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

	/**
	 * 行号
	 */
	@Column(name = "pai_number")
	private Short number;

	/**
	 * 状态
	 */
	@Column(name = "pai_status")
	private Integer status;

	/**
	 * 状态码
	 */
	@Column(name = "pai_statuscode")
	private Integer statusCode;

	/**
	 * 开票单价
	 */
	@Column(name = "pai_nowprice")
	private Double nowPrice;

	/**
	 * 成本单价
	 */
	@Column(name = "pai_costprice")
	private Double costPrice;

	/**
	 * 发票单价
	 */
	@Column(name = "pai_price")
	private Double price;

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

	/**
	 * 发票数量
	 */
	@Column(name = "pai_qty")
	private Double qty;

	/**
	 * 开票数量
	 */
	@Column(name = "pai_nowqty")
	private Double NowQty;

	@OneToOne(cascade = { CascadeType.REFRESH })
	@JoinColumn(name = "pai_prid", insertable = false, updatable = false)
	private Product product;

	/**
	 * 客户物料ID
	 */
	@Column(name = "pai_prid")
	private Long prid;

	/**
	 * 客户料号
	 */
	@Column(name = "pai_prodcode")
	private String prodCode;

	/**
	 * 客户订单单号
	 */
	@Column(name = "pai_ordercode")
	private String orderCode;

	/**
	 * 客户出入库类型
	 */
	@Column(name = "pai_orderclass")
	private String orderClass;

	/**
	 * 客户订单序号
	 */
	@Column(name = "pai_orderdetno")
	private Long orderDetno;

	/**
	 * 客户出入库单号
	 */
	@Column(name = "pai_inoutno")
	private String inoutno;

	/**
	 * 出入库单序号
	 */
	@Column(name = "pai_inoutnodetno")
	private Long inoutnodetno;

	/**
	 * 单据编号
	 */
	@Column(name = "pai_code")
	private String code;

	/**
	 * 主表ID
	 */
	@Column(name = "pai_billoutid")
	private Long billOutId;

	/**
	 * 发票ID
	 */
	@Column(name = "pai_orderid")
	private Long orderId;

	/**
	 * 明细行数据来源表
	 */
	@Column(name = "pai_sourcetable")
	private String sourceTable;

	/**
	 * 明细行数据来源对应ID
	 */
	@Column(name = "pai_sourceid")
	private Long sourceId;

	/**
	 * 此次开票之前的开票数（用于作废开票单时）
	 */
	@Column(name = "pai_oldqty")
	private Double oldQty;


	/**
	 * 开票账单主记录
	 */
	@ManyToOne(cascade = CascadeType.ALL, optional = true)
	@JoinColumn(name = "pai_paboid", nullable = false)
	private PurchaseApBillOut apBillOut;

	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 Integer getStatus() {
		return status;
	}

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

	public Integer getStatusCode() {
		return statusCode;
	}

	public void setStatusCode(Integer statusCode) {
		this.statusCode = statusCode;
	}

	public Double getNowPrice() {
		return nowPrice;
	}

	public void setNowPrice(Double nowPrice) {
		this.nowPrice = nowPrice;
	}

	public Double getCostPrice() {
		return costPrice;
	}

	public void setCostPrice(Double costPrice) {
		this.costPrice = costPrice;
	}

	public Double getPrice() {
		return price;
	}

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

	public Double getTaxrate() {
		return taxrate;
	}

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

	public Double getQty() {
		return qty;
	}

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

	public Double getNowQty() {
		return NowQty;
	}

	public void setNowQty(Double nowQty) {
		NowQty = nowQty;
	}

	public String getOrderCode() {
		return orderCode;
	}

	public void setOrderCode(String orderCode) {
		this.orderCode = orderCode;
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public Long getBillOutId() {
		return billOutId;
	}

	public void setBillOutId(Long billOutId) {
		this.billOutId = billOutId;
	}

	public Long getPrid() {
		return prid;
	}

	public void setPrid(Long prid) {
		this.prid = prid;
	}

	public String getProdCode() {
		return prodCode;
	}

	public void setProdCode(String prodCode) {
		this.prodCode = prodCode;
	}

	public String getSourceTable() {
		return sourceTable;
	}

	public void setSourceTable(String sourceTable) {
		this.sourceTable = sourceTable;
	}

	public Long getSourceId() {
		return sourceId;
	}

	public void setSourceId(Long sourceId) {
		this.sourceId = sourceId;
	}

	public Double getOldQty() {
		return oldQty;
	}

	public void setOldQty(Double oldQty) {
		this.oldQty = oldQty;
	}

	public Long getOrderId() {
		return orderId;
	}

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

	public Product getProduct() {
		return product;
	}

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

	@JSONField(serialize = false)
	@JsonIgnore
	public PurchaseApBillOut getApBillOut() {
		return apBillOut;
	}

	public void setApBillOut(PurchaseApBillOut apBillOut) {
		this.apBillOut = apBillOut;
	}

	public static List<PurchaseApBillOut> distinct(List<PurchaseApBillOutItem> items) {
		List<PurchaseApBillOut> PurchaseApBillOuts = new ArrayList<PurchaseApBillOut>();
		Set<Long> keys = new HashSet<Long>();
		for (PurchaseApBillOutItem item : items) {
			if (!keys.contains(item.apBillOut.getId())) {
				PurchaseApBillOuts.add(item.apBillOut);
				keys.add(item.apBillOut.getId());
			}
		}
		return PurchaseApBillOuts;
	}

	public String getOrderClass() {
		return orderClass;
	}

	public void setOrderClass(String orderClass) {
		this.orderClass = orderClass;
	}

	public Long getOrderDetno() {
		return orderDetno;
	}

	public void setOrderDetno(Long orderDetno) {
		this.orderDetno = orderDetno;
	}

	public String getInoutno() {
		return inoutno;
	}

	public void setInoutno(String inoutno) {
		this.inoutno = inoutno;
	}

	public Long getInoutnodetno() {
		return inoutnodetno;
	}

	public void setInoutnodetno(Long inoutnodetno) {
		this.inoutnodetno = inoutnodetno;
	}
}
