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.BoqItemTreeRespVO;
import com.yhy.module.core.controller.admin.boq.vo.BoqItemTreeSaveReqVO;
import com.yhy.module.core.controller.admin.boq.vo.BoqItemTreeSwapSortReqVO;
import com.yhy.module.core.service.boq.BoqItemTreeService;
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/item-tree")
@Validated
public class BoqItemTreeController {

    @Resource
    private BoqItemTreeService boqItemTreeService;

    @PostMapping("/create")
    @Operation(summary = "创建清单项树节点")
    @PreAuthorize("@ss.hasPermission('core:boq:item-tree:create')")
    public CommonResult<Long> createBoqItemTree(@Valid @RequestBody BoqItemTreeSaveReqVO createReqVO) {
        return success(boqItemTreeService.createBoqItemTree(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新清单项树节点")
    @PreAuthorize("@ss.hasPermission('core:boq:item-tree:update')")
    public CommonResult<Boolean> updateBoqItemTree(@Valid @RequestBody BoqItemTreeSaveReqVO updateReqVO) {
        boqItemTreeService.updateBoqItemTree(updateReqVO);
        return success(true);
    }

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

    @GetMapping("/get")
    @Operation(summary = "获取清单项树节点")
    @Parameter(name = "id", description = "节点ID", required = true, example = "1")
    @PreAuthorize("@ss.hasPermission('core:boq:item-tree:query')")
    public CommonResult<BoqItemTreeRespVO> getBoqItemTree(@RequestParam("id") Long id) {
        return success(boqItemTreeService.getBoqItemTree(id));
    }

    @GetMapping("/tree")
    @Operation(summary = "获取清单项树（树形结构）")
    @Parameter(name = "boqCatalogItemId", description = "清单专业ID", required = true, example = "1")
    @PreAuthorize("@ss.hasPermission('core:boq:item-tree:query')")
    public CommonResult<List<BoqItemTreeRespVO>> getBoqItemTreeTree(@RequestParam("boqCatalogItemId") Long boqCatalogItemId) {
        return success(boqItemTreeService.getBoqItemTreeTree(boqCatalogItemId));
    }

    @GetMapping("/list")
    @Operation(summary = "获取清单项树列表")
    @Parameter(name = "boqCatalogItemId", description = "清单专业ID", required = true, example = "1")
    @PreAuthorize("@ss.hasPermission('core:boq:item-tree:query')")
    public CommonResult<List<BoqItemTreeRespVO>> getBoqItemTreeList(@RequestParam("boqCatalogItemId") Long boqCatalogItemId) {
        return success(boqItemTreeService.getBoqItemTreeList(boqCatalogItemId));
    }

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