|
@@ -4,9 +4,12 @@ import cn.hutool.core.bean.BeanUtil;
|
|
|
import cn.hutool.core.lang.Validator;
|
|
|
import cn.hutool.core.util.StrUtil;
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
+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.extension.service.impl.ServiceImpl;
|
|
|
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.exception.CustomException;
|
|
|
import com.zhongzheng.common.utils.DateUtils;
|
|
@@ -14,17 +17,22 @@ import com.zhongzheng.common.utils.SecurityUtils;
|
|
|
import com.zhongzheng.common.utils.ServletUtils;
|
|
|
import com.zhongzheng.framework.mybatisplus.CustomTenantLineHandler;
|
|
|
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.domain.SysTenant;
|
|
|
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.vo.SysTenantVo;
|
|
|
+import org.apache.commons.lang3.RandomStringUtils;
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
|
+import java.util.ArrayList;
|
|
|
import java.util.Collection;
|
|
|
import java.util.List;
|
|
|
import java.util.stream.Collectors;
|
|
@@ -41,6 +49,9 @@ public class SysTenantServiceImpl extends ServiceImpl<SysTenantMapper, SysTenant
|
|
|
@Autowired
|
|
|
private ISysUserService userService;
|
|
|
|
|
|
+ @Autowired
|
|
|
+ private ISysMenuService menuService;
|
|
|
+
|
|
|
@Override
|
|
|
public SysTenantVo queryById(Long tenantId){
|
|
|
SysTenant db = this.baseMapper.selectById(tenantId);
|
|
@@ -109,6 +120,81 @@ public class SysTenantServiceImpl extends ServiceImpl<SysTenantMapper, SysTenant
|
|
|
return this.removeByIds(ids);
|
|
|
}
|
|
|
|
|
|
+ @Override
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ public void createTenantAdmin(SysTenantAdminBo bo) {
|
|
|
+ //中正后台企业ID
|
|
|
+ Long tenantId = 867735392558919680L;
|
|
|
+ //设置新机构
|
|
|
+ SysTenant sysTenant = getById(tenantId);
|
|
|
+ sysTenant.setTenantName(bo.getTenantName());
|
|
|
+ //生成tenantId
|
|
|
+ Long newTenantId = createTenantId();
|
|
|
+ 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);
|
|
|
+ });
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ private Long createTenantId(){
|
|
|
+ String randomNumeric = RandomStringUtils.randomNumeric(18);
|
|
|
+ int count = count(new LambdaQueryWrapper<SysTenant>()
|
|
|
+ .eq(SysTenant::getTenantId, Long.valueOf(randomNumeric)));
|
|
|
+ if (count == 0){
|
|
|
+ return Long.valueOf(randomNumeric);
|
|
|
+ }else {
|
|
|
+ createTenantId();
|
|
|
+ }
|
|
|
+ return Long.valueOf(randomNumeric);
|
|
|
+ }
|
|
|
+
|
|
|
private boolean checkNameUnique(SysTenant entity) {
|
|
|
SysTenant info = getOne(new LambdaQueryWrapper<SysTenant>()
|
|
|
.eq(SysTenant::getTenantName,entity.getTenantName())
|