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

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

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import com.yhy.module.core.controller.admin.workbench.vo.WbUnitInfoRespVO;
import com.yhy.module.core.controller.admin.workbench.vo.WbUnitInfoSaveReqVO;
import com.yhy.module.core.controller.admin.workbench.vo.WbUnitRateConfigSaveReqVO;
import com.yhy.module.core.controller.admin.workbench.vo.WbUsedQuotaSpecialtyRespVO;
import com.yhy.module.core.service.workbench.WbUnitInfoService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import java.util.List;
import java.util.Map;
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.PostMapping;
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/wb/unit-info")
@Validated
public class WbUnitInfoController {

    @Resource
    private WbUnitInfoService wbUnitInfoService;

    @GetMapping("/get")
    @Operation(summary = "获取单位工程信息")
    @Parameter(name = "compileTreeId", description = "编制树节点ID（单位工程节点）", required = true, example = "1")
    @PreAuthorize("@ss.hasPermission('core:wb-unit-info:query')")
    public CommonResult<WbUnitInfoRespVO> get(@RequestParam("compileTreeId") Long compileTreeId) {
        return success(wbUnitInfoService.getByCompileTreeId(compileTreeId));
    }

    @PostMapping("/save")
    @Operation(summary = "保存单位工程信息")
    @PreAuthorize("@ss.hasPermission('core:wb-unit-info:update')")
    public CommonResult<Long> save(@Valid @RequestBody WbUnitInfoSaveReqVO saveReqVO) {
        return success(wbUnitInfoService.save(saveReqVO));
    }

    // ==================== 单位工程级费率及取费相关接口 ====================

    @GetMapping("/rate-mode-bindings")
    @Operation(summary = "获取单位工程的费率模式绑定列表")
    @Parameter(name = "compileTreeId", description = "编制树节点ID（单位工程节点）", required = true, example = "1")
    @PreAuthorize("@ss.hasPermission('core:wb-unit-info:query')")
    public CommonResult<List<Map<String, Object>>> getUnitRateModeBindings(@RequestParam("compileTreeId") Long compileTreeId) {
        return success(wbUnitInfoService.getUnitRateModeBindings(compileTreeId));
    }

    @PostMapping("/switch-rate-mode")
    @Operation(summary = "切换单位工程的费率模式")
    @Parameters({
        @Parameter(name = "compileTreeId", description = "编制树节点ID（单位工程节点）", required = true, example = "1"),
        @Parameter(name = "quotaCatalogItemId", description = "定额专业ID", required = true, example = "100"),
        @Parameter(name = "rateModeId", description = "费率模式ID", required = true, example = "200")
    })
    @PreAuthorize("@ss.hasPermission('core:wb-unit-info:update')")
    public CommonResult<Boolean> switchUnitRateMode(
            @RequestParam("compileTreeId") Long compileTreeId,
            @RequestParam("quotaCatalogItemId") Long quotaCatalogItemId,
            @RequestParam("rateModeId") Long rateModeId) {
        wbUnitInfoService.switchUnitRateMode(compileTreeId, quotaCatalogItemId, rateModeId);
        return success(true);
    }

    @GetMapping("/rate-config")
    @Operation(summary = "获取单位工程的费率配置")
    @Parameters({
        @Parameter(name = "compileTreeId", description = "编制树节点ID（单位工程节点）", required = true, example = "1"),
        @Parameter(name = "quotaCatalogItemId", description = "定额专业ID", required = true, example = "100")
    })
    @PreAuthorize("@ss.hasPermission('core:wb-unit-info:query')")
    public CommonResult<Map<String, Object>> getUnitRateConfig(
            @RequestParam("compileTreeId") Long compileTreeId,
            @RequestParam("quotaCatalogItemId") Long quotaCatalogItemId) {
        return success(wbUnitInfoService.getUnitRateConfig(compileTreeId, quotaCatalogItemId));
    }

    @PostMapping("/save-rate-config")
    @Operation(summary = "保存单位工程的费率配置")
    @PreAuthorize("@ss.hasPermission('core:wb-unit-info:update')")
    public CommonResult<Boolean> saveUnitRateConfig(@Valid @RequestBody WbUnitRateConfigSaveReqVO reqVO) {
        wbUnitInfoService.saveUnitRateConfig(reqVO.getCompileTreeId(), reqVO.getQuotaCatalogItemId(),
                reqVO.getRateModeId(), reqVO.getRateSettings(), reqVO.getFeeSettings());
        return success(true);
    }

    @GetMapping("/used-quota-specialties-by-unit")
    @Operation(summary = "获取单位工程使用的定额专业列表")
    @Parameter(name = "compileTreeId", description = "编制树节点ID（单位工程节点）", required = true, example = "1")
    @PreAuthorize("@ss.hasPermission('core:wb-unit-info:query')")
    public CommonResult<List<WbUsedQuotaSpecialtyRespVO>> getUsedQuotaSpecialtiesByUnit(@RequestParam("compileTreeId") Long compileTreeId) {
        return success(wbUnitInfoService.getUsedQuotaSpecialtiesByUnit(compileTreeId));
    }

    @GetMapping("/used-quota-specialties")
    @Operation(summary = "获取项目下所有使用的定额专业列表（合并所有单位工程）")
    @Parameter(name = "projectId", description = "项目ID", required = true, example = "1")
    @PreAuthorize("@ss.hasPermission('core:wb-unit-info:query')")
    public CommonResult<List<WbUsedQuotaSpecialtyRespVO>> getUsedQuotaSpecialties(@RequestParam("projectId") Long projectId) {
        return success(wbUnitInfoService.getUsedQuotaSpecialtiesByProject(projectId));
    }
}
