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 OVERDUE_FIELD = "id_overdue";


	@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;

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

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

	/**
	 * 是否过期
	 * 
	 * @return
	 */
	private Short id_overdue;

	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;
	}

	@SuppressWarnings("deprecation")
	public Short getOverdue() {
		if (this.getInquiry().getEndDate() == 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, 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) {
			return 1;
		} else {
			return 0;
		}
	}

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

	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);
		return fields;
	}

	@Override
	public String toString() {
		return "PurchaseInquiryItemSimpleInfo [id=" + id + ", enterprise=" + enterprise + ", product=" + product
				+ ", sendStatus=" + sendStatus + ", inquiry=" + inquiry + ", status=" + status + ", id_overdue="
				+ id_overdue + "]";
	}

}
