package com.css.simulation.resource.server.adapter.controller.job_manage;

import api.common.pojo.common.ResponseBodyVO;
import api.common.pojo.vo.project.ProjectReportVO;
import api.common.pojo.vo.project.ProjectResultWithAlgorithmVO;
import api.common.util.StringUtil;
import com.css.simulation.resource.server.adapter.parameter.ProjectParam;
import com.css.simulation.resource.server.app.service.job_manage.JobManageApplicationService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 供算法平台使用
 */
@RestController
@RequestMapping("/report")
public class AlgorithmPlatformResultController {


    @Resource
    private JobManageApplicationService jobManageApplicationService;


    /**
     * 根据算法ID获取该算法最新执行的项目的报告
     * 获取测试报告详情
     */
    @RequestMapping("/getSimulationReportById")
    public ResponseBodyVO<List<ProjectResultWithAlgorithmVO>> getProjectReportByAlgorithmId(@RequestBody ProjectParam projectParam) {

        if (projectParam == null || StringUtil.isEmpty(projectParam.getAlgorithmId())) {
            return new ResponseBodyVO<>(false, 500, "参数必传！", null);
        }
        List<ProjectResultWithAlgorithmVO> result = new ArrayList<>();
        String algorithmIds = projectParam.getAlgorithmId();
        String[] algorithmIdArray = algorithmIds.split(",");
        for (String algorithmId : algorithmIdArray) {
            result.add(ProjectResultWithAlgorithmVO.builder()
                    .algorithmId(algorithmId)
                    .report(jobManageApplicationService.getLastProjectReportByAlgorithmId(algorithmId))
                    .videos(jobManageApplicationService.getLastVideoListByAlgorithmId(algorithmId))
                    .build());
        }
        return new ResponseBodyVO<>(ResponseBodyVO.Response.SUCCESS, result);
    }

    /**
     * (未使用)
     * 根据算法ID获取该算法最新执行的项目的报告
     * 获取测试报告详情
     */
    @RequestMapping("/getProjectReportByOneAlgorithmId")
    public ResponseBodyVO<ProjectReportVO> getProjectReportByOneAlgorithmId(@RequestBody ProjectParam projectParam) {

        if (projectParam == null || StringUtil.isEmpty(projectParam.getAlgorithmId())) {
            return new ResponseBodyVO<>(false, 500, "参数必传！", null);
        }
        String algorithmId = projectParam.getAlgorithmId();
        ProjectReportVO projectReportVO = jobManageApplicationService.getLastProjectReportByAlgorithmId(algorithmId);
        if (projectReportVO == null) {
            return new ResponseBodyVO<>(ResponseBodyVO.Response.SERVER_FAILURE, "该算法还未执行过项目。", null);
        } else {
            return new ResponseBodyVO<>(ResponseBodyVO.Response.SUCCESS, projectReportVO);
        }
    }

}
