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

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import com.yhy.module.core.controller.admin.quota.vo.QuotaRateItemRespVO;
import com.yhy.module.core.controller.admin.quota.vo.QuotaRateItemSaveReqVO;
import com.yhy.module.core.controller.admin.quota.vo.QuotaRateValueRulesReqVO;
import com.yhy.module.core.service.quota.QuotaRateItemService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

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

/**
 * 定额费率项 Controller
 */
@Tag(name = "管理后台 - 定额费率项")
@RestController
@RequestMapping("/core/quota/rate/item")
@Validated
@RequiredArgsConstructor
public class QuotaRateItemController {

    private final QuotaRateItemService rateItemService;

    @PostMapping("/create")
    @Operation(summary = "创建费率项")
    public CommonResult<Long> createRateItem(@Valid @RequestBody QuotaRateItemSaveReqVO createReqVO) {
        return success(rateItemService.createRateItem(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新费率项")
    public CommonResult<Boolean> updateRateItem(@Valid @RequestBody QuotaRateItemSaveReqVO updateReqVO) {
        rateItemService.updateRateItem(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除费率项")
    @Parameter(name = "id", description = "费率项ID", required = true)
    public CommonResult<Boolean> deleteRateItem(@RequestParam("id") Long id) {
        rateItemService.deleteRateItem(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获取费率项详情")
    @Parameter(name = "id", description = "费率项ID", required = true)
    public CommonResult<QuotaRateItemRespVO> getRateItem(@RequestParam("id") Long id) {
        return success(rateItemService.getRateItemDetail(id));
    }

    @GetMapping("/list")
    @Operation(summary = "获取费率项列表")
    @Parameter(name = "catalogItemId", description = "模式节点ID", required = true)
    @Parameter(name = "parentId", description = "父节点ID")
    public CommonResult<List<QuotaRateItemRespVO>> getRateItemList(
            @RequestParam("catalogItemId") Long catalogItemId,
            @RequestParam(value = "parentId", required = false) Long parentId) {
        return success(rateItemService.getRateItemList(catalogItemId, parentId));
    }

    @GetMapping("/tree")
    @Operation(summary = "获取费率项树")
    @Parameter(name = "catalogItemId", description = "模式节点ID", required = true)
    public CommonResult<List<QuotaRateItemRespVO>> getRateItemTree(
            @RequestParam("catalogItemId") Long catalogItemId) {
        return success(rateItemService.getRateItemTree(catalogItemId));
    }

    @PostMapping("/swap-sort")
    @Operation(summary = "同级交换排序")
    @Parameter(name = "nodeId1", description = "节点1 ID", required = true)
    @Parameter(name = "nodeId2", description = "节点2 ID", required = true)
    public CommonResult<Boolean> swapSort(
            @RequestParam("nodeId1") Long nodeId1,
            @RequestParam("nodeId2") Long nodeId2) {
        rateItemService.swapSort(nodeId1, nodeId2);
        return success(true);
    }

    @PostMapping("/config-value-rules")
    @Operation(summary = "配置动态取值规则")
    public CommonResult<Boolean> configValueRules(@Valid @RequestBody QuotaRateValueRulesReqVO reqVO) {
        rateItemService.configValueRules(reqVO);
        return success(true);
    }

    @GetMapping("/calculate-fields")
    @Operation(summary = "计算动态字段值")
    @Parameter(name = "rateItemId", description = "费率项ID", required = true)
    @Parameter(name = "baseValue", description = "基数值", required = true)
    public CommonResult<Map<Integer, BigDecimal>> calculateDynamicFields(
            @RequestParam("rateItemId") Long rateItemId,
            @RequestParam("baseValue") BigDecimal baseValue) {
        return success(rateItemService.calculateDynamicFields(rateItemId, baseValue));
    }
}
