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

import java.util.List;

/**
 * <p>
 * 律师用户举报 Mapper 接口
 * </p>
 *
 * @author system
 * @since 2025-01-XX
 */
public interface LawyerUserViolationMapper extends BaseMapper<LawyerUserViolation> {
    
    /**
     * 分页查询律师用户举报信息
     * 
     * @param page 分页对象
     * @param queryWrapper 查询条件包装器
     * @return 分页结果
     */
    @Select("<script>" +
            "WITH userInfo AS (" +
            "    SELECT " +
            "        lu.phone, " +
            "        lu.id, " +
            "        CASE lu.delete_flag " +
            "            WHEN 1 THEN CONCAT(lu.name, '(账号已注销)') " +
            "            ELSE lu.name " +
            "        END AS nick_name, " +
            "        'lawyer' AS type " +
            "    FROM lawyer_user lu " +
            "    UNION ALL " +
            "    SELECT " +
            "        su.phone, " +
            "        su.id, " +
            "        CASE su.delete_flag " +
            "            WHEN 1 THEN CONCAT(su.nick_name, '(账号已注销)') " +
            "            ELSE su.nick_name " +
            "        END AS nick_name, " +
            "        'store' AS type " +
            "    FROM store_user su " +
            "    UNION ALL " +
            "    SELECT " +
            "        lu2.user_phone AS phone, " +
            "        lu2.id, " +
            "        CASE lu2.delete_flag " +
            "            WHEN 1 THEN CONCAT(lu2.user_name, '(账号已注销)') " +
            "            ELSE lu2.user_name " +
            "        END AS nick_name, " +
            "        'user' AS type " +
            "    FROM life_user lu2 " +
            ") " +
            "SELECT " +
            "    luv.*, " +
            "    ui.nick_name AS nickname, " +
            "    ui.phone, " +
            "    ui_reported.phone AS reported_phone, " +
            "    ui.nick_name AS reportUserName, " +
            "    ui_reported.nick_name AS reportedUserName " +
            "FROM lawyer_user_violation luv " +
            "LEFT JOIN userInfo ui ON (" +
            "    (ui.type = 'lawyer' AND luv.reporting_user_type = '3' AND ui.id = luv.reporting_user_id) OR " +
            "    (ui.type = 'store' AND luv.reporting_user_type = '1' AND ui.id = luv.reporting_user_id) OR " +
            "    (ui.type = 'user' AND luv.reporting_user_type = '2' AND ui.id = luv.reporting_user_id)" +
            ") " +
            "LEFT JOIN userInfo ui_reported ON (" +
            "    (ui_reported.type = 'lawyer' AND luv.reported_user_type = '3' AND ui_reported.id = luv.reported_user_id) OR " +
            "    (ui_reported.type = 'store' AND luv.reported_user_type = '1' AND ui_reported.id = luv.reported_user_id) OR " +
            "    (ui_reported.type = 'user' AND luv.reported_user_type = '2' AND ui_reported.id = luv.reported_user_id)" +
            ") " +
            "    ${ew.customSqlSegment}" +
            "</script>")
    IPage<LawyerUserViolationVo> getViolationPage(
            IPage<LawyerUserViolationVo> page, 
            @Param(Constants.WRAPPER) QueryWrapper<LawyerUserViolationVo> queryWrapper
    );
    
    /**
     * 查询律师用户举报信息列表
     * 
     * @param queryWrapper 查询条件包装器
     * @return 举报信息列表
     */
    @Select("<script>" +
            "WITH userInfo AS (" +
            "    SELECT " +
            "        lu.phone, " +
            "        lu.id, " +
            "        CASE lu.delete_flag " +
            "            WHEN 1 THEN CONCAT(lu.name, '(账号已注销)') " +
            "            ELSE lu.name " +
            "        END AS nick_name, " +
            "        'lawyer' AS type " +
            "    FROM lawyer_user lu " +
            "    UNION ALL " +
            "    SELECT " +
            "        su.phone, " +
            "        su.id, " +
            "        CASE su.delete_flag " +
            "            WHEN 1 THEN CONCAT(su.nick_name, '(账号已注销)') " +
            "            ELSE su.nick_name " +
            "        END AS nick_name, " +
            "        'store' AS type " +
            "    FROM store_user su " +
            "    UNION ALL " +
            "    SELECT " +
            "        lu2.user_phone AS phone, " +
            "        lu2.id, " +
            "        CASE lu2.delete_flag " +
            "            WHEN 1 THEN CONCAT(lu2.user_name, '(账号已注销)') " +
            "            ELSE lu2.user_name " +
            "        END AS nick_name, " +
            "        'user' AS type " +
            "    FROM life_user lu2 " +
            ") " +
            "SELECT " +
            "    luv.*, " +
            "    ui.nick_name AS nickname, " +
            "    ui.phone " +
            "FROM lawyer_user_violation luv " +
            "LEFT JOIN userInfo ui ON (" +
            "    (ui.type = 'lawyer' AND luv.reporting_user_type = 'lawyer' AND ui.id = luv.reporting_user_id) OR " +
            "    (ui.type = 'store' AND luv.reporting_user_type = '1' AND ui.id = luv.reporting_user_id) OR " +
            "    (ui.type = 'user' AND luv.reporting_user_type = '2' AND ui.id = luv.reporting_user_id)" +
            ") " +
            "    ${ew.customSqlSegment}" +
            "</script>")
    List<LawyerUserViolationVo> getViolationList(
            @Param(Constants.WRAPPER) QueryWrapper<LawyerUserViolationVo> queryWrapper
    );
}

