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 = PurchaseTenderSimpleInfo.TABLE_NAME)
@Entity
public class PurchaseTenderSimpleInfo {

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

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

	public static final String CODE_FIELD = "pt_code";

	public static final String ENTERPRISE_FIELD = "pt_enuu";

	public static final String TITLE_FIELD = "pt_title";

	public static final String DATE_FIELD = "pt_date";

	public static final String ENDDATE_FIELD = "pt_enddate";
	
	public static final String PUBLISHDATE_FIELD = "pt_publishdate";
	
	public static final String USER_FIELD = "pt_user";
	
	public static final String USERTEL_FIELD = "pt_usertel";

	public static final String STATUS_FIELD = "pt_status";

//	public static final String OVERDUE_FIELD = "pt_overdue";

	public static final String SHIPADDRESS_FIELD = "pt_shipaddress";

	public static final String CURRENCY_FIELD = "pt_currency";
	
	public static final String PAYMENT_FIELD = "pt_payment";
	
	public static final String CERTIFICATE_FIELD = "pt_certificate";
	
	public static final String ISPUBLISH_FIELD = "pt_ispublish";
	
	public static final String IFOPEN_FIELD = "pt_ifopen";

	public static final String AUDITSTATUS_FIELD = "pt_auditstatus";

	public static final String TENDERPROD_FIELD = "tp_ptid";
	
	public static final String OVERDUE_FIELD = "pt_overdue"; // 过了投标截止日期为1 ，未过是0
	
	public static final String RESULT_FIELD = "pt_result"; // 过了公布结果日期为1， 未过是0
	
	@Id
	@Column(name = ID_FIELD)
	private Long id;

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

	/**
	 * 单据状态
	 */
	@Column(name = STATUS_FIELD)
	private String status;

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

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

	/**
	 * 公布结果日期
	 */
	@Column(name = PUBLISHDATE_FIELD)
	private Date publishDate;

	/**
	 * 付款方式
	 */
	@Column(name = PAYMENT_FIELD)
	private String payment;

	/**
	 * 送货地址
	 */
	@Column(name = SHIPADDRESS_FIELD)
	private String shipAddress;

	/**
	 * 招标标题
	 */
	@Column(name = TITLE_FIELD)
	private String title;
	
	/**
	 * 用户
	 */
	@Column(name = USER_FIELD)
	private String user;
	
	/**
	 * 用户电话
	 */
	@Column(name = USERTEL_FIELD)
	private String userTel;
	
	
	/**
	 * 币别
	 */
	@Column(name = CURRENCY_FIELD)
	private String currency;
	
	/**
	 * 证照要求
	 */
	@Column(name = CERTIFICATE_FIELD)
	private String certificate;
	
	/**
	 * 是否开放（1为开放，0为针对专门供应商）
	 */
	@Column(name = IFOPEN_FIELD)
	private Short ifOpen;
	
	/**
	 * 审批状态（1为已审批，0为已提交，null为不在审批流）
	 */
	@Column(name = AUDITSTATUS_FIELD)
	private Short auditStatus;

	/**
	 * 是否发布（1为发布，0为保存）
	 */
	@Column(name = ISPUBLISH_FIELD)
	private Short isPublish;

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

	/**
	 * 产品明细
	 */
	@OneToMany(cascade = { CascadeType.ALL}, fetch = FetchType.EAGER)
	@JoinColumn(name = TENDERPROD_FIELD, updatable = false, insertable = false)
	private Set<PurchaseTenderProdSimpleInfo> tenderProds;

	/**
	 * 是否截止投标
	 */
	private Short pt_overdue;
	
	/**
	 * 是否公布结果
	 */
	private Short pt_result;

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

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

	public void setIfOpen(Short ifOpen) {
		this.ifOpen = ifOpen;
	}

	public void setIsPublish(Short isPublish) {
		this.isPublish = isPublish;
	}

	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 Date getPublishDate() {
		return publishDate;
	}

	public void setPublishDate(Date publishDate) {
		this.publishDate = publishDate;
	}

	public String getPayment() {
		return payment;
	}

	public void setPayment(String payment) {
		this.payment = payment;
	}

	public String getShipAddress() {
		return shipAddress;
	}

	public void setShipAddress(String shipAddress) {
		this.shipAddress = shipAddress;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getUser() {
		return user;
	}

	public void setUser(String user) {
		this.user = user;
	}

	public String getUserTel() {
		return userTel;
	}

	public void setUserTel(String userTel) {
		this.userTel = userTel;
	}


	public String getCurrency() {
		return currency;
	}

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

	public String getCertificate() {
		return certificate;
	}

	public void setCertificate(String certificate) {
		this.certificate = certificate;
	}

	public Short getIfOpen() {
		return ifOpen;
	}

	public Short getIsPublish() {
		return isPublish;
	}

	public EnterpriseSimpleInfo getEnterprise() {
		return enterprise;
	}

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

	public Set<PurchaseTenderProdSimpleInfo> getTenderProds() {
		return tenderProds;
	}

	public void setTenderProds(Set<PurchaseTenderProdSimpleInfo> tenderProds) {
		this.tenderProds = tenderProds;
	}

	public Short getAuditStatus() {
		return auditStatus;
	}

	public void setAuditStatus(Short auditStatus) {
		this.auditStatus = auditStatus;
	}

	@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.pt_overdue = overdue;
	}
	
	@SuppressWarnings("deprecation")
	public Short getResult() {
		if (publishDate == null) {
			return null;
		}
		Date date = new Date();
		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, publishDate.getYear());
		end.set(Calendar.MONTH, publishDate.getMonth());
		end.set(Calendar.DAY_OF_MONTH, publishDate.getDate());
		if (now.compareTo(end) >= 0) {
			return 1;
		} else {
			return 0;
		}
	}

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




	@Override
	public String toString() {
		return "PurchaseTenderSimpleInfo [id=" + id + ", code=" + code + ", status=" + status + ", date=" + date
				+ ", endDate=" + endDate + ", publishDate=" + publishDate + ", payment=" + payment + ", shipAddress="
				+ shipAddress + ", title=" + title + ", user=" + user + ", userTel=" + userTel + ", currency="
				+ currency + ", certificate=" + certificate + ", ifOpen=" + ifOpen + ", auditStatus=" + auditStatus
				+ ", isPublish=" + isPublish + ", enterprise=" + enterprise + ", tenderProds=" + tenderProds
				+ ", pt_overdue=" + pt_overdue + ", pt_result=" + pt_result + "]";
	}

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

}
