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

import cn.iocoder.yudao.module.system.controller.admin.log.vo.UserOperationLogPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.log.vo.UserOperationLogRespVO;
import cn.iocoder.yudao.module.system.controller.admin.log.vo.UserOperationLogSaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.log.UserOperationLogDO;
import cn.iocoder.yudao.module.system.service.log.UserOperationLogService;
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/log/user-operation-log")
@Validated
public class UserOperationLogController {

    @Resource
    private UserOperationLogService userOperationLogService;

    @PostMapping("/create")
    @Operation(summary = "创建用户操作记录")
    @PreAuthorize("@ss.hasPermission('log:user-operation-log:create')")
    public CommonResult<Long> createUserOperationLog(@Valid @RequestBody UserOperationLogSaveReqVO createReqVO) {
        return success(userOperationLogService.createUserOperationLog(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新用户操作记录")
    @PreAuthorize("@ss.hasPermission('log:user-operation-log:update')")
    public CommonResult<Boolean> updateUserOperationLog(@Valid @RequestBody UserOperationLogSaveReqVO updateReqVO) {
        userOperationLogService.updateUserOperationLog(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除用户操作记录")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('log:user-operation-log:delete')")
    public CommonResult<Boolean> deleteUserOperationLog(@RequestParam("id") Long id) {
        userOperationLogService.deleteUserOperationLog(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得用户操作记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('log:user-operation-log:query')")
    public CommonResult<UserOperationLogRespVO> getUserOperationLog(@RequestParam("id") Long id) {
        UserOperationLogDO userOperationLog = userOperationLogService.getUserOperationLog(id);
        return success(BeanUtils.toBean(userOperationLog, UserOperationLogRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得用户操作记录分页")
    @PreAuthorize("@ss.hasPermission('log:user-operation-log:query')")
    public CommonResult<PageResult<UserOperationLogRespVO>> getUserOperationLogPage(@Valid UserOperationLogPageReqVO pageReqVO) {
        PageResult<UserOperationLogDO> pageResult = userOperationLogService.getUserOperationLogPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, UserOperationLogRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出用户操作记录 Excel")
    @PreAuthorize("@ss.hasPermission('log:user-operation-log:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportUserOperationLogExcel(@Valid UserOperationLogPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<UserOperationLogDO> list = userOperationLogService.getUserOperationLogPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "用户操作记录.xls", "数据", UserOperationLogRespVO.class,
                        BeanUtils.toBean(list, UserOperationLogRespVO.class));
    }

}