2021-03-16 16:59:49 +08:00
|
|
|
|
package io.dataease.service.panel;
|
|
|
|
|
|
2021-07-03 18:01:30 +08:00
|
|
|
|
import com.google.gson.Gson;
|
2021-03-19 13:05:14 +08:00
|
|
|
|
import io.dataease.auth.api.dto.CurrentRoleDto;
|
|
|
|
|
import io.dataease.auth.api.dto.CurrentUserDto;
|
2021-07-03 18:01:30 +08:00
|
|
|
|
import io.dataease.base.domain.PanelGroup;
|
2021-03-16 16:59:49 +08:00
|
|
|
|
import io.dataease.base.domain.PanelShare;
|
|
|
|
|
import io.dataease.base.domain.PanelShareExample;
|
2021-07-03 18:01:30 +08:00
|
|
|
|
import io.dataease.base.mapper.PanelGroupMapper;
|
2021-03-16 16:59:49 +08:00
|
|
|
|
import io.dataease.base.mapper.PanelShareMapper;
|
|
|
|
|
import io.dataease.base.mapper.ext.ExtPanelShareMapper;
|
|
|
|
|
import io.dataease.base.mapper.ext.query.GridExample;
|
2021-07-03 18:01:30 +08:00
|
|
|
|
import io.dataease.commons.model.AuthURD;
|
2021-03-16 16:59:49 +08:00
|
|
|
|
import io.dataease.commons.utils.AuthUtils;
|
2021-03-18 18:30:10 +08:00
|
|
|
|
import io.dataease.commons.utils.BeanUtils;
|
2021-03-16 16:59:49 +08:00
|
|
|
|
import io.dataease.commons.utils.CommonBeanFactory;
|
2021-07-05 18:18:06 +08:00
|
|
|
|
import io.dataease.controller.request.panel.PanelShareFineDto;
|
2021-11-10 18:04:20 +08:00
|
|
|
|
import io.dataease.controller.request.panel.PanelShareRemoveRequest;
|
2021-03-16 16:59:49 +08:00
|
|
|
|
import io.dataease.controller.request.panel.PanelShareRequest;
|
|
|
|
|
import io.dataease.controller.sys.base.BaseGridRequest;
|
|
|
|
|
import io.dataease.dto.panel.PanelShareDto;
|
2021-11-10 18:04:20 +08:00
|
|
|
|
import io.dataease.dto.panel.PanelShareOutDTO;
|
2021-03-18 18:30:10 +08:00
|
|
|
|
import io.dataease.dto.panel.PanelSharePo;
|
2021-07-02 19:19:38 +08:00
|
|
|
|
import io.dataease.service.message.DeMsgutil;
|
2021-07-05 18:18:06 +08:00
|
|
|
|
import lombok.Data;
|
2021-03-16 16:59:49 +08:00
|
|
|
|
import org.apache.commons.collections.CollectionUtils;
|
2021-05-25 15:52:28 +08:00
|
|
|
|
import org.apache.commons.lang3.StringUtils;
|
2021-03-16 16:59:49 +08:00
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
import javax.annotation.Resource;
|
2021-07-02 19:19:38 +08:00
|
|
|
|
import java.util.*;
|
2021-03-16 16:59:49 +08:00
|
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Service
|
|
|
|
|
public class ShareService {
|
|
|
|
|
|
|
|
|
|
@Autowired(required = false)
|
|
|
|
|
private PanelShareMapper mapper;
|
|
|
|
|
|
2021-07-03 18:01:30 +08:00
|
|
|
|
@Resource
|
|
|
|
|
private PanelGroupMapper panelGroupMapper;
|
|
|
|
|
|
2021-03-16 16:59:49 +08:00
|
|
|
|
@Resource
|
|
|
|
|
private ExtPanelShareMapper extPanelShareMapper;
|
|
|
|
|
|
2021-07-05 18:18:06 +08:00
|
|
|
|
/**
|
|
|
|
|
* 1.查询当前节点已经分享给了哪些目标
|
|
|
|
|
* 2.过滤出新增的目标
|
|
|
|
|
* 3.过滤出减少的目标
|
|
|
|
|
* 4.批量删除
|
|
|
|
|
* 5.批量新增
|
|
|
|
|
* 6.发送取消分享消息
|
|
|
|
|
* 7.发送新增分享消息
|
|
|
|
|
* @param panelShareFineDto
|
|
|
|
|
*/
|
|
|
|
|
@Transactional
|
|
|
|
|
public void fineSave(PanelShareFineDto panelShareFineDto) {
|
|
|
|
|
|
|
|
|
|
List<PanelShare> addShares = new ArrayList<>();//新增的分享
|
|
|
|
|
List<Long> redShareIdLists = new ArrayList<>();//取消的分享
|
|
|
|
|
|
|
|
|
|
String panelGroupId = panelShareFineDto.getResourceId();
|
|
|
|
|
AuthURD authURD = panelShareFineDto.getAuthURD();
|
|
|
|
|
AuthURD sharedAuthURD = new AuthURD();
|
|
|
|
|
AuthURD addAuthURD = new AuthURD();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Map<Integer, List<Long>> authURDMap = new HashMap<>();
|
|
|
|
|
authURDMap.put(0, authURD.getUserIds());
|
|
|
|
|
authURDMap.put(1, authURD.getRoleIds());
|
|
|
|
|
authURDMap.put(2, authURD.getDeptIds());
|
|
|
|
|
|
|
|
|
|
PanelShareExample example = new PanelShareExample();
|
|
|
|
|
example.createCriteria().andPanelGroupIdEqualTo(panelGroupId);
|
|
|
|
|
List<PanelShare> panelShares = mapper.selectByExample(example);
|
|
|
|
|
Map<Integer, List<TempShareNode>> typeSharedMap = panelShares.stream().map(this::convertNode).collect(Collectors.groupingBy(TempShareNode::getType));
|
|
|
|
|
|
|
|
|
|
for (Map.Entry<Integer, List<Long>> entry : authURDMap.entrySet()) {
|
|
|
|
|
Integer key = entry.getKey();
|
2021-11-22 18:34:49 +08:00
|
|
|
|
List<TempShareNode> shareNodes;
|
2021-07-05 18:18:06 +08:00
|
|
|
|
if (null == typeSharedMap || null == typeSharedMap.get(key)) {
|
|
|
|
|
shareNodes = new ArrayList<>();
|
|
|
|
|
}else{
|
|
|
|
|
shareNodes = typeSharedMap.get(key);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (null != authURDMap.get(key)) {
|
|
|
|
|
Map<String, Object> dataMap = filterData(authURDMap.get(key), shareNodes);
|
|
|
|
|
List<Long> newIds = (List<Long>)dataMap.get("add");
|
|
|
|
|
for (int i = 0; i < newIds.size(); i++) {
|
|
|
|
|
Long id = newIds.get(i);
|
|
|
|
|
PanelShare share = new PanelShare();
|
|
|
|
|
share.setCreateTime(System.currentTimeMillis());
|
|
|
|
|
share.setPanelGroupId(panelGroupId);
|
|
|
|
|
share.setTargetId(id);
|
|
|
|
|
share.setType(key);
|
|
|
|
|
addShares.add(share);
|
|
|
|
|
}
|
|
|
|
|
List<TempShareNode> redNodes = (List<TempShareNode>)dataMap.get("red");
|
|
|
|
|
List<Long> redIds = redNodes.stream().map(TempShareNode::getShareId).distinct().collect(Collectors.toList());
|
|
|
|
|
|
|
|
|
|
redShareIdLists.addAll(redIds);
|
|
|
|
|
buildRedAuthURD(key, redNodes.stream().map(TempShareNode::getTargetId).distinct().collect(Collectors.toList()) , sharedAuthURD);
|
|
|
|
|
buildRedAuthURD(key, newIds, addAuthURD);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (CollectionUtils.isNotEmpty(redShareIdLists)){
|
|
|
|
|
extPanelShareMapper.batchDelete(redShareIdLists);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (CollectionUtils.isNotEmpty(addShares)){
|
2021-11-09 18:23:45 +08:00
|
|
|
|
extPanelShareMapper.batchInsert(addShares, AuthUtils.getUser().getUsername());
|
2021-07-05 18:18:06 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 以上是业务代码
|
|
|
|
|
// 下面是消息发送
|
|
|
|
|
Set<Long> addUserIdSet = AuthUtils.userIdsByURD(addAuthURD);
|
|
|
|
|
Set<Long> redUserIdSet = AuthUtils.userIdsByURD(sharedAuthURD);
|
2021-11-22 18:34:49 +08:00
|
|
|
|
PanelGroup panelGroup = panelGroupMapper.selectByPrimaryKey(panelGroupId);
|
2021-07-05 18:18:06 +08:00
|
|
|
|
CurrentUserDto user = AuthUtils.getUser();
|
|
|
|
|
Gson gson = new Gson();
|
|
|
|
|
String msg = panelGroup.getName();
|
|
|
|
|
|
2021-11-22 18:34:49 +08:00
|
|
|
|
List<String> msgParam = new ArrayList<>();
|
2021-07-05 18:18:06 +08:00
|
|
|
|
msgParam.add(panelGroupId);
|
|
|
|
|
addUserIdSet.forEach(userId -> {
|
2021-11-22 18:34:49 +08:00
|
|
|
|
if (!redUserIdSet.contains(userId) && !user.getUserId().equals(userId)){
|
2021-11-15 16:23:24 +08:00
|
|
|
|
DeMsgutil.sendMsg(userId, 2L,user.getNickName()+" 分享了仪表板【"+msg+"】,请查收!", gson.toJson(msgParam));
|
2021-07-05 18:18:06 +08:00
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
redUserIdSet.forEach(userId -> {
|
2021-11-22 18:34:49 +08:00
|
|
|
|
if (!addUserIdSet.contains(userId) && !user.getUserId().equals(userId)){
|
2021-11-15 16:23:24 +08:00
|
|
|
|
DeMsgutil.sendMsg(userId, 3L, user.getNickName()+" 取消分享了仪表板【"+msg+"】,请查收!", gson.toJson(msgParam));
|
2021-07-05 18:18:06 +08:00
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void buildRedAuthURD(Integer type, List<Long> redIds , AuthURD authURD) {
|
|
|
|
|
if (type == 0) {
|
|
|
|
|
authURD.setUserIds(redIds);
|
|
|
|
|
}
|
|
|
|
|
if (type == 1) {
|
|
|
|
|
authURD.setRoleIds(redIds);
|
|
|
|
|
}
|
|
|
|
|
if (type == 2) {
|
|
|
|
|
authURD.setDeptIds(redIds);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
* @param newTargets 新的分享目标
|
|
|
|
|
* @param shareNodes 已景分享目标
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
private Map<String, Object> filterData(List<Long> newTargets, List<TempShareNode> shareNodes) {
|
|
|
|
|
Map<String, Object> result = new HashMap<>();
|
|
|
|
|
List<Long> newUserIds = new ArrayList<>();
|
|
|
|
|
for (int i = 0; i < newTargets.size(); i++) {
|
|
|
|
|
Long newTargetId = newTargets.get(i);
|
|
|
|
|
Boolean isNew = true;
|
|
|
|
|
for (int j = 0; j < shareNodes.size(); j++) {
|
|
|
|
|
TempShareNode shareNode = shareNodes.get(j);
|
|
|
|
|
Long sharedId = shareNode.getTargetId();
|
2021-11-22 18:34:49 +08:00
|
|
|
|
if (newTargetId.equals(sharedId)) {
|
2021-07-05 18:18:06 +08:00
|
|
|
|
shareNode.setMatched(true); // 已分享 重新命中
|
|
|
|
|
isNew = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (isNew) {
|
|
|
|
|
// 获取新增的
|
|
|
|
|
newUserIds.add(newTargetId);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
//获取需要取消分享的
|
|
|
|
|
List<TempShareNode> missNodes = shareNodes.stream().filter(item -> !item.getMatched()).collect(Collectors.toList());
|
|
|
|
|
result.put("add", newUserIds);
|
|
|
|
|
result.put("red", missNodes);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Data
|
|
|
|
|
private class TempShareNode {
|
|
|
|
|
private Long shareId;
|
|
|
|
|
private Integer type;
|
|
|
|
|
private Long targetId;
|
|
|
|
|
private Boolean matched = false;
|
|
|
|
|
|
|
|
|
|
public boolean targetMatch(Long tid) {
|
2021-11-22 18:34:49 +08:00
|
|
|
|
return targetId.equals(tid);
|
2021-07-05 18:18:06 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private TempShareNode convertNode(PanelShare panelShare) {
|
|
|
|
|
return BeanUtils.copyBean(new TempShareNode(), panelShare);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2021-03-16 16:59:49 +08:00
|
|
|
|
@Transactional
|
|
|
|
|
public void save(PanelShareRequest request){
|
2021-07-03 18:01:30 +08:00
|
|
|
|
List<PanelGroup> panelGroups = queryGroup(request.getPanelIds());
|
2021-03-16 16:59:49 +08:00
|
|
|
|
//1.先根据仪表板删除所有已经分享的
|
2021-03-18 16:55:31 +08:00
|
|
|
|
Integer type = request.getType();
|
2021-03-16 16:59:49 +08:00
|
|
|
|
List<String> panelIds = request.getPanelIds();
|
2021-03-18 16:55:31 +08:00
|
|
|
|
List<Long> targetIds = request.getTargetIds();
|
2021-03-16 16:59:49 +08:00
|
|
|
|
// 使用原生对象会导致事物失效 所以这里需要使用spring代理对象
|
|
|
|
|
if (CollectionUtils.isNotEmpty(panelIds)){
|
|
|
|
|
ShareService proxy = CommonBeanFactory.getBean(ShareService.class);
|
2021-11-22 18:34:49 +08:00
|
|
|
|
panelIds.forEach(panelId -> proxy.delete(panelId, type));
|
2021-03-16 16:59:49 +08:00
|
|
|
|
}
|
2021-03-18 16:55:31 +08:00
|
|
|
|
if (CollectionUtils.isEmpty(targetIds)) return;
|
|
|
|
|
|
2021-03-16 16:59:49 +08:00
|
|
|
|
long now = System.currentTimeMillis();
|
|
|
|
|
List<PanelShare> shares = panelIds.stream().flatMap(panelId ->
|
2021-03-18 16:55:31 +08:00
|
|
|
|
targetIds.stream().map(targetId -> {
|
2021-03-16 16:59:49 +08:00
|
|
|
|
PanelShare share = new PanelShare();
|
|
|
|
|
share.setCreateTime(now);
|
|
|
|
|
share.setPanelGroupId(panelId);
|
2021-03-18 16:55:31 +08:00
|
|
|
|
share.setTargetId(targetId);
|
|
|
|
|
share.setType(type);
|
2021-03-16 16:59:49 +08:00
|
|
|
|
return share;
|
|
|
|
|
})
|
|
|
|
|
).collect(Collectors.toList());
|
|
|
|
|
if (CollectionUtils.isNotEmpty(shares)){
|
2021-11-09 18:23:45 +08:00
|
|
|
|
extPanelShareMapper.batchInsert(shares, AuthUtils.getUser().getUsername());
|
2021-03-16 16:59:49 +08:00
|
|
|
|
}
|
2021-07-02 19:19:38 +08:00
|
|
|
|
|
|
|
|
|
// 下面是发送提醒消息逻辑
|
2021-11-22 18:34:49 +08:00
|
|
|
|
Set<Long> userIdSet;
|
2021-07-03 18:01:30 +08:00
|
|
|
|
AuthURD authURD = new AuthURD();
|
2021-07-02 19:19:38 +08:00
|
|
|
|
if (type == 0) {
|
2021-07-03 18:01:30 +08:00
|
|
|
|
authURD.setUserIds(targetIds);
|
|
|
|
|
}
|
|
|
|
|
if (type == 1) {
|
|
|
|
|
authURD.setRoleIds(targetIds);
|
2021-07-02 19:19:38 +08:00
|
|
|
|
}
|
2021-07-03 18:01:30 +08:00
|
|
|
|
if(type == 2) {
|
|
|
|
|
authURD.setDeptIds(targetIds);
|
|
|
|
|
}
|
|
|
|
|
userIdSet = AuthUtils.userIdsByURD(authURD);
|
|
|
|
|
|
2021-07-02 19:19:38 +08:00
|
|
|
|
CurrentUserDto user = AuthUtils.getUser();
|
2021-07-03 18:01:30 +08:00
|
|
|
|
String msg = StringUtils.joinWith(",", panelGroups.stream().map(PanelGroup::getName).collect(Collectors.toList()));
|
|
|
|
|
Gson gson = new Gson();
|
2021-11-22 18:34:49 +08:00
|
|
|
|
userIdSet.forEach(userId -> DeMsgutil.sendMsg(userId, 2L, user.getNickName()+" 分享了仪表板【"+msg+"】给您,请查收!", gson.toJson(panelIds)));
|
2021-07-02 19:19:38 +08:00
|
|
|
|
|
2021-03-16 16:59:49 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-07-03 18:01:30 +08:00
|
|
|
|
private List<PanelGroup> queryGroup(List<String> panelIds) {
|
|
|
|
|
return panelIds.stream().map(panelGroupMapper::selectByPrimaryKey).collect(Collectors.toList());
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-16 16:59:49 +08:00
|
|
|
|
/**
|
|
|
|
|
* panel_group_id建了索引 效率不会很差
|
|
|
|
|
* @param panel_group_id
|
|
|
|
|
*/
|
|
|
|
|
@Transactional
|
2021-03-18 16:55:31 +08:00
|
|
|
|
public void delete(String panel_group_id, Integer type){
|
2021-03-16 16:59:49 +08:00
|
|
|
|
PanelShareExample example = new PanelShareExample();
|
2021-05-21 10:52:19 +08:00
|
|
|
|
PanelShareExample.Criteria criteria = example.createCriteria();
|
|
|
|
|
criteria.andPanelGroupIdEqualTo(panel_group_id);
|
|
|
|
|
if(type != null){
|
|
|
|
|
criteria.andTypeEqualTo(type);
|
|
|
|
|
}
|
2021-03-16 16:59:49 +08:00
|
|
|
|
mapper.deleteByExample(example);
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-09 18:23:45 +08:00
|
|
|
|
public List<PanelSharePo> shareOut() {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<PanelSharePo> queryShareOut() {
|
|
|
|
|
String username = AuthUtils.getUser().getUsername();
|
2021-11-22 18:34:49 +08:00
|
|
|
|
return extPanelShareMapper.queryOut(username);
|
2021-11-09 18:23:45 +08:00
|
|
|
|
}
|
2021-03-16 16:59:49 +08:00
|
|
|
|
|
|
|
|
|
public List<PanelShareDto> queryTree(BaseGridRequest request){
|
2021-03-19 13:05:14 +08:00
|
|
|
|
CurrentUserDto user = AuthUtils.getUser();
|
2021-03-18 16:55:31 +08:00
|
|
|
|
Long userId = user.getUserId();
|
|
|
|
|
Long deptId = user.getDeptId();
|
2021-03-19 13:05:14 +08:00
|
|
|
|
List<Long> roleIds = user.getRoles().stream().map(CurrentRoleDto::getId).collect(Collectors.toList());
|
2021-03-18 16:55:31 +08:00
|
|
|
|
|
2021-06-02 21:49:51 +08:00
|
|
|
|
Map<String, Object> param = new HashMap<>();
|
|
|
|
|
param.put("userId", userId);
|
|
|
|
|
param.put("deptId", deptId);
|
|
|
|
|
param.put("roleIds", roleIds);
|
|
|
|
|
|
|
|
|
|
List<PanelSharePo> datas = extPanelShareMapper.query(param);
|
2021-03-18 18:30:10 +08:00
|
|
|
|
List<PanelShareDto> dtoLists = datas.stream().map(po -> BeanUtils.copyBean(new PanelShareDto(), po)).collect(Collectors.toList());
|
|
|
|
|
return convertTree(dtoLists);
|
2021-03-16 16:59:49 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//List构建Tree
|
|
|
|
|
private List<PanelShareDto> convertTree(List<PanelShareDto> datas){
|
2021-06-02 21:49:51 +08:00
|
|
|
|
String username = AuthUtils.getUser().getUsername();
|
|
|
|
|
Map<String, List<PanelShareDto>> map = datas.stream().filter(panelShareDto -> StringUtils.isNotEmpty(panelShareDto.getCreator()) && !StringUtils.equals(username, panelShareDto.getCreator())).collect(Collectors.groupingBy(PanelShareDto::getCreator));
|
2021-03-18 18:30:10 +08:00
|
|
|
|
return map.entrySet().stream().map(entry -> {
|
|
|
|
|
PanelShareDto panelShareDto = new PanelShareDto();
|
|
|
|
|
panelShareDto.setName(entry.getKey());
|
|
|
|
|
panelShareDto.setChildren(entry.getValue());
|
|
|
|
|
return panelShareDto;
|
|
|
|
|
}).collect(Collectors.toList());
|
2021-03-16 16:59:49 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-03-17 18:50:29 +08:00
|
|
|
|
public List<PanelShare> queryWithResource(BaseGridRequest request){
|
|
|
|
|
GridExample example = request.convertExample();
|
|
|
|
|
return extPanelShareMapper.queryWithResource(example);
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-10 18:04:20 +08:00
|
|
|
|
public List<PanelShareOutDTO> queryTargets(String panelId) {
|
|
|
|
|
return extPanelShareMapper.queryTargets(panelId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void removeShares(PanelShareRemoveRequest removeRequest) {
|
|
|
|
|
extPanelShareMapper.removeShares(removeRequest);
|
|
|
|
|
}
|
2021-03-16 16:59:49 +08:00
|
|
|
|
|
|
|
|
|
}
|