package com.njcn.prepare.harmonic.service.influxdb.impl;

import cn.hutool.core.date.DateTime;
import com.njcn.device.pq.pojo.po.DataV;
import com.njcn.influx.deprecated.InfluxDBPublicParam;
import com.njcn.influx.deprecated.InfluxDBSqlConstant;
import com.njcn.influx.pojo.constant.InfluxDBTableConstant;
import com.njcn.influx.pojo.po.DataFlicker;
import com.njcn.influx.utils.InfluxDbUtils;
import com.njcn.prepare.harmonic.service.influxdb.DataIntegrityRateInfluxService;
import com.njcn.prepare.harmonic.utils.PublicUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.influxdb.dto.QueryResult;
import org.influxdb.impl.InfluxDBResultMapper;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 数据完整率 influxDB业务
 *
 * @author qijian
 * @version 1.0.0
 * @createTime 2022/11/29 - 10:09
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class DataIntegrityRateInfluxServiceImpl implements DataIntegrityRateInfluxService {

    private final InfluxDbUtils influxDbUtils;

    /**
     * 获取data_v表五项稳态指标日数量
     * @author qijian
     * @date 2022/11/29
     */
    @Override
    public DataV getFiveSteadyData(String lineId, Date date, Integer type) {
        //根据data_v表获取五项稳态指标日数量（count）
        InfluxDBResultMapper resultMapper = new InfluxDBResultMapper();
        String searchSql = "count(freq) as freq_count,count(rms) as rms_count,count(rms_lvr) as rms_lvr_count,count(v_thd) as v_thd_count,count(v_unbalance) as v_unbalance_count ";
        String sql = getAppend(lineId, date,type, searchSql, InfluxDBPublicParam.DATA_V);
        QueryResult query = influxDbUtils.query(sql);
        List<DataV> dataVS = resultMapper.toPOJO(query, DataV.class);
        if (dataVS.size() > 0){
            DataV dataV = dataVS.get(0);
            return dataV;
        }
        return null;
    }

    /**
     * 根据data_flicker表获取两项闪变指标数量
     * @author qijian
     * @date 2022/11/29
     * @return
     */
    @Override
    public DataFlicker getTwoFlickerData(String lineId, Date date, Integer type) {
        //根据data_v表获取五项稳态指标日数量（count）
        InfluxDBResultMapper resultMapper = new InfluxDBResultMapper();
        String searchSql = "count(pst) as pst_count,count(plt) as plt_count ";
        String sql = getAppend(lineId, date,type, searchSql, InfluxDBPublicParam.DATA_FLICKER);
        QueryResult query = influxDbUtils.query(sql);
        List<DataFlicker> dataFlickers = resultMapper.toPOJO(query, DataFlicker.class);
        if (dataFlickers.size() > 0){
            DataFlicker dataFlicker = dataFlickers.get(0);
            return dataFlicker;
        }
        return null;
    }

    /**
     * 拼装sql
     * @param id,date,searchSql,tableName 参数
     * @param type
     * @return 结果
     */
    private String getAppend(String id, Date date, Integer type, String searchSql, String tableName) {
        StringBuilder stringBuilder = new StringBuilder();
        //根据类型获取日期的开始和结束
        Map<String, DateTime> beginAndEndByDate = PublicUtil.getBeginAndEndByDate(type, date);
        //拼接
        stringBuilder.append(InfluxDBSqlConstant.SELECT).append(searchSql)
                .append(InfluxDBSqlConstant.FROM).append(tableName)
                .append(InfluxDBSqlConstant.WHERE).append(InfluxDBPublicParam.TIME).append(InfluxDBSqlConstant.GE).append(InfluxDBSqlConstant.QM).append(beginAndEndByDate.get("startTime")).append(InfluxDBSqlConstant.QM)
                .append(InfluxDBSqlConstant.AND).append(InfluxDBPublicParam.TIME).append(InfluxDBSqlConstant.LE).append(InfluxDBSqlConstant.QM).append(beginAndEndByDate.get("endTime")).append(InfluxDBSqlConstant.QM)
                .append(InfluxDBSqlConstant.AND).append(InfluxDBPublicParam.LINE_ID).append(InfluxDBSqlConstant.EQ).append(InfluxDBSqlConstant.QM).append(id).append(InfluxDBSqlConstant.QM)
                .append(InfluxDBSqlConstant.AND).append(InfluxDBPublicParam.PHASIC_TYPE).append(InfluxDBSqlConstant.EQ).append(InfluxDBSqlConstant.QM).append(InfluxDBTableConstant.PHASE_TYPE_A).append(InfluxDBSqlConstant.QM);
        if (InfluxDBPublicParam.DATA_V.equals(tableName)){
            stringBuilder.append(InfluxDBSqlConstant.AND).append(InfluxDBPublicParam.VALUE_TYPE).append(InfluxDBSqlConstant.EQ).append(InfluxDBSqlConstant.QM).append(InfluxDBTableConstant.AVG).append(InfluxDBSqlConstant.QM);
        }
        return stringBuilder.toString();
    }
}
