package com.zcloud.safetyDutyList.command.query.tasklist;

import com.alibaba.cola.dto.PageResponse;
import com.zcloud.gbscommon.utils.PageQueryHelper;
import com.zcloud.safetyDutyList.dto.clientobject.tasklist.CorpStatisticsCO;
import com.zcloud.safetyDutyList.dto.clientobject.tasklist.EvaluationListCO;
import com.zcloud.safetyDutyList.dto.clientobject.tasklist.IssueStatisticsCO;
import com.zcloud.safetyDutyList.dto.tasklist.CorpStatisticsQry;
import com.zcloud.safetyDutyList.dto.tasklist.EvaluationListQry;
import com.zcloud.safetyDutyList.dto.tasklist.IssueStatisticsQry;
import com.zcloud.safetyDutyList.persistence.dataobject.tasklist.CorpStatisticsDO;
import com.zcloud.safetyDutyList.persistence.dataobject.tasklist.EvaluationListDO;
import com.zcloud.safetyDutyList.persistence.dataobject.tasklist.IssueStatisticsDO;
import com.zcloud.safetyDutyList.persistence.repository.tasklist.TaskListRepository;
import lombok.AllArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 统计查询执行器
 * <p>
 * 处理安全责任清单相关的统计查询业务，通过Mapper层联表聚合SQL一次性获取结果：
 *   - 企业统计（按企业分组统计完成和关闭的清单数量）
 *   - 考核清单查询（按企业分组统计清单数量）
 *   - 下发统计（按执行企业分组统计已下发的清单数量）
 */
@Component
@AllArgsConstructor
public class StatisticsQueryExe {
    private final TaskListRepository taskListRepository;

    public PageResponse<CorpStatisticsCO> corpStatistics(CorpStatisticsQry qry) {
        Map<String, Object> params = PageQueryHelper.toHashMap(qry);
        PageResponse<CorpStatisticsDO> pageResponse = taskListRepository.corpStatistics(params);
        List<CorpStatisticsCO> coList = pageResponse.getData().stream().map(d -> {
            CorpStatisticsCO co = new CorpStatisticsCO();
            BeanUtils.copyProperties(d, co);
            return co;
        }).collect(Collectors.toList());
        return PageResponse.of(coList, pageResponse.getTotalCount(), pageResponse.getPageSize(), pageResponse.getPageIndex());
    }

    public PageResponse<EvaluationListCO> evaluationList(EvaluationListQry qry) {
        Map<String, Object> params = PageQueryHelper.toHashMap(qry);
        PageResponse<EvaluationListDO> pageResponse = taskListRepository.evaluationList(params);
        List<EvaluationListCO> coList = pageResponse.getData().stream().map(d -> {
            EvaluationListCO co = new EvaluationListCO();
            BeanUtils.copyProperties(d, co);
            return co;
        }).collect(Collectors.toList());
        return PageResponse.of(coList, pageResponse.getTotalCount(), pageResponse.getPageSize(), pageResponse.getPageIndex());
    }

    public PageResponse<IssueStatisticsCO> issueStatistics(IssueStatisticsQry qry) {
        Map<String, Object> params = PageQueryHelper.toHashMap(qry);
        PageResponse<IssueStatisticsDO> pageResponse = taskListRepository.issueStatistics(params);
        List<IssueStatisticsCO> coList = pageResponse.getData().stream().map(d -> {
            IssueStatisticsCO co = new IssueStatisticsCO();
            BeanUtils.copyProperties(d, co);
            return co;
        }).collect(Collectors.toList());
        return PageResponse.of(coList, pageResponse.getTotalCount(), pageResponse.getPageSize(), pageResponse.getPageIndex());
    }
}
