|
|
@ -2,6 +2,7 @@ package com.acupuncture.web.controller.web; |
|
|
|
|
|
|
|
import cn.hutool.core.bean.BeanUtil; |
|
|
|
import cn.hutool.core.collection.CollectionUtil; |
|
|
|
import cn.hutool.core.util.StrUtil; |
|
|
|
import com.acupuncture.common.core.domain.BaseDto; |
|
|
|
import com.acupuncture.common.core.domain.JsonResponse; |
|
|
|
import com.acupuncture.common.core.domain.entity.SysUser; |
|
|
@ -68,15 +69,28 @@ public class AdminDmsUserController { |
|
|
|
@ApiOperation("添加租户用户") |
|
|
|
@PostMapping("/add") |
|
|
|
public JsonResponse<Integer> insert(@RequestBody @Validated AdminTenantUserDto.AddDto dto) { |
|
|
|
//新增分库数据
|
|
|
|
//切换到分库数据源
|
|
|
|
//判定租户及数据源是否存在
|
|
|
|
DmsTenant dmsTenant = dmsTenantMapper.selectByPrimaryKey(dto.getTenantId()); |
|
|
|
if (dmsTenant != null) { |
|
|
|
if (dmsTenant == null) { |
|
|
|
return JsonResponse.ok().fail("租户不存在"); |
|
|
|
} |
|
|
|
UmsDataSource umsDataSource = umsDataSourceMapper.selectByPrimaryKey(dmsTenant.getDataSourceId()); |
|
|
|
if (umsDataSource != null) { |
|
|
|
changeDataSource(umsDataSource.getDataSourceKey()); |
|
|
|
if (umsDataSource == null || StrUtil.isEmpty(umsDataSource.getDataSourceKey())) { |
|
|
|
return JsonResponse.ok().fail("数据源不存在"); |
|
|
|
} |
|
|
|
|
|
|
|
//新增主库租户用户
|
|
|
|
if (!dmsLoginService.checkUserNameUnique(dto)) { |
|
|
|
return JsonResponse.ok().fail("新增用户'" + dto.getUserName() + "'失败,登录账号已存在"); |
|
|
|
} else if (StringUtils.isNotEmpty(dto.getPhonenumber()) && !dmsLoginService.checkPhoneUnique(dto)) { |
|
|
|
return JsonResponse.ok().fail("新增用户'" + dto.getUserName() + "'失败,手机号码已存在"); |
|
|
|
} |
|
|
|
dto.setPassword(SecurityUtils.encryptPassword(dto.getPassword())); |
|
|
|
adminTenantUserService.insert(dto); |
|
|
|
|
|
|
|
//切换至从库
|
|
|
|
DynamicDataSourceContextHolder.setDataSourceType(umsDataSource.getDataSourceKey()); |
|
|
|
//新增从库数据
|
|
|
|
SysUser user = BeanUtil.copyProperties(dto, SysUser.class); |
|
|
|
if (!userService.checkUserNameUnique(user)) { |
|
|
|
return JsonResponse.ok().fail("新增用户'" + user.getUserName() + "'失败,登录账号已存在"); |
|
|
@ -90,17 +104,10 @@ public class AdminDmsUserController { |
|
|
|
user.setPhonenumber(dto.getContactPhone()); |
|
|
|
user.setPassword(SecurityUtils.encryptPassword(user.getPassword())); |
|
|
|
int insert = userService.insertUser(user); |
|
|
|
//新增主库
|
|
|
|
if (insert > 0) { |
|
|
|
DynamicDataSourceContextHolder.setDataSourceType("MASTER"); |
|
|
|
if (!dmsLoginService.checkUserNameUnique(dto)) { |
|
|
|
return JsonResponse.ok().fail("新增用户'" + dto.getUserName() + "'失败,登录账号已存在"); |
|
|
|
} else if (StringUtils.isNotEmpty(dto.getPhonenumber()) && !dmsLoginService.checkPhoneUnique(dto)) { |
|
|
|
return JsonResponse.ok().fail("新增用户'" + dto.getUserName() + "'失败,手机号码已存在"); |
|
|
|
} |
|
|
|
dto.setPassword(user.getPassword()); |
|
|
|
adminTenantUserService.insert(dto); |
|
|
|
} |
|
|
|
|
|
|
|
//销毁切换数据源
|
|
|
|
DynamicDataSourceContextHolder.clearDataSourceType(); |
|
|
|
|
|
|
|
return JsonResponse.ok(insert); |
|
|
|
} |
|
|
|
|
|
|
@ -124,11 +131,13 @@ public class AdminDmsUserController { |
|
|
|
if (CollectionUtil.isNotEmpty(tenantList)) { |
|
|
|
for (AdminTenantsVo.Result result : tenantList) { |
|
|
|
//切换数据源 删除分库数据
|
|
|
|
changeDataSource(result.getDataSourceKey()); |
|
|
|
DynamicDataSourceContextHolder.setDataSourceType(result.getDataSourceKey()); |
|
|
|
//修改分库用户
|
|
|
|
SysUser sysUser = BeanUtil.copyProperties(dto, SysUser.class); |
|
|
|
sysUser.setPhonenumber(dto.getContactPhone()); |
|
|
|
userService.updateUser(sysUser); |
|
|
|
//销毁切换数据源
|
|
|
|
DynamicDataSourceContextHolder.clearDataSourceType(); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@ -154,7 +163,7 @@ public class AdminDmsUserController { |
|
|
|
if (CollectionUtil.isNotEmpty(tenantList)) { |
|
|
|
for (AdminTenantsVo.Result result : tenantList) { |
|
|
|
//切换数据源 删除分库数据
|
|
|
|
changeDataSource(result.getDataSourceKey()); |
|
|
|
DynamicDataSourceContextHolder.setDataSourceType(result.getDataSourceKey()); |
|
|
|
//删除分库用户
|
|
|
|
userService.deleteUserByUsername(dmsUser.getUserName()); |
|
|
|
} |
|
|
@ -172,7 +181,4 @@ public class AdminDmsUserController { |
|
|
|
return JsonResponse.ok(new PageInfo<>(adminTenantUserService.query(dto.getParam()))); |
|
|
|
} |
|
|
|
|
|
|
|
private static void changeDataSource(String key) { |
|
|
|
DynamicDataSourceContextHolder.setDataSourceType(key); |
|
|
|
} |
|
|
|
} |
|
|
|