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

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

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import com.yhy.module.core.controller.admin.boq.vo.BoqGuideTreeRespVO;
import com.yhy.module.core.controller.admin.boq.vo.BoqGuideTreeSaveReqVO;
import com.yhy.module.core.controller.admin.boq.vo.BoqGuideTreeSwapSortReqVO;
import com.yhy.module.core.controller.admin.boq.vo.QuotaCodeValidateRespVO;
import com.yhy.module.core.service.boq.BoqGuideTreeService;
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/boq/guide-tree")
@Validated
public class BoqGuideTreeController {

    @Resource
    private BoqGuideTreeService boqGuideTreeService;

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

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

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

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

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

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

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

    @GetMapping("/validate-quota-code")
    @Operation(summary = "验证定额编码是否存在于绑定范围内")
    @Parameter(name = "boqSubItemId", description = "清单子项ID", required = true)
    @Parameter(name = "code", description = "定额编码", required = true)
    @PreAuthorize("@ss.hasPermission('core:boq:guide-tree:query')")
    public CommonResult<Long> validateQuotaCode(@RequestParam("boqSubItemId") Long boqSubItemId,
                                                 @RequestParam("code") String code) {
        return success(boqGuideTreeService.validateQuotaCodeInRange(boqSubItemId, code));
    }

    @GetMapping("/validate-quota-code-with-info")
    @Operation(summary = "验证定额编码并返回定额信息（包含名称）")
    @Parameter(name = "boqSubItemId", description = "清单子项ID", required = true)
    @Parameter(name = "code", description = "定额编码", required = true)
    @PreAuthorize("@ss.hasPermission('core:boq:guide-tree:query')")
    public CommonResult<QuotaCodeValidateRespVO> validateQuotaCodeWithInfo(@RequestParam("boqSubItemId") Long boqSubItemId,
                                                                            @RequestParam("code") String code) {
        return success(boqGuideTreeService.validateQuotaCodeWithInfo(boqSubItemId, code));
    }
}
