package com.ssyf.module.robot.controller.auth;

import com.ssyf.common.web.aop.RequestSynchronization;
import com.ssyf.common.web.apicode.ApiCodeUser;
import com.ssyf.common.web.result.Result;
import com.ssyf.plugs.logger.Log;
import com.ssyf.third.bmiss.model.ApiResponse;
import com.ssyf.third.bmiss.services.FundPoolService;
import com.ssyf.module.robot.domain.query.RFundPoolServiceQuery;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 资金池服务开放接口
 * 提供资金池查询、资金申请等功能
 * 
 * @author auth
 * @date 2024-12-31
 */
@Api(tags="资金-资金池管理")
@RestController
@RequestMapping("/auth/FundPoolService")
public class RFundPoolServiceAuthController {

    @Autowired
    private FundPoolService fundPoolService;

    @Log(title = "资金池服务-获取资金池总资金")
    @ApiOperation(value = "获取资金池总资金", notes = "获取当前资金池的总资金余额")
    @PostMapping(value = "/getTotalFund")
    public Result<Double> getTotalFund() {
        Double totalFund = fundPoolService.getTotalFund();
        return Result.success(totalFund);
    }

    @Log(title = "资金池服务-获取资金池详情")
    @ApiOperation(value = "获取资金池详情", notes = "获取资金池的详细信息")
    @PostMapping(value = "/getFundPoolDetail")
    public Result<Double> getFundPoolDetail() {
        Double fundDetail = fundPoolService.getFundPoolDetail();
        return Result.success(fundDetail);
    }

    @Log(title = "资金池服务-申请资金")
    @RequestSynchronization
    @ApiOperation(value = "申请资金", notes = "从资金池申请指定金额的资金")
    @PostMapping(value = "/applyForBalance")
    public Result<ApiResponse<Object>> applyForBalance(@RequestBody @Validated RFundPoolServiceQuery query) {
        
        if (query.getUserId() == null) {
            return Result.fail(ApiCodeUser.PARAMETER_ERROR.getCode(), "用户ID不能为空");
        }
        if (query.getReason() == null || query.getReason().trim().isEmpty()) {
            return Result.fail(ApiCodeUser.PARAMETER_ERROR.getCode(), "申请原因不能为空");
        }
        if (query.getAmount() == null || query.getAmount() <= 0) {
            return Result.fail(ApiCodeUser.PARAMETER_ERROR.getCode(), "申请金额必须大于0");
        }
        
        ApiResponse<Object> response = fundPoolService.applyForBalance(query.getUserId(), query.getReason(), query.getAmount());
        return Result.success(response);
    }

    @Log(title = "资金池服务-重置资金池缓存")
    @RequestSynchronization
    @ApiOperation(value = "重置资金池缓存", notes = "清除所有资金池相关的缓存数据")
    @PostMapping(value = "/resetCache")
    public Result resetCache() {
        fundPoolService.resetCache();
        return Result.success();
    }

    @Log(title = "资金池服务-清空资金池缓存")
    @RequestSynchronization
    @ApiOperation(value = "清空资金池缓存", notes = "清空所有资金池缓存数据")
    @PostMapping(value = "/clearFundPoolCache")
    public Result clearFundPoolCache() {
        fundPoolService.clearFundPoolCache();
        return Result.success();
    }
}