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

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import com.yhy.module.core.controller.admin.infoprice.vo.InfoPriceCategoryTreeRespVO;
import com.yhy.module.core.controller.admin.infoprice.vo.InfoPriceCategoryTreeSaveReqVO;
import com.yhy.module.core.controller.admin.infoprice.vo.InfoPriceCategoryTreeSwapSortReqVO;
import com.yhy.module.core.service.infoprice.InfoPriceCategoryTreeService;
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;

@Tag(name = "管理后台 - 信息价分类树")
@RestController
@RequestMapping("/core/info-price/category-tree")
@Validated
public class InfoPriceCategoryTreeController {

    @Resource
    private InfoPriceCategoryTreeService categoryTreeService;

    @PostMapping("/create")
    @Operation(summary = "创建信息价分类树节点")
    @PreAuthorize("@ss.hasPermission('core:info-price:create')")
    public CommonResult<Long> createCategoryTree(@Valid @RequestBody InfoPriceCategoryTreeSaveReqVO createReqVO) {
        return success(categoryTreeService.createCategoryTree(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新信息价分类树节点")
    @PreAuthorize("@ss.hasPermission('core:info-price:update')")
    public CommonResult<Boolean> updateCategoryTree(@Valid @RequestBody InfoPriceCategoryTreeSaveReqVO updateReqVO) {
        categoryTreeService.updateCategoryTree(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除信息价分类树节点")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('core:info-price:delete')")
    public CommonResult<Boolean> deleteCategoryTree(@RequestParam("id") Long id) {
        categoryTreeService.deleteCategoryTree(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得信息价分类树节点")
    @Parameter(name = "id", description = "编号", required = true, example = "1")
    @PreAuthorize("@ss.hasPermission('core:info-price:query')")
    public CommonResult<InfoPriceCategoryTreeRespVO> getCategoryTree(@RequestParam("id") Long id) {
        InfoPriceCategoryTreeRespVO categoryTree = categoryTreeService.getCategoryTree(id);
        return success(categoryTree);
    }

    @GetMapping("/list")
    @Operation(summary = "获得信息价分类树列表")
    @Parameter(name = "bookId", description = "信息价册ID", required = true, example = "1")
    @PreAuthorize("@ss.hasPermission('core:info-price:query')")
    public CommonResult<List<InfoPriceCategoryTreeRespVO>> getCategoryTreeList(@RequestParam("bookId") Long bookId) {
        List<InfoPriceCategoryTreeRespVO> list = categoryTreeService.getCategoryTreeList(bookId);
        return success(list);
    }

    @GetMapping("/tree")
    @Operation(summary = "获得信息价分类树（树形结构）")
    @Parameter(name = "bookId", description = "信息价册ID", required = true, example = "1")
    @PreAuthorize("@ss.hasPermission('core:info-price:query')")
    public CommonResult<List<InfoPriceCategoryTreeRespVO>> getCategoryTreeTree(@RequestParam("bookId") Long bookId) {
        List<InfoPriceCategoryTreeRespVO> tree = categoryTreeService.getCategoryTreeTree(bookId);
        return success(tree);
    }

    @PostMapping("/swap-sort")
    @Operation(summary = "交换排序")
    @PreAuthorize("@ss.hasPermission('core:info-price:update')")
    public CommonResult<Boolean> swapSort(@Valid @RequestBody InfoPriceCategoryTreeSwapSortReqVO reqVO) {
        categoryTreeService.swapSort(reqVO);
        return success(true);
    }

}
