package com.dora.mapper;

import com.dora.entity.PlazaWorkReport;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * 广场作品投诉 Mapper
 * 
 * @author 1818AI
 * @since 2025-11-14
 */
@Mapper
public interface PlazaWorkReportMapper {
    
    /**
     * 插入投诉记录
     */
    @Insert("INSERT INTO plaza_work_report (report_no, work_id, work_no, reporter_id, report_type, report_reason, report_status, create_time) " +
            "VALUES (#{reportNo}, #{workId}, #{workNo}, #{reporterId}, #{reportType}, #{reportReason}, #{reportStatus}, NOW())")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(PlazaWorkReport report);
    
    /**
     * 根据投诉编号查询
     */
    @Select("SELECT * FROM plaza_work_report WHERE report_no = #{reportNo} AND is_deleted = 0")
    PlazaWorkReport findByReportNo(String reportNo);
    
    /**
     * 根据作品ID和投诉人ID查询是否已投诉过
     */
    @Select("SELECT * FROM plaza_work_report WHERE work_id = #{workId} AND reporter_id = #{reporterId} AND is_deleted = 0 LIMIT 1")
    PlazaWorkReport findByWorkIdAndReporterId(@Param("workId") Long workId, @Param("reporterId") Long reporterId);
    
    /**
     * 查询用户的所有投诉（分页）
     */
    @Select("SELECT * FROM plaza_work_report WHERE reporter_id = #{reporterId} AND is_deleted = 0 " +
            "ORDER BY create_time DESC LIMIT #{offset}, #{size}")
    List<PlazaWorkReport> selectByReporterId(@Param("reporterId") Long reporterId, @Param("offset") int offset, @Param("size") int size);
    
    /**
     * 统计用户投诉总数
     */
    @Select("SELECT COUNT(*) FROM plaza_work_report WHERE reporter_id = #{reporterId} AND is_deleted = 0")
    Long countByReporterId(Long reporterId);
    
    /**
     * 管理员查询投诉列表（支持筛选）
     */
    @Select("<script>" +
            "SELECT * FROM plaza_work_report WHERE is_deleted = 0 " +
            "<if test='reportStatus != null and reportStatus != \"\"'> AND report_status = #{reportStatus} </if>" +
            "<if test='reportType != null and reportType != \"\"'> AND report_type = #{reportType} </if>" +
            "<if test='workNo != null and workNo != \"\"'> AND work_no = #{workNo} </if>" +
            "<if test='reporterId != null'> AND reporter_id = #{reporterId} </if>" +
            "ORDER BY create_time DESC LIMIT #{offset}, #{size}" +
            "</script>")
    List<PlazaWorkReport> selectAdminReportList(@Param("reportStatus") String reportStatus,
                                                 @Param("reportType") String reportType,
                                                 @Param("workNo") String workNo,
                                                 @Param("reporterId") Long reporterId,
                                                 @Param("offset") int offset,
                                                 @Param("size") int size);
    
    /**
     * 管理员统计投诉总数（支持筛选）
     */
    @Select("<script>" +
            "SELECT COUNT(*) FROM plaza_work_report WHERE is_deleted = 0 " +
            "<if test='reportStatus != null and reportStatus != \"\"'> AND report_status = #{reportStatus} </if>" +
            "<if test='reportType != null and reportType != \"\"'> AND report_type = #{reportType} </if>" +
            "<if test='workNo != null and workNo != \"\"'> AND work_no = #{workNo} </if>" +
            "<if test='reporterId != null'> AND reporter_id = #{reporterId} </if>" +
            "</script>")
    Long countAdminReportList(@Param("reportStatus") String reportStatus,
                              @Param("reportType") String reportType,
                              @Param("workNo") String workNo,
                              @Param("reporterId") Long reporterId);
    
    /**
     * 更新投诉审核信息
     */
    @Update("UPDATE plaza_work_report SET " +
            "report_status = #{reportStatus}, " +
            "audit_admin_id = #{auditAdminId}, " +
            "audit_admin_name = #{auditAdminName}, " +
            "audit_remark = #{auditRemark}, " +
            "audit_time = NOW(), " +
            "update_time = NOW() " +
            "WHERE report_no = #{reportNo} AND is_deleted = 0")
    int updateAuditInfo(PlazaWorkReport report);
    
    /**
     * 根据作品ID查询投诉列表
     */
    @Select("SELECT * FROM plaza_work_report WHERE work_id = #{workId} AND is_deleted = 0 ORDER BY create_time DESC")
    List<PlazaWorkReport> selectByWorkId(Long workId);
    
    /**
     * 统计待审核投诉数量
     */
    @Select("SELECT COUNT(*) FROM plaza_work_report WHERE report_status = 'pending' AND is_deleted = 0")
    Long countPendingReports();
}

