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.QuotaRateFieldRespVO;
import com.yhy.module.core.controller.admin.quota.vo.QuotaRateFieldSaveReqVO;
import com.yhy.module.core.service.quota.QuotaRateFieldService;
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.util.List;

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

/**
 * 定额费率字段绑定 Controller
 */
@Tag(name = "管理后台 - 定额费率字段绑定")
@RestController
@RequestMapping("/core/quota/rate/field")
@Validated
@RequiredArgsConstructor
public class QuotaRateFieldController {

    private final QuotaRateFieldService rateFieldService;

    @PostMapping("/save")
    @Operation(summary = "保存字段绑定（创建或更新）")
    public CommonResult<Long> saveRateField(@Valid @RequestBody QuotaRateFieldSaveReqVO saveReqVO) {
        return success(rateFieldService.saveRateField(saveReqVO));
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除字段绑定")
    @Parameter(name = "id", description = "字段绑定ID", required = true)
    public CommonResult<Boolean> deleteRateField(@RequestParam("id") Long id) {
        rateFieldService.deleteRateField(id);
        return success(true);
    }

    @GetMapping("/list")
    @Operation(summary = "获取费率项的字段绑定列表")
    @Parameter(name = "rateItemId", description = "费率项ID", required = true)
    public CommonResult<List<QuotaRateFieldRespVO>> getRateFieldList(
            @RequestParam("rateItemId") Long rateItemId) {
        return success(rateFieldService.getRateFieldList(rateItemId));
    }

    @PostMapping("/update-binding")
    @Operation(summary = "更新字段绑定")
    @Parameter(name = "rateItemId", description = "费率项ID", required = true)
    @Parameter(name = "fieldIndex", description = "字段索引", required = true)
    public CommonResult<Boolean> updateBinding(
            @RequestParam("rateItemId") Long rateItemId,
            @RequestParam("fieldIndex") Integer fieldIndex,
            @RequestBody Long[] bindingIds) {
        rateFieldService.updateBinding(rateItemId, fieldIndex, bindingIds);
        return success(true);
    }

    @PostMapping("/batch-save")
    @Operation(summary = "批量保存字段绑定")
    @Parameter(name = "rateItemId", description = "费率项ID", required = true)
    public CommonResult<Boolean> batchSaveRateFields(
            @RequestParam("rateItemId") Long rateItemId,
            @RequestBody List<QuotaRateFieldSaveReqVO> fields) {
        rateFieldService.batchSaveRateFields(rateItemId, fields);
        return success(true);
    }
}
