package cn.iocoder.yudao.module.server.dal.mysql.policycommunications;

import java.time.LocalDateTime;
import java.util.*;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.module.server.controller.admin.policycommunications.vo.analysis.*;
import cn.iocoder.yudao.module.server.dal.dataobject.policycommunications.PolicyCommunicationsDO;
import org.apache.ibatis.annotations.Mapper;
import cn.iocoder.yudao.module.server.controller.admin.policycommunications.vo.*;
import org.apache.ibatis.annotations.Param;

/**
 * 政策沟通交流 Mapper
 *
 * @author 超级管理员
 */
@Mapper
public interface PolicyCommunicationsMapper extends BaseMapperX<PolicyCommunicationsDO> {

    default PageResult<PolicyCommunicationsDO> selectPage(PolicyCommunicationsPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<PolicyCommunicationsDO>()
                .eqIfPresent(PolicyCommunicationsDO::getPolicyId, reqVO.getPolicyId())
                .likeIfPresent(PolicyCommunicationsDO::getPolicyTitle, reqVO.getPolicyTitle())
                .eqIfPresent(PolicyCommunicationsDO::getReplyDept, reqVO.getReplyDept())
                .eqIfPresent(PolicyCommunicationsDO::getReplyDeptid, reqVO.getReplyDeptid())
                .likeIfPresent(PolicyCommunicationsDO::getUserName, reqVO.getUserName())
                .eqIfPresent(PolicyCommunicationsDO::getUserId, reqVO.getUserId())
                .eqIfPresent(PolicyCommunicationsDO::getMobile, reqVO.getMobile())
                .eqIfPresent(PolicyCommunicationsDO::getSubmitType, reqVO.getSubmitType())
                .eqIfPresent(PolicyCommunicationsDO::getTitle, reqVO.getTitle())
                .eqIfPresent(PolicyCommunicationsDO::getContent, reqVO.getContent())
                .eqIfPresent(PolicyCommunicationsDO::getMsg, reqVO.getMsg())
                .betweenIfPresent(PolicyCommunicationsDO::getSubmitTime, reqVO.getSubmitTime())
                .eqIfPresent(PolicyCommunicationsDO::getSubmitIsPublic, reqVO.getSubmitIsPublic())
                .eqIfPresent(PolicyCommunicationsDO::getAttachment, reqVO.getAttachment())
                .eqIfPresent(PolicyCommunicationsDO::getDealStatus, reqVO.getDealStatus())
                .eqIfPresent(PolicyCommunicationsDO::getReplayContent, reqVO.getReplayContent())
                .betweenIfPresent(PolicyCommunicationsDO::getReplayTime, reqVO.getReplayTime())
                .eqIfPresent(PolicyCommunicationsDO::getReplayIsPublic, reqVO.getReplayIsPublic())
                // 按sort_order升序，id降序排序
                .orderByAsc(PolicyCommunicationsDO::getSortOrder)
                .orderByDesc(PolicyCommunicationsDO::getId));
    }

    default PageResult<PolicyCommunicationsDO> selectAnonymousPage(PolicyCommunicationsAnonymousQueryReqVO reqVO) {
        LambdaQueryWrapperX<PolicyCommunicationsDO> wrapper = (LambdaQueryWrapperX<PolicyCommunicationsDO>) new LambdaQueryWrapperX<PolicyCommunicationsDO>()
                .eqIfPresent(PolicyCommunicationsDO::getPolicyId, reqVO.getPolicyId())
                .likeIfPresent(PolicyCommunicationsDO::getPolicyTitle, reqVO.getPolicyTitle())
                .eqIfPresent(PolicyCommunicationsDO::getReplyDept, reqVO.getReplyDept())
                .eqIfPresent(PolicyCommunicationsDO::getReplyDeptid, reqVO.getReplyDeptid())
                .likeIfPresent(PolicyCommunicationsDO::getUserName, reqVO.getUserName())
                .eqIfPresent(PolicyCommunicationsDO::getUserId, reqVO.getUserId())
                .eqIfPresent(PolicyCommunicationsDO::getMobile, reqVO.getMobile())
                .eqIfPresent(PolicyCommunicationsDO::getSubmitType, reqVO.getSubmitType())
                .eqIfPresent(PolicyCommunicationsDO::getTitle, reqVO.getTitle())
                .eqIfPresent(PolicyCommunicationsDO::getContent, reqVO.getContent())
                .eqIfPresent(PolicyCommunicationsDO::getMsg, reqVO.getMsg())
                .betweenIfPresent(PolicyCommunicationsDO::getSubmitTime, reqVO.getSubmitTime())
                .eqIfPresent(PolicyCommunicationsDO::getSubmitIsPublic, reqVO.getSubmitIsPublic())
                .eqIfPresent(PolicyCommunicationsDO::getAttachment, reqVO.getAttachment())
                .eqIfPresent(PolicyCommunicationsDO::getReplayContent, reqVO.getReplayContent())
                .betweenIfPresent(PolicyCommunicationsDO::getReplayTime, reqVO.getReplayTime())
                // 处理 replayIsPublic（如果有设置）
                .eqIfPresent(PolicyCommunicationsDO::getReplayIsPublic, reqVO.getReplayIsPublic())
                .orderByAsc(PolicyCommunicationsDO::getSortOrder)
                .orderByDesc(PolicyCommunicationsDO::getId);

        // 处理 dealStatus 条件
        if (reqVO.getDealStatus() != null) {
            wrapper.eq(PolicyCommunicationsDO::getDealStatus, reqVO.getDealStatus());
        } else {
            // 当 dealStatus 为 null 时，查询 0、1、2 状态
            wrapper.in(PolicyCommunicationsDO::getDealStatus, Arrays.asList(0, 1, 2));
        }

        return selectPage(reqVO, wrapper);
    }


    /**
     * 统计查询方法
     */
    Long selectCount(@Param("pageReqVO") PolicyCommunicationsPageReqVO pageReqVO);
    Long selectRepliedCount(@Param("pageReqVO") PolicyCommunicationsPageReqVO pageReqVO);
    Double selectAvgProcessTime(@Param("pageReqVO") PolicyCommunicationsPageReqVO pageReqVO);
    /*List<PolicyCommunicationsRankRespVO.PolicyRankItem> selectPolicyRank(@Param("pageReqVO") PolicyCommunicationsPageReqVO pageReqVO,
                                                                         @Param("order") String order);*/
    List<PolicyCommunicationsRankRespVO.PolicyRankItem> selectPolicyRank(@Param("pageReqVO") PolicyCommunicationsPageReqVO pageReqVO,
                                                                         @Param("order") String order,
                                                                         @Param("limit") Integer limit);
    List<PolicyCommunicationsRankRespVO.DeptRankItem> selectDeptRank(@Param("pageReqVO") PolicyCommunicationsPageReqVO pageReqVO);
    List<PolicyCommunicationsTrendAnalysisRespVO.TrendItem> selectDayTrend(@Param("pageReqVO") PolicyCommunicationsPageReqVO pageReqVO);
    List<PolicyCommunicationsTrendAnalysisRespVO.TrendItem> selectWeekTrend(@Param("pageReqVO") PolicyCommunicationsPageReqVO pageReqVO);
    List<PolicyCommunicationsTrendAnalysisRespVO.TrendItem> selectMonthTrend(@Param("pageReqVO") PolicyCommunicationsPageReqVO pageReqVO);
    List<PolicyCommunicationsDepartmentAnalysisRespVO.DepartmentAnalysisItem> selectDepartmentAnalysis(@Param("pageReqVO") PolicyCommunicationsPageReqVO pageReqVO);
    List<PolicyCommunicationsDepartmentAnalysisRespVO.WorkloadItem> selectWorkloadDistribution(@Param("pageReqVO") PolicyCommunicationsPageReqVO pageReqVO);
    List<PolicyCommunicationsDepartmentAnalysisRespVO.EfficiencyItem> selectEfficiencyRanking(@Param("pageReqVO") PolicyCommunicationsPageReqVO pageReqVO);
    List<PolicyCommunicationsOverdueListRespVO.OverdueItem> selectOverdueList(@Param("pageReqVO") PolicyCommunicationsPageReqVO pageReqVO,
                                                                              @Param("deadline") LocalDateTime deadline);
    List<PolicyCommunicationsContentAnalysisRespVO.TypeDistributionItem> selectTypeDistribution(@Param("pageReqVO") PolicyCommunicationsPageReqVO pageReqVO);
    List<PolicyCommunicationsContentAnalysisRespVO.WordCloudItem> selectWordCloudData(@Param("pageReqVO") PolicyCommunicationsPageReqVO pageReqVO);
    List<PolicyCommunicationsContentAnalysisRespVO.HotWordItem> selectHotWords(@Param("pageReqVO") PolicyCommunicationsPageReqVO pageReqVO);

}