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.ManyToOne;
import javax.persistence.OneToOne;
import javax.persistence.Table;

/**
 * 平台里面，以供应商的角度来查看采购询价单明细
 * 
 * @author yingp
 *
 */
@Table(name = PurchaseInquiryItemSimpleInfo.TABLE_NAME)
@Entity
public class PurchaseInquiryItemSimpleInfo {

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

	public static final String ID_FIELD = "id_id";

	public static final String PRODUCT_FIELD = "id_prid";

	public static final String VEND_FIELD = "id_venduu";

	public static final String SENDSTATUS_FIELD = "id_sendstatus";

	public static final String INQUIRY_FIELD = "id_inid";

	public static final String STATUS_FIELD = "id_status";

	public static final String VENDTODATE_FIELD = "id_vendtodate";

	public static final String OVERDUE_FIELD = "id_overdue";

	public static final String INVALID_FIELD = "id_invalid";

	public static final String SOURCEAPP_FIELD = "id_sourceapp";

	public static final String AGREED_FIELD = "id_agreed";

	public static final String DATE_FIELD = "id_date";

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

	/**
	 * 企业UU
	 */
	@OneToOne
	@JoinColumn(name = VEND_FIELD, insertable = false, updatable = false)
	private EnterpriseSimpleInfo enterprise;

	/**
	 * 产品
	 */
	@OneToOne(cascade = { CascadeType.REFRESH })
	@JoinColumn(name = PRODUCT_FIELD, insertable = false, updatable = false)
	private ProductSimpleInfo product;

	/**
	 * (针对卖家的)询价传输状态{待上传、已下载}
	 */
	@Column(name = SENDSTATUS_FIELD)
	private Short sendStatus;

	/**
	 * 报价有效期截止
	 */
	@Column(name = VENDTODATE_FIELD)
	private Date vendToDate;

	/**
	 * 询价单
	 */
	@ManyToOne(cascade = CascadeType.ALL, optional = true)
	@JoinColumn(name = INQUIRY_FIELD, nullable = false)
	private PurchaseInquirySimpleInfo inquiry;

	/**
	 * 状态
	 * 
	 * @return
	 */
	@Column(name = STATUS_FIELD)
	private Short status;

	/**
	 * 是否过期（1过期，0未过期）--1为已失效，0为未失效
	 * 
	 * @return
	 */
	private Short id_overdue;

	/**
	 * 报价是否过期 (1为报价失效，0为为未效)
	 * 
	 * @return
	 */
	private Short id_invalid;

	/**
	 * 应用来源
	 */
	@Column(name = SOURCEAPP_FIELD)
	private String sourceapp;

	/**
	 * 是否采纳
	 */
	@Column(name = AGREED_FIELD)
	private Short agreed;

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

	public Long getId() {
		return id;
	}

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

	public ProductSimpleInfo getProduct() {
		return product;
	}

	public void setProduct(ProductSimpleInfo product) {
		this.product = product;
	}

	public Short getSendStatus() {
		return sendStatus;
	}

	public void setSendStatus(Short sendStatus) {
		this.sendStatus = sendStatus;
	}

	public EnterpriseSimpleInfo getEnterprise() {
		return enterprise;
	}

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

	public PurchaseInquirySimpleInfo getInquiry() {
		return inquiry;
	}

	public void setInquiry(PurchaseInquirySimpleInfo inquiry) {
		this.inquiry = inquiry;
	}

	public Short getStatus() {
		return status;
	}

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

	public String getSourceapp() {
		return sourceapp;
	}

	public void setSourceapp(String sourceapp) {
		this.sourceapp = sourceapp;
	}

	public Short getAgreed() {
		return agreed;
	}

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

	public Date getVendToDate() {
		return vendToDate;
	}

	public void setVendToDate(Date vendToDate) {
		this.vendToDate = vendToDate;
	}

	public Date getDate() {
		return this.getInquiry().getDate();
	}

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

	@SuppressWarnings("deprecation")
	public Short getOverdue() {
		if (this.status == 314 || this.getInvalid() == 1
				|| (this.status == 200 && null != this.getInquiry().getCheck() && this.getInquiry().getCheck() == 1)) { // 已作废、过了报价有效期、未报价但是客户已提交的
			return 1;
		}
		if (this.getInquiry().getEndDate() == null) {
			return 0;
		}
		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, this.getInquiry().getEndDate().getYear());
		end.set(Calendar.MONTH, this.getInquiry().getEndDate().getMonth());
		end.set(Calendar.DAY_OF_MONTH, this.getInquiry().getEndDate().getDate());
		if (now.compareTo(end) > 0 && this.status == 200) { // 过了截止时间未报价
			return 1;
		} else {
			return 0;
		}
	}

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

	@SuppressWarnings("deprecation")
	public Short getInvalid() {
		if (this.vendToDate == 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, this.vendToDate.getYear());
		end.set(Calendar.MONTH, this.vendToDate.getMonth());
		end.set(Calendar.DAY_OF_MONTH, this.vendToDate.getDate());
		if (now.compareTo(end) > 0) {
			return 1;
		} else {
			return 0;
		}
	}

	public void setInvalid(Short invalid) {
		this.id_invalid = invalid;
	}

	public static List<String> getKeywordFields() {
		List<String> fields = new ArrayList<>();
		fields.add(PRODUCT_FIELD);
		fields.add(VEND_FIELD);
		fields.add(SENDSTATUS_FIELD);
		fields.add(INQUIRY_FIELD);
		fields.add(SOURCEAPP_FIELD);
		return fields;
	}

	@Override
	public String toString() {
		return "PurchaseInquiryItemSimpleInfo{" +
				"id=" + id +
				", enterprise=" + enterprise +
				", product=" + product +
				", sendStatus=" + sendStatus +
				", vendToDate=" + vendToDate +
				", inquiry=" + inquiry +
				", status=" + status +
				", id_overdue=" + id_overdue +
				", id_invalid=" + id_invalid +
				", sourceapp='" + sourceapp + '\'' +
				", agreed=" + agreed +
				", date=" + date +
				'}';
	}


}
