init
This commit is contained in:
@@ -0,0 +1,40 @@
|
|||||||
|
package com.ruoyi.dk.listener;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
||||||
|
import com.ruoyi.component.core.constant.TenantConstants;
|
||||||
|
import com.ruoyi.dk.domain.HomeSetting;
|
||||||
|
import com.ruoyi.dk.domain.LoansSetting;
|
||||||
|
import com.ruoyi.dk.service.HomeSettingService;
|
||||||
|
import com.ruoyi.dk.service.LoansSettingService;
|
||||||
|
import com.ruoyi.system.event.AddTenantEvent;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.context.event.EventListener;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@Component
|
||||||
|
public class AddTenantEventListener {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private HomeSettingService homeSettingService;
|
||||||
|
@Autowired
|
||||||
|
private LoansSettingService loansSettingService;
|
||||||
|
|
||||||
|
@EventListener
|
||||||
|
public void event(AddTenantEvent addTenantEvent){
|
||||||
|
String defaultTenantId = TenantConstants.DEFAULT_TENANT_ID;
|
||||||
|
List<HomeSetting> homeSettingList = homeSettingService.list(Wrappers.lambdaQuery(HomeSetting.class).eq(HomeSetting::getTenantId, defaultTenantId));
|
||||||
|
for (HomeSetting setting : homeSettingList) {
|
||||||
|
setting.setTenantId(addTenantEvent.getTenantId());
|
||||||
|
setting.setId(null);
|
||||||
|
}
|
||||||
|
List<LoansSetting> loansSettingList = loansSettingService.list(Wrappers.lambdaQuery(LoansSetting.class).eq(LoansSetting::getTenantId, defaultTenantId));
|
||||||
|
for (LoansSetting setting : loansSettingList) {
|
||||||
|
setting.setTenantId(addTenantEvent.getTenantId());
|
||||||
|
setting.setId(null);
|
||||||
|
}
|
||||||
|
homeSettingService.saveBatch(homeSettingList);
|
||||||
|
loansSettingService.saveBatch(loansSettingList);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -42,6 +42,11 @@ public class SysTenant extends BaseEntity {
|
|||||||
*/
|
*/
|
||||||
private String contactPhone;
|
private String contactPhone;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 前缀
|
||||||
|
*/
|
||||||
|
private String prefix;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 企业名称
|
* 企业名称
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -159,6 +159,11 @@ public class SysUser extends TenantEntity {
|
|||||||
@TableField(exist = false)
|
@TableField(exist = false)
|
||||||
private Long roleId;
|
private Long roleId;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 前缀
|
||||||
|
*/
|
||||||
|
private String prefix;
|
||||||
|
|
||||||
public SysUser(Long userId) {
|
public SysUser(Long userId) {
|
||||||
this.userId = userId;
|
this.userId = userId;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -52,9 +52,10 @@ public class SysTenantBo extends BaseEntity {
|
|||||||
/**
|
/**
|
||||||
* 用户名(创建系统用户)
|
* 用户名(创建系统用户)
|
||||||
*/
|
*/
|
||||||
@NotBlank(message = "用户名不能为空", groups = { AddGroup.class })
|
// @NotBlank(message = "用户名不能为空", groups = { AddGroup.class })
|
||||||
private String username;
|
// private String username;
|
||||||
|
@NotBlank(message = "用户名前缀不能为空", groups = {AddGroup.class})
|
||||||
|
private String prefix;
|
||||||
/**
|
/**
|
||||||
* 密码(创建系统用户)
|
* 密码(创建系统用户)
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -99,6 +99,8 @@ public class SysTenantVo implements Serializable {
|
|||||||
@ExcelProperty(value = "用户数量")
|
@ExcelProperty(value = "用户数量")
|
||||||
private Long accountCount;
|
private Long accountCount;
|
||||||
|
|
||||||
|
private String prefix;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 租户状态(0正常 1停用)
|
* 租户状态(0正常 1停用)
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -0,0 +1,8 @@
|
|||||||
|
package com.ruoyi.system.event;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
public class AddTenantEvent {
|
||||||
|
private String tenantId;
|
||||||
|
}
|
||||||
@@ -19,6 +19,7 @@ import com.ruoyi.component.mybatis.core.page.TableDataInfo;
|
|||||||
import com.ruoyi.system.domain.*;
|
import com.ruoyi.system.domain.*;
|
||||||
import com.ruoyi.system.domain.bo.SysTenantBo;
|
import com.ruoyi.system.domain.bo.SysTenantBo;
|
||||||
import com.ruoyi.system.domain.vo.SysTenantVo;
|
import com.ruoyi.system.domain.vo.SysTenantVo;
|
||||||
|
import com.ruoyi.system.event.AddTenantEvent;
|
||||||
import com.ruoyi.system.mapper.*;
|
import com.ruoyi.system.mapper.*;
|
||||||
import com.ruoyi.system.service.ISysTenantService;
|
import com.ruoyi.system.service.ISysTenantService;
|
||||||
import org.springframework.cache.annotation.CacheEvict;
|
import org.springframework.cache.annotation.CacheEvict;
|
||||||
@@ -129,6 +130,10 @@ public class SysTenantServiceImpl implements ISysTenantService {
|
|||||||
new LambdaQueryWrapper<SysTenant>().select(SysTenant::getTenantId));
|
new LambdaQueryWrapper<SysTenant>().select(SysTenant::getTenantId));
|
||||||
String tenantId = generateTenantId(tenantIds);
|
String tenantId = generateTenantId(tenantIds);
|
||||||
add.setTenantId(tenantId);
|
add.setTenantId(tenantId);
|
||||||
|
boolean exists = baseMapper.exists(Wrappers.lambdaQuery(SysTenant.class).eq(SysTenant::getPrefix, bo.getPrefix()));
|
||||||
|
if(exists){
|
||||||
|
throw new ServiceException("用户名前缀已经存在!");
|
||||||
|
}
|
||||||
boolean flag = baseMapper.insert(add) > 0;
|
boolean flag = baseMapper.insert(add) > 0;
|
||||||
if (!flag) {
|
if (!flag) {
|
||||||
throw new ServiceException("创建租户失败");
|
throw new ServiceException("创建租户失败");
|
||||||
@@ -152,12 +157,18 @@ public class SysTenantServiceImpl implements ISysTenantService {
|
|||||||
roleDept.setRoleId(roleId);
|
roleDept.setRoleId(roleId);
|
||||||
roleDept.setDeptId(deptId);
|
roleDept.setDeptId(deptId);
|
||||||
roleDeptMapper.insert(roleDept);
|
roleDeptMapper.insert(roleDept);
|
||||||
|
String adminUsername = bo.getPrefix()+"_admin";
|
||||||
|
// 检测用户是否存在
|
||||||
|
SysUser sysUser = userMapper.selectUserByUserName(adminUsername);
|
||||||
|
if(sysUser != null){
|
||||||
|
throw new ServiceException("平台超管已经存在!");
|
||||||
|
}
|
||||||
// 创建系统用户
|
// 创建系统用户
|
||||||
SysUser user = new SysUser();
|
SysUser user = new SysUser();
|
||||||
user.setTenantId(tenantId);
|
user.setTenantId(tenantId);
|
||||||
user.setUserName(bo.getUsername());
|
user.setPrefix(bo.getPrefix());
|
||||||
user.setNickName(bo.getUsername());
|
user.setUserName(bo.getPrefix()+"_admin");
|
||||||
|
user.setNickName("平台管理员");
|
||||||
user.setPassword(BCrypt.hashpw(bo.getPassword()));
|
user.setPassword(BCrypt.hashpw(bo.getPassword()));
|
||||||
user.setDeptId(deptId);
|
user.setDeptId(deptId);
|
||||||
userMapper.insert(user);
|
userMapper.insert(user);
|
||||||
@@ -196,6 +207,10 @@ public class SysTenantServiceImpl implements ISysTenantService {
|
|||||||
config.setTenantId(tenantId);
|
config.setTenantId(tenantId);
|
||||||
}
|
}
|
||||||
// configMapper.insertBatch(sysConfigList);
|
// configMapper.insertBatch(sysConfigList);
|
||||||
|
// 执行系统自定义的接口生成
|
||||||
|
AddTenantEvent event = new AddTenantEvent();
|
||||||
|
event.setTenantId(tenantId);
|
||||||
|
SpringUtils.context().publishEvent(event);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user