package org.dromara.system.mapper;

import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.dromara.system.domain.SysFoodIngredient;
import org.dromara.system.domain.vo.SysFoodIngredientVo;
import org.dromara.common.mybatis.core.mapper.BaseMapperPlus;
import org.dromara.system.domain.vo.SysFoodStockBalanceVo;

import java.util.List;
import java.util.Set;

/**
 * 食材管理Mapper接口
 *
 * @author Lion Li
 * @date 2025-06-30
 */
public interface SysFoodIngredientMapper extends BaseMapperPlus<SysFoodIngredient, SysFoodIngredientVo> {

    @Select(" <script> SELECT fis.food_id AS foodId, fis.food_name as foodName, fis.main_class as mainClass, fis.main_class_name as mainClassName, fis.sub_class as subClass, fis.sub_class_name as subClassName, " +
        "fis.stock_unit as stockUnit, " +
        " SUM( CASE WHEN fis.type = '0' THEN num ELSE 0 END ) -  SUM( CASE WHEN fis.type IN ( '1', '2', '3' ) THEN fis.num ELSE 0 END ) AS stockNum " +
        " FROM food_ingredient_stock fis where 1 =1 " +
        "<if test='foodIds != null and foodIds.size() > 0'>" +
        "   AND fis.food_id IN " +
        "   <foreach collection='foodIds' item='id' open='(' separator=',' close=')'>" +
        "       #{id}" +
        "   </foreach>" +
        "</if>" +
        "GROUP BY fis.food_id, fis.main_class, fis.sub_class, fis.stock_unit, fis.food_name, fis.main_class_name, fis.sub_class_name " +
        "</script>" )
    List<SysFoodStockBalanceVo> queryStockBalance(@Param("foodIds") Set<Long> foodIds);

}
