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

import javax.persistence.*;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 平台里面采购询价单详情
 * 
 * @author yingp
 *
 */
@Table(name = PurcInquiryItemSimpleInfo2.TABLE_NAME)
@Entity
public class PurcInquiryItemSimpleInfo2 {

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

	public static final String ID_FIELD = "id_id";

	public static final String PRODUCT_FIELD = "id_prid";

	public static final String INQUIRY_FIELD = "id_inid";

	public static final String OVERDUE_FIELD = "id_overdue";

	public static final String ISOPEN_FIELD = "id_isopen";

	public static final String STATUS_FIELD = "id_status";

	public static final String AGREED_FIELD = "id_agreed";

	public static final String DATE_FIELD = "id_date";

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

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

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

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

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

	/**
	 * {未回复、已回复}
	 */
	@Column(name = STATUS_FIELD)
	private Short status;

	/**
	 * 是否采纳
	 */
	@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 PurcInquirySimpleInfo2 getInquiry() {
		return inquiry;
	}

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

	public Short getIsOpen() {
		return isOpen;
	}

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

	public Short getStatus() {
		return status;
	}

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

	public Short getAgreed() {
		return agreed;
	}

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

	public Date getDate() {
		return date;
	}

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

	@SuppressWarnings("deprecation")
	public Short getOverdue() {
		if (this.getInquiry().getEndDate() == 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, 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(INQUIRY_FIELD);
		fields.add(OVERDUE_FIELD);
		fields.add(ISOPEN_FIELD);
		fields.add(STATUS_FIELD);
		return fields;
	}

	@Override
	public String toString() {
		return "PurcInquiryItemSimpleInfo2{" +
				"id=" + id +
				", product=" + product +
				", inquiry=" + inquiry +
				", id_overdue=" + id_overdue +
				", isOpen=" + isOpen +
				", status=" + status +
				", agreed=" + agreed +
				", date=" + date +
				'}';
	}


}
