package com.uas.platform.b2b.model;

import com.uas.platform.b2b.core.util.DateUtils;
import com.uas.platform.core.model.Constant;
import com.uas.platform.core.model.Status;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.OrderBy;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;
import java.io.Serializable;
import java.util.Date;
import java.util.Set;

/**
 * 模具询价
 *
 * <pre>
 * CREATE OR REPLACE FORCE VIEW V$PURC$INQUIRYMOULD
 * SELECT purc$inquirymould."IM_ID",purc$inquirymould."IM_CODE",purc$inquirymould."IM_CURRENCY", purc$inquirymould."IM_ADOPTSTATUS", purc$inquirymould."IM_AUDITDATE", purc$inquirymould."IM_AUDITOR", purc$inquirymould."IM_DATE", purc$inquirymould."IM_ENDDATE", purc$inquirymould."IM_ENUU", purc$inquirymould."IM_ERPSOURCEID", purc$inquirymould."IM_RECORDDATE", purc$inquirymould."IM_RECORDER",purc$inquirymould."IM_RECORDERID", purc$inquirymould."IM_REMARK", purc$inquirymould."IM_SENDSTATUS", purc$inquirymould."IM_SOURCE", purc$inquirymould."IM_SOURCEID", purc$inquirymould."IM_SOURCETYPE", purc$inquirymould."IM_STATUS", purc$inquirymould."IM_STATUSCODE", purc$inquirymould."IM_TAXRATE", purc$inquirymould."IM_VENDNAME", purc$inquirymould."IM_VENDUU", purc$inquirymould."IM_VALID" ,purc$inquirymould."IM_BACKSTATUS", purc$inquirymould."IM_CHECKED", purc$inquirymould."IM_RECORDERUU", purc$inquirymould."IM_REPLYSENDSTATUS", purc$inquirymould."IM_VENDUSERUU" ,case when trunc(im_enddate) + 1 < sysdate then 1 else 0 end as im_overdue from purc$inquirymould
 * </pre>
 *
 */

@Table(name = "v$purc$inquirymould")
@Entity
public class PurchaseInquiryMouldInfo implements Serializable {

    /**
     * 序列化
     */
    private static final long serialVersionUID = 1L;

    /**
     * id
     */
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "im_id")
    private Long id;

    /**
     * 询价单号
     */
    @Column(name = "im_code")
    private String code;

    /**
     * 日期
     */
    @Column(name = "im_date")
    private Date date;

    /**
     * 供应商UU
     */
    @NotNull
    @Column(name = "im_venduu")
    private Long venduu;

    /**
     * 供应商名称
     */
    @Column(name = "im_vendname")
    private String vendName;

    /**
     * 供应商企业信息
     */
    @OneToOne(cascade = {CascadeType.MERGE, CascadeType.REFRESH})
    @JoinColumn(name = "im_venduu", insertable = false, updatable = false)
    private EnterpriseInfo vend;

    /**
     * 供应商联系人uu
     */
    @Column(name = "im_venduseruu")
    private Long vendUserUU;

    /**
     * 供应商联系人
     */
    @OneToOne(cascade = {CascadeType.MERGE, CascadeType.REFRESH})
    @JoinColumn(name = "im_venduseruu", insertable = false, updatable = false)
    private UserBaseInfo vendUser;

    /**
     * 币别
     */
    @Column(name = "im_currency")
    private String currency;

    /**
     * 税率
     */
    @Column(name = "im_taxrate")
    private Float taxrate;

    /**
     * 备注
     */
    @Column(name = "im_remark")
    private String remark;

    /**
     * 单据状态
     */
    @Column(name = "im_status")
    private Short status;

    /**
     * 所属企业uu
     */
    @NotNull
    @Column(name = "im_enuu")
    private Long enuu;

    /**
     * 当前用户UU
     */
    @Column(name = "im_useruu")
    private Long useruu;

    /**
     * 所属企业
     */
    @OneToOne(cascade = {CascadeType.MERGE, CascadeType.REFRESH})
    @JoinColumn(name = "im_enuu", insertable = false, updatable = false)
    private EnterpriseInfo enterprise;

    /**
     * 来源id
     */
    @Column(name = "im_sourceid")
    private Long sourceId;

    /**
     * 来源单号
     */
    @Column(name = "im_source")
    private String sourceCode;

    /**
     * 来源类型
     */
    @Column(name = "im_sourcetype")
    private String sourceType;

    /**
     * 状态编号
     */
    @Column(name = "im_statuscode")
    private String statusCode;

    /**
     * 审核人
     */
    @Column(name = "im_auditor")
    private String auditor;

    /**
     * 审核日期
     */
    @Column(name = "im_auditdate")
    private Date auditDate;

    /**
     * 录入人
     */
    @Column(name = "im_recorder")
    private String recorder;

    /**
     * 录入日期
     */
    @Column(name = "im_recorddate")
    private Date recordDate;

    /**
     * erp报价单传入日期
     */
    @Column(name = "im_erpindate")
    private Date erpInDate;

    /**
     * 录入人ID
     */
    @Column(name = "im_recorderuu")
    private Long recorderuu;

    /**
     * 报价日期
     */
    @Column(name = "im_replydate")
    private Date replyDate;

    /**
     * 报价截止日期
     */
    @Column(name = "im_enddate")
    private Date endDate;

    /**
     * 采纳状态(已采纳为1，待采纳为0)
     */
    @Column(name = "im_adoptstatus")
    private Short adoptStatus;

    /**
     * 上传状态（待上传202、已下载203）
     */
    @Column(name = "im_sendstatus")
    private Short sendStatus;

    /**
     * (针对买家的)报价信息传输状态{待上传、已下载}
     */
    @Column(name = "im_backstatus")
    private Short backStatus;

    /**
     * (针对卖家的)报价信息传输状态{待上传、已下载}
     */
    @Column(name = "im_replysendstatus")
    private Short replySendStatus;

    /**
     * 客户已提交(1为已提交，0为未提交)
     */
    @Column(name = "im_checked")
    private Short check;

    /**
     * (针对卖家的)是否采纳信息传输状态{待上传、已下载}
     */
    @Column(name = "im_decidestatus")
    private Short decideStatus;

    /**
     * 是否有效（0无效，1有效）
     */
    @Column(name = "im_valid")
    private Short valid;

    /**
     * 买家附件
     */
    @OneToMany(fetch = FetchType.EAGER, cascade = {CascadeType.ALL})
    @JoinTable(name = "purc$inquirymouldattach", joinColumns = @JoinColumn(name = "im_id", referencedColumnName = "im_id"), inverseJoinColumns = @JoinColumn(name = "at_id", referencedColumnName = "at_id"))
    private Set<Attach> attachs;

    /**
     * 卖家附件
     */
    @OneToMany(fetch = FetchType.EAGER, cascade = {CascadeType.ALL})
    @JoinTable(name = "purc$inquirymouldsellattach", joinColumns = @JoinColumn(name = "im_id", referencedColumnName = "im_id"), inverseJoinColumns = @JoinColumn(name = "at_id", referencedColumnName = "at_id"))
    private Set<Attach> sellAttachs;

    /**
     * erp sourceId
     */
    @Column(name = "im_erpsourceid")
    private Long erpSourceId;

    /**
     * 询价明细
     */
    @OneToMany(mappedBy = "inquiryMould", cascade = {CascadeType.REFRESH, CascadeType.PERSIST, CascadeType.REMOVE,
            CascadeType.MERGE}, fetch = FetchType.EAGER)
    @OrderBy("detno")
    private Set<PurchaseInquiryMouldItem> inquiryMouldItems;

    /**
     * 是否过期(0未过期,1已过期)(表中的im_overdue字段只是search项目过滤时报错时建立，无实际意义也不会赋值，
     * 只是用于搜索项目对是否已过期的判断，这里使用视图中的im_overdue来做其他操作)
     */
    @Column(name = "im_overdue", insertable = false, updatable = false)
    private Short overdue;

    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 Date getDate() {
        return date;
    }

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

    public Long getVenduu() {
        return venduu;
    }

    public void setVenduu(Long venduu) {
        this.venduu = venduu;
    }

    public EnterpriseInfo getVend() {
        return vend;
    }

    public void setVend(EnterpriseInfo vend) {
        this.vend = vend;
    }

    public String getCurrency() {
        return currency;
    }

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

    public String getRemark() {
        return remark;
    }

    public void setRemark(String remark) {
        this.remark = remark;
    }

    public Short getStatus() {
        return status;
    }

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

    public Short getBackStatus() {
        return backStatus;
    }

    public void setBackStatus(Short backStatus) {
        this.backStatus = backStatus;
    }

    public Short getReplySendStatus() {
        return replySendStatus;
    }

    public void setReplySendStatus(Short replySendStatus) {
        this.replySendStatus = replySendStatus;
    }

    public Short getCheck() {
        return check;
    }

    public void setCheck(Short check) {
        this.check = check;
    }

    public Long getEnuu() {
        return enuu;
    }

    public void setEnuu(Long enuu) {
        this.enuu = enuu;
    }

    public Long getUseruu() {
        return useruu;
    }

    public void setUseruu(Long useruu) {
        this.useruu = useruu;
    }

    public EnterpriseInfo getEnterprise() {
        return enterprise;
    }

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

    public Long getSourceId() {
        return sourceId;
    }

    public void setSourceId(Long sourceId) {
        this.sourceId = sourceId;
    }

    public String getSourceCode() {
        return sourceCode;
    }

    public void setSourceCode(String sourceCode) {
        this.sourceCode = sourceCode;
    }

    public String getSourceType() {
        return sourceType;
    }

    public void setSourceType(String sourceType) {
        this.sourceType = sourceType;
    }

    public String getStatusCode() {
        return statusCode;
    }

    public void setStatusCode(String statusCode) {
        this.statusCode = statusCode;
    }

    public String getAuditor() {
        return auditor;
    }

    public void setAuditor(String auditor) {
        this.auditor = auditor;
    }

    public Date getAuditDate() {
        return auditDate;
    }

    public void setAuditDate(Date auditDate) {
        this.auditDate = auditDate;
    }

    public String getRecorder() {
        return recorder;
    }

    public void setRecorder(String recorder) {
        this.recorder = recorder;
    }

    public Date getRecordDate() {
        return recordDate;
    }

    public Date getErpInDate() {
        return erpInDate;
    }

    public void setErpInDate(Date erpInDate) {
        this.erpInDate = erpInDate;
    }

    public void setRecordDate(Date recordDate) {
        this.recordDate = recordDate;
    }

    public Long getRecorderuu() {
        return recorderuu;
    }

    public void setRecorderuu(Long recorderuu) {
        this.recorderuu = recorderuu;
    }

    public Date getEndDate() {
        return endDate;
    }

    public void setEndDate(Date endDate) {
        this.endDate = endDate;
    }

    public Date getReplyDate() {
        return replyDate;
    }

    public void setReplyDate(Date replyDate) {
        this.replyDate = replyDate;
    }

    public Short getAdoptStatus() {
        return adoptStatus;
    }

    public void setAdoptStatus(Short adoptStatus) {
        this.adoptStatus = adoptStatus;
    }

    public Short getSendStatus() {
        return sendStatus;
    }

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

    public Set<Attach> getAttachs() {
        return attachs;
    }

    public void setAttachs(Set<Attach> attachs) {
        this.attachs = attachs;
    }

    public Set<Attach> getSellAttachs() {
        return sellAttachs;
    }

    public void setSellAttachs(Set<Attach> sellAttachs) {
        this.sellAttachs = sellAttachs;
    }

    public Long getErpSourceId() {
        return erpSourceId;
    }

    public void setErpSourceId(Long erpSourceId) {
        this.erpSourceId = erpSourceId;
    }

    public String getVendName() {
        return vendName;
    }

    public void setVendName(String vendName) {
        this.vendName = vendName;
    }

    public Long getVendUserUU() {
        return vendUserUU;
    }

    public void setVendUserUU(Long vendUserUU) {
        this.vendUserUU = vendUserUU;
    }

    public Float getTaxrate() {
        return taxrate;
    }

    public void setTaxrate(Float taxrate) {
        this.taxrate = taxrate;
    }

    public Short getOverdue() {
        return overdue;
    }

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

    public Short getValid() {
        return valid;
    }

    public void setValid(Short valid) {
        this.valid = valid;
    }

    public Short getDecideStatus() {
        return decideStatus;
    }

    public void setDecideStatus(Short decideStatus) {
        this.decideStatus = decideStatus;
    }

    public Set<PurchaseInquiryMouldItem> getInquiryMouldItems() {
        return inquiryMouldItems;
    }

    public void setInquiryMouldItems(Set<PurchaseInquiryMouldItem> inquiryMouldItems) {
        this.inquiryMouldItems = inquiryMouldItems;
    }

    public UserBaseInfo getVendUser() {
        return vendUser;
    }

    public void setVendUser(UserBaseInfo vendUser) {
        this.vendUser = vendUser;
    }

    /**
     * 是否可报价
     * <p>
     * 1.未报价，未截止报价
     * </p>
     * <p>
     * 2.已报价，未截止报价，客户未提交
     * </p>
     *
     * @return
     */
    public boolean isReplyable() {
        if (this.getEndDate() != null) {
            return DateUtils.compare(this.getEndDate(), new Date(), DateUtils.COMPARE_DAY) >= 0
                    && (this.status == Status.NOT_REPLY.value() || (this.status == Status.REPLIED.value()
                    && (this.getCheck() == null || this.getCheck() != Constant.YES)
                    && this.adoptStatus == null));
        }
        return this.status == Status.NOT_REPLY.value();
    }

}
