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

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.DeviceCategoryPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.DeviceCategoryRespVO;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.DeviceCategorySaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.biz.DeviceCategoryDO;
import cn.iocoder.yudao.module.system.service.biz.DeviceCategoryService;
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;

/**
 * @Author: jinyd
 * @CreateTime: 2024/10/15  16:10
 * @Description:
 * @Version: 4.0
 */


@Tag(name = "管理后台 - 设备分类")
@RestController
@RequestMapping("/device/category")
@Validated
public class DeviceCategoryController {

    @Resource
    private DeviceCategoryService categoryService;

    @PostMapping("/create")
    @Operation(summary = "创建设备分类")
    @PreAuthorize("@ss.hasPermission('device:category:create')")
    public CommonResult<Long> createCategory(@Valid @RequestBody DeviceCategorySaveReqVO createReqVO) {
        return success(categoryService.createCategory(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新设备分类")
    @PreAuthorize("@ss.hasPermission('device:category:update')")
    public CommonResult<Boolean> updateCategory(@Valid @RequestBody DeviceCategorySaveReqVO updateReqVO) {
        categoryService.updateCategory(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除设备分类")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('device:category:delete')")
    public CommonResult<Boolean> deleteCategory(@RequestParam("id") Long id) {
        categoryService.deleteCategory(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得设备分类")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('device:category:query')")
    public CommonResult<DeviceCategoryRespVO> getCategory(@RequestParam("id") Long id) {
        DeviceCategoryDO category = categoryService.getCategory(id);
        return success(BeanUtils.toBean(category, DeviceCategoryRespVO.class));
    }

    @GetMapping("/list")
    @Operation(summary = "获得设备分类列表")
//    @PreAuthorize("@ss.hasPermission('device:category:query')")
    public CommonResult<List<DeviceCategoryRespVO>> getCategoryPage(@Valid DeviceCategoryDO deviceCategoryDO) {
        List<DeviceCategoryDO> pageResult = categoryService.getList(deviceCategoryDO);
        return success(BeanUtils.toBean(pageResult, DeviceCategoryRespVO.class));
    }

}
