package com.ruoyi.sim.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.sim.domain.vo.TaskVo;
import com.ruoyi.sim.service.impl.TaskService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.sim.domain.Task;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 任务Controller
 *
 * @author tom
 * @date 2024-12-13
 */
@RestController
@RequestMapping("/sim/task")
@Api("任务Controller")
public class TaskController extends BaseController {
    @Autowired
    private TaskService taskService;

    // @PreAuthorize("@ss.hasPermi('sim:task:list')")
    @GetMapping("/list")
    @ApiOperation("查询任务列表")
    public TableDataInfo list(Task task) {
        startPage();
        List<Task> list = taskService.selectTaskList(task);
        return getDataTable(list);
    }

    /**
     * 导出任务列表
     */
    // @PreAuthorize("@ss.hasPermi('sim:task:export')")
    // @Log(title = "任务", businessType = BusinessType.EXPORT)
    // @PostMapping("/export")
    public void export(HttpServletResponse response, Task task) {
        List<Task> list = taskService.selectTaskList(task);
        ExcelUtil<Task> util = new ExcelUtil<Task>(Task.class);
        util.exportExcel(response, list, "任务数据");
    }

    // @PreAuthorize("@ss.hasPermi('sim:task:query')")
    @GetMapping(value = "/{taskId}")
    @ApiOperation("获取任务详细信息")
    public AjaxResult getInfo(@PathVariable("taskId") Long taskId) {
        return taskService.selectTaskByTaskId(taskId);
    }

    // @PreAuthorize("@ss.hasPermi('sim:task:add')")
    @Log(title = "任务", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation("新增任务")
    public AjaxResult add(@RequestBody TaskVo tv) {
        return taskService.insertTaskByTeacher(tv);
    }

    // @PreAuthorize("@ss.hasPermi('sim:task:edit')")
    @Log(title = "任务", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation("修改任务")
    public AjaxResult edit(@RequestBody TaskVo tv) {
        return taskService.updateTaskByTeacher(tv);
    }

    // @PreAuthorize("@ss.hasPermi('sim:task:remove')")
    @Log(title = "任务", businessType = BusinessType.DELETE)
    @DeleteMapping("/{taskIds}")
    @ApiOperation("批量删除任务")
    public AjaxResult remove(@PathVariable Long[] taskIds) {
        return taskService.deleteTaskByTaskIds(taskIds);
    }

    // -------------------------------- tom add  --------------------------------


    @GetMapping("/getInfoForAdd/{simType}")
    @ApiOperation("为了新建Task，返回一个带tree结构的任务详细信息。")
    public AjaxResult getInfoForAdd(@PathVariable("simType") String simType) {
        return taskService.getInfoForAdd(simType);
    }
}
