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

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateUtil;
import com.njcn.cloud.enums.app.UserCodeEnum;
import com.njcn.cloud.pojo.vo.AdminStatistic.StaticInfo;
import com.njcn.cloud.service.AdminStatistic.StaticService;
import com.njcn.common.pojo.exception.BusinessException;
import com.njcn.device.pq.api.GeneralDeviceInfoClient;
import com.njcn.device.pq.pojo.param.OnlineRateParam;
import com.njcn.device.pq.pojo.vo.DevDetail;
import com.njcn.device.pq.pojo.vo.RStatOnlineRateVO;
import com.njcn.user.api.UserFeignClient;
import com.njcn.user.pojo.po.User;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @description: StaticServiceImpl
 * @author: denghuajun
 * @time: 2020-01-02 10:07:27
 **/
@Service
@RequiredArgsConstructor
public class StaticServiceImpl implements StaticService {

    private final GeneralDeviceInfoClient generalDeviceInfoClient;
    private final UserFeignClient userFeignClient;


    @Override
    public StaticInfo getStatic(String userID) {
        Date date = new Date();
        //开始时间
//        Date startTime = DateUtil.beginOfDay(date);
        Date startTime = DateUtil.offset(date, DateField.YEAR,-3);
        //根据用户id获取用户部门
        List<User> users = userFeignClient.getUserByIdList(Collections.singletonList(userID)).getData();
        if(CollUtil.isEmpty(users)){
            throw new BusinessException(UserCodeEnum.NO_USER.getMsg());
        }
        //获取终端ids
        List<String> devIds = generalDeviceInfoClient.deptGetRunDev(users.get(0).getDeptId()).getData();
        //获取总终端在线率
        OnlineRateParam param=new OnlineRateParam();
        param.setIds(devIds);
        param.setStartTime(startTime.toString());
        param.setEndTime(date.toString());
        Float data = generalDeviceInfoClient.getTotalOnlineRates(param).getData();
        //获取终端信息
        List<DevDetail> devDetails = generalDeviceInfoClient.getDevInfoByIds(new ArrayList<>()).getData();
        //获取终端在线率
        List<RStatOnlineRateVO> onlineRates = generalDeviceInfoClient.getOnlineRateByDevIds(param).getData();
        //异常终端数据
        long count = devDetails.stream().filter(x -> 0==x.getComFlag()).count();
        StaticInfo info=new StaticInfo();
        info.setTimeStart(startTime);
        info.setTimeEnd(date);
        info.setOnlineRatez(data);
        info.setComError(count);
        List<StaticInfo.DevStatic> list=new ArrayList<>();
        Map<String, BigDecimal> onlineRateMap = onlineRates.stream().collect(Collectors.toMap(RStatOnlineRateVO::getDevIndex, RStatOnlineRateVO::getOnlineRate));
        StaticInfo.DevStatic devStatic;
        for (DevDetail devDetail : devDetails) {
            devStatic=new StaticInfo.DevStatic();
            devStatic.setDevIndex(devDetail.getDevIndex());
            devStatic.setDevName(devDetail.getDevName());
            devStatic.setIp(devDetail.getIp());
            devStatic.setOnlineRate(onlineRateMap.get(devDetail.getDevIndex()).setScale(3, RoundingMode.HALF_UP).floatValue());
            devStatic.setGdName(devDetail.getGdName());
            devStatic.setSubName(devDetail.getBdzName());
            devStatic.setTimeId(devDetail.getTimeID());
            list.add(devStatic);
        }
        info.setList(list);
        return info;
    }

}
