提交 | 用户 | 时间
|
1ac2bc
|
1 |
package cn.stylefeng.guns.sys.modular.system.service; |
懒 |
2 |
|
|
3 |
import cn.hutool.core.bean.BeanUtil; |
|
4 |
import cn.stylefeng.guns.base.enums.CommonStatus; |
|
5 |
import cn.stylefeng.guns.base.pojo.node.ZTreeNode; |
|
6 |
import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory; |
|
7 |
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo; |
|
8 |
import cn.stylefeng.guns.sys.core.exception.enums.BizExceptionEnum; |
|
9 |
import cn.stylefeng.guns.sys.modular.system.entity.Dict; |
|
10 |
import cn.stylefeng.guns.sys.modular.system.entity.DictType; |
|
11 |
import cn.stylefeng.guns.sys.modular.system.mapper.DictMapper; |
|
12 |
import cn.stylefeng.guns.sys.modular.system.model.params.DictParam; |
|
13 |
import cn.stylefeng.guns.sys.modular.system.model.result.DictResult; |
|
14 |
import cn.stylefeng.roses.core.util.ToolUtil; |
|
15 |
import cn.stylefeng.roses.kernel.model.exception.RequestEmptyException; |
|
16 |
import cn.stylefeng.roses.kernel.model.exception.ServiceException; |
|
17 |
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; |
|
18 |
import com.baomidou.mybatisplus.extension.plugins.pagination.Page; |
|
19 |
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
|
20 |
import org.springframework.beans.factory.annotation.Autowired; |
|
21 |
import org.springframework.stereotype.Service; |
|
22 |
|
|
23 |
import java.io.Serializable; |
|
24 |
import java.util.ArrayList; |
|
25 |
import java.util.List; |
|
26 |
import java.util.Map; |
|
27 |
|
|
28 |
/** |
|
29 |
* <p> |
|
30 |
* 基础字典 服务实现类 |
|
31 |
* </p> |
|
32 |
* |
|
33 |
* @author stylefeng |
|
34 |
* @since 2019-03-13 |
|
35 |
*/ |
|
36 |
@Service |
|
37 |
public class DictService extends ServiceImpl<DictMapper, Dict> { |
|
38 |
|
|
39 |
@Autowired |
|
40 |
private DictTypeService dictTypeService; |
|
41 |
|
|
42 |
/** |
|
43 |
* 新增 |
|
44 |
* |
|
45 |
* @author stylefeng |
|
46 |
* @Date 2019-03-13 |
|
47 |
*/ |
|
48 |
public void add(DictParam param) { |
|
49 |
|
|
50 |
//判断是否已经存在同编码或同名称字典 |
|
51 |
QueryWrapper<Dict> dictQueryWrapper = new QueryWrapper<>(); |
|
52 |
dictQueryWrapper |
|
53 |
.and(i -> i.eq("code", param.getCode()).or().eq("name", param.getName())) |
|
54 |
.and(i -> i.eq("dict_type_id", param.getDictTypeId())); |
|
55 |
List<Dict> list = this.list(dictQueryWrapper); |
|
56 |
if (list != null && list.size() > 0) { |
|
57 |
throw new ServiceException(BizExceptionEnum.DICT_EXISTED); |
|
58 |
} |
|
59 |
|
|
60 |
Dict entity = getEntity(param); |
|
61 |
|
|
62 |
//设置pids |
|
63 |
dictSetPids(entity); |
|
64 |
|
|
65 |
//设置状态 |
|
66 |
entity.setStatus(CommonStatus.ENABLE.getCode()); |
|
67 |
|
|
68 |
this.save(entity); |
|
69 |
} |
|
70 |
|
|
71 |
/** |
|
72 |
* 删除 |
|
73 |
* |
|
74 |
* @author stylefeng |
|
75 |
* @Date 2019-03-13 |
|
76 |
*/ |
|
77 |
public void delete(DictParam param) { |
|
78 |
|
|
79 |
//删除字典的所有子级 |
|
80 |
List<Long> subIds = getSubIds(param.getDictId()); |
|
81 |
if (subIds.size() > 0) { |
|
82 |
this.removeByIds(subIds); |
|
83 |
} |
|
84 |
|
|
85 |
this.removeById(getKey(param)); |
|
86 |
} |
|
87 |
|
|
88 |
/** |
|
89 |
* 更新 |
|
90 |
* |
|
91 |
* @author stylefeng |
|
92 |
* @Date 2019-03-13 |
|
93 |
*/ |
|
94 |
public void update(DictParam param) { |
|
95 |
Dict oldEntity = getOldEntity(param); |
|
96 |
Dict newEntity = getEntity(param); |
|
97 |
ToolUtil.copyProperties(newEntity, oldEntity); |
|
98 |
|
|
99 |
//判断编码是否重复 |
|
100 |
QueryWrapper<Dict> wrapper = new QueryWrapper<Dict>() |
|
101 |
.and(i -> i.eq("code", newEntity.getCode()).or().eq("name", newEntity.getName())) |
|
102 |
.and(i -> i.ne("dict_id", newEntity.getDictId())) |
|
103 |
.and(i -> i.eq("dict_type_id", param.getDictTypeId())); |
|
104 |
int dicts = this.count(wrapper); |
|
105 |
if (dicts > 0) { |
|
106 |
throw new ServiceException(BizExceptionEnum.DICT_EXISTED); |
|
107 |
} |
|
108 |
|
|
109 |
//设置pids |
|
110 |
dictSetPids(newEntity); |
|
111 |
|
|
112 |
this.updateById(newEntity); |
|
113 |
} |
|
114 |
|
|
115 |
/** |
|
116 |
* 查询单条数据,Specification模式 |
|
117 |
* |
|
118 |
* @author stylefeng |
|
119 |
* @Date 2019-03-13 |
|
120 |
*/ |
|
121 |
public DictResult findBySpec(DictParam param) { |
|
122 |
return null; |
|
123 |
} |
|
124 |
|
|
125 |
/** |
|
126 |
* 查询列表,Specification模式 |
|
127 |
* |
|
128 |
* @author stylefeng |
|
129 |
* @Date 2019-03-13 |
|
130 |
*/ |
|
131 |
public List<DictResult> findListBySpec(DictParam param) { |
|
132 |
return null; |
|
133 |
} |
|
134 |
|
|
135 |
/** |
|
136 |
* 查询分页数据,Specification模式 |
|
137 |
* |
|
138 |
* @author stylefeng |
|
139 |
* @Date 2019-03-13 |
|
140 |
*/ |
|
141 |
public LayuiPageInfo findPageBySpec(DictParam param) { |
|
142 |
QueryWrapper<Dict> objectQueryWrapper = new QueryWrapper<>(); |
|
143 |
objectQueryWrapper.eq("dict_type_id", param.getDictTypeId()); |
|
144 |
|
|
145 |
if (ToolUtil.isNotEmpty(param.getCondition())) { |
|
146 |
objectQueryWrapper.and(i -> i.eq("code", param.getCondition()).or().eq("name", param.getCondition())); |
|
147 |
} |
|
148 |
|
|
149 |
objectQueryWrapper.orderByAsc("sort"); |
|
150 |
|
|
151 |
List<Dict> list = this.list(objectQueryWrapper); |
|
152 |
|
|
153 |
//去除根节点为0的 |
|
154 |
if (list.size() > 0) { |
|
155 |
for (Dict dict : list) { |
|
156 |
if (dict.getParentId() != null && dict.getParentId().equals(0L)) { |
|
157 |
dict.setParentId(null); |
|
158 |
} |
|
159 |
} |
|
160 |
} |
|
161 |
|
|
162 |
LayuiPageInfo result = new LayuiPageInfo(); |
|
163 |
result.setData(list); |
|
164 |
|
|
165 |
return result; |
|
166 |
} |
|
167 |
|
|
168 |
/** |
|
169 |
* 获取字典的树形列表(ztree结构) |
|
170 |
* |
|
171 |
* @author fengshuonan |
|
172 |
* @Date 2019/3/14 3:40 PM |
|
173 |
*/ |
|
174 |
public List<ZTreeNode> dictTreeList(Long dictTypeId, Long dictId) { |
|
175 |
if (dictTypeId == null) { |
|
176 |
throw new RequestEmptyException(); |
|
177 |
} |
|
178 |
|
|
179 |
List<ZTreeNode> tree = this.baseMapper.dictTree(dictTypeId); |
|
180 |
|
|
181 |
//获取dict的所有子节点 |
|
182 |
List<Long> subIds = getSubIds(dictId); |
|
183 |
|
|
184 |
//如果传了dictId,则在返回结果里去掉 |
|
185 |
List<ZTreeNode> resultTree = new ArrayList<>(); |
|
186 |
for (ZTreeNode zTreeNode : tree) { |
|
187 |
|
|
188 |
//如果dictId等于树节点的某个id则去除 |
|
189 |
if (ToolUtil.isNotEmpty(dictId) && dictId.equals(zTreeNode.getId())) { |
|
190 |
continue; |
|
191 |
} |
|
192 |
if (subIds.contains(zTreeNode.getId())) { |
|
193 |
continue; |
|
194 |
} |
|
195 |
resultTree.add(zTreeNode); |
|
196 |
} |
|
197 |
|
|
198 |
resultTree.add(ZTreeNode.createParent()); |
|
199 |
|
|
200 |
return resultTree; |
|
201 |
} |
|
202 |
|
|
203 |
/** |
|
204 |
* 查看dict的详情 |
|
205 |
* |
|
206 |
* @author fengshuonan |
|
207 |
* @Date 2019/3/14 5:22 PM |
|
208 |
*/ |
|
209 |
public DictResult dictDetail(Long dictId) { |
|
210 |
if (ToolUtil.isEmpty(dictId)) { |
|
211 |
throw new RequestEmptyException(); |
|
212 |
} |
|
213 |
|
|
214 |
DictResult dictResult = new DictResult(); |
|
215 |
|
|
216 |
//查询字典 |
|
217 |
Dict detail = this.getById(dictId); |
|
218 |
if (detail == null) { |
|
219 |
throw new RequestEmptyException(); |
|
220 |
} |
|
221 |
|
|
222 |
//查询父级字典 |
|
223 |
if (ToolUtil.isNotEmpty(detail.getParentId())) { |
|
224 |
Long parentId = detail.getParentId(); |
|
225 |
Dict dictType = this.getById(parentId); |
|
226 |
if (dictType != null) { |
|
227 |
dictResult.setParentName(dictType.getName()); |
|
228 |
} else { |
|
229 |
dictResult.setParentName("无父级"); |
|
230 |
} |
|
231 |
} |
|
232 |
|
|
233 |
ToolUtil.copyProperties(detail, dictResult); |
|
234 |
|
|
235 |
return dictResult; |
|
236 |
} |
|
237 |
|
|
238 |
/** |
|
239 |
* 查询字典列表,通过字典类型 |
|
240 |
* |
|
241 |
* @author fengshuonan |
|
242 |
* @Date 2019-06-20 15:14 |
|
243 |
*/ |
|
244 |
public List<Dict> listDicts(Long dictTypeId) { |
|
245 |
|
|
246 |
QueryWrapper<Dict> objectQueryWrapper = new QueryWrapper<>(); |
|
247 |
objectQueryWrapper.eq("dict_type_id", dictTypeId); |
|
248 |
|
|
249 |
List<Dict> list = this.list(objectQueryWrapper); |
|
250 |
|
|
251 |
if (list == null) { |
|
252 |
return new ArrayList<>(); |
|
253 |
} else { |
|
254 |
return list; |
|
255 |
} |
|
256 |
|
|
257 |
} |
|
258 |
|
|
259 |
/** |
|
260 |
* 查询字典列表,通过字典类型code |
|
261 |
* |
|
262 |
* @author fengshuonan |
|
263 |
* @Date 2019-06-20 15:14 |
|
264 |
*/ |
|
265 |
public List<Dict> listDictsByCode(String dictTypeCode) { |
|
266 |
|
|
267 |
QueryWrapper<DictType> wrapper = new QueryWrapper<>(); |
|
268 |
wrapper.eq("code", dictTypeCode); |
|
269 |
|
|
270 |
DictType one = this.dictTypeService.getOne(wrapper); |
|
271 |
return listDicts(one.getDictTypeId()); |
|
272 |
} |
|
273 |
|
|
274 |
/** |
|
275 |
* 查询字典列表,通过字典类型code |
|
276 |
* |
|
277 |
* @author fengshuonan |
|
278 |
* @Date 2019-06-20 15:14 |
|
279 |
*/ |
|
280 |
public List<Map<String, Object>> getDictsByCodes(List<String> dictCodes) { |
|
281 |
|
|
282 |
QueryWrapper<Dict> wrapper = new QueryWrapper<>(); |
|
283 |
wrapper.in("code", dictCodes).orderByAsc("sort"); |
|
284 |
|
|
285 |
ArrayList<Map<String, Object>> results = new ArrayList<>(); |
|
286 |
|
|
287 |
//转成map |
|
288 |
List<Dict> list = this.list(wrapper); |
|
289 |
for (Dict dict : list) { |
|
290 |
Map<String, Object> map = BeanUtil.beanToMap(dict); |
|
291 |
results.add(map); |
|
292 |
} |
|
293 |
|
|
294 |
return results; |
|
295 |
} |
|
296 |
|
|
297 |
private Serializable getKey(DictParam param) { |
|
298 |
return param.getDictId(); |
|
299 |
} |
|
300 |
|
|
301 |
private Page getPageContext() { |
|
302 |
return LayuiPageFactory.defaultPage(); |
|
303 |
} |
|
304 |
|
|
305 |
private Dict getOldEntity(DictParam param) { |
|
306 |
return this.getById(getKey(param)); |
|
307 |
} |
|
308 |
|
|
309 |
private Dict getEntity(DictParam param) { |
|
310 |
Dict entity = new Dict(); |
|
311 |
ToolUtil.copyProperties(param, entity); |
|
312 |
return entity; |
|
313 |
} |
|
314 |
|
|
315 |
private List<Long> getSubIds(Long dictId) { |
|
316 |
|
|
317 |
ArrayList<Long> longs = new ArrayList<>(); |
|
318 |
|
|
319 |
if (ToolUtil.isEmpty(dictId)) { |
|
320 |
return longs; |
|
321 |
} else { |
|
322 |
List<Dict> list = this.baseMapper.likeParentIds(dictId); |
|
323 |
for (Dict dict : list) { |
|
324 |
longs.add(dict.getDictId()); |
|
325 |
} |
|
326 |
return longs; |
|
327 |
} |
|
328 |
} |
|
329 |
|
|
330 |
private void dictSetPids(Dict param) { |
|
331 |
if (param.getParentId().equals(0L)) { |
|
332 |
param.setParentIds("[0]"); |
|
333 |
} else { |
|
334 |
//获取父级的pids |
|
335 |
Long parentId = param.getParentId(); |
|
336 |
Dict parent = this.getById(parentId); |
|
337 |
if (parent == null) { |
|
338 |
param.setParentIds("[0]"); |
|
339 |
} else { |
|
340 |
param.setParentIds(parent.getParentIds() + "," + "[" + parentId + "]"); |
|
341 |
} |
|
342 |
} |
|
343 |
} |
|
344 |
|
|
345 |
} |