package cn.lihongjie.coal.purchaseOrder.entity;

import cn.lihongjie.coal.base.entity.OrgCommonEntity;
import cn.lihongjie.coal.coalInfo.entity.CoalInfoEntity;
import cn.lihongjie.coal.deliveryInformation.entity.DeliveryInformationEntity;

import jakarta.persistence.Entity;
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.util.List;

@Data
@Entity
public class PurchaseOrderEntity extends OrgCommonEntity {



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

    @Comment("预计到货日期")
    private LocalDate estimateDeliveryDate;


    @Formula("(select min(d.delivery_date) from t_delivery_information d where d.purchase_order_id = id)")
    private LocalDate firstDeliveryDate;

    @Formula("(select max(d.delivery_date) from t_delivery_information d where d.purchase_order_id = id)")
    private LocalDate lastDeliveryDate;



    @Formula("(estimate_delivery_date - now()::date)")
    private Integer dayLeft;


    @Formula("(estimate_delivery_date - purchase_date)")
    private Integer dayAmount;



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

    @OneToMany(mappedBy = "purchaseOrder")
    private List<DeliveryInformationEntity> deliveryInformationList;


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


    @Formula("(select sum(d.amount) from t_delivery_information d where d.purchase_order_id = id)")
    private Double receivedAmount;


    @Formula("(amount - (select sum(d.amount) from t_delivery_information d where d.purchase_order_id = id))")
    private Double leftAmount;


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

    @Formula("(amount * price)")
    private Double total;

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

    @Formula(
            "(select i.name\n"
                    + "from t_dictionary d,\n"
                    + "     t_dictionary_item i\n"
                    + "where d.id = i.dictionary_id\n"
                    + "  and d.code = 'purchaseOrder.status'\n"
                    + "  and i.code = order_status)")
    private String orderStatusName;

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

    private void updateStatus() {
        if (this.purchaseDate.isAfter(LocalDate.now())){
            this.orderStatus = "0";
        }else {

            double total = this.deliveryInformationList == null ? 0: this.deliveryInformationList.stream().mapToDouble(DeliveryInformationEntity::getAmount).sum();

            if (total >= this.amount){
                this.orderStatus = "2";
            }else {
                this.orderStatus = "1";
            }

        }
    }

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

    public void setDeliveryInformationList(List<cn.lihongjie.coal.deliveryInformation.entity.DeliveryInformationEntity> deliveryInformationList) {
        if (deliveryInformationList == null) {
            return;
        }
        deliveryInformationList.forEach(e -> e.setPurchaseOrder(this));
        this.deliveryInformationList = deliveryInformationList;
    }
}
