package com.zbf.system.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.zbf.system.domain.TMiStock;
import com.zbf.system.domain.TMiStockUpdate;
import com.zbf.system.domain.TMiXnStock;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.math.BigDecimal;
import java.util.List;

/**
 * 库存Mapper接口
 *
 * @author tony
 * @date 2024-06-12
 */
@Mapper
public interface TMiXnStockMapper extends BaseMapper<TMiXnStock> {


    void deleteStockByCtlAndLocationId(TMiXnStock tMiStock);

    void updateSts(TMiXnStock tMiStock);
    void updateLocation(TMiXnStock tMiStock);

    int insertTMiStock(TMiXnStock tMiStock);

    @Select("SELECT DISTINCT storage_id, storage_short_name FROM t_mi_stock ")
    List<TMiXnStock> selectGroupForYear();

    @Select("SELECT DISTINCT storage_id, storage_short_name FROM t_mi_stock where level_type ='1'")
    List<TMiXnStock> selectGroupForYear1();
    @Select("SELECT DISTINCT storage_id, storage_short_name FROM t_mi_stock WHERE NOT level_type = '1' ")
    List<TMiXnStock> selectGroupForYear2();

    @Select("SELECT * FROM t_mi_stock " +
            "WHERE storage_id = #{tMiStock.storageId} ")
    List<TMiXnStock> selectTMiStockListForYear(@Param("tMiStock") TMiXnStock tMiStock);

    @Select("SELECT COALESCE(SUM(total_money), 0) FROM t_mi_stock " +
            "WHERE YEAR(create_time) = #{year} AND MONTH(create_time) = #{month}")
    BigDecimal getTotalMoneyForSpecificMonth(@Param("year") int year, @Param("month") int month);

    @Select("SELECT COALESCE(SUM(total_money), 0) " +
            "FROM t_mi_stock " +
            "WHERE ware_date <= #{endDate} ")
    BigDecimal getTotalMoneyForMonth(@Param("startDate") String startDate,
                                     @Param("endDate") String endDate);
    @Select("SELECT COALESCE(SUM(total_money), 0) " +
            "FROM t_mi_stock " +
            "WHERE ware_date <= #{endDate} AND level_type = '1'")
    BigDecimal getTotalMoneyForMonth1(@Param("startDate") String startDate,
                                     @Param("endDate") String endDate);

    @Select("SELECT COALESCE(SUM(total_money), 0) " +
            "FROM t_mi_stock " +
            "WHERE ware_date <= #{endDate} AND level_type != '1'")
    BigDecimal getTotalMoneyForMonth3(@Param("startDate") String startDate,
                                     @Param("endDate") String endDate);



//    @Select("SELECT COALESCE(SUM(total_money), 0) " +
//            "FROM t_mi_stock " +
//            "WHERE ware_date >= #{startDate} " +
//            "AND ware_date < #{endDate}  AND level_type = '1'")
//    BigDecimal getTotalMoneyForMonth1(@Param("startDate") String startDate,
//                                     @Param("endDate") String endDate);
//
//    @Select("SELECT COALESCE(SUM(total_money), 0) " +
//            "FROM t_mi_stock " +
//            "WHERE ware_date >= #{startDate} " +
//            "AND ware_date < #{endDate} AND level_type != '1'")
//    BigDecimal getTotalMoneyForMonth3(@Param("startDate") String startDate,
//                                      @Param("endDate") String endDate);


    @Select("SELECT COALESCE(SUM(total_money), 0) " +
            "FROM t_mi_stock " +
            "WHERE ware_date >= #{startDate} " +
            "AND ware_date < #{endDate} AND level_type = '2'")
    BigDecimal getTotalMoneyForMonth2(@Param("startDate") String startDate,
                                     @Param("endDate") String endDate);

    @Select("SELECT COALESCE(SUM(total_money), 0) AS total_money_all_years FROM t_mi_stock")
    BigDecimal getTotalMoney();

    @Select("SELECT COALESCE(SUM(total_money), 0) AS total_money_all_years FROM t_mi_stock where not level_type = '1'")
    BigDecimal getTotalMoney1();

    @Select("SELECT COALESCE(SUM(total_money), 0) AS total_money_all_years FROM t_mi_stock where  level_type = '1'")
    BigDecimal getTotalMoney2();
    List<TMiXnStock> selectTMiStockList(TMiXnStock tMiStock);
    /**
     * 根据物料id和仓库id 按插入时间正序列表
     * @param tMiStock
     * @return
     */
    public List<TMiXnStock> selectTMiStockListByGoodsId(TMiXnStock tMiStock);
    int updateShelvesNum(TMiXnStock tMiStock);

    /**
     * 库存查询
     * @param tMiStock
     * @return
     */
    public List<TMiXnStock> inventoryStatisticsGoods(TMiXnStock tMiStock);

    /**
     * 盘点库存查询
     * @param tMiStock
     * @return
     */
    public List<TMiXnStock> selectPanGoodsList(TMiXnStock tMiStock);

    /**
     * 查询当日变化的库位
     * @param beginDate
     * @param endDate
     * @return
     */
    public List<String> selectPanTimeLocation(String queryDate);

    /**
     * 改变库位
     * @param tMiStockUpdate
     * @return
     */
    public int changeLocation(TMiStockUpdate tMiStockUpdate);

    /**
     * 更换容器
     * @param tMiStockUpdate
     * @return
     */
    public int changeCtlNo(TMiStockUpdate tMiStockUpdate);

    /**
     * 拆分容器
     * @param tMiStockUpdate
     * @return
     */
    public int updateTMiStockSplitCtl(TMiStock tMiStock);

    void updateTMiStockNum(TMiXnStock old);

    public List<TMiStock> selectGroup();
}
