package com.zcloud.fire.check.command.query;

import cn.hutool.core.collection.CollUtil;
import com.alibaba.cola.dto.PageResponse;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.jjb.saas.framework.auth.utils.AuthContext;
import com.zcloud.fire.check.command.convertor.FireRegionCoConvertor;
import com.zcloud.fire.check.dto.FireRegionPageQry;
import com.zcloud.fire.check.dto.clientobject.FireRegionCO;
import com.zcloud.fire.check.persistence.dataobject.FireRegionDO;
import com.zcloud.fire.check.persistence.repository.FirePointRepository;
import com.zcloud.fire.check.persistence.repository.FireRegionRepository;
import com.zcloud.gbscommon.utils.PageQueryHelper;
import com.zcloud.gbscommon.utils.Query;
import lombok.AllArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
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 zhaokai
 * @Date 2025-11-26 10:13:09
 */
@Component
@AllArgsConstructor
public class FireRegionQueryExe {
    private final FireRegionRepository fireRegionRepository;
    private final FireRegionCoConvertor fireRegionCoConvertor;
    private final FirePointRepository firePointRepository;

    /**
     * 分页
     *
     * @param fireRegionPageQry
     * @return
     */
    public PageResponse<FireRegionCO> execute(FireRegionPageQry fireRegionPageQry) {
        Map<String, Object> params = PageQueryHelper.toHashMap(fireRegionPageQry);

        PageResponse<FireRegionDO> pageResponse = fireRegionRepository.listPage(params);
        List<FireRegionCO> examCenterCOS = fireRegionCoConvertor.converDOsToCOs(pageResponse.getData());
        if(CollUtil.isEmpty(examCenterCOS)){
            return PageResponse.of(examCenterCOS, pageResponse.getTotalCount(), pageResponse.getPageSize(), pageResponse.getPageIndex());
        }
        //获取区域下点位数
        //examCenterCOS获取id
        List<String> fireRegionIds = examCenterCOS.stream().map(FireRegionCO::getFireRegionId).collect(Collectors.toList());
        Map<String,Long> pointCountMap =firePointRepository.countByFireRegionIdIn(fireRegionIds);
        examCenterCOS.forEach(examCenterCO -> examCenterCO.setPointCount(pointCountMap.getOrDefault(examCenterCO.getFireRegionId(),0L)));
        return PageResponse.of(examCenterCOS, pageResponse.getTotalCount(), pageResponse.getPageSize(), pageResponse.getPageIndex());
    }

    public FireRegionCO getInfoById(Long id) {

        FireRegionDO fireRegionDO = fireRegionRepository.getById(id);
        if(fireRegionDO == null){
            return null;
        }
        FireRegionCO fireRegionCO = new FireRegionCO();
        BeanUtils.copyProperties(fireRegionDO, fireRegionCO);
        return fireRegionCO;
    }

    public List<FireRegionCO> listAll(Long corpinfoId) {
        if(corpinfoId == null){
            corpinfoId = AuthContext.getTenantId();
        }
        return fireRegionCoConvertor.converDOsToCOs(fireRegionRepository.listByCorpinfoId(corpinfoId));
    }
}

