lyh
2025-01-17 ea704e018a27c26ef6deeaea4adc8a28b4d0b27e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package org.jeecg.modules.dnc.service.impl;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.modules.dnc.entity.ComponentPermission;
import org.jeecg.modules.dnc.mapper.ComponentPermissionMapper;
import org.jeecg.modules.dnc.ucenter.UserDepartExt;
import org.jeecg.modules.dnc.utils.ValidateUtil;
import org.jeecg.modules.system.entity.SysUser;
import org.jeecg.modules.dnc.service.IComponentPermissionService;
import org.apache.commons.collections4.ListUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
 
@Service
public class ComponentPermissionServiceImpl extends ServiceImpl<ComponentPermissionMapper, ComponentPermission> implements IComponentPermissionService {
    @Override
    public ComponentPermission getByComponentIdAndUserId(String componentId, String userId) {
        if(!ValidateUtil.validateString(componentId) || !ValidateUtil.validateString(userId))
            return null;
        List<ComponentPermission>  permissions = super.lambdaQuery().eq(ComponentPermission::getComponentId, componentId).eq(ComponentPermission::getUserId, userId).list();
        if(permissions == null || permissions.isEmpty())
            return null;
        return permissions.get(0);
    }
 
    @Override
    @Transactional(rollbackFor = {Exception.class})
    public boolean deleteByComponentId(String componentId) {
        if(!ValidateUtil.validateString(componentId))
            return false;
        LambdaQueryWrapper<ComponentPermission> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.eq(ComponentPermission::getComponentId, componentId);
        return super.remove(lambdaQueryWrapper);
    }
 
    @Override
    public List<UserDepartExt> getUserPermsByComponentId(String componentId) {
        return super.getBaseMapper().getUserPermsByComponentId(componentId);
    }
 
    @Override
    public List<SysUser> getUserNonPermsByComponentId(String componentId) {
        return super.getBaseMapper().getUserNonPermsByComponentId(componentId);
    }
 
    @Override
    @Transactional(rollbackFor = {Exception.class})
    public boolean removeByCollection(List<ComponentPermission> permissionList) {
        if(permissionList == null || permissionList.isEmpty())
            return false;
        if(permissionList.size() == 1)
            return super.removeById(permissionList.get(0).getComponentPermId());
        List<String> ids = new ArrayList<>();
        permissionList.forEach(item -> {
            ids.add(item.getComponentPermId());
        });
        return super.removeByIds(ids);
    }
 
    @Override
    public List<ComponentPermission> getByComponentId(String componentId) {
        List<ComponentPermission> list = super.lambdaQuery().eq(ComponentPermission::getComponentId, componentId).list();
        if(list == null)
            list = Collections.emptyList();
        return list;
    }
 
    @Override
    public List<ComponentPermission> getByComponentIdsAndUserIds(List<String> componentIds, List<String> userIds) {
        if(componentIds == null || componentIds.isEmpty() || userIds == null || userIds.isEmpty())
            return null;
        List<ComponentPermission> total = new ArrayList<>();
        List<List<String>> compListArr;
        List<List<String>> userListArr;
        if(componentIds.size() > 1000){
            compListArr = ListUtils.partition(componentIds, 100);
        }else {
            compListArr = ListUtils.partition(componentIds, 1000);
        }
        if(userIds.size() > 1000){
            userListArr = ListUtils.partition(userIds, 100);
        }else {
            userListArr = ListUtils.partition(userIds, 1000);
        }
        for(List<String> compList : compListArr) {
            for(List<String> userList : userListArr){
                LambdaQueryWrapper<ComponentPermission> queryWrapper = Wrappers.lambdaQuery();
                queryWrapper.in(ComponentPermission::getComponentId, compList);
                queryWrapper.in(ComponentPermission::getUserId, userList);
                List<ComponentPermission> list = super.list(queryWrapper);
                if(list != null && !list.isEmpty()){
                    total.addAll(list);
                }
            }
        }
        return total;
    }
}