ArchivesDeptServiceImpl.java
4.74 KB
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package com.ruoyi.service.impl;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;
import com.ruoyi.domain.ArchivesDept;
import com.ruoyi.mapper.ArchivesDeptMapper;
import com.ruoyi.util.TreeSelect;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.service.IArchivesDeptService;
/**
* 部门Service业务层处理
*
* @author li
* @date 2022-08-01
*/
@Service
public class ArchivesDeptServiceImpl implements IArchivesDeptService
{
@Autowired
private ArchivesDeptMapper archivesDeptMapper;
/**
* 查询部门
*
* @param deptId 部门主键
* @return 部门
*/
@Override
public ArchivesDept selectArchivesDeptByDeptId(Long deptId)
{
return archivesDeptMapper.selectArchivesDeptByDeptId(deptId);
}
/**
* 查询部门列表
*
* @param archivesDept 部门
* @return 部门
*/
@Override
public List<ArchivesDept> selectArchivesDeptList(ArchivesDept archivesDept)
{
return archivesDeptMapper.selectArchivesDeptList(archivesDept);
}
/**
* 新增部门
*
* @param archivesDept 部门
* @return 结果
*/
@Override
public int insertArchivesDept(ArchivesDept archivesDept)
{
archivesDept.setCreateTime(DateUtils.getNowDate());
return archivesDeptMapper.insertArchivesDept(archivesDept);
}
/**
* 修改部门
*
* @param archivesDept 部门
* @return 结果
*/
@Override
public int updateArchivesDept(ArchivesDept archivesDept)
{
archivesDept.setUpdateTime(DateUtils.getNowDate());
return archivesDeptMapper.updateArchivesDept(archivesDept);
}
/**
* 批量删除部门
*
* @param deptIds 需要删除的部门主键
* @return 结果
*/
@Override
public int deleteArchivesDeptByDeptIds(Long[] deptIds)
{
return archivesDeptMapper.deleteArchivesDeptByDeptIds(deptIds);
}
/**
* 删除部门信息
*
* @param deptId 部门主键
* @return 结果
*/
@Override
public int deleteArchivesDeptByDeptId(Long deptId)
{
return archivesDeptMapper.deleteArchivesDeptByDeptId(deptId);
}
/**
* 构建前端所需要下拉树结构
*
* @param depts 部门列表
* @return 下拉树结构列表
*/
public List<TreeSelect> buildDeptTreeSelect(List<ArchivesDept> depts){
List<ArchivesDept> deptTrees = buildDeptTree(depts);
return deptTrees.stream().map(TreeSelect::new).collect(Collectors.toList());
}
/**
* 构建前端所需要树结构
*
* @param depts 部门列表
* @return 树结构列表
*/
@Override
public List<ArchivesDept> buildDeptTree(List<ArchivesDept> depts)
{
List<ArchivesDept> returnList = new ArrayList<ArchivesDept>();
List<Long> tempList = new ArrayList<Long>();
for (ArchivesDept dept : depts)
{
tempList.add(dept.getDeptId());
}
for (ArchivesDept dept : depts)
{
// 如果是顶级节点, 遍历该父节点的所有子节点
if (!tempList.contains(dept.getParentId()))
{
recursionFn(depts, dept);
returnList.add(dept);
}
}
if (returnList.isEmpty())
{
returnList = depts;
}
return returnList;
}
/**
* 递归列表
*/
private void recursionFn(List<ArchivesDept> list, ArchivesDept t)
{
// 得到子节点列表
List<ArchivesDept> childList = getChildList(list, t);
t.setChildren(childList);
for (ArchivesDept tChild : childList)
{
if (hasChild(list, tChild))
{
recursionFn(list, tChild);
}
}
}
/**
* 得到子节点列表
*/
private List<ArchivesDept> getChildList(List<ArchivesDept> list, ArchivesDept t)
{
List<ArchivesDept> tlist = new ArrayList<ArchivesDept>();
Iterator<ArchivesDept> it = list.iterator();
while (it.hasNext())
{
ArchivesDept n = (ArchivesDept) it.next();
if (StringUtils.isNotNull(n.getParentId()) && n.getParentId().longValue() == t.getDeptId().longValue())
{
tlist.add(n);
}
}
return tlist;
}
/**
* 判断是否有子节点
*/
private boolean hasChild(List<ArchivesDept> list, ArchivesDept t)
{
return getChildList(list, t).size() > 0;
}
}