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

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

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import com.yhy.module.core.controller.admin.quota.vo.QuotaItemRespVO;
import com.yhy.module.core.controller.admin.quota.vo.QuotaItemSaveReqVO;
import com.yhy.module.core.service.quota.QuotaItemService;
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.Collections;
import java.util.List;
import javax.annotation.Resource;
import javax.validation.Valid;
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/item")
@Validated
public class QuotaItemController {

    @Resource
    private QuotaItemService quotaItemService;

    @PostMapping("/create")
    @Operation(summary = "创建定额基价")
    @PreAuthorize("@ss.hasPermission('core:quota:create')")
    public CommonResult<Long> createQuotaItem(@Valid @RequestBody QuotaItemSaveReqVO createReqVO) {
        return success(quotaItemService.createQuotaItem(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新定额基价")
    @PreAuthorize("@ss.hasPermission('core:quota:update')")
    public CommonResult<Boolean> updateQuotaItem(@Valid @RequestBody QuotaItemSaveReqVO updateReqVO) {
        quotaItemService.updateQuotaItem(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除定额基价")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('core:quota:delete')")
    public CommonResult<Boolean> deleteQuotaItem(@RequestParam("id") Long id) {
        quotaItemService.deleteQuotaItem(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得定额基价详情")
    @Parameter(name = "id", description = "编号", required = true, example = "1")
    @PreAuthorize("@ss.hasPermission('core:quota:query')")
    public CommonResult<QuotaItemRespVO> getQuotaItem(@RequestParam("id") Long id) {
        return success(quotaItemService.getQuotaItemDetail(id));
    }

    @GetMapping("/list")
    @Operation(summary = "获得定额基价列表")
    @Parameter(name = "catalogItemId", description = "定额条目ID", required = false, example = "1")
    @PreAuthorize("@ss.hasPermission('core:quota:query')")
    public CommonResult<List<QuotaItemRespVO>> getQuotaItemList(@RequestParam(value = "catalogItemId", required = false) Long catalogItemId) {
        if (catalogItemId == null) {
            return success(Collections.emptyList());
        }
        return success(quotaItemService.getQuotaItemList(catalogItemId));
    }

    @PostMapping("/calculate-price")
    @Operation(summary = "计算定额基价")
    @Parameter(name = "id", description = "定额基价ID", required = true)
    @PreAuthorize("@ss.hasPermission('core:quota:update')")
    public CommonResult<Boolean> calculateBasePrice(@RequestParam("id") Long id) {
        quotaItemService.calculateBasePrice(id);
        return success(true);
    }

    @GetMapping("/get-by-code")
    @Operation(summary = "根据编码查询定额基价")
    @Parameter(name = "code", description = "编码", required = true, example = "A001")
    @PreAuthorize("@ss.hasPermission('core:quota:query')")
    public CommonResult<List<QuotaItemRespVO>> getQuotaItemByCode(@RequestParam("code") String code) {
        return success(quotaItemService.getQuotaItemByCode(code));
    }

    @GetMapping("/get-rate-mode-id")
    @Operation(summary = "获取定额基价对应的费率模式节点ID")
    @Parameter(name = "quotaItemId", description = "定额基价ID", required = true, example = "1")
    @PreAuthorize("@ss.hasPermission('core:quota:query')")
    public CommonResult<Long> getRateModeIdByQuotaItem(@RequestParam("quotaItemId") Long quotaItemId) {
        return success(quotaItemService.getRateModeIdByQuotaItem(quotaItemId));
    }
}
