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



import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.njcn.device.biz.commApi.CommLineClient;
import com.njcn.device.biz.commApi.CommTerminalGeneralClient;
import com.njcn.device.pms.mapper.majornetwork.MonitorMapper;
import com.njcn.device.pms.mapper.majornetwork.RStatIntegrityDMapper;
import com.njcn.device.pms.pojo.dto.MonitorIntegrityDataDTO;
import com.njcn.device.pms.pojo.po.Monitor;
import com.njcn.device.pms.service.majornetwork.IRStatIntegrityDService;
import com.njcn.device.pq.pojo.param.LineBaseQueryParam;
import com.njcn.device.pq.pojo.po.RStatIntegrityD;

import com.njcn.user.api.DeptFeignClient;
import com.njcn.web.factory.PageFactory;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

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

/**
 * <p>
 * 数据完整性日表 服务实现类
 * </p>
 *
 * @author hongawen
 * @since 2023-03-28
 */
@Service
@RequiredArgsConstructor
public class RStatIntegrityDServiceImpl extends ServiceImpl<RStatIntegrityDMapper, RStatIntegrityD> implements IRStatIntegrityDService {

    private final DeptFeignClient deptFeignClient;

    private final MonitorMapper monitorMapper;


    @Override
    public Page<MonitorIntegrityDataDTO> getMonitorIntegrityData(MonitorIntegrityDataDTO monitorIntegrityDataDTO) {
        Page<MonitorIntegrityDataDTO> pageResult =new Page<>(PageFactory.getPageNum(monitorIntegrityDataDTO),PageFactory.getPageSize(monitorIntegrityDataDTO));

        List<String> deptCodes = deptFeignClient.getDepSonSelfCodetByDeptId(monitorIntegrityDataDTO.getDeptId()).getData();
        List<Monitor> monitorList = monitorMapper.selectList(new LambdaQueryWrapper<Monitor>().select(Monitor::getId,Monitor::getName,Monitor::getOrgName,Monitor::getPowerrName)
                .in(Monitor::getOrgId,deptCodes).eq(Objects.nonNull(monitorIntegrityDataDTO.getIsUpToGrid()),Monitor::getIsUpToGrid,monitorIntegrityDataDTO.getIsUpToGrid()));

        List<String> monitorIds = monitorList.stream().map(Monitor::getId).collect(Collectors.toList());

        Map<String,Monitor> map = monitorList.stream().collect(Collectors.toMap(Monitor::getId, Function.identity()));

        Page<RStatIntegrityD> page = this.baseMapper.getMonitorIntegrityData(new Page<>(PageFactory.getPageNum(monitorIntegrityDataDTO),PageFactory.getPageSize(monitorIntegrityDataDTO)),monitorIntegrityDataDTO,monitorIds);
        List<MonitorIntegrityDataDTO> monitorIntegrityDataDTOList = page.getRecords().stream().map(item->{
            MonitorIntegrityDataDTO monitorIntegrityDataDTOTem = new MonitorIntegrityDataDTO();
            monitorIntegrityDataDTOTem.setMonitorId(item.getLineIndex());

            Monitor monitor = map.get(item.getLineIndex());
            monitorIntegrityDataDTOTem.setMonitorName(monitor.getName());
            monitorIntegrityDataDTOTem.setDeptName(monitor.getOrgName());
            monitorIntegrityDataDTOTem.setIntegrityRate(item.getIntegrityData());
            monitorIntegrityDataDTOTem.setStationName(monitor.getPowerrName());

            return monitorIntegrityDataDTOTem;
        }).collect(Collectors.toList());
        pageResult.setRecords(monitorIntegrityDataDTOList);
        pageResult.setTotal(page.getTotal());
        return pageResult;
    }
}
