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

import com.alibaba.cola.dto.MultiResponse;
import com.alibaba.cola.dto.PageResponse;
import com.alibaba.cola.dto.SingleResponse;
import com.alibaba.cola.exception.BizException;
import com.zcloud.gbscommon.utils.PageQueryHelper;
import com.zcloud.safetyDutyList.dto.clientobject.tasklist.*;
import com.zcloud.safetyDutyList.dto.tasklist.FeedbackListQry;
import com.zcloud.safetyDutyList.dto.tasklist.FeedbackPeriodGroupQry;
import com.zcloud.safetyDutyList.persistence.dataobject.tasklist.FeedbackDO;
import com.zcloud.safetyDutyList.persistence.dataobject.tasklist.TaskDetailDO;
import com.zcloud.safetyDutyList.persistence.repository.tasklist.FeedbackRepository;
import com.zcloud.safetyDutyList.persistence.repository.tasklist.TaskDetailRepository;
import lombok.AllArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

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

@Component
@AllArgsConstructor
public class FeedbackQueryExe {
    private final FeedbackRepository feedbackRepository;
    private final TaskDetailRepository taskDetailRepository;

    public MultiResponse<FeedbackPeriodGroupCO> periodGroupList(FeedbackPeriodGroupQry qry) {
        List<FeedbackDO> feedbackList = feedbackRepository.listByTaskDetailId(qry.getTaskDetailId());
        if (feedbackList == null || feedbackList.isEmpty()) {
            return MultiResponse.of(new ArrayList<>());
        }

        Map<String, List<FeedbackDO>> groupedByPeriod = feedbackList.stream()
                .filter(f -> f.getFeedbackPeriodFlag() != null)
                .collect(Collectors.groupingBy(FeedbackDO::getFeedbackPeriodFlag));

        List<FeedbackPeriodGroupCO> result = new ArrayList<>();
        groupedByPeriod.forEach((periodFlag, feedbacks) -> {
            FeedbackPeriodGroupCO co = new FeedbackPeriodGroupCO();
            co.setTaskDetailId(qry.getTaskDetailId());
            co.setFeedbackPeriodFlag(periodFlag);
            co.setFeedbackCount(feedbacks.size());
            if (!feedbacks.isEmpty()) {
                FeedbackDO first = feedbacks.get(0);
                co.setFeedbackPeriodStartTime(first.getFeedbackPeriodStartTime());
                co.setFeedbackPeriodEndTime(first.getFeedbackPeriodEndTime());
                co.setFeedbackUserName(first.getFeedbackUserName());
            }
            result.add(co);
        });
        return MultiResponse.of(result);
    }

    public PageResponse<FeedbackCO> feedbackPage(FeedbackListQry qry) {
        Map<String, Object> params = PageQueryHelper.toHashMap(qry);
        PageResponse<FeedbackDO> pageResponse = feedbackRepository.feedbackPage(params);
        List<FeedbackDO> doList = pageResponse.getData();
        if (doList == null || doList.isEmpty()) {
            return PageResponse.of(new ArrayList<>(), pageResponse.getTotalCount(), pageResponse.getPageSize(), pageResponse.getPageIndex());
        }
        List<FeedbackCO> coList = doList.stream().map(feedbackDO -> {
            FeedbackCO co = new FeedbackCO();
            BeanUtils.copyProperties(feedbackDO, co);
            return co;
        }).collect(Collectors.toList());
        return PageResponse.of(coList, pageResponse.getTotalCount(), pageResponse.getPageSize(), pageResponse.getPageIndex());
    }

    public SingleResponse<FeedbackInfoCO> getByFeedbackId(String feedbackId) {
        FeedbackDO feedbackDO = feedbackRepository.getByFeedbackId(feedbackId);
        if (feedbackDO == null) {
            throw new BizException("反馈记录不存在");
        }
        FeedbackInfoCO co = new FeedbackInfoCO();
        BeanUtils.copyProperties(feedbackDO, co);

        TaskDetailDO detailDO = taskDetailRepository.getByTaskDetailId(feedbackDO.getTaskDetailId());
        if (detailDO != null) {
            co.setExecuteContent(detailDO.getExecuteContent());
            co.setFeedbackCycleType(detailDO.getFeedbackCycleType());
        }
        return SingleResponse.of(co);
    }
}
