package shop.alien.mapper;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import shop.alien.entity.store.StoreImg;
import shop.alien.entity.store.vo.StoreImgTypeVo;

import java.util.List;

/**
 * 门店图片 Mapper 接口
 *
 * @author ssk
 * @since 2024-12-05
 */
@Mapper
public interface StoreImgMapper extends BaseMapper<StoreImg> {

    @Select("select IFNULL(img_type, 0) img_type, count(1) count, img_url imgUrl from store_img where store_id = #{storeId} and img_type = 0 and delete_flag= 0 " +
            "union all " +
            "select IFNULL(img_type, 3) img_type, count(1) count, img_url imgUrl from store_img where store_id = #{storeId} and img_type = 3 and delete_flag= 0 " +
            "union all " +
            "select IFNULL(img_type, 4) img_type, count(1) count, img_url imgUrl from store_img where store_id = #{storeId} and img_type = 4 and delete_flag= 0 " +
            "union all " +
            "select IFNULL(img_type, 5) img_type, count(1) count, img_url imgUrl from store_img where store_id = #{storeId} and img_type = 5 and delete_flag= 0")
    List<StoreImgTypeVo> getStoreImgTypeCount(@Param("storeId") Integer storeId);

    /**
     * 根据商品id获取图片
     *
     * @param queryWrapper 商品id
     * @return 商品图片
     */
    @Select("SELECT * " +
            "FROM store_img ${ew.customSqlSegment}")
    List<StoreImg> getImgsByGoodsIds(@Param(Constants.WRAPPER) QueryWrapper<StoreImg> queryWrapper);



    /**
     * 获取封面图片
     *
     * @param queryWrapper 商品id
     * @return 商品图片
     */
    @Select("SELECT * " +
            "FROM store_img ${ew.customSqlSegment}")
    List<StoreImg> getByCover(@Param(Constants.WRAPPER) QueryWrapper<StoreImg> queryWrapper);

    /**
     * 通过连表查询获取官方相册图片列表（包含相册名称）
     * 连接条件：store_img.business_id = store_official_album.id
     *
     * @param storeId 门店ID
     * @param imgType 图片类型
     * @param albumIds 相册ID列表
     * @return 图片列表（包含相册名称）
     */
    @Select("<script>" +
            "SELECT si.*, soa.album_name as albumName " +
            "FROM store_img si " +
            "LEFT JOIN store_official_album soa ON si.business_id = soa.id " +
            "WHERE si.store_id = #{storeId} " +
            "AND si.img_type = #{imgType} " +
            "AND si.delete_flag = 0 " +
            "<if test='albumIds != null and albumIds.size() > 0'>" +
            "AND si.business_id IN " +
            "<foreach collection='albumIds' item='albumId' open='(' separator=',' close=')'>" +
            "#{albumId}" +
            "</foreach>" +
            "</if>" +
            "ORDER BY si.img_sort ASC" +
            "</script>")
    List<StoreImg> selectOfficialAlbumImgWithName(@Param("storeId") Integer storeId,
                                                   @Param("imgType") Integer imgType,
                                                   @Param("albumIds") List<Integer> albumIds);

    @Select("SELECT * FROM store_img ${ew.customSqlSegment}")
    StoreImg selectDeleImg(@Param(Constants.WRAPPER) LambdaQueryWrapper<StoreImg> storeImgLambdaQueryWrapper);

    /**
     * 更新删除状态
     *
     * @param set 更新条件
     * @return 更新行数
     */
    @Update("UPDATE store_img SET delete_flag = 0 WHERE id = #{id}")
    int updateDelete(@Param("id") String id );
}
