package com.bailuntec.balancesheet.mapper;

import com.bailuntec.balancesheet.entity.DcSemiDailyStock;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;
import java.util.Date;

@Repository
public interface DcSemiDailyStockMapper {
    @Insert("insert ignore into dc_semi_daily_stock(day,inventory_amount,sku_code,sku_name,stock_quantity,price,warehouse_id,warehouse_name,type_id,type_name,product_category_id,product_category_id_name,redundancy_stock) " +
            "select #{lastDay},l.final_stock * ifnull(ol.price,0),l.sku_code,l.sku_name,l.final_stock,ifnull(ol.price,0),l.warehouse_id,l.warehouse_name,l.type_id,l.type_name,l.product_category_id,l.product_category_name," +
            "case when l.redundancy_stock is null then 0 when l.redundancy_stock < 0 then 0 else l.redundancy_stock end " +
            "from dc_semi_stock_log l left join dc_semi_supplier_offer_log ol on l.sku_code = ol.sku_code")
    void insertSemiDailyStock(@Param("lastDay") Date lastDay);

    @Select("select sum(inventory_amount) from dc_semi_daily_stock where day = #{lastDay}")
    BigDecimal getSemiDailyStockSum(@Param("lastDay") Date lastDay);

    @Select("select * from dc_semi_daily_stock where day = #{lastDay} and sku_code =#{skuCode} and warehouse_id = #{warehouseId}" )
    DcSemiDailyStock query(@Param("lastDay") Date lastDay, @Param("skuCode") String skuCode, @Param("warehouseId") String warehouseId);

    @Select("select sum(inventory_amount) from dc_semi_daily_stock where day = #{lastDay} and warehouse_id in (27,1,26,5,17,15,19,18,2)")
    BigDecimal getSemiBailunDailyStockSum(@Param("lastDay") Date lastDay);

    @Select("select sum(inventory_amount) from dc_semi_daily_stock where day = #{lastDay} and warehouse_id in (16,9)")
    BigDecimal getSemiHaNiDailyStockSum(@Param("lastDay") Date lastDay);

    @Select("select sum((jit_prod_stock + plan_prod_stock) * new_price) from dc_semi_stock_prod_daily where record_time = #{lastDay} and warehouse_name != '哈倪蔓美甲灌装车间'")
    BigDecimal getSemiProdInventoryAmountSum(@Param("lastDay") Date lastDay);

}
