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

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

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import com.yhy.module.core.controller.admin.quota.vo.QuotaRateFieldLabelRespVO;
import com.yhy.module.core.controller.admin.quota.vo.QuotaRateFieldLabelSaveReqVO;
import com.yhy.module.core.dal.dataobject.quota.QuotaRateFieldLabelDO;
import com.yhy.module.core.service.quota.QuotaRateFieldLabelService;
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.validation.Valid;
import lombok.RequiredArgsConstructor;
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;

/**
 * 定额费率字段标签字典 Controller
 *
 * @author yhy
 */
@Tag(name = "管理后台 - 定额费率字段标签字典")
@RestController
@RequestMapping("/core/quota/rate-field-label")
@RequiredArgsConstructor
@Validated
public class QuotaRateFieldLabelController {

    private final QuotaRateFieldLabelService fieldLabelService;

    @PostMapping("/create")
    @Operation(summary = "创建字段标签")
    @PreAuthorize("@ss.hasPermission('core:quota:rate:write')")
    public CommonResult<Long> createFieldLabel(@Valid @RequestBody QuotaRateFieldLabelSaveReqVO createReqVO) {
        return success(fieldLabelService.createFieldLabel(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新字段标签")
    @PreAuthorize("@ss.hasPermission('core:quota:rate:write')")
    public CommonResult<Boolean> updateFieldLabel(@Valid @RequestBody QuotaRateFieldLabelSaveReqVO updateReqVO) {
        fieldLabelService.updateFieldLabel(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除字段标签")
    @Parameter(name = "id", description = "标签ID", required = true, example = "1")
    @PreAuthorize("@ss.hasPermission('core:quota:rate:write')")
    public CommonResult<Boolean> deleteFieldLabel(@RequestParam("id") Long id) {
        fieldLabelService.deleteFieldLabel(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获取字段标签详情")
    @Parameter(name = "id", description = "标签ID", required = true, example = "1")
    @PreAuthorize("@ss.hasPermission('core:quota:rate:read')")
    public CommonResult<QuotaRateFieldLabelRespVO> getFieldLabel(@RequestParam("id") Long id) {
        QuotaRateFieldLabelDO label = fieldLabelService.getFieldLabel(id);
        return success(BeanUtils.toBean(label, QuotaRateFieldLabelRespVO.class));
    }

    @GetMapping("/list")
    @Operation(summary = "获取字段标签列表")
    @Parameter(name = "catalogItemId", description = "模式节点ID", required = true, example = "1")
    @PreAuthorize("@ss.hasPermission('core:quota:rate:read')")
    public CommonResult<List<QuotaRateFieldLabelRespVO>> getFieldLabelList(@RequestParam("catalogItemId") Long catalogItemId) {
        return success(fieldLabelService.getFieldLabelList(catalogItemId));
    }

    @PostMapping("/swap-sort")
    @Operation(summary = "交换排序")
    @PreAuthorize("@ss.hasPermission('core:quota:rate:write')")
    public CommonResult<Boolean> swapSort(
            @RequestParam("labelId1") Long labelId1,
            @RequestParam("labelId2") Long labelId2) {
        fieldLabelService.swapSort(labelId1, labelId2);
        return success(true);
    }

}
