package com.njcn.csharmonic.service.impl;

import com.njcn.common.pojo.exception.BusinessException;
import com.njcn.csdevice.api.CsLineFeignClient;
import com.njcn.csdevice.api.EquipmentFeignClient;
import com.njcn.csdevice.enums.AlgorithmResponseEnum;
import com.njcn.csdevice.pojo.dto.CsEquipmentDeliveryDTO;
import com.njcn.csdevice.pojo.po.CsLinePO;
import com.njcn.csharmonic.pojo.vo.TemperatureVO;
import com.njcn.csharmonic.service.TemperatureService;
import com.njcn.influx.pojo.dto.StatisticalDataDTO;
import com.njcn.influx.service.CommonService;
import com.njcn.system.api.DicDataFeignClient;
import com.njcn.system.enums.DicDataEnum;
import com.njcn.system.pojo.po.DictData;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.time.Instant;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * Description:
 * Date: 2023/8/30  9:05【需求编号】
 *
 * @author clam
 * @version V1.0.0
 */
@Service
@RequiredArgsConstructor
public class TemperatureServiceImpl implements TemperatureService {
    private final CommonService commonService;
    private final CsLineFeignClient csLineFeignClient;
    private final DicDataFeignClient dicDataFeignClient;
    private final EquipmentFeignClient equipmentFeignClient;
    @Override
    public List<StatisticalDataDTO> queryTemperature(String devId) {
        List<CsEquipmentDeliveryDTO> data1 = equipmentFeignClient.queryDeviceById(Stream.of(devId).collect(Collectors.toList())).getData();
        Integer moduleNumber = data1.get(0).getModuleNumber();
        List<CsLinePO> csLinePOList = csLineFeignClient.queryLineByDevId(devId).getData();
        Optional.ofNullable(csLinePOList).orElseThrow(()-> new BusinessException(AlgorithmResponseEnum.LINE_DATA_ERROR));
        DictData data = dicDataFeignClient.getDicDataByCode(DicDataEnum.OUTPUT_SIDE.getCode()).getData();
        List<CsLinePO> collect = csLinePOList.stream().filter(temp -> Objects.equals(temp.getPosition(), data.getId())).collect(Collectors.toList());
        if(CollectionUtils.isEmpty(collect)){
            throw new BusinessException("不存在输出侧监测点，无温度信息");
        }
        List<StatisticalDataDTO> topTemperature = commonService.getTopTemperature(collect.get(0).getLineId(), "apf_data", "Apf_Temp_Env",data1.get(0).getProcess()+"");
        Instant time = topTemperature.stream().max(Comparator.comparing(StatisticalDataDTO::getTime)).get().getTime();
        List<StatisticalDataDTO> result = new ArrayList<>();
        for(int i = 0; i <=moduleNumber; i++){
            StatisticalDataDTO statisticalData = new StatisticalDataDTO();
            int finalI = i;
            StatisticalDataDTO statisticalDataDTO = topTemperature.stream().filter(tem -> Objects.equals(tem.getClDid(), finalI + "")).findFirst().orElse(new StatisticalDataDTO());
            statisticalDataDTO.setClDid(finalI+"");
            if (!Objects.equals(time,statisticalDataDTO.getTime())) {
                statisticalDataDTO.setValue(null);
            }
            result.add(statisticalDataDTO);
        }
        return result;
    }
}
