package com.zg.project.wisdom.controller;

import java.util.List;

import com.zg.common.utils.poi.ExcelUtil;
import com.zg.framework.aspectj.lang.annotation.Log;
import com.zg.framework.aspectj.lang.enums.BusinessType;
import com.zg.framework.web.controller.BaseController;
import com.zg.framework.web.domain.AjaxResult;
import com.zg.framework.web.page.TableDataInfo;
import com.zg.project.wisdom.domain.DdTask;
import com.zg.project.wisdom.domain.dto.TaskExecuteDTO;
import com.zg.project.wisdom.domain.vo.TaskExecuteResultVO;
import com.zg.project.wisdom.service.IDdTaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.security.access.prepost.PreAuthorize;

/**
 * 调度任务 Controller
 * 
 * @author zg
 */
@RestController
@RequestMapping("/wisdom/task")
public class DdTaskController extends BaseController {

    @Autowired
    private IDdTaskService ddTaskService;

    /**
     * 新增调度任务
     */
    @PostMapping("/add")
    public AjaxResult add(@RequestBody DdTask ddTask) {
        return toAjax(ddTaskService.insertDdTask(ddTask));
    }

    /**
     * 执行任务
     * @param
     * @return
     */
    @PostMapping("/execute")
    public AjaxResult execute(@RequestBody TaskExecuteDTO dto) {
        TaskExecuteResultVO vo = ddTaskService.executeTask(dto);
        return AjaxResult.success(vo);
    }

    /**
     * 查询调度任务列表
     */
    @GetMapping("/list")
    public TableDataInfo list(DdTask ddTask) {
        startPage();
        List<DdTask> list = ddTaskService.selectDdTaskList(ddTask);
        return getDataTable(list);
    }

    /**
     * 获取调度任务详情
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(ddTaskService.selectDdTaskById(id));
    }


    /**
     * 导出调度任务列表
     */
    @Log(title = "调度任务", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public AjaxResult export(DdTask ddTask) {
        List<DdTask> list = ddTaskService.selectDdTaskList(ddTask);
        ExcelUtil<DdTask> util = new ExcelUtil<>(DdTask.class);
        return util.exportExcel(list, "调度任务数据");
    }

    /**
     * 修改调度任务
     */
    @Log(title = "调度任务", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DdTask ddTask) {
        return toAjax(ddTaskService.updateDdTask(ddTask));
    }

    /**
     * 删除调度任务
     */
    @Log(title = "调度任务", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(ddTaskService.deleteDdTaskByIds(ids));
    }

}
