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.AimaTask;
import com.cmvr.aima.domain.vo.AimaTaskVo;
import com.cmvr.aima.service.IAimaTaskService;
import com.cmvr.common.utils.poi.ExcelUtil;
import com.cmvr.common.core.page.TableDataInfo;

@RestController
@RequestMapping("/aima/task")
@Api(tags = "爱玛电动车--任务管理")
public class AimaTaskController extends BaseController {
    @Autowired
    private IAimaTaskService aimaTaskService;

    @ApiOperation("查询任务列表")
    @PreAuthorize("@ss.hasPermi('aima:task:list')")
    @GetMapping("/list")
    public TableDataInfo list(AimaTask aimaTask) {
        startPage();
        List<AimaTaskVo> list = aimaTaskService.selectAimaTaskVoList(aimaTask);
        return getDataTable(list);
    }

    @ApiOperation("导出任务列表")
    @PreAuthorize("@ss.hasPermi('aima:task:export')")
    @Log(title = "任务管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AimaTask aimaTask) {
        List<AimaTaskVo> list = aimaTaskService.selectAimaTaskVoList(aimaTask);
        ExcelUtil<AimaTaskVo> util = new ExcelUtil<>(AimaTaskVo.class);
        util.exportExcel(response, list, "任务数据");
    }

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

    @ApiOperation("新增任务")
    @PreAuthorize("@ss.hasPermi('aima:task:add')")
    @Log(title = "任务管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AimaTask aimaTask) {
        return toAjax(aimaTaskService.insertAimaTask(aimaTask));
    }

    @ApiOperation("修改任务")
    @PreAuthorize("@ss.hasPermi('aima:task:edit')")
    @Log(title = "任务管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AimaTask aimaTask) {
        return toAjax(aimaTaskService.updateAimaTask(aimaTask));
    }

    @ApiOperation("删除任务")
    @PreAuthorize("@ss.hasPermi('aima:task:remove')")
    @Log(title = "任务管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(aimaTaskService.deleteAimaTaskByIds(ids));
    }

    /**
     * 绑定测试用例到任务
     */
    @ApiOperation("绑定测试用例")
    @PreAuthorize("@ss.hasPermi('aima:task:edit')")
    @Log(title = "任务管理", businessType = BusinessType.UPDATE)
    @PostMapping("/bindTestCases/{taskId}")
    public AjaxResult bindTestCases(@PathVariable("taskId") String taskId, @RequestBody List<String> testCaseIds) {
        aimaTaskService.bindTestCases(taskId, testCaseIds);
        return success();
    }

    /**
     * 获取任务的测试用例列表
     */
    @ApiOperation("获取任务测试用例")
    @PreAuthorize("@ss.hasPermi('aima:task:query')")
    @GetMapping("/testCases/{taskId}")
    public AjaxResult getTestCases(@PathVariable String taskId) {
        return success(aimaTaskService.getTestCaseIds(taskId));
    }
}
