增加注册bot 编程式事务

This commit is contained in:
623756217 2023-03-13 11:27:14 +08:00
parent 68083c5409
commit 1d510dbc9c
1 changed files with 47 additions and 23 deletions

View File

@ -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<BotLimitEvent> botLimitEvents = botLimitMapper.selectList(Wrappers.<BotLimitEvent>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<Bot>().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> 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<Bot>().eq("bot_name",botVO.getBotName()));
//插入注册表
RegisterBot registerBot = shiftBotToRegisterBot(botVO,insertBot);
registerMapper.insert(registerBot);
log.info("注册Bot结束{}",JSON.toJSONString(shiftBotToBotOutputVo(botVO,insertBot)));
//插入权限表
List<BotLimitEvent> 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<RegisterBot>().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)){