李林
2023-10-07 658d4927d468c47208fd012d9128b09249c07eff
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package com.chinaztt.mes.aps.service.impl;
 
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chinaztt.mes.aps.dto.ApsResourceGroupDTO;
import com.chinaztt.mes.aps.entity.ApsResourceGroup;
import com.chinaztt.mes.aps.entity.ApsResourceRelation;
import com.chinaztt.mes.aps.mapper.ApsResourceGroupMapper;
import com.chinaztt.mes.aps.service.ApsResourceRelationService;
import com.chinaztt.mes.aps.service.ApsResourceGroupService;
import com.chinaztt.ztt.admin.api.dto.DeptTree;
import com.chinaztt.ztt.admin.api.vo.TreeUtil;
import com.chinaztt.ztt.common.data.datascope.DataScope;
import lombok.AllArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
 
/**
 * @author sunxiaoling
 * @date 2020-08-27 10:32:31
 */
@Service
@AllArgsConstructor
public class ApsResourceGroupServiceImpl extends ServiceImpl<ApsResourceGroupMapper, ApsResourceGroup> implements ApsResourceGroupService {
 
    private final ApsResourceRelationService apsResourceRelationService;
 
    /**
     * 添加资源分组部门
     * @param resource 资源
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean saveResource(ApsResourceGroup resource) {
        ApsResourceGroup apsResource = new ApsResourceGroup();
        BeanUtils.copyProperties(resource, apsResource);
        this.save(apsResource);
        apsResourceRelationService.insertDeptRelation(apsResource);
        return Boolean.TRUE;
    }
    /**
     * 删除资源分组
     * @param id 部门 ID
     * @return 成功、失败
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean removeDeptById(Integer id) {
        // 级联删除部门
        List<Long> idList = apsResourceRelationService
                .list(Wrappers.<ApsResourceRelation>query().lambda().eq(ApsResourceRelation::getAncestry,id.longValue())).stream()
                .map(ApsResourceRelation::getDescendants).collect(Collectors.toList());
 
        if (CollUtil.isNotEmpty(idList)) {
            this.removeByIds(idList);
        }
 
        // 删除部门级联关系
        apsResourceRelationService.deleteAllDeptRealtion(id);
        return Boolean.TRUE;
    }
    /**
     * 更新资源分组
     * @param apsResource 资源分组信息
     * @return 成功、失败
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateDeptById(ApsResourceGroup apsResource) {
        // 更新部门状态
        this.updateById(apsResource);
        // 更新部门关系
        ApsResourceRelation relation = new ApsResourceRelation();
        relation.setAncestry(apsResource.getParentId());
        relation.setDescendants(apsResource.getId());
        apsResourceRelationService.updateDeptRealtion(relation);
        return Boolean.TRUE;
    }
 
    @Override
    public ApsResourceGroupDTO getResourceById(Integer id) {
        return baseMapper.getResourceById(id);
    }
 
    /**
     * 查询全部资源分组树
     * @return 树
     */
    @Override
    public List<DeptTree> selectTree() {
        // 查询全部资源分组
        List<ApsResourceGroup> deptAllList = baseMapper.selectList(Wrappers.emptyWrapper());
        // 查询数据权限内资源分组
        List<Long> deptOwnIdList = baseMapper.selectList(Wrappers.emptyWrapper()).stream()
                .map(ApsResourceGroup::getId).collect(Collectors.toList());
 
        // 权限内部门
        List<DeptTree> collect = deptAllList.stream().filter(dept -> dept.getId().intValue() != dept.getParentId())
                .sorted(Comparator.comparing(ApsResourceGroup::getRscGroupNo)).map(dept -> {
                    DeptTree node = new DeptTree();
                    node.setId(dept.getId().intValue());
                    node.setParentId(dept.getParentId().intValue());
                    node.setName(dept.getRscGroupName());
 
                    // 有权限不返回标识
                    if (deptOwnIdList.contains(dept.getId())) {
                        node.setIsLock(Boolean.FALSE);
                    }
                    return node;
                }).collect(Collectors.toList());
        return TreeUtil.build(collect, 0);
    }
}