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

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.alibaba.cola.dto.PageResponse;
import com.alibaba.fastjson.JSON;
import com.zcloud.fire.check.command.convertor.FireCheckRecordDeviceCoConvertor;
import com.zcloud.fire.check.command.convertor.FireCheckRecordHiddenCoConvertor;
import com.zcloud.fire.check.command.convertor.FireCheckRecordPointCoConvertor;
import com.zcloud.fire.check.command.convertor.FireCheckRecordUserCoConvertor;
import com.zcloud.fire.check.domain.enums.HiddenCheckTypeEnum;
import com.zcloud.fire.check.dto.CheckPointListQry;
import com.zcloud.fire.check.dto.CheckRecordPointListQry;
import com.zcloud.fire.check.dto.FireCheckRecordPointPageQry;
import com.zcloud.fire.check.dto.clientobject.*;
import com.zcloud.fire.check.persistence.FireCheckPointListDO;
import com.zcloud.fire.check.persistence.dataobject.*;
import com.zcloud.fire.check.persistence.repository.*;
import com.zcloud.gbscommon.utils.PageQueryHelper;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Component;

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


/**
 * web-app
 *
 * @Author zhaokai
 * @Date 2025-12-12 11:10:20
 */
@Component
@AllArgsConstructor
public class FireCheckRecordPointQueryExe {
    private final FireCheckRecordPointRepository fireCheckRecordPointRepository;
    private final FireCheckRecordPointCoConvertor fireCheckRecordPointCoConvertor;
    private final FireCheckRecordDeviceRepository fireCheckRecordDeviceRepository;
    private final FireCheckRecordDeviceCoConvertor fireCheckRecordDeviceCoConvertor;
    private final FireCheckRecordHiddenRepository fireCheckRecordHiddenRepository;
    private final FireCheckRecordHiddenCoConvertor fireCheckRecordHiddenCoConvertor;
    private final FireCheckRecordRepository fireCheckRecordRepository;

    private final FireCheckRecordUserRepository fireCheckRecordUserRepository;
    private final FireCheckRecordUserCoConvertor fireCheckRecordUserCoConvertor;

    /**
     * 分页
     *
     * @param fireCheckRecordPointPageQry
     * @return
     */
    public PageResponse<FireCheckRecordPointCO> execute(FireCheckRecordPointPageQry fireCheckRecordPointPageQry) {
        Map<String, Object> params = PageQueryHelper.toHashMap(fireCheckRecordPointPageQry);
        PageResponse<FireCheckRecordPointDO> pageResponse = fireCheckRecordPointRepository.listPage(params);
        List<FireCheckRecordPointCO> examCenterCOS = fireCheckRecordPointCoConvertor.converDOsToCOs(pageResponse.getData());
        return PageResponse.of(examCenterCOS, pageResponse.getTotalCount(), pageResponse.getPageSize(), pageResponse.getPageIndex());
    }

    public AppFireCheckRecordCO getCheckRecordPointList(CheckRecordPointListQry qry) {
        Map<String, Object> params = PageQueryHelper.toHashMap(qry);
        //查找记录信息
        FireCheckRecordDO fireCheckRecordDO =fireCheckRecordRepository.getCheckRecordInfo(qry.getFireRecordId());
        if(fireCheckRecordDO == null){
            return null;
        }
        AppFireCheckRecordCO appFireCheckRecordCO = new AppFireCheckRecordCO();
        BeanUtil.copyProperties(fireCheckRecordDO,appFireCheckRecordCO);

        List<FireCheckRecordPointDO> fireCheckRecordPointDOList = fireCheckRecordPointRepository.getCheckRecordPointList(params);
        List<AppRecordFireCheckPointListCO> fireCheckPointListCOList = fireCheckRecordPointCoConvertor.converDOsToAppPointCOs(fireCheckRecordPointDOList);
        if(CollUtil.isEmpty(fireCheckPointListCOList)){
            return appFireCheckRecordCO;
        }
        //组装设备信息,fireCheckPointListCOList idlist集合

        List<String> idList = fireCheckPointListCOList.stream().map(AppRecordFireCheckPointListCO::getFireRecordPointId).collect(Collectors.toList());
        //idList 获取打回的设备检查项信息
        List<FireCheckRecordDeviceDO> fireCheckRecordDeviceDOList = fireCheckRecordDeviceRepository.getBackChecKRecordPoiList(idList,qry.getCheckedFlag());
        if(CollUtil.isEmpty(fireCheckRecordDeviceDOList)){
            return appFireCheckRecordCO;
        }

        List<String> fireRecordDeviceIdList = fireCheckRecordDeviceDOList.stream().map(FireCheckRecordDeviceDO::getFireRecordDeviceId).collect(Collectors.toList());
        //获取隐患信息
        List<FireCheckRecordHiddenDO> fireCheckRecordHiddenDOList = fireCheckRecordHiddenRepository.getChecKRecordDeviceList(fireRecordDeviceIdList);
        //fireCheckRecordHiddenDOList 按照设备id key value对象

        Map<String, FireCheckRecordHiddenDO> fireCheckRecordHiddenDOMap = fireCheckRecordHiddenDOList.stream()
                .collect(Collectors.toMap(FireCheckRecordHiddenDO::getFireRecordDeviceId, Function.identity()));

        //fireCheckRecordDeviceDOList 分组
        Map<String, List<FireCheckRecordDeviceDO>> collect = fireCheckRecordDeviceDOList.stream().collect(Collectors.groupingBy(FireCheckRecordDeviceDO::getFireRecordPointId));
        //fireCheckPointListCOList 遍历  合并设备信息
        fireCheckPointListCOList.forEach(item -> {
            List<FireCheckRecordDeviceDO> deviceDOList = collect.get(item.getFireRecordPointId());
            if(CollUtil.isNotEmpty(deviceDOList)){
                List<FireCheckRecordDeviceCO> fireCheckPointDeviceListCOS = fireCheckRecordDeviceCoConvertor.converDOsToDeviceCOs(deviceDOList);
                fireCheckPointDeviceListCOS.forEach(deviceCO -> {
                    //处理检查标准
                    deviceCO.setFireCheckStandardInfo(deviceCO.getFireCheckStandardInfo());
                    //处理检查隐患信息
                    FireCheckRecordHiddenDO hiddenDO = fireCheckRecordHiddenDOMap.get(deviceCO.getFireRecordDeviceId());
                    if(hiddenDO != null){
                        deviceCO.setFireCheckRecordHiddenCO(fireCheckRecordHiddenCoConvertor.convertToCO(hiddenDO));
                    }
                });

                item.setFireCheckRecordDeviceStatusList(fireCheckPointDeviceListCOS);
                //处理检查标准

            }
        });
        appFireCheckRecordCO.setFireCheckPointListCOList(fireCheckPointListCOList);
        //组装隐患信息
        //补充检查用户信息
        List<FireCheckRecordUserDO> fireCheckRecordUserDOList =fireCheckRecordUserRepository.getListByFireRecordId(qry.getFireRecordId());
        if(CollUtil.isNotEmpty(fireCheckRecordUserDOList)){
            appFireCheckRecordCO.setFireCheckRecordUserList(fireCheckRecordUserCoConvertor.converDOsToCOs(fireCheckRecordUserDOList));
        }
        //补充其他隐患信息
        List< FireCheckRecordHiddenDO> fireCheckRecordHiddenDOOtherList = fireCheckRecordHiddenRepository.getChecKRecordHiddenList(qry.getFireRecordId(), HiddenCheckTypeEnum.OTHER_HIDDEN.getCode());
        if(CollUtil.isNotEmpty(fireCheckRecordHiddenDOOtherList)){
            appFireCheckRecordCO.setFireCheckRecordHiddenList(fireCheckRecordHiddenCoConvertor.converDOsToCOs(fireCheckRecordHiddenDOOtherList));
        }
        //组装用户信息
        return appFireCheckRecordCO;
    }
}

