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.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 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;

/**
 * 定额子目 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 = true, example = "1")
    @PreAuthorize("@ss.hasPermission('core:quota:query')")
    public CommonResult<List<QuotaItemRespVO>> getQuotaItemList(@RequestParam("catalogItemId") Long catalogItemId) {
        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);
    }
}
