package com.bailuntec.balancesheet.mapper;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

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

@Repository
public interface DcSemiDailyStockMapper {
    @Insert("insert into dc_semi_daily_stock(day,inventory_amount,sku_code,sku_name,stock_quantity,price,warehouse_id,warehouse_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," +
            "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);

}