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

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
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.njcn.device.pms.mapper.majornetwork.DistributionMonitorMapper;
import com.njcn.device.pms.mapper.majornetwork.RStatIntegrityDMapper;
import com.njcn.device.pms.pojo.dto.MonitorIntegrityDataDTO;
import com.njcn.device.pms.pojo.param.pwUser.PwUserMonitorParam;
import com.njcn.device.pms.pojo.po.DistributionMonitor;
import com.njcn.device.pms.pojo.vo.pwUser.PwUserMonitorDataVO;
import com.njcn.device.pms.service.ledgerManger.PwUserMonitorDataService;
import com.njcn.device.pms.service.majornetwork.IDistributionMonitorService;
import com.njcn.device.pq.pojo.po.RStatIntegrityD;
import com.njcn.system.api.DicDataFeignClient;
import com.njcn.system.enums.DicDataEnum;
import com.njcn.system.enums.DicDataTypeEnum;
import com.njcn.system.pojo.po.DictData;
import com.njcn.user.api.DeptFeignClient;
import com.njcn.web.factory.PageFactory;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
public class PwUserMonitorDataServiceImpl implements PwUserMonitorDataService {

    private final DeptFeignClient deptFeignClient;

    private final IDistributionMonitorService iDistributionMonitorService;

    private final DicDataFeignClient dicDataFeignClient;

    private final RStatIntegrityDMapper rStatIntegrityDMapper;

    @Override
    public Page<PwUserMonitorDataVO> pageIntegrityData(PwUserMonitorParam pwUserMonitorParam) {
        Page<PwUserMonitorDataVO> pageResult = new Page<>(PageFactory.getPageNum(pwUserMonitorParam),PageFactory.getPageSize(pwUserMonitorParam));
        List<PwUserMonitorDataVO> pwUserMonitorDataVOList = iDistributionMonitorService.getPwUserIds(pwUserMonitorParam);
        if(CollUtil.isEmpty(pwUserMonitorDataVOList)){
            return pageResult;
        }
        List<String> ids = pwUserMonitorDataVOList.stream().map(PwUserMonitorDataVO::getId).collect(Collectors.toList());
        MonitorIntegrityDataDTO monitorIntegrityDataDTO = new MonitorIntegrityDataDTO();
        monitorIntegrityDataDTO.setSearchBeginTime(pwUserMonitorParam.getSearchBeginTime());
        monitorIntegrityDataDTO.setSearchEndTime(pwUserMonitorParam.getSearchEndTime());
        if(Objects.nonNull(pwUserMonitorParam.getLimitValue())){
            monitorIntegrityDataDTO.setLimitValue(pwUserMonitorParam.getLimitValue());
        }
        if(StrUtil.isNotBlank(pwUserMonitorParam.getSortBy())){
            monitorIntegrityDataDTO.setSortBy(pwUserMonitorParam.getSortBy());
            monitorIntegrityDataDTO.setOrderBy(pwUserMonitorParam.getOrderBy());
        }


        Page<RStatIntegrityD> page = rStatIntegrityDMapper.getMonitorIntegrityData(new Page<>(PageFactory.getPageNum(pwUserMonitorParam),PageFactory.getPageSize(pwUserMonitorParam)),monitorIntegrityDataDTO,ids);
        if(CollUtil.isNotEmpty(page.getRecords())){
            Map<String,PwUserMonitorDataVO> map = pwUserMonitorDataVOList.stream().collect(Collectors.toMap(PwUserMonitorDataVO::getId,Function.identity()));
            List<PwUserMonitorDataVO> voList = new ArrayList<>();
            page.getRecords().forEach(item->{
                if(map.containsKey(item.getLineIndex())){
                    PwUserMonitorDataVO pwUserMonitorDataVO =  map.get(item.getLineIndex());
                    pwUserMonitorDataVO.setIntegrityRate(item.getIntegrityData() > 100 ?100:item.getIntegrityData());
                    voList.add(pwUserMonitorDataVO);
                }
            });
            pageResult.setRecords(voList);
            pageResult.setTotal(page.getTotal());
        }
        return pageResult;
    }

    @Override
    public Page<PwUserMonitorDataVO> getPwDeviceOnline(PwUserMonitorParam pwUserMonitorParam) {
        return null;
    }


    /**
     * 超标情况
     */
}
