package com.cmvr.web.controller.aima;

import java.util.List;
import jakarta.servlet.http.HttpServletResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.cmvr.common.annotation.Log;
import com.cmvr.common.core.controller.BaseController;
import com.cmvr.common.core.domain.AjaxResult;
import com.cmvr.common.enums.BusinessType;
import com.cmvr.aima.domain.AimaTaskInstance;
import com.cmvr.aima.domain.dto.AimaTaskInstanceQuery;
import com.cmvr.aima.domain.vo.AimaTaskInstanceVo;
import com.cmvr.aima.service.IAimaTaskInstanceService;
import com.cmvr.common.utils.poi.ExcelUtil;
import com.cmvr.common.core.page.TableDataInfo;

@RestController
@RequestMapping("/aima/taskinstance")
@Api(tags = "爱玛电动车--任务执行实例")
public class AimaTaskInstanceController extends BaseController {
    @Autowired
    private IAimaTaskInstanceService aimaTaskInstanceService;

    @ApiOperation("查询任务实例列表")
    @PreAuthorize("@ss.hasPermi('aima:taskinstance:list')")
    @GetMapping("/list")
    public TableDataInfo list(AimaTaskInstanceQuery query) {
        startPage();
        List<AimaTaskInstanceVo> list = aimaTaskInstanceService.selectAimaTaskInstanceVoList(query);
        return getDataTable(list);
    }

    @ApiOperation("导出任务实例列表")
    @PreAuthorize("@ss.hasPermi('aima:taskinstance:export')")
    @Log(title = "任务执行实例", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AimaTaskInstanceQuery query) {
        List<AimaTaskInstanceVo> list = aimaTaskInstanceService.selectAimaTaskInstanceVoList(query);
        ExcelUtil<AimaTaskInstanceVo> util = new ExcelUtil<>(AimaTaskInstanceVo.class);
        util.exportExcel(response, list, "任务实例数据");
    }

    @ApiOperation("获取任务实例详细信息")
    @PreAuthorize("@ss.hasPermi('aima:taskinstance:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return success(aimaTaskInstanceService.selectAimaTaskInstanceById(id));
    }

    @ApiOperation("新增任务实例")
    @PreAuthorize("@ss.hasPermi('aima:taskinstance:add')")
    @Log(title = "任务执行实例", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AimaTaskInstance aimaTaskInstance) {
        return toAjax(aimaTaskInstanceService.insertAimaTaskInstance(aimaTaskInstance));
    }

    @ApiOperation("修改任务实例")
    @PreAuthorize("@ss.hasPermi('aima:taskinstance:edit')")
    @Log(title = "任务执行实例", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AimaTaskInstance aimaTaskInstance) {
        return toAjax(aimaTaskInstanceService.updateAimaTaskInstance(aimaTaskInstance));
    }

    @ApiOperation("删除任务实例")
    @PreAuthorize("@ss.hasPermi('aima:taskinstance:remove')")
    @Log(title = "任务执行实例", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(aimaTaskInstanceService.deleteAimaTaskInstanceByIds(ids));
    }

    /**
     * 开始执行任务实例
     */
    @ApiOperation("开始执行任务实例")
    @PreAuthorize("@ss.hasPermi('aima:taskinstance:edit')")
    @Log(title = "任务执行实例", businessType = BusinessType.UPDATE)
    @PutMapping("/start/{id}")
    public AjaxResult start(@PathVariable("id") String id) {
        return toAjax(aimaTaskInstanceService.startInstance(id));
    }

    /**
     * 暂停任务实例
     */
    @ApiOperation("暂停任务实例")
    @PreAuthorize("@ss.hasPermi('aima:taskinstance:edit')")
    @Log(title = "任务执行实例", businessType = BusinessType.UPDATE)
    @PutMapping("/pause/{id}")
    public AjaxResult pause(@PathVariable("id") String id) {
        return toAjax(aimaTaskInstanceService.pauseInstance(id));
    }

    /**
     * 终止任务实例
     */
    @ApiOperation("终止任务实例")
    @PreAuthorize("@ss.hasPermi('aima:taskinstance:edit')")
    @Log(title = "任务执行实例", businessType = BusinessType.UPDATE)
    @PutMapping("/stop/{id}")
    public AjaxResult stop(@PathVariable("id") String id) {
        return toAjax(aimaTaskInstanceService.stopInstance(id));
    }

    /**
     * 恢复任务实例
     */
    @ApiOperation("恢复任务实例")
    @PreAuthorize("@ss.hasPermi('aima:taskinstance:edit')")
    @Log(title = "任务执行实例", businessType = BusinessType.UPDATE)
    @PutMapping("/resume/{id}")
    public AjaxResult resume(@PathVariable("id") String id) {
        return toAjax(aimaTaskInstanceService.resumeInstance(id));
    }
}
