|
@@ -4,14 +4,28 @@ import com.webchat.common.constants.RedPacketConstants;
|
|
|
import com.webchat.common.enums.AccountRelationTypeEnum;
|
|
|
import com.webchat.common.enums.RedisKeyEnum;
|
|
|
import com.webchat.common.enums.RoleCodeEnum;
|
|
|
+import com.webchat.common.enums.payment.PaymentTransEventEnum;
|
|
|
+import com.webchat.common.enums.payment.PaymentTransTypeEnum;
|
|
|
import com.webchat.common.exception.BusinessException;
|
|
|
import com.webchat.common.service.RedisService;
|
|
|
+import com.webchat.common.util.ThreadPoolExecutorUtil;
|
|
|
+import com.webchat.domain.dto.payment.PaymentTransRequestDTO;
|
|
|
+import com.webchat.domain.vo.request.SendRedPacketRequestVO;
|
|
|
import com.webchat.domain.vo.response.RedPacketBaseVO;
|
|
|
+import com.webchat.domain.vo.response.RedPacketDetailVO;
|
|
|
import com.webchat.domain.vo.response.UserBaseResponseInfoVO;
|
|
|
+import com.webchat.ugc.repository.dao.IRedPacketRecordDAO;
|
|
|
+import com.webchat.ugc.repository.entity.RedPacketRecordEntity;
|
|
|
import com.webchat.ugc.service.AccountService;
|
|
|
+import com.webchat.ugc.service.PaymentService;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
import org.springframework.util.Assert;
|
|
|
|
|
|
+import java.math.BigDecimal;
|
|
|
+import java.math.RoundingMode;
|
|
|
+import java.util.Date;
|
|
|
+
|
|
|
|
|
|
public abstract class AbstractOpenRedPacketService implements RedPacketOpenInter {
|
|
|
|
|
@@ -21,6 +35,10 @@ public abstract class AbstractOpenRedPacketService implements RedPacketOpenInter
|
|
|
private RedPacketService redPacketService;
|
|
|
@Autowired
|
|
|
private AccountService accountService;
|
|
|
+ @Autowired
|
|
|
+ private PaymentService paymentService;
|
|
|
+ @Autowired
|
|
|
+ private IRedPacketRecordDAO redPacketRecordDAO;
|
|
|
|
|
|
/**
|
|
|
* 实际红包拆分(保证并发数据安全)
|
|
@@ -83,10 +101,11 @@ public abstract class AbstractOpenRedPacketService implements RedPacketOpenInter
|
|
|
Assert.isTrue(!isOpen, "重复拆分");
|
|
|
}
|
|
|
|
|
|
+ @Transactional
|
|
|
@Override
|
|
|
public int open(Long redPacketId, String userId) {
|
|
|
|
|
|
- RedPacketBaseVO redPacket = redPacketService.getRedPacket(redPacketId);
|
|
|
+ RedPacketDetailVO redPacket = redPacketService.getRedPacket(redPacketId);
|
|
|
/**
|
|
|
* 1. 校验红包拆分权限
|
|
|
* 包含:红包有效、拆包权限 ……
|
|
@@ -97,15 +116,26 @@ public abstract class AbstractOpenRedPacketService implements RedPacketOpenInter
|
|
|
* 2. 获得拆包资格,拆红包
|
|
|
*/
|
|
|
int amount = this.openRedPacket(redPacket, userId);
|
|
|
-
|
|
|
+ BigDecimal openYun = new BigDecimal(amount).movePointLeft(2).setScale(2, RoundingMode.HALF_UP);
|
|
|
/**
|
|
|
- * 3. 调用支付平台(webchat-pay 微服务,完成用户拆包金额入账)
|
|
|
+ * 3. 业务侧持久化拆分数据
|
|
|
*/
|
|
|
-
|
|
|
+ RedPacketRecordEntity redPacketRecord = new RedPacketRecordEntity();
|
|
|
+ redPacketRecord.setRedPacketId(redPacketId);
|
|
|
+ redPacketRecord.setUserId(userId);
|
|
|
+ redPacketRecord.setMoney(openYun);
|
|
|
+ redPacketRecord.setCreateDate(new Date());
|
|
|
+ redPacketRecordDAO.save(redPacketRecord);
|
|
|
/**
|
|
|
- * 4. 业务侧持久化拆分数据
|
|
|
+ * 4. 调用支付平台(webchat-pay 微服务,完成用户拆包金额入账)
|
|
|
*/
|
|
|
-
|
|
|
+ String orderId = redPacket.getOrderId();
|
|
|
+ PaymentTransRequestDTO paymentTransRequest = this.buildPaymentTransRequestDTO(orderId, userId, openYun);
|
|
|
+ boolean transResult = paymentService.doTrans(paymentTransRequest);
|
|
|
+ if (!transResult) {
|
|
|
+ // TODO 回滚本次红包拆分消耗数据(回滚Redis缓存数据)
|
|
|
+ throw new BusinessException("服务繁忙");
|
|
|
+ }
|
|
|
return amount;
|
|
|
}
|
|
|
|
|
@@ -146,4 +176,15 @@ public abstract class AbstractOpenRedPacketService implements RedPacketOpenInter
|
|
|
|
|
|
redPacketService.updateRedPacketStatus(redPacketId, status);
|
|
|
}
|
|
|
+
|
|
|
+ private PaymentTransRequestDTO buildPaymentTransRequestDTO(String orderId, String userId, BigDecimal openYun) {
|
|
|
+ PaymentTransRequestDTO dto = new PaymentTransRequestDTO();
|
|
|
+ dto.setOrderId(orderId);
|
|
|
+ dto.setAmount(openYun);
|
|
|
+ dto.setTransType(PaymentTransTypeEnum.INCOME.getTransType());
|
|
|
+ dto.setTransDetail("拆分红包");
|
|
|
+ dto.setTransEvent(PaymentTransEventEnum.RED_PACKET.getTransEvent());
|
|
|
+ dto.setSourceUserId(userId);
|
|
|
+ return dto;
|
|
|
+ }
|
|
|
}
|