package cn.iocoder.yudao.module.server.controller.admin.materialdownloadlog;

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.*;

import cn.iocoder.yudao.module.server.controller.admin.materialdownloadlog.vo.*;
import cn.iocoder.yudao.module.server.dal.dataobject.materialdownloadlog.MaterialDownloadLogDO;
import cn.iocoder.yudao.module.server.service.materialdownloadlog.MaterialDownloadLogService;

@Tag(name = "管理后台 - 资料下载日志")
@RestController
@RequestMapping("/server/material-download-log")
@Validated
public class MaterialDownloadLogController {

    @Resource
    private MaterialDownloadLogService materialDownloadLogService;

    @PostMapping("/create")
    @Operation(summary = "创建资料下载日志")
    @PreAuthorize("@ss.hasPermission('server:material-download-log:create')")
    public CommonResult<Long> createMaterialDownloadLog(@Valid @RequestBody MaterialDownloadLogSaveReqVO createReqVO) {
        return success(materialDownloadLogService.createMaterialDownloadLog(createReqVO));
    }

    @PostMapping("/update")
    @Operation(summary = "更新资料下载日志")
    @PreAuthorize("@ss.hasPermission('server:material-download-log:update')")
    public CommonResult<Boolean> updateMaterialDownloadLog(@Valid @RequestBody MaterialDownloadLogSaveReqVO updateReqVO) {
        materialDownloadLogService.updateMaterialDownloadLog(updateReqVO);
        return success(true);
    }

    @PostMapping("/delete")
    @Operation(summary = "删除资料下载日志")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('server:material-download-log:delete')")
    public CommonResult<Boolean> deleteMaterialDownloadLog(@RequestParam("id") Long id) {
        materialDownloadLogService.deleteMaterialDownloadLog(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得资料下载日志")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('server:material-download-log:query')")
    public CommonResult<MaterialDownloadLogRespVO> getMaterialDownloadLog(@RequestParam("id") Long id) {
        MaterialDownloadLogDO materialDownloadLog = materialDownloadLogService.getMaterialDownloadLog(id);
        return success(BeanUtils.toBean(materialDownloadLog, MaterialDownloadLogRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得资料下载日志分页")
    @PreAuthorize("@ss.hasPermission('server:material-download-log:query')")
    public CommonResult<PageResult<MaterialDownloadLogRespVO>> getMaterialDownloadLogPage(@Valid MaterialDownloadLogPageReqVO pageReqVO) {
        PageResult<MaterialDownloadLogDO> pageResult = materialDownloadLogService.getMaterialDownloadLogPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, MaterialDownloadLogRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出资料下载日志 Excel")
    @PreAuthorize("@ss.hasPermission('server:material-download-log:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportMaterialDownloadLogExcel(@Valid MaterialDownloadLogPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<MaterialDownloadLogDO> list = materialDownloadLogService.getMaterialDownloadLogPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "资料下载日志.xls", "数据", MaterialDownloadLogRespVO.class,
                        BeanUtils.toBean(list, MaterialDownloadLogRespVO.class));
    }

}