package cn.iocoder.yudao.module.system.controller.admin.cycle;

import cn.iocoder.yudao.module.system.controller.admin.cycle.vo.TaskCyclePageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.cycle.vo.TaskCycleRespVO;
import cn.iocoder.yudao.module.system.controller.admin.cycle.vo.TaskCycleSaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.cycle.TaskCycleDO;
import cn.iocoder.yudao.module.system.service.cycle.TaskCycleService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;


@Tag(name = "管理后台 - 循环任务点位")
@RestController
@RequestMapping("/robot/task-cycle")
@Validated
public class TaskCycleController {

    @Resource
    private TaskCycleService taskCycleService;

    @PostMapping("/create")
    @Operation(summary = "创建循环任务点位")
    @PreAuthorize("@ss.hasPermission('robot:task-cycle:create')")
    public CommonResult<Long> createTaskCycle(@Valid @RequestBody TaskCycleSaveReqVO createReqVO) {
        return success(taskCycleService.createTaskCycle(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新循环任务点位")
    @PreAuthorize("@ss.hasPermission('robot:task-cycle:update')")
    public CommonResult<Boolean> updateTaskCycle(@Valid @RequestBody TaskCycleSaveReqVO updateReqVO) {
        taskCycleService.updateTaskCycle(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除循环任务点位")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('robot:task-cycle:delete')")
    public CommonResult<Boolean> deleteTaskCycle(@RequestParam("id") Long id) {
        taskCycleService.deleteTaskCycle(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得循环任务点位")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('robot:task-cycle:query')")
    public CommonResult<TaskCycleRespVO> getTaskCycle(@RequestParam("id") Long id) {
        TaskCycleDO taskCycle = taskCycleService.getTaskCycle(id);
        return success(BeanUtils.toBean(taskCycle, TaskCycleRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得循环任务点位分页")
    @PreAuthorize("@ss.hasPermission('robot:task-cycle:query')")
    public CommonResult<PageResult<TaskCycleRespVO>> getTaskCyclePage(@Valid TaskCyclePageReqVO pageReqVO) {
        PageResult<TaskCycleDO> pageResult = taskCycleService.getTaskCyclePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, TaskCycleRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出循环任务点位 Excel")
    @PreAuthorize("@ss.hasPermission('robot:task-cycle:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportTaskCycleExcel(@Valid TaskCyclePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<TaskCycleDO> list = taskCycleService.getTaskCyclePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "循环任务点位.xls", "数据", TaskCycleRespVO.class,
                        BeanUtils.toBean(list, TaskCycleRespVO.class));
    }

}