|
@@ -5,19 +5,23 @@ import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
import com.zanxiang.common.enums.OrderStateEnum;
|
|
|
import com.zanxiang.common.exception.BaseException;
|
|
|
import com.zanxiang.common.utils.bean.BeanUtils;
|
|
|
-import com.zanxiang.mybatis.entity.Order;
|
|
|
+import com.zanxiang.mybatis.entity.*;
|
|
|
import com.zanxiang.mybatis.mapper.OrderMapper;
|
|
|
import com.zanxiang.sdk.domain.bo.PlatformOrderBO;
|
|
|
import com.zanxiang.sdk.domain.dto.PlatformOrderDTO;
|
|
|
+import com.zanxiang.sdk.domain.params.ProductPayParam;
|
|
|
+import com.zanxiang.sdk.domain.params.UserData;
|
|
|
import com.zanxiang.sdk.listener.OrderPaySuccessEvent;
|
|
|
-import com.zanxiang.sdk.service.OrderService;
|
|
|
+import com.zanxiang.sdk.service.*;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.apache.commons.lang3.RandomStringUtils;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.context.ApplicationContext;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
|
import java.time.LocalDateTime;
|
|
|
+import java.time.format.DateTimeFormatter;
|
|
|
|
|
|
/**
|
|
|
* 平台订单逻辑开发
|
|
@@ -30,20 +34,128 @@ import java.time.LocalDateTime;
|
|
|
@Transactional(rollbackFor = {Exception.class, RuntimeException.class})
|
|
|
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {
|
|
|
|
|
|
+ @Autowired
|
|
|
+ private PromoChannelService promoChannelService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private GameService gameService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private UserService userService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private GameUserService gameUserService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private GameUserRoleService gameUserRoleService;
|
|
|
+
|
|
|
@Autowired
|
|
|
private ApplicationContext applicationContext;
|
|
|
|
|
|
+ @Autowired
|
|
|
+ private GamePayWayService gamePayWayService;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 创建订单
|
|
|
+ *
|
|
|
+ * @param payParam 支付参数
|
|
|
+ * @param userData 用户数据
|
|
|
+ * @return {@link Boolean}
|
|
|
+ */
|
|
|
@Override
|
|
|
- @Transactional(rollbackFor = Exception.class)
|
|
|
- public String create(PlatformOrderBO platformOrderBO) {
|
|
|
- try {
|
|
|
- log.info("生成订单请求参数 platformOrderBO:{}", platformOrderBO);
|
|
|
- Order data = BeanUtils.copy(platformOrderBO, Order.class);
|
|
|
- super.save(data);
|
|
|
- } catch (Exception e) {
|
|
|
- log.error("生成订单失败 platformOrderBO:{}, error:{}", platformOrderBO, e);
|
|
|
+ public Boolean createOrder(ProductPayParam payParam, UserData userData) {
|
|
|
+ //用户id
|
|
|
+ Long userId = userData.getUserId();
|
|
|
+ //游戏id
|
|
|
+ Long gameId = userData.getGameId();
|
|
|
+ //游戏信息
|
|
|
+ Game game = gameService.getById(userData.getGameId());
|
|
|
+ if (game == null) {
|
|
|
+ throw new BaseException("参数错误, 游戏信息不存在");
|
|
|
+ }
|
|
|
+ //用户信息
|
|
|
+ User user = userService.getById(userId);
|
|
|
+ if (user == null) {
|
|
|
+ throw new BaseException("参数错误, 用户信息不存在");
|
|
|
+ }
|
|
|
+ //玩家信息
|
|
|
+ GameUser gameUser = gameUserService.getOne(new LambdaQueryWrapper<GameUser>()
|
|
|
+ .eq(GameUser::getUserId, userData.getUserId()));
|
|
|
+ if (gameUser == null) {
|
|
|
+ throw new BaseException("参数错误, 玩家信息不存在");
|
|
|
+ }
|
|
|
+ //角色信息
|
|
|
+ GameUserRole gameUserRole = gameUserRoleService.getOne(new LambdaQueryWrapper<GameUserRole>()
|
|
|
+ .eq(GameUserRole::getGameId, userData.getGameId())
|
|
|
+ .eq(GameUserRole::getRoleId, payParam.getRoleId()));
|
|
|
+ if (gameUserRole == null) {
|
|
|
+ throw new BaseException("参数错误, 角色信息不存在");
|
|
|
+ }
|
|
|
+ //游戏支付配置信息
|
|
|
+ GamePayWay gamePayWay = gamePayWayService.getPayWayToOrderPay(String.valueOf(gameId), payParam.getPayWay());
|
|
|
+ //生成订单id
|
|
|
+ String orderNum = this.getOrderNum(userData.getUserId());
|
|
|
+ //用户已有订单数
|
|
|
+ int count = super.count(new LambdaQueryWrapper<Order>().eq(Order::getGameId, gameId).eq(Order::getUserId, user));
|
|
|
+ //设置订单号
|
|
|
+ payParam.setOrderId(orderNum);
|
|
|
+ //构造订单
|
|
|
+ return super.save(Order.builder()
|
|
|
+ .orderId(orderNum)
|
|
|
+ .agentId(promoChannelService.getAgentIdByChannel(userData.getChannel()))
|
|
|
+ .cpId(game.getCpId())
|
|
|
+ .cpOrderId(payParam.getOrderId())
|
|
|
+ .userId(userData.getUserId())
|
|
|
+ .mgUserId(gameUser.getId())
|
|
|
+ .roleId(gameUserRole.getRoleId())
|
|
|
+ .roleName(gameUserRole.getRoleName())
|
|
|
+ .serverId(gameUserRole.getServerId())
|
|
|
+ .serverName(gameUserRole.getServerName())
|
|
|
+ .gameId(userData.getGameId())
|
|
|
+ .amount(payParam.getAmount())
|
|
|
+ .productId(payParam.getProductId())
|
|
|
+ .productName(payParam.getProductName())
|
|
|
+ .gamePaywayId(gamePayWay.getId())
|
|
|
+ .ext(payParam.getExtension())
|
|
|
+ .isFirstRecharge(count <= 0 ? 0 : 1)
|
|
|
+ .payDevice(payParam.getPayDevice())
|
|
|
+ .createTime(LocalDateTime.now())
|
|
|
+ .updateTime(LocalDateTime.now())
|
|
|
+ .username(user.getUsername())
|
|
|
+ .regTime(user.getCreateTime())
|
|
|
+ .deviceSystem(userData.getDeviceSystem())
|
|
|
+ .merchantNo(gamePayWay.getMerchantNo())
|
|
|
+ .merchantName(gamePayWay.getMerchantName())
|
|
|
+ .payWayId(gamePayWay.getPayWayId())
|
|
|
+ .regGameId(user.getGameId())
|
|
|
+ .roleLevel(gameUserRole.getRoleLevel())
|
|
|
+ .roleVipLevel(gameUserRole.getRoleVipLevel())
|
|
|
+ .build());
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 生成订单号(25位):时间(精确到毫秒)+ 3位随机数 + 5位户id
|
|
|
+ *
|
|
|
+ * @param userId 用户id
|
|
|
+ * @return {@link String}
|
|
|
+ */
|
|
|
+ private String getOrderNum(Long userId) {
|
|
|
+ //时间(精确到毫秒)
|
|
|
+ DateTimeFormatter ofPattern = DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS");
|
|
|
+ String localDate = LocalDateTime.now().format(ofPattern);
|
|
|
+ //3位随机数
|
|
|
+ String randomNumeric = RandomStringUtils.randomNumeric(3);
|
|
|
+ //5位用户id
|
|
|
+ int subStrLength = 5;
|
|
|
+ String sUserId = userId.toString();
|
|
|
+ int length = sUserId.length();
|
|
|
+ String str;
|
|
|
+ if (length >= subStrLength) {
|
|
|
+ str = sUserId.substring(length - subStrLength, length);
|
|
|
+ } else {
|
|
|
+ str = String.format("%0" + subStrLength + "d", userId);
|
|
|
}
|
|
|
- return null;
|
|
|
+ return localDate + randomNumeric + str;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
@@ -51,7 +163,7 @@ public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements
|
|
|
public Boolean pay(PlatformOrderBO platformOrderBO) {
|
|
|
log.info("订单支付请求参数 platformOrderBO:{}", platformOrderBO);
|
|
|
try {
|
|
|
- Order order = getOne(new LambdaQueryWrapper<Order>().eq(Order::getId, platformOrderBO.getId()));
|
|
|
+ Order order = getOne(new LambdaQueryWrapper<Order>().eq(Order::getOrderId, platformOrderBO.getOrderId()));
|
|
|
//已支付情况,直接返回成功
|
|
|
if (order.getStatus().equals(OrderStateEnum.SUCCESS.getCode())) {
|
|
|
return true;
|
|
@@ -63,7 +175,7 @@ public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements
|
|
|
log.info("订单支付提交数据 data:{}", platformOrderBO);
|
|
|
boolean result = updateById(BeanUtils.copy(platformOrderBO, Order.class));
|
|
|
if (result) {
|
|
|
- OrderPaySuccessEvent orderPaySuccessEvent = new OrderPaySuccessEvent(this, order.getId());
|
|
|
+ OrderPaySuccessEvent orderPaySuccessEvent = new OrderPaySuccessEvent(this, order.getOrderId());
|
|
|
applicationContext.publishEvent(orderPaySuccessEvent);
|
|
|
return true;
|
|
|
}
|
|
@@ -75,8 +187,8 @@ public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public PlatformOrderDTO info(String id) {
|
|
|
- Order order = getOne(new LambdaQueryWrapper<Order>().eq(Order::getId, id));
|
|
|
+ public PlatformOrderDTO info(String orderId) {
|
|
|
+ Order order = getOne(new LambdaQueryWrapper<Order>().eq(Order::getOrderId, orderId));
|
|
|
return BeanUtils.copy(order, PlatformOrderDTO.class);
|
|
|
}
|
|
|
}
|