package com.njcn.influx.service.impl;

import com.njcn.influx.imapper.PowerQualityMapper;
import com.njcn.influx.pojo.po.PowerQualityData;
import com.njcn.influx.query.InfluxQueryWrapper;
import com.njcn.influx.service.PowerQualityService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Description:
 * Date: 2023/5/18  16:12【需求编号】
 *
 * @author clam
 * @version V1.0.0
 */
@Service
@RequiredArgsConstructor
public class PowerQualityServiceImpl implements PowerQualityService {

    private final PowerQualityMapper powerQualityMapper;
    @Override
    public List<PowerQualityData> getFirstPowerQuality(List<String> lineIds,String columnName) {
        List<PowerQualityData> result1 ;
        InfluxQueryWrapper influxQueryWrapper = new InfluxQueryWrapper(PowerQualityData.class);
        influxQueryWrapper.or(PowerQualityData::getLineId, lineIds)
        .last(columnName,columnName).groupBy(PowerQualityData::getLineId, PowerQualityData::getStatMethod, PowerQualityData::getPhase);

        result1 = powerQualityMapper.getFirstPowerQuality(influxQueryWrapper);

        return result1;
    }

    @Override
    public List<PowerQualityData> getPowerQuality(List<String> lineIds,String columnName, String startTime, String endTime) {
        List<PowerQualityData> result1;

        InfluxQueryWrapper influxQueryWrapper = new InfluxQueryWrapper(PowerQualityData.class);
        influxQueryWrapper.or(PowerQualityData::getLineId, lineIds)
                .percentile(columnName, 95)
                .between(PowerQualityData::getTime, startTime, endTime)
                .groupBy(PowerQualityData::getLineId,PowerQualityData::getStatMethod,PowerQualityData::getPhase);
        result1 = powerQualityMapper.getPowerQuality(influxQueryWrapper);

        return result1;
    }
}
