This commit is contained in:
2025-09-26 14:17:56 +08:00
committed by khalil
parent 23a516cd43
commit 71561f8695
9 changed files with 447 additions and 0 deletions

View File

@@ -0,0 +1,9 @@
package com.accompany.business.vo.dealer;
import lombok.Data;
@Data
public class DealerAccount {
private Long coinBalance;
private Long saleAmount;
}

View File

@@ -0,0 +1,34 @@
package com.accompany.business.vo.dealer;
public enum DealerBusiStatus {
SUCCESS(200, "Success"),
IP_LIMIT(131, "IP addresses cannot be requested"),
SECRETKEY_ERROR(132, "account error"),
ACCOUNT_ERROR(133, "account error"),
DEALER_LIMIT(134, "Not a coin dealer"),
SELL_LIMIT(135, "Can't sell it to yourself"),
SELL_ERROR(136, "can not sell"),
SIGN_ERROR(137, "agent config error"),
USER_NOT_EXIST(138, "not this taala user"),
USER_NOT_REGION(139, "Not in the same district"),
AGENT_USER_NOT_EXIST(140, "agent user not exist"),
SEND_SELF_FAIL(141, "SEND_SELF_FAIL"),
PARAM_ERROR(400, "param error"),
;
private int code;
private String message;
DealerBusiStatus(int code, String message) {
this.code = code;
this.message = message;
}
public int getCode() {
return code;
}
public String getMessage() {
return message;
}
}

View File

@@ -0,0 +1,14 @@
package com.accompany.business.vo.dealer;
import lombok.Data;
import java.util.List;
@Data
public class DealerConfigVO {
private String secretKey;
private List<String> ipWhitelist;
private Long agentUid;
private Integer daySendLimit = 100;
private Integer daySendGoldLimit = 700000;
}

View File

@@ -0,0 +1,32 @@
package com.accompany.business.vo.dealer;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
@Data
public class DealerResponse<T> {
@ApiModelProperty("响应状态码成功200")
private Integer code;
@ApiModelProperty("响应消息")
private String message;
@ApiModelProperty("响应数据")
private T data;
public DealerResponse(DealerBusiStatus dealerBusiStatus) {
this.code = dealerBusiStatus.getCode();
this.message = dealerBusiStatus.getMessage();
}
public DealerResponse(Integer code, String message, T data) {
this.code = code;
this.message = message;
this.data = data;
}
public DealerResponse(Integer code, String message) {
this.code = code;
this.message = message;
}
}

View File

@@ -0,0 +1,14 @@
package com.accompany.business.vo.dealer;
import lombok.Data;
import java.util.List;
@Data
public class DealerResponsePage<T> {
private Long total;
private Integer currentPage;
private Integer pageSize;
private Integer lastPage;
private List<T> rows;
}

View File

@@ -0,0 +1,10 @@
package com.accompany.business.vo.dealer;
import lombok.Data;
@Data
public class DealerSaleRequestVO {
private String secretKey;
private Long toUserId;
private Long coins;
}

View File

@@ -0,0 +1,23 @@
package com.accompany.business.vo.dealer;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
@AllArgsConstructor
@NoArgsConstructor
@Builder
@Data
public class DealerUserInfo {
private Long uid;
private String nickName;
private Long userId;
private String gender;
private String avatar;
private Long toUserId;
private String toNickName;
private Double coins;
private Long createAt;
}

View File

@@ -0,0 +1,228 @@
package com.accompany.business.service.dealer;
import cn.hutool.core.date.DateUtil;
import com.accompany.business.model.UserPurse;
import com.accompany.business.service.purse.DiamondGiveHistoryService;
import com.accompany.business.service.purse.UserPurseService;
import com.accompany.business.service.user.UsersService;
import com.accompany.business.vo.DiamondGiveHistoryVo;
import com.accompany.business.vo.dealer.*;
import com.accompany.common.constant.Constant;
import com.accompany.common.push.MarkdownMessage;
import com.accompany.common.status.BusiStatus;
import com.accompany.common.utils.EnvComponent;
import com.accompany.core.enumeration.PartitionEnum;
import com.accompany.core.exception.ServiceException;
import com.accompany.core.model.Users;
import com.accompany.core.service.SysConfService;
import com.accompany.core.service.message.MessageRobotPushService;
import com.accompany.payment.service.RechargeUserService;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.*;
import static com.accompany.common.constant.Constant.SysConfId.WEB_API_AGENT_CHARGE;
@Slf4j
@Service
public class DealerService {
@Autowired
private UsersService usersService;
@Autowired
private UserPurseService userPurseService;
@Autowired
private RechargeUserService rechargeUserService;
@Autowired
private DiamondGiveHistoryService diamondGiveHistoryService;
@Autowired
private EnvComponent envComponent;
@Autowired
private MessageRobotPushService messageRobotPushService;
@Autowired
private SysConfService sysConfService;
public DealerResponse checkParam(String secretKey, String ipStr, Long userId, DealerConfigVO dealerConfigVO) {
if (StringUtils.isEmpty(secretKey) || StringUtils.isEmpty(ipStr)) {
return new DealerResponse(DealerBusiStatus.PARAM_ERROR);
}
if (dealerConfigVO == null || dealerConfigVO.getAgentUid() == null
|| StringUtils.isEmpty(dealerConfigVO.getSecretKey())) {
return new DealerResponse(DealerBusiStatus.ACCOUNT_ERROR);
}
if (CollectionUtils.isNotEmpty(dealerConfigVO.getIpWhitelist()) && !dealerConfigVO.getIpWhitelist().contains(ipStr)) {
return new DealerResponse(DealerBusiStatus.IP_LIMIT);
}
if (!dealerConfigVO.getSecretKey().equals(secretKey)) {
return new DealerResponse(DealerBusiStatus.SECRETKEY_ERROR);
}
Boolean rechargeUser = rechargeUserService.isRechargeUser(dealerConfigVO.getAgentUid());
if (!rechargeUser) {
return new DealerResponse(DealerBusiStatus.SIGN_ERROR);
}
Users user = usersService.getUsersByUid(dealerConfigVO.getAgentUid());
if (user == null) {
return new DealerResponse(DealerBusiStatus.AGENT_USER_NOT_EXIST);
}
if (userId != null) {
Users usersByErBanNo = usersService.getUserByErbanNo(userId);
if (usersByErBanNo == null) {
return new DealerResponse(DealerBusiStatus.USER_NOT_EXIST);
}
if (user.getUid().equals(usersByErBanNo.getUid())) {
return new DealerResponse(DealerBusiStatus.SEND_SELF_FAIL);
}
if (!user.getPartitionId().equals(usersByErBanNo.getPartitionId())) {
return new DealerResponse(DealerBusiStatus.USER_NOT_REGION);
}
}
return new DealerResponse(DealerBusiStatus.SUCCESS);
}
public void accountInfo(DealerResponse dealerResponse, DealerConfigVO dealerConfigVO) {
DealerAccount dealerAccount = new DealerAccount();
dealerResponse.setData(dealerAccount);
Long totalGoldNum = 0L;
UserPurse userPurse = userPurseService.queryUserPurse(dealerConfigVO.getAgentUid());
if (userPurse != null) {
totalGoldNum += userPurse.getDiamonds().longValue();
}
dealerAccount.setCoinBalance(totalGoldNum);
}
public DealerResponse userInfo(Long erbanNo) {
DealerUserInfo userInfo = new DealerUserInfo();
Users users = usersService.getUserByErbanNo(erbanNo);
userInfo.setUserId(users.getErbanNo());
userInfo.setAvatar(users.getAvatar());
userInfo.setGender(users.getGender() == 1 ? "MALE" : "FEMALE");
userInfo.setNickName(users.getNick());
userInfo.setUid(users.getUid());
DealerResponse dealerResponse = new DealerResponse(DealerBusiStatus.SUCCESS);
dealerResponse.setData(userInfo);
return dealerResponse;
}
public DealerResponse sale(Long toErbanNo, Long coins, DealerConfigVO dealerConfigVO) {
if (coins == null || coins <= 0) {
return new DealerResponse(DealerBusiStatus.PARAM_ERROR);
}
Users toUser = usersService.getUserByErbanNo(toErbanNo);
Long toUid = toUser.getUid();
boolean toAgentUser = rechargeUserService.isRechargeUser(toUid);
if (toAgentUser) {
log.info("DealerService.sale:toErbanNo:{} are agent", toErbanNo);
return new DealerResponse(DealerBusiStatus.SIGN_ERROR);
}
Long agentUid = dealerConfigVO.getAgentUid();
Users usersByUid = usersService.getUsersByUid(agentUid);
try {
diamondGiveHistoryService.webGiveDiamond(agentUid, toUid, coins);
sendRobotMsg(usersByUid.getPartitionId(), usersByUid.getErbanNo(), toUser.getErbanNo(), coins);
} catch (ServiceException e) {
BusiStatus busiStatus = e.getBusiStatus();
log.error("DealerService.sale,error:{}", JSONObject.toJSONString(busiStatus), e);
return new DealerResponse(busiStatus.getCode(), busiStatus.getName());
}
return new DealerResponse(DealerBusiStatus.SUCCESS);
}
private void sendRobotMsg(Integer partitionId, Long agentErbanNo, Long erbanNo, Long coins) {
String key;
if (envComponent.getDevOrNativeEnv()) {
key = "474c92e5-385d-4b6a-b606-b33769f59457";
} else {
key = "37355bec-05c4-4168-8287-51c8fcfd1475";
}
PartitionEnum partitionEnum = PartitionEnum.getByPartitionId(partitionId);
String title = " Eparty-WEB-API代理充值(" + partitionEnum.getDesc() + "区)";
MarkdownMessage msg = new MarkdownMessage();
msg.add(MarkdownMessage.getHeaderText(3, title));
msg.add(MarkdownMessage.getReferenceText("充值用户ID " + erbanNo));
msg.add(MarkdownMessage.getReferenceText("充值使用的代理ID " + agentErbanNo));
msg.add(MarkdownMessage.getReferenceText("充值coins " + coins));
msg.add(MarkdownMessage.getReferenceText("充值时间GMT+8 " + DateUtil.formatDateTime(new Date())));
messageRobotPushService.pushMessageByKey(key, msg, false);
}
public DealerResponse<DealerResponsePage<DealerUserInfo>> list(Integer page, DealerConfigVO dealerConfigVO) {
DealerResponsePage dealerResponsePage = new DealerResponsePage();
DealerResponse dealerResponse = new DealerResponse<>(DealerBusiStatus.SUCCESS);
List<DealerUserInfo> result = new ArrayList<>();
dealerResponse.setData(dealerResponsePage);
dealerResponsePage.setRows(result);
Long agentUid = dealerConfigVO.getAgentUid();
dealerResponsePage.setPageSize(10);
dealerResponsePage.setCurrentPage(page);
IPage<DiamondGiveHistoryVo> giveHistoryVoIPage = diamondGiveHistoryService.listRecordVoByType(Constant.UserGiveType.webDiamond, agentUid,
dealerResponsePage.getCurrentPage(), dealerResponsePage.getPageSize());
dealerResponsePage.setTotal(giveHistoryVoIPage.getTotal());
int lastPage = dealerResponsePage.getTotal().intValue() / 10 + 1;
dealerResponsePage.setLastPage(lastPage);
if (dealerResponsePage.getTotal() <= 0 || page > lastPage) {
return dealerResponse;
}
List<DiamondGiveHistoryVo> records = giveHistoryVoIPage.getRecords();
if (CollectionUtils.isNotEmpty(records)) {
for (DiamondGiveHistoryVo giveHistoryDTO : records) {
Users usersByUid = usersService.getUsersByUid(giveHistoryDTO.getTargetUid());
DealerUserInfo build = DealerUserInfo.builder()
.toUserId(usersByUid.getErbanNo())
.createAt(giveHistoryDTO.getCreateTime().getTime())
.coins(giveHistoryDTO.getRealDiamondNum())
.toNickName(usersByUid.getNick()).build();
result.add(build);
}
}
return dealerResponse;
}
/**
* 获取配置
*
* @param secretKey
* @return
*/
public DealerConfigVO getConfigVo(String secretKey) {
return this.getConfigMap().get(secretKey);
}
private Map<String, DealerConfigVO> getConfigMap() {
Map<String, DealerConfigVO> resultMap = new HashMap<>();
String sysConfValueById = sysConfService.getSysConfValueById(WEB_API_AGENT_CHARGE);
log.info("DealerService.getConfigMap:{}", sysConfValueById);
if (StringUtils.isEmpty(sysConfValueById)) {
return resultMap;
}
List<DealerConfigVO> configVOS = JSONObject.parseArray(sysConfValueById, DealerConfigVO.class);
if (CollectionUtils.isNotEmpty(configVOS)) {
configVOS.forEach(configVO -> {
resultMap.put(configVO.getSecretKey(), configVO);
});
}
return resultMap;
}
public static void main(String[] args) {
DealerConfigVO configVO = new DealerConfigVO();
configVO.setSecretKey("e364f8c9a6c8402ca375594deadcbf52");
configVO.setAgentUid(1222L);
configVO.setIpWhitelist(new ArrayList<>());
List<DealerConfigVO> configVOS = new ArrayList<>();
configVOS.add(configVO);
System.out.println(JSONObject.toJSONString(configVOS));
}
}

View File

@@ -0,0 +1,83 @@
package com.accompany.business.controller.dealer;
import com.accompany.business.service.dealer.DealerService;
import com.accompany.business.vo.dealer.*;
import com.accompany.common.status.BusiStatus;
import com.accompany.common.utils.IPUtils;
import com.google.common.util.concurrent.RateLimiter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import java.util.concurrent.TimeUnit;
@RequestMapping("/dealer")
@RestController
public class DealerController {
@Autowired
private DealerService dealerService;
private final RateLimiter certificationLimit = RateLimiter.create(200);
@GetMapping("/account")
public DealerResponse<DealerAccount> accountInfo(String secretKey, HttpServletRequest request) {
if (!certificationLimit.tryAcquire(5, TimeUnit.SECONDS)) {
return new DealerResponse(BusiStatus.SERVERBUSY.getCode(), BusiStatus.SERVERBUSY.getReasonPhrase());
}
String ipAddress = IPUtils.getRealIpAddress(request);
DealerConfigVO dealerConfigVO = dealerService.getConfigVo(secretKey);
DealerResponse dealerResponse = dealerService.checkParam(secretKey, ipAddress, null, dealerConfigVO);
if (dealerResponse.getCode() != DealerBusiStatus.SUCCESS.getCode()) {
return dealerResponse;
}
dealerService.accountInfo(dealerResponse, dealerConfigVO);
return dealerResponse;
}
@GetMapping("/user-info")
public DealerResponse<DealerUserInfo> userInfo(String secretKey, Long userId, HttpServletRequest request) {
if (!certificationLimit.tryAcquire(5, TimeUnit.SECONDS)) {
return new DealerResponse(BusiStatus.SERVERBUSY.getCode(), BusiStatus.SERVERBUSY.getReasonPhrase());
}
String ipAddress = IPUtils.getRealIpAddress(request);
DealerConfigVO dealerConfigVO = dealerService.getConfigVo(secretKey);
DealerResponse dealerResponse = dealerService.checkParam(secretKey, ipAddress, userId, dealerConfigVO);
if (dealerResponse.getCode() != DealerBusiStatus.SUCCESS.getCode()) {
return dealerResponse;
}
return dealerService.userInfo(userId);
}
@PostMapping("/sale")
public DealerResponse buy(DealerSaleRequestVO dealerSaleRequestVO, HttpServletRequest request) {
if (!certificationLimit.tryAcquire(5, TimeUnit.SECONDS)) {
return new DealerResponse(BusiStatus.SERVERBUSY.getCode(), BusiStatus.SERVERBUSY.getReasonPhrase());
}
String ipAddress = IPUtils.getRealIpAddress(request);
DealerConfigVO dealerConfigVO = dealerService.getConfigVo(dealerSaleRequestVO.getSecretKey());
DealerResponse dealerResponse = dealerService.checkParam(dealerSaleRequestVO.getSecretKey(), ipAddress, dealerSaleRequestVO.getToUserId(), dealerConfigVO);
if (dealerResponse.getCode() != DealerBusiStatus.SUCCESS.getCode()) {
return dealerResponse;
}
return dealerService.sale(dealerSaleRequestVO.getToUserId(), dealerSaleRequestVO.getCoins(), dealerConfigVO);
}
@GetMapping("/list")
public DealerResponse<DealerResponsePage<DealerUserInfo>> list(String secretKey, Integer page, HttpServletRequest request) {
if (!certificationLimit.tryAcquire(5, TimeUnit.SECONDS)) {
return new DealerResponse(BusiStatus.SERVERBUSY.getCode(), BusiStatus.SERVERBUSY.getReasonPhrase());
}
String ipAddress = IPUtils.getRealIpAddress(request);
DealerConfigVO dealerConfigVO = dealerService.getConfigVo(secretKey);
DealerResponse dealerResponse = dealerService.checkParam(secretKey, ipAddress, null, dealerConfigVO);
if (dealerResponse.getCode() != DealerBusiStatus.SUCCESS.getCode()) {
return dealerResponse;
}
return dealerService.list(page, dealerConfigVO);
}
}