package com.njcn.cloud.service.impl.AdminStatistic;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import com.njcn.cloud.enums.app.UserCodeEnum;
import com.njcn.cloud.pojo.vo.AdminStatistic.Integrity;
import com.njcn.cloud.service.AdminStatistic.DataIntegrityService;
import com.njcn.common.pojo.exception.BusinessException;
import com.njcn.device.pq.api.GeneralDeviceInfoClient;
import com.njcn.device.pq.api.LineFeignClient;
import com.njcn.device.pq.api.LineIntegrityClient;
import com.njcn.device.pq.pojo.param.LineBaseQueryParam;
import com.njcn.device.pq.pojo.po.RStatIntegrityD;
import com.njcn.device.pq.pojo.vo.LineDeviceStateVO;
import com.njcn.user.api.UserFeignClient;
import com.njcn.user.pojo.po.User;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

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


/**
 * @description: DataIntegrityServiceImpl
 * @author: denghuajun
 * @time: 2020-01-02 10:05:11
 **/
@Service
@RequiredArgsConstructor
public class DataIntegrityServiceImpl implements DataIntegrityService {

    private final LineIntegrityClient lineIntegrityClient;
    private final UserFeignClient userFeignClient;
    private final GeneralDeviceInfoClient generalDeviceInfoClient;
    private final LineFeignClient lineFeignClient;

    @Override
    public Integrity getIntegrity(String userID) {
        Date date = new Date();
        //开始时间
        Date startTime = DateUtil.beginOfDay(date);
        //根据用户id获取用户部门
        List<User> users = userFeignClient.getUserByIdList(Collections.singletonList(userID)).getData();
        if(CollUtil.isEmpty(users)){
            throw new BusinessException(UserCodeEnum.NO_USER.getMsg());
        }
        //获取监测点ids
        List<String> lindIds = generalDeviceInfoClient.deptGetRunLine(users.get(0).getDeptId()).getData();
        //获取总终端在线率
        LineBaseQueryParam param=new LineBaseQueryParam();
        param.setLineIds(lindIds);
        param.setStartTime(startTime.toString());
        param.setEndTime(date.toString());
        Float data = lineIntegrityClient.getTotalIntegrityByLineIds(param).getData();
        Integrity info=new Integrity();
        info.setTimeStart(startTime);
        info.setTimeEnd(date);
        info.setIntegrityz(data);
        List<Integrity.DataIntegrity> list=new ArrayList<>();
        //获取监测点数据完整性
        List<RStatIntegrityD> integrityDS = lineIntegrityClient.getIntegrityByLineIds(lindIds,startTime.toString(),date.toString()).getData();
        Map<String, Float> onIntegrityMap = integrityDS.stream().collect(Collectors.toMap(RStatIntegrityD::getLineIndex, RStatIntegrityD::getIntegrityData));
        Integrity.DataIntegrity integrity;
        //获取监测点信息
        List<LineDeviceStateVO> lineDetails = lineFeignClient.getAllLine(lindIds).getData();

        for (LineDeviceStateVO lineDetail : lineDetails) {
            integrity=new Integrity.DataIntegrity();
            integrity.setLineIndex(lineDetail.getId());
            integrity.setLineName(lineDetail.getName());
            integrity.setIntegrity(onIntegrityMap.get(lineDetail.getId())==null?0:onIntegrityMap.get(lineDetail.getId()));
            list.add(integrity);
        }
        info.setList(list);
        return info;
    }


}