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

import cn.iocoder.yudao.module.system.controller.admin.robot.vo.RobotTaskAutoMovePageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.robot.vo.RobotTaskAutoMoveRespVO;
import cn.iocoder.yudao.module.system.controller.admin.robot.vo.RobotTaskAutoMoveSaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.robot.RobotTaskAutoMoveDO;
import cn.iocoder.yudao.module.system.service.robot.job.RobotTaskAutoMoveService;
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-auto-move")
@Validated
public class RobotTaskAutoMoveController {

    @Resource
    private RobotTaskAutoMoveService taskAutoMoveService;

    @PostMapping("/create")
    @Operation(summary = "创建机器人自动移库")
    @PreAuthorize("@ss.hasPermission('robot:task-auto-move:create')")
    public CommonResult<Long> createTaskAutoMove(@Valid @RequestBody RobotTaskAutoMoveSaveReqVO createReqVO) {
        return success(taskAutoMoveService.createTaskAutoMove(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新机器人自动移库")
    @PreAuthorize("@ss.hasPermission('robot:task-auto-move:update')")
    public CommonResult<Boolean> updateTaskAutoMove(@Valid @RequestBody RobotTaskAutoMoveSaveReqVO updateReqVO) {
        taskAutoMoveService.updateTaskAutoMove(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除机器人自动移库")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('robot:task-auto-move:delete')")
    public CommonResult<Boolean> deleteTaskAutoMove(@RequestParam("id") Long id) {
        taskAutoMoveService.deleteTaskAutoMove(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得机器人自动移库")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('robot:task-auto-move:query')")
    public CommonResult<RobotTaskAutoMoveRespVO> getTaskAutoMove(@RequestParam("id") Long id) {
        RobotTaskAutoMoveDO taskAutoMove = taskAutoMoveService.getTaskAutoMove(id);
        return success(BeanUtils.toBean(taskAutoMove, RobotTaskAutoMoveRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得机器人自动移库分页")
    @PreAuthorize("@ss.hasPermission('robot:task-auto-move:query')")
    public CommonResult<PageResult<RobotTaskAutoMoveRespVO>> getTaskAutoMovePage(@Valid RobotTaskAutoMovePageReqVO pageReqVO) {
        PageResult<RobotTaskAutoMoveDO> pageResult = taskAutoMoveService.getTaskAutoMovePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, RobotTaskAutoMoveRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出机器人自动移库 Excel")
    @PreAuthorize("@ss.hasPermission('robot:task-auto-move:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportTaskAutoMoveExcel(@Valid RobotTaskAutoMovePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<RobotTaskAutoMoveDO> list = taskAutoMoveService.getTaskAutoMovePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "机器人自动移库.xls", "数据", RobotTaskAutoMoveRespVO.class,
                        BeanUtils.toBean(list, RobotTaskAutoMoveRespVO.class));
    }

}