package cn.lihongjie.coal.purchaseOrder.entity;

import cn.lihongjie.coal.base.entity.OrgCommonEntity;
import cn.lihongjie.coal.coalInfo.entity.CoalInfoEntity;
import cn.lihongjie.coal.weightDeviceData.entity.WeightDeviceDataEntity;

import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.OneToMany;

import lombok.Data;

import org.hibernate.annotations.Comment;
import org.hibernate.annotations.Formula;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Data
@Entity

@jakarta.persistence.Table(indexes = @jakarta.persistence.Index(name ="idx_purchase_order_org_id", columnList = "organization_id"))

public class PurchaseOrderEntity extends OrgCommonEntity {



    @Comment("采购日期")
    private LocalDate purchaseDate;







    @OneToMany(mappedBy = "purchaseOrder", fetch = FetchType.LAZY)
    private List<WeightDeviceDataEntity> weightDataList;



    @ManyToOne
    @Comment("煤炭信息")
    private CoalInfoEntity coalInfo;




    @Comment("采购数量")
    private Double amount;


    @Comment("是否含税")
    private Boolean includeTax;

    @Comment("税率")
    private Double taxRate;





    @Comment("采购单价")
    private Double price;



    @Comment("其他费用")
    private Double otherFee;


    @Formula("((amount * price) + coalesce(other_fee, 0))")
    private Double total;

    @Comment("采购单状态")
    private String orderStatus;



    @Comment("完成/作废时间")
    private LocalDateTime finishTime;


    @Comment("订单开始时间")
    private LocalDateTime startTime;



    @Formula("(round(EXTRACT(EPOCH FROM (coalesce(finish_time, now()) - start_time)) / 1000.0 / 60.0 / 24.0 ,2))")
    private Double dayAmount;


    @Override
    public void prePersist() {
        super.prePersist();
    }

    @Override
    public void preUpdate() {
        super.preUpdate();
    }
}