feat: 增加数据集同步消息提醒

This commit is contained in:
fit2cloud-chenyw
2021-07-03 18:01:30 +08:00
parent 25203a6bb8
commit dadeb745b4
23 changed files with 371 additions and 68 deletions

View File

@@ -0,0 +1,12 @@
package io.dataease.auth.service;
import io.dataease.commons.model.AuthURD;
import java.util.Set;
public interface ExtAuthService {
Set<Long> userIdsByRD(AuthURD request);
AuthURD resourceTarget(String resourceId);
}

View File

@@ -0,0 +1,63 @@
package io.dataease.auth.service.impl;
import io.dataease.auth.service.ExtAuthService;
import io.dataease.base.domain.SysAuth;
import io.dataease.base.domain.SysAuthExample;
import io.dataease.base.mapper.SysAuthMapper;
import io.dataease.base.mapper.ext.ExtAuthMapper;
import io.dataease.commons.model.AuthURD;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import javax.annotation.Resource;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
@Service
public class ExtAuthServiceImpl implements ExtAuthService {
@Resource
private ExtAuthMapper extAuthMapper;
@Resource
private SysAuthMapper sysAuthMapper;
@Override
public Set<Long> userIdsByRD(AuthURD request) {
Set<Long> result = new HashSet<>();
List<Long> roleIds = request.getRoleIds();
List<Long> deptIds = request.getDeptIds();
if (!CollectionUtils.isEmpty(roleIds)) {
result.addAll(extAuthMapper.queryUserIdWithRoleIds(roleIds));
}
if (!CollectionUtils.isEmpty(deptIds)) {
result.addAll(extAuthMapper.queryUserIdWithDeptIds(deptIds));
}
return result;
}
@Override
public AuthURD resourceTarget(String resourceId) {
AuthURD authURD = new AuthURD();
SysAuthExample example = new SysAuthExample();
example.createCriteria().andAuthSourceEqualTo(resourceId);
List<SysAuth> sysAuths = sysAuthMapper.selectByExample(example);
Map<String, List<SysAuth>> authMap = sysAuths.stream().collect(Collectors.groupingBy(SysAuth::getAuthTargetType));
if (!CollectionUtils.isEmpty(authMap.get("user"))) {
authURD.setUserIds(authMap.get("user").stream().map(item -> Long.parseLong(item.getAuthTarget())).collect(Collectors.toList()));
}
if (!CollectionUtils.isEmpty(authMap.get("role"))) {
authURD.setUserIds(authMap.get("role").stream().map(item -> Long.parseLong(item.getAuthTarget())).collect(Collectors.toList()));
}
if (!CollectionUtils.isEmpty(authMap.get("dept"))) {
authURD.setUserIds(authMap.get("dept").stream().map(item -> Long.parseLong(item.getAuthTarget())).collect(Collectors.toList()));
}
return authURD;
}
}

View File

@@ -0,0 +1,17 @@
package io.dataease.base.mapper.ext;
import org.apache.ibatis.annotations.Param;
import java.util.List;
public interface ExtAuthMapper {
List<Long> queryUserIdWithRoleIds(@Param("roleIds") List<Long> roleIds);
List<Long> queryUserIdWithDeptIds(@Param("deptIds") List<Long> deptIds);
// Set<Long> queryUserIdWithRD(@Param("roleIds") List<Long> roleIds, @Param("deptIds") List<Long> deptIds);
}

View File

@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="io.dataease.base.mapper.ext.ExtAuthMapper">
<select id="queryUserIdWithRoleIds" resultType="java.lang.Long" >
select user_id
from sys_users_roles
where role_id in
<foreach collection="roleIds" item="roleId" open='(' separator=',' close=')'>
#{roleId}
</foreach>
</select>
<select id="queryUserIdWithDeptIds" resultType="java.lang.Long" >
select user_id
from sys_user
where dept_id in
<foreach collection="deptIds" item="deptId" open='(' separator=',' close=')'>
#{deptId}
</foreach>
</select>
</mapper>

View File

@@ -46,9 +46,9 @@
</select>
<select id="queryUserIdWithRoleIds" >
<select id="queryUserIdWithRoleIds" resultType="java.lang.Long" >
select user_id
from sys_user
from sys_users_roles
where role_id in
<foreach collection="roleIds" item="roleId" open='(' separator=',' close=')'>
#{roleId}
@@ -56,7 +56,7 @@
</select>
<select id="queryUserIdWithDeptIds" >
<select id="queryUserIdWithDeptIds" resultType="java.lang.Long" >
select user_id
from sys_user
where dept_id in

View File

@@ -0,0 +1,19 @@
package io.dataease.commons.model;
import lombok.Data;
import java.io.Serializable;
import java.util.List;
@Data
public class AuthURD implements Serializable {
private List<Long> userIds;
private List<Long> deptIds;
private List<Long> roleIds;
}

View File

@@ -1,23 +1,41 @@
package io.dataease.commons.utils;
import io.dataease.auth.api.dto.CurrentUserDto;
import io.dataease.service.sys.SysUserService;
import io.dataease.auth.service.ExtAuthService;
import io.dataease.commons.model.AuthURD;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import java.util.Set;
@Component
public class AuthUtils {
private static SysUserService sysUserService;
private static ExtAuthService extAuthService;
@Autowired
public void setSysUserService(SysUserService sysUserService) {
AuthUtils.sysUserService = sysUserService;
public void setExtAuthService(ExtAuthService extAuthService) {
AuthUtils.extAuthService = extAuthService;
}
public static CurrentUserDto getUser(){
CurrentUserDto userDto = (CurrentUserDto)SecurityUtils.getSubject().getPrincipal();
return userDto;
}
//根据组织 角色 用户 获取下属用户ID
public static Set<Long> userIdsByURD(AuthURD request) {
Set<Long> userIds = extAuthService.userIdsByRD(request);
if (!CollectionUtils.isEmpty(request.getUserIds())) {
userIds.addAll(request.getUserIds());
}
return userIds;
}
// 获取资源对那些人/角色/组织 有权限
public static AuthURD authURDR(String resourceId) {
return extAuthService.resourceTarget(resourceId);
}
}

View File

@@ -10,10 +10,8 @@ import io.dataease.commons.constants.JdbcConstants;
import io.dataease.commons.constants.JobStatus;
import io.dataease.commons.constants.ScheduleType;
import io.dataease.commons.constants.UpdateType;
import io.dataease.commons.utils.CommonBeanFactory;
import io.dataease.commons.utils.DorisTableUtils;
import io.dataease.commons.utils.HttpClientUtil;
import io.dataease.commons.utils.LogUtil;
import io.dataease.commons.model.AuthURD;
import io.dataease.commons.utils.*;
import io.dataease.datasource.constants.DatasourceTypes;
import io.dataease.datasource.dto.DorisConfigration;
import io.dataease.datasource.dto.MysqlConfigration;
@@ -27,6 +25,7 @@ import io.dataease.dto.dataset.DataTableInfoDTO;
import io.dataease.exception.DataEaseException;
import io.dataease.listener.util.CacheUtils;
import io.dataease.provider.QueryProvider;
import io.dataease.service.message.DeMsgutil;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
@@ -77,8 +76,7 @@ import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.net.InetAddress;
import java.util.ArrayList;
import java.util.List;
import java.util.*;
import java.util.stream.Collectors;
@Service
@@ -209,10 +207,12 @@ public class ExtractDataService {
extractData(datasetTable, "all_scope");
replaceTable(DorisTableUtils.dorisName(datasetTableId));
saveSucessLog(datasetTableTaskLog);
sendWebMsg(datasetTable, taskId,true);
deleteFile("all_scope", datasetTableId);
updateTableStatus(datasetTableId, datasetTable, JobStatus.Completed, execTime);
}catch (Exception e){
saveErrorLog(datasetTableId, taskId, e);
sendWebMsg(datasetTable, taskId,false);
updateTableStatus(datasetTableId, datasetTable, JobStatus.Error, null);
dropDorisTable(DorisTableUtils.dorisTmpName(DorisTableUtils.dorisName(datasetTableId)));
deleteFile("all_scope", datasetTableId);
@@ -233,6 +233,7 @@ public class ExtractDataService {
Long execTime = System.currentTimeMillis();
extractData(datasetTable, "incremental_add");
saveSucessLog(datasetTableTaskLog);
sendWebMsg(datasetTable, taskId,true);
updateTableStatus(datasetTableId, datasetTable, JobStatus.Completed, execTime);
}else {
DatasetTableIncrementalConfig datasetTableIncrementalConfig = dataSetTableService.incrementalConfig(datasetTableId);
@@ -270,12 +271,14 @@ public class ExtractDataService {
extractData(datasetTable, "incremental_delete");
}
saveSucessLog(datasetTableTaskLog);
sendWebMsg(datasetTable, taskId,true);
deleteFile("incremental_add", datasetTableId);
deleteFile("incremental_delete", datasetTableId);
updateTableStatus(datasetTableId, datasetTable, JobStatus.Completed, execTime);
}
}catch (Exception e){
saveErrorLog(datasetTableId, taskId, e);
sendWebMsg(datasetTable, taskId,false);
updateTableStatus(datasetTableId, datasetTable, JobStatus.Error, null);
deleteFile("incremental_add", datasetTableId);
deleteFile("incremental_delete", datasetTableId);
@@ -297,6 +300,20 @@ public class ExtractDataService {
}
private void sendWebMsg(DatasetTable datasetTable, String taskId, Boolean status) {
String msg = status ? "成功" : "失败";
String id = datasetTable.getId();
AuthURD authURD = AuthUtils.authURDR(id);
Set<Long> userIds = AuthUtils.userIdsByURD(authURD);
Gson gson = new Gson();
userIds.forEach(userId -> {
Map<String,Object> param = new HashMap<>();
param.put("tableId", id);
param.put("taskId", taskId);
DeMsgutil.sendMsg(userId, 1, "数据集【"+datasetTable.getName()+"】同步"+msg, gson.toJson(param));
});
}
private void updateTableStatus(String datasetTableId, DatasetTable datasetTable, JobStatus completed, Long execTime) {
datasetTable.setSyncStatus(completed.name());
if(execTime != null){

View File

@@ -16,8 +16,8 @@ public class DeMsgutil {
@PostConstruct
public void init() {
routerMap = new HashMap<>();
routerMap.put(0, "/panel/index");
routerMap.put(1, "/dataset/index");
routerMap.put(0, "panel");
routerMap.put(1, "dataset");
}
private static SysMsgService sysMsgService;
@@ -38,4 +38,16 @@ public class DeMsgutil {
sysMsgService.save(sysMsg);
}
public static void sendMsg(Long userId, int type, String content, String param) {
SysMsg sysMsg = new SysMsg();
sysMsg.setUserId(userId);
sysMsg.setType(type);
sysMsg.setContent(content);
sysMsg.setRouter(routerMap.get(type));
sysMsg.setStatus(false);
sysMsg.setCreateTime(System.currentTimeMillis());
sysMsg.setParam(param);
sysMsgService.save(sysMsg);
}
}

View File

@@ -1,12 +1,16 @@
package io.dataease.service.panel;
import com.google.gson.Gson;
import io.dataease.auth.api.dto.CurrentRoleDto;
import io.dataease.auth.api.dto.CurrentUserDto;
import io.dataease.base.domain.PanelGroup;
import io.dataease.base.domain.PanelShare;
import io.dataease.base.domain.PanelShareExample;
import io.dataease.base.mapper.PanelGroupMapper;
import io.dataease.base.mapper.PanelShareMapper;
import io.dataease.base.mapper.ext.ExtPanelShareMapper;
import io.dataease.base.mapper.ext.query.GridExample;
import io.dataease.commons.model.AuthURD;
import io.dataease.commons.utils.AuthUtils;
import io.dataease.commons.utils.BeanUtils;
import io.dataease.commons.utils.CommonBeanFactory;
@@ -31,12 +35,15 @@ public class ShareService {
@Autowired(required = false)
private PanelShareMapper mapper;
@Resource
private PanelGroupMapper panelGroupMapper;
@Resource
private ExtPanelShareMapper extPanelShareMapper;
@Transactional
public void save(PanelShareRequest request){
List<PanelGroup> panelGroups = queryGroup(request.getPanelIds());
//1.先根据仪表板删除所有已经分享的
Integer type = request.getType();
List<String> panelIds = request.getPanelIds();
@@ -67,26 +74,32 @@ public class ShareService {
// 下面是发送提醒消息逻辑
Set<Long> userIdSet = new HashSet<Long>();
AuthURD authURD = new AuthURD();
if (type == 0) {
userIdSet.addAll(targetIds);
}else if(type == 1) {
Map<String, List<Long>> param = new HashMap<>();
param.put("roleIds", targetIds);
List<Long> userIdList = extPanelShareMapper.queryUserIdWithRoleIds(param);
userIdSet.addAll(userIdList);
} else if (type == 2) {
Map<String, List<Long>> param = new HashMap<>();
param.put("deptIds", targetIds);
List<Long> userIdList = extPanelShareMapper.queryUserIdWithDeptIds(param);
userIdSet.addAll(userIdList);
authURD.setUserIds(targetIds);
}
if (type == 1) {
authURD.setRoleIds(targetIds);
}
if(type == 2) {
authURD.setDeptIds(targetIds);
}
userIdSet = AuthUtils.userIdsByURD(authURD);
CurrentUserDto user = AuthUtils.getUser();
String msg = StringUtils.joinWith("", panelGroups.stream().map(PanelGroup::getName).collect(Collectors.toList()));
Gson gson = new Gson();
userIdSet.forEach(userId -> {
DeMsgutil.sendMsg(userId, 0, "用户 [" + user.getNickName()+"] 分享了仪表板给您,请查收!");
// DeMsgutil.sendMsg(userId, 0, user.getNickName()+" 分享了仪表板【"+msg+"】给您,请查收!");
DeMsgutil.sendMsg(userId, 0, user.getNickName()+" 分享了仪表板【"+msg+"】给您,请查收!", gson.toJson(panelIds));
});
}
private List<PanelGroup> queryGroup(List<String> panelIds) {
return panelIds.stream().map(panelGroupMapper::selectByPrimaryKey).collect(Collectors.toList());
}
/**
* panel_group_id建了索引 效率不会很差
* @param panel_group_id