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.StoreOfficialAlbum;
import shop.alien.entity.store.vo.StoreOfficialAlbumVo;

import java.util.List;

/**
 * 官方相册信息
 */
@Mapper
public interface StoreOfficialAlbumMapper extends BaseMapper<StoreOfficialAlbum> {

    /**
     * 获取菜单
     *
     * @param storeId  门店id
     * @return List<StoreOfficialAlbumVo>
     */
    //"图片类型, 0:其他, 1:入口图, 2:相册, 3:菜品, 4:环境, 5:价目表, 6:推荐菜, 7:菜单, 8:用户评论, 9:商家申诉, 10:商家头像, 11:店铺轮播图"
    @Select("SELECT a.*, b.img_url FROM store_official_album a LEFT JOIN store_img b ON b.id = " +
            "(SELECT b2.id FROM store_img b2 WHERE b2.business_id = a.id and b2.img_type in ( '2','4') and b2.delete_flag = 0 " +
            "order by b2.img_sort LIMIT 1) where a.delete_flag = '0' and  a.store_id = #{storeId}")
    List<StoreOfficialAlbumVo> getStoreOfficialAlbumList(@Param("storeId") Integer storeId);

    @Select("select " +
            " soa.id," +
            " CASE " +
            "     WHEN soa.album_name = '视频' THEN " +
            "         (SELECT COUNT(*) " +
            "          FROM store_video sv " +
            "          WHERE sv.business_id = soa.id " +
            "            AND sv.delete_flag = 0 " +
            "            AND sv.store_id = #{storeId}) " +
            "     ELSE " +
            "         (SELECT COUNT(*) " +
            "          FROM store_img b2 " +
            "          WHERE b2.business_id = soa.id " +
            "            AND b2.img_type IN ('2', '4') " +
            "            AND b2.delete_flag = 0 " +
            "            AND b2.store_id = #{storeId}) " +
            " END as imgCount " +
            " from" +
            " store_official_album soa\n" +
            " where " +
            " soa.store_id = #{storeId} " +
            " and soa.delete_flag = 0")
    List<StoreOfficialAlbum> getStoreOfficialAlbumImgCount(@Param("storeId") Integer storeId);
}
