package com.ecep.contract.ds.project.model;

import java.time.LocalDateTime;
import java.util.Objects;

import com.ecep.contract.ds.customer.model.CompanyCustomerEvaluationFormFile;
import com.ecep.contract.model.Employee;
import com.ecep.contract.model.IdentityEntity;
import com.ecep.contract.model.Voable;
import org.hibernate.annotations.ColumnDefault;

import com.ecep.contract.util.HibernateProxyUtils;
import com.ecep.contract.vo.ProjectQuotationVo;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.Table;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

/**
 * 项目报价
 * <p>
 * Approval 审批
 */
@Getter
@Setter
@Entity
@Table(name = "PROJECT_QUOTATION")
@ToString
public class ProjectQuotation implements IdentityEntity, ProjectBasedEntity, Voable<ProjectQuotationVo> {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "ID", nullable = false)
    private Integer id;

    /**
     * 项目
     */
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "PROJECT_ID")
    @ToString.Exclude
    private Project project;

    /**
     * 客户资信等级
     */
    @Column(name = "LEVEL", nullable = false)
    private int level;
    /**
     * 是否标准付款方式
     */
    @ColumnDefault("0")
    @Column(name = "STANDARD_PAY_WAY", nullable = false)
    private boolean standardPayWay;
    /**
     * 非标准付款方式文本
     */
    @Column(name = "NO_STANDARD_PAY_WAY_TEXT")
    private String noStandardPayWayText;
    /**
     * 报价金额
     */
    @Column(name = "AMOUNT")
    private double amount;
    /**
     * 申请人
     */
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "APPLICANT_ID")
    @ToString.Exclude
    private Employee applicant;
    /**
     * 申请日期
     */
    @Column(name = "APPLY_DATE")
    private LocalDateTime applyTime;
    /**
     * 审核人
     */
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "AUTHORIZER_ID")
    @ToString.Exclude
    private Employee authorizer;
    /**
     * 审核时间
     */
    @Column(name = "AUTHORIZER_DATE")
    private LocalDateTime authorizationTime;

    /**
     * 审核文件
     */
    @Column(name = "AUTHORIZER_FILE")
    private String authorizationFile;

    /**
     * 说明
     */
    @Column(name = "DESCRIPTION", columnDefinition = "TEXT")
    private String description;

    /**
     * 评价表单文件
     */
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "CUS_EVAL_FILE_ID")
    @ToString.Exclude
    private CompanyCustomerEvaluationFormFile evaluationFile;

    @Override
    public boolean equals(Object object) {
        if (this == object) {
            return true;
        }
        if (object == null) {
            return false;
        }
        if (HibernateProxyUtils.isNotEffectiveClassEquals(object, this)) {
            return false;
        }
        ProjectQuotation that = (ProjectQuotation) object;
        return getId() != null && Objects.equals(getId(), that.getId());
    }

    @Override
    public final int hashCode() {
        return HibernateProxyUtils.hashCode(this);
    }

    @Override
    public ProjectQuotationVo toVo() {
        ProjectQuotationVo vo = new ProjectQuotationVo();
        vo.setId(id);
        if (project != null) {
            vo.setProject(project.getId());
        }
        vo.setLevel(level);
        vo.setStandardPayWay(standardPayWay);
        vo.setNoStandardPayWayText(noStandardPayWayText);
        vo.setAmount(amount);
        if (applicant != null) {
            vo.setApplicantId(applicant.getId());
        }
        vo.setApplyTime(applyTime);
        if (authorizer != null) {
            vo.setAuthorizerId(authorizer.getId());
        }
        vo.setAuthorizationTime(authorizationTime);
        vo.setAuthorizationFile(authorizationFile);
        if (evaluationFile != null) {
            vo.setEvaluationFileId(evaluationFile.getId());
        }
        vo.setDescription(description);
        // active字段默认为false，在ProjectQuotationVo类中已经设置
        return vo;
    }
}
