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

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.QuotaFeeItemRespVO;
import com.yhy.module.core.controller.admin.quota.vo.QuotaFeeItemSaveReqVO;
import com.yhy.module.core.controller.admin.quota.vo.QuotaFeeItemSwapSortReqVO;
import com.yhy.module.core.dal.dataobject.quota.QuotaFeeItemDO;
import com.yhy.module.core.service.quota.QuotaFeeItemService;
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/quota/fee/item")
@Validated
public class QuotaFeeItemController {

    @Resource
    private QuotaFeeItemService quotaFeeItemService;

    @PostMapping("/create")
    @Operation(summary = "创建取费项")
    @PreAuthorize("@ss.hasPermission('core:quota:fee:create')")
    public CommonResult<Long> createFeeItem(@Valid @RequestBody QuotaFeeItemSaveReqVO createReqVO) {
        return success(quotaFeeItemService.createFeeItem(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新取费项")
    @PreAuthorize("@ss.hasPermission('core:quota:fee:update')")
    public CommonResult<Boolean> updateFeeItem(@Valid @RequestBody QuotaFeeItemSaveReqVO updateReqVO) {
        quotaFeeItemService.updateFeeItem(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除取费项")
    @Parameter(name = "id", description = "取费项ID", required = true, example = "1")
    @PreAuthorize("@ss.hasPermission('core:quota:fee:delete')")
    public CommonResult<Boolean> deleteFeeItem(@RequestParam("id") Long id) {
        quotaFeeItemService.deleteFeeItem(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获取取费项详情")
    @Parameter(name = "id", description = "取费项ID", required = true, example = "1")
    @PreAuthorize("@ss.hasPermission('core:quota:fee:query')")
    public CommonResult<QuotaFeeItemRespVO> getFeeItem(@RequestParam("id") Long id) {
        QuotaFeeItemDO feeItem = quotaFeeItemService.getFeeItem(id);
        return success(BeanUtils.toBean(feeItem, QuotaFeeItemRespVO.class));
    }

    @GetMapping("/list")
    @Operation(summary = "获取取费项列表")
    @Parameter(name = "catalogItemId", description = "模式节点ID", required = true, example = "1002")
    @PreAuthorize("@ss.hasPermission('core:quota:fee:query')")
    public CommonResult<List<QuotaFeeItemRespVO>> getFeeItemList(@RequestParam("catalogItemId") Long catalogItemId) {
        List<QuotaFeeItemDO> list = quotaFeeItemService.getFeeItemList(catalogItemId);
        return success(BeanUtils.toBean(list, QuotaFeeItemRespVO.class));
    }

    @PostMapping("/swap-sort")
    @Operation(summary = "交换排序")
    @PreAuthorize("@ss.hasPermission('core:quota:fee:update')")
    public CommonResult<Boolean> swapSort(@Valid @RequestBody QuotaFeeItemSwapSortReqVO reqVO) {
        quotaFeeItemService.swapSort(reqVO.getNodeId1(), reqVO.getNodeId2());
        return success(true);
    }
}
