game-api/ff-game/src/main/java/com/ff/agent/controller/AgentController.java

148 lines
5.5 KiB
Java

package com.ff.agent.controller;
import com.ff.agent.dto.AgentCreateTenantDTO;
import com.ff.agent.dto.AgentTenantSecretKeyDTO;
import com.ff.base.constant.Constants;
import com.ff.base.core.controller.BaseController;
import com.ff.base.core.domain.AjaxResult;
import com.ff.base.core.page.TableDataInfo;
import com.ff.base.enums.OperationType;
import com.ff.base.enums.QuotaType;
import com.ff.base.exception.base.BaseException;
import com.ff.base.system.domain.TenantPlatform;
import com.ff.base.system.domain.TenantSecretKey;
import com.ff.base.system.dto.CreateTenantDTO;
import com.ff.base.system.dto.TenantSecretKeyDTO;
import com.ff.base.system.service.ITenantPlatformService;
import com.ff.base.system.service.ITenantSecretKeyService;
import com.ff.base.utils.DateUtils;
import com.ff.base.utils.bean.BeanUtils;
import com.ff.base.utils.ip.IpUtils;
import com.ff.common.domain.TenantAgentPlatform;
import com.ff.common.dto.BalanceChangesDTO;
import com.ff.common.service.ITenantAgentPlatformService;
import com.ff.common.service.ITenantGameQuotaService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
/**
* api控制器
*
* @author shi
* @date 2025/02/10
*/
@RestController
@RequestMapping("/agent/tenant")
@Slf4j
public class AgentController extends BaseController {
@Resource
private ITenantSecretKeyService tenantSecretKeyService;
@Resource
private ITenantGameQuotaService tenantGameQuotaService;
@Resource
private ITenantPlatformService tenantPlatformService;
@Resource
private ITenantAgentPlatformService tenantAgentPlatformService;
/**
* 列表
*
* @param tenantSecretKeyDTO 租户密钥
* @return {@link TableDataInfo }
*/
@GetMapping("/list")
@Transactional
@PreAuthorize("@ss.hasPermi('agent:tenant:list')")
public TableDataInfo list(TenantSecretKeyDTO tenantSecretKeyDTO) {
startPage();
tenantSecretKeyDTO.setAgentId(getUserId());
List<TenantSecretKey> tenantSecretKeys = tenantSecretKeyService.selectTenantSecretKeyDTOList(tenantSecretKeyDTO);
TableDataInfo dataTable = getDataTable(tenantSecretKeys);
List<AgentTenantSecretKeyDTO> list = new ArrayList<>();
for (TenantSecretKey row : (List<TenantSecretKey>) dataTable.getRows()) {
AgentTenantSecretKeyDTO agentTenantSecretKeyDTO = new AgentTenantSecretKeyDTO();
BeanUtils.copyProperties(row, agentTenantSecretKeyDTO);
agentTenantSecretKeyDTO.setTenantPlatforms(tenantPlatformService.selectTenantPlatformList(TenantPlatform.builder()
.tenantId(row.getId())
.build()));
list.add(agentTenantSecretKeyDTO);
}
dataTable.setRows(list);
return dataTable;
}
/**
* 创建租户
*
* @return {@link AjaxResult }
*/
@PostMapping("/create")
@Transactional
@PreAuthorize("@ss.hasPermi('agent:tenant:create')")
public AjaxResult createTenant(@Validated @RequestBody AgentCreateTenantDTO agentCreateTenantDTO) {
//构造租户的充值信息
List<TenantPlatform> tenantPlatforms = new ArrayList<>();
for (TenantAgentPlatform tenantAgentPlatform : agentCreateTenantDTO.getTenantAgentPlatforms()) {
TenantAgentPlatform agentPlatform = tenantAgentPlatformService.selectTenantAgentPlatformById(tenantAgentPlatform.getId());
// 成本比平台成本大
if (agentPlatform.getCost().compareTo(tenantAgentPlatform.getCost()) > 0) {
throw new BaseException("成本比例不允许比最低比例小");
}
TenantPlatform tenantPlatform = new TenantPlatform();
BeanUtils.copyProperties(tenantAgentPlatform, tenantPlatform);
tenantPlatforms.add(tenantPlatform);
}
//创建租户
Integer tenant = tenantSecretKeyService.createTenant(CreateTenantDTO.builder()
.registerTime(DateUtils.getNowDate())
.registerIp(IpUtils.getIpAddr())
.agentId(getUserId())
.account(agentCreateTenantDTO.getAccount())
.password(agentCreateTenantDTO.getPassword())
.scoreRatio(agentCreateTenantDTO.getScoreRatio())
.tenantType(agentCreateTenantDTO.getTenantType())
.depositRatio(agentCreateTenantDTO.getDepositRatio())
.tenantPlatforms(tenantPlatforms)
.build());
if (tenant > 0) {
//信誉额度
if (!ObjectUtils.isEmpty(agentCreateTenantDTO.getRealBalance())) {
tenantGameQuotaService.balanceChanges(BalanceChangesDTO.builder()
.isOut(Boolean.TRUE)
.currencyCode(Constants.USDT)
.tenantKey(agentCreateTenantDTO.getAccount())
.balance(agentCreateTenantDTO.getRealBalance())
.operationType(OperationType.TENANT_RECHARGE.getCode())
.quotaType(QuotaType.REPUTATION.getCode())
.remark(OperationType.TENANT_RECHARGE.getDescription())
.build());
}
}
return toAjax(tenant);
}
}