package com.playlet.web.controller.system;

import java.util.List;

import com.github.pagehelper.PageInfo;
import com.playlet.common.core.domain.Result;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.playlet.common.annotation.Log;
import com.playlet.common.enums.BusinessType;
import com.playlet.system.domain.PlayletTask;
import com.playlet.system.service.IPlayletTaskService;
import com.playlet.common.core.controller.BaseController;
import com.playlet.common.core.domain.AjaxResult;
import com.playlet.common.utils.poi.ExcelUtil;
import com.playlet.common.core.page.TableDataInfo;

/**
 * @Date: 2024-03-21 23:51
 * @Author: 但星霖
 * @Version: v1.0
 * @Description: 任务Controller
 */
@Controller
@RequestMapping("/system/task")
public class PlayletTaskController extends BaseController {
    private String prefix = "system/task";

    @Autowired
    private IPlayletTaskService playletTaskService;

    @RequiresPermissions("system:task:view")
    @GetMapping()
    public String task() {
        return prefix + "/task";
    }

    /**
     * 查询任务列表
     */
    @RequiresPermissions("system:task:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(PlayletTask playletTask) {
        startPage();
        List<PlayletTask> list = playletTaskService.selectPlayletTaskList(playletTask);
        return getDataTable(list);
    }

    /**
     * 导出任务列表
     */
    @RequiresPermissions("system:task:export")
    @Log(title = "任务", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(PlayletTask playletTask) {
        List<PlayletTask> list = playletTaskService.selectPlayletTaskList(playletTask);
        ExcelUtil<PlayletTask> util = new ExcelUtil<PlayletTask>(PlayletTask.class);
        return util.exportExcel(list, "任务数据");
    }

    /**
     * 新增任务
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存任务
     */
    @RequiresPermissions("system:task:add")
    @Log(title = "任务", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(PlayletTask playletTask) {
        return toAjax(playletTaskService.insertPlayletTask(playletTask));
    }

    /**
     * 修改任务
     */
    @RequiresPermissions("system:task:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        PlayletTask playletTask = playletTaskService.selectPlayletTaskById(id);
        mmap.put("playletTask", playletTask);
        return prefix + "/edit";
    }

    /**
     * 修改保存任务
     */
    @RequiresPermissions("system:task:edit")
    @Log(title = "任务", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(PlayletTask playletTask) {
        return toAjax(playletTaskService.updatePlayletTask(playletTask));
    }

    /**
     * 删除任务
     */
    @RequiresPermissions("system:task:remove")
    @Log(title = "任务", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(playletTaskService.deletePlayletTaskByIds(ids));
    }
}
