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

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;

/**
 * 主动报价单
 * 
 * @author dongbw
 * @since 2016年11月11日 17:42:49
 */

@Entity
@Table(name = SaleQuotationSimpleInfo.TABLE_NAME)
public class SaleQuotationSimpleInfo {
	/**
	 * 表名
	 */
	public static final String TABLE_NAME = "SALE$QUOTATION";

	/**
	 * 各字段列名
	 * 
	 */
	public static final String ID_FIELD = "qu_id";

	public static final String CODE_FIELD = "qu_code";

	public static final String STATUS_FIELD = "qu_status";

	public static final String DATE_FIELD = "qu_date";

	public static final String ENDDATE_FIELD = "qu_enddate";

	public static final String ENTERPRISE_FIELD = "qu_enuu";

	public static final String VEND_FIELD = "qu_custuu";

	public static final String ITEMS_FIELD = "qd_quid";

	public static final String OVERDUE_FIELD = "qu_overdue";

	public static final String AGREED_FIELD = "qu_agreed";

	@Id
	@Column(name = ID_FIELD)
	private Long id;

	/**
	 * 报价单流水号
	 * 
	 */
	@Column(name = CODE_FIELD)
	private String code;

	/**
	 * 处理状态（101已提交、100未提交、314已作废）
	 */
	@Column(name = STATUS_FIELD)
	private Short status;

	/**
	 * 日期
	 */
	@Column(name = DATE_FIELD)
	private Date date;

	/**
	 * 有效截止日期
	 */
	@Column(name = ENDDATE_FIELD)
	private Date endDate;

	/**
	 * 客户是否已同意
	 */
	@Column(name = AGREED_FIELD)
	private Short agreed;

	/**
	 * 企业UU
	 */
	@OneToOne
	@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<SaleQuotationItemSimpleInfo> saleQuotationItems;

	/**
	 * 是否过期（过了截止日期为1，没过为0）-- 作为是否失效的判断条件。
	 */
	private Short qu_overdue;

	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 EnterpriseSimpleInfo getVend() {
		return vend;
	}

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

	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 Set<SaleQuotationItemSimpleInfo> getSaleQuotationItems() {
		return saleQuotationItems;
	}

	public void setSaleQuotationItems(Set<SaleQuotationItemSimpleInfo> saleQuotationItems) {
		this.saleQuotationItems = saleQuotationItems;
	}

	public Date getEndDate() {
		return endDate;
	}

	public void setEndDate(Date endDate) {
		this.endDate = endDate;
	}

	public Short getAgreed() {
		return agreed;
	}

	public void setAgreed(Short agreed) {
		this.agreed = agreed;
	}

	@SuppressWarnings("deprecation")
	public Short getOverdue() {
		if (status == 314) { // 已作废的算作已失效。
			return 1;
		}
		if (status == 100) { // 在录入不算已失效
			return 0;
		}
		if (endDate == null) {
			return 0;
		}
		Date before = new Date();
		Date date = new Date(before.getTime());
		Calendar now = Calendar.getInstance();
		now.set(Calendar.YEAR, date.getYear());
		now.set(Calendar.MONTH, date.getMonth());
		now.set(Calendar.DAY_OF_MONTH, date.getDate());
		Calendar end = Calendar.getInstance();
		end.set(Calendar.YEAR, endDate.getYear());
		end.set(Calendar.MONTH, endDate.getMonth());
		end.set(Calendar.DAY_OF_MONTH, endDate.getDate());
		if (now.compareTo(end) > 0) {
			return 1;
		} else {
			return 0;
		}
	}

	public void setOverdue(Short overdue) {
		this.qu_overdue = overdue;
	}

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

	@Override
	public String toString() {
		return "SaleQuotationSimpleInfo [id=" + id + ", code=" + code + ", status=" + status + ", date=" + date
				+ ", endDate=" + endDate + ", agreed=" + agreed + ", enterprise=" + enterprise + ", vend=" + vend
				+ ", saleQuotationItems=" + saleQuotationItems + ", qu_overdue=" + qu_overdue + "]";
	}
}
