change - 数据源创建时自动分配管理员,删除时清除已分配的用户绑定

master
Wen JY 2 years ago
parent 8641c5a85a
commit cfe4b87274

@ -5,7 +5,12 @@ import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import com.ruoyi.common.core.utils.uuid.UUID;
import com.ruoyi.system.api.domain.SysUser;
import com.ruoyi.system.domain.SysUserDatasource;
import com.ruoyi.system.service.ISysUserDatasourceService;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
@ -37,6 +42,12 @@ public class SysDatasourceController extends BaseController
@Autowired
private ISysDatasourceService sysDatasourceService;
@Autowired
private ISysUserDatasourceService sysUserDatasourceService;
@Autowired
private ISysUserService userService;
/**
*
*/
@ -82,6 +93,8 @@ public class SysDatasourceController extends BaseController
{
UUID uuid = UUID.randomUUID();
sysDatasource.setId(uuid.toString());
//将数据源分配给管理员
AddUserDataSource(sysDatasource);
return toAjax(sysDatasourceService.insertSysDatasource(sysDatasource));
}
@ -104,6 +117,56 @@ public class SysDatasourceController extends BaseController
@DeleteMapping("/{ids}")
public AjaxResult remove(@PathVariable String[] ids)
{
//删除已经分配的数据源
DeleteUserDataSource(ids);
return toAjax(sysDatasourceService.deleteSysDatasourceByIds(ids));
}
/**
*
* @param sysDatasource
*/
public void AddUserDataSource(SysDatasource sysDatasource){
try{
SysUser sysUser = userService.selectUserByUserName("admin");
SysUserDatasource sysUserDatasource = new SysUserDatasource();
sysUserDatasource.setUserId(sysUser.getUserId().toString());
sysUserDatasource.setDatasourceId(sysDatasource.getId());
int i = sysUserDatasourceService.insertSysUserDatasource(sysUserDatasource);
if(i>0){
logger.info("数据源:"+sysDatasource.getParkName()+";分配成功!");
}else {
logger.info("数据源:"+sysDatasource.getParkName()+";分配成功!");
}
}catch (Exception ex){
logger.error("数据源分配异常:"+ex.getMessage());
}
}
/**
*
* @param ids
*/
public void DeleteUserDataSource(String[] ids){
try{
for (String id : ids) {
SysUserDatasource sysUserDatasource = new SysUserDatasource();
sysUserDatasource.setDatasourceId(id);
List<SysUserDatasource> sysUserDatasources = sysUserDatasourceService.selectSysUserDatasourceList(sysUserDatasource);
if(sysUserDatasources.size() > 0){
Long[] ageArray = sysUserDatasources.stream()
.map(SysUserDatasource::getId)
.toArray(Long[]::new);
int i = sysUserDatasourceService.deleteSysUserDatasourceByIds(ageArray);
}
}
}catch (Exception ex){
logger.error("删除绑定数据信息异常:"+ex.getMessage());
}
}
}

@ -411,7 +411,7 @@ public class SysUserController extends BaseController {
sysUserDatasource.setDatasourceId(dataSourceId);
sysUserDatasource.setUserId(userId.toString());
int i = sysUserDatasourceService.insertSysUserDatasource(sysUserDatasource);
if (i>1){
if (i>0){
System.out.println("保存成功");
}else {
System.out.println("保存失败");

Loading…
Cancel
Save