package cn.lihongjie.coal.warehouseGoodsSummary.entity;

import cn.lihongjie.coal.base.entity.OrgCommonEntity;
import cn.lihongjie.coal.warehouse.entity.WarehouseEntity;
import cn.lihongjie.coal.warehouseGoodsBrand.entity.WarehouseGoodsBrandEntity;
import cn.lihongjie.coal.warehouseGoodsCategory.entity.WarehouseGoodsCategoryEntity;
import cn.lihongjie.coal.warehouseGoodsUnit.entity.WarehouseGoodsUnitEntity;

import jakarta.persistence.Entity;
import jakarta.persistence.ManyToOne;

import lombok.Data;

import org.hibernate.annotations.*;

@Data
@Entity
@Subselect(
        """

select 
    distinct on (g.id)
    g.id,
       g.create_time,
       g.create_user_id,
       g.file_ids,
       g.update_time,
       g.update_user_id,
       g.code,
       g.name,
       g.remarks,
       g.sort_key,
       g.status,
       g.organization_id,
       g.bar_code,
       g.price1,
       g.price2,
       g.price3,
       g.price4,
       g.price5,
       g.size,
       g.spec,
       g.model,
       g.volume,
       g.weight,
       g.brand_id,
       g.category_id,
       g.unit_id,
       g.enable_shelf_life,
       g.shelf_life,
       d.warehouse_id                       as warehouse_id,
       count(d.id) over (partition by g.id) as detail_count,
       round((sum(d.number) filter ( where d.receipt_type = '0' and (r.receipt_date >= :startTime and r.receipt_date <= :endTime) :supplier ) over (partition by g.id, d.warehouse_id))::numeric,
             2)                             as type0number,
       round((sum(d.number) filter ( where d.receipt_type = '1' and (r.receipt_date >= :startTime and r.receipt_date <= :endTime) :supplier ) over (partition by g.id, d.warehouse_id))::numeric,
             2)                             as type1number,

       round((sum(d.amount) filter ( where d.receipt_type = '0' and (r.receipt_date >= :startTime and r.receipt_date <= :endTime) :supplier ) over (partition by g.id, d.warehouse_id))::numeric,
             2)                             as type0amount,
       round((sum(d.amount) filter ( where d.receipt_type = '1' and (r.receipt_date >= :startTime and r.receipt_date <= :endTime) :supplier ) over (partition by g.id, d.warehouse_id))::numeric,
             2)                             as type1amount

from t_warehouse_goods g
         inner join t_warehouse_receipt_detail d on d.goods_id = g.id and d.organization_id = g.organization_id
         inner join t_warehouse_receipt r on r.id = d.receipt_id and r.organization_id = g.organization_id


""")

@FilterDef(name = "warehouseGoodsSummaryTimeFilter", parameters = {

        @ParamDef(name = "startTime", type = java.time.LocalDateTime.class),
        @ParamDef(name = "endTime", type = java.time.LocalDateTime.class),
})
public class WarehouseGoodsSummaryEntity extends OrgCommonEntity {

    @ManyToOne
    private WarehouseGoodsCategoryEntity category;

    @ManyToOne
    private WarehouseGoodsBrandEntity brand;


    @ManyToOne
    private WarehouseGoodsUnitEntity unit;



    @ManyToOne
    private WarehouseEntity warehouse;



    private Integer detailCount;

    private Double type0Number;
    private Double type1Number;

    private Double type0Amount;

    private Double type1Amount;


    @Comment("条码")
    private String barCode;

    @Comment("规格")
    private String spec;

    @Comment("型号")

    private String model;

    @Comment("重量 kg")
    private Double weight;

    @Comment("体积 m3")
    private Double volume;

    @Comment("尺寸")
    private String size;

    @Comment("进货价")
    private Double price1;
    @Comment("市场价")
    private Double price2;
    @Comment("仓库核算价")
    private Double price3;

    @Comment("批发价")
    private Double price4;
    private Double price5;



    @Comment("是否启用保质期")

    private Boolean enableShelfLife;


    @Comment("保质期 天")
    private Integer shelfLife;

}
