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.PumpRoomCoConvertor;
import com.zcloud.zcGbsServicer.command.convertor.ResourceDeviceCoConvertor;
import com.zcloud.zcGbsServicer.domain.model.PumpRoomE;
import com.zcloud.zcGbsServicer.dto.PumpRoomPageQry;
import com.zcloud.zcGbsServicer.dto.PumpRoomScreenQry;
import com.zcloud.zcGbsServicer.dto.clientobject.PumpRoomCO;
import com.zcloud.zcGbsServicer.dto.clientobject.PumpRoomScreenCO;
import com.zcloud.zcGbsServicer.persistence.dataobject.PumpRoomDO;
import com.zcloud.zcGbsServicer.persistence.dataobject.ResourceDeviceDO;
import com.zcloud.zcGbsServicer.persistence.repository.PumpRoomRepository;
import com.zcloud.zcGbsServicer.persistence.repository.ResourceDeviceRepository;
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:22:16
*/
@Component
@AllArgsConstructor
public class PumpRoomQueryExe {
    private final PumpRoomRepository pumpRoomRepository;
    private final PumpRoomCoConvertor pumpRoomCoConvertor;

    private final ResourceDeviceRepository resourceDeviceRepository;
    private final ResourceDeviceCoConvertor resourceDeviceCoConvertor;

    /**
    * 鍒嗛〉
    *
    * @param qry
    * @return
    */
    public PageResponse<PumpRoomCO> execute(PumpRoomPageQry qry) {
        Map<String, Object> params = PageQueryHelper.toHashMap(qry);
        PageResponse<PumpRoomDO> page = pumpRoomRepository.listPage(params);
        List<PumpRoomDO> records = page.getData();
        if (CollUtil.isEmpty(records)) {
            return PageResponse.of(Collections.emptyList(), 0, page.getPageSize(), page.getPageIndex());
        }
        List<PumpRoomE> es = pumpRoomCoConvertor.converDOsToE(records);
        List<String> pumpRoomIds = es.stream()
                .map(PumpRoomE::getPumpRoomId)
                .collect(Collectors.toList());
        Map<String, List<ResourceDeviceDO>> deviceMap =
                resourceDeviceRepository.listByBizId(pumpRoomIds)
                        .stream()
                        .collect(Collectors.groupingBy(ResourceDeviceDO::getBizId));
        es.forEach(e -> {
            List<ResourceDeviceDO> devices = deviceMap.get(e.getPumpRoomId());
            e.device(resourceDeviceCoConvertor.converDOsToE(CollUtil.emptyIfNull(devices)));
        });
        List<PumpRoomCO> cos = pumpRoomCoConvertor.converEtoCOs(es);
        return PageResponse.of(cos, page.getTotalCount(), page.getPageSize(), page.getPageIndex());
    }

    /**
     * 璇︽儏
     */
    public PumpRoomCO getById(Long id) {
        PumpRoomDO pumpRoomDO = pumpRoomRepository.getById(id);
        if (pumpRoomDO == null) {
            return null;
        }
        PumpRoomE pumpRoomE = pumpRoomCoConvertor.converDOtoE(pumpRoomDO);
        pumpRoomE.deviceInfo(resourceDeviceCoConvertor.converDOsToE(resourceDeviceRepository.listByBizId(CollUtil.newArrayList(pumpRoomDO.getPumpRoomId()))));
        return pumpRoomCoConvertor.converEtoCO(pumpRoomE);
    }

    public PageResponse<PumpRoomScreenCO> listScreen(PumpRoomScreenQry qry) {

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