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

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.quota.vo.QuotaCatalogTreeRespVO;
import com.yhy.module.core.controller.admin.quota.vo.QuotaCatalogTreeSaveReqVO;
import com.yhy.module.core.controller.admin.quota.vo.QuotaCatalogTreeSwapSortReqVO;
import com.yhy.module.core.service.quota.QuotaCatalogTreeService;
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;

@Tag(name = "管理后台 - 定额子目树")
@RestController
@RequestMapping("/core/quota/catalog-tree")
@Validated
public class QuotaCatalogTreeController {

    @Resource
    private QuotaCatalogTreeService quotaCatalogTreeService;

    @PostMapping("/create")
    @Operation(summary = "创建定额子目树节点")
    @PreAuthorize("@ss.hasPermission('core:quota:catalog-tree:create')")
    public CommonResult<Long> createCatalogTree(@Valid @RequestBody QuotaCatalogTreeSaveReqVO createReqVO) {
        return success(quotaCatalogTreeService.createCatalogTree(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新定额子目树节点")
    @PreAuthorize("@ss.hasPermission('core:quota:catalog-tree:update')")
    public CommonResult<Boolean> updateCatalogTree(@Valid @RequestBody QuotaCatalogTreeSaveReqVO updateReqVO) {
        quotaCatalogTreeService.updateCatalogTree(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除定额子目树节点")
    @Parameter(name = "id", description = "节点ID", required = true, example = "1")
    @PreAuthorize("@ss.hasPermission('core:quota:catalog-tree:delete')")
    public CommonResult<Boolean> deleteCatalogTree(@RequestParam("id") Long id) {
        quotaCatalogTreeService.deleteCatalogTree(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获取定额子目树节点详情")
    @Parameter(name = "id", description = "节点ID", required = true, example = "1")
    @PreAuthorize("@ss.hasPermission('core:quota:catalog-tree:query')")
    public CommonResult<QuotaCatalogTreeRespVO> getCatalogTree(@RequestParam("id") Long id) {
        return success(quotaCatalogTreeService.getCatalogTree(id));
    }

    @GetMapping("/list")
    @Operation(summary = "获取定额子目树节点列表")
    @Parameter(name = "catalogItemId", description = "定额专业节点ID", required = true, example = "1")
    @Parameter(name = "parentId", description = "父节点ID", example = "1")
    @PreAuthorize("@ss.hasPermission('core:quota:catalog-tree:query')")
    public CommonResult<List<QuotaCatalogTreeRespVO>> getCatalogTreeList(
            @RequestParam("catalogItemId") Long catalogItemId,
            @RequestParam(value = "parentId", required = false) Long parentId) {
        return success(quotaCatalogTreeService.getCatalogTreeList(catalogItemId, parentId));
    }

    @GetMapping("/tree")
    @Operation(summary = "获取定额子目树结构")
    @Parameter(name = "catalogItemId", description = "定额专业节点ID", required = true, example = "1")
    @PreAuthorize("@ss.hasPermission('core:quota:catalog-tree:query')")
    public CommonResult<List<QuotaCatalogTreeRespVO>> getCatalogTreeTree(
            @RequestParam("catalogItemId") Long catalogItemId) {
        return success(quotaCatalogTreeService.getCatalogTreeTree(catalogItemId));
    }

    @PostMapping("/swap-sort")
    @Operation(summary = "交换排序")
    @PreAuthorize("@ss.hasPermission('core:quota:catalog-tree:update')")
    public CommonResult<Boolean> swapSort(@Valid @RequestBody QuotaCatalogTreeSwapSortReqVO swapReqVO) {
        quotaCatalogTreeService.swapSort(swapReqVO.getNodeId1(), swapReqVO.getNodeId2());
        return success(true);
    }

    @GetMapping("/list-by-rate-item")
    @Operation(summary = "根据费率项ID查询绑定的定额子目树")
    @Parameter(name = "rateItemId", description = "费率项ID", required = true, example = "3011")
    @PreAuthorize("@ss.hasPermission('core:quota:catalog-tree:query')")
    public CommonResult<List<QuotaCatalogTreeRespVO>> getCatalogTreeByRateItem(
            @RequestParam("rateItemId") Long rateItemId) {
        return success(quotaCatalogTreeService.getCatalogTreeByRateItem(rateItemId));
    }

    @GetMapping("/list-by-rate-mode-node")
    @Operation(summary = "根据费率模式节点ID查询所属定额专业的子目录树")
    @Parameter(name = "rateModeNodeId", description = "费率模式节点ID", required = true, example = "1002")
    @PreAuthorize("@ss.hasPermission('core:quota:catalog-tree:query')")
    public CommonResult<List<QuotaCatalogTreeRespVO>> getCatalogTreeByRateModeNode(
            @RequestParam("rateModeNodeId") Long rateModeNodeId) {
        return success(quotaCatalogTreeService.getCatalogTreeByRateModeNode(rateModeNodeId));
    }
}
