package com.njcn.advance.service.impl;

import cn.hutool.core.collection.CollUtil;
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.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.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.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();
        deviceInfoParam.setDeptIndex(advanceBaseParam.getDeptId());
        //统计类型
        SimpleDTO simpleDTO = new SimpleDTO();
        simpleDTO.setCode(String.valueOf(StatisticsEnum.POWER_NETWORK));
        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;
            }

            List<AdvanceBaseDTO> aList = balanceMapper.getFeatureAmplitude(PubUtils.beginTimeToLocalDateTime(advanceBaseParam.getStartTime()), PubUtils.endTimeToLocalDateTime(advanceBaseParam.getEndTime()), generalDeviceDTO.getLineIndexes());
            Map<String, Float> map = aList.stream().collect(Collectors.toMap(AdvanceBaseDTO::getLineId, AdvanceBaseDTO::getValueA));

            List<AdvanceBaseDTO> a90List = balanceMapper.getSarfi(PubUtils.beginTimeToLocalDateTime(advanceBaseParam.getStartTime()), PubUtils.endTimeToLocalDateTime(advanceBaseParam.getEndTime()), generalDeviceDTO.getLineIndexes(), 0.9f);
            Map<String, Integer> 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);
            Map<String, Integer> 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);
            Map<String, Float> 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 = MapUtils.getFloat(map,lineId,0f);
                qtIdx.sarfi_90 = MapUtils.getInteger(map90,lineId,0);
                qtIdx.sarifi_50 = MapUtils.getInteger(map50,lineId,0);
                qtIdx.r_assi = MapUtils.getInteger(aSeverityListMap,lineId,0);
                qtIdx.r_asei = MapUtils.getInteger(aSeverityListMap,lineId,0);
                pointInfo.setQtIdx(qtIdx);
                listTem.add(pointInfo);
            }
            balanceInfo.setList(listTem);
            balanceInfoList.add(balanceInfo);
        }
        // 打包数据传入dll/so计算结果

        getBalanceUtils.translateData(balanceInfoList);

        return balanceInfoList;
    }
}
