package cn.lihongjie.coal.inventoryCheck.entity;

import cn.lihongjie.coal.base.entity.OrgCommonEntity;

import jakarta.persistence.Entity;

import lombok.Data;

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

import java.time.LocalDateTime;

@Data
@Entity
public class InventoryCheckEntity extends OrgCommonEntity {

    private LocalDateTime checkTime;

    @Comment("盘点类型")
    private String checkType;

    @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 = 'inventoryCheck.type'\n"
                    + "  and i.code = check_type)")
    private String checkTypeName;

    /** 精煤 合计 */
    @Formula(
            """
(
select sum(d.amount) from t_inventory_check_detail d
    left join t_coal_info c on d.coal_info_id = c.id
    left join t_product p on d.product_id = p.id
where (c.coal_type = '0' or p.coal_type = '0') and d.inventory_check_id = id
)""")
    private Long sum0;

    /** 中煤 合计 */
    @Formula(
            """
(
select sum(d.amount) from t_inventory_check_detail d
    left join t_coal_info c on d.coal_info_id = c.id
    left join t_product p on d.product_id = p.id
where (c.coal_type = '1' or p.coal_type = '1') and d.inventory_check_id = id
)""")
    private Long sum1;

    /** 原煤 合计 */
    @Formula(
            """
(
select sum(d.amount) from t_inventory_check_detail d
    left join t_coal_info c on d.coal_info_id = c.id
    left join t_product p on d.product_id = p.id
where (c.coal_type = '2' or p.coal_type = '2') and d.inventory_check_id = id
)""")
    private Long sum2;

    /** 煤泥 合计 */
    @Formula(
            """
(
select sum(d.amount) from t_inventory_check_detail d
    left join t_coal_info c on d.coal_info_id = c.id
    left join t_product p on d.product_id = p.id
where (c.coal_type = '3' or p.coal_type = '3') and d.inventory_check_id = id
)""")
    private Long sum3;

    /** 矸石 合计 */
    @Formula(
            """
(
select sum(d.amount) from t_inventory_check_detail d
    left join t_coal_info c on d.coal_info_id = c.id
    left join t_product p on d.product_id = p.id
where (c.coal_type = '4' or p.coal_type = '4') and d.inventory_check_id = id
)""")
    private Long sum4;

    /** 其他 合计 */
    @Formula(
            """
(
select sum(d.amount) from t_inventory_check_detail d
    left join t_coal_info c on d.coal_info_id = c.id
    left join t_product p on d.product_id = p.id
where (c.coal_type = '5' or p.coal_type = '5') and d.inventory_check_id = id
)""")
    private Long sum5;
}
