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

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

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

@Table(name = ErpSaleTenderSimpleInfo.TABLE_NAME)
@Entity
public class ErpSaleTenderSimpleInfo {

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

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

	public static final String CODE_FIELD = "st_code";

	public static final String ENTERPRISE_FIELD = "st_enuu";

	public static final String VEND_FIELD = "st_venduu";

	public static final String TITLE_FIELD = "st_title";

	public static final String DATE_FIELD = "st_date";

	public static final String ENDDATE_FIELD = "st_enddate";

	public static final String PUBLISHDATE_FIELD = "st_publishdate";

	public static final String USER_FIELD = "st_user";

	public static final String USERTEL_FIELD = "st_usertel";

	public static final String STATUS_FIELD = "st_status";

	public static final String PAYMENT_FIELD = "st_payment";

	public static final String SHIPADDRESS_FIELD = "st_shipaddress";

	public static final String IFOPEN_FIELD = "st_ifopen";

	public static final String ISPUBLISH_FIELD = "st_ispublish";

	public static final String OVERDUE_FIELD = "st_overdue"; // 过了投标截止日期为1 ，未过是0

	public static final String RESULT_FIELD = "st_result"; // 过了公布结果日期为1， 未过是0

	public static final String AUDITSTATUS_FIELD = "st_auditstatus";

	@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 = SHIPADDRESS_FIELD)
	private String shipAddress;

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

	/**
	 * 招标标题
	 */
	@Column(name = TITLE_FIELD)
	private String title;

	/**
	 * 用户
	 */
	@Column(name = USER_FIELD)
	private String user;

	/**
	 * 用户电话
	 */
	@Column(name = USERTEL_FIELD)
	private String userTel;

	/**
	 * 是否开放（1为开放，0为针对专门供应商）
	 */
	@Column(name = IFOPEN_FIELD)
	private Short ifOpen;

	public Short getIsPublish() {
		return isPublish;
	}

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

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

	/**
	 * 审核状态（0为已提交，1为已审核， null为不在审批流）
	 */
	@Column(name = AUDITSTATUS_FIELD)
	private Short auditStatus;


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

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

	/**
	 * 是否截止投标
	 */
	private Short st_overdue;

	/**
	 * 是否公布结果
	 */
	private Short st_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 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 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 getPayment() {
		return payment;
	}

	public EnterpriseSimpleInfo getVendor() {
		return vendor;
	}

	public void setVendor(EnterpriseSimpleInfo vendor) {
		this.vendor = vendor;
	}

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

	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 Short getIfOpen() {
		return ifOpen;
	}

	public EnterpriseSimpleInfo getEnterprise() {
		return enterprise;
	}

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

	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.st_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.st_overdue = overdue;
	}

	/**
	 * 获取所有可以搜索的字段列名（索引的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(ENTERPRISE_FIELD);
		return fields;
	}

	@Override
	public String toString() {
		return "ErpSaleTenderSimpleInfo [id=" + id + ", code=" + code + ", status=" + status + ", date=" + date
				+ ", endDate=" + endDate + ", publishDate=" + publishDate + ", shipAddress=" + shipAddress
				+ ", payment=" + payment + ", title=" + title + ", user=" + user + ", userTel=" + userTel + ", ifOpen="
				+ ifOpen + ", isPublish=" + isPublish + ", auditStatus=" + auditStatus + ", enterprise=" + enterprise
				+ ", vendor=" + vendor + ", st_overdue=" + st_overdue + ", st_result=" + st_result + "]";
	}

}
