package com.uas.platform.b2b.model;

import static com.alibaba.druid.sql.ast.expr.SQLUnaryOperator.Not;
import static org.springframework.data.jpa.domain.AbstractPersistable_.id;

import com.alibaba.fastjson.annotation.JSONField;
import javax.validation.constraints.NotNull;
import org.codehaus.jackson.annotate.JsonIgnore;
import org.springframework.util.CollectionUtils;

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

/**
 * 平台里面，供应商发起的发货单（明细表角度）<br>
 * （以供应商的视角）
 * 
 * @author yingp
 * 
 */
@Table(name = "sale$send", indexes = {@Index(name = "index_salesend_back", columnList = "ss_backstatus"), @Index(name = "index_salesend_send", columnList = "ss_sendstatus")})
@Entity
public class SaleSend implements Serializable {

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

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

	/**
	 * 单号
	 */
	@Column(name = "ss_code")
	private String code;

	/**
	 * 发货单 所属企业(卖家)UU
	 */
	@Column(name = "ss_enuu")
	private Long enUU;

	/**
	 * 发货单 所属企业(卖家)
	 */
	@OneToOne(cascade = { CascadeType.REFRESH })
	@JoinColumn(name = "ss_enuu", insertable = false, updatable = false)
	private EnterpriseInfo vendor;


	/**
	 * 发货单日期
	 */
	@Column(name = "ss_date")
	private Date date;

	/**
	 * 付款方式
	 */
	@Column(name = "ss_payments")
	private String payments;

	/**
	 * 币别
	 */
	@Column(name = "ss_currency")
	private String currency;

	/**
	 * 汇率
	 */
	@Column(name = "ss_rate")
	private Float rate;

	/**
	 * 客户企业UU
	 */
	@Column(name = "ss_custuu")
	private Long custUU;

	/**
	 * 客户企业
	 */
	@OneToOne(cascade = { CascadeType.REFRESH })
	@JoinColumn(name = "ss_custuu", insertable = false, updatable = false)
	private EnterpriseInfo customer;

	/**
	 * 客户联系人UU
	 */
	@Column(name = "ss_custuseruu")
	private Long custUserUU;

	/**
	 * 客户联系人
	 */
	@OneToOne(cascade = { CascadeType.REFRESH })
	@JoinColumn(name = "ss_custuseruu", insertable = false, updatable = false)
	private UserBaseInfo custUser;


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

	/**
	 * 录单人
	 */
	@Column(name = "ss_recorder")
	private String recorder;

	/**
	 * 发货明细
	 */
	@OneToMany(mappedBy = "send", cascade = { CascadeType.REFRESH, CascadeType.PERSIST, CascadeType.REMOVE, CascadeType.MERGE }, fetch = FetchType.EAGER)
	@OrderBy("number")
	private Set<SaleSendItem> sendItems;

	/**
	 * （对买家）传输状态
	 */
	@Column(name = "ss_sendstatus")
	private Short sendStatus;

	/**
	 * （对卖家）传输状态
	 */
	@Column(name = "ss_backstatus")
	private Short backStatus;

	/**
	 * 打印次数
	 */
	@Column(name = "ss_print")
	private Short print;

	/**
	 * 收料状态
	 * 0 未收料，1 未收料
	 */
	@Column(name = "ss_verifystatus")
	private Short verifystatus;

	/**
	 * 每次产生送货单时记录一个唯一标识，防止重复
	 */
	@Column(name = "ss_uuid", unique = true)
	@NotNull
	private String uuid;

	public SaleSend() {
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getCode() {
		return code;
	}

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

	public Long getEnUU() {
		return enUU;
	}

	public void setEnUU(Long enUU) {
		this.enUU = enUU;
	}

	public String getPayments() {
		return payments;
	}

	public void setPayments(String payments) {
		this.payments = payments;
	}

	public String getCurrency() {
		return currency;
	}

	public void setCurrency(String currency) {
		this.currency = currency;
	}

	public Float getRate() {
		return rate;
	}

	public void setRate(Float rate) {
		this.rate = rate;
	}

	public Long getCustUU() {
		return custUU;
	}

	public void setCustUU(Long custUU) {
		this.custUU = custUU;
	}

	public Long getCustUserUU() {
		return custUserUU;
	}

	public void setCustUserUU(Long custUserUU) {
		this.custUserUU = custUserUU;
	}

	public String getRemark() {
		return remark;
	}

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

	@JsonIgnore
	@JSONField(serialize = false)
	public Set<SaleSendItem> getSendItems() {
		return sendItems;
	}

	public void setSendItems(Set<SaleSendItem> sendItems) {
		this.sendItems = sendItems;
	}

	public Short getSendStatus() {
		return sendStatus;
	}

	public void setSendStatus(Short sendStatus) {
		this.sendStatus = sendStatus;
	}

	public Short getBackStatus() {
		return backStatus;
	}

	public void setBackStatus(Short backStatus) {
		this.backStatus = backStatus;
	}

	public String getRecorder() {
		return recorder;
	}

	public void setRecorder(String recorder) {
		this.recorder = recorder;
	}

	public Date getDate() {
		return date;
	}

	public void setDate(Date date) {
		this.date = date;
	}

	public Short getVerifystatus() {
		return verifystatus;
	}

	public void setVerifystatus(Short verifystatus) {
		this.verifystatus = verifystatus;
	}

	public Short getPrint() {
		return print;
	}

	public void setPrint(Short print) {
		this.print = print;
	}

	public EnterpriseInfo getVendor() {
		return vendor;
	}

	public void setVendor(EnterpriseInfo vendor) {
		this.vendor = vendor;
	}

	public EnterpriseInfo getCustomer() {
		return customer;
	}

	public void setCustomer(EnterpriseInfo customer) {
		this.customer = customer;
	}

	public UserBaseInfo getCustUser() {
		return custUser;
	}

	public void setCustUser(UserBaseInfo custUser) {
		this.custUser = custUser;
	}

	public String getUuid() {
		return uuid;
	}

	public void setUuid(String uuid) {
		this.uuid = uuid;
	}

	@Override
	public String toString() {
		return "SaleSend{" +
				"id=" + id +
				", code='" + code + '\'' +
				", enUU=" + enUU +
				", vendor=" + vendor +
				", date=" + date +
				", payments='" + payments + '\'' +
				", currency='" + currency + '\'' +
				", rate=" + rate +
				", custUU=" + custUU +
				", customer=" + customer +
				", custUserUU=" + custUserUU +
				", custUser=" + custUser +
				", remark='" + remark + '\'' +
				", recorder='" + recorder + '\'' +
				", sendItems=" + sendItems +
				", sendStatus=" + sendStatus +
				", backStatus=" + backStatus +
				", print=" + print +
				", verifystatus=" + verifystatus +
				", uuid='" + uuid + '\'' +
				'}';
	}

	/**
	 * 送货单详细
	 * 
	 * @return
	 */
	public String sendDescription() {
		if (!CollectionUtils.isEmpty(this.sendItems)) {
			StringBuffer sb = new StringBuffer("送货单号：").append(this.code).append("【");
			for (SaleSendItem item : this.sendItems) {
                sb.append("行：").append(item.getNumber()).append("，数量：").append(item.getQty()).append("；");
            }
			return sb.append("】").toString();
		}
		return null;
	}

}
