|
|
@@ -780,21 +780,74 @@ public class StorePlatformUserRoleServiceImpl extends ServiceImpl<StorePlatformU
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
public boolean updateSubAccountNew(Integer userId, String phone, String accountName, Integer storeId, Long roleId,Integer id) {
|
|
|
- LambdaUpdateWrapper<StorePlatformUserRole> deleteWrapper = new LambdaUpdateWrapper<>();
|
|
|
- deleteWrapper.eq(StorePlatformUserRole::getUserId, userId)
|
|
|
- .eq(StorePlatformUserRole::getId, id)
|
|
|
- .set(StorePlatformUserRole::getDeleteFlag, 1);
|
|
|
- int deleteResult = storePlatformUserRoleMapper.update(null, deleteWrapper);
|
|
|
- if (deleteResult > 0) {
|
|
|
- log.info("成功删除角色关联记录: userId={}, storeId={}, roleId={}", userId, storeId, roleId);
|
|
|
- R<String> A =createAccountAndAssignRole(phone, accountName, storeId, roleId);
|
|
|
- if (A.getCode()==200){
|
|
|
- return true;
|
|
|
- }
|
|
|
+ try {
|
|
|
+ // 先查询要删除的记录,保存信息以便回滚
|
|
|
+ StorePlatformUserRole oldUserRole = storePlatformUserRoleMapper.selectById(id);
|
|
|
+ if (oldUserRole == null) {
|
|
|
+ log.error("要删除的角色关联记录不存在: id={}", id);
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 逻辑删除记录
|
|
|
+ LambdaUpdateWrapper<StorePlatformUserRole> deleteWrapper = new LambdaUpdateWrapper<>();
|
|
|
+ deleteWrapper.eq(StorePlatformUserRole::getUserId, userId)
|
|
|
+ .eq(StorePlatformUserRole::getId, id)
|
|
|
+ .set(StorePlatformUserRole::getDeleteFlag, 1);
|
|
|
+ int deleteResult = storePlatformUserRoleMapper.update(null, deleteWrapper);
|
|
|
+
|
|
|
+ if (deleteResult > 0) {
|
|
|
+ log.info("成功删除角色关联记录: userId={}, storeId={}, roleId={}", userId, storeId, roleId);
|
|
|
+
|
|
|
+ // 尝试创建新账号并分配角色
|
|
|
+ R<String> createResult = createAccountAndAssignRole(phone, accountName, storeId, roleId);
|
|
|
+
|
|
|
+ if (createResult.getCode() == 200) {
|
|
|
+ log.info("成功创建新账号并分配角色: phone={}, storeId={}, roleId={}", phone, storeId, roleId);
|
|
|
+ return true;
|
|
|
+ } else {
|
|
|
+ // 创建失败,执行回滚:恢复删除的记录
|
|
|
+ log.warn("创建新账号失败,执行回滚操作: phone={}, storeId={}, roleId={}, error={}",
|
|
|
+ phone, storeId, roleId, createResult.getMsg());
|
|
|
+
|
|
|
+ LambdaUpdateWrapper<StorePlatformUserRole> rollbackWrapper = new LambdaUpdateWrapper<>();
|
|
|
+ rollbackWrapper.eq(StorePlatformUserRole::getId, id)
|
|
|
+ .set(StorePlatformUserRole::getDeleteFlag, 0);
|
|
|
+ int rollbackResult = storePlatformUserRoleMapper.update(null, rollbackWrapper);
|
|
|
+
|
|
|
+ if (rollbackResult > 0) {
|
|
|
+ log.info("成功回滚,恢复角色关联记录: id={}, userId={}, storeId={}, roleId={}",
|
|
|
+ id, userId, storeId, roleId);
|
|
|
+ } else {
|
|
|
+ log.error("回滚失败,无法恢复角色关联记录: id={}, userId={}, storeId={}, roleId={}",
|
|
|
+ id, userId, storeId, roleId);
|
|
|
+ }
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ log.error("删除角色关联记录失败: userId={}, id={}", userId, id);
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("更新子账号失败,尝试回滚: userId={}, phone={}, accountName={}, storeId={}, roleId={}, id={}",
|
|
|
+ userId, phone, accountName, storeId, roleId, id, e);
|
|
|
+
|
|
|
+ // 异常情况下也尝试回滚
|
|
|
+ try {
|
|
|
+ LambdaUpdateWrapper<StorePlatformUserRole> rollbackWrapper = new LambdaUpdateWrapper<>();
|
|
|
+ rollbackWrapper.eq(StorePlatformUserRole::getId, id)
|
|
|
+ .set(StorePlatformUserRole::getDeleteFlag, 0);
|
|
|
+ int rollbackResult = storePlatformUserRoleMapper.update(null, rollbackWrapper);
|
|
|
+ if (rollbackResult > 0) {
|
|
|
+ log.info("异常情况下成功回滚,恢复角色关联记录: id={}", id);
|
|
|
+ }
|
|
|
+ } catch (Exception rollbackException) {
|
|
|
+ log.error("回滚操作也失败: id={}", id, rollbackException);
|
|
|
+ }
|
|
|
+
|
|
|
+ return false;
|
|
|
}
|
|
|
-
|
|
|
- return false;
|
|
|
}
|
|
|
|
|
|
@Override
|