package com.njcn.advance.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.njcn.advance.mapper.BalanceMapper;
import com.njcn.advance.pojo.dto.AdvanceBaseDTO;
import com.njcn.advance.pojo.dto.BalanceInfo;
import com.njcn.advance.pojo.dto.relevent.QtIdx;
import com.njcn.advance.pojo.param.AdvanceBaseParam;
import com.njcn.advance.service.BalanceService;
import com.njcn.advance.utils.GetBalanceUtils;
import com.njcn.common.pojo.dto.SimpleDTO;
import com.njcn.common.pojo.enums.common.ServerEnum;
import com.njcn.common.pojo.exception.BusinessException;
import com.njcn.common.utils.PubUtils;
import com.njcn.device.pq.api.GeneralDeviceInfoClient;
import com.njcn.device.pq.pojo.dto.GeneralDeviceDTO;
import com.njcn.device.pq.pojo.param.DeviceInfoParam;
import com.njcn.event.enums.EventResponseEnum;
import com.njcn.system.pojo.enums.StatisticsEnum;
import lombok.RequiredArgsConstructor;
import org.apache.commons.collections.MapUtils;
import org.springframework.stereotype.Service;

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

/**
 * pqs
 *
 * @author cdf
 * @date 2023/6/19
 */
@Service
@RequiredArgsConstructor
public class BalanceServiceImpl implements BalanceService {

    private final GeneralDeviceInfoClient generalDeviceInfoClient;

    private final BalanceMapper balanceMapper;

    private final GetBalanceUtils getBalanceUtils;


    @Override
    public List<BalanceInfo> getBalanceInfo(AdvanceBaseParam advanceBaseParam) {
        List<BalanceInfo> balanceInfoList = new ArrayList<>();

        DeviceInfoParam deviceInfoParam = new DeviceInfoParam();

        //统计类型
        SimpleDTO simpleDTO = new SimpleDTO();
        simpleDTO.setCode(String.valueOf(StatisticsEnum.POWER_NETWORK));
        deviceInfoParam.setDeptIndex(advanceBaseParam.getDeptId());
        deviceInfoParam.setStatisticalType(simpleDTO);
        deviceInfoParam.setServerName(ServerEnum.EVENT.getName());
        List<GeneralDeviceDTO> generalDeviceDTOList = generalDeviceInfoClient.getPracticalRunDeviceInfo(deviceInfoParam).getData();

        for (GeneralDeviceDTO generalDeviceDTO : generalDeviceDTOList) {
            BalanceInfo balanceInfo = new BalanceInfo();
            balanceInfo.setAreaIndex(generalDeviceDTO.getIndex());
            balanceInfo.setAreaName(generalDeviceDTO.getName());

            if (CollUtil.isEmpty(generalDeviceDTO.getLineIndexes())) {
                balanceInfo.setList(new ArrayList<>());
                continue;
            }

            Map<String, Float> map = new HashMap<>();
            Map<String, Integer> map90 = new HashMap<>();
            Map<String, Integer> map50 = new HashMap<>();
            Map<String, Float> aSeverityListMap = new HashMap<>();
            List<AdvanceBaseDTO> aList = balanceMapper.getFeatureAmplitude(PubUtils.beginTimeToLocalDateTime(advanceBaseParam.getStartTime()), PubUtils.endTimeToLocalDateTime(advanceBaseParam.getEndTime()), generalDeviceDTO.getLineIndexes());
            if (CollUtil.isNotEmpty(aList)) {
                map = aList.stream().collect(Collectors.toMap(AdvanceBaseDTO::getLineId, AdvanceBaseDTO::getValueA));
            }else {
              continue;
            }

            List<AdvanceBaseDTO> a90List = balanceMapper.getSarfi(PubUtils.beginTimeToLocalDateTime(advanceBaseParam.getStartTime()), PubUtils.endTimeToLocalDateTime(advanceBaseParam.getEndTime()), generalDeviceDTO.getLineIndexes(), 0.9f);
            if (CollUtil.isNotEmpty(a90List)) {
                map90 = a90List.stream().collect(Collectors.toMap(AdvanceBaseDTO::getLineId, AdvanceBaseDTO::getValueB));
            }

            List<AdvanceBaseDTO> a50List = balanceMapper.getSarfi(PubUtils.beginTimeToLocalDateTime(advanceBaseParam.getStartTime()), PubUtils.endTimeToLocalDateTime(advanceBaseParam.getEndTime()), generalDeviceDTO.getLineIndexes(), 0.5f);
            if (CollUtil.isNotEmpty(a50List)) {
                map50 = a50List.stream().collect(Collectors.toMap(AdvanceBaseDTO::getLineId, AdvanceBaseDTO::getValueB));
            }

            List<AdvanceBaseDTO> aSeverityList = balanceMapper.getSarfiSeverity(PubUtils.beginTimeToLocalDateTime(advanceBaseParam.getStartTime()), PubUtils.endTimeToLocalDateTime(advanceBaseParam.getEndTime()), generalDeviceDTO.getLineIndexes(), 0.5f);
            if (CollUtil.isNotEmpty(aSeverityList)) {
                aSeverityListMap = aSeverityList.stream().collect(Collectors.toMap(AdvanceBaseDTO::getLineId, AdvanceBaseDTO::getValueA));
            }

            List<BalanceInfo.PointInfo> listTem = new ArrayList<>();
            for (String lineId : generalDeviceDTO.getLineIndexes()) {
                BalanceInfo.PointInfo pointInfo = new BalanceInfo.PointInfo();
                pointInfo.setPointId(lineId);

                QtIdx qtIdx = new QtIdx();



                qtIdx.r_esm = map.getOrDefault(lineId,0f);
                qtIdx.sarfi_90 = map90.getOrDefault(lineId, 0);
                qtIdx.sarifi_50 = map50.getOrDefault(lineId, 0);
                qtIdx.r_assi = aSeverityListMap.getOrDefault(lineId, 0f);
                qtIdx.r_asei = aSeverityListMap.getOrDefault(lineId, 0f);
                pointInfo.setQtIdx(qtIdx);
                listTem.add(pointInfo);
            }
            balanceInfo.setList(listTem);
            balanceInfoList.add(balanceInfo);
        }
        // 打包数据传入dll/so计算结果
        getBalanceUtils.translateData(balanceInfoList);
        return balanceInfoList;
    }
}
