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

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

import org.hibernate.annotations.OnDelete;
import org.hibernate.annotations.OnDeleteAction;

import com.ecep.contract.ds.company.model.Company;
import com.ecep.contract.ds.contract.model.Contract;
import com.ecep.contract.ds.contract.model.ContractBasedEntity;
import com.ecep.contract.model.BasedEntity;
import com.ecep.contract.model.Employee;
import com.ecep.contract.model.IdentityEntity;
import com.ecep.contract.model.Voable;
import com.ecep.contract.util.HibernateProxyUtils;
import com.ecep.contract.vo.SalesOrderVo;

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.ForeignKey;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

/**
 * 销售订单
 * 对应 U8 的 SO_SOMain 表
 */
@Getter
@Setter
@Entity
@Table(name = "CONTRACT_SALES_ORDER", schema = "supplier_ms")
@ToString
public class SalesOrder implements IdentityEntity, BasedEntity, ContractBasedEntity, Voable<SalesOrderVo> {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "ID", nullable = false)
    private Integer id;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "CONTRACT_ID", foreignKey = @ForeignKey(name = "FK_SALES_ORDER_CONTRACT"))
    @OnDelete(action = OnDeleteAction.CASCADE)
    @ToString.Exclude
    private Contract contract;

    /**
     * 业务员
     */
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "EMPLOYEE_ID", foreignKey = @ForeignKey(name = "FK_SALES_ORDER_EMPLOYEE"))
    @OnDelete(action = OnDeleteAction.SET_NULL)
    @ToString.Exclude
    private Employee employee;

    /**
     * 单据编号
     */
    @Column(name = "CODE")
    private String code;

    @Column(name = "REF_ID")
    private int refId;

    /**
     * 税率
     */
    @Column(name = "TAX_RATE")
    private float taxRate;

    /**
     * 客户
     */
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "CUSTOMER_ID", foreignKey = @ForeignKey(name = "FK_SALES_ORDER_CUSTOMER"))
    @OnDelete(action = OnDeleteAction.SET_NULL)
    @ToString.Exclude
    private Company customer;

    /**
     * 客户联系人
     */
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "CUSTOMER_PERSON_ID", foreignKey = @ForeignKey(name = "FK_SALES_ORDER_CUSTOMER_PERSON"))
    @OnDelete(action = OnDeleteAction.SET_NULL)
    @ToString.Exclude
    private Employee customerPerson;

    /**
     * 客户地址
     */
    @Column(name = "CUSTOMER_ADDRESS")
    private String customerAddress;

    /**
     * 制单人
     */
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "MAKER_ID", foreignKey = @ForeignKey(name = "FK_SALES_ORDER_MAKER"))
    @OnDelete(action = OnDeleteAction.SET_NULL)
    @ToString.Exclude
    private Employee maker;

    /**
     * 制单日期
     */
    @Column(name = "MAKER_DATE")
    private LocalDate makerDate;
    /**
     * 审核人
     */
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "VERIFIER_ID", foreignKey = @ForeignKey(name = "FK_SALES_ORDER_VERIFIER"))
    @OnDelete(action = OnDeleteAction.SET_NULL)
    @ToString.Exclude
    private Employee verifier;
    /**
     * 审核日期
     */
    @Column(name = "VERIFIED_DATE")
    private LocalDate verifierDate;

    /**
     * 修改人
     */
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "MODIFIER_ID", foreignKey = @ForeignKey(name = "FK_SALES_ORDER_MODIFIER"))
    @OnDelete(action = OnDeleteAction.SET_NULL)
    @ToString.Exclude
    private Employee modifier;

    @Column(name = "MODIFIED_DATE")
    private LocalDate modifierDate;

    /**
     * 关闭人
     */
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "CLOSER_ID", foreignKey = @ForeignKey(name = "FK_SALES_ORDER_CLOSER"))
    @OnDelete(action = OnDeleteAction.SET_NULL)
    @ToString.Exclude
    private Employee closer;

    @Column(name = "CLOSED_DATE")
    private LocalDate closerDate;
    /**
     * 备注
     */
    @Column(name = "DESCRIPTION", columnDefinition = "TEXT")
    private String description;

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

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

    @Override
    public String toPrettyString() {
        return "#" + getId();
    }

    @Override
    public SalesOrderVo toVo() {
        SalesOrderVo vo = new SalesOrderVo();
        // 基本字段映射
        vo.setId(id);
        vo.setCode(code);
        vo.setRefId(refId);
        vo.setTaxRate(taxRate);
        vo.setCustomerAddress(customerAddress);
        vo.setMakerDate(makerDate);
        vo.setVerifierDate(verifierDate);
        vo.setModifierDate(modifierDate);
        vo.setCloserDate(closerDate);
        vo.setDescription(description);

        // 关联对象ID映射
        if (contract != null) {
            vo.setContractId(contract.getId());
        }
        if (employee != null) {
            vo.setEmployeeId(employee.getId());
        }
        if (maker != null) {
            vo.setMakerId(maker.getId());
        }
        if (verifier != null) {
            vo.setVerifierId(verifier.getId());
        }
        if (customer != null) {
            vo.setCustomerId(customer.getId());
        }
        if (modifier != null) {
            vo.setModifierId(modifier.getId());
        }
        if (closer != null) {
            vo.setCloserId(closer.getId());
        }

        // active字段默认为false，在SalesOrderVo类中已经设置
        return vo;
    }
}
