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

import cn.hutool.core.collection.CollUtil;
import com.alibaba.cola.dto.MultiResponse;
import com.alibaba.cola.dto.PageResponse;
import com.alibaba.cola.dto.SingleResponse;
import com.zcloud.gbscommon.utils.PageQueryHelper;
import com.zcloud.safetyDutyList.command.convertor.tasklist.TaskListCoConvertor;
import com.zcloud.safetyDutyList.dto.clientobject.tasklist.*;
import com.zcloud.safetyDutyList.dto.tasklist.*;
import com.zcloud.safetyDutyList.persistence.dataobject.tasklist.*;
import com.zcloud.safetyDutyList.persistence.repository.tasklist.TaskListRepository;
import com.zcloud.safetyDutyList.persistence.repository.tasklist.FeedbackRepository;
import com.zcloud.safetyDutyList.persistence.repository.tasklist.FeedbackExceptionRepository;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Component;

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

/**
 * 清单查询执行器
 * <p>
 * 处理安全责任清单相关的查询业务：
 *   - 清单分页列表查询
 *   - 清单详情查询
 *   - 反馈异常分页查询
 *   - 清单统计
 */
@Component
@AllArgsConstructor
public class TaskListQueryExe {
    private final TaskListRepository taskListRepository;
    private final FeedbackRepository feedbackRepository;
    private final FeedbackExceptionRepository feedbackExceptionRepository;
    private final TaskListCoConvertor taskListCoConvertor;

    public PageResponse<TaskListCO> listPage(TaskListPageQry qry) {
        Map<String, Object> params = PageQueryHelper.toHashMap(qry);
        PageResponse<TaskListDO> pageResponse = taskListRepository.listPage(params);
        List<TaskListCO> coList = taskListCoConvertor.converDOsToCOs(pageResponse.getData());
        if (CollUtil.isEmpty(coList)) {
            return PageResponse.of(coList, pageResponse.getTotalCount(), pageResponse.getPageSize(), pageResponse.getPageIndex());
        }
        return PageResponse.of(coList, pageResponse.getTotalCount(), pageResponse.getPageSize(), pageResponse.getPageIndex());
    }

    public SingleResponse<TaskListCO> getInfoById(Long id) {
        TaskListDO taskListDO = taskListRepository.getInfoById(id);
        if (taskListDO == null) {
            return SingleResponse.buildFailure("NOT_FOUND", "任务清单不存在");
        }
        TaskListCO co = taskListCoConvertor.converDOToCO(taskListDO);
        return SingleResponse.of(co);
    }

    public PageResponse<FeedbackCO> feedbackPage(FeedbackPageQry qry) {
        Map<String, Object> params = PageQueryHelper.toHashMap(qry);
        PageResponse<FeedbackDO> pageResponse = feedbackRepository.feedbackPage(params);
        List<FeedbackCO> coList = taskListCoConvertor.converFeedbackDOsToCOs(pageResponse.getData());
        return PageResponse.of(coList, pageResponse.getTotalCount(), pageResponse.getPageSize(), pageResponse.getPageIndex());
    }

    public PageResponse<FeedbackExceptionCO> exceptionPage(FeedbackExceptionPageQry qry) {
        Map<String, Object> params = PageQueryHelper.toHashMap(qry);
        PageResponse<FeedbackExceptionDO> pageResponse = feedbackExceptionRepository.exceptionPage(params);
        List<FeedbackExceptionCO> coList = taskListCoConvertor.converExceptionDOsToCOs(pageResponse.getData());
        return PageResponse.of(coList, pageResponse.getTotalCount(), pageResponse.getPageSize(), pageResponse.getPageIndex());
    }

    public MultiResponse<TaskListStatisticsCO> statistics(TaskListStatisticsQry qry) {
        Map<String, Object> params = PageQueryHelper.toHashMap(qry);
        List<TaskListDO> allList = taskListRepository.list(params);

        Map<Long, List<TaskListDO>> groupedByCorp = allList.stream()
                .filter(d -> d.getCreateCorpId() != null)
                .collect(Collectors.groupingBy(TaskListDO::getCreateCorpId));

        List<TaskListStatisticsCO> result = new ArrayList<>();
        groupedByCorp.forEach((corpId, taskLists) -> {
            TaskListStatisticsCO co = new TaskListStatisticsCO();
            co.setCreateCorpId(corpId);
            co.setTaskListCount(taskLists.size());
            result.add(co);
        });
        return MultiResponse.of(result);
    }
}
