|
@@ -0,0 +1,322 @@
|
|
|
|
+package com.qmth.distributed.print.business.service.impl;
|
|
|
|
+
|
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
|
+import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
|
|
|
|
+import com.qmth.boot.core.enums.Platform;
|
|
|
|
+import com.qmth.distributed.print.business.bean.auth.AuthBean;
|
|
|
|
+import com.qmth.distributed.print.business.bean.dto.PrivilegeCacheDto;
|
|
|
|
+import com.qmth.distributed.print.business.bean.result.PrivilegeResult;
|
|
|
|
+import com.qmth.distributed.print.business.bean.result.RolePrivilegeResult;
|
|
|
|
+import com.qmth.distributed.print.business.config.DictionaryConfig;
|
|
|
|
+import com.qmth.distributed.print.business.entity.*;
|
|
|
|
+import com.qmth.distributed.print.business.enums.PrivilegeEnum;
|
|
|
|
+import com.qmth.distributed.print.business.enums.PrivilegePropertyEnum;
|
|
|
|
+import com.qmth.distributed.print.business.enums.RoleTypeEnum;
|
|
|
|
+import com.qmth.distributed.print.business.enums.UploadFileEnum;
|
|
|
|
+import com.qmth.distributed.print.business.service.*;
|
|
|
|
+import com.qmth.distributed.print.business.util.OssUtil;
|
|
|
|
+import com.qmth.distributed.print.business.util.RedisUtil;
|
|
|
|
+import com.qmth.distributed.print.business.util.ServletUtil;
|
|
|
|
+import com.qmth.distributed.print.common.contant.SpringContextHolder;
|
|
|
|
+import com.qmth.distributed.print.common.contant.SystemConstant;
|
|
|
|
+import com.qmth.distributed.print.common.enums.ExceptionResultEnum;
|
|
|
|
+import com.qmth.distributed.print.common.util.SessionUtil;
|
|
|
|
+import org.slf4j.Logger;
|
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
|
+import org.springframework.util.LinkedMultiValueMap;
|
|
|
|
+
|
|
|
|
+import javax.annotation.Resource;
|
|
|
|
+import java.io.File;
|
|
|
|
+import java.security.NoSuchAlgorithmException;
|
|
|
|
+import java.util.*;
|
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * @Description: 公共服务service impl
|
|
|
|
+ * @Param:
|
|
|
|
+ * @return:
|
|
|
|
+ * @Author: wangliang
|
|
|
|
+ * @Date: 2021/3/26
|
|
|
|
+ */
|
|
|
|
+@Service
|
|
|
|
+public class CommonServiceImpl implements CommonService {
|
|
|
|
+ private final static Logger log = LoggerFactory.getLogger(CommonServiceImpl.class);
|
|
|
|
+
|
|
|
|
+ @Resource
|
|
|
|
+ CacheService cacheService;
|
|
|
|
+
|
|
|
|
+ @Resource
|
|
|
|
+ SysUserRoleService sysUserRoleService;
|
|
|
|
+
|
|
|
|
+ @Resource
|
|
|
|
+ SysPrivilegeService sysPrivilegeService;
|
|
|
|
+
|
|
|
|
+ @Resource
|
|
|
|
+ SysRolePrivilegeService sysRolePrivilegeService;
|
|
|
|
+
|
|
|
|
+ @Resource
|
|
|
|
+ SysRoleService sysRoleService;
|
|
|
|
+
|
|
|
|
+ @Resource
|
|
|
|
+ TBSessionService tbSessionService;
|
|
|
|
+
|
|
|
|
+ @Resource
|
|
|
|
+ RedisUtil redisUtil;
|
|
|
|
+
|
|
|
|
+ @Resource
|
|
|
|
+ DictionaryConfig dictionaryConfig;
|
|
|
|
+
|
|
|
|
+ @Resource
|
|
|
|
+ OssUtil ossUtil;
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 新增用户权限
|
|
|
|
+ *
|
|
|
|
+ * @param sysUser
|
|
|
|
+ * @param roleId
|
|
|
|
+ */
|
|
|
|
+ @Override
|
|
|
|
+ public void addUserRolePrivilege(SysUser sysUser, Long roleId) {
|
|
|
|
+ List<SysRolePrivilege> sysRolePrivilegeList = cacheService.rolePrivilegeCache(roleId);
|
|
|
|
+ List<SysUserRole> sysUserRoleList = new ArrayList<>();
|
|
|
|
+ sysRolePrivilegeList.forEach(s -> {
|
|
|
|
+ sysUserRoleList.add(new SysUserRole(sysUser.getId(), s.getRoleId(), s.getPrivilegeId()));
|
|
|
|
+ });
|
|
|
|
+ sysUserRoleService.saveBatch(sysUserRoleList);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 获取用户角色权限
|
|
|
|
+ *
|
|
|
|
+ * @param userId
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ @Override
|
|
|
|
+ public List<SysUserRole> getUserRolePrivilege(Long userId) {
|
|
|
|
+ QueryWrapper<SysUserRole> sysUserRoleQueryWrapper = new QueryWrapper<>();
|
|
|
|
+ sysUserRoleQueryWrapper.lambda().eq(SysUserRole::getUserId, userId)
|
|
|
|
+ .eq(SysUserRole::getEnable, true);
|
|
|
|
+ return sysUserRoleService.list(sysUserRoleQueryWrapper);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 获取角色权限
|
|
|
|
+ *
|
|
|
|
+ * @param roleId
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ @Override
|
|
|
|
+ public List<SysRolePrivilege> getRolePrivilege(Long roleId) {
|
|
|
|
+ QueryWrapper<SysRolePrivilege> sysRolePrivilegeQueryWrapper = new QueryWrapper<>();
|
|
|
|
+ sysRolePrivilegeQueryWrapper.lambda().eq(SysRolePrivilege::getRoleId, roleId);
|
|
|
|
+ return sysRolePrivilegeService.list(sysRolePrivilegeQueryWrapper);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 获取用户权限
|
|
|
|
+ *
|
|
|
|
+ * @param userId
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ @Override
|
|
|
|
+ public AuthBean getUserAuth(Long userId) {
|
|
|
|
+ AuthBean authBean = null;
|
|
|
|
+ try {
|
|
|
|
+ CacheService cacheService = SpringContextHolder.getBean(CacheService.class);
|
|
|
|
+ SysUser user = cacheService.userCache(userId);
|
|
|
|
+ if (Objects.isNull(user)) {
|
|
|
|
+ throw ExceptionResultEnum.USER_NO_DATA.exception();
|
|
|
|
+ }
|
|
|
|
+ //查询用户角色和权限
|
|
|
|
+ List<SysUserRole> sysUserRoleList = cacheService.userRolePrivilegeCache(user.getId());
|
|
|
|
+ if (Objects.nonNull(sysUserRoleList) && sysUserRoleList.size() > 0) {
|
|
|
|
+ Set<Long> roleIds = new HashSet<>();
|
|
|
|
+ Set<Long> privilegeIds = new HashSet<>();
|
|
|
|
+ sysUserRoleList.forEach(s -> {
|
|
|
|
+ roleIds.add(s.getRoleId());
|
|
|
|
+ privilegeIds.add(s.getPrivilegeId());
|
|
|
|
+ });
|
|
|
|
+ QueryWrapper<SysRole> sysRoleQueryWrapper = new QueryWrapper<>();
|
|
|
|
+ sysRoleQueryWrapper.lambda().in(SysRole::getId, roleIds)
|
|
|
|
+ .eq(SysRole::getEnable, true);
|
|
|
|
+ List<SysRole> sysRoleList = sysRoleService.list(sysRoleQueryWrapper);
|
|
|
|
+ long count = 0;
|
|
|
|
+ if (Objects.nonNull(sysRoleList) && sysRoleList.size() > 0) {
|
|
|
|
+ count = sysRoleList.stream().filter(s -> s.getType() == RoleTypeEnum.ADMIN).count();
|
|
|
|
+ }
|
|
|
|
+ QueryWrapper<SysPrivilege> sysPrivilegeQueryWrapper = new QueryWrapper<>();
|
|
|
|
+ if (count > 0) {//超级系统管理员
|
|
|
|
+ sysPrivilegeQueryWrapper.lambda().eq(SysPrivilege::getType, PrivilegeEnum.URL)
|
|
|
|
+ .eq(SysPrivilege::getProperty, PrivilegePropertyEnum.AUTH);
|
|
|
|
+ List<SysPrivilege> sysPrivilegeList = sysPrivilegeService.list(sysPrivilegeQueryWrapper);
|
|
|
|
+ authBean = new AuthBean(sysRoleList, sysPrivilegeList.stream().map(s -> s.getUrl()).collect(Collectors.toSet()));
|
|
|
|
+ } else {
|
|
|
|
+ BasicSchool tbSchool = cacheService.schoolCache(user.getSchoolId());
|
|
|
|
+ SysOrg org = cacheService.orgCache(user.getOrgId());
|
|
|
|
+ sysPrivilegeQueryWrapper.lambda().in(SysPrivilege::getId, privilegeIds)
|
|
|
|
+ .eq(SysPrivilege::getProperty, PrivilegePropertyEnum.AUTH);
|
|
|
|
+ List<SysPrivilege> sysPrivilegeList = sysPrivilegeService.list(sysPrivilegeQueryWrapper);
|
|
|
|
+ authBean = new AuthBean(sysRoleList, sysPrivilegeList.stream().map(s -> s.getUrl()).collect(Collectors.toSet()), tbSchool, org);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ log.error("请求出错", e);
|
|
|
|
+ throw ExceptionResultEnum.ERROR.exception("添加用户鉴权缓存失败");
|
|
|
|
+ }
|
|
|
|
+ return authBean;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 获取鉴权url
|
|
|
|
+ *
|
|
|
|
+ * @param privilegePropertyEnum
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ @Override
|
|
|
|
+ public List<String> getPrivilegeUrl(PrivilegePropertyEnum privilegePropertyEnum) {
|
|
|
|
+ QueryWrapper<SysPrivilege> sysPrivilegeQueryWrapper = new QueryWrapper<>();
|
|
|
|
+ sysPrivilegeQueryWrapper.lambda().eq(SysPrivilege::getType, PrivilegeEnum.URL)
|
|
|
|
+ .eq(SysPrivilege::getProperty, privilegePropertyEnum)
|
|
|
|
+ .eq(SysPrivilege::getEnable, true);
|
|
|
|
+ List<SysPrivilege> sysPrivilegeList = sysPrivilegeService.list(sysPrivilegeQueryWrapper);
|
|
|
|
+ return Objects.nonNull(sysPrivilegeList) && sysPrivilegeList.size() > 0 ? sysPrivilegeList.stream().map(s -> s.getUrl()).collect(Collectors.toList()) : null;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 删除用户信息
|
|
|
|
+ *
|
|
|
|
+ * @param userId
|
|
|
|
+ */
|
|
|
|
+ @Override
|
|
|
|
+ public void removeUserInfo(Long userId) throws NoSuchAlgorithmException {
|
|
|
|
+ AuthBean authBean = cacheService.userAuthCache(userId);
|
|
|
|
+ if (Objects.isNull(authBean)) {
|
|
|
|
+ throw ExceptionResultEnum.NOT_LOGIN.exception();
|
|
|
|
+ }
|
|
|
|
+ for (Platform p : Platform.values()) {
|
|
|
|
+ String sessionId = SessionUtil.digest(userId, Math.abs(authBean.getRoleList().stream().map(s -> s.getType()).collect(Collectors.toSet()).hashCode()), p.name());
|
|
|
|
+ tbSessionService.removeById(sessionId);
|
|
|
|
+ redisUtil.deleteUserSession(sessionId);
|
|
|
|
+ }
|
|
|
|
+ cacheService.removeUserCache(userId);
|
|
|
|
+ cacheService.removeUserAuthCache(userId);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 修改角色状态
|
|
|
|
+ *
|
|
|
|
+ * @param roleIds
|
|
|
|
+ * @param enable
|
|
|
|
+ */
|
|
|
|
+ @Override
|
|
|
|
+ public void updateRoleEnable(Long[] roleIds, Boolean enable) {
|
|
|
|
+ SysUser requestUser = (SysUser) ServletUtil.getRequestUser();
|
|
|
|
+ List<SysRole> dbRoleList = sysRoleService.listByIds(Arrays.asList(roleIds));
|
|
|
|
+ if (Objects.isNull(dbRoleList) || dbRoleList.size() == 0) {
|
|
|
|
+ throw ExceptionResultEnum.ROLE_NO_DATA.exception();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ //修改角色
|
|
|
|
+ UpdateWrapper<SysRole> sysRoleUpdateWrapper = new UpdateWrapper<>();
|
|
|
|
+ sysRoleUpdateWrapper.lambda().in(SysRole::getId, roleIds)
|
|
|
|
+ .set(SysRole::getEnable, enable)
|
|
|
|
+ .set(SysRole::getUpdateId, requestUser.getId())
|
|
|
|
+ .set(SysRole::getUpdateTime, System.currentTimeMillis());
|
|
|
|
+ sysRoleService.update(sysRoleUpdateWrapper);
|
|
|
|
+
|
|
|
|
+ //修改角色权限
|
|
|
|
+ UpdateWrapper<SysRolePrivilege> sysRolePrivilegeUpdateWrapper = new UpdateWrapper<>();
|
|
|
|
+ sysRolePrivilegeUpdateWrapper.lambda().in(SysRolePrivilege::getRoleId, roleIds)
|
|
|
|
+ .set(SysRolePrivilege::getEnable, enable);
|
|
|
|
+ sysRolePrivilegeService.update(sysRolePrivilegeUpdateWrapper);
|
|
|
|
+
|
|
|
|
+ //修改用户角色权限
|
|
|
|
+ UpdateWrapper<SysUserRole> sysUserRoleUpdateWrapper = new UpdateWrapper<>();
|
|
|
|
+ sysUserRoleUpdateWrapper.lambda().in(SysUserRole::getRoleId, roleIds)
|
|
|
|
+ .set(SysUserRole::getEnable, enable);
|
|
|
|
+ sysUserRoleService.update(sysUserRoleUpdateWrapper);
|
|
|
|
+
|
|
|
|
+ List<Long> updateRoleIds = new ArrayList<>();
|
|
|
|
+ for (SysRole dbRole : dbRoleList) {
|
|
|
|
+ //如果数据库的状态和修改的状态不同
|
|
|
|
+ if (Objects.nonNull(dbRole.getEnable()) && dbRole.getEnable().booleanValue() != enable.booleanValue()) {
|
|
|
|
+ //更新角色权限
|
|
|
|
+ cacheService.updateRolePrivilegeCache(dbRole.getId());
|
|
|
|
+ updateRoleIds.add(dbRole.getId());
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ //更新用户权限状态
|
|
|
|
+ QueryWrapper<SysUserRole> sysUserRoleQueryWrapper = new QueryWrapper<>();
|
|
|
|
+ sysUserRoleQueryWrapper.lambda().eq(SysUserRole::getRoleId, updateRoleIds);
|
|
|
|
+ List<SysUserRole> sysUserRoleList = sysUserRoleService.list(sysUserRoleQueryWrapper);
|
|
|
|
+ Set<Long> userIds = sysUserRoleList.stream().map(s -> s.getUserId()).collect(Collectors.toSet());
|
|
|
|
+ for (Long l : userIds) {
|
|
|
|
+ cacheService.updateUserRolePrivilegeCache(l);
|
|
|
|
+ cacheService.updateUserAuthCache(l);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 查询用户或者角色权限信息
|
|
|
|
+ *
|
|
|
|
+ * @param privilegeCacheDtoList
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ @Override
|
|
|
|
+ public RolePrivilegeResult getUserOrRolePrivilege(List<PrivilegeCacheDto> privilegeCacheDtoList) {
|
|
|
|
+ LinkedMultiValueMap<Long, PrivilegeResult> buttonsLinkedMultiValueMap = new LinkedMultiValueMap<>(), linksLinkedMultiValueMap = new LinkedMultiValueMap<>();
|
|
|
|
+ List<PrivilegeCacheDto> buttonsFilterList = new ArrayList<>(), linksFilterList = new ArrayList<>();
|
|
|
|
+
|
|
|
|
+ for (PrivilegeCacheDto privilegeCacheDto : privilegeCacheDtoList) {
|
|
|
|
+ if (privilegeCacheDto.getPrivilegeType() == PrivilegeEnum.BUTTON) {
|
|
|
|
+ buttonsFilterList.add(privilegeCacheDto);
|
|
|
|
+ buttonsLinkedMultiValueMap.add(privilegeCacheDto.getParentId(), new PrivilegeResult(privilegeCacheDto));
|
|
|
|
+ } else if (privilegeCacheDto.getPrivilegeType() == PrivilegeEnum.LINK) {
|
|
|
|
+ linksFilterList.add(privilegeCacheDto);
|
|
|
|
+ linksLinkedMultiValueMap.add(privilegeCacheDto.getParentId(), new PrivilegeResult(privilegeCacheDto));
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ privilegeCacheDtoList.removeAll(buttonsFilterList);
|
|
|
|
+ privilegeCacheDtoList.removeAll(linksFilterList);
|
|
|
|
+
|
|
|
|
+ RolePrivilegeResult rolePrivilegeResult = new RolePrivilegeResult();
|
|
|
|
+ List<PrivilegeResult> privilegeResultList = new ArrayList<>();
|
|
|
|
+ for (PrivilegeCacheDto privilegeCacheDto : privilegeCacheDtoList) {
|
|
|
|
+ rolePrivilegeResult.setInfo(privilegeCacheDto);
|
|
|
|
+ PrivilegeResult privilegeResult = new PrivilegeResult(privilegeCacheDto);
|
|
|
|
+ privilegeResult.setButtons(buttonsLinkedMultiValueMap.get(privilegeResult.getId()));
|
|
|
|
+ privilegeResult.setLinks(linksLinkedMultiValueMap.get(privilegeResult.getId()));
|
|
|
|
+ privilegeResultList.add(privilegeResult);
|
|
|
|
+ }
|
|
|
|
+ rolePrivilegeResult.setPrivileges(privilegeResultList);
|
|
|
|
+ return rolePrivilegeResult;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 文件预览
|
|
|
|
+ *
|
|
|
|
+ * @param path
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ @Override
|
|
|
|
+ public String filePreview(String path) {
|
|
|
|
+ String url = null;
|
|
|
|
+ JSONObject jsonObject = JSONObject.parseObject(path);
|
|
|
|
+ String attachmentType = (String) jsonObject.get(SystemConstant.TYPE);
|
|
|
|
+ String filePath = (String) jsonObject.get(SystemConstant.PATH);
|
|
|
|
+ UploadFileEnum uploadFileEnum = UploadFileEnum.valueOf((String) jsonObject.get(SystemConstant.UPLOAD_TYPE));
|
|
|
|
+ if (Objects.equals(attachmentType, SystemConstant.LOCAL)) {
|
|
|
|
+ url = SystemConstant.HTTP + dictionaryConfig.sysDomain().getFileHost() + File.separator + filePath;
|
|
|
|
+ } else {
|
|
|
|
+ if (uploadFileEnum == UploadFileEnum.PAPER) {
|
|
|
|
+ url = ossUtil.getPrivateUrl(filePath);
|
|
|
|
+ } else {
|
|
|
|
+ url = dictionaryConfig.aliYunOssDomain().getUrl() + File.separator + filePath;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ return url;
|
|
|
|
+ }
|
|
|
|
+}
|