Forráskód Böngészése

新增租户时创建机构和角色
租户删除修改为逻辑删除

wangli 4 éve
szülő
commit
49c994fb06

+ 3 - 0
common/src/main/java/com/huaxu/model/ResultStatus.java

@@ -100,11 +100,14 @@ public enum ResultStatus {
     ACCESS_DISABLED_ERROR(900013, "您的账号已被禁用,如有问题请联系管理员。"),
     PHONE_NUMBER_NOT_FOUND_WECHAT_ERROR(900014, "您微信绑定的手机号尚未注册,请使用平台注册账号进行登录。"),
 
+    TENANT_NAME_ALREADY_EXISTS(900015,"租户名称已存在"),
+
     EXSIT_IS_PARENT_ERROR(800001,"存在下级信息不能删除"),
     EXSIT_IS_DICTCODE_ERROR(800002,"存在相同字典编码不能新增"),
 
     SYSTEM_ERROR(999999, "系统错误"),
     PARAM_ERROR(700001, "参数缺失"),
+
     ;
 
     private final int status;

+ 11 - 5
user_center/src/main/java/com/huaxu/controller/TenantController.java

@@ -5,6 +5,7 @@ import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.huaxu.common.FileUploadUtil;
 import com.huaxu.dto.TenantDto;
 import com.huaxu.entity.TenantEntity;
+import com.huaxu.entity.UserEntity;
 import com.huaxu.model.AjaxMessage;
 import com.huaxu.model.Pagination;
 import com.huaxu.model.ResultStatus;
@@ -37,8 +38,6 @@ public class TenantController {
      */
     @Autowired
     private TenantService tenantService;
-    @Autowired
-    private UserService userService;
 
     @Value("${UMIS.sys_config_path}")
     private String baseDir;
@@ -100,10 +99,10 @@ public class TenantController {
     @RequestMapping(value = "insert", method = RequestMethod.POST)
     @ApiOperation(value = "插入租户信息")
     public AjaxMessage<Integer> insert(@ApiParam(value = "租户信息", required = true) @RequestBody TenantDto tenantDto) {
-        if (tenantDto.getPhone() != null) {
-            boolean isExsit = userService.checkMobileUnique(tenantDto.getPhone());
+        if (tenantDto.getTenantName() != null) {
+            boolean isExsit = tenantService.checkTenantNameUnique(tenantDto);
             if (isExsit) {
-                return new AjaxMessage<>(ResultStatus.MEMBER_TELPHONE_ALREADY_EXISTS, 0);
+                return new AjaxMessage<>(ResultStatus.TENANT_NAME_ALREADY_EXISTS, 0);
             }
         }
         int result = tenantService.insert(tenantDto);
@@ -120,6 +119,13 @@ public class TenantController {
     @RequestMapping(value = "update", method = RequestMethod.POST)
     @ApiOperation(value = "修改租户信息")
     public AjaxMessage<Integer> update(@ApiParam(value = "租户信息", required = true) @RequestBody TenantDto tenantDto) {
+        if (tenantDto.getTenantName() != null) {
+            boolean isExsit = tenantService.checkTenantNameUnique(tenantDto);
+            if (isExsit) {
+                return new AjaxMessage<>(ResultStatus.TENANT_NAME_ALREADY_EXISTS, 0);
+            }
+        }
+
         int result = tenantService.update(tenantDto);
         return new AjaxMessage<>(ResultStatus.OK, result);
 

+ 6 - 0
user_center/src/main/java/com/huaxu/dao/TenantMapper.java

@@ -80,6 +80,12 @@ public interface TenantMapper  {
      */
     int count();
 
+    /**
+     * 根据租户名字查询租户id
+     * @param tenantName
+     * @return
+     */
+    List<Integer> countIds( String tenantName);
     /**
      *  分页查询
      * @param page

+ 7 - 0
user_center/src/main/java/com/huaxu/service/TenantService.java

@@ -68,5 +68,12 @@ public interface TenantService {
      */
     int count();
 
+    /**
+     * 校验租户名称唯一
+     * @param tenantDto
+     * @return
+     */
+    boolean checkTenantNameUnique(TenantDto tenantDto);
+
     IPage<TenantDto> selectPage( IPage<TenantDto> page, TenantDto tenantDto);
 }

+ 113 - 21
user_center/src/main/java/com/huaxu/service/impl/TenantServiceImpl.java

@@ -1,14 +1,23 @@
 package com.huaxu.service.impl;
 
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.baomidou.mybatisplus.core.metadata.IPage;
+import com.huaxu.common.ToolUtil;
 import com.huaxu.dao.TenantMapper;
+import com.huaxu.dto.RoleRequestDto;
 import com.huaxu.dto.TenantDto;
+import com.huaxu.entity.Org;
+import com.huaxu.entity.Role;
+import com.huaxu.entity.TenantEntity;
 import com.huaxu.entity.UserEntity;
 import com.huaxu.model.LoginUser;
+import com.huaxu.service.OrgService;
+import com.huaxu.service.RoleService;
 import com.huaxu.service.TenantService;
 import com.huaxu.service.UserService;
 import com.huaxu.util.Snowflake;
 import com.huaxu.util.UserUtil;
+import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
 import javax.annotation.Resource;
@@ -24,12 +33,26 @@ import java.util.stream.Collectors;
  */
 @Service("tenantService")
 public class TenantServiceImpl implements TenantService {
-
-    @Resource
-    private TenantMapper tenantMapper;
-
+    /**
+     * 租户
+     */
      @Resource
+     private TenantMapper tenantMapper;
+    /**
+     * 用户
+     */
+     @Autowired
      private UserService userService;
+    /**
+     * 角色
+     */
+    @Autowired
+     private RoleService roleService;
+    /**
+     * 机构
+     */
+    @Autowired
+    private OrgService orgService;
 
     /**
      * 通过ID查询单条数据
@@ -78,13 +101,50 @@ public class TenantServiceImpl implements TenantService {
     public Integer insert(TenantDto tenantDto) {
         LoginUser loginUser = UserUtil.getCurrentUser();
         //创建租户信息
-        tenantDto.setCreateBy(loginUser.getUsername());
+        if(loginUser != null){
+            tenantDto.setUpdateBy(loginUser.getUsername());
+            tenantDto.setCreateBy(loginUser.getUsername());
+        }
         tenantDto.setDateCreate(new Date());
-        tenantDto.setUpdateBy(loginUser.getUsername());
         tenantDto.setDateUpdate(new Date());
         tenantDto.setCode(Snowflake.generateId().toString());
         tenantDto.setStatus(1);
+        tenantDto.setTenantState(1);
         tenantMapper.insert(tenantDto);
+
+        //创建租户菜单信息
+        if(tenantDto.getTenantMenuIds().size()>0){
+            tenantMapper.createTenantMenu(tenantDto.getCreateBy(),tenantDto.getId(),tenantDto.getTenantMenuIds());
+        }
+        //创建角色
+        RoleRequestDto roleRequestDto = new RoleRequestDto();
+        Role role = new Role();
+        role.setRoleName(tenantDto.getTenantName());
+        role.setDescription(tenantDto.getTenantName() +"初始化角色");
+        role.setRoleState(1);
+        role.setPermissionType(1);
+        role.setStatus(1);
+        roleRequestDto.setRole(role);
+        //查询租户菜单id
+//        List<Integer > tenantMenuIds = tenantMapper.selectTenantMenuIds(tenantDto.getId());
+        roleRequestDto.setIds(tenantDto.getTenantMenuIds());
+        //插入角色
+        roleService.insert(roleRequestDto);
+        //插入角色菜单(租户菜单id)
+        roleService.updateRoleMenuRelations(roleRequestDto);
+
+        /*创建机构*/
+        Org org = new Org();
+        org.setOrgLeaderName(tenantDto.getUserName());
+        org.setOrgLeaderPhone(tenantDto.getPhone());
+        org.setOrgName(tenantDto.getTenantName());
+        org.setOrgState(1);
+        org.setStatus(1);
+        org.setOrgType("company");  //机构类型
+        org.setParentId(0);
+        orgService.insert(org);
+
+
         //创建管理员信息
         UserEntity userEntity =new UserEntity();
         userEntity.setTenantId(tenantDto.getCode());
@@ -96,11 +156,10 @@ public class TenantServiceImpl implements TenantService {
         userEntity.setPhone(tenantDto.getPhone());
         userEntity.setUsername(tenantDto.getUserName());
         userEntity.setUserType("1");//租户系统管理员
+        userEntity.setRoleId(role.getId().longValue()); //赋值角色
+        userEntity.setCompanyOrgId(org.getId().longValue());//公司
+        userEntity.setDeptOrgId(org.getId().longValue());//机构
         userService.addUser(userEntity);
-        //创建租户菜单信息
-        if(tenantDto.getTenantMenuIds().size()>0){
-            tenantMapper.createTenantMenu(tenantDto.getCreateBy(),tenantDto.getId(),tenantDto.getTenantMenuIds());
-        }
         return 1;
     }
 
@@ -118,20 +177,13 @@ public class TenantServiceImpl implements TenantService {
             return 0;
         }
         //修改租户信息
-        tenantDto.setUpdateBy(loginUser.getUsername());
+        if(loginUser != null){
+            tenantDto.setUpdateBy(loginUser.getUsername());
+        }
         tenantDto.setDateUpdate(new Date());
 
         tenantMapper.update(tenantDto);
-        //修改管理员信息
-        UserEntity userEntity =new UserEntity();
-        userEntity.setId(tenantDto.getUserId());
-        userEntity.setTenantId(tenantDto.getCode());
-        userEntity.setUpdateBy(tenantDto.getUpdateBy());
-        userEntity.setDateUpdate(new Date());
-        userEntity.setPhone(tenantDto.getPhone());
-        userEntity.setUsername(tenantDto.getUserName());
-        userEntity.setUserType("1");//租户系统管理员
-        userService.updateUserById(userEntity);
+
         //修改租户菜单信息
         //界面传回的租户菜单id
         List<Integer > newTenantMenuIds = tenantDto.getTenantMenuIds();
@@ -150,6 +202,28 @@ public class TenantServiceImpl implements TenantService {
         if(deleteTenantMenuIds.size()>0){
             tenantMapper.deleteTenantMenuIds(tenantDto.getId(),deleteTenantMenuIds);
         }
+        //修改管理员信息
+        UserEntity userEntity =userService.findUserById(tenantDto.getUserId());
+        userEntity.setUpdateBy(tenantDto.getUpdateBy());
+        userEntity.setDateUpdate(new Date());
+        userEntity.setPhone(tenantDto.getPhone());
+        userEntity.setUsername(tenantDto.getUserName());
+        userService.updateUserById(userEntity);
+
+        //创建角色
+        RoleRequestDto roleRequestDto = new RoleRequestDto();
+        Role role = roleService.selectById(userEntity.getRoleId().intValue());
+        role.setRoleName(tenantDto.getTenantName());
+        role.setDescription(tenantDto.getTenantName() +"初始化角色");
+        roleRequestDto.setRole(role);
+        //查询租户菜单id
+//        List<Integer > tenantMenuIds = tenantMapper.selectTenantMenuIds(tenantDto.getId());
+
+        roleRequestDto.setIds(newTenantMenuIds);
+        //插入角色
+        roleService.update(roleRequestDto);
+        //插入角色菜单(租户菜单id)
+        roleService.updateRoleMenuRelations(roleRequestDto);
 
         return 1;
     }
@@ -186,4 +260,22 @@ public class TenantServiceImpl implements TenantService {
     public IPage<TenantDto> selectPage(IPage<TenantDto> page, TenantDto tenantDto) {
         return tenantMapper.selectPage(page, tenantDto);
     }
+
+    public boolean checkTenantNameUnique(TenantDto tenantDto) {
+        List<Integer> tenantIds = tenantMapper.countIds(tenantDto.getTenantName());
+
+        if(tenantIds.size()>1){ //名字匹配多个结果
+            return false;
+        }else if(tenantIds.size() == 0 ){   //没有匹配的结果
+            return true;
+        }else if(ToolUtil.isEmpty(tenantDto.getId())){  //新增情况匹配到一个结果
+            return false;
+        }else if(!tenantDto.getId().equals(tenantIds.get(0))){ //修改时匹配到一个结果,但id不相等
+            return false;
+        }else { //修改时匹配到一个结果且id相等
+            return true;
+        }
+
+    }
+
 }

+ 13 - 2
user_center/src/main/resources/mapper/TenantMapper.xml

@@ -30,7 +30,7 @@
         <include refid="Base_Column_List"/>
         from uims_tenant t
         <include refid="tenantJoins"/>
-        where t.ID = #{id}
+        where t.ID = #{id} and t.state = 1
     </select>
 
     <!-- 查询全部 -->
@@ -39,6 +39,7 @@
         <include refid="Base_Column_List"/>
         from uims_tenant t
         <include refid="tenantJoins"/>
+        where t.state = 1
     </select>
 
     <!--通过实体作为筛选条件查询-->
@@ -48,6 +49,7 @@
         from uims_tenant t
         <include refid="tenantJoins"/>
         <where>
+            t.state = 1
             <if test="tenantName != null and tenantName != ''">
                 and TENANT_NAME like concat('%',#{tenantName},'%')
             </if>
@@ -117,13 +119,21 @@
 
     <!--通过主键删除-->
     <delete id="deleteById">
-        delete from uims_tenant where ID = #{id}
+        /*delete from uims_tenant where ID = #{id}*/
+        update  uims_tenant set state = 0 where ID = #{id}
     </delete>
 
     <!-- 总数 -->
     <select id="count" resultType="int">
         select count(*) from uims_tenant
     </select>
+    <select id="countIds" resultType="java.lang.Integer">
+        select id from uims_tenant
+        <where>
+            TENANT_NAME =#{tenantName}
+        </where>
+    </select>
+
     <!-- 分页查询 -->
     <select id="selectPage" resultType="com.huaxu.dto.TenantDto">
         select
@@ -131,6 +141,7 @@
         from uims_tenant t
         <include refid="tenantJoins"/>
         <where>
+            t.state = 1
             <if test="tenantDto.tenantName != null and tenantDto.tenantName != ''">
                 and t.TENANT_NAME like concat('%',#{tenantDto.tenantName},'%')
             </if>