package com.yhy.module.core.controller.admin.resource;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import com.yhy.module.core.controller.admin.resource.vo.ResourceCategorySaveReqVO;
import com.yhy.module.core.controller.admin.resource.vo.SwapSortOrderReqVO;
import com.yhy.module.core.dal.dataobject.resource.ResourceCategoryDO;
import com.yhy.module.core.service.resource.ResourceCategoryService;
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.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("/core/resource/categories")
@Validated
public class ResourceCategoryController {

    @Resource
    private ResourceCategoryService categoryService;

    @PostMapping
    @Operation(summary = "创建工料机类别")
    public CommonResult<Long> create(@Valid @RequestBody ResourceCategorySaveReqVO reqVO) {
        return success(categoryService.create(reqVO));
    }

    @PutMapping
    @Operation(summary = "更新工料机类别")
    public CommonResult<Boolean> update(@Valid @RequestBody ResourceCategorySaveReqVO reqVO) {
        categoryService.update(reqVO);
        return success(true);
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除工料机类别")
    @Parameter(name = "id", description = "类别ID", required = true, example = "1")
    public CommonResult<Boolean> delete(@PathVariable("id") Long id) {
        categoryService.delete(id);
        return success(true);
    }

    @GetMapping("/{id}")
    @Operation(summary = "获取工料机类别详情")
    @Parameter(name = "id", description = "类别ID", required = true, example = "1")
    public CommonResult<ResourceCategoryDO> get(@PathVariable("id") Long id) {
        return success(categoryService.get(id));
    }

    @GetMapping
    @Operation(summary = "获取工料机类别列表")
    public CommonResult<List<ResourceCategoryDO>> list() {
        return success(categoryService.list());
    }
    
    @PutMapping("/swap-sort")
    @Operation(summary = "交换两个类别的排序")
    public CommonResult<Boolean> swapSortOrder(@Valid @RequestBody SwapSortOrderReqVO reqVO) {
        categoryService.swapSortOrder(reqVO.getNodeId1(), reqVO.getNodeId2());
        return success(true);
    }
}
