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;

/**
 * 
 * 模具询价单
 *
 */
@Table(name = PurchaseInquiryMouldSimpleInfo.TABLE_NAME)
@Entity
public class PurchaseInquiryMouldSimpleInfo {

	/**
	 * 表名
	 */
	public static final String TABLE_NAME = "PURC$INQUIRYMOULD";

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

	public static final String CODE_FIELD = "im_code";

	public static final String ENTERPRISE_FIELD = "im_enuu";

	public static final String VEND_FIELD = "im_venduu";

	public static final String DATE_FIELD = "im_date";

	public static final String ENDDATE_FIELD = "im_enddate";

	public static final String STATUS_FIELD = "im_status";

	public static final String ADOPTSTATUS_FIELD = "im_adoptstatus";

	public static final String SENDSTATUS_FIELD = "im_sendstatus";

	public static final String OVERDUE_FIELD = "im_overdue";

	public static final String VALID_FIELD = "im_valid";

	public static final String ITEMS_FIELD = "imi_imid";


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

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

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

	/**
	 * 采纳状态
	 */
	@Column(name = ADOPTSTATUS_FIELD)
	private Short adoptstatus;

	/**
	 * 上传状态
	 */
	@Column(name = SENDSTATUS_FIELD)
	private Short sendstatus;

	/**
	 * 是否有效（作废）
	 */
	@Column(name = VALID_FIELD)
	private Short valid;

	/**
	 * 是否过期
	 */
	private Short im_overdue;

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

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

	/**
	 * 采购企业
	 */
	@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<PurchaseInquiryMouldItemSimpleInfo> mouldItems;



	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 getValid() {
		// valid 1为有效，0为已作废。因为已作废和已过期要合并，
		// 所以是已过期时，直接返回0，不是才根据valid值判断
		if (this.getOverdue() == 1 && this.status == 200) {
			return 0;
		}
		return valid;
	}

	public void setValid(Short valid) {
		this.valid = valid;
	}

	public Short getAdoptstatus() {
		return adoptstatus;
	}

	public void setAdoptstatus(Short adoptstatus) {
		this.adoptstatus = adoptstatus;
	}

	public Short getSendstatus() {
		return sendstatus;
	}

	public void setSendstatus(Short sendstatus) {
		this.sendstatus = sendstatus;
	}

	@SuppressWarnings("deprecation")
	public Short getOverdue() {
		if (endDate == null) {
			return null;
		}
		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.im_overdue = overdue;
	}

	public Date getDate() {
		return date;
	}

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

	public Date getEndDate() {
		return endDate;
	}

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

	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<PurchaseInquiryMouldItemSimpleInfo> getMouldItems() {
		return mouldItems;
	}

	public void setMouldItems(Set<PurchaseInquiryMouldItemSimpleInfo> mouldItems) {
		this.mouldItems = mouldItems;
	}


	@Override
	public String toString() {
		return "PurchaseInquiryMouldSimpleInfo [id=" + id + ", code=" + code + ", status=" + status + ", adoptstatus="
				+ adoptstatus + ", sendstatus=" + sendstatus + ", valid=" + valid + ", im_overdue=" + im_overdue
				+ ", date=" + date + ", endDate=" + endDate + ", enterprise=" + enterprise + ", vend=" + vend
				+ ", mouldItems=" + mouldItems + "]";
	}

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

}
