支付事务手动提交测试

This commit is contained in:
SongZhang 2024-10-16 17:29:29 +08:00
parent ebdf2f873a
commit ebf1f97fda
2 changed files with 23 additions and 4 deletions

View File

@ -1302,7 +1302,7 @@ public class OrderService {
}
if (orderInfo.getOrderType().equals("return")) {
if (orderInfo.getOrderType() != null && orderInfo.getOrderType().equals("return")) {
List<SkuInfoPo> skuInfoPos = tbOrderInfoMapper.selectSkuByOrderIdAndType(orderInfo.getId().toString());
if (Objects.isNull(skuInfoPos) || skuInfoPos.size() < 0) {
skuInfoPos = new ArrayList<>();

View File

@ -1,6 +1,7 @@
package com.chaozhanggui.system.cashierservice.service;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
@ -37,7 +38,13 @@ import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.DefaultTransactionDefinition;
import org.springframework.transaction.support.TransactionCallback;
import org.springframework.transaction.support.TransactionTemplate;
import org.springframework.web.client.RestTemplate;
import javax.annotation.Resource;
@ -120,9 +127,14 @@ public class PayService {
private MPOrderDetailMapper mPOrderDetailMapper;
@Autowired
private RabbitMsgUtils rabbitMsgUtils;
@Autowired
private TransactionTemplate transactionTemplate;
public PayService(RedisTemplate<String, Object> redisTemplate) {
private final Utils utils;
public PayService(RedisTemplate<String, Object> redisTemplate, Utils utils) {
this.redisTemplate = redisTemplate;
this.utils = utils;
}
public static void main(String[] args) {
@ -568,7 +580,6 @@ public class PayService {
}
TbOrderInfo orderInfo = tbOrderInfoMapper.selectByPrimaryKey(Integer.valueOf(orderId));
if (ObjectUtil.isEmpty(orderInfo)) {
return Result.fail(CodeEnum.ORDERNOEXIST);
@ -672,13 +683,14 @@ public class PayService {
tbOrderDetailMapper.updateStatusByOrderId(Integer.valueOf(orderId), "closed", null);
}
log.info("更新购物车:{}", cartCount);
JSONObject jsonObject = new JSONObject();
jsonObject.put("token", token);
jsonObject.put("type", "create");
jsonObject.put("orderId", orderId);
producer.putOrderCollect(jsonObject.toJSONString());
// 打印消息
@ -1007,6 +1019,13 @@ public class PayService {
orderInfo.setPayOrderNo("cash".concat(SnowFlakeUtil.generateOrderNo()));
tbOrderInfoMapper.updateByPrimaryKeySelective(orderInfo);
ThreadUtil.sleep(10);
// 提交当前事务
transactionTemplate.execute((TransactionCallback<Void>) status -> {
status.flush(); // 提交
return null;
});
//更新购物车状态
int cartCount = tbCashierCartMapper.updateByOrderId(orderId, "final");