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
    );


    @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 " +
            " ) " +
            " , violationInfo as ( " +
            " SELECT " +
            " luv.id, ui.nick_name, ui.phone, luv.report_context_type,  " +
            " case when luv.violation_type = 1 then '用户违规' when luv.violation_type = 2 then '色情低俗' when luv.violation_type = 3 then '违法违规' when luv.violation_type = 4 then '谩骂嘲讽、煽动对立' when luv.violation_type = 5 then '涉嫌诈骗' " +
            " when luv.violation_type = 6 then '人身攻击' when luv.violation_type = 7 then '种族歧视' when luv.violation_type = 8 then '政治敏感' when luv.violation_type = 9 then '虚假、不实内容' " +
            " when luv.violation_type = 10 then '违反公德秩序' when luv.violation_type = 11 then '危害人身安全' when luv.violation_type = 12 then '网络暴力' else '其他原因' end violation_type_name,  " +
            " case when luv.report_context_type = 0 then '商户' when luv.report_context_type = 1 then '用户' when luv.report_context_type = 2 then '动态' else '评论' end report_context_name, " +
            " luv.processing_status, case when luv.processing_status = 0 then '待处理' when luv.processing_status = 1 then '已通过' else '已驳回' end processing_name,  " +
            " luv.processing_time, luv.created_time, img.img_url image, luv.updated_time " +
            " 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 " +
            " where luv.delete_flag = 0 and luv.report_context_type in ('1', '2', '3') " +
            " union all " +
            " select " +
            " luv.id, lu.user_name nick_name, lu.user_phone phone, luv.report_context_type, sd.dict_detail violation_type_name,  " +
            " case when luv.report_context_type = '4' then '商品' else '用户' end report_context_name,  " +
            " luv.processing_status, case when luv.processing_status = 0 then '待处理' when luv.processing_status = 1 then '已通过' else '已驳回' end processing_name, " +
            " luv.processing_time, luv.created_time, '' image, luv.updated_time " +
            " from life_user_violation luv " +
            " left join life_user lu on luv.reporting_user_id = lu.id and lu.delete_flag = 0 " +
            " left join store_dictionary sd on luv.dict_type = sd.type_name and luv.dict_id = sd.dict_id and sd.delete_flag = 0 " +
            " where luv.report_context_type in ('4', '5') and sd.delete_flag = 0 " +
            " ) " +
            " select * from violationInfo " +
            " ${ew.customSqlSegment}" +
            "</script>")
    IPage<LifeUserViolationVo> getAllViolationPage(
            IPage<LifeUserViolationVo> page,
            @Param(Constants.WRAPPER) QueryWrapper<LifeUserViolationVo> queryWrapper
    );
}
