package com.ecep.contract.model;

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

import org.hibernate.annotations.ColumnDefault;

import com.ecep.contract.util.HibernateProxyUtils;

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 jakarta.persistence.Version;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

/**
 * 项目
 */
@Getter
@Setter
@Entity
@Table(name = "PROJECT", schema = "supplier_ms")
@ToString
public class Project implements IdentityEntity, NamedEntity, BasedEntity, Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @Column(name = "ID", nullable = false)
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;

    /**
     * 项目对应的客户公司
     */
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "CUSTOMER_ID")
    @ToString.Exclude
    private Company customer;

    /**
     * 项目名称
     */
    @Column(name = "NAME")
    private String name;

    /**
     * 项目代码，{@link #saleType
     * saleType.Code}+{@link #codeYear}+{@link #codeSequenceNumber
     * codeSequenceNumber} 组成
     */
    @Column(name = "CODE")
    private String code;

    /**
     * 项目代码年份
     */
    @Column(name = "CODE_YEAR")
    private int codeYear;

    /**
     * 项目代码序号
     */
    @Column(name = "CODE_SN")
    private int codeSequenceNumber;

    /**
     * 项目地址
     */
    @Column(name = "ADDRESS")
    private String address;

    /**
     * 是否投标
     */
    @Column(name = "USE_BID")
    private boolean useBid;

    /**
     * 是否报价
     */
    @Column(name = "USE_OFFER")
    private boolean useOffer;

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

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

    /**
     * 最终用户行业类型
     */
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "INDUSTRY_ID")
    @ToString.Exclude
    private ProjectIndustry industry;

    /**
     * 销售类型
     */
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "SALE_TYPE_ID")
    @ToString.Exclude
    private ProjectSaleType saleType;

    /**
     * 项目类型
     */
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "PROJECT_TYPE_ID")
    @ToString.Exclude
    private ProjectType projectType;

    /**
     * 产品类型
     */
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "PRODUCT_TYPE_ID")
    @ToString.Exclude
    private ProductType productType;

    /**
     * 交货签收方式
     * Delivery signature method
     */
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "DELIVERY_SIGN_METHOD_ID")
    @ToString.Exclude
    private DeliverySignMethod deliverySignMethod;

    /**
     * 产品用途
     */
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "PRODUCT_USAGE_ID")
    @ToString.Exclude
    private ProductUsage productUsage;

    /**
     * 申请人
     */
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "APPLICANT_ID")
    @ToString.Exclude
    private Employee applicant;

    /**
     * 审核人
     */
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "AUTHORIZER_ID")
    @ToString.Exclude
    private Employee authorizer;

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

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

    @Column(name = "CREATED")
    private LocalDate created;

    /**
     * 预计开工时间
     */
    @Column(name = "PLANNED_START_TIME")
    private LocalDate PlannedStartTime;
    /**
     * 预计完工时间
     */
    @Column(name = "PLANNED_COMPLETION_TIME")
    private LocalDate PlannedCompletionTime;
    /**
     * 银行账户（公司可能有多个账户，此项目关联其中一个账户）
     */
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "COMPANY_BANK_ACCOUNT_ID")
    @ToString.Exclude
    private CompanyBankAccount bankAccount;
    /**
     * 开票信息（公司可能有多个开票信息，此项目关联其中一个开票信息）
     */
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "COMPANY_INVOICE_INFO_ID")
    @ToString.Exclude
    private CompanyInvoiceInfo invoiceInfo;
    /**
     * 主联系人（公司可能有多个联系人，此项目关联其中一个为主联系人）
     */
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "MAIN_CONTACT_ID")
    @ToString.Exclude
    private CompanyContact mainContact;
    /**
     * 备用联系人（公司可能有多个联系人，此项目关联其中一个为备用联系人）
     */
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "SUB_CONTACT_ID")
    @ToString.Exclude
    private CompanyContact subContact;

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

    @Override
    public String toPrettyString() {
        return code + " " + name;
    }

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

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