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

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.module.system.dal.dataobject.dict.CategoryDO;
import cn.iocoder.yudao.module.system.service.dict.CategoryService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 分类字典")
@RestController
@RequestMapping("/system/category")
@Validated
public class CategoryController {

    @Resource
    private CategoryService categoryService;

    @PostMapping("create")
    @Operation(summary = "创建分类字典")
    @PreAuthorize("@ss.hasPermission('system:category:create')")
    public CommonResult<String> createDept(@Valid @RequestBody CategoryDO createReqVO) {
        String id = categoryService.createCategory(createReqVO);
        return success(id);
    }

    @PutMapping("update")
    @Operation(summary = "更新分类字典")
    @PreAuthorize("@ss.hasPermission('system:category:update')")
    public CommonResult<Boolean> updateDept(@Valid @RequestBody CategoryDO updateReqVO) {
        categoryService.updateCategory(updateReqVO);
        return success(true);
    }

    @DeleteMapping("delete")
    @Operation(summary = "删除分类字典")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('system:category:delete')")
    public CommonResult<Boolean> deleteDept(@RequestParam("id") String id) {
        categoryService.deleteCategory(id);
        return success(true);
    }

    @GetMapping("/list")
    @Operation(summary = "获取分类字典列表")
    @PreAuthorize("@ss.hasPermission('system:category:query')")
    public CommonResult<List<CategoryDO>> getDeptList(CategoryDO reqVO) {
        return success(categoryService.getCategoryList(reqVO));
    }

    @GetMapping("/get")
    @Operation(summary = "获得分类字典信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('system:category:query')")
    public CommonResult<CategoryDO> getDept(@RequestParam("id") String id) {
        return success(categoryService.getCategory(id));
    }

}
