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

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import com.yhy.module.core.controller.admin.workbench.vo.WbCompileTreeRespVO;
import com.yhy.module.core.controller.admin.workbench.vo.WbCompileTreeSaveReqVO;
import com.yhy.module.core.controller.admin.workbench.vo.WbCompileTreeSwapSortReqVO;
import com.yhy.module.core.service.workbench.WbCompileTreeService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import java.util.List;
import javax.annotation.Resource;
import javax.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 编制模式树 Controller
 *
 * @author yhy
 */
@Tag(name = "工作台 - 编制模式树")
@RestController
@RequestMapping("/core/wb/compile")
@Validated
public class WbCompileTreeController {

    @Resource
    private WbCompileTreeService wbCompileTreeService;

    @PostMapping("/init")
    @Operation(summary = "初始化编制模式树（创建根节点）")
    @PreAuthorize("@ss.hasPermission('core:wb-compile:create')")
    public CommonResult<Long> initCompileTree(
            @RequestParam("projectId") Long projectId,
            @RequestParam("projectName") String projectName) {
        return success(wbCompileTreeService.initCompileTree(projectId, projectName));
    }

    @PostMapping("/create")
    @Operation(summary = "创建节点（单项或单位工程）")
    @PreAuthorize("@ss.hasPermission('core:wb-compile:create')")
    public CommonResult<Long> createNode(@Valid @RequestBody WbCompileTreeSaveReqVO createReqVO) {
        return success(wbCompileTreeService.createNode(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新节点")
    @PreAuthorize("@ss.hasPermission('core:wb-compile:update')")
    public CommonResult<Boolean> updateNode(@Valid @RequestBody WbCompileTreeSaveReqVO updateReqVO) {
        wbCompileTreeService.updateNode(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除节点")
    @Parameter(name = "id", description = "节点ID", required = true, example = "1")
    @PreAuthorize("@ss.hasPermission('core:wb-compile:delete')")
    public CommonResult<Boolean> deleteNode(@RequestParam("id") Long id) {
        wbCompileTreeService.deleteNode(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获取节点详情")
    @Parameter(name = "id", description = "节点ID", required = true, example = "1")
    @PreAuthorize("@ss.hasPermission('core:wb-compile:query')")
    public CommonResult<WbCompileTreeRespVO> getNode(@RequestParam("id") Long id) {
        return success(wbCompileTreeService.getNode(id));
    }

    @GetMapping("/tree")
    @Operation(summary = "获取编制模式树")
    @Parameter(name = "projectId", description = "项目节点ID", required = true, example = "1")
    @PreAuthorize("@ss.hasPermission('core:wb-compile:query')")
    public CommonResult<List<WbCompileTreeRespVO>> getTree(@RequestParam("projectId") Long projectId) {
        return success(wbCompileTreeService.getTree(projectId));
    }

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