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.InfoPriceTreeRespVO;
import com.yhy.module.core.controller.admin.infoprice.vo.InfoPriceTreeSaveReqVO;
import com.yhy.module.core.controller.admin.infoprice.vo.InfoPriceTreeSwapSortReqVO;
import com.yhy.module.core.service.infoprice.InfoPriceTreeService;
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/tree")
@Validated
public class InfoPriceTreeController {

    @Resource
    private InfoPriceTreeService infoPriceTreeService;

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

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

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

    @GetMapping("/get")
    @Operation(summary = "获取信息价树节点")
    @Parameter(name = "id", description = "节点ID", required = true)
    @PreAuthorize("@ss.hasPermission('core:info-price:query')")
    public CommonResult<InfoPriceTreeRespVO> getInfoPriceTree(@RequestParam("id") Long id) {
        return success(infoPriceTreeService.getInfoPriceTree(id));
    }

    @GetMapping("/tree")
    @Operation(summary = "获取信息价树结构")
    @Parameter(name = "enumType", description = "枚举类型", required = true, example = "region_a")
    @PreAuthorize("@ss.hasPermission('core:info-price:query')")
    public CommonResult<List<InfoPriceTreeRespVO>> getInfoPriceTreeList(@RequestParam("enumType") String enumType) {
        return success(infoPriceTreeService.getInfoPriceTreeList(enumType));
    }

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