|
@@ -1,5 +1,7 @@
|
|
|
package com.ruoyi.system.service.impl;
|
|
|
|
|
|
+import com.baomidou.dynamic.datasource.annotation.DSTransactional;
|
|
|
+import com.baomidou.mybatisplus.core.toolkit.IdWorker;
|
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
import com.ruoyi.common.annotation.DataScope;
|
|
|
import com.ruoyi.common.constant.UserConstants;
|
|
@@ -12,15 +14,17 @@ import com.ruoyi.common.exception.ServiceException;
|
|
|
import com.ruoyi.common.utils.SecurityUtils;
|
|
|
import com.ruoyi.common.utils.StringUtils;
|
|
|
import com.ruoyi.common.utils.spring.SpringUtils;
|
|
|
+import com.ruoyi.system.domain.bo.TenantInfoBo;
|
|
|
import com.ruoyi.system.mapper.SysDeptMapper;
|
|
|
import com.ruoyi.system.mapper.SysRoleMapper;
|
|
|
import com.ruoyi.system.service.ISysDeptService;
|
|
|
+import com.ruoyi.system.service.ISysRoleService;
|
|
|
+import com.ruoyi.system.service.ISysUserService;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
-import java.util.ArrayList;
|
|
|
-import java.util.Iterator;
|
|
|
-import java.util.List;
|
|
|
+import java.util.*;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
/**
|
|
@@ -37,6 +41,11 @@ public class SysDeptServiceImpl extends ServiceImpl<SysDeptMapper, SysDept> impl
|
|
|
@Autowired
|
|
|
private SysRoleMapper roleMapper;
|
|
|
|
|
|
+ @Autowired
|
|
|
+ private ISysUserService sysUserService;
|
|
|
+ @Autowired
|
|
|
+ private ISysRoleService sysRoleService;
|
|
|
+
|
|
|
/**
|
|
|
* 查询部门管理数据
|
|
|
*
|
|
@@ -194,14 +203,74 @@ public class SysDeptServiceImpl extends ServiceImpl<SysDeptMapper, SysDept> impl
|
|
|
* @return 结果
|
|
|
*/
|
|
|
@Override
|
|
|
+ @DSTransactional
|
|
|
public int insertDept(SysDept dept) {
|
|
|
+ dept.setDeptId(IdWorker.getId());
|
|
|
+
|
|
|
+ //添加公司时将租户id赋值成自己
|
|
|
+ if (dept.getType() == 0) {
|
|
|
+ dept.setTenantId(String.valueOf(dept.getDeptId()));
|
|
|
+ }
|
|
|
+
|
|
|
SysDept info = deptMapper.selectDeptById(dept.getParentId());
|
|
|
// 如果父节点不为正常状态,则不允许新增子节点
|
|
|
if (!UserConstants.DEPT_NORMAL.equals(info.getStatus())) {
|
|
|
throw new ServiceException("部门停用,不允许新增");
|
|
|
}
|
|
|
dept.setAncestors(info.getAncestors() + "," + dept.getParentId());
|
|
|
- return deptMapper.insertDept(dept);
|
|
|
+ int addCount = deptMapper.insertDept(dept);
|
|
|
+ if (addCount <= 0) {
|
|
|
+ throw new ServiceException("添加部门失败");
|
|
|
+ }
|
|
|
+
|
|
|
+ //添加公司时添加租户
|
|
|
+ if (dept.getType() == 0) {
|
|
|
+ TenantInfoBo tenantInfoDto = new TenantInfoBo();
|
|
|
+ tenantInfoDto.setId(IdWorker.getId());
|
|
|
+ tenantInfoDto.setTenantId(String.valueOf(dept.getDeptId()));
|
|
|
+ tenantInfoDto.setEnterpriseName(dept.getDeptName());
|
|
|
+ deptMapper.addTenantInfo(tenantInfoDto);
|
|
|
+
|
|
|
+// SysDept sysDept = new SysDept();
|
|
|
+// sysDept.setParentId(0L);
|
|
|
+// sysDept.setDeptName(tenantInfoDto.getEnterpriseName());
|
|
|
+// sysDept.setOrderNum(1);
|
|
|
+// sysDept.setStatus("0");
|
|
|
+// sysDept.setType(0);
|
|
|
+// sysDept.setTenantId(tenantInfoDto.getTenantId());
|
|
|
+// sysDept.setCreateBy(SecurityUtils.getUsername());
|
|
|
+// sysDept.setCreateTime(new Date());
|
|
|
+// sysDeptService.save(sysDept);
|
|
|
+
|
|
|
+ SysUser sysUser = new SysUser();
|
|
|
+ sysUser.setDeptId(dept.getDeptId());
|
|
|
+ sysUser.setTenantId(tenantInfoDto.getTenantId());
|
|
|
+ sysUser.setUserName("admin");
|
|
|
+ sysUser.setNickName(tenantInfoDto.getEnterpriseName() + "管理员");
|
|
|
+
|
|
|
+ BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
|
|
|
+ String pwd = passwordEncoder.encode("123456");
|
|
|
+
|
|
|
+ sysUser.setPassword(pwd);
|
|
|
+ sysUser.setStatus("0");
|
|
|
+ sysUser.setCreateBy(SecurityUtils.getUsername());
|
|
|
+ sysUser.setCreateTime(new Date());
|
|
|
+ sysUserService.save(sysUser);
|
|
|
+
|
|
|
+ SysRole sysRole = new SysRole();
|
|
|
+ sysRole.setRoleName("管理员");
|
|
|
+ sysRole.setRoleKey("admin");
|
|
|
+ sysRole.setRoleSort(1);
|
|
|
+ sysRole.setDataScope("1");
|
|
|
+ sysRole.setStatus("0");
|
|
|
+ sysRole.setTenantId(tenantInfoDto.getTenantId());
|
|
|
+ sysRole.setCreateBy(SecurityUtils.getUsername());
|
|
|
+ sysRole.setCreateTime(new Date());
|
|
|
+ sysRoleService.save(sysRole);
|
|
|
+
|
|
|
+ sysRoleService.insertAuthUsers(sysRole.getRoleId(), new Long[]{sysUser.getUserId()});
|
|
|
+ }
|
|
|
+ return addCount;
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -227,6 +296,13 @@ public class SysDeptServiceImpl extends ServiceImpl<SysDeptMapper, SysDept> impl
|
|
|
// 如果该部门是启用状态,则启用该部门的所有上级部门
|
|
|
updateParentDeptStatusNormal(dept);
|
|
|
}
|
|
|
+
|
|
|
+ //如果修改的是公司同步修改租户名称
|
|
|
+ TenantInfoBo tenantInfoBo = new TenantInfoBo();
|
|
|
+ tenantInfoBo.setTenantId(dept.getTenantId());
|
|
|
+ tenantInfoBo.setEnterpriseName(dept.getDeptName());
|
|
|
+ deptMapper.editTenantInfo(tenantInfoBo);
|
|
|
+
|
|
|
return result;
|
|
|
}
|
|
|
|
|
@@ -266,6 +342,12 @@ public class SysDeptServiceImpl extends ServiceImpl<SysDeptMapper, SysDept> impl
|
|
|
*/
|
|
|
@Override
|
|
|
public int deleteDeptById(Long deptId) {
|
|
|
+ //删除公司时同时删除租户
|
|
|
+ SysDept sysDept = this.getById(deptId);
|
|
|
+ if (Objects.equals(sysDept.getType(), 0)) {
|
|
|
+ deptMapper.delTenantInfo(String.valueOf(deptId));
|
|
|
+ }
|
|
|
+
|
|
|
return deptMapper.deleteDeptById(deptId);
|
|
|
}
|
|
|
|