package com.energy.manage.service.test;

import com.alibaba.fastjson.JSONArray;
import com.energy.manage.common.reponse.ResultResp;
import com.energy.manage.service.config.AnalysisTypeConfig;
import com.energy.manage.service.config.annotations.UserLoginToken;
import com.energy.manage.service.constant.analysis.AnalysisConstants;
import com.energy.manage.service.controller.base.BaseServiceController;
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.energy.manage.service.util.IPUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author chy
 * @date 2024/6/19 11:22
 * @desc
 */
@Slf4j
@RestController
@RequestMapping("/test")
@RefreshScope
public class TestController extends BaseServiceController {

    @Autowired
    private AnalysisService analysisService;

    @Autowired
    private DataTransferMapper dataTransferMapper;

    @Autowired
    private AnalysisTypeMapper typeMapper;

    @Autowired
    private AnalysisResultMapper analysisResultMapper;

    @Autowired
    private AnalysisTypeConfig analysisTypeConfig;

    @Value("${download.url}")
    private String downloadUrl;

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

    @UserLoginToken
    @PostMapping("/testRefresh")
    public ResultResp testRefresh() {
        return success(JSONArray.parseArray(analysisTypeConfig.getAnalysisTypeConfig()));
    }

    @UserLoginToken
    @PostMapping("/testIpUtils")
    public ResultResp testIpUtils(HttpServletRequest request) {
        return success(IPUtils.getIpAddr(request));
    }

    /**
     * 设置分析参数
     * @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;
    }


}
