package com.njcn.energy.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.njcn.energy.mapper.EleBindMapper;
import com.njcn.energy.pojo.constant.TableName;
import com.njcn.energy.pojo.param.ElDataBindParam;
import com.njcn.energy.pojo.po.DataArray;
import com.njcn.energy.pojo.po.EleBind;
import com.njcn.energy.pojo.vo.YunCaiDataVO;
import com.njcn.energy.service.IEleBindService;
import com.njcn.influx.utils.InfluxDbUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * pqs
 *
 * @author cdf
 * @date 2022/3/30
 */
@Service
@RequiredArgsConstructor
public class EleBindServiceImpl extends ServiceImpl<EleBindMapper, EleBind> implements IEleBindService {

    private final InfluxDbUtils influxDbUtils;

    @Override
    public boolean addBind(ElDataBindParam elDataBindParam) {
        LambdaQueryWrapper<EleBind> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        List<String> lineList = elDataBindParam.getList().stream().map(EleBind::getLineId).collect(Collectors.toList());
        lambdaQueryWrapper.in(EleBind::getLineId, lineList);
        this.remove(lambdaQueryWrapper);
        return this.saveBatch(elDataBindParam.getList());
    }

    @Override
    public List<EleBind> getList(String ndid) {
        LambdaQueryWrapper<EleBind> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(EleBind::getNdid, ndid);
        return this.list(lambdaQueryWrapper);
    }

    @Override
    public List<YunCaiDataVO> getLineListDetail(List<String> lineList) {
        List<YunCaiDataVO> listRes = new ArrayList<>();
        for (String lineId : lineList) {
            HashMap<String, String> map = new HashMap<>();
            List<DataArray> arrList = this.baseMapper.getArrByLineId(lineId, null);
            if (CollectionUtil.isNotEmpty(arrList)) {
                Map<String, List<DataArray>> classMap = new HashMap<>();
                arrList.stream().filter(item -> item.getClassId() != null).collect(Collectors.groupingBy(DataArray::getClassId, Collectors.toList()))
                        .forEach(classMap::put);
                classMap.forEach((className, dataArrList) -> {
                    StringBuilder stringBuilderMax = new StringBuilder("select ");
                    for (int j = 0; j < dataArrList.size(); j++) {
                        if (j == dataArrList.size() - 1) {
                            stringBuilderMax.append(dataArrList.get(j).getName()).append(" ");
                        } else {
                            stringBuilderMax.append(dataArrList.get(j).getName()).append(", ");
                        }
                    }
                    if (className.equals(TableName.powerData) || className.equals(TableName.powerQuality)) {
                        stringBuilderMax.append("from ").append(className).append("_Real").append(" where").append(" LineId='" + lineId + "'").append(" and Phase = 'T'");
                        List<Map<String, Object>> mapList = influxDbUtils.getResult(stringBuilderMax.toString());
                        if (CollectionUtil.isNotEmpty(mapList)) {
                            mapList.get(0).forEach((i, k) -> map.put(i, k != null ? k.toString() : "/"));
                        }
                    } else {
                        //空调
                        stringBuilderMax.append("from ").append(className).append("_Real").append(" where").append(" LineId='" + lineId + "'");
                        List<Map<String, Object>> mapList = influxDbUtils.getResult(stringBuilderMax.toString());
                        if (CollectionUtil.isNotEmpty(mapList)) {
                            for (int i = 0; i < mapList.size(); i++) {
                                mapList.get(i).forEach((key, value) -> {
                                    if (Objects.nonNull(value)) {
                                        map.put(key, value.toString());
                                    }
                                });
                            }
                        }
                    }
                });
            }
            YunCaiDataVO yunCaiDataVO = new YunCaiDataVO();
            yunCaiDataVO.setLineId(lineId);
            yunCaiDataVO.setMap(map);
            listRes.add(yunCaiDataVO);
        }
        return listRes;

    }
}
