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

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import com.yhy.module.core.controller.admin.boq.vo.*;
import com.yhy.module.core.service.boq.BoqCatalogItemService;
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;

/**
 * 清单配置目录树 Controller
 *
 * @author yhy
 */
@Tag(name = "管理后台 - 清单配置目录树")
@RestController
@RequestMapping("/core/boq/catalog-item")
@Validated
public class BoqCatalogItemController {

    @Resource
    private BoqCatalogItemService boqCatalogItemService;

    @PostMapping("/create")
    @Operation(summary = "创建清单配置目录树节点")
    @PreAuthorize("@ss.hasPermission('core:boq-catalog-item:create')")
    public CommonResult<Long> createBoqCatalogItem(@Valid @RequestBody BoqCatalogItemSaveReqVO createReqVO) {
        return success(boqCatalogItemService.createBoqCatalogItem(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新清单配置目录树节点")
    @PreAuthorize("@ss.hasPermission('core:boq-catalog-item:update')")
    public CommonResult<Boolean> updateBoqCatalogItem(@Valid @RequestBody BoqCatalogItemSaveReqVO updateReqVO) {
        boqCatalogItemService.updateBoqCatalogItem(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除清单配置目录树节点")
    @Parameter(name = "id", description = "节点ID", required = true, example = "1")
    @PreAuthorize("@ss.hasPermission('core:boq-catalog-item:delete')")
    public CommonResult<Boolean> deleteBoqCatalogItem(@RequestParam("id") Long id) {
        boqCatalogItemService.deleteBoqCatalogItem(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获取清单配置目录树节点")
    @Parameter(name = "id", description = "节点ID", required = true, example = "1")
    @PreAuthorize("@ss.hasPermission('core:boq-catalog-item:query')")
    public CommonResult<BoqCatalogItemRespVO> getBoqCatalogItem(@RequestParam("id") Long id) {
        return success(boqCatalogItemService.getBoqCatalogItem(id));
    }

    @GetMapping("/tree")
    @Operation(summary = "获取清单配置目录树（树形结构）")
    @PreAuthorize("@ss.hasPermission('core:boq-catalog-item:query')")
    public CommonResult<List<BoqCatalogItemRespVO>> getBoqCatalogItemTree() {
        return success(boqCatalogItemService.getBoqCatalogItemTree());
    }

    @PostMapping("/bind-quota")
    @Operation(summary = "绑定定额基价节点")
    @PreAuthorize("@ss.hasPermission('core:boq-catalog-item:bind')")
    public CommonResult<Boolean> bindQuotaCatalogItem(@Valid @RequestBody BoqCatalogItemBindQuotaReqVO bindReqVO) {
        boqCatalogItemService.bindQuotaCatalogItem(bindReqVO);
        return success(true);
    }

    @PostMapping("/swap-sort")
    @Operation(summary = "交换排序")
    @PreAuthorize("@ss.hasPermission('core:boq-catalog-item:update')")
    public CommonResult<Boolean> swapSort(@Valid @RequestBody BoqCatalogItemSwapSortReqVO swapReqVO) {
        boqCatalogItemService.swapSort(swapReqVO);
        return success(true);
    }
}
