工厂数据源切换

highway
zhaoxiaolin 1 year ago
parent ded76bd02d
commit 974dd13455

@ -16,6 +16,7 @@ import com.op.system.api.factory.RemoteUserFallbackFactory;
import com.op.system.api.model.LoginUser;
import java.util.List;
import java.util.Map;
/**
*
@ -48,4 +49,12 @@ public interface RemoteUserService {
@PostMapping("/file/upLoadFile")
public R<Boolean> upLoadFile(@RequestBody List<BaseFileData> files);
/**
*
* @param user
* @return
*/
@PostMapping("/user/getPoolNameList")
public R<List<Map<String, String>>> getPoolNameList(@RequestBody SysUser user);
}

@ -14,8 +14,8 @@ import com.op.common.core.xss.Xss;
/**
* sys_user
*
* @author OP
*
* @author sf
*/
public class SysUser extends BaseEntity {
private static final long serialVersionUID = 1L;
@ -85,6 +85,15 @@ public class SysUser extends BaseEntity {
/** 角色ID */
private Long roleId;
private String datasourceId;
public String getDatasourceId() {
return datasourceId;
}
public void setDatasourceId(String datasourceId) {
this.datasourceId = datasourceId;
}
public SysUser() {

@ -11,6 +11,7 @@ import com.op.system.api.domain.SysUser;
import com.op.system.api.model.LoginUser;
import java.util.List;
import java.util.Map;
/**
*
@ -39,6 +40,11 @@ public class RemoteUserFallbackFactory implements FallbackFactory<RemoteUserServ
public R<Boolean> upLoadFile(List<BaseFileData> files) {
return R.fail("上传失败:" + throwable.getMessage());
}
@Override
public R<List<Map<String, String>>> getPoolNameList(SysUser user) {
return R.fail("工厂数据源获取失败:" + throwable.getMessage());
}
};
}
}

@ -2,12 +2,13 @@ package com.op.system.api.model;
import java.io.Serializable;
import java.util.Set;
import com.op.system.api.domain.SysUser;
/**
*
*
* @author OP
* @author sf
*/
public class LoginUser implements Serializable {
private static final long serialVersionUID = 1L;
@ -51,6 +52,8 @@ public class LoginUser implements Serializable {
*
*/
private Set<String> roles;
// 可访问工厂数据源
private String poolName;
/**
*
@ -128,4 +131,12 @@ public class LoginUser implements Serializable {
public void setSysUser(SysUser sysUser) {
this.sysUser = sysUser;
}
public String getPoolName() {
return poolName;
}
public void setPoolName(String poolName) {
this.poolName = poolName;
}
}

@ -1,7 +1,10 @@
package com.op.auth.service;
import java.util.concurrent.TimeUnit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.op.common.core.constant.CacheConstants;
import com.op.common.core.constant.Constants;
import com.op.common.core.constant.SecurityConstants;
@ -20,8 +23,8 @@ import com.op.system.api.model.LoginUser;
/**
*
*
* @author OP
*
* @author sf
*/
@Component
public class SysLoginService {
@ -86,7 +89,15 @@ public class SysLoginService {
recordLogService.recordLogininfor(username, Constants.LOGIN_FAIL, "用户已停用,请联系管理员");
throw new ServiceException("对不起,您的账号:" + username + " 已停用");
}
/****** 2023年4月20日17:01:52 检查是否配置工厂数据源 ******/
if (StringUtils.isEmpty(userInfo.getPoolName())) {
recordLogService.recordLogininfor(username, Constants.LOGIN_FAIL, "用户暂未分配工厂数据源,请联系管理员");
throw new ServiceException("对不起,您的账号:" + username + " 暂未分配工厂数据源");
}
passwordService.validate(user, password);
// 缓存用户工厂数据源
redisService.setCacheObject(CacheConstants.USER_POOL_NAME_CURRENT + username, userInfo.getPoolName(),
CacheConstants.REFRESH_TIME, TimeUnit.MINUTES);
recordLogService.recordLogininfor(username, Constants.LOGIN_SUCCESS, "登录成功");
return userInfo;
}

@ -2,7 +2,7 @@ package com.op.common.core.constant;
/**
*
*
*
* @author OP
*/
public class CacheConstants {
@ -55,4 +55,6 @@ public class CacheConstants {
* IP cache key
*/
public static final String SYS_LOGIN_BLACKIPLIST = SYS_CONFIG_KEY + "sys.login.blackIPList";
// 用户当前所选工厂
public static final String USER_POOL_NAME_CURRENT = "user_pool_name_current:";
}

@ -10,8 +10,8 @@ import com.fasterxml.jackson.annotation.JsonInclude;
/**
* Entity
*
* @author OP
*
* @author sf
*/
public class BaseEntity implements Serializable {
private static final long serialVersionUID = 1L;
@ -36,6 +36,10 @@ public class BaseEntity implements Serializable {
/** 备注 */
private String remark;
//数据源标识
private String poolName;
//部门
private Long deptId;
/** 请求参数 */
@JsonInclude(JsonInclude.Include.NON_EMPTY)
@ -99,4 +103,20 @@ public class BaseEntity implements Serializable {
public void setParams(Map<String, Object> params) {
this.params = params;
}
public String getPoolName() {
return poolName;
}
public void setPoolName(String poolName) {
this.poolName = poolName;
}
public Long getDeptId() {
return deptId;
}
public void setDeptId(Long deptId) {
this.deptId = deptId;
}
}

@ -8,8 +8,8 @@ import java.lang.annotation.Target;
/**
*
*
* @author OP
*
* @author sf
*/
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)

@ -0,0 +1,24 @@
package com.op.common.datascope.annotation;
import java.lang.annotation.*;
/**
*
*
* @ClassName: DataPoolDept
* @Description: TODO
* @author shichangzhou
* @date 2023424 5:47:07
*/
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface PoolDeptAuto {
// 是否自动设置数据源标识
public boolean pool() default true;
// 是否自动设置部门
public boolean dept() default true;
}

@ -1,16 +1,26 @@
package com.op.common.datascope.aspect;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import com.op.common.datascope.annotation.DataScope;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.op.common.core.constant.CacheConstants;
import com.op.common.core.context.SecurityContextHolder;
import com.op.common.core.text.Convert;
import com.op.common.core.utils.StringUtils;
import com.op.common.core.web.domain.BaseEntity;
import com.op.common.datascope.annotation.DataScope;
import com.op.common.datascope.annotation.PoolDeptAuto;
import com.op.common.redis.service.RedisService;
import com.op.common.security.utils.SecurityUtils;
import com.op.system.api.domain.SysRole;
import com.op.system.api.domain.SysUser;
@ -18,12 +28,18 @@ import com.op.system.api.model.LoginUser;
/**
*
*
* @author OP
*
* @author sf
*/
@Aspect
@Component
public class DataScopeAspect {
private static final Logger log = LoggerFactory.getLogger(DataScopeAspect.class);
@Autowired
private RedisService redisService;
/**
*
*/
@ -60,6 +76,57 @@ public class DataScopeAspect {
handleDataScope(point, controllerDataScope);
}
@Before("@annotation(controllerPoolDeptAuto)")
public void doBefore(JoinPoint point, PoolDeptAuto controllerPoolDeptAuto)
throws SecurityException, IllegalArgumentException, IllegalAccessException, NoSuchFieldException {
Object obj = point.getArgs()[0];
if (controllerPoolDeptAuto.pool()) {
log.debug("已开启自动设置数据源标识");
Field field = null;
try {
field = obj.getClass().getSuperclass().getDeclaredField("poolName");
} catch (NoSuchFieldException e) {
field = obj.getClass().getDeclaredField("poolName");
log.error("If PoolDeptAuto annotation is used, the parameter must contain poolName!", e);
throw e;
}
field.setAccessible(true);
Object poolName = field.get(obj);
if (poolName == null) {
// 缓存用户工厂数据源
poolName = redisService
.getCacheObject(CacheConstants.USER_POOL_NAME_CURRENT + SecurityUtils.getUsername());
if (StringUtils.isNotNull(poolName)) {
field.set(obj, poolName);
} else {
log.error("Cannot be found the current poolName!");
}
}
}
if (controllerPoolDeptAuto.dept()) {
Field field = null;
try {
field = obj.getClass().getSuperclass().getDeclaredField("deptId");
} catch (NoSuchFieldException e) {
field = obj.getClass().getDeclaredField("deptId");
log.error("If PoolDeptAuto annotation is used, the parameter must contain deptId!", e);
throw e;
}
field.setAccessible(true);
Object deptId = field.get(obj);
if (deptId == null) {
// 获取当前的用户
LoginUser loginUser = SecurityUtils.getLoginUser();
if (StringUtils.isNotNull(loginUser)) {
field.set(obj, loginUser.getSysUser().getDeptId());
} else {
log.error("Cannot be found the current login user deptId!");
}
}
}
}
protected void handleDataScope(final JoinPoint joinPoint, DataScope controllerDataScope) {
// 获取当前的用户
LoginUser loginUser = SecurityUtils.getLoginUser();
@ -77,7 +144,7 @@ public class DataScopeAspect {
/**
*
*
*
* @param joinPoint
* @param user
* @param deptAlias
@ -85,7 +152,7 @@ public class DataScopeAspect {
* @param permission
*/
public static void dataScopeFilter(JoinPoint joinPoint, SysUser user, String deptAlias, String userAlias,
String permission) {
String permission) {
StringBuilder sqlString = new StringBuilder();
List<String> conditions = new ArrayList<String>();

@ -0,0 +1,36 @@
package com.op.common.datasource.creator;
import com.baomidou.dynamic.datasource.DynamicRoutingDataSource;
import com.baomidou.dynamic.datasource.creator.DefaultDataSourceCreator;
import com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DataSourceProperty;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import javax.sql.DataSource;
/**
*
*
* @ClassName: DynamicDatasourceCreator
* @Description: TODO
* @author shichangzhou
* @date 2023420 1:15:09
*/
@Component
public class DynamicDatasourceCreator {
private static final Logger log = LoggerFactory.getLogger(DynamicDatasourceCreator.class);
@Resource
private DynamicRoutingDataSource dynamicRoutingDataSource;
@Resource
private DefaultDataSourceCreator defaultDataSourceCreator;
public DataSource createDynamicDataSource(DataSourceProperty dataSourceProperty) {
log.info("Dynamic Datasource init.....");
DataSource dataSource = defaultDataSourceCreator.createDataSource(dataSourceProperty);
dynamicRoutingDataSource.addDataSource(dataSourceProperty.getPoolName(), dataSource);
return dataSource;
}
}

@ -0,0 +1,53 @@
package com.op.mes.config;
import com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DataSourceProperty;
import com.op.common.core.domain.R;
import com.op.common.datasource.creator.DynamicDatasourceCreator;
import com.op.system.api.RemoteUserService;
import com.op.system.api.domain.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
/**
*
*
* @ClassName: DynamicDatasource
* @Description: TODO
* @author shichangzhou
* @date 2023419 1:01:30
*/
@Component
public class DynamicDatasource {
@Resource
private DynamicDatasourceCreator dynamicDatasourceCreator;
@Autowired
private RemoteUserService remoteUserService;
@PostConstruct
public void init() {
// 加载sf-cloud库的sys_datasource
SysUser sysUser = new SysUser();
sysUser.setUserId(1L);
R<List<Map<String, String>>> dateSources0 = remoteUserService.getPoolNameList(sysUser);
List<Map<String, String>> dateSources = dateSources0.getData();
for (Map<String, String> dateSource : dateSources) {
DataSourceProperty sdp = new DataSourceProperty();
sdp.setUrl(dateSource.get("url"));
sdp.setUsername(dateSource.get("userName"));
sdp.setPassword(dateSource.get("password"));
sdp.setDriverClassName(dateSource.get("driveClassName"));
sdp.setPoolName(dateSource.get("poolName"));// 这是数据源的key
sdp.setLazy(false);
dynamicDatasourceCreator.createDynamicDataSource(sdp);
}
}
}

@ -34,6 +34,7 @@ public class BaseFileController extends BaseController {
*/
@RequiresPermissions("system:file:list")
@GetMapping("/list")
public TableDataInfo list(BaseFile baseFile) {
startPage();
List<BaseFile> list = baseFileService.selectBaseFileList(baseFile);

@ -3,6 +3,7 @@ package com.op.mes.controller;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.op.common.core.utils.poi.ExcelUtil;
import com.op.common.core.utils.uuid.IdUtils;
import com.op.common.core.web.domain.AjaxResult;

@ -0,0 +1,148 @@
package com.op.mes.domain;
import com.op.common.core.annotation.Excel;
import com.op.common.core.web.domain.BaseEntity;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
/**
* sys_datasource
*
* @author sf
* @date 2023-04-20
*/
public class SysDatasource extends BaseEntity
{
private static final long serialVersionUID = 1L;
/** 用户ID */
private String id;
/** 地址 */
@Excel(name = "地址")
private String url;
/** 用户名 */
@Excel(name = "用户名")
private String userName;
/** 密码 */
@Excel(name = "密码")
private String password;
/** 驱动 */
@Excel(name = "驱动")
private String driveClassName;
/** 数据源标识 */
@Excel(name = "数据源标识")
private String poolName;
private String parkName;
/** 帐号状态0正常 */
@Excel(name = "帐号状态", readConverterExp = "帐号状态0正常")
private String status;
/** 删除标志0代表存在 */
private String delFlag;
public void setId(String id)
{
this.id = id;
}
public String getId()
{
return id;
}
public void setUrl(String url)
{
this.url = url;
}
public String getUrl()
{
return url;
}
public void setUserName(String userName)
{
this.userName = userName;
}
public String getUserName()
{
return userName;
}
public void setPassword(String password)
{
this.password = password;
}
public String getPassword()
{
return password;
}
public void setDriveClassName(String driveClassName)
{
this.driveClassName = driveClassName;
}
public String getDriveClassName()
{
return driveClassName;
}
public void setPoolName(String poolName)
{
this.poolName = poolName;
}
public String getPoolName()
{
return poolName;
}
public void setStatus(String status)
{
this.status = status;
}
public String getStatus()
{
return status;
}
public void setDelFlag(String delFlag)
{
this.delFlag = delFlag;
}
public String getDelFlag()
{
return delFlag;
}
public String getParkName() {
return parkName;
}
public void setParkName(String parkName) {
this.parkName = parkName;
}
@Override
public String toString() {
return new ToStringBuilder(this,ToStringStyle.MULTI_LINE_STYLE)
.append("id", getId())
.append("url", getUrl())
.append("userName", getUserName())
.append("password", getPassword())
.append("driveClassName", getDriveClassName())
.append("poolName", getPoolName())
.append("status", getStatus())
.append("delFlag", getDelFlag())
.append("createBy", getCreateBy())
.append("createTime", getCreateTime())
.append("updateBy", getUpdateBy())
.append("updateTime", getUpdateTime())
.append("remark", getRemark())
.toString();
}
}

@ -1,5 +1,6 @@
package com.op.mes.service.impl;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.op.common.core.domain.BaseFileData;
import com.op.common.core.utils.DateUtils;
import com.op.mes.domain.BaseFile;
@ -28,6 +29,7 @@ public class BaseFileServiceImpl implements IBaseFileService {
* @return
*/
@Override
@DS("#header.poolName")
public BaseFile selectBaseFileByFileId(String fileId) {
return baseFileMapper.selectBaseFileByFileId(fileId);
}
@ -39,6 +41,7 @@ public class BaseFileServiceImpl implements IBaseFileService {
* @return
*/
@Override
@DS("#header.poolName")
public List<BaseFile> selectBaseFileList(BaseFile baseFile) {
return baseFileMapper.selectBaseFileList(baseFile);
}
@ -50,6 +53,7 @@ public class BaseFileServiceImpl implements IBaseFileService {
* @return
*/
@Override
@DS("#header.poolName")
public int insertBaseFile(BaseFile baseFile) {
baseFile.setCreateTime(DateUtils.getNowDate());
return baseFileMapper.insertBaseFile(baseFile);
@ -62,6 +66,7 @@ public class BaseFileServiceImpl implements IBaseFileService {
* @return
*/
@Override
@DS("#header.poolName")
public int updateBaseFile(BaseFile baseFile) {
baseFile.setUpdateTime(DateUtils.getNowDate());
return baseFileMapper.updateBaseFile(baseFile);
@ -74,6 +79,7 @@ public class BaseFileServiceImpl implements IBaseFileService {
* @return
*/
@Override
@DS("#header.poolName")
public int deleteBaseFileByFileIds(String[] fileIds) {
return baseFileMapper.deleteBaseFileByFileIds(fileIds);
}
@ -85,6 +91,7 @@ public class BaseFileServiceImpl implements IBaseFileService {
* @return
*/
@Override
@DS("#header.poolName")
public int deleteBaseFileByFileId(String fileId) {
return baseFileMapper.deleteBaseFileByFileId(fileId);
}
@ -96,6 +103,7 @@ public class BaseFileServiceImpl implements IBaseFileService {
* @return
*/
@Override
@DS("#header.poolName")
public Boolean insertBaseFileBatch(List<BaseFileData> baseFiles) {
return baseFileMapper.insertBaseFileBatch(baseFiles);
}

@ -1,6 +1,8 @@
package com.op.mes.service.impl;
import java.util.List;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.op.common.core.utils.DateUtils;
import com.op.common.core.utils.uuid.IdUtils;
import org.springframework.beans.factory.annotation.Autowired;
@ -27,6 +29,7 @@ public class ProOrderServiceImpl implements IProOrderService {
* @return
*/
@Override
@DS("#header.poolName")
public ProOrder selectProOrderById(String id) {
return proOrderMapper.selectProOrderById(id);
}
@ -38,6 +41,7 @@ public class ProOrderServiceImpl implements IProOrderService {
* @return
*/
@Override
@DS("#header.poolName")
public List<ProOrder> selectProOrderList(ProOrder proOrder) {
return proOrderMapper.selectProOrderList(proOrder);
}
@ -49,6 +53,7 @@ public class ProOrderServiceImpl implements IProOrderService {
* @return
*/
@Override
@DS("#header.poolName")
public int insertProOrder(ProOrder proOrder) {
proOrder.setId(IdUtils.fastSimpleUUID());
proOrder.setCreateTime(DateUtils.getNowDate());
@ -62,6 +67,7 @@ public class ProOrderServiceImpl implements IProOrderService {
* @return
*/
@Override
@DS("#header.poolName")
public int updateProOrder(ProOrder proOrder) {
proOrder.setUpdateTime(DateUtils.getNowDate());
return proOrderMapper.updateProOrder(proOrder);
@ -74,6 +80,7 @@ public class ProOrderServiceImpl implements IProOrderService {
* @return
*/
@Override
@DS("#header.poolName")
public int deleteProOrderByIds(String[] ids) {
return proOrderMapper.deleteProOrderByIds(ids);
}
@ -85,6 +92,7 @@ public class ProOrderServiceImpl implements IProOrderService {
* @return
*/
@Override
@DS("#header.poolName")
public int deleteProOrderById(String id) {
return proOrderMapper.deleteProOrderById(id);
}

@ -2,6 +2,7 @@ package com.op.mes.service.impl;
import java.util.List;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.op.common.core.utils.DateUtils;
import com.op.common.core.utils.uuid.IdUtils;
import com.op.mes.domain.ProProcessContent;
@ -29,6 +30,7 @@ public class ProProcessContentServiceImpl implements IProProcessContentService
* @return
*/
@Override
@DS("#header.poolName")
public ProProcessContent selectProProcessContentByContentId(String contentId)
{
return proProcessContentMapper.selectProProcessContentByContentId(contentId);
@ -41,6 +43,7 @@ public class ProProcessContentServiceImpl implements IProProcessContentService
* @return
*/
@Override
@DS("#header.poolName")
public List<ProProcessContent> selectProProcessContentList(ProProcessContent proProcessContent)
{
return proProcessContentMapper.selectProProcessContentList(proProcessContent);
@ -53,6 +56,7 @@ public class ProProcessContentServiceImpl implements IProProcessContentService
* @return
*/
@Override
@DS("#header.poolName")
public int insertProProcessContent(ProProcessContent proProcessContent)
{
proProcessContent.setCreateTime(DateUtils.getNowDate());
@ -67,6 +71,7 @@ public class ProProcessContentServiceImpl implements IProProcessContentService
* @return
*/
@Override
@DS("#header.poolName")
public int updateProProcessContent(ProProcessContent proProcessContent)
{
proProcessContent.setUpdateTime(DateUtils.getNowDate());
@ -80,6 +85,7 @@ public class ProProcessContentServiceImpl implements IProProcessContentService
* @return
*/
@Override
@DS("#header.poolName")
public int deleteProProcessContentByContentIds(String[] contentIds)
{
return proProcessContentMapper.deleteProProcessContentByContentIds(contentIds);
@ -92,6 +98,7 @@ public class ProProcessContentServiceImpl implements IProProcessContentService
* @return
*/
@Override
@DS("#header.poolName")
public int deleteProProcessContentByContentId(String contentId)
{
return proProcessContentMapper.deleteProProcessContentByContentId(contentId);

@ -1,5 +1,6 @@
package com.op.mes.service.impl;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.op.common.core.constant.UserConstants;
import com.op.common.core.domain.BaseFileData;
import com.op.common.core.utils.DateUtils;
@ -43,6 +44,7 @@ public class ProProcessServiceImpl implements IProProcessService
* @return
*/
@Override
@DS("#header.poolName")
public ProProcess selectProProcessByProcessId(String processId)
{
ProProcess p = proProcessMapper.selectProProcessByProcessId(processId);
@ -60,6 +62,7 @@ public class ProProcessServiceImpl implements IProProcessService
* @return
*/
@Override
@DS("#header.poolName")
public List<ProProcess> selectProProcessList(ProProcess proProcess)
{
return proProcessMapper.selectProProcessList(proProcess);
@ -71,6 +74,7 @@ public class ProProcessServiceImpl implements IProProcessService
* @return
*/
@Override
@DS("#header.poolName")
public Boolean checkProcessCodeUnique(ProProcess proProcess) {
ProProcess process = proProcessMapper.checkProcessCodeUnique(proProcess);
if(StringUtils.isNotNull(process) && !process.getProcessId().equals(proProcess.getProcessId())){
@ -86,6 +90,7 @@ public class ProProcessServiceImpl implements IProProcessService
* @return
*/
@Override
@DS("#header.poolName")
public Boolean checkProcessNameUnique(ProProcess proProcess) {
ProProcess process = proProcessMapper.checkProcessNameUnique(proProcess);
@ -102,6 +107,7 @@ public class ProProcessServiceImpl implements IProProcessService
* @return
*/
@Override
@DS("#header.poolName")
public int insertProProcess(ProProcess proProcess)
{
proProcess.setProcessId(IdUtils.fastSimpleUUID());
@ -135,6 +141,7 @@ public class ProProcessServiceImpl implements IProProcessService
* @return
*/
@Override
@DS("#header.poolName")
public int updateProProcess(ProProcess proProcess)
{
@ -168,6 +175,7 @@ public class ProProcessServiceImpl implements IProProcessService
* @return
*/
@Override
@DS("#header.poolName")
public int deleteProProcessByProcessIds(String[] processIds)
{
return proProcessMapper.deleteProProcessByProcessIds(processIds);
@ -180,6 +188,7 @@ public class ProProcessServiceImpl implements IProProcessService
* @return
*/
@Override
@DS("#header.poolName")
public int deleteProProcessByProcessId(String processId)
{
return proProcessMapper.deleteProProcessByProcessId(processId);

@ -2,6 +2,8 @@ package com.op.mes.service.impl;
import java.util.Date;
import java.util.List;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.op.common.core.utils.DateUtils;
import com.op.common.core.utils.uuid.IdUtils;
import com.op.common.security.utils.SecurityUtils;
@ -30,6 +32,7 @@ public class ProRouteLineServiceImpl implements IProRouteLineService {
* @return 线
*/
@Override
@DS("#header.poolName")
public ProRouteLine selectProRouteLineByRecordId(String recordId) {
return proRouteLineMapper.selectProRouteLineByRecordId(recordId);
}
@ -41,6 +44,7 @@ public class ProRouteLineServiceImpl implements IProRouteLineService {
* @return 线
*/
@Override
@DS("#header.poolName")
public List<ProRouteLine> selectProRouteLineList(ProRouteLine proRouteLine) {
return proRouteLineMapper.selectProRouteLineList(proRouteLine);
}
@ -52,6 +56,7 @@ public class ProRouteLineServiceImpl implements IProRouteLineService {
* @return
*/
@Override
@DS("#header.poolName")
public int insertProRouteLine(ProRouteLine proRouteLine) {
Date now = DateUtils.getNowDate();
if(StringUtils.isNotBlank(proRouteLine.getRouteId())){
@ -81,6 +86,7 @@ public class ProRouteLineServiceImpl implements IProRouteLineService {
* @return
*/
@Override
@DS("#header.poolName")
public int updateProRouteLine(ProRouteLine proRouteLine) {
proRouteLine.setUpdateTime(DateUtils.getNowDate());
return proRouteLineMapper.updateProRouteLine(proRouteLine);
@ -93,6 +99,7 @@ public class ProRouteLineServiceImpl implements IProRouteLineService {
* @return
*/
@Override
@DS("#header.poolName")
public int deleteProRouteLineByRecordIds(String[] recordIds) {
return proRouteLineMapper.deleteProRouteLineByRecordIds(recordIds);
}
@ -104,11 +111,13 @@ public class ProRouteLineServiceImpl implements IProRouteLineService {
* @return
*/
@Override
@DS("#header.poolName")
public int deleteProRouteLineByRecordId(String recordId) {
return proRouteLineMapper.deleteProRouteLineByRecordId(recordId);
}
@Override
@DS("#header.poolName")
public ProRouteLine getList(ProRouteLine proRouteLine) {
ProRouteLine dto = new ProRouteLine();
List<ProRouteLine> unSelected= proRouteLineMapper.getRouteProdLineListUndo(proRouteLine);

@ -3,6 +3,7 @@ package com.op.mes.service.impl;
import java.util.List;
import java.util.stream.Collectors;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.op.common.core.constant.UserConstants;
import com.op.common.core.utils.DateUtils;
import com.op.common.core.utils.StringUtils;
@ -37,6 +38,7 @@ public class ProRouteProcessServiceImpl implements IProRouteProcessService
* @return
*/
@Override
@DS("#header.poolName")
public ProRouteProcess selectProRouteProcessByRecordId(String recordId)
{
return proRouteProcessMapper.selectProRouteProcessByRecordId(recordId);
@ -49,12 +51,14 @@ public class ProRouteProcessServiceImpl implements IProRouteProcessService
* @return
*/
@Override
@DS("#header.poolName")
public List<ProRouteProcess> selectProRouteProcessList(ProRouteProcess proRouteProcess)
{
return proRouteProcessMapper.selectProRouteProcessList(proRouteProcess);
}
@Override
@DS("#header.poolName")
public Boolean checkOrderNumExists(ProRouteProcess proRouteProcess) {
ProRouteProcess process = proRouteProcessMapper.checkOrderNumExists(proRouteProcess);
String recordId = proRouteProcess.getRecordId();
@ -65,6 +69,7 @@ public class ProRouteProcessServiceImpl implements IProRouteProcessService
}
@Override
@DS("#header.poolName")
public Boolean checkProcessExists(ProRouteProcess proRouteProcess) {
ProRouteProcess process = proRouteProcessMapper.checkProcessExists(proRouteProcess);
String recordId = proRouteProcess.getRecordId();
@ -75,6 +80,7 @@ public class ProRouteProcessServiceImpl implements IProRouteProcessService
}
@Override
@DS("#header.poolName")
public Boolean checkUpdateFlagUnique(ProRouteProcess proRouteProcess) {
ProRouteProcess process = proRouteProcessMapper.checkUpdateFlagUnique(proRouteProcess);
String recordId = proRouteProcess.getRecordId();
@ -115,11 +121,13 @@ public class ProRouteProcessServiceImpl implements IProRouteProcessService
// }
@Override
@DS("#header.poolName")
public ProRouteProcess findPreProcess(ProRouteProcess proRouteProcess) {
return proRouteProcessMapper.findPreProcess(proRouteProcess);
}
@Override
@DS("#header.poolName")
public ProRouteProcess findNextProcess(ProRouteProcess proRouteProcess) {
return proRouteProcessMapper.findNextProcess(proRouteProcess);
}
@ -131,6 +139,7 @@ public class ProRouteProcessServiceImpl implements IProRouteProcessService
* @return
*/
@Override
@DS("#header.poolName")
public int insertProRouteProcess(ProRouteProcess proRouteProcess)
{
proRouteProcess.setCreateTime(DateUtils.getNowDate());
@ -145,6 +154,7 @@ public class ProRouteProcessServiceImpl implements IProRouteProcessService
* @return
*/
@Override
@DS("#header.poolName")
public int updateProRouteProcess(ProRouteProcess proRouteProcess)
{
proRouteProcess.setUpdateTime(DateUtils.getNowDate());
@ -158,6 +168,7 @@ public class ProRouteProcessServiceImpl implements IProRouteProcessService
* @return
*/
@Override
@DS("#header.poolName")
public int deleteProRouteProcessByRecordIds(String[] recordIds)
{
return proRouteProcessMapper.deleteProRouteProcessByRecordIds(recordIds);
@ -170,12 +181,14 @@ public class ProRouteProcessServiceImpl implements IProRouteProcessService
* @return
*/
@Override
@DS("#header.poolName")
public int deleteProRouteProcessByRecordId(String recordId)
{
return proRouteProcessMapper.deleteProRouteProcessByRecordId(recordId);
}
@Override
@DS("#header.poolName")
public int deleteByRouteId(String routeId) {
return proRouteProcessMapper.deleteByRouteId(routeId);
}

@ -4,6 +4,7 @@ import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.op.common.core.constant.UserConstants;
import com.op.common.core.domain.BaseFileData;
import com.op.common.core.utils.DateUtils;
@ -41,6 +42,7 @@ public class ProRouteServiceImpl implements IProRouteService
* @return 线
*/
@Override
@DS("#header.poolName")
public ProRoute selectProRouteByRouteId(String routeId)
{
@ -59,6 +61,7 @@ public class ProRouteServiceImpl implements IProRouteService
* @return 线
*/
@Override
@DS("#header.poolName")
public List<ProRoute> selectProRouteList(ProRoute proRoute)
{
return proRouteMapper.selectProRouteList(proRoute);
@ -71,6 +74,7 @@ public class ProRouteServiceImpl implements IProRouteService
@Override
@DS("#header.poolName")
public Boolean checkRouteCodeUnique(ProRoute proRoute) {
ProRoute route = proRouteMapper.checkRouteCodeUnique(proRoute);
String routeId = proRoute.getRouteId();
@ -87,6 +91,7 @@ public class ProRouteServiceImpl implements IProRouteService
* @return
*/
@Override
@DS("#header.poolName")
public int insertProRoute(ProRoute proRoute)
{
proRoute.setCreateTime(DateUtils.getNowDate());
@ -119,6 +124,7 @@ public class ProRouteServiceImpl implements IProRouteService
* @return
*/
@Override
@DS("#header.poolName")
public int updateProRoute(ProRoute proRoute)
{
proRoute.setUpdateTime(DateUtils.getNowDate());
@ -158,6 +164,7 @@ public class ProRouteServiceImpl implements IProRouteService
* @return
*/
@Override
@DS("#header.poolName")
public int deleteProRouteByRouteIds(String[] routeIds)
{
return proRouteMapper.deleteProRouteByRouteIds(routeIds);
@ -170,6 +177,7 @@ public class ProRouteServiceImpl implements IProRouteService
* @return
*/
@Override
@DS("#header.poolName")
public int deleteProRouteByRouteId(String routeId)
{
return proRouteMapper.deleteProRouteByRouteId(routeId);

@ -194,5 +194,6 @@
<orderEntry type="library" name="Maven: org.bouncycastle:bcpkix-jdk15on:1.69" level="project" />
<orderEntry type="library" name="Maven: org.bouncycastle:bcprov-jdk15on:1.69" level="project" />
<orderEntry type="library" name="Maven: org.bouncycastle:bcutil-jdk15on:1.69" level="project" />
<orderEntry type="library" name="sapjco3" level="project" />
</component>
</module>

@ -13,13 +13,13 @@ spring:
cloud:
nacos:
discovery:
namespace: haiwei-test
group: ywl
namespace: lanju-op
group: zxl
# 服务注册地址
server-addr: 140.249.53.142:8848
config:
namespace: haiwei-test
group: ywl
namespace: lanju-op
group: zxl
#命名空间
#group: local
# 配置中心地址

@ -0,0 +1,47 @@
package com.op.system.config;
import com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DataSourceProperty;
import com.op.common.datasource.creator.DynamicDatasourceCreator;
import com.op.system.api.domain.SysUser;
import com.op.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
/**
*
*
* @ClassName: DynamicDatasource
* @Description: TODO
* @author shichangzhou
* @date 2023419 1:01:30
*/
@Component
public class DynamicDatasource {
@Resource
private DynamicDatasourceCreator dynamicDatasourceCreator;
@Autowired
private ISysUserService sysUserService;
@PostConstruct
public void init() {
SysUser sysUser = new SysUser();
sysUser.setUserId(1L);
List<Map<String, String>> dateSources = sysUserService.getPoolNameList(sysUser);
for (Map<String, String> dateSource : dateSources) {
DataSourceProperty sdp = new DataSourceProperty();
sdp.setUrl(dateSource.get("url"));
sdp.setUsername(dateSource.get("userName"));
sdp.setPassword(dateSource.get("password"));
sdp.setDriverClassName(dateSource.get("driveClassName"));
sdp.setPoolName(dateSource.get("poolName"));// 这是数据源的key
sdp.setLazy(false);
dynamicDatasourceCreator.createDynamicDataSource(sdp);
}
}
}

@ -0,0 +1,155 @@
package com.op.system.controller;
import com.op.common.core.utils.poi.ExcelUtil;
import com.op.common.core.web.controller.BaseController;
import com.op.common.core.web.domain.AjaxResult;
import com.op.common.core.web.page.TableDataInfo;
import com.op.common.log.annotation.Log;
import com.op.common.log.enums.BusinessType;
import com.op.common.security.annotation.RequiresPermissions;
import com.op.system.api.domain.SysUser;
import com.op.system.domain.SysDatasource;
import com.op.system.service.ISysDatasourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
/**
* Controller
*
* @author sf
* @date 2023-05-18
*/
@RestController
@RequestMapping("/datasource")
public class SysDatasourceController extends BaseController {
@Autowired
private ISysDatasourceService sysDatasourceService;
/**
*
*/
@RequiresPermissions("system:datasource:list")
@GetMapping("/list")
public TableDataInfo list(SysDatasource sysDatasource) {
startPage();
List<SysDatasource> list = sysDatasourceService.selectSysDatasourceList(sysDatasource);
return getDataTable(list);
}
/**
*
*/
@RequiresPermissions("system:datasource:export")
@Log(title = "工厂数据库", businessType = BusinessType.EXPORT)
@PostMapping("/export")
public void export(HttpServletResponse response, SysDatasource sysDatasource) {
List<SysDatasource> list = sysDatasourceService.selectSysDatasourceList(sysDatasource);
ExcelUtil<SysDatasource> util = new ExcelUtil<SysDatasource>(SysDatasource.class);
util.exportExcel(response, list, "工厂数据库数据");
}
/**
*
*/
@RequiresPermissions("system:datasource:query")
@GetMapping(value = "/{id}")
public AjaxResult getInfo(@PathVariable("id") String id) {
return success(sysDatasourceService.selectSysDatasourceById(id));
}
/**
*
*/
@RequiresPermissions("system:datasource:add")
@Log(title = "工厂数据库", businessType = BusinessType.INSERT)
@PostMapping
public AjaxResult add(@RequestBody SysDatasource sysDatasource) {
return toAjax(sysDatasourceService.insertSysDatasource(sysDatasource));
}
/**
*
*/
@RequiresPermissions("system:datasource:edit")
@Log(title = "工厂数据库", businessType = BusinessType.UPDATE)
@PutMapping
public AjaxResult edit(@RequestBody SysDatasource sysDatasource) {
return toAjax(sysDatasourceService.updateSysDatasource(sysDatasource));
}
/**
*
*/
@RequiresPermissions("system:datasource:remove")
@Log(title = "工厂数据库", businessType = BusinessType.DELETE)
@DeleteMapping("/{ids}")
public AjaxResult remove(@PathVariable String[] ids) {
return toAjax(sysDatasourceService.deleteSysDatasourceByIds(ids));
}
/**
*
*/
@RequiresPermissions("system:datasource:edit")
@GetMapping("/authUser/allocatedList")
public TableDataInfo allocatedList(SysDatasource sysDatasource) {
startPage();
List<SysDatasource> list = sysDatasourceService.selectAllocatedList(sysDatasource);
return getDataTable(list);
}
/**
*
*/
//@RequiresPermissions("system:role:list")
@GetMapping("/authUser/unallocatedList")
public TableDataInfo unallocatedList(SysUser user) {
startPage();
List<SysUser> list = sysDatasourceService.selectUnallocatedList(user);
return getDataTable(list);
}
/**
*
*/
//@RequiresPermissions("system:role:edit")
@Log(title = "数据源管理", businessType = BusinessType.GRANT)
@PutMapping("/authUser/selectAll")
public AjaxResult selectAuthUserAll(String datasourceId, Long[] userIds) {
//sysDatasourceService.checkRoleDataScope(datasourceId);
return toAjax(sysDatasourceService.insertAuthUsers(datasourceId, userIds));
}
/**
*
*/
//@RequiresPermissions("system:role:edit")
@Log(title = "数据源管理", businessType = BusinessType.GRANT)
@PutMapping("/authUser/cancel")
public AjaxResult cancelAuthUser(@RequestBody SysDatasource sysDatasource) {
return toAjax(sysDatasourceService.deleteAuthUser(sysDatasource));
}
/**
*
*/
//@RequiresPermissions("system:role:edit")
@Log(title = "角色管理", businessType = BusinessType.GRANT)
@PutMapping("/authUser/cancelAll")
public AjaxResult cancelAuthUserAll(String datasourceId, Long[] userIds) {
return toAjax(sysDatasourceService.deleteAuthUsers(datasourceId, userIds));
}
@RequiresPermissions("system:datasource:add")
@Log(title = "激活工厂数据库", businessType = BusinessType.UPDATE)
@GetMapping("/changeDBStatus/{datasourceId}")
public AjaxResult changeDBStatus(@PathVariable("datasourceId") String datasourceId) {
SysDatasource sysDatasource = new SysDatasource();
sysDatasource.setDatasourceId(datasourceId);
return success(sysDatasourceService.changeDBStatus(sysDatasource));
}
}

@ -2,11 +2,16 @@ package com.op.system.controller;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
@ -17,6 +22,8 @@ import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import com.op.common.core.constant.CacheConstants;
import com.op.common.core.domain.R;
import com.op.common.core.utils.StringUtils;
import com.op.common.core.utils.poi.ExcelUtil;
@ -25,6 +32,7 @@ import com.op.common.core.web.domain.AjaxResult;
import com.op.common.core.web.page.TableDataInfo;
import com.op.common.log.annotation.Log;
import com.op.common.log.enums.BusinessType;
import com.op.common.redis.service.RedisService;
import com.op.common.security.annotation.InnerAuth;
import com.op.common.security.annotation.RequiresPermissions;
import com.op.common.security.utils.SecurityUtils;
@ -41,8 +49,8 @@ import com.op.system.service.ISysUserService;
/**
*
*
* @author OP
*
* @author sf
*/
@RestController
@RequestMapping("/user")
@ -64,6 +72,8 @@ public class SysUserController extends BaseController {
@Autowired
private ISysConfigService configService;
@Autowired
private RedisService redisService;
/**
*
@ -103,7 +113,7 @@ public class SysUserController extends BaseController {
}
/**
*
* ,
*/
@InnerAuth
@GetMapping("/info/{username}")
@ -116,10 +126,15 @@ public class SysUserController extends BaseController {
Set<String> roles = permissionService.getRolePermission(sysUser);
// 权限集合
Set<String> permissions = permissionService.getMenuPermission(sysUser);
// 工厂数据源集合
List<Map<String, String>> poolNameList = permissionService.getPoolNameList(sysUser);
LoginUser sysUserVo = new LoginUser();
sysUserVo.setSysUser(sysUser);
sysUserVo.setRoles(roles);
sysUserVo.setPermissions(permissions);
if (!CollectionUtils.isEmpty(poolNameList)) {
sysUserVo.setPoolName(poolNameList.get(0).get("poolName"));
}
return R.ok(sysUserVo);
}
@ -140,8 +155,8 @@ public class SysUserController extends BaseController {
}
/**
*
*
*
*
* @return
*/
@GetMapping("getInfo")
@ -151,10 +166,16 @@ public class SysUserController extends BaseController {
Set<String> roles = permissionService.getRolePermission(user);
// 权限集合
Set<String> permissions = permissionService.getMenuPermission(user);
// 工厂数据源集合
List<Map<String, String>> poolNameList = permissionService.getPoolNameList(user);
// 缓存用户工厂数据源
String poolName = redisService.getCacheObject(CacheConstants.USER_POOL_NAME_CURRENT + user.getUserName());
user.setPoolName(poolName);
AjaxResult ajax = AjaxResult.success();
ajax.put("user", user);
ajax.put("roles", roles);
ajax.put("permissions", permissions);
ajax.put("poolNameList", poolNameList);
return ajax;
}
@ -291,6 +312,25 @@ public class SysUserController extends BaseController {
@RequiresPermissions("system:user:list")
@GetMapping("/deptTree")
public AjaxResult deptTree(SysDept dept) {
return success(deptService.selectDeptTreeList(dept));
}
/**
*
*/
@RequiresPermissions("system:user:changePoolName")
@PutMapping("/changePoolName")
public AjaxResult changePoolName(@RequestBody SysUser user) {
String username = SecurityUtils.getUsername();
// 缓存用户工厂数据源
redisService.setCacheObject(CacheConstants.USER_POOL_NAME_CURRENT + username, user.getPoolName(),
CacheConstants.REFRESH_TIME, TimeUnit.MINUTES);
return success();
}
@PostMapping("/getPoolNameList")
public R<List<Map<String, String>>> getPoolNameLis(@RequestBody SysUser user) {
return R.ok(userService.getPoolNameList(user));
}
}

@ -0,0 +1,240 @@
package com.op.system.domain;
import com.op.common.core.annotation.Excel;
import com.op.common.core.web.domain.BaseEntity;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
/**
* sys_datasource
*
* @author sf
* @date 2023-05-18
*/
public class SysDatasource extends BaseEntity
{
private static final long serialVersionUID = 1L;
/** 主键 */
private String id;
/** 地址 */
@Excel(name = "地址")
private String url;
/** ip:端口 */
@Excel(name = "ip:端口")
private String ipPort;
/** 用户名 */
@Excel(name = "用户名")
private String userName;
/** 密码 */
@Excel(name = "密码")
private String password;
/** 驱动 */
@Excel(name = "驱动")
private String driveClassName;
/** 数据源标识 */
@Excel(name = "数据源标识")
private String poolName;
/** 数据源名称 */
@Excel(name = "数据源名称")
private String parkName;
/** 帐号状态0正常 */
@Excel(name = "帐号状态", readConverterExp = "帐号状态0正常")
private String status;
/** 删除标志0代表存在 */
private String delFlag;
/** 数据所属部门 */
@Excel(name = "数据所属部门")
private Long deptId;
private String deptName;
/** 0未创建 */
@Excel(name = "0未创建")
private String schemaCreated;
private String datasourceId;
private String userId;
private String nickName;
private String systemType;
public String getDeptName() {
return deptName;
}
public void setDeptName(String deptName) {
this.deptName = deptName;
}
public String getSystemType() {
return systemType;
}
public void setSystemType(String systemType) {
this.systemType = systemType;
}
public String getDatasourceId() {
return datasourceId;
}
public void setDatasourceId(String datasourceId) {
this.datasourceId = datasourceId;
}
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
public String getNickName() {
return nickName;
}
public void setNickName(String nickName) {
this.nickName = nickName;
}
public void setId(String id)
{
this.id = id;
}
public String getId()
{
return id;
}
public void setUrl(String url)
{
this.url = url;
}
public String getUrl()
{
return url;
}
public void setIpPort(String ipPort)
{
this.ipPort = ipPort;
}
public String getIpPort()
{
return ipPort;
}
public void setUserName(String userName)
{
this.userName = userName;
}
public String getUserName()
{
return userName;
}
public void setPassword(String password)
{
this.password = password;
}
public String getPassword()
{
return password;
}
public void setDriveClassName(String driveClassName)
{
this.driveClassName = driveClassName;
}
public String getDriveClassName()
{
return driveClassName;
}
public void setPoolName(String poolName)
{
this.poolName = poolName;
}
public String getPoolName()
{
return poolName;
}
public void setParkName(String parkName)
{
this.parkName = parkName;
}
public String getParkName()
{
return parkName;
}
public void setStatus(String status)
{
this.status = status;
}
public String getStatus()
{
return status;
}
public void setDelFlag(String delFlag)
{
this.delFlag = delFlag;
}
public String getDelFlag()
{
return delFlag;
}
public void setDeptId(Long deptId)
{
this.deptId = deptId;
}
public Long getDeptId()
{
return deptId;
}
public void setSchemaCreated(String schemaCreated)
{
this.schemaCreated = schemaCreated;
}
public String getSchemaCreated()
{
return schemaCreated;
}
@Override
public String toString() {
return new ToStringBuilder(this,ToStringStyle.MULTI_LINE_STYLE)
.append("id", getId())
.append("url", getUrl())
.append("ipPort", getIpPort())
.append("userName", getUserName())
.append("password", getPassword())
.append("driveClassName", getDriveClassName())
.append("poolName", getPoolName())
.append("parkName", getParkName())
.append("status", getStatus())
.append("delFlag", getDelFlag())
.append("createBy", getCreateBy())
.append("createTime", getCreateTime())
.append("updateBy", getUpdateBy())
.append("updateTime", getUpdateTime())
.append("remark", getRemark())
.append("deptId", getDeptId())
.append("schemaCreated", getSchemaCreated())
.toString();
}
}

@ -0,0 +1,78 @@
package com.op.system.mapper;
import com.op.system.api.domain.SysUser;
import com.op.system.domain.SysDatasource;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import java.util.List;
/**
* Mapper
*
* @author sf
* @date 2023-05-18
*/
@Mapper
public interface SysDatasourceMapper
{
/**
*
*
* @param id
* @return
*/
public SysDatasource selectSysDatasourceById(String id);
/**
*
*
* @param sysDatasource
* @return
*/
public List<SysDatasource> selectSysDatasourceList(SysDatasource sysDatasource);
/**
*
*
* @param sysDatasource
* @return
*/
public int insertSysDatasource(SysDatasource sysDatasource);
/**
*
*
* @param sysDatasource
* @return
*/
public int updateSysDatasource(SysDatasource sysDatasource);
/**
*
*
* @param id
* @return
*/
public int deleteSysDatasourceById(String id);
/**
*
*
* @param ids
* @return
*/
public int deleteSysDatasourceByIds(String[] ids);
List<SysDatasource> selectAllocatedList(SysDatasource sysDatasource);
List<SysUser> selectUnallocatedList(SysUser user);
int batchUserDB(List<SysDatasource> list);
int deleteUserRoleInfo(SysDatasource sysDatasource);
int deleteUserRoleInfos(@Param("datasourceId") String datasourceId, @Param("userIds") Long[] userIds);
List<SysDatasource> selectDBListByDept(SysDatasource qo);
}

@ -1,18 +1,20 @@
package com.op.system.mapper;
import java.util.List;
import java.util.Map;
import org.apache.ibatis.annotations.Param;
import com.op.system.api.domain.SysUser;
/**
*
*
*
* @author OP
*/
public interface SysUserMapper {
/**
*
*
*
* @param sysUser
* @return
*/
@ -20,7 +22,7 @@ public interface SysUserMapper {
/**
*
*
*
* @param user
* @return
*/
@ -28,7 +30,7 @@ public interface SysUserMapper {
/**
*
*
*
* @param user
* @return
*/
@ -36,7 +38,7 @@ public interface SysUserMapper {
/**
*
*
*
* @param userName
* @return
*/
@ -44,7 +46,7 @@ public interface SysUserMapper {
/**
* ID
*
*
* @param userId ID
* @return
*/
@ -52,7 +54,7 @@ public interface SysUserMapper {
/**
*
*
*
* @param user
* @return
*/
@ -60,7 +62,7 @@ public interface SysUserMapper {
/**
*
*
*
* @param user
* @return
*/
@ -68,7 +70,7 @@ public interface SysUserMapper {
/**
*
*
*
* @param userName
* @param avatar
* @return
@ -77,7 +79,7 @@ public interface SysUserMapper {
/**
*
*
*
* @param userName
* @param password
* @return
@ -86,7 +88,7 @@ public interface SysUserMapper {
/**
* ID
*
*
* @param userId ID
* @return
*/
@ -94,7 +96,7 @@ public interface SysUserMapper {
/**
*
*
*
* @param userIds ID
* @return
*/
@ -102,7 +104,7 @@ public interface SysUserMapper {
/**
*
*
*
* @param userName
* @return
*/
@ -123,4 +125,6 @@ public interface SysUserMapper {
* @return
*/
public SysUser checkEmailUnique(String email);
public List<Map<String, String>> getPoolNameList(SysUser user);
}

@ -0,0 +1,77 @@
package com.op.system.service;
import com.op.system.api.domain.SysUser;
import com.op.system.domain.SysDatasource;
import java.util.List;
/**
* Service
*
* @author sf
* @date 2023-05-18
*/
public interface ISysDatasourceService
{
/**
*
*
* @param id
* @return
*/
public SysDatasource selectSysDatasourceById(String id);
/**
*
*
* @param sysDatasource
* @return
*/
public List<SysDatasource> selectSysDatasourceList(SysDatasource sysDatasource);
/**
*
*
* @param sysDatasource
* @return
*/
public int insertSysDatasource(SysDatasource sysDatasource);
/**
*
*
* @param sysDatasource
* @return
*/
public int updateSysDatasource(SysDatasource sysDatasource);
/**
*
*
* @param ids
* @return
*/
public int deleteSysDatasourceByIds(String[] ids);
/**
*
*
* @param id
* @return
*/
public int deleteSysDatasourceById(String id);
List<SysDatasource> selectAllocatedList(SysDatasource sysDatasource);
List<SysUser> selectUnallocatedList(SysUser user);
void checkRoleDataScope(String datasourceId);
int insertAuthUsers(String datasourceId, Long[] userIds);
int deleteAuthUser(SysDatasource sysDatasource);
int deleteAuthUsers(String datasourceId, Long[] userIds);
String changeDBStatus(SysDatasource sysDatasource);
}

@ -1,18 +1,20 @@
package com.op.system.service;
import java.util.List;
import java.util.Map;
import java.util.Set;
import com.op.system.api.domain.SysUser;
/**
*
*
*
* @author OP
*/
public interface ISysPermissionService {
/**
*
*
*
* @param userId Id
* @return
*/
@ -20,9 +22,12 @@ public interface ISysPermissionService {
/**
*
*
*
* @param userId Id
* @return
*/
public Set<String> getMenuPermission(SysUser user);
// 用户工厂数据源
public List<Map<String,String>> getPoolNameList(SysUser sysUser);
}

@ -1,17 +1,19 @@
package com.op.system.service;
import java.util.List;
import java.util.Map;
import com.op.system.api.domain.SysUser;
/**
*
*
*
* @author OP
*/
public interface ISysUserService {
/**
*
*
*
* @param user
* @return
*/
@ -19,7 +21,7 @@ public interface ISysUserService {
/**
*
*
*
* @param user
* @return
*/
@ -27,7 +29,7 @@ public interface ISysUserService {
/**
*
*
*
* @param user
* @return
*/
@ -35,7 +37,7 @@ public interface ISysUserService {
/**
*
*
*
* @param userName
* @return
*/
@ -43,7 +45,7 @@ public interface ISysUserService {
/**
* ID
*
*
* @param userId ID
* @return
*/
@ -51,7 +53,7 @@ public interface ISysUserService {
/**
* ID
*
*
* @param userName
* @return
*/
@ -59,7 +61,7 @@ public interface ISysUserService {
/**
* ID
*
*
* @param userName
* @return
*/
@ -67,7 +69,7 @@ public interface ISysUserService {
/**
*
*
*
* @param user
* @return
*/
@ -91,21 +93,21 @@ public interface ISysUserService {
/**
*
*
*
* @param user
*/
public void checkUserAllowed(SysUser user);
/**
*
*
*
* @param userId id
*/
public void checkUserDataScope(Long userId);
/**
*
*
*
* @param user
* @return
*/
@ -113,7 +115,7 @@ public interface ISysUserService {
/**
*
*
*
* @param user
* @return
*/
@ -121,7 +123,7 @@ public interface ISysUserService {
/**
*
*
*
* @param user
* @return
*/
@ -129,7 +131,7 @@ public interface ISysUserService {
/**
*
*
*
* @param userId ID
* @param roleIds
*/
@ -137,7 +139,7 @@ public interface ISysUserService {
/**
*
*
*
* @param user
* @return
*/
@ -145,7 +147,7 @@ public interface ISysUserService {
/**
*
*
*
* @param user
* @return
*/
@ -153,7 +155,7 @@ public interface ISysUserService {
/**
*
*
*
* @param userName
* @param avatar
* @return
@ -162,7 +164,7 @@ public interface ISysUserService {
/**
*
*
*
* @param user
* @return
*/
@ -170,7 +172,7 @@ public interface ISysUserService {
/**
*
*
*
* @param userName
* @param password
* @return
@ -179,7 +181,7 @@ public interface ISysUserService {
/**
* ID
*
*
* @param userId ID
* @return
*/
@ -187,7 +189,7 @@ public interface ISysUserService {
/**
*
*
*
* @param userIds ID
* @return
*/
@ -195,11 +197,15 @@ public interface ISysUserService {
/**
*
*
*
* @param userList
* @param isUpdateSupport
* @param operName
* @return
*/
public String importUser(List<SysUser> userList, Boolean isUpdateSupport, String operName);
// 查询用户可见数据源
public List<Map<String, String>> getPoolNameList(SysUser user);
}

@ -0,0 +1,224 @@
package com.op.system.service.impl;
import com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DataSourceProperty;
import com.op.common.core.utils.DateUtils;
import com.op.common.core.utils.StringUtils;
import com.op.common.core.utils.uuid.IdUtils;
import com.op.common.datasource.creator.DynamicDatasourceCreator;
import com.op.system.api.domain.SysUser;
import com.op.system.domain.SysDatasource;
import com.op.system.mapper.SysDatasourceMapper;
import com.op.system.service.ISysDatasourceService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
/**
* Service
*
* @author sf
* @date 2023-05-18
*/
@Service
public class SysDatasourceServiceImpl implements ISysDatasourceService {
private static final Logger log = LoggerFactory.getLogger(SysDatasourceServiceImpl.class);
@Autowired
private SysDatasourceMapper sysDatasourceMapper;
@Value("${sqlserve.prev}")
private String mysqlPrev;
@Value("${sqlserve.affter}")
private String mysqlAffter;
@Value("${sqlserve.poolName.prev}")
private String poolNamePrev;
@Value("${sqlserve.driver}")
private String driver;
@Resource
private DynamicDatasourceCreator dynamicDatasourceCreator;
/**
*
*
* @param id
* @return
*/
@Override
public SysDatasource selectSysDatasourceById(String id) {
return sysDatasourceMapper.selectSysDatasourceById(id);
}
/**
*
*
* @param sysDatasource
* @return
*/
@Override
public List<SysDatasource> selectSysDatasourceList(SysDatasource sysDatasource) {
return sysDatasourceMapper.selectSysDatasourceList(sysDatasource);
}
/**
*
*
* @param sysDatasource
* @return
*/
@Override
public int insertSysDatasource(SysDatasource sysDatasource) {
sysDatasource.setCreateTime(DateUtils.getNowDate());
sysDatasource.setId(IdUtils.randomUUID().replace("-",""));
sysDatasource.setDriveClassName(driver);
if(StringUtils.isNotEmpty(sysDatasource.getIpPort())){
sysDatasource.setUrl(mysqlPrev+sysDatasource.getIpPort().split("/")[0]
+mysqlAffter+sysDatasource.getIpPort().split("/")[1]);
}
if(sysDatasource.getDeptId()!=null){
if(StringUtils.isNotEmpty(sysDatasource.getSystemType())){
sysDatasource.setPoolName(poolNamePrev+"_"+sysDatasource.getSystemType()+"_"+sysDatasource.getDeptId());
}else{
sysDatasource.setPoolName(poolNamePrev+"_"+sysDatasource.getDeptId());
}
}
return sysDatasourceMapper.insertSysDatasource(sysDatasource);
}
/**
*
*
* @param sysDatasource
* @return
*/
@Override
public int updateSysDatasource(SysDatasource sysDatasource) {
//修改之后status->1 不生效,需要重新测试链接才生效
sysDatasource.setStatus("1");
sysDatasource.setUpdateTime(DateUtils.getNowDate());
return sysDatasourceMapper.updateSysDatasource(sysDatasource);
}
/**
*
*
* @param ids
* @return
*/
@Override
public int deleteSysDatasourceByIds(String[] ids) {
return sysDatasourceMapper.deleteSysDatasourceByIds(ids);
}
/**
*
*
* @param id
* @return
*/
@Override
public int deleteSysDatasourceById(String id) {
return sysDatasourceMapper.deleteSysDatasourceById(id);
}
@Override
public List<SysDatasource> selectAllocatedList(SysDatasource sysDatasource) {
return sysDatasourceMapper.selectAllocatedList(sysDatasource);
}
@Override
public List<SysUser> selectUnallocatedList(SysUser user) {
return sysDatasourceMapper.selectUnallocatedList(user);
}
@Override
public void checkRoleDataScope(String datasourceId) {
// if (!SysUser.isAdmin(SecurityUtils.getUserId())) {
// SysRole role = new SysRole();
// role.setRoleId(roleId);
// List<SysRole> roles = SpringUtils.getAopProxy(this).selectRoleList(role);
// if (StringUtils.isEmpty(roles)) {
// throw new ServiceException("没有权限访问角色数据!");
// }
// }
}
@Override
public int insertAuthUsers(String datasourceId, Long[] userIds) {
// 新增用户与数据源管理
List<SysDatasource> list = new ArrayList<SysDatasource>();
for (Long userId : userIds) {
SysDatasource ur = new SysDatasource();
ur.setUserId(userId.toString());
ur.setDatasourceId(datasourceId.toString());
list.add(ur);
}
return sysDatasourceMapper.batchUserDB(list);
}
@Override
public int deleteAuthUser(SysDatasource sysDatasource) {
return sysDatasourceMapper.deleteUserRoleInfo(sysDatasource);
}
@Override
public int deleteAuthUsers(String datasourceId, Long[] userIds) {
return sysDatasourceMapper.deleteUserRoleInfos(datasourceId, userIds);
}
@Override
public String changeDBStatus(SysDatasource sd) {
SysDatasource sysDatasource = sysDatasourceMapper.selectSysDatasourceById(sd.getDatasourceId());
if(sysDatasource == null){
return "数据库链接不存在";
}
try {
Class.forName(sysDatasource.getDriveClassName());
Connection conn = DriverManager.getConnection(sysDatasource.getUrl(),
sysDatasource.getUserName(),
sysDatasource.getPassword());
if (conn != null) {
if (conn.isClosed()) {
log.error("[{}]数据库连接检测异常:[{}]",sysDatasource.getPoolName());
return "数据库链接异常,请检查数据库基本信息。";//假如打不开数据库链接则跳过初始化数据库
} else {
//说明数据库可以正常连接,关闭测试连接
conn.close();
log.info("[{}]数据库连接检测正常", sysDatasource.getPoolName());
}
} else {
log.error("[{}]数据库连接检测异常:[{}]", sysDatasource.getPoolName());
return "数据库链接异常,请检查数据库基本信息。";
}
sysDatasource.setUpdateTime(DateUtils.getNowDate());
sysDatasource.setStatus("0");//激活
sysDatasourceMapper.updateSysDatasource(sysDatasource);
DataSourceProperty dataSourceProperty = new DataSourceProperty();
dataSourceProperty.setUrl(sysDatasource.getUrl());
dataSourceProperty.setUsername(sysDatasource.getUserName());
dataSourceProperty.setPassword(sysDatasource.getPassword());
dataSourceProperty.setDriverClassName(sysDatasource.getDriveClassName());
dataSourceProperty.setPoolName(sysDatasource.getPoolName());// 这是数据源的key
dataSourceProperty.setLazy(false);
dynamicDatasourceCreator.createDynamicDataSource(dataSourceProperty);
return "数据库链接测试成功,数据库链接状态正常";
}catch (ClassNotFoundException | SQLException classNotFoundException){
return "数据库链接异常,请检查数据库基本信息。";
}
}
}

@ -2,7 +2,10 @@ package com.op.system.service.impl;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import com.op.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.op.system.api.domain.SysRole;
@ -13,7 +16,7 @@ import com.op.system.service.ISysRoleService;
/**
*
*
*
* @author OP
*/
@Service
@ -23,10 +26,11 @@ public class SysPermissionServiceImpl implements ISysPermissionService {
@Autowired
private ISysMenuService menuService;
@Autowired
private ISysUserService userService;
/**
*
*
*
* @param userId Id
* @return
*/
@ -44,7 +48,7 @@ public class SysPermissionServiceImpl implements ISysPermissionService {
/**
*
*
*
* @param userId Id
* @return
*/
@ -69,4 +73,8 @@ public class SysPermissionServiceImpl implements ISysPermissionService {
}
return perms;
}
@Override
public List<Map<String, String>> getPoolNameList(SysUser user) {
return userService.getPoolNameList(user);
}
}

@ -2,6 +2,7 @@ package com.op.system.service.impl;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import javax.validation.Validator;
import org.slf4j.Logger;
@ -32,7 +33,7 @@ import com.op.system.service.ISysUserService;
/**
*
*
*
* @author OP
*/
@Service
@ -62,7 +63,7 @@ public class SysUserServiceImpl implements ISysUserService {
/**
*
*
*
* @param user
* @return
*/
@ -74,7 +75,7 @@ public class SysUserServiceImpl implements ISysUserService {
/**
*
*
*
* @param user
* @return
*/
@ -86,7 +87,7 @@ public class SysUserServiceImpl implements ISysUserService {
/**
*
*
*
* @param user
* @return
*/
@ -98,7 +99,7 @@ public class SysUserServiceImpl implements ISysUserService {
/**
*
*
*
* @param userName
* @return
*/
@ -109,7 +110,7 @@ public class SysUserServiceImpl implements ISysUserService {
/**
* ID
*
*
* @param userId ID
* @return
*/
@ -120,7 +121,7 @@ public class SysUserServiceImpl implements ISysUserService {
/**
*
*
*
* @param userName
* @return
*/
@ -135,7 +136,7 @@ public class SysUserServiceImpl implements ISysUserService {
/**
*
*
*
* @param userName
* @return
*/
@ -150,7 +151,7 @@ public class SysUserServiceImpl implements ISysUserService {
/**
*
*
*
* @param user
* @return
*/
@ -198,7 +199,7 @@ public class SysUserServiceImpl implements ISysUserService {
/**
*
*
*
* @param user
*/
@Override
@ -210,7 +211,7 @@ public class SysUserServiceImpl implements ISysUserService {
/**
*
*
*
* @param userId id
*/
@Override
@ -227,7 +228,7 @@ public class SysUserServiceImpl implements ISysUserService {
/**
*
*
*
* @param user
* @return
*/
@ -245,7 +246,7 @@ public class SysUserServiceImpl implements ISysUserService {
/**
*
*
*
* @param user
* @return
*/
@ -256,7 +257,7 @@ public class SysUserServiceImpl implements ISysUserService {
/**
*
*
*
* @param user
* @return
*/
@ -277,7 +278,7 @@ public class SysUserServiceImpl implements ISysUserService {
/**
*
*
*
* @param userId ID
* @param roleIds
*/
@ -290,7 +291,7 @@ public class SysUserServiceImpl implements ISysUserService {
/**
*
*
*
* @param user
* @return
*/
@ -301,7 +302,7 @@ public class SysUserServiceImpl implements ISysUserService {
/**
*
*
*
* @param user
* @return
*/
@ -312,7 +313,7 @@ public class SysUserServiceImpl implements ISysUserService {
/**
*
*
*
* @param userName
* @param avatar
* @return
@ -324,7 +325,7 @@ public class SysUserServiceImpl implements ISysUserService {
/**
*
*
*
* @param user
* @return
*/
@ -335,7 +336,7 @@ public class SysUserServiceImpl implements ISysUserService {
/**
*
*
*
* @param userName
* @param password
* @return
@ -347,7 +348,7 @@ public class SysUserServiceImpl implements ISysUserService {
/**
*
*
*
* @param user
*/
public void insertUserRole(SysUser user) {
@ -356,7 +357,7 @@ public class SysUserServiceImpl implements ISysUserService {
/**
*
*
*
* @param user
*/
public void insertUserPost(SysUser user) {
@ -376,7 +377,7 @@ public class SysUserServiceImpl implements ISysUserService {
/**
*
*
*
* @param userId ID
* @param roleIds
*/
@ -396,7 +397,7 @@ public class SysUserServiceImpl implements ISysUserService {
/**
* ID
*
*
* @param userId ID
* @return
*/
@ -412,7 +413,7 @@ public class SysUserServiceImpl implements ISysUserService {
/**
*
*
*
* @param userIds ID
* @return
*/
@ -432,7 +433,7 @@ public class SysUserServiceImpl implements ISysUserService {
/**
*
*
*
* @param userList
* @param isUpdateSupport
* @param operName
@ -487,5 +488,8 @@ public class SysUserServiceImpl implements ISysUserService {
}
return successMsg.toString();
}
@Override
public List<Map<String, String>> getPoolNameList(SysUser user) {
return userMapper.getPoolNameList(user);
}
}

@ -29,3 +29,12 @@ spring:
# 共享配置
shared-configs:
- application-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}
sqlserve:
#数据库链接前缀
prev: jdbc:sqlserver://
#数据库链接后缀
affter: :1433;DatabaseName=
driver: com.microsoft.sqlserver.jdbc.SQLServerDriver
#数据标识前缀
poolName:
prev: factory

@ -0,0 +1,191 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.op.system.mapper.SysDatasourceMapper">
<resultMap type="SysDatasource" id="SysDatasourceResult">
<result property="id" column="id" />
<result property="url" column="url" />
<result property="ipPort" column="ip_port" />
<result property="userName" column="user_name" />
<result property="password" column="password" />
<result property="driveClassName" column="drive_class_name" />
<result property="poolName" column="pool_name" />
<result property="parkName" column="park_name" />
<result property="status" column="status" />
<result property="delFlag" column="del_flag" />
<result property="createBy" column="create_by" />
<result property="createTime" column="create_time" />
<result property="updateBy" column="update_by" />
<result property="updateTime" column="update_time" />
<result property="remark" column="remark" />
<result property="deptId" column="dept_id" />
<result property="schemaCreated" column="schema_created" />
<result property="deptName" column="dept_name" />
<result property="systemType" column="system_type" />
</resultMap>
<sql id="selectSysDatasourceVo">
select id, url, ip_port, user_name, password, drive_class_name, pool_name, park_name,
status, del_flag, create_by, create_time, update_by, update_time, remark,system_type,
dept_id, schema_created from sys_datasource
</sql>
<select id="selectSysDatasourceList" parameterType="SysDatasource" resultMap="SysDatasourceResult">
select sd.id, sd.url, sd.ip_port, sd.user_name, sd.password, sd.drive_class_name, sd.pool_name, sd.park_name,
sd.status, sd.del_flag, sd.create_by, sd.create_time, sd.update_by, sd.update_time, sd.remark,sd.system_type,
sd.dept_id,d.dept_name, sd.schema_created from sys_datasource sd
left join sys_dept d on sd.dept_id = d.dept_id
<where>
<if test="url != null and url != ''"> and url = #{url}</if>
<if test="ipPort != null and ipPort != ''"> and ip_port like concat('%', #{ipPort}, '%')</if>
<if test="userName != null and userName != ''"> and user_name like concat('%', #{userName}, '%')</if>
<if test="password != null and password != ''"> and password = #{password}</if>
<if test="driveClassName != null and driveClassName != ''"> and drive_class_name like concat('%', #{driveClassName}, '%')</if>
<if test="poolName != null and poolName != ''"> and pool_name like concat('%', #{poolName}, '%')</if>
<if test="parkName != null and parkName != ''"> and park_name like concat('%', #{parkName}, '%')</if>
<if test="status != null and status != ''"> and status = #{status}</if>
<if test="deptId != null "> and dept_id = #{deptId}</if>
<if test="schemaCreated != null and schemaCreated != ''"> and schema_created = #{schemaCreated}</if>
</where>
</select>
<select id="selectSysDatasourceById" parameterType="String" resultMap="SysDatasourceResult">
<include refid="selectSysDatasourceVo"/>
where id = #{id}
</select>
<select id="selectAllocatedList" resultType="com.op.system.domain.SysDatasource">
select sd.id,
sd.ip_port ipPort,
sd.pool_name poolName,
sud.user_id userId,
su.user_name userName,
su.nick_name nickName
from sys_datasource sd
left join sys_user_datasource sud on sd.id = sud.datasource_id
left join sys_user su on su.user_id = sud.user_id
where sud.user_id is not null
<if test="nickName != null and nickName != ''">
AND su.nick_name like concat('%', #{nickName}, '%')
</if>
<if test="datasourceId != null and datasourceId != ''">
AND sd.id = #{datasourceId}
</if>
</select>
<select id="selectUnallocatedList" resultType="com.op.system.api.domain.SysUser">
select distinct u.user_id userId, u.dept_id deptId, u.user_name userName, u.nick_name nickName,
u.email, u.phonenumber, u.status, u.create_time createTime
from sys_user u
left join sys_user_datasource ur on u.user_id = ur.user_id
left join sys_datasource r on r.id = ur.datasource_id
where u.del_flag = '0' and (r.id != #{datasourceId} or r.id IS NULL)
and u.user_id not in (select ur.user_id from sys_user_datasource ur where ur.datasource_id = #{datasourceId})
<if test="userName != null and userName != ''">
AND u.user_name like concat('%', #{userName}, '%')
</if>
<if test="phonenumber != null and phonenumber != ''">
AND u.phonenumber like concat('%', #{phonenumber}, '%')
</if>
</select>
<select id="selectDBListByDept" resultType="com.op.system.domain.SysDatasource">
select id
from sys_datasource
where del_flag = 0 and dept_id = #{deptId}
</select>
<insert id="insertSysDatasource" parameterType="SysDatasource">
insert into sys_datasource
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="id != null">id,</if>
<if test="url != null and url != ''">url,</if>
<if test="ipPort != null">ip_port,</if>
<if test="userName != null and userName != ''">user_name,</if>
<if test="password != null and password != ''">password,</if>
<if test="driveClassName != null and driveClassName != ''">drive_class_name,</if>
<if test="poolName != null and poolName != ''">pool_name,</if>
<if test="parkName != null">park_name,</if>
<if test="status != null">status,</if>
<if test="delFlag != null">del_flag,</if>
<if test="createBy != null">create_by,</if>
<if test="createTime != null">create_time,</if>
<if test="updateBy != null">update_by,</if>
<if test="updateTime != null">update_time,</if>
<if test="remark != null">remark,</if>
<if test="deptId != null">dept_id,</if>
<if test="schemaCreated != null">schema_created,</if>
<if test="systemType != null">system_type,</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="id != null">#{id},</if>
<if test="url != null and url != ''">#{url},</if>
<if test="ipPort != null">#{ipPort},</if>
<if test="userName != null and userName != ''">#{userName},</if>
<if test="password != null and password != ''">#{password},</if>
<if test="driveClassName != null and driveClassName != ''">#{driveClassName},</if>
<if test="poolName != null and poolName != ''">#{poolName},</if>
<if test="parkName != null">#{parkName},</if>
<if test="status != null">#{status},</if>
<if test="delFlag != null">#{delFlag},</if>
<if test="createBy != null">#{createBy},</if>
<if test="createTime != null">#{createTime},</if>
<if test="updateBy != null">#{updateBy},</if>
<if test="updateTime != null">#{updateTime},</if>
<if test="remark != null">#{remark},</if>
<if test="deptId != null">#{deptId},</if>
<if test="schemaCreated != null">#{schemaCreated},</if>
<if test="systemType != null">#{systemType},</if>
</trim>
</insert>
<insert id="batchUserDB">
insert into sys_user_datasource(user_id, datasource_id) values
<foreach item="item" index="index" collection="list" separator=",">
(#{item.userId},#{item.datasourceId})
</foreach>
</insert>
<update id="updateSysDatasource" parameterType="SysDatasource">
update sys_datasource
<trim prefix="SET" suffixOverrides=",">
<if test="url != null and url != ''">url = #{url},</if>
<if test="ipPort != null">ip_port = #{ipPort},</if>
<if test="userName != null and userName != ''">user_name = #{userName},</if>
<if test="password != null and password != ''">password = #{password},</if>
<if test="driveClassName != null and driveClassName != ''">drive_class_name = #{driveClassName},</if>
<if test="poolName != null and poolName != ''">pool_name = #{poolName},</if>
<if test="parkName != null">park_name = #{parkName},</if>
<if test="status != null">status = #{status},</if>
<if test="delFlag != null">del_flag = #{delFlag},</if>
<if test="createBy != null">create_by = #{createBy},</if>
<if test="createTime != null">create_time = #{createTime},</if>
<if test="updateBy != null">update_by = #{updateBy},</if>
<if test="updateTime != null">update_time = #{updateTime},</if>
<if test="remark != null">remark = #{remark},</if>
<if test="deptId != null">dept_id = #{deptId},</if>
<if test="schemaCreated != null">schema_created = #{schemaCreated},</if>
<if test="systemType != null">system_type = #{systemType},</if>
</trim>
where id = #{id}
</update>
<delete id="deleteSysDatasourceById" parameterType="String">
delete from sys_datasource where id = #{id}
</delete>
<delete id="deleteSysDatasourceByIds" parameterType="String">
delete from sys_datasource where id in
<foreach item="id" collection="array" open="(" separator="," close=")">
#{id}
</foreach>
</delete>
<delete id="deleteUserRoleInfo">
delete from sys_user_datasource where user_id=#{userId} and datasource_id=#{datasourceId}
</delete>
<delete id="deleteUserRoleInfos">
delete from sys_user_datasource where datasource_id=#{datasourceId} and user_id in
<foreach collection="userIds" item="userId" open="(" separator="," close=")">
#{userId}
</foreach>
</delete>
</mapper>

@ -217,5 +217,19 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
#{userId}
</foreach>
</delete>
<select id="getPoolNameList" parameterType="SysUser" resultType="java.util.Map">
SELECT
DISTINCT s.pool_name as poolName,
s.park_name as parkName,
url,
user_name as userName,
password,
drive_class_name as driveClassName
FROM sys_user_datasource ud
LEFT JOIN sys_datasource s ON ud.datasource_id = s.id
WHERE status=0
<if test="userId != '1'">
and ud.user_id = #{userId}
</if>
</select>
</mapper>

@ -0,0 +1,51 @@
package com.op.wms.config;
import com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DataSourceProperty;
import com.op.common.core.domain.R;
import com.op.common.datasource.creator.DynamicDatasourceCreator;
import com.op.system.api.RemoteUserService;
import com.op.system.api.domain.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
/**
*
*
* @ClassName: DynamicDatasource
* @Description: TODO
* @author shichangzhou
* @date 2023419 1:01:30
*/
@Component
public class DynamicDatasource {
@Resource
private DynamicDatasourceCreator dynamicDatasourceCreator;
@Autowired
private RemoteUserService remoteUserService;
@PostConstruct
public void init() {
// 加载sf-cloud库的sys_datasource
SysUser sysUser = new SysUser();
sysUser.setUserId(1L);
R<List<Map<String, String>>> dateSources0 = remoteUserService.getPoolNameList(sysUser);
List<Map<String, String>> dateSources = dateSources0.getData();
for (Map<String, String> dateSource : dateSources) {
DataSourceProperty sdp = new DataSourceProperty();
sdp.setUrl(dateSource.get("url"));
sdp.setUsername(dateSource.get("userName"));
sdp.setPassword(dateSource.get("password"));
sdp.setDriverClassName(dateSource.get("driveClassName"));
sdp.setPoolName(dateSource.get("poolName"));// 这是数据源的key
sdp.setLazy(false);
dynamicDatasourceCreator.createDynamicDataSource(sdp);
}
}
}
Loading…
Cancel
Save