package com.njcn.device.pms.service.majornetwork.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.njcn.common.pojo.enums.common.DataStateEnum;
import com.njcn.common.pojo.enums.response.CommonResponseEnum;
import com.njcn.common.pojo.exception.BusinessException;
import com.njcn.device.pms.pojo.param.RStatDevSignalParam;
import com.njcn.device.pms.pojo.po.RStatDevSignalD;
import com.njcn.device.pms.pojo.po.PmsTerminal;
import com.njcn.device.pms.pojo.vo.RStatDevSignalVO;
import com.njcn.device.pms.service.majornetwork.ITerminalService;
import com.njcn.device.pms.service.majornetwork.RStatDevSignalDService;
import com.njcn.device.pms.mapper.majornetwork.RStatDevSignalDMapper;
import com.njcn.event.pojo.po.RStatEventOrgQ;
import com.njcn.user.api.DeptFeignClient;
import com.njcn.user.pojo.dto.DeptDTO;
import com.njcn.web.utils.WebUtil;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

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

/**
* @author jianghf
* @description 针对表【r_stat_dev_signal_d(终端通信管理日表)】的数据库操作Service实现
* @createDate 2022-11-11 15:35:35
*/
@Service
@RequiredArgsConstructor
public class RStatDevSignalDServiceImpl extends ServiceImpl<RStatDevSignalDMapper, RStatDevSignalD>
    implements RStatDevSignalDService{

    private final ITerminalService iTerminalService;

    private final DeptFeignClient deptFeignClient;

    @Override
    public List<RStatDevSignalVO> getRStatDevSignalList(RStatDevSignalParam rStatDevSignalParam) {
        //提取查询条件
        String id = rStatDevSignalParam.getId(); //单位id
        String deviceName = rStatDevSignalParam.getDeviceName(); //终端名称
        String deviceStatus = rStatDevSignalParam.getDeviceStatus(); //终端状态
        String runStatus = rStatDevSignalParam.getRunStatus(); //运行状态

        if (StringUtils.isBlank(id)) {
            throw new BusinessException(CommonResponseEnum.NO_DATA, "单位id不可为空");
        }

        //获取所有子部门信息
        List<DeptDTO> deptDTOList = deptFeignClient.getDeptDescendantIndexes(id, WebUtil.filterDeptType()).getData();
//        /*获取下一级子部门信息*/
//        List<DeptDTO> deptDTOList = deptFeignClient.getDepSonDetailByDeptId (id).getData ( );
        if (CollUtil.isEmpty(deptDTOList)) {
            throw new BusinessException(CommonResponseEnum.NO_DATA, "部门不存在");
        }
        //单位id集合
        List<String> orgCodeList = deptDTOList.stream().map(DeptDTO::getCode).collect(Collectors.toList());

        //查询终端数据
        LambdaQueryWrapper<PmsTerminal> terminalWrapper = new LambdaQueryWrapper<>();
        terminalWrapper.in(PmsTerminal::getOrgId, orgCodeList)
                .eq(PmsTerminal::getStatus, DataStateEnum.ENABLE.getCode())
                .like(StringUtils.isNotBlank(deviceName), PmsTerminal::getName, deviceName);
        List<PmsTerminal> terminalList = iTerminalService.list(terminalWrapper);
        //提取终端id
        List<String> terminalIdList = terminalList.stream().map(PmsTerminal::getId).collect(Collectors.toList());
        //将终端信息转为map集合 key: 终端id  value: 终端实体
        Map<String, PmsTerminal> terminalMap = terminalList.stream().collect(Collectors.toMap(PmsTerminal::getId, terminal -> terminal));
        if (CollUtil.isEmpty(terminalIdList)) {
            return new ArrayList<>();
        }

        //查询业务数据
        LambdaQueryWrapper<RStatDevSignalD> businessWrapper = new LambdaQueryWrapper<>();
        businessWrapper.in(RStatDevSignalD::getDeviceId, terminalIdList)
                .eq(StringUtils.isNotBlank(deviceStatus), RStatDevSignalD::getDeviceStatus, deviceStatus)
                .eq(StringUtils.isNotBlank(runStatus), RStatDevSignalD::getRunStatus, runStatus);
        List<RStatDevSignalD> list = this.list(businessWrapper);
        //封装返回数据
        List<RStatDevSignalVO> resultList = list.stream().map(item -> {
            RStatDevSignalVO rStatDevSignalVO = new RStatDevSignalVO();
            BeanUtils.copyProperties(item, rStatDevSignalVO); //设备状态、运行状态、最后通讯时间(更新时间)
            rStatDevSignalVO.setTerminalId(item.getDeviceId()); //终端id(终端编号)
            rStatDevSignalVO.setOrgId(terminalMap.get(item.getDeviceId()).getOrgId()); //单位id
            rStatDevSignalVO.setOrgName(terminalMap.get(item.getDeviceId()).getOrgName()); //单位名称
            rStatDevSignalVO.setTerminalName(terminalMap.get(item.getDeviceId()).getName()); //终端名称
            rStatDevSignalVO.setIp(terminalMap.get(item.getDeviceId()).getIp()); //ip
            rStatDevSignalVO.setPort(terminalMap.get(item.getDeviceId()).getPort()); //端口
            return rStatDevSignalVO;
        }).collect(Collectors.toList());
        Map<String, RStatDevSignalD> signalMap = list.stream().collect(Collectors.toMap(RStatDevSignalD::getDeviceId, terminal -> terminal));

        List<PmsTerminal> notList = terminalList.stream().filter(r -> !signalMap.containsKey(r.getId()))
                .collect(Collectors.toList());
        if(CollectionUtil.isNotEmpty(notList)){
            RStatDevSignalVO rStatDevSignalVO ;
            for (PmsTerminal terminal : notList) {
                rStatDevSignalVO = new RStatDevSignalVO();
                rStatDevSignalVO.setTerminalId(terminal.getId());
                rStatDevSignalVO.setOrgId(terminal.getOrgId());
                rStatDevSignalVO.setOrgName(terminal.getOrgName());
                rStatDevSignalVO.setTerminalName(terminal.getName());
                rStatDevSignalVO.setDeviceStatus("/");
                rStatDevSignalVO.setRunStatus("/");
                rStatDevSignalVO.setIp(terminal.getIp());
                rStatDevSignalVO.setPort(terminal.getPort());
                rStatDevSignalVO.setUpdateTime("/");
                resultList.add(rStatDevSignalVO);
            }
        }
        return resultList;
    }
}




