package com.uas.search.console.b2b.model;

import com.uas.search.util.CollectionUtils;

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

/**
 * 平台里面，以供应商的角度来查看采购订单
 * 
 * @author yingp
 *
 */
@Entity
@Table(name = PurchaseOrderSimpleInfo.TABLE_NAME)
public class PurchaseOrderSimpleInfo {

	/**
	 * 表名
	 */
	// TODO
	// 必须与com.uas.search.b2b.service.SearchService.Table_name对应的单据类型的value保持一致
	public static final String TABLE_NAME = "PURC$ORDERS";

	/**
	 * 各字段列名（也是存储索引时的field名）
	 */
	public static final String ID_FIELD = "pu_id";

	public static final String CODE_FIELD = "pu_code";

	public static final String STATUS_FIELD = "pu_status";

	public static final String DISPLAY_FIELD = "pu_display";

	public static final String DATE_FIELD = "pu_date";

	public static final String ENTERPRISE_FIELD = "pu_enuu";

	public static final String VEND_FIELD = "pu_venduu";

	public static final String ITEMS_FIELD = "pd_puid";

	public static final String END_FIELD = "pu_end";

	public static final String REPLY_FIELD = "pu_reply";

	public static final String USERUU_FIELD = "pu_useruu";

	@Id
	@Column(name = ID_FIELD)
	// TODO
	// 每个主表必须存在并且命名为id，否则com.uas.search.console.b2b.util.ClassAndTableNameUtils.getId(T)无法正常执行
	// 因此也必须有ID_FIELD变量
	private Long id;

	/**
	 * 采购单号
	 */
	@Column(name = CODE_FIELD)
	private String code;

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

	/**
	 * 1表示该采购单的明细至少有一条全部回复完的情况
	 */
	@Column(name = REPLY_FIELD)
	private Short reply;

	/**
	 * 1表示该采购单的明细至少有一条结案的情况
	 */
	@Column(name = END_FIELD)
	private Short end;

	/**
	 * 显示状态(0 已查看 ， 1 未查看， 2 置顶)
	 */
	@Column(name = DISPLAY_FIELD)
	private Short display;

	/**
	 * 单据归属日期
	 */
	@Column(name = DATE_FIELD)
	private Date date;

	/**
	 * 采购企业
	 */
	@OneToOne(cascade = { CascadeType.REFRESH })
	@JoinColumn(name = ENTERPRISE_FIELD, insertable = false, updatable = false)
	private EnterpriseSimpleInfo enterprise;

	/**
	 * 供应商
	 */
	@OneToOne
	@JoinColumn(name = VEND_FIELD, insertable = false, updatable = false)
	private EnterpriseSimpleInfo vend;

	/**
	 * 采购单明细
	 */
	@OneToMany(cascade = { CascadeType.ALL }, fetch = FetchType.EAGER)
	@JoinColumn(name = ITEMS_FIELD, updatable = false, insertable = false)
	private Set<PurchaseOrderItemSimpleInfo> orderItems;

	/**
	 * 采购员UU号
	 */
	@Column(name = USERUU_FIELD)
	private Long userUU;

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

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

	public Short getDisplay() {
		return display;
	}

	public void setDisplay(Short display) {
		this.display = display;
	}

	public Date getDate() {
		return date;
	}

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

	public EnterpriseSimpleInfo getEnterprise() {
		return enterprise;
	}

	public void setEnterprise(EnterpriseSimpleInfo enterprise) {
		this.enterprise = enterprise;
	}

	public EnterpriseSimpleInfo getVend() {
		return vend;
	}

	public void setVend(EnterpriseSimpleInfo vend) {
		this.vend = vend;
	}

	public Set<PurchaseOrderItemSimpleInfo> getOrderItems() {
		return orderItems;
	}

	public void setOrderItems(Set<PurchaseOrderItemSimpleInfo> orderItems) {
		this.orderItems = orderItems;
	}

	public Long getUserUU() {
		return userUU;
	}

	public void setUserUU(Long userUU) {
		this.userUU = userUU;
	}

	public Short getReply() {
		return reply;
	}

	public void setReply(Short reply) {
		this.reply = reply;
	}

	public Short getEnd() {
		if (null == end || 0 == end) { // 不是已结案状态时，根据数量判断是否是待交货
			List<PurchaseOrderItemSimpleInfo> infos = new ArrayList<>();
			for (PurchaseOrderItemSimpleInfo itemSimpleInfo : this.getOrderItems()) {
				if (itemSimpleInfo.getAcceptQty() < itemSimpleInfo.getQty() + itemSimpleInfo.getReturnQty()) {
					infos.add(itemSimpleInfo);// 有待交货的，就添加到List中
				}
			}
			if (CollectionUtils.isEmpty(infos)) { // 没有待交货的明细时，判断为已结案。
				// return 1; // 返回1的话，在已回复中不会显示，但是已结案状态，有数据库字段pu_end =
				// 1条件过滤也不会显示。
				return 2; // 所有交货完毕，且end不为1的。返回2来做区分。
			}
		}
		return end;
	}

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

	/**
	 * 获取所有可以搜索的字段列名（索引的field名）
	 * 
	 * @return 可以搜索的字段列名
	 */
	public static List<String> getKeywordFields() {
		List<String> fields = new ArrayList<>();
		fields.add(CODE_FIELD);
		fields.add(VEND_FIELD);
		fields.add(ITEMS_FIELD);
		fields.add(ENTERPRISE_FIELD);
		return fields;
	}

	@Override
	public String toString() {
		return "PurchaseOrderSimpleInfo [id=" + id + ", code=" + code + ", status=" + status + ", reply=" + reply
				+ ", end=" + end + ", display=" + display + ", date=" + date + ", enterprise=" + enterprise + ", vend="
				+ vend + ", orderItems=" + orderItems + "]";
	}

}
