Browse Source

租户模块

24282 2 years ago
parent
commit
91335928d5

+ 97 - 0
hx-tenant/src/main/java/com/fjhx/tenant/controller/tenant/DetpTenantController.java

@@ -0,0 +1,97 @@
+package com.fjhx.tenant.controller.tenant;
+
+import cn.hutool.core.util.ObjectUtil;
+import com.baomidou.dynamic.datasource.annotation.DS;
+import com.ruoyi.common.annotation.Log;
+import com.ruoyi.common.annotation.TenantIgnore;
+import com.ruoyi.common.constant.DatasourceConstant;
+import com.ruoyi.common.constant.UserConstants;
+import com.ruoyi.common.core.domain.entity.SysDept;
+import com.ruoyi.common.enums.BusinessType;
+import com.ruoyi.common.exception.ServiceException;
+import com.ruoyi.common.utils.SecurityUtils;
+import com.ruoyi.common.utils.StringUtils;
+import com.ruoyi.system.service.ISysDeptService;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.validation.annotation.Validated;
+import org.springframework.web.bind.annotation.*;
+
+import java.util.List;
+
+@TenantIgnore
+@DS(DatasourceConstant.SLAVE_NAME)
+@RestController
+@RequestMapping("/tenantDept")
+public class DetpTenantController {
+
+    @Autowired
+    private ISysDeptService deptService;
+
+    /**
+     * 获取部门列表
+     */
+    @GetMapping("/list")
+    public List<SysDept> list(SysDept dept) {
+        return deptService.selectDeptList(dept);
+    }
+
+    /**
+     * 新增部门
+     */
+    @Log(title = "部门管理", businessType = BusinessType.INSERT)
+    @PostMapping
+    public void add(@Validated @RequestBody SysDept dept) {
+        String tenantId = dept.getTenantId();
+        if (ObjectUtil.isEmpty(tenantId)) {
+            throw new ServiceException("租户id不能为空");
+        }
+
+        if (!deptService.checkDeptNameUnique(dept)) {
+            throw new ServiceException("新增部门'" + dept.getDeptName() + "'失败,部门名称已存在");
+        }
+        dept.setCreateBy(SecurityUtils.getUsername());
+        deptService.insertDept(dept);
+    }
+
+    /**
+     * 修改部门
+     */
+    @Log(title = "部门管理", businessType = BusinessType.UPDATE)
+    @PutMapping
+    public void edit(@Validated @RequestBody SysDept dept) {
+        String tenantId = dept.getTenantId();
+        if (ObjectUtil.isEmpty(tenantId)) {
+            throw new ServiceException("租户id不能为空");
+        }
+
+        Long deptId = dept.getDeptId();
+        deptService.checkDeptDataScope(deptId);
+        if (!deptService.checkDeptNameUnique(dept)) {
+            throw new ServiceException("修改部门'" + dept.getDeptName() + "'失败,部门名称已存在");
+        } else if (dept.getParentId().equals(deptId)) {
+            throw new ServiceException("修改部门'" + dept.getDeptName() + "'失败,上级部门不能是自己");
+        } else if (StringUtils.equals(UserConstants.DEPT_DISABLE, dept.getStatus()) && deptService.selectNormalChildrenDeptById(deptId) > 0) {
+            throw new ServiceException("该部门包含未停用的子部门!");
+        }
+        dept.setUpdateBy(SecurityUtils.getUsername());
+        deptService.updateDept(dept);
+    }
+
+    /**
+     * 删除部门
+     */
+    @Log(title = "部门管理", businessType = BusinessType.DELETE)
+    @DeleteMapping("/{deptId}")
+    public void remove(@PathVariable Long deptId) {
+        if (deptService.hasChildByDeptId(deptId)) {
+            throw new ServiceException("存在下级部门,不允许删除");
+        }
+        if (deptService.checkDeptExistUser(deptId)) {
+            throw new ServiceException("部门存在用户,不允许删除");
+        }
+        deptService.checkDeptDataScope(deptId);
+        deptService.deleteDeptById(deptId);
+    }
+
+
+}

+ 21 - 0
hx-tenant/src/main/java/com/fjhx/tenant/controller/tenant/TenantInfoController.java

@@ -2,19 +2,24 @@ package com.fjhx.tenant.controller.tenant;
 
 import com.baomidou.dynamic.datasource.annotation.DS;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
+import com.fjhx.tenant.entity.tenant.dto.BindingMenuDto;
 import com.fjhx.tenant.entity.tenant.dto.TenantInfoDto;
 import com.fjhx.tenant.entity.tenant.dto.TenantInfoSelectDto;
+import com.fjhx.tenant.entity.tenant.po.TenantInfo;
 import com.fjhx.tenant.entity.tenant.vo.TenantInfoVo;
 import com.fjhx.tenant.service.tenant.TenantInfoService;
 import com.ruoyi.common.annotation.TenantIgnore;
 import com.ruoyi.common.constant.DatasourceConstant;
 import com.ruoyi.common.core.domain.BaseSelectDto;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.validation.annotation.Validated;
 import org.springframework.web.bind.annotation.PostMapping;
 import org.springframework.web.bind.annotation.RequestBody;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RestController;
 
+import java.util.List;
+
 
 /**
  * <p>
@@ -36,6 +41,14 @@ public class TenantInfoController {
     /**
      * 租户表分页
      */
+    @PostMapping("/list")
+    public List<TenantInfo> list(@RequestBody TenantInfoSelectDto dto) {
+        return tenantInfoService.getList(dto);
+    }
+
+    /**
+     * 租户表分页
+     */
     @PostMapping("/page")
     public Page<TenantInfoVo> page(@RequestBody TenantInfoSelectDto dto) {
         return tenantInfoService.getPage(dto);
@@ -73,4 +86,12 @@ public class TenantInfoController {
         tenantInfoService.delete(dto.getId());
     }
 
+    /**
+     * 租户绑定菜单
+     */
+    @PostMapping("/bindingMenu")
+    public void bindingMenu(@RequestBody @Validated BindingMenuDto dto) {
+        tenantInfoService.bindingMenu(dto);
+    }
+
 }

+ 25 - 0
hx-tenant/src/main/java/com/fjhx/tenant/entity/tenant/dto/BindingMenuDto.java

@@ -0,0 +1,25 @@
+package com.fjhx.tenant.entity.tenant.dto;
+
+import lombok.Getter;
+import lombok.Setter;
+
+import javax.validation.constraints.NotEmpty;
+import java.util.List;
+
+@Getter
+@Setter
+public class BindingMenuDto {
+
+    /**
+     * 租户id
+     */
+    @NotEmpty(message = "租户id不能为空")
+    private String tenantId;
+
+    /**
+     * 菜单id
+     */
+    @NotEmpty(message = "菜单id不能为空")
+    private List<Long> menuIdList;
+
+}

+ 13 - 0
hx-tenant/src/main/java/com/fjhx/tenant/service/tenant/TenantInfoService.java

@@ -1,5 +1,6 @@
 package com.fjhx.tenant.service.tenant;
 
+import com.fjhx.tenant.entity.tenant.dto.BindingMenuDto;
 import com.fjhx.tenant.entity.tenant.po.TenantInfo;
 import com.ruoyi.common.core.service.BaseService;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
@@ -7,6 +8,8 @@ import com.fjhx.tenant.entity.tenant.vo.TenantInfoVo;
 import com.fjhx.tenant.entity.tenant.dto.TenantInfoSelectDto;
 import com.fjhx.tenant.entity.tenant.dto.TenantInfoDto;
 
+import java.util.List;
+
 
 /**
  * <p>
@@ -19,6 +22,11 @@ import com.fjhx.tenant.entity.tenant.dto.TenantInfoDto;
 public interface TenantInfoService extends BaseService<TenantInfo> {
 
     /**
+     * 租户表列表
+     */
+    List<TenantInfo> getList(TenantInfoSelectDto dto);
+
+    /**
      * 租户表分页
      */
     Page<TenantInfoVo> getPage(TenantInfoSelectDto dto);
@@ -43,4 +51,9 @@ public interface TenantInfoService extends BaseService<TenantInfo> {
      */
     void delete(Long id);
 
+    /**
+     * 绑定菜单
+     */
+    void bindingMenu(BindingMenuDto dto);
+
 }

+ 74 - 0
hx-tenant/src/main/java/com/fjhx/tenant/service/tenant/impl/TenantInfoServiceImpl.java

@@ -5,6 +5,7 @@ import cn.hutool.core.util.ObjectUtil;
 import com.baomidou.mybatisplus.core.toolkit.Wrappers;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import com.fjhx.tenant.entity.tenant.dto.BindingMenuDto;
 import com.fjhx.tenant.entity.tenant.dto.TenantInfoDto;
 import com.fjhx.tenant.entity.tenant.dto.TenantInfoSelectDto;
 import com.fjhx.tenant.entity.tenant.po.TenantInfo;
@@ -18,6 +19,8 @@ import com.ruoyi.common.exception.ServiceException;
 import com.ruoyi.common.utils.SecurityUtils;
 import com.ruoyi.common.utils.wrapper.IWrapper;
 import com.ruoyi.common.utils.wrapper.SqlField;
+import com.ruoyi.system.domain.SysRoleMenu;
+import com.ruoyi.system.mapper.SysRoleMenuMapper;
 import com.ruoyi.system.service.ISysDeptService;
 import com.ruoyi.system.service.ISysRoleService;
 import com.ruoyi.system.service.ISysUserService;
@@ -51,6 +54,26 @@ public class TenantInfoServiceImpl extends ServiceImpl<TenantInfoMapper, TenantI
     @Autowired
     private ISysRoleService sysRoleService;
 
+    @Autowired
+    private SysRoleMenuMapper sysRoleMenuMapper;
+
+    @Override
+    public List<TenantInfo> getList(TenantInfoSelectDto dto) {
+        IWrapper<TenantInfo> wrapper = getWrapper();
+        wrapper.orderByDesc(TenantInfo::getId)
+                .eq(TenantInfo::getStatus, dto.getStatus())
+                .eq(TenantInfo::getFlowStatus, dto.getFlowStatus())
+                .like(TenantInfo::getTenantId, dto.getTenantId())
+                .like(TenantInfo::getEnterpriseName, dto.getEnterpriseName())
+                .keyword(dto,
+                        new SqlField(TenantInfo::getEnterpriseName),
+                        new SqlField(TenantInfo::getTenantId)
+                );
+
+        return list(wrapper);
+
+    }
+
     public Page<TenantInfoVo> getPage(TenantInfoSelectDto dto) {
         IWrapper<TenantInfo> wrapper = getWrapper();
         wrapper.orderByDesc("ti", TenantInfo::getId)
@@ -154,4 +177,55 @@ public class TenantInfoServiceImpl extends ServiceImpl<TenantInfoMapper, TenantI
         this.removeById(id);
     }
 
+    @Transactional
+    @Override
+    public void bindingMenu(BindingMenuDto dto) {
+        String tenantId = dto.getTenantId();
+        TenantInfo tenantInfo = this.getOne(q -> q.eq(TenantInfo::getTenantId, tenantId));
+        if (tenantInfo == null) {
+            throw new ServiceException("未找到租户");
+        }
+
+        // 查询租户下的所有角色
+        List<SysRole> sysRoleList = sysRoleService.list(Wrappers.<SysRole>lambdaQuery().eq(SysRole::getTenantId, tenantId));
+        if (sysRoleList.size() == 0) {
+            return;
+        }
+
+        List<Long> roleIdList = sysRoleList.stream().map(SysRole::getRoleId).collect(Collectors.toList());
+
+        // 删除租户所有角色除此之外的菜单
+        sysRoleMenuMapper.removeOtherMenu(roleIdList, dto.getMenuIdList());
+
+        // 添加租户管理员对应权限
+        Long adminRoleId = null;
+        for (SysRole sysRole : sysRoleList) {
+            String roleKey = sysRole.getRoleKey();
+            if ("admin".equals(roleKey)) {
+                adminRoleId = sysRole.getRoleId();
+                break;
+            }
+        }
+        if (adminRoleId == null) {
+            throw new ServiceException("没有找到租户管理员角色");
+        }
+
+        // 找出原有菜单
+        List<Long> adminMenuIdList = sysRoleMenuMapper.getMenuIdByRoleId(adminRoleId);
+
+        // 新增菜单
+        final Long role = adminRoleId;
+        List<SysRoleMenu> sysRoleMenuList = dto.getMenuIdList().stream()
+                .filter(item -> !adminMenuIdList.contains(item))
+                .map(item -> {
+                    SysRoleMenu sysRoleMenu = new SysRoleMenu();
+                    sysRoleMenu.setMenuId(item);
+                    sysRoleMenu.setRoleId(role);
+                    return sysRoleMenu;
+                })
+                .collect(Collectors.toList());
+        sysRoleMenuMapper.batchRoleMenu(sysRoleMenuList);
+
+    }
+
 }