diff --git a/src/main/java/com/gitlink/softbot/service/user/impl/UserService.java b/src/main/java/com/gitlink/softbot/service/user/impl/UserService.java index 759efca..da54fbf 100644 --- a/src/main/java/com/gitlink/softbot/service/user/impl/UserService.java +++ b/src/main/java/com/gitlink/softbot/service/user/impl/UserService.java @@ -24,7 +24,12 @@ import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.dao.DuplicateKeyException; 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 java.util.*; import java.util.stream.Collectors; @@ -49,6 +54,8 @@ public class UserService extends AbstractUserBot implements IUserService { TransferBotMapper transferBotMapper; + @Autowired + private PlatformTransactionManager transactionManager; @Autowired GitLinkApi api; @@ -70,6 +77,11 @@ public class UserService extends AbstractUserBot implements IUserService { throw new BotException("该Bot没有注册!"); } Bot bot = botMapper.selectById(botId); + if(bot.getPrivateKey()==null||bot.getPrivateKey().equals("")){ + Response response = api.activeBotAuth(registerBot.getDeveloperId(), bot.getId(), AuthOperate.AUTH_ACTIVE); + checkException(response); + bot = botMapper.selectById(botId); + } List botLimitEvents = botLimitMapper.selectList(Wrappers.lambdaQuery() .eq(BotLimitEvent::getBotId,botId)); LimitVO limitVO = getLimitFromLimitEvents(botLimitEvents); @@ -103,38 +115,50 @@ public class UserService extends AbstractUserBot implements IUserService { @Override // @Transactional(rollbackFor = Exception.class) public BotOutputVO registerBot(BotInputVO botVO) throws BotException { - log.info("开始注册Bot{}", JSON.toJSONString(botVO)); - //校验必填信息信息 - checkInfo(botVO); + TransactionStatus transactionStatus = null; - //插入Bot表 - botMapper.insert(shiftObject(botVO)); - Bot insertBot = botMapper.selectOne(new QueryWrapper().eq("bot_name",botVO.getBotName())); + DefaultTransactionDefinition transactionDefinition; + Bot insertBot = null; + try { + // 有事务,则使用当前事务;否则开启新事务 + transactionDefinition = new DefaultTransactionDefinition(); + transactionDefinition.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED); + transactionStatus = transactionManager.getTransaction(transactionDefinition); - //插入权限表 - List botLimitEvent = shiftBotToLimit(botVO,insertBot); - botLimitEvent.forEach(botLimitEvent1 -> botLimitMapper.insert(botLimitEvent1)); + log.info("开始注册Bot{}", JSON.toJSONString(botVO)); + //校验必填信息信息 + checkInfo(botVO); + //插入Bot表 + botMapper.insert(shiftObject(botVO)); + insertBot = botMapper.selectOne(new QueryWrapper().eq("bot_name",botVO.getBotName())); - //插入注册表 - RegisterBot registerBot = shiftBotToRegisterBot(botVO,insertBot); - registerMapper.insert(registerBot); - log.info("注册Bot结束{}",JSON.toJSONString(shiftBotToBotOutputVo(botVO,insertBot))); + //插入权限表 + List botLimitEvent = shiftBotToLimit(botVO,insertBot); + botLimitEvent.forEach(botLimitEvent1 -> botLimitMapper.insert(botLimitEvent1)); - Bot bot = botMapper.selectById(registerBot.getBotId()); - log.info("bot:{}",JSON.toJSONString(bot)); + //插入注册表 + RegisterBot registerBot = shiftBotToRegisterBot(botVO,insertBot); + registerMapper.insert(registerBot); + log.info("注册Bot结束{}",JSON.toJSONString(shiftBotToBotOutputVo(botVO,insertBot))); - RegisterBot registerBot1 = registerMapper.selectOne(new QueryWrapper().eq("bot_id",bot.getId())); - log.info("registerBot:{}",JSON.toJSONString(registerBot1)); + transactionManager.commit(transactionStatus); + } catch (Exception e){ + if (transactionStatus != null) { + transactionManager.rollback(transactionStatus); + } + throw new BotException("Bot注册失败:" + e); + } //激活Bot - Response response = api.activeBotAuth(botVO.getUserId(), bot.getId(), AuthOperate.AUTH_ACTIVE); - - //校验异常 - checkException(response); - - log.info("激活信息:{}",response); + if(insertBot!=null){ + Response response = api.activeBotAuth(botVO.getUserId(), insertBot.getId(), AuthOperate.AUTH_ACTIVE); + //校验异常 + checkException(response); + log.info("注册激活信息:{}",response); + } return shiftBotToBotOutputVo(botVO,insertBot); } + private void checkException(Response response) throws BotException{ JSONObject object = JSONObject.parseObject(response.getData().toString()); if (object.get("status")!=null&&!object.get("status").equals(0)){