package com.ecep.contract.model;

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

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

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;

/**
 * 销售订单条目
 * 对应 U8 的 SO_SODetails 表
 */
@Getter
@Setter
@Entity
@Table(name = "CONTRACT_SALES_ORDER_ITEM", schema = "supplier_ms")
@ToString
public class SalesOrderItem implements IdentityEntity, BasedEntity, Serializable, Voable<SalesOrderItemVo> {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "ID", nullable = false)
    private Integer id;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "ORDER_ID")
    @ToString.Exclude
    private SalesOrder order;

    @Column(name = "CODE")
    private String code;

    @Column(name = "NAME")
    private String name;

    @Column(name = "QUANTITY")
    private double quantity;

    /**
     * 含税单价
     */
    @Column(name = "TAX_PRICE")
    private double price;

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

    /**
     * 不含税单价
     */
    @Column(name = "EX_TAX_PRICE")
    private double exclusiveTaxPrice;

    @Column(name = "START_DATE")
    private LocalDate startDate;

    @Column(name = "END_DATE")
    private LocalDate endDate;

    @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;
        }
        SalesOrderItem that = (SalesOrderItem) 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 SalesOrderItemVo toVo() {
        SalesOrderItemVo vo = new SalesOrderItemVo();
        vo.setId(id);
        vo.setCode(code);
        vo.setName(name);
        if (order != null) {
            vo.setSalesOrderId(order.getId());
        }
        vo.setItemName(name);
        vo.setQuantity(quantity);
        vo.setPrice(price);
        vo.setTaxRate(taxRate);
        vo.setExclusiveTaxPrice(exclusiveTaxPrice);
        vo.setStartDate(startDate);
        vo.setEndDate(endDate);
        vo.setDescription(description);
        return vo;
    }
}
