package com.zcloud.basic.info.command.query;

import com.alibaba.cola.dto.PageResponse;
import com.zcloud.basic.info.command.convertor.CorpFormCoConvertor;
import com.zcloud.basic.info.command.convertor.CorpInfoXgfCoConvertor;
import com.zcloud.basic.info.domain.enums.CorpFormTypeEnum;
import com.zcloud.basic.info.dto.CorpInfoXgfPageQry;
import com.zcloud.basic.info.dto.clientobject.CorpInfoXgfCO;
import com.zcloud.basic.info.dto.clientobject.CorpInfoXgfItemCO;
import com.zcloud.basic.info.persistence.dataobject.CorpFormDO;
import com.zcloud.basic.info.persistence.dataobject.CorpInfoDO;
import com.zcloud.basic.info.persistence.repository.CorpFormRepository;
import com.zcloud.basic.info.persistence.repository.CorpInfoRepository;
import com.zcloud.gbscommon.utils.PageQueryHelper;
import lombok.AllArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

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


/**
 * web-app
 *
 * @Author zhaokai
 * @Date 2025-10-31 10:52:09
 */
@Component
@AllArgsConstructor
public class CorpInfoXgfQueryExe {
    private final CorpInfoRepository corpInfoRepository;
    private final CorpInfoXgfCoConvertor corpInfoXgfCoConvertor;
    private final CorpFormRepository corpFormRepository;
    private final CorpFormCoConvertor corpFormCoConvertor;


    /**
     * 分页
     *
     * @param corpInfoXgfPageQry
     * @return
     */
    public PageResponse<CorpInfoXgfCO> execute(CorpInfoXgfPageQry corpInfoXgfPageQry) {
        Map<String, Object> params = PageQueryHelper.toHashMap(corpInfoXgfPageQry);
        PageResponse<CorpInfoDO> pageResponse = corpInfoRepository.listPage(params);
        List<CorpInfoXgfCO> examCenterCOS = corpInfoXgfCoConvertor.converDOsToCOs(pageResponse.getData());
    /*    // 组装营业执照和多选框
        examCenterCOS.forEach(info -> {
            Long corpInfoId = info.getId();
            List<Integer> typeList = Arrays.asList(CorpFormTypeEnum.MAINDEPARTMENT.getCode()
                    , CorpFormTypeEnum.MANAGERDEPARTMENT.getCode(), CorpFormTypeEnum.SUPERVISECORPDEPT.getCode()
                    , CorpFormTypeEnum.CORP_TYPE.getCode(), CorpFormTypeEnum.COMPETENTDEPT.getCode());
            List<CorpFormDO> infoByTypeList = corpFormRepository.getInfoByTypeList(corpInfoId, typeList);

            Map<Integer, List<CorpFormDO>> groupedByType = infoByTypeList.stream()
                    .collect(Collectors.groupingBy(CorpFormDO::getType));
            info.setManagerDepartmentList(corpFormCoConvertor.converDOsToItemCOs(groupedByType.get(CorpFormTypeEnum.MAINDEPARTMENT.getCode())));
            info.setMainDepartmentList(corpFormCoConvertor.converDOsToItemCOs(groupedByType.get(CorpFormTypeEnum.MANAGERDEPARTMENT.getCode())));
            info.setCompetentDeptList(corpFormCoConvertor.converDOsToItemCOs(groupedByType.get(CorpFormTypeEnum.COMPETENTDEPT.getCode())));
            info.setSuperVisecorpDeptList(corpFormCoConvertor.converDOsToItemCOs(groupedByType.get(CorpFormTypeEnum.SUPERVISECORPDEPT.getCode())));
            info.setCorpTypeList(corpFormCoConvertor.converDOsToItemCOs(groupedByType.get(CorpFormTypeEnum.CORP_TYPE.getCode())));
        });*/


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

    public CorpInfoXgfCO info(Long id) {
        CorpInfoDO corpInfoDO = corpInfoRepository.getById(id);
        CorpInfoXgfCO corpInfoXgfCO = new CorpInfoXgfCO();
        BeanUtils.copyProperties(corpInfoDO, corpInfoXgfCO);
        //查找多选按钮
/*
        List<CorpFormDO> employmentformList = corpFormRepository.getInfoByType(corpInfoDO.getId(), CorpFormTypeEnum.EMPLOYMENTFORM.getCode());
        corpInfoXgfCO.setEmploymentformList(corpFormCoConvertor.converDOsToItemCOs(employmentformList));
        List<CorpFormDO> selectFromList = corpFormRepository.getInfoByType(corpInfoDO.getId(), CorpFormTypeEnum.SELECTFROM.getCode());
        corpInfoXgfCO.setSelectfromList(corpFormCoConvertor.converDOsToItemCOs(selectFromList));
        List<CorpFormDO> managerDepartmentList = corpFormRepository.getInfoByType(corpInfoDO.getId(), CorpFormTypeEnum.MANAGERDEPARTMENT.getCode());
        corpInfoXgfCO.setManagerDepartmentList(corpFormCoConvertor.converDOsToItemCOs(managerDepartmentList));

        List<CorpFormDO> mainDepartmentList = corpFormRepository.getInfoByType(corpInfoDO.getId(), CorpFormTypeEnum.MAINDEPARTMENT.getCode());
        corpInfoXgfCO.setMainDepartmentList(corpFormCoConvertor.converDOsToItemCOs(mainDepartmentList));

        List<CorpFormDO> competentDeptList = corpFormRepository.getInfoByType(corpInfoDO.getId(), CorpFormTypeEnum.COMPETENTDEPT.getCode());
        corpInfoXgfCO.setCompetentDeptList(corpFormCoConvertor.converDOsToItemCOs(competentDeptList));

        List<CorpFormDO> superVisecorpDeptList = corpFormRepository.getInfoByType(corpInfoDO.getId(), CorpFormTypeEnum.SUPERVISECORPDEPT.getCode());
        corpInfoXgfCO.setSuperVisecorpDeptList(corpFormCoConvertor.converDOsToItemCOs(superVisecorpDeptList));
        List<CorpFormDO> corpTypeList = corpFormRepository.getInfoByType(corpInfoDO.getId(), CorpFormTypeEnum.CORP_TYPE.getCode());
        corpInfoXgfCO.setCorpTypeList(corpFormCoConvertor.converDOsToItemCOs(corpTypeList));*/
        return corpInfoXgfCO;
    }
}

