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.ControlRoomCoConvertor;
import com.zcloud.zcGbsServicer.command.convertor.ResourceDeviceCoConvertor;
import com.zcloud.zcGbsServicer.command.convertor.ResourcePersonCoConvertor;
import com.zcloud.zcGbsServicer.domain.model.ControlRoomE;
import com.zcloud.zcGbsServicer.domain.model.ResourceDeviceE;
import com.zcloud.zcGbsServicer.domain.model.ResourcePersonE;
import com.zcloud.zcGbsServicer.dto.ControlRoomPageQry;
import com.zcloud.zcGbsServicer.dto.ControlRoomScreenQry;
import com.zcloud.zcGbsServicer.dto.clientobject.ControlRoomCO;
import com.zcloud.zcGbsServicer.dto.clientobject.ControlRoomScreenCO;
import com.zcloud.zcGbsServicer.persistence.dataobject.ControlRoomDO;
import com.zcloud.zcGbsServicer.persistence.dataobject.ResourceDeviceDO;
import com.zcloud.zcGbsServicer.persistence.dataobject.ResourcePersonDO;
import com.zcloud.zcGbsServicer.persistence.repository.ControlRoomRepository;
import com.zcloud.zcGbsServicer.persistence.repository.ResourceDeviceRepository;
import com.zcloud.zcGbsServicer.persistence.repository.ResourcePersonRepository;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Component;

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

/**
* web-app
* @Author makejava
* @Date 2026-01-21 17:21:50
*/
@Component
@AllArgsConstructor
public class ControlRoomQueryExe {
    private final ControlRoomRepository controlRoomRepository;
    private final ControlRoomCoConvertor controlRoomCoConvertor;

    private final ResourceDeviceRepository resourceDeviceRepository;
    private final ResourcePersonCoConvertor resourcePersonCoConvertor;
    private final ResourcePersonRepository resourcePersonRepository;
    private final ResourceDeviceCoConvertor resourceDeviceCoConvertor;

    /**
    * 鍒嗛〉
    */
    public PageResponse<ControlRoomCO> execute(ControlRoomPageQry qry) {
        Map<String, Object> params = PageQueryHelper.toHashMap(qry);
        PageResponse<ControlRoomDO> page = controlRoomRepository.listPage(params);

        List<ControlRoomDO> records = page.getData();
        if (CollUtil.isEmpty(records)) {
            return PageResponse.of(Collections.emptyList(), 0, page.getPageSize(), page.getPageIndex());
        }
        List<ControlRoomE> es = controlRoomCoConvertor.converDOsToE(records);
        List<String> roomIds = es.stream()
                .map(ControlRoomE::getRoomId)
                .collect(Collectors.toList());
        Map<String, List<ResourceDeviceDO>> deviceMap =
                resourceDeviceRepository.listByBizId(roomIds)
                        .stream()
                        .collect(Collectors.groupingBy(ResourceDeviceDO::getBizId));
        Map<String, List<ResourcePersonDO>> personMap =
                resourcePersonRepository.listByBizId(roomIds)
                        .stream()
                        .collect(Collectors.groupingBy(ResourcePersonDO::getBizId));
        es.forEach(e -> {
            int deviceCount = CollUtil.size(deviceMap.get(e.getRoomId()));
            int personCount = CollUtil.size(personMap.get(e.getRoomId()));
            e.relationInfo(deviceCount, personCount);
        });

        List<ControlRoomCO> cos = controlRoomCoConvertor.converEtoCOs(es);
        return PageResponse.of(cos, page.getTotalCount(), page.getPageSize(), page.getPageIndex());
    }

    /**
    * 璇︽儏
    */
    public ControlRoomCO getById(Long id) {
        ControlRoomDO rescueTeamDO = controlRoomRepository.getById(id);
        if (rescueTeamDO == null) {
            return null;
        }
        List<ResourceDeviceE> deviceDOs = resourceDeviceCoConvertor.converDOsToE(resourceDeviceRepository.listByBizId(CollUtil.newArrayList(rescueTeamDO.getRoomId())));
        List<ResourcePersonE> personDOs = resourcePersonCoConvertor.converDOsTOEOs(resourcePersonRepository.listByBizId(CollUtil.newArrayList(rescueTeamDO.getRoomId())));
        ControlRoomE rescueTeamE = controlRoomCoConvertor.converDOtoE(rescueTeamDO);
        rescueTeamE.relationData(deviceDOs, personDOs);

        return controlRoomCoConvertor.converEtoCO(rescueTeamE);
    }

    public PageResponse<ControlRoomScreenCO> listScreen(ControlRoomScreenQry qry) {

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