package com.ruoyi.platform.controller.experiment;

import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.GenericsAjaxResult;
import com.ruoyi.platform.domain.ExperimentIns;
import com.ruoyi.platform.service.ExperimentInsService;
import com.ruoyi.platform.vo.LogRequestVo;
import com.ruoyi.platform.vo.PodLogVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * (ExperimentIns)表控制层
 *
 * @author makejava
 * @since 2023-11-07 15:08:22
 */
@RestController
@RequestMapping("experimentIns")
@Api("实验实例管理")
public class ExperimentInsController extends BaseController {
    /**
     * 服务对象
     */
    @Resource
    private ExperimentInsService experimentInsService;

    /**
     * 分页查询
     *
     * @param experimentIns 筛选条件
     * @return 查询结果
     */
    @GetMapping
    @ApiOperation("分页查询")
    public GenericsAjaxResult<Page<ExperimentIns>> queryByPage(ExperimentIns experimentIns, int page, int size) throws IOException {
        PageRequest pageRequest = PageRequest.of(page, size);
        return genericsSuccess(this.experimentInsService.queryByPage(experimentIns, pageRequest));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    @ApiOperation("通过id查询实验实例")
    public GenericsAjaxResult<ExperimentIns> queryById(@PathVariable("id") Integer id) throws IOException {
        return genericsSuccess(this.experimentInsService.queryById(id));
    }

    /**
     * 通过实验ID查询单条数据
     *
     * @param experimentId 主键
     * @return 单条数据
     */
    @GetMapping("/queryByExperimentId/{Experiment_id}")
    @ApiOperation("通过实验id查询查询实验实例列表")
    public GenericsAjaxResult<List<ExperimentIns>> queryByExperimentId(@PathVariable("Experiment_id") Integer experimentId) throws IOException {
        return genericsSuccess(this.experimentInsService.getByExperimentId(experimentId));
    }

    /**
     * 新增数据
     *
     * @param experimentIns 实体
     * @return 新增结果
     */
    @PostMapping
    @ApiOperation("新增实验实例")
    public GenericsAjaxResult<ExperimentIns> add(@RequestBody ExperimentIns experimentIns) {
        return genericsSuccess(this.experimentInsService.insert(experimentIns));
    }

    /**
     * 编辑数据
     *
     * @param experimentIns 实体
     * @return 编辑结果
     */
    @PutMapping
    @ApiOperation("编辑实验实例")
    public GenericsAjaxResult<ExperimentIns> edit(@RequestBody ExperimentIns experimentIns) throws IOException {
        return genericsSuccess(this.experimentInsService.update(experimentIns));
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping("{id}")
    @ApiOperation("删除实验实例")
    public GenericsAjaxResult<String> deleteById(@PathVariable("id") Integer id) {
        return genericsSuccess(this.experimentInsService.removeById(id));
    }

    @DeleteMapping("batchDelete")
    @ApiOperation("批量删除实验实例")
    public GenericsAjaxResult<String> batchDelete(@RequestBody List<Integer> ids) throws Exception {
        return genericsSuccess(this.experimentInsService.batchDelete(ids));
    }

    /**
     * 终止实验实例
     *
     * @param id 主键
     * @return 运行结果
     */
    @PutMapping("{id}")
    @ApiOperation("终止实验实例")
    public GenericsAjaxResult<Boolean> terminateExperimentIns(@PathVariable("id") Integer id) throws Exception {
        return genericsSuccess(this.experimentInsService.terminateExperimentIns(id));
    }

    /**
     * 查询实验实例日志
     *
     * @return 运行日志
     */

    @GetMapping("/log")
    @ApiOperation("查询实例日志")
    public GenericsAjaxResult<String> showExperimentInsLog(@RequestParam("id") Integer id,
                                                           @RequestParam("component_id") String componentId) {
        return genericsSuccess(this.experimentInsService.showExperimentInsLog(id, componentId));
    }

    /**
     * 查询pods实时日志
     *
     * @return 运行日志
     */

    @GetMapping("/pods/log")
    @ApiOperation("获取pod实时日志请求")
    public GenericsAjaxResult<Map<String, Object>> getRealtimePodLog(@RequestParam("pod_name") String podName,
                                                                     @RequestParam("start_time") String startTime) {
        return genericsSuccess(this.experimentInsService.getRealtimePodLog(podName, startTime));
    }


    @PostMapping("/pods/realTimeLog")
    @ApiOperation("获取pod实时日志请求")
    public GenericsAjaxResult<String> getRealtimePodLogFromPod(@RequestBody PodLogVo podLogVo) {
        return genericsSuccess(this.experimentInsService.getRealtimePodLogFromPod(podLogVo));
    }


    /**
     * 查询实验实例实时日志
     *
     * @return 运行日志
     */

    @PostMapping("/realTimeLog")
    @ApiOperation("查询实验实例实时日志")
    public GenericsAjaxResult<Map<String, Object>> getRealtimeWorkflowLog(@RequestBody LogRequestVo logRequest) {
        return genericsSuccess(this.experimentInsService.getRealtimeWorkflowLog(logRequest));
    }


    /**
     * 查询实验节点结果
     *
     * @return 运行日志
     */
    @PostMapping("/nodeResult")
    @ApiOperation("查询实例节点结果")
    public GenericsAjaxResult<List> getNodeResult(@RequestBody Map map) throws Exception {
        Integer id = Integer.parseInt((String) map.get("id"));
        String nodeId = (String) map.get("node_id");
        return genericsSuccess(this.experimentInsService.getNodeResult(id, nodeId));
    }

    @GetMapping("/time")
    @ApiOperation("获取当前时间")
    public GenericsAjaxResult<Date> getTime() {
        return genericsSuccess(new Date());
    }
}

