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.CatalogItemSaveReqVO;
import com.yhy.module.core.controller.admin.resource.vo.ResourceCategorySimpleRespVO;
import com.yhy.module.core.controller.admin.resource.vo.SwapSortOrderReqVO;
import com.yhy.module.core.dal.dataobject.resource.ResourceCatalogItemDO;
import com.yhy.module.core.service.resource.ResourceCatalogItemService;
import com.yhy.module.core.service.resource.ResourceCategoryTreeService;
import io.swagger.v3.oas.annotations.Operation;
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/catalog-items")
@Validated
public class ResourceCatalogItemController {

    @Resource
    private ResourceCatalogItemService itemService;
    
    @Resource
    private ResourceCategoryTreeService categoryTreeService;

    @GetMapping("/{catalogId}/tree")
    @Operation(summary = "获取分类/条目树")
    public CommonResult<List<ResourceCatalogItemDO>> tree(@PathVariable("catalogId") Long catalogId) {
        return success(itemService.listByCatalog(catalogId));
    }
    
    @GetMapping("/{id}/allowed-categories")
    @Operation(summary = "获取目录树节点允许的类别列表", description = "用于创建工料机项时，限制类别选择范围")
    public CommonResult<List<ResourceCategorySimpleRespVO>> getAllowedCategories(@PathVariable("id") Long id) {
        return success(itemService.getAllowedCategories(id));
    }

    @PostMapping
    @Operation(summary = "创建分类/条目")
    public CommonResult<Long> create(@Valid @RequestBody CatalogItemSaveReqVO reqVO) {
        return success(itemService.create(reqVO));
    }

    @PutMapping
    @Operation(summary = "更新分类/条目")
    public CommonResult<Boolean> update(@Valid @RequestBody CatalogItemSaveReqVO reqVO) {
        itemService.update(reqVO);
        return success(true);
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除分类/条目")
    public CommonResult<Boolean> delete(@PathVariable("id") Long id) {
        itemService.delete(id);
        return success(true);
    }
    
    @PutMapping("/swap-sort")
    @Operation(summary = "交换两个节点的排序")
    public CommonResult<Boolean> swapSortOrder(@Valid @RequestBody SwapSortOrderReqVO reqVO) {
        itemService.swapSortOrder(reqVO.getNodeId1(), reqVO.getNodeId2());
        return success(true);
    }
}
