package com.njcn.csharmonic.util;

import com.njcn.common.pojo.exception.BusinessException;
import com.njcn.csdevice.api.CsLineFeignClient;
import com.njcn.csdevice.api.EquipmentFeignClient;
import com.njcn.csdevice.constant.DataParam;
import com.njcn.csdevice.enums.AlgorithmResponseEnum;
import com.njcn.csdevice.pojo.dto.CsEquipmentDeliveryDTO;
import com.njcn.csdevice.pojo.po.CsLinePO;
import com.njcn.system.api.DicDataFeignClient;
import com.njcn.system.api.DictTreeFeignClient;
import com.njcn.system.enums.DicDataEnum;
import com.njcn.system.pojo.po.DictData;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Component
@RequiredArgsConstructor
public class InfluxDbParamUtil {

    private final CsLineFeignClient csLineFeignClient;

    private final DicDataFeignClient dicDataFeignClient;

    private final EquipmentFeignClient equipmentFeignClient;
    private final DictTreeFeignClient dictTreeFeignClient;

    /*表名换成了id本方法做转换*/
    public String getTableNameByClassId(String classId){
        DictData data = dicDataFeignClient.getDicDataById(classId).getData();
        if(Objects.isNull(data)){
            throw new BusinessException(AlgorithmResponseEnum.DATA_MISSING.getMessage());
        }
        return data.getCode();
    }

    public String getClDidByLineId(String lineId){
        List<CsLinePO> finalCsLinePOList = csLineFeignClient.queryLineById(Arrays.asList(lineId)).getData();
        if(finalCsLinePOList == null || finalCsLinePOList.isEmpty()){
            throw new BusinessException(AlgorithmResponseEnum.DATA_MISSING.getMessage());
        }
        List<CsEquipmentDeliveryDTO> csEquipmentDeliveryDTOList = equipmentFeignClient.queryDeviceById(Stream.of(finalCsLinePOList.get(0).getDeviceId()).collect(Collectors.toList())).getData();
        if(csEquipmentDeliveryDTOList == null || csEquipmentDeliveryDTOList.isEmpty()){
            throw new BusinessException(AlgorithmResponseEnum.DATA_MISSING.getMessage());
        }
        //新增逻辑:如果是便携式设备的话 直接返回便携式测点的cldid
        if(DataParam.portableDevType.equals(csEquipmentDeliveryDTOList.get(0).getDevType())){
            return finalCsLinePOList.get(0).getClDid()+"";
        }
        //如果是云前置设备,直接返回clDid
        boolean isCLdDevice = DicDataEnum.DEV_CLD.getCode().equals(dictTreeFeignClient.queryById(csEquipmentDeliveryDTOList.get(0).getDevType()).getData().getCode());
        if(isCLdDevice){
            return String.valueOf(finalCsLinePOList.get(0).getLineNo());
        }
        //之后的逻辑还是按照原来的不变
        String position = csLineFeignClient.getPositionById(lineId).getData();
        if (Objects.isNull(position)){
            throw new BusinessException(AlgorithmResponseEnum.POSITION_ERROR);
        }
        String clDid = null;
        String areaCode =  dicDataFeignClient.getDicDataById(position).getData().getCode();
        if (Objects.equals(areaCode, DicDataEnum.OUTPUT_SIDE.getCode())){
            clDid = "0";
        } else if (Objects.equals(areaCode, DicDataEnum.GRID_SIDE.getCode())){
            clDid = "1";
        } else if (Objects.equals(areaCode, DicDataEnum.LOAD_SIDE.getCode())){
            clDid = "2";
        }
        return clDid;
    }
}
