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

import cn.iocoder.yudao.module.system.controller.admin.robot.vo.RobotTaskDetailPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.robot.vo.RobotTaskDetailRespVO;
import cn.iocoder.yudao.module.system.controller.admin.robot.vo.RobotTaskDetailSaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.robot.RobotTaskDetailDO;
import cn.iocoder.yudao.module.system.enums.robot.task.RobotTaskManualInterventionEnum;
import cn.iocoder.yudao.module.system.service.robot.RobotTaskDetailService;
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.*;
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-detail")
@Validated
public class RobotTaskDetailController {

    @Resource
    private RobotTaskDetailService taskDetailService;

    @PostMapping("/create")
    @Operation(summary = "创建机器人任务明细")
    @PreAuthorize("@ss.hasPermission('robot:task-detail:create')")
    public CommonResult<Long> createTaskDetail(@Valid @RequestBody RobotTaskDetailSaveReqVO createReqVO) {
        return success(taskDetailService.createTaskDetail(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新机器人任务明细")
    @PreAuthorize("@ss.hasPermission('robot:task-detail:update')")
    public CommonResult<Boolean> updateTaskDetail(@Valid @RequestBody RobotTaskDetailSaveReqVO updateReqVO) {
        taskDetailService.updateTaskDetail(updateReqVO);
        return success(true);
    }

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

    @GetMapping("/get")
    @Operation(summary = "获得机器人任务明细")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('robot:task-detail:query')")
    public CommonResult<RobotTaskDetailRespVO> getTaskDetail(@RequestParam("id") Long id) {
        RobotTaskDetailDO taskDetail = taskDetailService.getTaskDetail(id);
        return success(BeanUtils.toBean(taskDetail, RobotTaskDetailRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得机器人任务明细分页")
    @PreAuthorize("@ss.hasPermission('robot:task-detail:query')")
    public CommonResult<PageResult<RobotTaskDetailRespVO>> getTaskDetailPage(@Valid RobotTaskDetailPageReqVO pageReqVO) {
        PageResult<RobotTaskDetailDO> pageResult = taskDetailService.getTaskDetailPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, RobotTaskDetailRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出机器人任务明细 Excel")
    @PreAuthorize("@ss.hasPermission('robot:task-detail:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportTaskDetailExcel(@Valid RobotTaskDetailPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<RobotTaskDetailDO> list = taskDetailService.getTaskDetailPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "机器人任务明细.xls", "数据", RobotTaskDetailRespVO.class,
                        BeanUtils.toBean(list, RobotTaskDetailRespVO.class));
    }

    @PostMapping("/manuallyCompleted")
    @Operation(summary = "人工完成")
    @PreAuthorize("@ss.hasPermission('robot:task-detail:manuallyCompleted')")
    public CommonResult manuallyCompleted(@RequestParam("id") Long id) {
        taskDetailService.manuallyCompleted(id,"人工完成", RobotTaskManualInterventionEnum.RCS_DONE.getType());
        return success(true);
    }

}