package com.energy.manage.service.task;

import com.energy.manage.service.constant.analysis.AnalysisConstants;
import com.energy.manage.service.domain.dto.analysis.AnalysisDto;
import com.energy.manage.service.domain.vo.analysis.AnalysisTypeVo;
import com.energy.manage.service.domain.vo.datatransfer.DataTransferVo;
import com.energy.manage.service.mappers.analysis.AnalysisResultMapper;
import com.energy.manage.service.mappers.analysis.AnalysisTypeMapper;
import com.energy.manage.service.mappers.datatransfer.DataTransferMapper;
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.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

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

/**
 * 自动分析定时任务
 * @author chy
 * @date 2024/6/2 21:35
 * @desc
 */
@Slf4j
@Component
public class AutoAnalysisTask {

    @Autowired
    private AnalysisService analysisService;

    @Autowired
    private DataTransferMapper dataTransferMapper;

    @Autowired
    private AnalysisTypeMapper typeMapper;

    @Autowired
    private AnalysisResultMapper analysisResultMapper;

    /**
     * 自动分析
     */
//    @XxlJob(value = "autoAnalysis")
//    void autoAnalysis() {
//        log.info("触发自动分析定时任务开始");
//        if(analysisService.checkAnalysising()){
//            log.info("当前有正在分析的数据，自动分析返回");
//            return;
//        }
//        //获取转换完成且未自动分析的的批次号
//        DataTransferVo dataTransferVo = dataTransferMapper.selectBatchCodeForAutoAnaly();
//        if(dataTransferVo == null || StringUtils.isBlank(dataTransferVo.getBatchCode())){
//            log.info("当前没有秒级、分钟级全部转换完成的数据，自动分析返回");
//            return;
//        }
//        //分析调用
//        analysisService.analysis(assignmentAnalysisDto(dataTransferVo));
//        //自动分析调用完成之后更新状态
//        analysisResultMapper.updateCallStatus(dataTransferVo.getBatchCode());
//        log.info("触发自动分析定时任务结束");
//    }


    /**
     * 设置分析参数
     * @param dataTransferVo
     * @return
     */
    private AnalysisDto assignmentAnalysisDto(DataTransferVo dataTransferVo){
        AnalysisDto analysisDto = new AnalysisDto();
        analysisDto.setAutoOrManual(AnalysisConstants.AUTO_ANALYSIS);
        analysisDto.setDataBatchNum(dataTransferVo.getBatchCode());
        analysisDto.setPowerFarmID(dataTransferVo.getFieldCode());
        List<String> typeCodeList = typeMapper.selectAllVo().parallelStream().map(AnalysisTypeVo::getTypeFlag).collect(Collectors.toList());
        analysisDto.setConfigAnalysis(typeCodeList);
        return analysisDto;
    }
}
