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

import cn.iocoder.yudao.module.system.controller.admin.log.vo.RobotTaskDetailActionLogPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.log.vo.RobotTaskDetailActionLogRespVO;
import cn.iocoder.yudao.module.system.controller.admin.log.vo.RobotTaskDetailActionLogSaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.log.RobotTaskDetailActionLogDO;
import cn.iocoder.yudao.module.system.service.log.RobotTaskDetailActionLogService;
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-action-log")
@Validated
public class RobotTaskDetailActionLogController {

    @Resource
    private RobotTaskDetailActionLogService taskDetailActionLogService;

    @PostMapping("/create")
    @Operation(summary = "创建车辆动作记录")
    @PreAuthorize("@ss.hasPermission('robot:task-detail-action-log:create')")
    public CommonResult<Long> createTaskDetailActionLog(@Valid @RequestBody RobotTaskDetailActionLogSaveReqVO createReqVO) {
        return success(taskDetailActionLogService.createTaskDetailActionLog(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新车辆动作记录")
    @PreAuthorize("@ss.hasPermission('robot:task-detail-action-log:update')")
    public CommonResult<Boolean> updateTaskDetailActionLog(@Valid @RequestBody RobotTaskDetailActionLogSaveReqVO updateReqVO) {
        taskDetailActionLogService.updateTaskDetailActionLog(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除车辆动作记录")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('robot:task-detail-action-log:delete')")
    public CommonResult<Boolean> deleteTaskDetailActionLog(@RequestParam("id") Long id) {
        taskDetailActionLogService.deleteTaskDetailActionLog(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得车辆动作记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('robot:task-detail-action-log:query')")
    public CommonResult<RobotTaskDetailActionLogRespVO> getTaskDetailActionLog(@RequestParam("id") Long id) {
        RobotTaskDetailActionLogDO taskDetailActionLog = taskDetailActionLogService.getTaskDetailActionLog(id);
        return success(BeanUtils.toBean(taskDetailActionLog, RobotTaskDetailActionLogRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得车辆动作记录分页")
    @PreAuthorize("@ss.hasPermission('robot:task-detail-action-log:query')")
    public CommonResult<PageResult<RobotTaskDetailActionLogRespVO>> getTaskDetailActionLogPage(@Valid RobotTaskDetailActionLogPageReqVO pageReqVO) {
        PageResult<RobotTaskDetailActionLogDO> pageResult = taskDetailActionLogService.getTaskDetailActionLogPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, RobotTaskDetailActionLogRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出车辆动作记录 Excel")
    @PreAuthorize("@ss.hasPermission('robot:task-detail-action-log:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportTaskDetailActionLogExcel(@Valid RobotTaskDetailActionLogPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<RobotTaskDetailActionLogDO> list = taskDetailActionLogService.getTaskDetailActionLogPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "车辆动作记录.xls", "数据", RobotTaskDetailActionLogRespVO.class,
                        BeanUtils.toBean(list, RobotTaskDetailActionLogRespVO.class));
    }

}