|
@@ -6,25 +6,22 @@ import cn.hutool.core.util.StrUtil;
|
|
import com.baomidou.dynamic.datasource.annotation.DS;
|
|
import com.baomidou.dynamic.datasource.annotation.DS;
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
|
|
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
|
|
|
|
+import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
|
|
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
|
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
import com.github.pagehelper.Page;
|
|
import com.github.pagehelper.Page;
|
|
-import com.zhongzheng.common.core.domain.entity.SysMenu;
|
|
|
|
-import com.zhongzheng.common.core.domain.entity.SysUser;
|
|
|
|
|
|
+import com.zhongzheng.common.core.domain.entity.*;
|
|
import com.zhongzheng.common.exception.CustomException;
|
|
import com.zhongzheng.common.exception.CustomException;
|
|
import com.zhongzheng.common.utils.DateUtils;
|
|
import com.zhongzheng.common.utils.DateUtils;
|
|
import com.zhongzheng.common.utils.SecurityUtils;
|
|
import com.zhongzheng.common.utils.SecurityUtils;
|
|
import com.zhongzheng.common.utils.ServletUtils;
|
|
import com.zhongzheng.common.utils.ServletUtils;
|
|
-import com.zhongzheng.modules.system.bo.SysTenantAddBo;
|
|
|
|
-import com.zhongzheng.modules.system.bo.SysTenantAdminBo;
|
|
|
|
-import com.zhongzheng.modules.system.bo.SysTenantEditBo;
|
|
|
|
-import com.zhongzheng.modules.system.bo.SysTenantQueryBo;
|
|
|
|
|
|
+import com.zhongzheng.modules.system.bo.*;
|
|
|
|
+import com.zhongzheng.modules.system.domain.SysConfig;
|
|
|
|
+import com.zhongzheng.modules.system.domain.SysRoleMenu;
|
|
import com.zhongzheng.modules.system.domain.SysTenant;
|
|
import com.zhongzheng.modules.system.domain.SysTenant;
|
|
|
|
+import com.zhongzheng.modules.system.mapper.SysRoleMenuMapper;
|
|
import com.zhongzheng.modules.system.mapper.SysTenantMapper;
|
|
import com.zhongzheng.modules.system.mapper.SysTenantMapper;
|
|
-import com.zhongzheng.modules.system.service.ISysMenuService;
|
|
|
|
-import com.zhongzheng.modules.system.service.ISysTenantService;
|
|
|
|
-import com.zhongzheng.modules.system.service.ISysUserService;
|
|
|
|
-import com.zhongzheng.modules.system.service.ISysWebService;
|
|
|
|
|
|
+import com.zhongzheng.modules.system.service.*;
|
|
import com.zhongzheng.modules.system.vo.SysTenantVo;
|
|
import com.zhongzheng.modules.system.vo.SysTenantVo;
|
|
import org.apache.commons.lang3.RandomStringUtils;
|
|
import org.apache.commons.lang3.RandomStringUtils;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
@@ -32,6 +29,7 @@ import org.springframework.stereotype.Service;
|
|
import org.springframework.transaction.annotation.Propagation;
|
|
import org.springframework.transaction.annotation.Propagation;
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
|
|
|
+import java.util.ArrayList;
|
|
import java.util.Collection;
|
|
import java.util.Collection;
|
|
import java.util.List;
|
|
import java.util.List;
|
|
import java.util.stream.Collectors;
|
|
import java.util.stream.Collectors;
|
|
@@ -52,9 +50,24 @@ public class SysTenantServiceImpl extends ServiceImpl<SysTenantMapper, SysTenant
|
|
@Autowired
|
|
@Autowired
|
|
private ISysMenuService menuService;
|
|
private ISysMenuService menuService;
|
|
|
|
|
|
|
|
+ @Autowired
|
|
|
|
+ private ISysRoleService roleService;
|
|
|
|
+
|
|
|
|
+ @Autowired
|
|
|
|
+ private SysRoleMenuMapper sysRoleMenuMapper;
|
|
|
|
+
|
|
@Autowired
|
|
@Autowired
|
|
private ISysWebService iSysWebService;
|
|
private ISysWebService iSysWebService;
|
|
|
|
|
|
|
|
+ @Autowired
|
|
|
|
+ private ISysConfigService iSysConfigService;
|
|
|
|
+
|
|
|
|
+ @Autowired
|
|
|
|
+ private ISysDictDataService iSysDictDataService;
|
|
|
|
+
|
|
|
|
+ @Autowired
|
|
|
|
+ private ISysDictTypeService iSysDictTypeService;
|
|
|
|
+
|
|
@Override
|
|
@Override
|
|
public SysTenantVo queryById(Long tenantId){
|
|
public SysTenantVo queryById(Long tenantId){
|
|
SysTenant db = this.baseMapper.selectById(tenantId);
|
|
SysTenant db = this.baseMapper.selectById(tenantId);
|
|
@@ -192,6 +205,12 @@ public class SysTenantServiceImpl extends ServiceImpl<SysTenantMapper, SysTenant
|
|
newMenus.forEach(item -> {
|
|
newMenus.forEach(item -> {
|
|
menuService.updateParentById(item);
|
|
menuService.updateParentById(item);
|
|
});
|
|
});
|
|
|
|
+
|
|
|
|
+ //新增分销角色
|
|
|
|
+ initRoles(newTenantId,tenantId);
|
|
|
|
+
|
|
|
|
+ //初始化配置和字典
|
|
|
|
+ initConfigAndDict(newTenantId,tenantId);
|
|
return newTenantId;
|
|
return newTenantId;
|
|
|
|
|
|
}
|
|
}
|
|
@@ -248,4 +267,202 @@ public class SysTenantServiceImpl extends ServiceImpl<SysTenantMapper, SysTenant
|
|
return list(new LambdaQueryWrapper<SysTenant>().eq(SysTenant::getStatus, 1));
|
|
return list(new LambdaQueryWrapper<SysTenant>().eq(SysTenant::getStatus, 1));
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ @Override
|
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
|
+ public void createTenantAdminOld(SysTenantAdminOldBo bo) {
|
|
|
|
+ if(Validator.isEmpty(bo.getTenantName())){
|
|
|
|
+ throw new CustomException("没有企业名称");
|
|
|
|
+ }
|
|
|
|
+ //中正后台企业ID
|
|
|
|
+ Long tenantId = 867735392558919680L;
|
|
|
|
+ //设置新机构
|
|
|
|
+ SysTenant sysTenant = getById(tenantId);
|
|
|
|
+ sysTenant.setTenantName(bo.getTenantName());
|
|
|
|
+ //生成tenantId
|
|
|
|
+// Long newTenantId = createTenantId();
|
|
|
|
+ Long newTenantId = bo.getTenantId();
|
|
|
|
+ sysTenant.setTenantId(newTenantId);
|
|
|
|
+ if (!save(sysTenant)){
|
|
|
|
+ throw new CustomException("创建企业失败");
|
|
|
|
+ }
|
|
|
|
+ //创建账号
|
|
|
|
+ SysUser admin = userService.selectUserByTenant("admin",tenantId);
|
|
|
|
+ admin.setTenantId(newTenantId);
|
|
|
|
+ admin.setUserId(null);
|
|
|
|
+
|
|
|
|
+ if (!userService.save(admin)){
|
|
|
|
+ throw new CustomException("创建后台账号失败");
|
|
|
|
+ }
|
|
|
|
+ //创建菜单
|
|
|
|
+ List<SysMenu> list = menuService.listSysMenuByTenant(tenantId);
|
|
|
|
+ if (CollectionUtils.isEmpty(list)){
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+ List<SysMenu> oldMenus = list.stream().map(item -> BeanUtil.toBean(item, SysMenu.class)).collect(Collectors.toList());
|
|
|
|
+ list.forEach(item -> {
|
|
|
|
+ item.setMenuId(null);
|
|
|
|
+ item.setTenantId(newTenantId);
|
|
|
|
+ });
|
|
|
|
+ if (!menuService.saveBatch(list)){
|
|
|
|
+ throw new CustomException("添加菜单失败");
|
|
|
|
+ }
|
|
|
|
+ //新菜单
|
|
|
|
+ List<SysMenu> newMenus = menuService.listSysMenuByTenant(newTenantId);
|
|
|
|
+ //匹配parent_id
|
|
|
|
+ for (SysMenu newMenu : newMenus) {
|
|
|
|
+ if (newMenu.getParentId() == 0){
|
|
|
|
+ continue;
|
|
|
|
+ }
|
|
|
|
+ List<SysMenu> collect = oldMenus.stream().filter(x -> x.getMenuName().equals(newMenu.getMenuName())
|
|
|
|
+ && x.getCreateTime().equals(newMenu.getCreateTime()) && x.getOrderNum().equals(newMenu.getOrderNum())).collect(Collectors.toList());
|
|
|
|
+ if (CollectionUtils.isEmpty(collect) || collect.size() > 1){
|
|
|
|
+ throw new CustomException("菜单匹配失败:"+newMenu.getMenuName());
|
|
|
|
+ }
|
|
|
|
+ SysMenu sysMenu = collect.stream().findFirst().orElse(null);
|
|
|
|
+ SysMenu parentMenu = oldMenus.stream().filter(x -> x.getMenuId().equals(sysMenu.getParentId())).findFirst().orElse(null);
|
|
|
|
+ List<SysMenu> collect2 = newMenus.stream().filter(x -> x.getMenuName().equals(parentMenu.getMenuName())
|
|
|
|
+ && x.getCreateTime().equals(parentMenu.getCreateTime()) && x.getOrderNum().equals(parentMenu.getOrderNum())).collect(Collectors.toList());
|
|
|
|
+ if (CollectionUtils.isEmpty(collect2) || collect2.size() > 1){
|
|
|
|
+ throw new CustomException("菜单匹配失败:"+newMenu.getMenuName());
|
|
|
|
+ }
|
|
|
|
+ newMenu.setParentId(collect2.get(0).getMenuId());
|
|
|
|
+ }
|
|
|
|
+ //修改父ID
|
|
|
|
+// menuService.updateBatchById(newMenus);
|
|
|
|
+ newMenus.forEach(item -> {
|
|
|
|
+ menuService.updateParentById(item);
|
|
|
|
+ });
|
|
|
|
+
|
|
|
|
+ //新增分销角色
|
|
|
|
+ initRoles(newTenantId,tenantId);
|
|
|
|
+
|
|
|
|
+ //初始化配置和字典
|
|
|
|
+ initConfigAndDict(newTenantId,tenantId);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private void initConfigAndDict(Long newTenantId, Long tenantId) {
|
|
|
|
+ SysConfig config = iSysConfigService.getSysConfigByKeyTenant("home.header", newTenantId);
|
|
|
|
+ if (ObjectUtils.isNull(config)){
|
|
|
|
+ SysConfig sysConfigByKeyTenant = iSysConfigService.getSysConfigByKeyTenant("home.header", tenantId);
|
|
|
|
+ sysConfigByKeyTenant.setTenantId(newTenantId);
|
|
|
|
+ sysConfigByKeyTenant.setConfigId(null);
|
|
|
|
+ iSysConfigService.save(sysConfigByKeyTenant);
|
|
|
|
+ }
|
|
|
|
+ //字典
|
|
|
|
+ List<SysDictType> dictTypes = iSysDictTypeService.getListByTenant(tenantId);
|
|
|
|
+ if (CollectionUtils.isEmpty(dictTypes)){
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+ List<SysDictType> dictTypeList = dictTypes.stream().map(item -> {
|
|
|
|
+ item.setDictId(null);
|
|
|
|
+ item.setTenantId(newTenantId);
|
|
|
|
+ return item;
|
|
|
|
+ }).collect(Collectors.toList());
|
|
|
|
+ iSysDictTypeService.saveBatch(dictTypeList);
|
|
|
|
+ List<SysDictData> dictDatas = iSysDictDataService.getListByTenant(tenantId);
|
|
|
|
+ if (CollectionUtils.isEmpty(dictDatas)){
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+ for (SysDictData dictData : dictDatas) {
|
|
|
|
+ SysDictType dictType = iSysDictTypeService.getByIdTenant(dictData.getDictTypeId(),tenantId);
|
|
|
|
+ if (ObjectUtils.isNull(dictType)){
|
|
|
|
+ continue;
|
|
|
|
+ }
|
|
|
|
+ SysDictType dictType1 = dictTypeList.stream().filter(item -> item.getDictType().equals(dictType.getDictType())).findFirst().orElse(null);
|
|
|
|
+ if (ObjectUtils.isNull(dictType1)){
|
|
|
|
+ continue;
|
|
|
|
+ }
|
|
|
|
+ dictData.setDictCode(null);
|
|
|
|
+ dictData.setDictTypeId(dictType1.getDictId());
|
|
|
|
+ dictData.setTenantId(newTenantId);
|
|
|
|
+ }
|
|
|
|
+ iSysDictDataService.saveBatch(dictDatas);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public void updateRoleTenant(List<Long> tenantIds) {
|
|
|
|
+ Long oldTenantId = 867735392558919680L;
|
|
|
|
+ for (Long tenantId : tenantIds) {
|
|
|
|
+ initRoles(tenantId,oldTenantId);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private void initRoles(Long newTenantId,Long tenantId) {
|
|
|
|
+ List<String> roleKey = new ArrayList<>();
|
|
|
|
+ roleKey.add("seller");
|
|
|
|
+ roleKey.add("seller_admin");
|
|
|
|
+ roleKey.add("supervisory");
|
|
|
|
+ roleKey.add("cashier");
|
|
|
|
+ roleKey.add("accounting");
|
|
|
|
+ roleKey.add("boss");
|
|
|
|
+ roleKey.add("edu");
|
|
|
|
+ roleKey.forEach(key -> {
|
|
|
|
+ SysRole tenantRole = roleService.getRoleByTenantKey(key,newTenantId);
|
|
|
|
+ if (ObjectUtils.isNull(tenantRole)){
|
|
|
|
+ //新增
|
|
|
|
+ SysRole role = roleService.getRoleByTenantKey(key, tenantId);
|
|
|
|
+ if (ObjectUtils.isNull(role)){
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+ role.setRoleId(null);
|
|
|
|
+ role.setTenantId(newTenantId.toString());
|
|
|
|
+ roleService.save(role);
|
|
|
|
+ tenantRole = role;
|
|
|
|
+ }
|
|
|
|
+ //角色菜单
|
|
|
|
+ SysRole oldRole = roleService.getRoleByTenantKey(tenantRole.getRoleKey(), tenantId);
|
|
|
|
+ List<SysRoleMenu> sysRoleMenus = sysRoleMenuMapper.listByTenant(oldRole.getRoleId(),tenantId);
|
|
|
|
+ if (CollectionUtils.isNotEmpty(sysRoleMenus)){
|
|
|
|
+ List<SysRoleMenu> roleMenus = new ArrayList<>();
|
|
|
|
+ for (SysRoleMenu menu : sysRoleMenus) {
|
|
|
|
+ SysMenu oldSysMenu = menuService.getMenuById(menu.getMenuId(),tenantId);
|
|
|
|
+ if (ObjectUtils.isNull(oldSysMenu)){
|
|
|
|
+ continue;
|
|
|
|
+ }
|
|
|
|
+ Long menuId = menuService.getMenuByTenant(oldSysMenu.getMenuName(),oldSysMenu.getOrderNum(),DateUtils.formatDate(oldSysMenu.getCreateTime(),"yyyy-MM-dd HH:mm:ss"),newTenantId);
|
|
|
|
+ if (ObjectUtils.isNull(menuId)){
|
|
|
|
+ oldSysMenu.setMenuId(null);
|
|
|
|
+ oldSysMenu.setTenantId(newTenantId);
|
|
|
|
+ //查询父ID
|
|
|
|
+ Long parentId = getParentId(oldSysMenu,tenantId,newTenantId);
|
|
|
|
+ oldSysMenu.setParentId(parentId);
|
|
|
|
+ menuService.save(oldSysMenu);
|
|
|
|
+ menuId = oldSysMenu.getMenuId();
|
|
|
|
+ }
|
|
|
|
+ SysRoleMenu sysRoleMenu = new SysRoleMenu();
|
|
|
|
+ sysRoleMenu.setRoleId(tenantRole.getRoleId());
|
|
|
|
+ sysRoleMenu.setMenuId(menuId);
|
|
|
|
+ sysRoleMenu.setTenantId(newTenantId.toString());
|
|
|
|
+ roleMenus.add(sysRoleMenu);
|
|
|
|
+ }
|
|
|
|
+ if (CollectionUtils.isNotEmpty(roleMenus)){
|
|
|
|
+ roleMenus.forEach(item -> {
|
|
|
|
+ sysRoleMenuMapper.insert(item);
|
|
|
|
+ });
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ });
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private Long getParentId(SysMenu oldSysMenu, Long tenantId, Long newTenantId) {
|
|
|
|
+ if (oldSysMenu.getParentId() == 0){
|
|
|
|
+ return 0L;
|
|
|
|
+ }
|
|
|
|
+ SysMenu sysMenu = menuService.getMenuById(oldSysMenu.getParentId(),tenantId);
|
|
|
|
+ if (ObjectUtils.isNull(oldSysMenu)){
|
|
|
|
+ return 0L;
|
|
|
|
+ }
|
|
|
|
+ Long menuId = menuService.getMenuByTenant(sysMenu.getMenuName(),sysMenu.getOrderNum(),DateUtils.formatDate(sysMenu.getCreateTime(),"yyyy-MM-dd HH:mm:ss"),newTenantId);
|
|
|
|
+ if (ObjectUtils.isNull(menuId)){
|
|
|
|
+ oldSysMenu.setMenuId(null);
|
|
|
|
+ oldSysMenu.setTenantId(newTenantId);
|
|
|
|
+ //查询父ID
|
|
|
|
+ Long parentId = getParentId(sysMenu,tenantId,newTenantId);
|
|
|
|
+ oldSysMenu.setParentId(parentId);
|
|
|
|
+ menuService.save(oldSysMenu);
|
|
|
|
+ menuId = oldSysMenu.getMenuId();
|
|
|
|
+ }
|
|
|
|
+ return menuId;
|
|
|
|
+ }
|
|
|
|
+
|
|
}
|
|
}
|