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

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import com.yhy.module.core.controller.admin.workbench.vo.calcbaserate.CalcBaseRateCatalogTreeRespVO;
import com.yhy.module.core.controller.admin.workbench.vo.calcbaserate.CalcBaseRateDirectoryRespVO;
import com.yhy.module.core.controller.admin.workbench.vo.calcbaserate.CalcBaseRateItemRespVO;
import com.yhy.module.core.controller.admin.workbench.vo.calcbaserate.UpdateBoqDivisionBaseRateReqVO;
import com.yhy.module.core.service.calcbaserate.CalcBaseRateCatalogService;
import com.yhy.module.core.service.calcbaserate.CalcBaseRateDirectoryService;
import com.yhy.module.core.service.calcbaserate.CalcBaseRateItemService;
import com.yhy.module.core.service.workbench.WbBoqDivisionService;
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.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.GetMapping;
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;

/**
 * 工作台 - 基数费率 Controller
 * 用于项目指引弹窗中的基数费率标签页
 *
 * @author yhy
 */
@Tag(name = "工作台 - 基数费率")
@RestController
@RequestMapping("/core/wb/calc-base-rate")
@Validated
public class WbCalcBaseRateController {

    @Resource
    private CalcBaseRateCatalogService calcBaseRateCatalogService;

    @Resource
    private CalcBaseRateDirectoryService calcBaseRateDirectoryService;

    @Resource
    private CalcBaseRateItemService calcBaseRateItemService;

    @Resource
    private WbBoqDivisionService wbBoqDivisionService;

    @GetMapping("/catalog/tree")
    @Operation(summary = "获取基数费率目录树（全部）")
    @PreAuthorize("@ss.hasPermission('core:wb:query')")
    public CommonResult<List<CalcBaseRateCatalogTreeRespVO>> getCalcBaseRateCatalogTree() {
        return success(calcBaseRateCatalogService.getCalcBaseRateCatalogTreeForWorkbench());
    }

    @GetMapping("/directory/tree")
    @Operation(summary = "获取基数费率目录树（根据目录树节点）")
    @Parameter(name = "catalogId", description = "基数费率目录树节点ID", required = true)
    @PreAuthorize("@ss.hasPermission('core:wb:query')")
    public CommonResult<List<CalcBaseRateDirectoryRespVO>> getCalcBaseRateDirectoryTree(
            @RequestParam("catalogId") Long catalogId) {
        return success(calcBaseRateDirectoryService.getCalcBaseRateDirectoryTreeForWorkbench(catalogId));
    }

    @GetMapping("/item/list")
    @Operation(summary = "获取基数费率项列表")
    @Parameter(name = "directoryId", description = "基数费率目录ID", required = true)
    @PreAuthorize("@ss.hasPermission('core:wb:query')")
    public CommonResult<List<CalcBaseRateItemRespVO>> getCalcBaseRateItemList(
            @RequestParam("directoryId") Long directoryId) {
        return success(calcBaseRateItemService.getCalcBaseRateItemListForWorkbench(directoryId));
    }

    @PutMapping("/update-division")
    @Operation(summary = "更新清单的基数费率")
    @PreAuthorize("@ss.hasPermission('core:wb:update')")
    public CommonResult<Boolean> updateBoqDivisionBaseRate(
            @Valid @RequestBody UpdateBoqDivisionBaseRateReqVO reqVO) {
        wbBoqDivisionService.updateBoqDivisionBaseRate(reqVO);
        return success(true);
    }
}
