package com.zksy.base.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zksy.base.domain.CaseInfo;
import com.zksy.base.domain.EquipmentBase;
import com.zksy.base.domain.EquipmentStatus;
import com.zksy.base.domain.EquipmentType;
import com.zksy.base.mapper.CaseInfoMapper;
import com.zksy.base.mapper.EquipmentBaseMapper;
import com.zksy.base.mapper.EquipmentStatusMapper;
import com.zksy.base.mapper.EquipmentTypeMapper;
import com.zksy.base.service.CaseInfoService;
import com.zksy.manhole.dto.in.CaseInfoPageInDTO;
import com.zksy.manhole.dto.out.CaseInfoOutDTO;
import com.zksy.manhole.dto.out.EquipmentStatusOutDTO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
* @author Administrator
* @description 针对表【case_info(案件信息表)】的数据库操作Service实现
* @createDate 2026-06-08 15:01:11
*/
@Slf4j
@Service
public class CaseInfoServiceImpl extends ServiceImpl<CaseInfoMapper, CaseInfo>
    implements CaseInfoService{

    @Autowired
    private EquipmentBaseMapper equipmentBaseMapper;

    @Autowired
    private EquipmentTypeMapper equipmentTypeMapper;

    @Autowired
    private EquipmentStatusMapper equipmentStatusMapper;

    /**
     * 分页查询案件信息列表
     * @param pageInDTO
     * @return
     */
    @Override
    public Page<CaseInfoOutDTO> findByPage(CaseInfoPageInDTO pageInDTO) {
        log.info("分页查询案件信息列表-入参:{}", pageInDTO);
        List<String> equipmentIds = null;
        if(StringUtils.isNotEmpty(pageInDTO.getEquipmentLocation())){
            List<EquipmentBase> equipmentBaseList = equipmentBaseMapper.selectList(new LambdaQueryWrapper<EquipmentBase>()
                    .like(EquipmentBase::getEquipmentLocation, pageInDTO.getEquipmentLocation()));
            if(CollUtil.isEmpty(equipmentBaseList)){
                return new Page<>(pageInDTO.getPageNum(), pageInDTO.getPageSize(), 0);
            }
            equipmentIds = equipmentBaseList.stream().map(EquipmentBase::getEquipmentId).distinct().collect(Collectors.toList());
        }
        Page<CaseInfo> page = new Page<>(pageInDTO.getPageNum(), pageInDTO.getPageSize());
        LambdaQueryWrapper<CaseInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotEmpty(pageInDTO.getCaseNo()), CaseInfo::getCaseNo, pageInDTO.getCaseNo())
                .in(CollUtil.isNotEmpty(equipmentIds), CaseInfo::getDeviceId, equipmentIds)
                .orderByDesc(CaseInfo::getUpdateTime);
        Page<CaseInfo> rs = this.page(page, queryWrapper);
        List<CaseInfoOutDTO> outDTOList = BeanUtil.copyToList(rs.getRecords(), CaseInfoOutDTO.class);
        if (CollUtil.isNotEmpty(outDTOList)) {
            //查询设备状态
            List<String> equipmentIdList = outDTOList.stream().map(CaseInfoOutDTO::getDeviceId).distinct().collect(Collectors.toList());
            List<EquipmentStatus> equipmentStatusList = equipmentStatusMapper.selectList(new LambdaQueryWrapper<EquipmentStatus>()
                    .in(EquipmentStatus::getEquipmentId, equipmentIdList));
            Map<String, EquipmentStatus> equipmentStatusMap = new HashMap<>();
            if(CollUtil.isNotEmpty(equipmentStatusList)){
                equipmentStatusMap.putAll(equipmentStatusList.stream().collect(Collectors.toMap(EquipmentStatus::getEquipmentId, Function.identity(), (key1, key2) -> key2)));
            }
            //查询设备类型
            List<EquipmentBase> equipmentBaseList = equipmentBaseMapper.selectBatchIds(equipmentIdList);
            Map<String, EquipmentBase> equipmentBaseMap = new HashMap<>();
            if(CollUtil.isNotEmpty(equipmentBaseList)){
                equipmentBaseMap.putAll(equipmentBaseList.stream().collect(Collectors.toMap(EquipmentBase::getEquipmentId, Function.identity(), (key1, key2) -> key2)));
            }
            List<String> equipmentTypeIdList = equipmentBaseList.stream().map(EquipmentBase::getEquipmentTypeId).distinct().collect(Collectors.toList());
            List<EquipmentType> equipmentTypeList = equipmentTypeMapper.selectBatchIds(equipmentTypeIdList);
            Map<String, EquipmentType> equipmentTypeMap = new HashMap<>();
            if(CollUtil.isNotEmpty(equipmentTypeList)){
                equipmentTypeMap.putAll(equipmentTypeList.stream().collect(Collectors.toMap(EquipmentType::getId, Function.identity(), (key1, key2) -> key2)));
            }
            for(CaseInfoOutDTO outDTO : outDTOList) {
                EquipmentBase equipmentBase = equipmentBaseMap.get(outDTO.getDeviceId());
                if(equipmentBase != null){
                    outDTO.setDeviceCode(equipmentBase.getEquipmentCode());
                    EquipmentType equipmentType = equipmentTypeMap.get(equipmentBase.getEquipmentTypeId());
                    if(equipmentType != null){
                        outDTO.setEquipmentTypeName(equipmentType.getTypeName());
                    }
                }
                EquipmentStatus equipmentStatus = equipmentStatusMap.get(outDTO.getDeviceId());
                if(equipmentStatus != null){
                    EquipmentStatusOutDTO equipmentStatusOutDTO = BeanUtil.copyProperties(equipmentStatus, EquipmentStatusOutDTO.class);
                    outDTO.setEquipmentStatus(equipmentStatusOutDTO);
                }
            }
        }
        Page<CaseInfoOutDTO> outPage = new Page<>(pageInDTO.getPageNum(), pageInDTO.getPageSize(), rs.getTotal());
        outPage.setRecords(outDTOList);
        return outPage;
    }
}




