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.BoqDetailTreeRespVO;
import com.yhy.module.core.controller.admin.boq.vo.BoqDetailTreeSaveReqVO;
import com.yhy.module.core.controller.admin.boq.vo.BoqDetailTreeSwapSortReqVO;
import com.yhy.module.core.service.boq.BoqDetailTreeService;
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/detail-tree")
@Validated
public class BoqDetailTreeController {

    @Resource
    private BoqDetailTreeService boqDetailTreeService;

    @PostMapping("/create")
    @Operation(summary = "创建清单明细树节点")
    @PreAuthorize("@ss.hasPermission('core:boq:detail-tree:create')")
    public CommonResult<Long> createNode(@Valid @RequestBody BoqDetailTreeSaveReqVO createReqVO) {
        return success(boqDetailTreeService.createNode(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新清单明细树节点")
    @PreAuthorize("@ss.hasPermission('core:boq:detail-tree:update')")
    public CommonResult<Boolean> updateNode(@Valid @RequestBody BoqDetailTreeSaveReqVO updateReqVO) {
        boqDetailTreeService.updateNode(updateReqVO);
        return success(true);
    }

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

    @GetMapping("/get")
    @Operation(summary = "获取清单明细树节点详情")
    @Parameter(name = "id", description = "节点ID", required = true)
    @PreAuthorize("@ss.hasPermission('core:boq:detail-tree:query')")
    public CommonResult<BoqDetailTreeRespVO> getNode(@RequestParam("id") Long id) {
        return success(boqDetailTreeService.getNode(id));
    }

    @GetMapping("/tree")
    @Operation(summary = "获取清单明细树形结构")
    @Parameter(name = "boqSubItemId", description = "清单子项ID", required = true)
    @PreAuthorize("@ss.hasPermission('core:boq:detail-tree:query')")
    public CommonResult<List<BoqDetailTreeRespVO>> getTree(@RequestParam("boqSubItemId") Long boqSubItemId) {
        return success(boqDetailTreeService.getTree(boqSubItemId));
    }

    @GetMapping("/list")
    @Operation(summary = "获取清单明细树列表")
    @Parameter(name = "boqSubItemId", description = "清单子项ID", required = true)
    @PreAuthorize("@ss.hasPermission('core:boq:detail-tree:query')")
    public CommonResult<List<BoqDetailTreeRespVO>> getList(@RequestParam("boqSubItemId") Long boqSubItemId) {
        return success(boqDetailTreeService.getList(boqSubItemId));
    }

    @PostMapping("/swap-sort")
    @Operation(summary = "交换清单明细树节点排序")
    @PreAuthorize("@ss.hasPermission('core:boq:detail-tree:update')")
    public CommonResult<Boolean> swapSort(@Valid @RequestBody BoqDetailTreeSwapSortReqVO reqVO) {
        boqDetailTreeService.swapSort(reqVO.getNodeId1(), reqVO.getNodeId2());
        return success(true);
    }
}
