package com.ecep.contract.model;

import java.io.Serializable;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Objects;

import org.hibernate.annotations.ColumnDefault;

import com.ecep.contract.ContractPayWay;
import com.ecep.contract.util.HibernateProxyUtils;

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

/**
 * 合同类
 */
@Getter
@Setter
@Entity
@Table(name = "CONTRACT", schema = "supplier_ms")
@ToString
public class Contract implements IdentityEntity, NamedEntity, BasedEntity, CompanyBasedEntity, Serializable {
    private static final long serialVersionUID = 1L;
    /**
     * 主键
     */
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "ID", nullable = false)
    private Integer id;

    /**
     * GUID，合同在U8系统中的唯一标识
     */
    @Column(name = "GUID")
    private String guid;

    @Column(name = "PAY_WAY")
    @Enumerated(EnumType.ORDINAL)
    private ContractPayWay payWay;

    /**
     * 合同编号，合同编号由录入员在U8系统中录入，同步导入，合同编号规则参考制度文件
     */
    @Column(name = "CODE")
    private String code;

    /**
     * 合同名称，合同名称由录入员在U8系统中录入，同步导入
     */
    @Column(name = "NAME")
    private String name;

    /**
     * 合同状态，U8 系统中的合同状态，目前含义未知
     * <table>
     * <caption>各个状态值的统计情况如下，数据样本日期：2025-02-08</caption>
     * <tr>
     * <th>State</th>
     * <th>Count</th>
     * </tr>
     * <tr>
     * <td>null</td>
     * <td>3891</td>
     * </tr>
     * <tr>
     * <td>A</td>
     * <td>9</td>
     * </tr>
     * <tr>
     * <td>B</td>
     * <td>6499</td>
     * </tr>
     * <tr>
     * <td>C</td>
     * <td>79</td>
     * </tr>
     * </table>
     */
    @Column(name = "STATE")
    private String state;
    /**
     * 合同分组
     */
    // @ManyToOne(fetch = FetchType.EAGER)
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "GROUP_ID")
    private ContractGroup group;
    /**
     * 分类
     */
    // @ManyToOne(fetch = FetchType.EAGER)
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "TYPE_ID")
    private ContractType type;
    /**
     * 性质
     */
    // @ManyToOne(fetch = FetchType.EAGER)
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "KIND_ID")
    private ContractKind kind;

    /**
     * 主合同编号，一般未销售合同，子合同未采购合同（服务合同、分包合同）
     */
    @Column(name = "PARENT_CODE")
    private String parentCode;
    /**
     * 签订日期
     */
    @Column(name = "ORDER_DATE")
    private LocalDate orderDate;
    /**
     * 合同起始日期
     */
    @Column(name = "START_DATE")
    private LocalDate startDate;
    /**
     * 合同截止日期
     */
    @Column(name = "END_DATE")
    private LocalDate endDate;

    /**
     * 创建人
     */
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "SETUP_PERSON_ID")
    @ToString.Exclude
    private Employee setupPerson;
    /**
     * 提交日期
     */
    @Column(name = "SETUP_DATE")
    private LocalDate setupDate;

    /**
     * 生效人
     */
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "INURE_PERSON_ID")
    @ToString.Exclude
    private Employee inurePerson;

    /**
     * 生效日期
     */
    @Column(name = "INURE_DATE")
    private LocalDate inureDate;

    /**
     * 修改人
     */
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "VARY_PERSON_ID")
    @ToString.Exclude
    private Employee varyPerson;
    @Column(name = "VARY_DATE")
    private LocalDate varyDate;

    /**
     * 文件夹路径
     */
    @Column(name = "PATH")
    private String path;

    @Lob
    @Column(name = "DESCRIPTION", columnDefinition = "TEXT")
    private String description;

    /**
     * 创建时间
     */
    @Column(name = "CREATED")
    private LocalDateTime created;

    /**
     * 所属公司
     */
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "COMPANY_ID")
    @ToString.Exclude
    private Company company;

    /**
     * 业务员
     */
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "BM_EMPLOYEE_ID")
    @ToString.Exclude
    private Employee employee;

    /**
     * 经办人
     */
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "BM_HANDLER_ID")
    @ToString.Exclude
    private Employee handler;

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

    /**
     * 合同金额
     */
    @Column(name = "AMOUNT")
    private Double amount;

    /**
     * 是否标准付款方式
     */
    @ColumnDefault("0")
    @Column(name = "STANDARD_PAY_WAY", nullable = false)
    private boolean standardPayWay;

    /**
     * 是否标准合同文本
     */
    @ColumnDefault("0")
    @Column(name = "STANDARD_CONTRACT_TEXT", nullable = false)
    private boolean standardPContractText;

    @Version
    @ColumnDefault("0")
    @Column(name = "VERSION", nullable = false)
    private int version;

    /**
     * 总数量
     */
    @Column(name = "TOTAL_QUANTITY")
    private double totalQuantity;
    /**
     * 总金额
     */
    @Column(name = "TOTAL_AMOUNT")
    private double totalAmount;
    /**
     * 总不含税金额
     */
    @Column(name = "TOTAL_UNTAX_AMOUNT")
    private double totalUnTaxAmount;

    /**
     * 已经执行数量
     */
    @Column(name = "EXEC_QUANTITY")
    private double execQuantity;

    /**
     * 已经执行金额
     */
    @Column(name = "EXEC_AMOUNT")
    private double execAmount;

    /**
     * 已经执行不含税金额
     */
    @Column(name = "EXEC_UNTAX_AMOUNT")
    private double execUnTaxAmount;

    @Override
    public String toPrettyString() {
        return getCode() + " " + getName();
    }

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

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