package com.energy.manage.service.task;

import com.energy.manage.common.po.analysisdatarelationrecords.AnalysisDataRelationRecordsPo;
import com.energy.manage.common.po.analysispriorityrecords.AnalysisPriorityRecordsPo;
import com.energy.manage.service.constant.analysis.AnalysisConstants;
import com.energy.manage.service.domain.dto.analysis.AnalysisDto;
import com.energy.manage.service.mappers.analysis.AnalysisResultMapper;
import com.energy.manage.service.mappers.analysisdatarelationrecords.AnalysisDataRelationRecordsMapper;
import com.energy.manage.service.mappers.analysispriorityrecords.AnalysisPriorityRecordsMapper;
import com.energy.manage.service.service.analysis.AnalysisService;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 分析排队任务执行
 * @author chy
 * @date 2025/2/11 16:18
 * @desc 分析任务执行
 */

@Slf4j
@Component
public class AnalysisTask {

    @Autowired
    private AnalysisService analysisService;

    @Autowired
    private AnalysisPriorityRecordsMapper priorityRecordsMapper;

    @Autowired
    private AnalysisResultMapper resultMapper;

    @Autowired
    private AnalysisDataRelationRecordsMapper dataRelationRecordsMapper;

    @XxlJob(value = "analysisTaskExecute")
    public void analysisTaskExecute() {
        log.info("排队分析定时任务开始");
        try{
            int analysisingCount = resultMapper.selectAnalysisProcessingCount();
            if (analysisingCount > 0){
                log.info("当前有正在分析的数据，排队分析返回");
                return;
            }
            //获取要执行的分析任务
            AnalysisPriorityRecordsPo priorityRecordsPo = priorityRecordsMapper.queryFirstPriority();
            if (priorityRecordsPo == null){
                log.info("当前没有要分析的任务在排队中，分析返回");
                return;
            }
            //调用分析
            analysisService.callAnalysis(getAnalysisDto(priorityRecordsPo));
            //更新任务优先级状态为已执行
            priorityRecordsMapper.updateExecuteStatus(priorityRecordsPo.getTaskId(), AnalysisConstants.ON_CALL);
        }catch (Exception e){
            log.error("排队分析定时任务异常",e);
        }

        log.info("排队分析定时任务结束");
    }


    /**
     * 获取要执行的分析任务参数
     * @return
     */
    private AnalysisDto getAnalysisDto(AnalysisPriorityRecordsPo priorityRecordsPo){
        AnalysisDataRelationRecordsPo dataRelationRecordsPo = dataRelationRecordsMapper.selectByTaskId(priorityRecordsPo.getTaskId());
        AnalysisDto returnDto = new AnalysisDto();
        BeanUtils.copyProperties(dataRelationRecordsPo,returnDto);
        //设置分析类型
        returnDto.setConfigAnalysis(Arrays.asList(dataRelationRecordsPo.getConfigAnalysis().split(",")));
        //设置风机
        if(StringUtils.isNotEmpty(dataRelationRecordsPo.getTurbines())){
            returnDto.setTurbines(Arrays.asList(dataRelationRecordsPo.getTurbines().split(",")));
        }
        //设置排除月份
        if(StringUtils.isNotEmpty(dataRelationRecordsPo.getExcludingMonths())) {
            returnDto.setExcludingMonths(Arrays.asList(dataRelationRecordsPo.getExcludingMonths().split(",")));
        }
        //设置有功功率
        if(StringUtils.isNotEmpty(dataRelationRecordsPo.getValueActivePower())) {
            returnDto.setValueActivePower(getDoubleList(dataRelationRecordsPo.getValueActivePower()));
        }
        //设置风机转速
        if(StringUtils.isNotEmpty(dataRelationRecordsPo.getValueGeneratorSpeed())) {
            returnDto.setValueGeneratorSpeed(getDoubleList(dataRelationRecordsPo.getValueGeneratorSpeed()));
        }
        //设置桨距角
        if(StringUtils.isNotEmpty(dataRelationRecordsPo.getValuePitchAngle())) {
            returnDto.setValuePitchAngle(getDoubleList(dataRelationRecordsPo.getValuePitchAngle()));
        }
        //设置风速
        if(StringUtils.isNotEmpty(dataRelationRecordsPo.getValueWindSpeed())) {
            returnDto.setValueWindSpeed(getDoubleList(dataRelationRecordsPo.getValueWindSpeed()));
        }
        return returnDto;
     }

     private List<Double> getDoubleList(String value){
         if(StringUtils.isNotEmpty(value)){
             return Arrays.asList(value.split(",")).stream().map(Double::parseDouble).collect(Collectors.toList());
         }
         return null;
     }
}
