package com.njcn.influx.service.impl;

import com.njcn.influx.imapper.CommonMapper;
import com.njcn.influx.pojo.constant.InfluxDBTableConstant;
import com.njcn.influx.pojo.dto.StatisticalDataDTO;
import com.njcn.influx.query.InfluxQueryWrapper;
import com.njcn.influx.service.CommonService;
import com.njcn.influx.utils.ReflectUitl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Objects;

/**
 * Description:
 * Date: 2023/6/2  16:04【需求编号】
 *
 * @author clam
 * @version V1.0.0
 */
@Service
@RequiredArgsConstructor
public class CommonServiceImpl implements CommonService {

    private final CommonMapper commonMapper;

    @Override
    public StatisticalDataDTO getLineRtData(String lineId, String tableName, String columnName, String phasic, String dataType) {
        InfluxQueryWrapper influxQueryWrapper = new InfluxQueryWrapper(tableName,StatisticalDataDTO.class);
        influxQueryWrapper.select(StatisticalDataDTO::getLineId)
                .select(StatisticalDataDTO::getPhaseType)
                .select(StatisticalDataDTO::getValueType)
                .last(columnName)
                .eq(InfluxDBTableConstant.LINE_ID,lineId)
                .eq(InfluxDBTableConstant.PHASIC_TYPE,phasic)
                .eq(InfluxDBTableConstant.VALUE_TYPE,dataType);
        return commonMapper.getLineRtData(influxQueryWrapper);
    }

    @Override
    public List<StatisticalDataDTO> getDeviceRtData(List<String> lineIds, String tableName, String columnName, String phasic, String dataType) {
        InfluxQueryWrapper influxQueryWrapper = new InfluxQueryWrapper(tableName,StatisticalDataDTO.class);
        influxQueryWrapper.select(StatisticalDataDTO::getLineId)
                .select(StatisticalDataDTO::getPhaseType)
                .select(StatisticalDataDTO::getValueType)
                .last(columnName)
                .or(InfluxDBTableConstant.LINE_ID,lineIds)
                .eq(InfluxDBTableConstant.PHASIC_TYPE,phasic)
                .eq(InfluxDBTableConstant.VALUE_TYPE,dataType).groupBy(InfluxDBTableConstant.LINE_ID);
        return commonMapper.getDeviceRtData(influxQueryWrapper);
    }

    @Override
    public List<StatisticalDataDTO> getDeviceRtDataByTime(List<String> lineIds, String tableName, String columnName, String phasic, String dataType, String startTime, String endTime) {
        InfluxQueryWrapper influxQueryWrapper = new InfluxQueryWrapper(tableName,StatisticalDataDTO.class);
        influxQueryWrapper.select(StatisticalDataDTO::getLineId)
                .select(StatisticalDataDTO::getPhaseType)
                .select(StatisticalDataDTO::getValueType)
                .select(columnName,InfluxDBTableConstant.VALUE)
                .or(InfluxDBTableConstant.LINE_ID,lineIds)
                .eq(InfluxDBTableConstant.PHASIC_TYPE,phasic)
                .between(InfluxDBTableConstant.TIME, startTime, endTime)
                .eq(InfluxDBTableConstant.VALUE_TYPE,dataType);
        return commonMapper.getDeviceRtDataByTime(influxQueryWrapper);
    }

    @Override
    public StatisticalDataDTO getLineHistoryData(String lineId, String tableName, String columnName, String startTime, String endTime) {
        InfluxQueryWrapper influxQueryWrapper = new InfluxQueryWrapper(tableName,StatisticalDataDTO.class);
        influxQueryWrapper.max(columnName,InfluxDBTableConstant.MAX_VALUE)
                .min(columnName,InfluxDBTableConstant.MIN_VALUE)
                .eq(InfluxDBTableConstant.LINE_ID,lineId)
                .between(InfluxDBTableConstant.TIME, startTime, endTime);
        return commonMapper.getLineHistoryData(influxQueryWrapper);
    }

    @Override
    public StatisticalDataDTO selectBySql(StringBuilder sql) {
        return commonMapper.selectBySql(sql);
    }
}
