package com.zcloud.domain.command.query;

import cn.hutool.core.collection.CollUtil;
import com.alibaba.cola.dto.PageResponse;
import com.jjb.saas.framework.auth.utils.AuthContext;
import com.zcloud.domain.command.convertor.DomainDepartmentCoConvertor;
import com.zcloud.domain.command.convertor.DomainDepartmentUserCoConvertor;
import com.zcloud.domain.dto.DomainDepartmentPageQry;
import com.zcloud.domain.dto.clientobject.DomainDepartmentCO;
import com.zcloud.domain.dto.clientobject.DomainDepartmentListCO;
import com.zcloud.domain.dto.clientobject.DomainDepartmentUserCO;
import com.zcloud.domain.persistence.dataobject.DomainDepartmentDO;
import com.zcloud.domain.persistence.dataobject.DomainDepartmentUserDO;
import com.zcloud.domain.persistence.dataobject.ProjectTaskDO;
import com.zcloud.domain.persistence.repository.CorpInfoRepository;
import com.zcloud.domain.persistence.repository.DomainDepartmentRepository;
import com.zcloud.domain.persistence.repository.DomainDepartmentUserRepository;
import com.zcloud.domain.persistence.repository.ProjectTaskRepository;
import com.zcloud.gbscommon.utils.PageQueryHelper;
import lombok.AllArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

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


/**
 * web-app
 *
 * @Author zhaokai
 * @Date 2026-01-27 11:28:51
 */
@Component
@AllArgsConstructor
public class DomainDepartmentQueryExe {
    private final DomainDepartmentRepository domainDepartmentRepository;
    private final DomainDepartmentCoConvertor domainDepartmentCoConvertor;
    private final DomainDepartmentUserRepository domainDepartmentUserRepository;
    private final DomainDepartmentUserCoConvertor domainDepartmentUserCoConvertor;
    private final ProjectTaskRepository projectTaskRepository;
    private final CorpInfoRepository corpInfoRepository;

    /**
     * 分页
     *
     * @param domainDepartmentPageQry
     * @return
     */
    public PageResponse<DomainDepartmentListCO> execute(DomainDepartmentPageQry domainDepartmentPageQry) {
        Map<String, Object> params = PageQueryHelper.toHashMap(domainDepartmentPageQry);
        PageResponse<DomainDepartmentDO> pageResponse = domainDepartmentRepository.listPage(params);
        List<DomainDepartmentListCO> examCenterCOS = domainDepartmentCoConvertor.converDOsToListCOs(pageResponse.getData());
        //有进行中的不允许修改
        if(CollUtil.isEmpty(examCenterCOS)){
            return PageResponse.of(examCenterCOS, pageResponse.getTotalCount(), pageResponse.getPageSize(), pageResponse.getPageIndex());
        }
        //
        List<String> domainDepartmentIds = examCenterCOS.stream().map(DomainDepartmentListCO::getDomainDepartmentId).collect(Collectors.toList());

        List<ProjectTaskDO> projectTaskDOList = projectTaskRepository.getNoFinishByDomainDepartmentId(domainDepartmentIds);
        //分组
        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::getDomainDepartmentId));
        examCenterCOS.forEach(info -> {
            List<ProjectTaskDO> projectTaskDOS = projectTaskDOMap.get(info.getDomainDepartmentId());
            if(CollUtil.isNotEmpty(projectTaskDOS)){
                info.setUpdateFlag( false);
            }else{
                info.setUpdateFlag( true);
            }
        });


        return PageResponse.of(examCenterCOS, pageResponse.getTotalCount(), pageResponse.getPageSize(), pageResponse.getPageIndex());
    }

    public DomainDepartmentCO getInfoById(Long id) {
        DomainDepartmentDO domainDepartmentDO =domainDepartmentRepository.getInfoById(id);
        if(domainDepartmentDO == null){
            return null;
        }
        DomainDepartmentCO domainDepartmentCO = new DomainDepartmentCO();
        BeanUtils.copyProperties(domainDepartmentDO, domainDepartmentCO);
        //赋值 用户信息
        List<DomainDepartmentUserDO> domainDepartmentUserDOList =domainDepartmentUserRepository.getListByDomainDepartmentId(domainDepartmentDO.getDomainDepartmentId());
        List<DomainDepartmentUserCO> domainDepartmentUserCOList = domainDepartmentUserCoConvertor.converDOsToCOs(domainDepartmentUserDOList);
        domainDepartmentCO.setDomainDepartmentUserList(domainDepartmentUserCOList);
        return domainDepartmentCO;

    }

    public List<DomainDepartmentCO> listAll() {
        //如果不是监管端，则不能查看所有
        // 2026-06-03 需求修改：领域类型与领域组不存在强绑定关系，可以看到所有类型
        Map<String, Object> params = new HashMap<>();
//        corpInfoRepository.isSupper();
//        if(!corpInfoRepository.isSupper()){
//            params.put("corpinfoId", AuthContext.getTenantId());
//        }
        List<DomainDepartmentDO> domainDepartmentDOList =domainDepartmentRepository.listAll(params);
        List<DomainDepartmentCO> domainDepartmentCOList = domainDepartmentCoConvertor.converDOsToCOs(domainDepartmentDOList);
        return domainDepartmentCOList;
    }
}

