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;
import javax.persistence.Transient;

/**
 * 平台新增询价单
 * 
 * @author hejq
 * @time 创建时间：2017年4月28日
 */
@Entity
@Table(name = PurcInquirySimpleInfo.TABLE_NAME)
public class PurcInquirySimpleInfo {

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

	public static final String ID_FIELD = "in_id";

	public static final String RECORDERUU_FIELD = "in_recorderuu";

	public static final String CODE_FIELD = "in_code";

	public static final String DATE_FIELD = "in_date";

	public static final String RECORDER_FIELD = "in_recorder";

	public static final String ENDDATE_FIELD = "in_enddate";

	public static final String ENTERYSTATUS_FIELD = "in_enterystatus";

	public static final String ISOPEN_FIELD = "in_isopen";

	public static final String ITEMS_FIELD = "id_inid";

	public static final String ENTERPRISE_FIELD = "in_enuu";
	@Id
	@Column(name = ID_FIELD)
	private Long id;

	/**
	 * 询价单所属用户UU
	 */
	@Column(name = RECORDERUU_FIELD)
	private Long recorderUU;

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

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

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

	/**
	 * 录入人
	 */
	@Column(name = RECORDER_FIELD)
	private String recorder;

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

	/**
	 * 询价明细
	 */
	@OneToMany(cascade = { CascadeType.ALL }, fetch = FetchType.EAGER)
	@JoinColumn(name = ITEMS_FIELD, updatable = false, insertable = false)
	private Set<PurcInquiryItemSimpleInfo> inquiryItems;

	/**
	 * 单据状态（已提交、在录入)
	 */
	@Column(name = ENTERYSTATUS_FIELD)
	private Integer enteryStatus;

	/**
	 * 是否过期
	 */
	@Transient
	private Short overdue;

	/**
	 * 是否公开
	 */
	@Column(name = "in_isopen")
	private Short isOpen;

	public Long getId() {
		return id;
	}

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

	public Long getRecorderUU() {
		return recorderUU;
	}

	public void setRecorderUU(Long recorderUU) {
		this.recorderUU = recorderUU;
	}

	public EnterpriseSimpleInfo getEnterprise() {
		return enterprise;
	}

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

	public String getCode() {
		return code;
	}

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

	public Date getDate() {
		return date;
	}

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

	public String getRecorder() {
		return recorder;
	}

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

	public Date getEndDate() {
		return endDate;
	}

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

	public Set<PurcInquiryItemSimpleInfo> getInquiryItems() {
		return inquiryItems;
	}

	public void setInquiryItems(Set<PurcInquiryItemSimpleInfo> inquiryItems) {
		this.inquiryItems = inquiryItems;
	}

	public Integer getEnteryStatus() {
		return enteryStatus;
	}

	public void setEnteryStatus(Integer enteryStatus) {
		this.enteryStatus = enteryStatus;
	}

	@SuppressWarnings("deprecation")
	public Short getOverdue() {
		if (this.getEndDate() == null) {
			return null;
		}
		// 从页面选择的时间是零点，应该要当晚24点才是过期
		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, this.getEndDate().getYear());
		end.set(Calendar.MONTH, this.getEndDate().getMonth());
		end.set(Calendar.DAY_OF_MONTH, this.getEndDate().getDate());
		if (now.compareTo(end) > 0) {
			return 1;
		} else {
			return 0;
		}
	}

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

	public Short getIsOpen() {
		return isOpen;
	}

	public void setIsOpen(Short isOpen) {
		this.isOpen = isOpen;
	}

	public static List<String> getKeywordFields() {
		List<String> fields = new ArrayList<>();
		fields.add(CODE_FIELD);
		fields.add(RECORDER_FIELD);
		fields.add(RECORDERUU_FIELD);
		fields.add(ENTERPRISE_FIELD);
		return fields;
	}

	@Override
	public String toString() {
		return "PurcInquirySimpleInfo [id=" + id + ", recorderUU=" + recorderUU + ", enterprise=" + enterprise
				+ ", code=" + code + ", date=" + date + ", recorder=" + recorder + ", endDate=" + endDate
				+ ", inquiryItems=" + inquiryItems + ", enteryStatus=" + enteryStatus + ", overdue=" + overdue
				+ ", isOpen=" + isOpen + "]";
	}

}
