package shop.alien.mapper;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import shop.alien.entity.store.LifeUserViolation;
import shop.alien.entity.store.dto.LifeUserViolationDto;
import shop.alien.entity.store.vo.LifeUserViolationVo;

import java.util.List;

/**
 * <p>
 * 用户举报 Mapper 接口
 * </p>
 *
 * @author ssk
 * @since 2025-04-29
 */
public interface LifeUserViolationMapper extends BaseMapper<LifeUserViolation> {

    /**
     * 分页查询用户举报信息
     *
     * @param page         分页对象
     * @param queryWrapper 查询条件包装器
     * @return 分页结果
     */
    @Select("<script>" +
            " WITH userInfo AS ( " +
            " SELECT " +
            " su.phone, " +
            " su.id, " +
            " CASE su.delete_flag " +
            " WHEN 1 THEN CONCAT(su.nick_name, '(账号已注销)') " +
            " ELSE su.nick_name " +
            " END AS nick_name, " +
            " '1' AS type " +
            " FROM store_user su " +
            " UNION ALL " +
            " SELECT " +
            " lu.user_phone AS phone, " +
            " lu.id, " +
            " CASE lu.delete_flag " +
            " WHEN 1 THEN CONCAT(lu.user_name, '(账号已注销)') " +
            " ELSE lu.user_name " +
            " END AS nick_name, " +
            " '2' AS type " +
            " FROM life_user lu " +
            " ) " +
            " SELECT " +
            " luv.*, " +
            " ui.nick_name AS nickname, " +
            " ui.phone, " +
            " img.img_url image" +
            " FROM life_user_violation luv " +
            " LEFT JOIN userInfo ui ON ui.type = luv.reporting_user_type " +
            " AND ui.id = luv.reporting_user_id " +
            " left join store_img img on luv.id = img.store_id and img.delete_flag = 0 " +
            " ${ew.customSqlSegment}" +
            "</script>")
    IPage<LifeUserViolationVo> getViolationPage(
            IPage<LifeUserViolationVo> page,
            @Param(Constants.WRAPPER) QueryWrapper<LifeUserViolationVo> queryWrapper
    );

    @Select("select luv.*, img.img_url image " +
            "FROM life_user_violation luv " +
            "left join store_img img on luv.id = img.store_id and img.delete_flag = 0 " +
            "${ew.customSqlSegment}")
    LifeUserViolationDto getDetailById(@Param(Constants.WRAPPER) QueryWrapper<LifeUserViolationDto> queryWrapper);

    /**
     * 查询用户举报信息列表
     *
     * @param queryWrapper 查询条件包装器
     * @return 举报信息列表
     */
    @Select("<script>" +
            "WITH userInfo AS (" +
            "    SELECT " +
            "        su.phone, " +
            "        su.id, " +
            "        CASE su.delete_flag " +
            "            WHEN 1 THEN CONCAT(su.nick_name, '(账号已注销)') " +
            "            ELSE su.nick_name " +
            "        END AS nick_name, " +
            "        '1' AS type " +
            "    FROM store_user su " +
            "    UNION ALL " +
            "    SELECT " +
            "        lu.user_phone AS phone, " +
            "        lu.id, " +
            "        CASE lu.delete_flag " +
            "            WHEN 1 THEN CONCAT(lu.user_name, '(账号已注销)') " +
            "            ELSE lu.user_name " +
            "        END AS nick_name, " +
            "        '2' AS type " +
            "    FROM life_user lu " +
            ") " +
            "SELECT " +
            "    luv.*, " +
            "    ui.nick_name AS nickname, " +
            "    ui.phone " +
            "FROM life_user_violation luv " +
            "LEFT JOIN userInfo ui ON ui.type = luv.reporting_user_type " +
            "    AND ui.id = luv.reporting_user_id " +
            "    ${ew.customSqlSegment}" +
            "</script>")
    List<LifeUserViolationVo> getViolationList(
            @Param(Constants.WRAPPER) QueryWrapper<LifeUserViolationVo> queryWrapper
    );
}
