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

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.materialcategory.vo.*;
import cn.iocoder.yudao.module.server.dal.dataobject.materialcategory.MaterialCategoryDO;
import cn.iocoder.yudao.module.server.service.materialcategory.MaterialCategoryService;

@Tag(name = "管理后台 - 资料分类")
@RestController
@RequestMapping("/server/material-category")
@Validated
public class MaterialCategoryController {

    @Resource
    private MaterialCategoryService materialCategoryService;

    @PostMapping("/create")
    @Operation(summary = "创建资料分类")
    @PreAuthorize("@ss.hasPermission('server:material-category:create')")
    public CommonResult<Long> createMaterialCategory(@Valid @RequestBody MaterialCategorySaveReqVO createReqVO) {
        return success(materialCategoryService.createMaterialCategory(createReqVO));
    }

    @PostMapping("/update")
    @Operation(summary = "更新资料分类")
    @PreAuthorize("@ss.hasPermission('server:material-category:update')")
    public CommonResult<Boolean> updateMaterialCategory(@Valid @RequestBody MaterialCategorySaveReqVO updateReqVO) {
        materialCategoryService.updateMaterialCategory(updateReqVO);
        return success(true);
    }

    @PostMapping("/delete")
    @Operation(summary = "删除资料分类")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('server:material-category:delete')")
    public CommonResult<Boolean> deleteMaterialCategory(@RequestParam("id") Long id) {
        materialCategoryService.deleteMaterialCategory(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得资料分类")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('server:material-category:query')")
    public CommonResult<MaterialCategoryRespVO> getMaterialCategory(@RequestParam("id") Long id) {
        MaterialCategoryDO materialCategory = materialCategoryService.getMaterialCategory(id);
        return success(BeanUtils.toBean(materialCategory, MaterialCategoryRespVO.class));
    }

    @GetMapping("/list")
    @Operation(summary = "获得资料分类列表")
    @PreAuthorize("@ss.hasPermission('server:material-category:query')")
    public CommonResult<List<MaterialCategoryRespVO>> getMaterialCategoryList(@Valid MaterialCategoryListReqVO listReqVO) {
        List<MaterialCategoryDO> list = materialCategoryService.getMaterialCategoryList(listReqVO);
        return success(BeanUtils.toBean(list, MaterialCategoryRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出资料分类 Excel")
    @PreAuthorize("@ss.hasPermission('server:material-category:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportMaterialCategoryExcel(@Valid MaterialCategoryListReqVO listReqVO,
              HttpServletResponse response) throws IOException {
        List<MaterialCategoryDO> list = materialCategoryService.getMaterialCategoryList(listReqVO);
        // 导出 Excel
        ExcelUtils.write(response, "资料分类.xls", "数据", MaterialCategoryRespVO.class,
                        BeanUtils.toBean(list, MaterialCategoryRespVO.class));
    }

}