package com.zcloud.domain.command.query;

import cn.hutool.core.collection.CollUtil;
import com.alibaba.cola.dto.PageResponse;
import com.zcloud.domain.command.convertor.DomainGroupCoConvertor;
import com.zcloud.domain.command.convertor.DomainGroupUserCoConvertor;
import com.zcloud.domain.dto.DomainGroupPageQry;
import com.zcloud.domain.dto.DomainGroupQry;
import com.zcloud.domain.dto.clientobject.DomainDepartmentListCO;
import com.zcloud.domain.dto.clientobject.DomainGroupCO;
import com.zcloud.domain.dto.clientobject.DomainGroupInfoCO;
import com.zcloud.domain.dto.clientobject.DomainGroupUserCO;
import com.zcloud.domain.persistence.dataobject.DomainGroupDO;
import com.zcloud.domain.persistence.dataobject.DomainGroupUserDO;
import com.zcloud.domain.persistence.dataobject.ProjectTaskDO;
import com.zcloud.domain.persistence.repository.DomainGroupRepository;
import com.zcloud.domain.persistence.repository.DomainGroupUserRepository;
import com.zcloud.domain.persistence.repository.ProjectTaskRepository;
import com.zcloud.gbscommon.utils.PageQueryHelper;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Component;

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


/**
 * web-app
 *
 * @Author zhaokai
 * @Date 2026-01-27 11:29:27
 */
@Component
@AllArgsConstructor
public class DomainGroupQueryExe {
    private final DomainGroupRepository domainGroupRepository;
    private final DomainGroupCoConvertor domainGroupCoConvertor;
    private final DomainGroupUserRepository domainGroupUserRepository;
    private final DomainGroupUserCoConvertor domainGroupUserCoConvertor;
    private final ProjectTaskRepository projectTaskRepository;

    /**
     * 分页
     *
     * @param domainGroupPageQry
     * @return
     */
    public PageResponse<DomainGroupCO> execute(DomainGroupPageQry domainGroupPageQry) {
        Map<String, Object> params = PageQueryHelper.toHashMap(domainGroupPageQry);
        PageResponse<DomainGroupDO> pageResponse = domainGroupRepository.listPage(params);
        List<DomainGroupCO> examCenterCOS = domainGroupCoConvertor.converDOsToCOs(pageResponse.getData());
        //有进行中的不允许修改
        if(CollUtil.isEmpty(examCenterCOS)){
            return PageResponse.of(examCenterCOS, pageResponse.getTotalCount(), pageResponse.getPageSize(), pageResponse.getPageIndex());
        }
        //
        List<String> groupIdList = examCenterCOS.stream().map(DomainGroupCO::getDomainGroupId).collect(Collectors.toList());

        List<ProjectTaskDO> projectTaskDOList = projectTaskRepository.getNoFinishByDomainGroupId(groupIdList);
        //分组
        if(CollUtil.isEmpty(projectTaskDOList)){
            return PageResponse.of(examCenterCOS, pageResponse.getTotalCount(), pageResponse.getPageSize(), pageResponse.getPageIndex());
        }
        //分组
        Map<String, List<ProjectTaskDO>> projectTaskDOMap = projectTaskDOList.stream().collect(Collectors.groupingBy(ProjectTaskDO::getDomainGroupId));
        examCenterCOS.forEach(info -> {
            List<ProjectTaskDO> projectTaskDOS = projectTaskDOMap.get(info.getDomainGroupId());
            if(CollUtil.isNotEmpty(projectTaskDOS)){
                info.setUpdateFlag( false);
            }else{
                info.setUpdateFlag( true);
            }
        });
        return PageResponse.of(examCenterCOS, pageResponse.getTotalCount(), pageResponse.getPageSize(), pageResponse.getPageIndex());
    }

    public DomainGroupInfoCO getInfoById(Long id) {
        DomainGroupDO domainGroupDO = domainGroupRepository.getInfoById(id);
        if(domainGroupDO == null){
            return null;
        }
        DomainGroupInfoCO domainGroupInfoCO =domainGroupCoConvertor.converDOToInfoCO(domainGroupDO);
        //用户信息
        List<DomainGroupUserDO> domainGroupUserDOList =domainGroupUserRepository.getListByGroupId(domainGroupInfoCO.getDomainGroupId());
        List<DomainGroupUserCO> domainGroupUserCOS = domainGroupUserCoConvertor.converDOsToCOs(domainGroupUserDOList);
        domainGroupInfoCO.setDomainGroupUserList(domainGroupUserCOS);
        return domainGroupInfoCO;
    }

    public List<DomainGroupCO> listAll(DomainGroupQry qry) {
        Map<String, Object> params = PageQueryHelper.toHashMap(qry);
        List<DomainGroupDO> response = domainGroupRepository.listAll(params);
        List<DomainGroupCO> examCenterCOS = domainGroupCoConvertor.converDOsToCOs(response);
        return examCenterCOS;
    }
}

