package com.cmvr.web.controller.ti;

import com.cmvr.common.core.controller.BaseController;
import com.cmvr.common.core.domain.AjaxResult;
import com.cmvr.common.core.page.TableDataInfo;
import com.cmvr.test.model.vo.TeTaskExecuteProjectVO;
import com.cmvr.ti.model.domain.TiProject;
import com.cmvr.ti.service.ITiProjectService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Api(tags = "触控交互--项目管理")
@RestController
@RequestMapping("/ti/project")
@RequiredArgsConstructor
public class TiProjectController extends BaseController {

    private final ITiProjectService tiProjectService;

    @ApiOperation("获取项目列表")
    @PreAuthorize("@ss.hasPermi('ti:project:list')")
    @GetMapping("/list")
    public TableDataInfo list(TiProject tiProject) {
        startPage();
        List<TiProject> list = tiProjectService.selectTiProjectList(tiProject);
        return getDataTable(list);
    }

    @ApiOperation("根据id获取项目信息")
    @PreAuthorize("@ss.hasPermi('ti:project:query')")
    @GetMapping(value = "/{projectId}")
    public AjaxResult getInfo(@PathVariable("projectId") String projectId) {
        return success(tiProjectService.selectTiProjectByProjectId(projectId));
    }

    @ApiOperation("添加项目")
    @PreAuthorize("@ss.hasPermi('ti:project:add')")
    @PostMapping
    public AjaxResult add(@RequestBody TiProject tiProject) {
        return toAjax(tiProjectService.insertTiProject(tiProject));
    }

    @ApiOperation("编辑项目")
    @PreAuthorize("@ss.hasPermi('ti:project:edit')")
    @PutMapping
    public AjaxResult edit(@RequestBody TiProject tiProject) {
        return toAjax(tiProjectService.updateTiProject(tiProject));
    }

    @ApiOperation("删除项目")
    @PreAuthorize("@ss.hasPermi('ti:project:remove')")
    @DeleteMapping("/{projectIds}")
    public AjaxResult remove(@PathVariable String[] projectIds) {
        return toAjax(tiProjectService.deleteTiProjectByProjectIds(projectIds));
    }

    @ApiOperation("查询项目执行时运行参数")
    @GetMapping("/queryExecuteRunParams")
    public AjaxResult queryExecuteRunParams(@RequestParam("projectId") String projectId) {
        return AjaxResult.ok(tiProjectService.queryExecuteRunParams(projectId));
    }

    @ApiOperation("执行项目")
    @PostMapping("/execute")
    public AjaxResult execute(@RequestBody TeTaskExecuteProjectVO taskExecuteProjectVO) {
        return AjaxResult.ok(tiProjectService.executeProject(taskExecuteProjectVO));
    }

    @ApiOperation("查询最近一次项目执行实例id")
    @GetMapping("/queryExecuteInstId/{projectId}")
    public AjaxResult queryExecuteInstId(@PathVariable("projectId") String projectId) {
        return AjaxResult.ok(tiProjectService.queryExecuteInstId(projectId));
    }

    @ApiOperation("临时执行项目评估")
    @PostMapping("/evaluation")
    public AjaxResult evaluation(@RequestBody  List<Long> funcIds) {
        tiProjectService.executeTiEvaluation(funcIds);
        return AjaxResult.ok();
    }

    @ApiOperation("评估查询")
    @GetMapping("/evaluation/{projectId}")
    public AjaxResult queryEvaluation(@PathVariable("projectId") String projectId) {
        return AjaxResult.ok(tiProjectService.queryEvaluation(projectId));
    }
}
