package shop.alien.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import shop.alien.entity.store.StoreMenu;
import shop.alien.entity.store.vo.StoreMenuVo;

import java.util.List;

/**
 * 门店推荐 Mapper 接口
 *
 * @author ssk
 * @since 2024-12-05
 */
@Mapper
public interface StoreMenuMapper extends BaseMapper<StoreMenu> {

    /**
     * 获取菜单
     *
     * @param storeId  门店id
     * @param dishType 菜品类型, 0:菜单, 1:推荐
     * @return List<StoreMenuVo>
     */
    //"图片类型, 0:其他, 1:入口图, 2:相册, 3:菜品, 4:环境, 5:价目表, 6:推荐菜, 7:菜单, 8:用户评论, 9:商家申诉, 10:商家头像, 11:店铺轮播图"
    @Select("select a.*, b.img_url, b.img_sort, b.img_description from store_menu a " +
            "left join store_img b on a.img_id = b.id where a.delete_flag = 0 and b.delete_flag = 0 " +
            "and a.store_id = #{storeId} " +
            "and (if(#{dishType} is null, 1 = 1, a.dish_type = #{dishType})) " +
            "and (if(#{dishMenuType} is null, 1 = 1, a.dish_menu_type = #{dishMenuType}))")
    List<StoreMenuVo> getStoreMenuInfoList(@Param("storeId") Integer storeId, @Param("dishType") Integer dishType, @Param("dishMenuType") Integer dishMenuType);

    /**
     * 获取菜单
     *
     * @param storeId  门店id
     * @param dishType 菜品类型, 0:菜单, 1:推荐
     * @return List<StoreMenuVo>
     */
    //"图片类型, 0:其他, 1:入口图, 2:相册, 3:菜品, 4:环境, 5:价目表, 6:推荐菜, 7:菜单, 8:用户评论, 9:商家申诉, 10:商家头像, 11:店铺轮播图"
    @Select("select a.*, b.img_url, b.img_sort, b.img_description from store_menu a " +
            "left join store_img b on a.img_id = b.id where a.delete_flag = 0 and b.delete_flag = 0 " +
            "and a.store_id = #{storeId} AND (if(#{dishType} is null, 1 = 1, a.dish_type = #{dishType}))")
    List<StoreMenuVo> getStoreMenuList(@Param("storeId") Integer storeId, @Param("dishType") Integer dishType);

    @Select("select a.*, b.img_url, b.img_sort, b.img_description from store_menu a " +
            "left join store_img b on a.img_id = b.id where a.delete_flag = 0 and b.delete_flag = 0 " +
            "and a.id = #{id}")
    StoreMenuVo getMenuInfo(@Param("id") Integer id);

    /**
     * 获取菜单（客户端）
     * <p>
     * 根据门店ID查询菜单列表，支持按菜品类型和菜单类型筛选。
     * 查询未删除的菜单及其关联的图片信息。
     * </p>
     *
     * @param storeId      门店ID，必填
     * @param dishType     菜品类型，可选，0:非推荐, 1:推荐。当为null时，不限制菜品类型
     * @param dishMenuType 菜单类型，可选，1-菜单，2-酒水。当为null时，不限制菜单类型
     * @return 菜单列表，包含菜单信息和关联的图片信息
     */
    @Select("<script>" +
            "SELECT " +
            "    a.*, " +
            "    b.img_url, " +
            "    b.img_sort, " +
            "    b.img_description " +
            "FROM store_menu a " +
            "LEFT JOIN store_img b ON a.img_id = b.id " +
            "WHERE a.delete_flag = 0 " +
            "    AND (b.delete_flag = 0 OR b.delete_flag IS NULL) " +
            "    AND a.store_id = #{storeId} " +
            "<if test='dishType != null'>" +
            "    AND a.dish_type = #{dishType} " +
            "</if>" +
            "<if test='dishMenuType != null'>" +
            "    AND a.dish_menu_type = #{dishMenuType} " +
            "</if>" +
            "</script>")
    List<StoreMenuVo> getClientMenuByStoreId(@Param("storeId") Integer storeId,
                                             @Param("dishType") Integer dishType,
                                             @Param("dishMenuType") Integer dishMenuType);

    @Select("select a.*, b.img_url, b.img_sort, b.img_description from store_menu a " +
            "left join store_img b on a.img_id = b.id where a.delete_flag = 0 and b.delete_flag = 0 " +
            "and a.id = #{id}")
    StoreMenuVo getClientMenuInfoById(@Param("id") Integer id);
}