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

import cn.iocoder.yudao.module.system.controller.admin.robot.detail.RobotTaskDetailLogResoVO;
import cn.iocoder.yudao.module.system.controller.admin.robot.detail.RobotTaskDetailLogVO;
import cn.iocoder.yudao.module.system.controller.admin.robot.vo.RobotTaskPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.robot.vo.RobotTaskRespVO;
import cn.iocoder.yudao.module.system.controller.admin.robot.vo.RobotTaskSaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.robot.RobotTaskDO;
import cn.iocoder.yudao.module.system.service.robot.RobotTaskService;
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("/system/robot/task")
@Validated
public class RobotTaskController {

    @Resource
    private RobotTaskService taskService;


    @PostMapping("/create")
    @Operation(summary = "创建机器人任务主表")
    @PreAuthorize("@ss.hasPermission('robot:task:create')")
    public CommonResult<Long> createTask(@Valid @RequestBody RobotTaskSaveReqVO createReqVO) throws InterruptedException {
        return success(taskService.createTask(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新机器人任务主表")
    @PreAuthorize("@ss.hasPermission('robot:task:update')")
    public CommonResult<Boolean> updateTask(@Valid @RequestBody RobotTaskSaveReqVO updateReqVO) {
        taskService.updateTask(updateReqVO);
        return success(true);
    }

    @PutMapping("/close")
    @Operation(summary = "更新机器人任务主表")
    @PreAuthorize("@ss.hasPermission('robot:task:close')")
    public CommonResult<Boolean> closeTask(@Valid @RequestBody RobotTaskSaveReqVO updateReqVO) {
        taskService.closeTask(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除机器人任务主表")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('robot:task:delete')")
    public CommonResult<Boolean> deleteTask(@RequestParam("id") Long id) {
        taskService.deleteTask(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得机器人任务主表")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('robot:task:query')")
    public CommonResult<RobotTaskRespVO> getTask(@RequestParam("id") Long id) {
        RobotTaskDO task = taskService.getTask(id);
        return success(BeanUtils.toBean(task, RobotTaskRespVO.class));
    }

    @GetMapping("/getTaskNo")
    @Operation(summary = "获得任务号")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('robot:task:getTaskNo')")
    public CommonResult<String> getTaskNo() {
        return success(taskService.getTaskNo());
    }

    @PostMapping("/page")
    @Operation(summary = "获得机器人任务主表分页")
    @PreAuthorize("@ss.hasPermission('robot:task:query')")
    public CommonResult<PageResult<RobotTaskRespVO>> getTaskPage(@Valid @RequestBody RobotTaskPageReqVO pageReqVO) {
        PageResult<RobotTaskRespVO> pageResult = taskService.getTaskPage(pageReqVO);
        return success(pageResult);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出机器人任务主表 Excel")
    @PreAuthorize("@ss.hasPermission('robot:task:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportTaskExcel(@Valid RobotTaskPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<RobotTaskRespVO> list = taskService.getTaskPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "机器人任务主表.xls", "数据", RobotTaskRespVO.class,
                        BeanUtils.toBean(list, RobotTaskRespVO.class));
    }

    @PostMapping("/logPage")
    @Operation(summary = "任务日志分页")
    @PreAuthorize("@ss.hasPermission('robot:task:logPage')")
    public CommonResult<PageResult<RobotTaskDetailLogResoVO>> logPage(@Valid @RequestBody RobotTaskDetailLogVO pageReqVO) {
        PageResult<RobotTaskDetailLogResoVO> pageResult = taskService.logPage(pageReqVO);
        return success(pageResult);
    }

    @PostMapping("/checkHaveTask")
    @Operation(summary = "判断是否存在任务")
    @PreAuthorize("@ss.hasPermission('robot:task:checkHaveTask')")
    public CommonResult<Boolean> checkHaveTask() {
        //校验是否存在未完成的任务
        taskService.checkHaveDoingTask();
        return success(true);
    }

    @GetMapping("/getRobotTaskNum")
    @Operation(summary = "获取这台车工作的任务总数")
    @Parameter(name = "robotNo", description = "车辆编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('robot:task:getRobotTaskNum')")
    public CommonResult<Integer> getRobotTaskNum(@RequestParam("robotNo") String robotNo) {
        return success(taskService.getRobotTaskNum(robotNo));
    }
}