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

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import com.yhy.module.core.controller.admin.calcbaserate.vo.CalcBaseRateDirectoryRespVO;
import com.yhy.module.core.controller.admin.calcbaserate.vo.CalcBaseRateDirectorySaveReqVO;
import com.yhy.module.core.controller.admin.calcbaserate.vo.CalcBaseRateDirectorySwapSortReqVO;
import com.yhy.module.core.service.calcbaserate.CalcBaseRateDirectoryService;
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;

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

/**
 * 基数费率目录 Controller
 *
 * @author yhy
 */
@Tag(name = "管理后台 - 基数费率目录")
@RestController
@RequestMapping("/core/calc-base-rate/directory")
@Validated
public class CalcBaseRateDirectoryController {

    @Resource
    private CalcBaseRateDirectoryService calcBaseRateDirectoryService;

    @PostMapping("/create")
    @Operation(summary = "创建基数费率目录节点")
    @PreAuthorize("@ss.hasPermission('core:calc-base-rate-directory:create')")
    public CommonResult<Long> createCalcBaseRateDirectory(
            @Valid @Validated(CalcBaseRateDirectorySaveReqVO.CreateGroup.class)
            @RequestBody CalcBaseRateDirectorySaveReqVO createReqVO) {
        return success(calcBaseRateDirectoryService.createCalcBaseRateDirectory(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新基数费率目录节点")
    @PreAuthorize("@ss.hasPermission('core:calc-base-rate-directory:update')")
    public CommonResult<Boolean> updateCalcBaseRateDirectory(
            @Valid @Validated(CalcBaseRateDirectorySaveReqVO.UpdateGroup.class)
            @RequestBody CalcBaseRateDirectorySaveReqVO updateReqVO) {
        calcBaseRateDirectoryService.updateCalcBaseRateDirectory(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除基数费率目录节点")
    @Parameter(name = "id", description = "节点ID", required = true)
    @PreAuthorize("@ss.hasPermission('core:calc-base-rate-directory:delete')")
    public CommonResult<Boolean> deleteCalcBaseRateDirectory(@RequestParam("id") Long id) {
        calcBaseRateDirectoryService.deleteCalcBaseRateDirectory(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获取基数费率目录节点")
    @Parameter(name = "id", description = "节点ID", required = true)
    @PreAuthorize("@ss.hasPermission('core:calc-base-rate-directory:query')")
    public CommonResult<CalcBaseRateDirectoryRespVO> getCalcBaseRateDirectory(@RequestParam("id") Long id) {
        return success(calcBaseRateDirectoryService.getCalcBaseRateDirectory(id));
    }

    @GetMapping("/tree")
    @Operation(summary = "获取基数费率目录树")
    @Parameter(name = "calcBaseRateCatalogId", description = "基数费率目录树节点ID", required = true)
    @PreAuthorize("@ss.hasPermission('core:calc-base-rate-directory:query')")
    public CommonResult<List<CalcBaseRateDirectoryRespVO>> getCalcBaseRateDirectoryTree(
            @RequestParam("calcBaseRateCatalogId") Long calcBaseRateCatalogId) {
        return success(calcBaseRateDirectoryService.getCalcBaseRateDirectoryTree(calcBaseRateCatalogId));
    }

    @PostMapping("/swap-sort")
    @Operation(summary = "交换排序")
    @PreAuthorize("@ss.hasPermission('core:calc-base-rate-directory:update')")
    public CommonResult<Boolean> swapSort(@Valid @RequestBody CalcBaseRateDirectorySwapSortReqVO swapReqVO) {
        calcBaseRateDirectoryService.swapSort(swapReqVO);
        return success(true);
    }

    @DeleteMapping("/force-delete")
    @Operation(summary = "强制删除目录节点（级联删除所有关联数据）")
    @Parameter(name = "id", description = "节点ID", required = true)
    @PreAuthorize("@ss.hasPermission('core:calc-base-rate-directory:delete')")
    public CommonResult<Boolean> forceDeleteCalcBaseRateDirectory(@RequestParam("id") Long id) {
        calcBaseRateDirectoryService.forceDeleteCalcBaseRateDirectory(id);
        return success(true);
    }
}
