package com.ruoyi.system.mapper;

import java.util.Date;
import java.util.List;

import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.ruoyi.system.domain.PosFood;

/**
 * foodMapper接口
 * 
 * @author ruoyi
 * @date 2023-05-21
 */
public interface PosFoodMapper  extends BaseMapper<PosFood>
{
    /**
     * 查询food
     * 
     * @param id food主键
     * @return food
     */
    public PosFood selectPosFoodById(Long id);

    /**
     * 查询food列表
     * 
     * @param posFood food
     * @return food集合
     */
    public List<PosFood> selectPosFoodList(PosFood posFood);

    /**
     * 新增food
     * 
     * @param posFood food
     * @return 结果
     */
    public int insertPosFood(PosFood posFood);

    /**
     * 修改food
     * 
     * @param posFood food
     * @return 结果
     */
    public int updatePosFood(PosFood posFood);

    /**
     * 删除food
     * 
     * @param id food主键
     * @return 结果
     */
    public int deletePosFoodById(Long id);

    /**
     * 批量删除food
     * 
     * @param ids 需要删除的数据主键集合
     * @return 结果
     */
    public int deletePosFoodByIds(Long[] ids);

    /**
     * 根据创建时间查询food数量
     * @param createdAt 创建时间
     * @return 结果
     */
    public int countPosFoodByCreatedAt(@Param("createdAt") Date createdAt);

    /**
     * 查询多个门店的商品，每个门店最多返回指定数量的商品（使用窗口函数）
     * @param storeIds 门店ID列表
     * @param language 语言
     * @param limitPerStore 每个门店的商品数量限制
     * @return 商品列表
     */
    @Select("<script>" +
            "SELECT id, fl_id, mdid, name, image, price, introduce, recommend, sort, language, stacking_up, to_examine " +
            "FROM pos_food f " +
            "WHERE f.mdid IN " +
            "<foreach collection='storeIds' item='storeId' open='(' separator=',' close=')'>" +
            "  #{storeId}" +
            "</foreach>" +
            "AND f.to_examine = '1' " +
            "AND f.stacking_up &lt;&gt; '1' " +
            "AND f.language = #{language} " +
            "AND (SELECT COUNT(*) FROM pos_food f2 WHERE f2.mdid = f.mdid AND f2.id &gt; f.id AND f2.to_examine = '1' AND f2.stacking_up &lt;&gt; '1' AND f2.language = #{language}) &lt; #{limitPerStore} " +
            "ORDER BY f.mdid, f.id DESC" +
            "</script>")
    List<PosFood> selectFoodsByStoreIdsWithLimit(@Param("storeIds") List<Long> storeIds,
                                                 @Param("language") String language,
                                                 @Param("limitPerStore") int limitPerStore);

    /**
     * 按店铺查商品列表（审核通过、上架状态）
     */
    List<PosFood> selectFoodsByStoreId(@Param("storeId") Long storeId,
                                       @Param("language") String language);
}
