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

import cn.hutool.core.collection.CollUtil;
import com.google.common.base.Objects;
import com.njcn.common.utils.NjcnBeanUtil;
import com.njcn.device.pq.pojo.po.DataV;
import com.njcn.influx.deprecated.InfluxDBPublicParam;
import com.njcn.influx.utils.InfluxDbUtils;
import com.njcn.prepare.harmonic.pojo.dto.DataVFiveItemDTO;
import com.njcn.prepare.harmonic.service.influxdb.DataVInfluxdbService;
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.List;
import java.util.stream.Collectors;

/**
 * Description:
 * 接口文档访问地址：http://serverIP:port/swagger-ui.html
 * Date: 2023/2/23  10:32【需求编号】
 *
 * @author clam
 * @version V1.0.0
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class DataVInfluxdbServiceImpl implements DataVInfluxdbService {
    private final InfluxDbUtils influxDbUtils;
    /**
     * @param lineIds
     * @param date
     * @param type
     * @Description: 1.1.1.2.1.    主网单位在线监测点数日统计 :data_v表中查询phasic_type = A当天RMS数据数量，数量大于1则该监测点为在线监测点
     * @Param:
     * @return:
     * @Author: clam
     * @Date: 2023/2/23
     */
    @Override
    public Integer queryTransitMeasurementPoints(List<String> lineIds, String date, String type) {
        String startTime = date+" "+"00:00:00";
        String endTime = date+" "+"23:59:59";
        Integer i =0;
        for (String temp:lineIds){
            String Sql = "SELECT count(rms) FROM \"data_v\" where line_id='"+temp+
                    "'and "+ InfluxDBPublicParam.TIME+">='"+startTime+"'and "+ InfluxDBPublicParam.TIME+"<='"+endTime+"' and phasic_type ='A'  and rms>1 "+ InfluxDBPublicParam.TIME_ZONE;
            QueryResult sqlResult4 = influxDbUtils.query(Sql);

            List<QueryResult.Series> series = sqlResult4.getResults().get(0).getSeries();
            Double tem =0.0;
            if(CollUtil.isNotEmpty(series)){
                tem =(Double)series.get(0).getValues().get(0).get(1);
            }
            if(tem>0){
                i++;
            }
        }
        return i;
    }

    /**
     * @param lineId
     * @Description: 获取监测点当天数据
     * @Param: [lineId, date]
     * @return: java.util.List<com.njcn.prepare.harmonic.pojo.vo.DataVOracleVO>
     * @Author: clam
     * @Date: 2023/2/23
     */
    @Override
    public List<DataVFiveItemDTO> getFiveItems(String lineId,String beginTime, String endTime, Integer statisticalInterval) {
        /*按时间分组每组16条，*/
       /*freq,v_thd,v_unbalance 取T项数据*/
        String Sql = "SELECT time, MAX(freq) as freq_max,MIN(freq) as freq_min,MAX(v_thd) as v_thd_max,MIN(v_thd) as v_thd_min,MAX(v_unbalance) as v_unbalance_max,MIN(v_unbalance) as v_unbalance_min  from data_v  where line_id='"+lineId+
                "'and "+ InfluxDBPublicParam.TIME+">='"+beginTime+"'and "+ InfluxDBPublicParam.TIME+"<='"+endTime+"' and phasic_type ='T' and value_type='AVG' group by time("+statisticalInterval+"m ) fill(none)  "+InfluxDBPublicParam.TIME_ZONE;
        QueryResult query = influxDbUtils.query(Sql);
        InfluxDBResultMapper resultMapper = new InfluxDBResultMapper();
        List<DataV> dataVS = resultMapper.toPOJO(query, DataV.class);
        /*rms,rms_lvr 取ABC项数据任意一个数据*/

        String Sql2 = "SELECT time, MAX(rms) as rms_max,MIN(rms) as rms_min,MAX(rms_lvr) as rms_lvr_max,MIN(rms_lvr) as rms_lvr_min  from data_v  where line_id='"+lineId+
                "'and "+ InfluxDBPublicParam.TIME+">='"+beginTime+"'and "+ InfluxDBPublicParam.TIME+"<='"+endTime+"' and phasic_type ='A' and value_type='AVG'  group by time("+statisticalInterval+"m ) fill(none)  "+InfluxDBPublicParam.TIME_ZONE;
        QueryResult query2 = influxDbUtils.query(Sql2);
        InfluxDBResultMapper resultMapper2 = new InfluxDBResultMapper();
        List<DataV> dataVS2 = resultMapper2.toPOJO(query2, DataV.class);


        List<DataVFiveItemDTO> collect = dataVS.stream ( ).map (temp -> {
            DataVFiveItemDTO dataVFiveItemDTO = new DataVFiveItemDTO ( );
            NjcnBeanUtil.copyPropertiesIgnoreCase (temp,dataVFiveItemDTO);
            DataV dataV = dataVS2.stream ( ).filter (temp2 -> Objects.equal (temp.getTime ( ), temp2.getTime ( ))).
                    collect (Collectors.toList ( )).get (0);
            dataVFiveItemDTO.setRmsMax (dataV.getRmsMAX ());
            dataVFiveItemDTO.setRmsMin (dataV.getRmsMIN ( ));
            dataVFiveItemDTO.setRmsLvrMax (dataV.getRmsLvrMAX ());
            dataVFiveItemDTO.setRmsLvrMin (dataV.getRmsLvrMIN ());

            return dataVFiveItemDTO;
        }).collect (Collectors.toList ( ));
        return collect;
    }
}
