package com.zcloud.zcGbsServicer.command.query;

import cn.hutool.core.collection.CollUtil;
import com.alibaba.cola.dto.PageResponse;
import com.alibaba.cola.exception.BizException;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zcloud.gbscommon.utils.PageQueryHelper;
import com.zcloud.gbscommon.utils.Query;
import com.zcloud.zcGbsServicer.command.convertor.RescueTeamCoConvertor;
import com.zcloud.zcGbsServicer.command.convertor.ResourcePersonCoConvertor;
import com.zcloud.zcGbsServicer.domain.model.RescueTeamE;
import com.zcloud.zcGbsServicer.domain.model.ResourcePersonE;
import com.zcloud.zcGbsServicer.dto.RescueTeamNameCheckQry;
import com.zcloud.zcGbsServicer.dto.RescueTeamPageQry;
import com.zcloud.zcGbsServicer.dto.RescueTeamScreenQry;
import com.zcloud.zcGbsServicer.dto.clientobject.RescueTeamCO;
import com.zcloud.zcGbsServicer.dto.clientobject.RescueTeamScreenCO;
import com.zcloud.zcGbsServicer.persistence.dataobject.RescueTeamDO;
import com.zcloud.zcGbsServicer.persistence.dataobject.ResourcePersonDO;
import com.zcloud.zcGbsServicer.persistence.repository.RescueTeamRepository;
import com.zcloud.zcGbsServicer.persistence.repository.ResourcePersonRepository;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Component;

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

/**
 * web-app
 * @Author makejava
 * @Date 2026-01-21 17:23:00
 */
@Component
@AllArgsConstructor
public class RescueTeamQueryExe {
    private final RescueTeamRepository rescueTeamRepository;
    private final RescueTeamCoConvertor rescueTeamCoConvertor;

    private final ResourcePersonRepository resourcePersonRepository;
    private final ResourcePersonCoConvertor resourcePersonCoConvertor;

    /**
     * 分页
     */
    public PageResponse<RescueTeamCO> execute(RescueTeamPageQry rescueTeamPageQry) {
        Map<String, Object> params = PageQueryHelper.toHashMap(rescueTeamPageQry);
        PageResponse<RescueTeamDO> pageResponse = rescueTeamRepository.listPage(params);
        List<RescueTeamCO> rescueTeamList = rescueTeamCoConvertor.converDOsToCOs(pageResponse.getData());
        if (CollUtil.isEmpty(rescueTeamList)) {
            return toPageResponse(pageResponse, rescueTeamList);
        }
        List<String> teamIds = rescueTeamList.stream()
                .map(RescueTeamCO::getTeamId)
                .collect(Collectors.toList());
        List<ResourcePersonDO> allMembers = resourcePersonRepository.listByBizId(teamIds);
        if (CollUtil.isNotEmpty(allMembers)) {
            Map<String, List<ResourcePersonDO>> memberMap = allMembers.stream().collect(Collectors.groupingBy(ResourcePersonDO::getBizId));
            for (RescueTeamCO team : rescueTeamList) {
                List<ResourcePersonDO> members = memberMap.get(team.getTeamId());
                if (CollUtil.isNotEmpty(members)) {
                    team.setMemberCount(members.size());
                }
            }
        }
        return toPageResponse(pageResponse, rescueTeamList);
    }

    /**
     * 可视化大屏消防救援队查询
     */
    public PageResponse<RescueTeamScreenCO> listScreen(RescueTeamScreenQry qry) {

        Map<String, Object> params = PageQueryHelper.toHashMap(qry);
        IPage<RescueTeamDO> page = new Query<RescueTeamDO>().getPage(params);
        PageResponse<RescueTeamDO> pageResponse = rescueTeamRepository.listScreen(page, params);
        List<RescueTeamScreenCO> data = rescueTeamCoConvertor.converDOsToScreenCOs(pageResponse.getData());
        return PageResponse.of(data, pageResponse.getTotalCount(), pageResponse.getPageSize(), pageResponse.getPageIndex());
    }

    private PageResponse<RescueTeamCO> toPageResponse(PageResponse<RescueTeamDO> sourcePage, List<RescueTeamCO> data) {
        return PageResponse.of(data, sourcePage.getTotalCount(), sourcePage.getPageSize(), sourcePage.getPageIndex());
    }

    /**
     * 详情
     */
    public RescueTeamCO getById(Long id) {
        RescueTeamDO rescueTeamDO = rescueTeamRepository.getById(id);
        if (rescueTeamDO == null) {
            return null;
        }
        RescueTeamE rescueTeamE = rescueTeamCoConvertor.converDOToEO(rescueTeamDO);
        List<ResourcePersonE> memberList = resourcePersonCoConvertor.converDOsTOEOs(resourcePersonRepository.listByBizId(CollUtil.newArrayList(rescueTeamDO.getTeamId())));
        if (CollUtil.isNotEmpty(memberList)) {
            rescueTeamE.memberInfo(memberList);
        }
        return rescueTeamCoConvertor.converEToCO(rescueTeamE);
    }

    public Boolean checkName(RescueTeamNameCheckQry qry) {
        return rescueTeamRepository.existsByTeamName(qry.getTeamName(), qry.getId());
    }
}
