Przeglądaj źródła

增加
平台账号机构权限管理相关

hgw 3 lat temu
rodzic
commit
021b5ca26e

+ 3 - 0
mec-biz/src/main/java/com/ym/mec/biz/dal/dao/SysUserTenantDao.java

@@ -2,6 +2,7 @@ package com.ym.mec.biz.dal.dao;
 
 import com.baomidou.mybatisplus.core.mapper.BaseMapper;
 import com.ym.mec.biz.dal.entity.SysUserTenant;
+import com.ym.mec.biz.dal.vo.SysUserTenantVo;
 import org.apache.ibatis.annotations.Param;
 
 import java.util.List;
@@ -16,5 +17,7 @@ public interface SysUserTenantDao extends BaseMapper<SysUserTenant> {
 
     int insertBatch(@Param("entities") List<SysUserTenant> entities);
 
+    List<SysUserTenantVo> queryUserTenant(@Param("userId") Integer userId);
+
 }
 

+ 12 - 0
mec-biz/src/main/java/com/ym/mec/biz/dal/dto/EmployeeDto.java

@@ -1,5 +1,7 @@
 package com.ym.mec.biz.dal.dto;
 
+import com.ym.mec.biz.dal.vo.SysUserTenantVo;
+import com.ym.mec.biz.dal.vo.TenantInfoPageVo;
 import com.ym.mec.common.enums.UserGenderEnum;
 import io.swagger.annotations.ApiModelProperty;
 
@@ -77,6 +79,8 @@ public class EmployeeDto {
     @ApiModelProperty(value = "岗位管理范围",required = false)
     private String postDeptIds;
 
+    private List<SysUserTenantVo> userTenantList;
+
     public String getPositionNames() {
         return positionNames;
     }
@@ -252,4 +256,12 @@ public class EmployeeDto {
     public void setDeptId(Integer deptId) {
         this.deptId = deptId;
     }
+
+    public List<SysUserTenantVo> getUserTenantList() {
+        return userTenantList;
+    }
+
+    public void setUserTenantList(List<SysUserTenantVo> userTenantList) {
+        this.userTenantList = userTenantList;
+    }
 }

+ 11 - 0
mec-biz/src/main/java/com/ym/mec/biz/dal/entity/Employee.java

@@ -92,6 +92,9 @@ public class Employee extends SysUser {
 	@ApiModelProperty(value = "职位id列表",required = false)
 	private List<Integer> positionIds;
 
+	@ApiModelProperty(value = "机构id列表")
+	private List<Integer> tenantIds;
+
 	private String contactAddress;
 
 	private String postalCode;
@@ -292,4 +295,12 @@ public class Employee extends SysUser {
 	public void setDeptId(Integer deptId) {
 		this.deptId = deptId;
 	}
+
+    public List<Integer> getTenantIds() {
+        return tenantIds;
+    }
+
+    public void setTenantIds(List<Integer> tenantIds) {
+        this.tenantIds = tenantIds;
+    }
 }

+ 56 - 0
mec-biz/src/main/java/com/ym/mec/biz/dal/vo/SysUserTenantVo.java

@@ -0,0 +1,56 @@
+package com.ym.mec.biz.dal.vo;
+
+import io.swagger.annotations.ApiModelProperty;
+
+import java.io.Serializable;
+
+/**
+ * @author hgw
+ * Created by 2022-03-02
+ */
+public class SysUserTenantVo implements Serializable {
+
+    @ApiModelProperty(value = "关系表id")
+    private Integer id;
+
+    @ApiModelProperty(value = "机构id")
+    private Integer userId;
+
+    @ApiModelProperty(value = "机构id")
+    private Integer tenantId;
+
+    @ApiModelProperty(value = "机构名称")
+    private String tenantName;
+
+    public Integer getId() {
+        return id;
+    }
+
+    public void setId(Integer id) {
+        this.id = id;
+    }
+
+    public Integer getUserId() {
+        return userId;
+    }
+
+    public void setUserId(Integer userId) {
+        this.userId = userId;
+    }
+
+    public Integer getTenantId() {
+        return tenantId;
+    }
+
+    public void setTenantId(Integer tenantId) {
+        this.tenantId = tenantId;
+    }
+
+    public String getTenantName() {
+        return tenantName;
+    }
+
+    public void setTenantName(String tenantName) {
+        this.tenantName = tenantName;
+    }
+}

+ 7 - 0
mec-biz/src/main/java/com/ym/mec/biz/service/SysUserTenantService.java

@@ -3,6 +3,9 @@ package com.ym.mec.biz.service;
 import com.baomidou.mybatisplus.extension.service.IService;
 import com.ym.mec.biz.dal.dao.SysUserTenantDao;
 import com.ym.mec.biz.dal.entity.SysUserTenant;
+import com.ym.mec.biz.dal.vo.SysUserTenantVo;
+
+import java.util.List;
 
 /**
  * 平台用户与机构关系表(SysUserTenant)表服务接口
@@ -14,5 +17,9 @@ public interface SysUserTenantService extends IService<SysUserTenant> {
 
     SysUserTenantDao getDao();
 
+    List<SysUserTenantVo> queryUserTenant(Integer userId);
+
+    void deleteByUserId(Integer userId);
+
 }
 

+ 39 - 5
mec-biz/src/main/java/com/ym/mec/biz/service/impl/EmployeeServiceImpl.java

@@ -7,10 +7,7 @@ import com.ym.mec.auth.api.entity.SysUser;
 import com.ym.mec.biz.dal.dao.*;
 import com.ym.mec.biz.dal.dto.EmployeeDto;
 import com.ym.mec.biz.dal.dto.EmployeeLevelDto;
-import com.ym.mec.biz.dal.entity.Employee;
-import com.ym.mec.biz.dal.entity.Organization;
-import com.ym.mec.biz.dal.entity.SysUserCashAccount;
-import com.ym.mec.biz.dal.entity.TenantInfo;
+import com.ym.mec.biz.dal.entity.*;
 import com.ym.mec.biz.dal.enums.EmployeeOperateEnum;
 import com.ym.mec.biz.dal.enums.GroupType;
 import com.ym.mec.biz.dal.enums.ParamEnum;
@@ -31,6 +28,7 @@ import com.ym.mec.im.entity.GroupModel;
 import com.ym.mec.util.collection.MapUtil;
 import org.apache.commons.lang3.StringUtils;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.dao.DuplicateKeyException;
 import org.springframework.security.authentication.LockedException;
 import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
 import org.springframework.stereotype.Service;
@@ -76,6 +74,8 @@ public class EmployeeServiceImpl extends BaseServiceImpl<Integer, Employee> impl
     private SysEmployeePositionService employeePositionService;
     @Autowired
     private SysEmployeePositionDao employeePositionDao;
+    @Autowired
+    private SysUserTenantService sysUserTenantService;
 
     @Override
     public BaseDAO<Integer, Employee> getDAO() {
@@ -138,11 +138,38 @@ public class EmployeeServiceImpl extends BaseServiceImpl<Integer, Employee> impl
         if (user != null) {
             employee.setAvatar(user.getAvatar());
         }
-
+        //添加平台用户和机构的关系
+        addUserTenant(employee.getTenantIds(), tenantId, employee.getId());
         //添加到OA
         oaUserService.addOaUser(employee);
     }
 
+    /**
+     * 添加平台用户和机构的关系
+     *
+     * @param tenantIds    机构id集合
+     * @param userTenantId 用户当前机构id
+     * @param userId       用户id
+     */
+    private void addUserTenant(List<Integer> tenantIds, Integer userTenantId, Integer userId) {
+        if (Objects.nonNull(userTenantId) && userTenantId == -1) {
+            if (CollectionUtils.isEmpty(tenantIds)) {
+                throw new BizException("平台账号必须指定一个机构!");
+            }
+            Date now = new Date();
+            tenantIds.forEach(t -> {
+                SysUserTenant userTenant = new SysUserTenant();
+                userTenant.setUserId(userId);
+                userTenant.setTenantId(t);
+                userTenant.setCreatedTime(now);
+                try {
+                    sysUserTenantService.save(userTenant);
+                } catch (DuplicateKeyException ignored) {
+                }
+            });
+        }
+    }
+
     @Override
     @Transactional(rollbackFor = Exception.class)
     public void updateEmployee(Employee employee) {
@@ -193,6 +220,9 @@ public class EmployeeServiceImpl extends BaseServiceImpl<Integer, Employee> impl
             tenantInfo.setUpdatedBy(sysUser.getId());
             tenantInfoService.updateById(tenantInfo);
         }
+        //更新用户与机构的关系
+        sysUserTenantService.deleteByUserId(employee.getId());
+        addUserTenant(employee.getTenantIds(), employee.getTenantId(), employee.getId());
 
         //更新OA信息
         oaUserService.updateOaUser(employee);
@@ -226,6 +256,10 @@ public class EmployeeServiceImpl extends BaseServiceImpl<Integer, Employee> impl
                 }
                 List<Integer> organIds = Arrays.stream(employeeDto.getOrganIdStr().split(",")).map(s -> Integer.valueOf(s)).collect(Collectors.toList());
                 allOrganIds.addAll(organIds);
+                //平台账户查询所拥有的机构
+                if(Objects.nonNull(queryInfo.getTenantId()) && queryInfo.getTenantId() == -1){
+                    employeeDto.setUserTenantList(sysUserTenantService.queryUserTenant(employeeDto.getId()));
+                }
             }
             if (!CollectionUtils.isEmpty(allOrganIds)) {
                 List<Organization> allOrgans = organizationDao.findOrgans(allOrganIds);

+ 20 - 2
mec-biz/src/main/java/com/ym/mec/biz/service/impl/SysUserTenantServiceImpl.java

@@ -1,14 +1,17 @@
 package com.ym.mec.biz.service.impl;
 
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 import com.ym.mec.biz.dal.dao.SysUserTenantDao;
 import com.ym.mec.biz.dal.entity.SysUserTenant;
+import com.ym.mec.biz.dal.vo.SysUserTenantVo;
 import com.ym.mec.biz.service.SysUserTenantService;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
+import java.util.List;
 
 /**
  * 平台用户与机构关系表(SysUserTenant)表服务实现类
@@ -26,5 +29,20 @@ public class SysUserTenantServiceImpl extends ServiceImpl<SysUserTenantDao, SysU
         return this.baseMapper;
     }
 
+    @Override
+    public List<SysUserTenantVo> queryUserTenant(Integer userId) {
+        return baseMapper.queryUserTenant(userId);
+    }
+
+    /**
+     * 根据用户id删除用户与机构关系
+     *
+     * @param userId 用户id
+     */
+    @Override
+    public void deleteByUserId(Integer userId) {
+        baseMapper.delete(new QueryWrapper<SysUserTenant>().eq("user_id_", userId));
+    }
+
 }
 

+ 30 - 1
mec-biz/src/main/java/com/ym/mec/biz/service/impl/TenantInfoServiceImpl.java

@@ -18,6 +18,7 @@ import com.ym.mec.biz.dal.entity.*;
 import com.ym.mec.biz.dal.entity.TenantContractRecord.TenantContractRecordEnum;
 import com.ym.mec.biz.dal.enums.*;
 import com.ym.mec.biz.dal.vo.PlatformServePageVo;
+import com.ym.mec.biz.dal.vo.SysUserTenantVo;
 import com.ym.mec.biz.dal.vo.TenantInfoPageVo;
 import com.ym.mec.biz.service.*;
 import com.ym.mec.common.exception.BizException;
@@ -119,6 +120,8 @@ public class TenantInfoServiceImpl extends ServiceImpl<TenantInfoDao, TenantInfo
     private TenantContractRecordService tenantContractRecordService;
     @Autowired
     private TenantContractTemplateDao tenantContractTemplateDao;
+    @Autowired
+    private SysUserTenantService sysUserTenantService;
 
     @Value("${contract.baseDir:/var/pdf}")
     private String contractBaseDir;
@@ -182,6 +185,12 @@ public class TenantInfoServiceImpl extends ServiceImpl<TenantInfoDao, TenantInfo
             preJoin.setState(1);
             tenantPreJoinService.updateById(preJoin);
         }
+        //添加人员机构关联关系
+        SysUserTenant userTenant = new SysUserTenant();
+        userTenant.setUserId(userId);
+        userTenant.setTenantId(tenantId);
+        userTenant.setCreatedTime(new Date());
+        sysUserTenantService.save(userTenant);
         //释放
         bucket.delete();
     }
@@ -514,6 +523,20 @@ public class TenantInfoServiceImpl extends ServiceImpl<TenantInfoDao, TenantInfo
      */
     @Override
     public PageInfo<TenantInfoPageVo> queryPage(Map<String, Object> param) {
+        //获取当前登录用户
+        SysUser user = getUser();
+        if (Objects.nonNull(user.getIsSuperAdmin()) && user.getTenantId() == -1) {
+            //不是超级管理员 但是 是平台账号,则需要查询自己关联的机构有哪些
+            if (!user.getIsSuperAdmin()) {
+                List<SysUserTenantVo> sysUserTenantVos = sysUserTenantService.queryUserTenant(user.getId());
+                if (CollectionUtils.isNotEmpty(sysUserTenantVos)) {
+                    List<Integer> tenantIds = sysUserTenantVos.stream().map(SysUserTenantVo::getTenantId).collect(Collectors.toList());
+                    param.put("tenantIds", tenantIds);
+                } else {
+                    throw new BizException("您的账号暂无机构权限,请联系平台管理员配置");
+                }
+            }
+        }
         Page<TenantInfoPageVo> pageInfo = PageUtil.getPageInfo(param);
         pageInfo.setAsc("a.id_");
         return PageUtil.pageInfo(baseMapper.queryPage(pageInfo, param));
@@ -978,11 +1001,17 @@ public class TenantInfoServiceImpl extends ServiceImpl<TenantInfoDao, TenantInfo
 
     private Integer getUserId() {
         //修改机构基础信息
-        return Optional.ofNullable(sysUserFeignService.queryUserInfo())
+        return Optional.ofNullable(getUser())
                 .map(SysUser::getId)
                 .orElseThrow(() -> new BizException("用户信息获取失败,请刷新页面或者重新登录!"));
     }
 
+    private SysUser getUser() {
+        //修改机构基础信息
+        return Optional.ofNullable(sysUserFeignService.queryUserInfo())
+                .orElseThrow(() -> new BizException("用户信息获取失败,请刷新页面或者重新登录!"));
+    }
+
     private void checkTsign(String code) {
         //校验营业信息
         SysUserTsign sysUserTsign = sysUserTsignService.queryByCardNo(code);

+ 11 - 0
mec-biz/src/main/resources/config/mybatis/SysUserTenantMapper.xml

@@ -23,4 +23,15 @@
         </foreach>
     </insert>
 
+    <select id="queryUserTenant" resultType="com.ym.mec.biz.dal.vo.SysUserTenantVo">
+        select
+            a.id_ as id,
+            a.user_id_ as userId,
+            b.id_ as tenantId,
+            b.name_ as tenantName
+        from sys_user_tenant as a
+                 left join tenant_info as b on a.tenant_id_ = b.id_
+        where a.user_id_ = #{userId}
+    </select>
+
 </mapper>

+ 6 - 0
mec-biz/src/main/resources/config/mybatis/TenantInfoMapper.xml

@@ -116,6 +116,12 @@
                 OR a.`phone_` LIKE CONCAT('%', #{param.search},'%')
                 )
             </if>
+            <if test="param.tenantIds != null ">
+                AND a.id_ in
+                <foreach collection="param.tenantIds" item="tenantId" open="(" close=")" separator=",">
+                    #{tenantId}
+                </foreach>
+            </if>
             <if test="param.tenantId != null ">
                 AND a.`id_` = #{param.tenantId}
             </if>