JAVA全系列 教程
3762个小节阅读:7095.1k
目录
C语言快速入门
JAVA全系列 教程
面向对象的程序设计语言
Python全系列 教程
Python3.x版本,未来主流的版本
人工智能 教程
顺势而为,AI创新未来
大厂算法 教程
算法,程序员自我提升必经之路
C++ 教程
一门通用计算机编程语言
微服务 教程
目前业界流行的框架组合
web前端全系列 教程
通向WEB技术世界的钥匙
大数据全系列 教程
站在云端操控万千数据
AIGC全能工具班
A A
White Night
xxxxxxxxxx
@Slf4j
@Component
@RocketMQMessageListener(consumerGroup = "consumer_group_account", topic = "topic_nofitymsg")
public class NotifyMsgAccountListener implements RocketMQListener<String> {
@Autowired
private IAccountInfoService accountInfoService;
@Override
public void onMessage(String message) {
log.info("账户微服务收到RocketMQ的消息:{}", JSONObject.toJSONString(message));
//如果是充值成功,则修改账户余额
PayInfo payInfo = JSON.parseObject(message, PayInfo.class);
if("success".equals(payInfo.getPayResult())){
accountInfoService.updateAccountBalance(payInfo);
}
log.info("更新账户余额完毕:{}", JSONObject.toJSONString(payInfo));
}
}
xxxxxxxxxx
/**
* 更新账户余额
*/
void updateAccountBalance(PayInfo payInfo);
xxxxxxxxxx
@Slf4j
@Service
public class AccountInfoServiceImpl extends ServiceImpl<AccountInfoMapper, AccountInfo> implements IAccountInfoService {
@Resource
private AccountInfoMapper accountInfoMapper;
@Resource
private PayInfoMapper payInfoMapper;
/**
*
* @param payInfo
*/
@Transactional(rollbackFor = Exception.class)
@Override
public void updateAccountBalance(PayInfo payInfo) {
if(payInfoMapper.selectById(payInfo.getTxNo()) != null){
log.info("账户微服务已经处理过当前事务...");
return;
}
LambdaUpdateWrapper<AccountInfo> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
lambdaUpdateWrapper.eq(AccountInfo::getAccountNo,payInfo.getAccountNo());
//更新账户余额
List<AccountInfo> accountInfos = baseMapper.selectList(lambdaUpdateWrapper);
if (accountInfos != null && !accountInfos.isEmpty()){
AccountInfo accountInfo = accountInfos.get(0);
accountInfo.setAccountBalance(accountInfo.getAccountBalance().add(payInfo.getPayAmount()));
accountInfoMapper.updateById(accountInfo);
}
//保存充值记录
payInfoMapper.insert(payInfo);
}
}