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

import cn.iocoder.yudao.module.system.controller.admin.log.vo.RobotOperationLogPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.log.vo.RobotOperationLogRespVO;
import cn.iocoder.yudao.module.system.controller.admin.log.vo.RobotOperationLogSaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.log.RobotOperationLogDO;
import cn.iocoder.yudao.module.system.service.log.RobotOperationLogService;
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/operation-log")
@Validated
public class RobotOperationLogController {

    @Resource
    private RobotOperationLogService operationLogService;

    @PostMapping("/create")
    @Operation(summary = "创建机器人动作记录")
    @PreAuthorize("@ss.hasPermission('robot:operation-log:create')")
    public CommonResult<Long> createOperationLog(@Valid @RequestBody RobotOperationLogSaveReqVO createReqVO) {
        return success(operationLogService.createOperationLog(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新机器人动作记录")
    @PreAuthorize("@ss.hasPermission('robot:operation-log:update')")
    public CommonResult<Boolean> updateOperationLog(@Valid @RequestBody RobotOperationLogSaveReqVO updateReqVO) {
        operationLogService.updateOperationLog(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除机器人动作记录")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('robot:operation-log:delete')")
    public CommonResult<Boolean> deleteOperationLog(@RequestParam("id") Long id) {
        operationLogService.deleteOperationLog(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得机器人动作记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('robot:operation-log:query')")
    public CommonResult<RobotOperationLogRespVO> getOperationLog(@RequestParam("id") Long id) {
        RobotOperationLogDO operationLog = operationLogService.getOperationLog(id);
        return success(BeanUtils.toBean(operationLog, RobotOperationLogRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得机器人动作记录分页")
    @PreAuthorize("@ss.hasPermission('robot:operation-log:query')")
    public CommonResult<PageResult<RobotOperationLogRespVO>> getOperationLogPage(@Valid RobotOperationLogPageReqVO pageReqVO) {
        PageResult<RobotOperationLogDO> pageResult = operationLogService.getOperationLogPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, RobotOperationLogRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出机器人动作记录 Excel")
    @PreAuthorize("@ss.hasPermission('robot:operation-log:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportOperationLogExcel(@Valid RobotOperationLogPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<RobotOperationLogDO> list = operationLogService.getOperationLogPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "机器人动作记录.xls", "数据", RobotOperationLogRespVO.class,
                        BeanUtils.toBean(list, RobotOperationLogRespVO.class));
    }

}