package com.njcn.harmonic.service.activepowerrange.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.jeffreyning.mybatisplus.service.MppServiceImpl;
import com.njcn.event.pojo.constant.Param;
import com.njcn.harmonic.mapper.RActivePowerRangeMapper;
import com.njcn.harmonic.pojo.po.RActivePowerRangePO;
import com.njcn.harmonic.pojo.vo.PowerStatisticsTargetVO;
import com.njcn.harmonic.pojo.vo.RActivePowerRangeVO;
import com.njcn.harmonic.service.activepowerrange.RActivePowerRangeService;
import com.njcn.influx.pojo.constant.InfluxDBTableConstant;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 有功功率趋势 服务实现类
 *
 * @author guofeihu
 * @since 2024-08-22
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class RActivePowerRangeServiceImpl extends MppServiceImpl<RActivePowerRangeMapper, RActivePowerRangePO> implements RActivePowerRangeService {


    private DateTimeFormatter formatter = DateTimeFormatter.ofPattern(Param.DATE_FORMAT);
    private final DecimalFormat df = new DecimalFormat(com.njcn.harmonic.constant.Param.DECIMAL_FORMATSTR);
    private SimpleDateFormat sdf = new SimpleDateFormat(Param.DATE_FORMAT);
    private List<String> PHASE = Arrays.asList(InfluxDBTableConstant.PHASE_TYPE_A, InfluxDBTableConstant.PHASE_TYPE_B, InfluxDBTableConstant.PHASE_TYPE_C);

    /**
     * 整个算法的业务逻辑:
     * 1:查询InfluxDb的功率表:data_harmpower_p
     * 2:遍历功率集合将功率(p字段) / line_id监测点绑定的新能源场站中的风电场的额定功率,看落在哪个区间内
     * 3:遍历功率集合将每条功率对应的各个指标(指标的所有相别和统计方式都要一一比较)与国标限值比较看看是否越限(功率对应的各个指标详情请见下方)
     * --------------------------------指标---------------------------------
     * 电压偏差	data_v	vu_dev	电压偏差	如果vu_dev>0,使用上偏差限值对比；如果vu_dev<0,使用下偏差限值对比
     * 谐波电压	data_v	v_thd	电压总谐波总谐波畸变率	有一个指标超标，则谐波电压超标
     * data_harmrate_v	v2-v25	2-25次谐波电压含有率
     * 谐波电流	data_i	i2-i25	2-25次谐波电流幅值	有一个指标超标，则谐波电压超标
     * 三相电压不平衡度	data_v	v_unbalance
     * 电压波动	data_fluc	fluc
     * 长时闪变	data_plt	plt
     * 间谐波电压含有率	data_inharm_v	v1-v16	0.5-15.5次间谐波电压含有率	有一个指标超标，则谐波电压超标
     * 电流不平衡度	data_i	i_unbalance	这个不确定，找宝哥确认下
     *
     * @param 
     * @return
     */

    @Override
    public RActivePowerRangeVO getDataByLineId(String lineId, String startTime, String endTime) {
        LambdaQueryWrapper<RActivePowerRangePO> lambdaQueryWrapper = new LambdaQueryWrapper();
        lambdaQueryWrapper.eq(RActivePowerRangePO::getLineId, lineId)
                .eq(RActivePowerRangePO::getState, "1")
                .between(RActivePowerRangePO::getTimeId, startTime, endTime)
                .orderByAsc(RActivePowerRangePO::getTimeId)
        ;
        List<RActivePowerRangePO> rActivePowerRangePOS = this.baseMapper.selectList(lambdaQueryWrapper);
        if (!rActivePowerRangePOS.isEmpty()) {
            RActivePowerRangeVO rActivePowerRangePO = new RActivePowerRangeVO();
            rActivePowerRangePO.setLineId(lineId);
            rActivePowerRangePO.setTimeId(rActivePowerRangePOS.stream().map(RActivePowerRangePO::getTimeId).collect(Collectors.toList()));

            //多条记录则次数累加,是否越限只要有一个区间越限则统一视为该区间越限
            rActivePowerRangePO.setMinsNum0(rActivePowerRangePOS.stream().mapToInt(RActivePowerRangePO::getMinsNum0).sum());
            rActivePowerRangePO.setMinsNum1(rActivePowerRangePOS.stream().mapToInt(RActivePowerRangePO::getMinsNum1).sum());
            rActivePowerRangePO.setMinsNum2(rActivePowerRangePOS.stream().mapToInt(RActivePowerRangePO::getMinsNum2).sum());
            rActivePowerRangePO.setMinsNum3(rActivePowerRangePOS.stream().mapToInt(RActivePowerRangePO::getMinsNum3).sum());
            rActivePowerRangePO.setMinsNum4(rActivePowerRangePOS.stream().mapToInt(RActivePowerRangePO::getMinsNum4).sum());
            rActivePowerRangePO.setMinsNum5(rActivePowerRangePOS.stream().mapToInt(RActivePowerRangePO::getMinsNum5).sum());
            rActivePowerRangePO.setMinsNum6(rActivePowerRangePOS.stream().mapToInt(RActivePowerRangePO::getMinsNum6).sum());
            rActivePowerRangePO.setMinsNum7(rActivePowerRangePOS.stream().mapToInt(RActivePowerRangePO::getMinsNum7).sum());
            rActivePowerRangePO.setMinsNum8(rActivePowerRangePOS.stream().mapToInt(RActivePowerRangePO::getMinsNum8).sum());
            rActivePowerRangePO.setMinsNum9(rActivePowerRangePOS.stream().mapToInt(RActivePowerRangePO::getMinsNum9).sum());
            rActivePowerRangePO.setIsOrNot0(rActivePowerRangePOS.stream().mapToInt(RActivePowerRangePO::getIsOrNot0).sum());
            rActivePowerRangePO.setIsOrNot1(rActivePowerRangePOS.stream().mapToInt(RActivePowerRangePO::getIsOrNot1).sum());
            rActivePowerRangePO.setIsOrNot2(rActivePowerRangePOS.stream().mapToInt(RActivePowerRangePO::getIsOrNot2).sum());
            rActivePowerRangePO.setIsOrNot3(rActivePowerRangePOS.stream().mapToInt(RActivePowerRangePO::getIsOrNot3).sum());
            rActivePowerRangePO.setIsOrNot4(rActivePowerRangePOS.stream().mapToInt(RActivePowerRangePO::getIsOrNot4).sum());
            rActivePowerRangePO.setIsOrNot5(rActivePowerRangePOS.stream().mapToInt(RActivePowerRangePO::getIsOrNot5).sum());
            rActivePowerRangePO.setIsOrNot6(rActivePowerRangePOS.stream().mapToInt(RActivePowerRangePO::getIsOrNot6).sum());
            rActivePowerRangePO.setIsOrNot7(rActivePowerRangePOS.stream().mapToInt(RActivePowerRangePO::getIsOrNot7).sum());
            rActivePowerRangePO.setIsOrNot8(rActivePowerRangePOS.stream().mapToInt(RActivePowerRangePO::getIsOrNot8).sum());
            rActivePowerRangePO.setIsOrNot9(rActivePowerRangePOS.stream().mapToInt(RActivePowerRangePO::getIsOrNot9).sum());


            rActivePowerRangePO.setMinsTime0(rActivePowerRangePOS.stream().map(RActivePowerRangePO::getMinsTime0).collect(Collectors.toList()));
            rActivePowerRangePO.setMinsTime1(rActivePowerRangePOS.stream().map(RActivePowerRangePO::getMinsTime1).collect(Collectors.toList()));
            rActivePowerRangePO.setMinsTime2(rActivePowerRangePOS.stream().map(RActivePowerRangePO::getMinsTime2).collect(Collectors.toList()));
            rActivePowerRangePO.setMinsTime3(rActivePowerRangePOS.stream().map(RActivePowerRangePO::getMinsTime3).collect(Collectors.toList()));
            rActivePowerRangePO.setMinsTime4(rActivePowerRangePOS.stream().map(RActivePowerRangePO::getMinsTime4).collect(Collectors.toList()));
            rActivePowerRangePO.setMinsTime5(rActivePowerRangePOS.stream().map(RActivePowerRangePO::getMinsTime5).collect(Collectors.toList()));
            rActivePowerRangePO.setMinsTime6(rActivePowerRangePOS.stream().map(RActivePowerRangePO::getMinsTime6).collect(Collectors.toList()));
            rActivePowerRangePO.setMinsTime7(rActivePowerRangePOS.stream().map(RActivePowerRangePO::getMinsTime7).collect(Collectors.toList()));
            rActivePowerRangePO.setMinsTime8(rActivePowerRangePOS.stream().map(RActivePowerRangePO::getMinsTime8).collect(Collectors.toList()));
            rActivePowerRangePO.setMinsTime9(rActivePowerRangePOS.stream().map(RActivePowerRangePO::getMinsTime9).collect(Collectors.toList()));
            return rActivePowerRangePO;
        }
        return null;
    }

    private String minsTimeJson(List<String> list) {
        if (CollUtil.isNotEmpty(list)) {

        }
        return null;
    }
}
