作者 王勇

完善一下用户的添加

@@ -37,7 +37,6 @@ public class UserInfoController { @@ -37,7 +37,6 @@ public class UserInfoController {
37 return userInfoService.selectUserInfoList(userInfo, pageNum, pageSize); 37 return userInfoService.selectUserInfoList(userInfo, pageNum, pageSize);
38 } 38 }
39 39
40 -  
41 /** 40 /**
42 * 仅,查询用户列表 41 * 仅,查询用户列表
43 */ 42 */
@@ -120,4 +119,31 @@ public class UserInfoController { @@ -120,4 +119,31 @@ public class UserInfoController {
120 return new ResultJson<>("500", "服务器异常,删除用户关系失败"); 119 return new ResultJson<>("500", "服务器异常,删除用户关系失败");
121 } 120 }
122 } 121 }
  122 +
  123 + /**
  124 + * 批量删除用户(MQ服务器 and 数据库)
  125 + */
  126 + @DeleteMapping("/batch_delete")
  127 + public ResultJson batchDeleteUser(@RequestBody List<UserInfo> userInfoList)
  128 + {
  129 + try {
  130 + return userInfoService.batchDeleteUser(userInfoList);
  131 + } catch (IOException | URISyntaxException e) {
  132 + return new ResultJson<>("500", "服务器异常,批量删除用户关系失败");
  133 + }
  134 + }
  135 +
  136 + /**
  137 + * 删除用户关系(MQ服务器 and 数据库)
  138 + */
  139 + @DeleteMapping("/batch_deleteRelation")
  140 + public ResultJson batchDeleteUserRelation(@RequestBody UserInfo userInfo)
  141 + {
  142 + String ids = userInfo.getId();
  143 + try {
  144 + return userInfoService.batchDeleteUserRelation(ids);
  145 + } catch (IOException | URISyntaxException e) {
  146 + return new ResultJson<>("500", "服务器异常,批量删除用户关系,失败!");
  147 + }
  148 + }
123 } 149 }
@@ -36,6 +36,9 @@ public interface UserInfoService { @@ -36,6 +36,9 @@ public interface UserInfoService {
36 * 36 *
37 * @param record the record 37 * @param record the record
38 * @return insert count 38 * @return insert count
  39 + * @throws IOException
  40 + * @throws URISyntaxException
  41 + * @throws TimeoutException
39 */ 42 */
40 ResultJson insertSelective(UserInfo record) throws IOException, URISyntaxException, TimeoutException; 43 ResultJson insertSelective(UserInfo record) throws IOException, URISyntaxException, TimeoutException;
41 44
@@ -113,6 +116,24 @@ public interface UserInfoService { @@ -113,6 +116,24 @@ public interface UserInfoService {
113 * @return 返回结果 116 * @return 返回结果
114 */ 117 */
115 ResultJson deleteUserRelation(UserInfo userInfo) throws IOException, URISyntaxException; 118 ResultJson deleteUserRelation(UserInfo userInfo) throws IOException, URISyntaxException;
  119 +
  120 + /**
  121 + * 批量删除用户信息
  122 + *
  123 + * @param userInfoList 用户信息集合
  124 + * @return 返回结果
  125 + * @throws IOException
  126 + * @throws URISyntaxException
  127 + */
  128 + ResultJson batchDeleteUser(List<UserInfo> userInfoList) throws IOException, URISyntaxException;
  129 +
  130 + /**
  131 + * 批量删除用户关系
  132 + *
  133 + * @param ids
  134 + * @return
  135 + */
  136 + ResultJson batchDeleteUserRelation(String ids) throws IOException, URISyntaxException;
116 } 137 }
117 138
118 139
@@ -7,6 +7,7 @@ import com.sunyo.wlpt.message.bus.service.domain.BusServer; @@ -7,6 +7,7 @@ import com.sunyo.wlpt.message.bus.service.domain.BusServer;
7 import com.sunyo.wlpt.message.bus.service.domain.UserInfo; 7 import com.sunyo.wlpt.message.bus.service.domain.UserInfo;
8 import com.sunyo.wlpt.message.bus.service.domain.VirtualHost; 8 import com.sunyo.wlpt.message.bus.service.domain.VirtualHost;
9 import com.sunyo.wlpt.message.bus.service.mapper.BusQueueMapper; 9 import com.sunyo.wlpt.message.bus.service.mapper.BusQueueMapper;
  10 +import com.sunyo.wlpt.message.bus.service.mapper.BusServerMapper;
10 import com.sunyo.wlpt.message.bus.service.mapper.UserInfoMapper; 11 import com.sunyo.wlpt.message.bus.service.mapper.UserInfoMapper;
11 import com.sunyo.wlpt.message.bus.service.rabbit.utils.ClientUtils; 12 import com.sunyo.wlpt.message.bus.service.rabbit.utils.ClientUtils;
12 import com.sunyo.wlpt.message.bus.service.rabbit.utils.RabbitUtils; 13 import com.sunyo.wlpt.message.bus.service.rabbit.utils.RabbitUtils;
@@ -18,7 +19,6 @@ import com.sunyo.wlpt.message.bus.service.service.VirtualHostService; @@ -18,7 +19,6 @@ import com.sunyo.wlpt.message.bus.service.service.VirtualHostService;
18 import com.sunyo.wlpt.message.bus.service.utils.IdUtils; 19 import com.sunyo.wlpt.message.bus.service.utils.IdUtils;
19 import io.netty.util.internal.StringUtil; 20 import io.netty.util.internal.StringUtil;
20 import lombok.extern.slf4j.Slf4j; 21 import lombok.extern.slf4j.Slf4j;
21 -import org.springframework.beans.factory.annotation.Value;  
22 import org.springframework.boot.autoconfigure.amqp.RabbitProperties; 22 import org.springframework.boot.autoconfigure.amqp.RabbitProperties;
23 import org.springframework.stereotype.Service; 23 import org.springframework.stereotype.Service;
24 import org.springframework.transaction.annotation.Propagation; 24 import org.springframework.transaction.annotation.Propagation;
@@ -55,25 +55,14 @@ public class UserInfoServiceImpl implements UserInfoService { @@ -55,25 +55,14 @@ public class UserInfoServiceImpl implements UserInfoService {
55 private BusServerService busServerService; 55 private BusServerService busServerService;
56 56
57 @Resource 57 @Resource
  58 + private BusServerMapper busServerMapper;
  59 +
  60 + @Resource
58 private VirtualHostService virtualHostService; 61 private VirtualHostService virtualHostService;
59 62
60 @Resource 63 @Resource
61 private BusQueueService busQueueService; 64 private BusQueueService busQueueService;
62 65
63 - @Value("${spring.rabbitmq.host}")  
64 - private String host;  
65 -  
66 - @Value("${spring.rabbitmq.port}")  
67 - private int port;  
68 -  
69 - @Value("${spring.rabbitmq.username}")  
70 - private String rabbitUsername;  
71 -  
72 - @Value("${spring.rabbitmq.password}")  
73 - private String rabbitPassword;  
74 -  
75 - @Value("${spring.rabbitmq.virtual-host}")  
76 - private String vHost;  
77 66
78 @Override 67 @Override
79 public int deleteByPrimaryKey(String id) 68 public int deleteByPrimaryKey(String id)
@@ -104,10 +93,16 @@ public class UserInfoServiceImpl implements UserInfoService { @@ -104,10 +93,16 @@ public class UserInfoServiceImpl implements UserInfoService {
104 @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED) 93 @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
105 public ResultJson insertSelective(UserInfo userInfo) throws IOException, URISyntaxException, TimeoutException 94 public ResultJson insertSelective(UserInfo userInfo) throws IOException, URISyntaxException, TimeoutException
106 { 95 {
  96 + String errorMessage = "";
  97 +
  98 + String username = userInfo.getUsername();
107 String password = userInfo.getPassword(); 99 String password = userInfo.getPassword();
  100 + if (StringUtil.isNullOrEmpty(password) || StringUtil.isNullOrEmpty(username)) {
  101 + return new ResultJson<>("400", "用户名以及密码,不得为空,请务必选择");
  102 + }
108 String virtualHostId = userInfo.getVirtualHostId(); 103 String virtualHostId = userInfo.getVirtualHostId();
109 if (StringUtil.isNullOrEmpty(virtualHostId)) { 104 if (StringUtil.isNullOrEmpty(virtualHostId)) {
110 - return new ResultJson<>("400", "用户关系不能为空,务必选择!"); 105 + return new ResultJson<>("400", "用户关系不能为空,务必选择!");
111 } 106 }
112 107
113 String splitItem = ","; 108 String splitItem = ",";
@@ -117,26 +112,40 @@ public class UserInfoServiceImpl implements UserInfoService { @@ -117,26 +112,40 @@ public class UserInfoServiceImpl implements UserInfoService {
117 for (int i = 0; i < split_hostId.length; i++) { 112 for (int i = 0; i < split_hostId.length; i++) {
118 VirtualHost virtualHost = virtualHostService.selectByPrimaryKey(split_hostId[i]); 113 VirtualHost virtualHost = virtualHostService.selectByPrimaryKey(split_hostId[i]);
119 if (virtualHost != null) { 114 if (virtualHost != null) {
  115 + userInfo.setId(IdUtils.generateId());
120 userInfo.setVirtualHostId(virtualHost.getId()); 116 userInfo.setVirtualHostId(virtualHost.getId());
121 userInfo.setVirtualHostName(virtualHost.getVirtualHostName()); 117 userInfo.setVirtualHostName(virtualHost.getVirtualHostName());
122 -  
123 - userInfo.setVirtualHostName(virtualHost.getVirtualHostName());  
124 BusServer busServer = busServerService.selectByPrimaryKey(virtualHost.getServerId()); 118 BusServer busServer = busServerService.selectByPrimaryKey(virtualHost.getServerId());
125 -  
126 userInfo.setServerId(busServer.getId()); 119 userInfo.setServerId(busServer.getId());
127 userInfo.setServerName(busServer.getServerName()); 120 userInfo.setServerName(busServer.getServerName());
128 121
129 - userInfo.setId(IdUtils.generateId());  
130 - 122 + // 判断用户关系是否存在
  123 + List<UserInfo> userInfoList = userInfoMapper.validateUserInfo(userInfo);
  124 + if (userInfoList.size() > 0) {
  125 + errorMessage = errorMessage + "用户名称:" + userInfo.getUsername() + ",服务器名称:" + userInfo.getServerName()
  126 + + ",虚拟主机名称:" + userInfo.getVirtualHostName() + "已存在;";
  127 + continue;
  128 + }
  129 + // 判断用户的默认队列是否存在
  130 + List<BusQueue> queueList = busQueueMapper.selectByUsernameAndHostId(userInfo.getUsername(), userInfo.getVirtualHostId());
  131 + if (queueList.size() > 0) {
  132 + errorMessage = errorMessage + "用户名称:" + userInfo.getUsername() + ",服务器名称:" + userInfo.getServerName()
  133 + + ",虚拟主机名称:" + userInfo.getVirtualHostName()
  134 + + "-->的默认队列已存在,请联系管理员;" + "\r\n" + "<------->";
  135 + } else {
131 int num = userInfoMapper.insertSelective(userInfo); 136 int num = userInfoMapper.insertSelective(userInfo);
132 ClientUtils.addRabbitUser(userInfo, busServer, password); 137 ClientUtils.addRabbitUser(userInfo, busServer, password);
133 ClientUtils.userRelation(userInfo, busServer); 138 ClientUtils.userRelation(userInfo, busServer);
134 createQueue(userInfo); 139 createQueue(userInfo);
135 index = index + num; 140 index = index + num;
  141 + }
136 } else { 142 } else {
137 log.warn(split_hostId[i] + "->是服务器id"); 143 log.warn(split_hostId[i] + "->是服务器id");
138 } 144 }
139 } 145 }
  146 + if (!StringUtil.isNullOrEmpty(errorMessage)) {
  147 + return new ResultJson<>("201", errorMessage);
  148 + }
140 return index > 0 149 return index > 0
141 ? new ResultJson<>("200", "添加用户信息,成功") 150 ? new ResultJson<>("200", "添加用户信息,成功")
142 : new ResultJson<>("500", "添加用户信息,失败"); 151 : new ResultJson<>("500", "添加用户信息,失败");
@@ -155,10 +164,24 @@ public class UserInfoServiceImpl implements UserInfoService { @@ -155,10 +164,24 @@ public class UserInfoServiceImpl implements UserInfoService {
155 164
156 userInfo.setId(IdUtils.generateId()); 165 userInfo.setId(IdUtils.generateId());
157 166
  167 + // 判断用户关系是否存在
  168 + List<UserInfo> userInfoList = userInfoMapper.validateUserInfo(userInfo);
  169 + if (userInfoList.size() > 0) {
  170 + errorMessage = errorMessage + "用户名称:" + userInfo.getUsername() + ",服务器名称:" + userInfo.getServerName()
  171 + + ",虚拟主机名称:" + userInfo.getVirtualHostName() + "已存在;";
  172 + } else {
  173 + // 判断其默认队列是否已经存在
  174 + List<BusQueue> queueList = busQueueMapper.selectByUsernameAndHostId(userInfo.getUsername(), userInfo.getVirtualHostId());
  175 + if (queueList.size() > 0) {
  176 + errorMessage = errorMessage + "用户名称" + userInfo.getUsername() + ",服务器名称:" + userInfo.getServerName()
  177 + + ",虚拟主机名称:" + userInfo.getVirtualHostName() + "-->的默认队列已存在,请联系管理员";
  178 + } else {
158 num = userInfoMapper.insertSelective(userInfo); 179 num = userInfoMapper.insertSelective(userInfo);
159 ClientUtils.addRabbitUser(userInfo, busServer, password); 180 ClientUtils.addRabbitUser(userInfo, busServer, password);
160 ClientUtils.userRelation(userInfo, busServer); 181 ClientUtils.userRelation(userInfo, busServer);
161 createQueue(userInfo); 182 createQueue(userInfo);
  183 + }
  184 + }
162 } else { 185 } else {
163 log.error(userInfo.getVirtualHostId() + "->并不是虚拟主机id;故添加失败"); 186 log.error(userInfo.getVirtualHostId() + "->并不是虚拟主机id;故添加失败");
164 } 187 }
@@ -298,8 +321,6 @@ public class UserInfoServiceImpl implements UserInfoService { @@ -298,8 +321,6 @@ public class UserInfoServiceImpl implements UserInfoService {
298 { 321 {
299 String username = userInfo.getUsername(); 322 String username = userInfo.getUsername();
300 String serverName = userInfo.getServerName(); 323 String serverName = userInfo.getServerName();
301 - String virtualHostId = userInfo.getVirtualHostId();  
302 - String splitItem = ",";  
303 324
304 List<UserInfo> userInfoList = userInfoMapper.selectByUsernameAndServerName(username, serverName); 325 List<UserInfo> userInfoList = userInfoMapper.selectByUsernameAndServerName(username, serverName);
305 int num = userInfoMapper.deleteByUsernameAndServerName(username, serverName); 326 int num = userInfoMapper.deleteByUsernameAndServerName(username, serverName);
@@ -311,20 +332,22 @@ public class UserInfoServiceImpl implements UserInfoService { @@ -311,20 +332,22 @@ public class UserInfoServiceImpl implements UserInfoService {
311 /** 332 /**
312 * TODO: 删除该用户对应的队列 333 * TODO: 删除该用户对应的队列
313 */ 334 */
314 - if (virtualHostId.contains(splitItem)) {  
315 - String[] splitHostId = virtualHostId.split(splitItem);  
316 - for (int i = 0; i < splitHostId.length; i++) {  
317 - List<BusQueue> queueList = busQueueMapper.selectByUsernameAndHostId(username, splitHostId[i]);  
318 - for (BusQueue queue : queueList) {  
319 - busQueueService.deleteByPrimaryKey(queue.getId());  
320 - }  
321 - }  
322 - } else {  
323 - List<BusQueue> queueList = busQueueMapper.selectByUsernameAndHostId(username, virtualHostId);  
324 - for (BusQueue queue : queueList) {  
325 - busQueueService.deleteByPrimaryKey(queue.getId());  
326 - }  
327 - } 335 +// String virtualHostId = userInfo.getVirtualHostId();
  336 +// String splitItem = ",";
  337 +// if (virtualHostId.contains(splitItem)) {
  338 +// String[] splitHostId = virtualHostId.split(splitItem);
  339 +// for (int i = 0; i < splitHostId.length; i++) {
  340 +// List<BusQueue> queueList = busQueueMapper.selectByUsernameAndHostId(username, splitHostId[i]);
  341 +// for (BusQueue queue : queueList) {
  342 +// busQueueService.deleteByPrimaryKey(queue.getId());
  343 +// }
  344 +// }
  345 +// } else {
  346 +// List<BusQueue> queueList = busQueueMapper.selectByUsernameAndHostId(username, virtualHostId);
  347 +// for (BusQueue queue : queueList) {
  348 +// busQueueService.deleteByPrimaryKey(queue.getId());
  349 +// }
  350 +// }
328 return num > 0 351 return num > 0
329 ? new ResultJson<>("200", "删除用户信息,成功!") 352 ? new ResultJson<>("200", "删除用户信息,成功!")
330 : new ResultJson<>("500", "删除用户信息,失败!"); 353 : new ResultJson<>("500", "删除用户信息,失败!");
@@ -360,6 +383,77 @@ public class UserInfoServiceImpl implements UserInfoService { @@ -360,6 +383,77 @@ public class UserInfoServiceImpl implements UserInfoService {
360 ? new ResultJson<>("200", "删除用户关系,成功!") 383 ? new ResultJson<>("200", "删除用户关系,成功!")
361 : new ResultJson<>("500", "删除用户关系,失败!"); 384 : new ResultJson<>("500", "删除用户关系,失败!");
362 } 385 }
  386 +
  387 + /**
  388 + * 批量删除用户(不删除用户对应的队列)
  389 + *
  390 + * @param userInfoList 用户信息集合
  391 + * @return
  392 + */
  393 + @Override
  394 + @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
  395 + public ResultJson batchDeleteUser(List<UserInfo> userInfoList) throws IOException, URISyntaxException
  396 + {
  397 + int index = 0;
  398 + int num = 0;
  399 + for (UserInfo userInfo : userInfoList) {
  400 + String serverName = userInfo.getServerName();
  401 + String username = userInfo.getUsername();
  402 + if (StringUtil.isNullOrEmpty(serverName) || StringUtil.isNullOrEmpty(username)) {
  403 + continue;
  404 + }
  405 + BusServer busServer = busServerMapper.selectByServerName(serverName);
  406 + ClientUtils.deleteMQUser(username, busServer);
  407 + num = userInfoMapper.deleteByUsernameAndServerName(username, serverName);
  408 + if (num > 0) {
  409 + index = index + num;
  410 + }
  411 + }
  412 + return index > 0
  413 + ? new ResultJson<>("200", "批量删除用户,成功!")
  414 + : new ResultJson<>("500", "批量删除用户,失败!");
  415 + }
  416 +
  417 + /**
  418 + * 批量删除用户关系
  419 + *
  420 + * @param ids id集合
  421 + * @return
  422 + */
  423 + @Override
  424 + public ResultJson batchDeleteUserRelation(String ids) throws IOException, URISyntaxException
  425 + {
  426 + int index = 0;
  427 + String splitItem = ",";
  428 + if (ids.contains(splitItem)) {
  429 + String[] split = ids.split(splitItem);
  430 + for (String id : split) {
  431 + UserInfo userInfo = userInfoMapper.selectByPrimaryKey(id);
  432 + String username = userInfo.getUsername();
  433 + String serverName = userInfo.getServerName();
  434 + String virtualHostName = userInfo.getVirtualHostName();
  435 + BusServer busServer = busServerMapper.selectByServerName(serverName);
  436 + int num = userInfoMapper.deleteByPrimaryKey(id);
  437 + ClientUtils.clearPermissions(busServer, virtualHostName, username);
  438 + if (num > 0) {
  439 + index = index + num;
  440 + }
  441 + }
  442 + return split.length == index
  443 + ? new ResultJson<>("200", "批量删除用户关系,成功!")
  444 + : new ResultJson<>("500", "批量删除用户关系,失败!");
  445 + }
  446 + UserInfo userInfo = userInfoMapper.selectByPrimaryKey(ids);
  447 + String username = userInfo.getUsername();
  448 + String serverName = userInfo.getServerName();
  449 + String virtualHostName = userInfo.getVirtualHostName();
  450 + BusServer busServer = busServerMapper.selectByServerName(serverName);
  451 + int num = userInfoMapper.deleteByPrimaryKey(ids);
  452 + ClientUtils.clearPermissions(busServer, virtualHostName, username);
  453 + return num > 0
  454 + ? new ResultJson<>("200", "批量删除用户关系,成功!")
  455 + : new ResultJson<>("500", "批量删除用户关系,失败!");
  456 + }
363 } 457 }
364 458
365 459