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

import cn.hutool.core.bean.BeanUtil;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import com.yhy.module.core.controller.admin.quota.vo.QuotaFeeItemWithRateRespVO;
import com.yhy.module.core.controller.admin.quota.vo.QuotaUnifiedFeeRespVO;
import com.yhy.module.core.controller.admin.quota.vo.QuotaUnifiedFeeSaveReqVO;
import com.yhy.module.core.dal.dataobject.quota.QuotaUnifiedFeeDO;
import com.yhy.module.core.service.quota.QuotaUnifiedFeeService;
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 java.util.stream.Collectors;
import javax.annotation.Resource;
import javax.validation.Valid;
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;

@Tag(name = "管理后台 - 统一取费单价")
@RestController
@RequestMapping("/core/quota/unified-fee")
@Validated
public class QuotaUnifiedFeeController {

    @Resource
    private QuotaUnifiedFeeService unifiedFeeService;

    @PostMapping("/create")
    @Operation(summary = "创建统一取费单价")
    public CommonResult<Long> createUnifiedFee(@Valid @RequestBody QuotaUnifiedFeeSaveReqVO createReqVO) {
        return success(unifiedFeeService.createUnifiedFee(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新统一取费单价")
    public CommonResult<Boolean> updateUnifiedFee(@Valid @RequestBody QuotaUnifiedFeeSaveReqVO updateReqVO) {
        unifiedFeeService.updateUnifiedFee(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除统一取费单价")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> deleteUnifiedFee(@RequestParam("id") Long id) {
        unifiedFeeService.deleteUnifiedFee(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获取统一取费单价详情")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<QuotaUnifiedFeeRespVO> getUnifiedFee(@RequestParam("id") Long id) {
        QuotaUnifiedFeeDO unifiedFee = unifiedFeeService.getUnifiedFee(id);
        return success(BeanUtil.copyProperties(unifiedFee, QuotaUnifiedFeeRespVO.class));
    }

    @GetMapping("/list")
    @Operation(summary = "获取统一取费单价列表")
    @Parameter(name = "catalogItemId", description = "模式节点ID", required = true)
    public CommonResult<List<QuotaUnifiedFeeRespVO>> getUnifiedFeeList(
            @RequestParam("catalogItemId") Long catalogItemId) {
        List<QuotaUnifiedFeeDO> list = unifiedFeeService.getUnifiedFeeList(catalogItemId);
        return success(list.stream()
                .map(item -> BeanUtil.copyProperties(item, QuotaUnifiedFeeRespVO.class))
                .collect(Collectors.toList()));
    }

    @GetMapping("/tree")
    @Operation(summary = "获取统一取费单价树（与定额取费显示一致的费率项+取费项合并视图）")
    @Parameter(name = "catalogItemId", description = "模式节点ID", required = true)
    public CommonResult<List<QuotaFeeItemWithRateRespVO>> getUnifiedFeeTree(
            @RequestParam("catalogItemId") Long catalogItemId) {
        return success(unifiedFeeService.getUnifiedFeeTree(catalogItemId));
    }

    @PostMapping("/swap-sort")
    @Operation(summary = "交换排序")
    public CommonResult<Boolean> swapSort(@RequestParam("nodeId1") Long nodeId1,
                                          @RequestParam("nodeId2") Long nodeId2) {
        unifiedFeeService.swapSort(nodeId1, nodeId2);
        return success(true);
    }
}
