懒羊羊
2023-08-30 1ac2bc1590406d9babec036e154d8d08f34a6aa1
提交 | 用户 | 时间
1ac2bc 1 package cn.stylefeng.guns.sys.modular.rest.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.rest.entity.RestDict;
10 import cn.stylefeng.guns.sys.modular.rest.entity.RestDictType;
11 import cn.stylefeng.guns.sys.modular.rest.mapper.RestDictMapper;
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.stereotype.Service;
21
22 import javax.annotation.Resource;
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 RestDictService extends ServiceImpl<RestDictMapper, RestDict> {
38
39     @Resource
40     private RestDictTypeService restDictTypeService;
41
42     /**
43      * 新增
44      *
45      * @author stylefeng
46      * @Date 2019-03-13
47      */
48     public void add(DictParam param) {
49
50         //判断是否已经存在同编码或同名称字典
51         QueryWrapper<RestDict> 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<RestDict> list = this.list(dictQueryWrapper);
56         if (list != null && list.size() > 0) {
57             throw new ServiceException(BizExceptionEnum.DICT_EXISTED);
58         }
59
60         RestDict 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         RestDict oldEntity = getOldEntity(param);
96         RestDict newEntity = getEntity(param);
97         ToolUtil.copyProperties(newEntity, oldEntity);
98
99         //判断编码是否重复
100         QueryWrapper<RestDict> wrapper = new QueryWrapper<RestDict>()
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<RestDict> 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<RestDict> list = this.list(objectQueryWrapper);
152
153         //创建根节点
154         RestDict dict = new RestDict();
155         dict.setName("根节点");
156         dict.setDictId(0L);
157         dict.setParentId(-999L);
158         list.add(dict);
159
160         LayuiPageInfo result = new LayuiPageInfo();
161         result.setData(list);
162
163         return result;
164     }
165
166     /**
167      * 获取字典的树形列表(ztree结构)
168      *
169      * @author fengshuonan
170      * @Date 2019/3/14 3:40 PM
171      */
172     public List<ZTreeNode> dictTreeList(Long dictTypeId, Long dictId) {
173         if (dictTypeId == null) {
174             throw new RequestEmptyException();
175         }
176
177         List<ZTreeNode> tree = this.baseMapper.dictTree(dictTypeId);
178
179         //获取dict的所有子节点
180         List<Long> subIds = getSubIds(dictId);
181
182         //如果传了dictId,则在返回结果里去掉
183         List<ZTreeNode> resultTree = new ArrayList<>();
184         for (ZTreeNode zTreeNode : tree) {
185
186             //如果dictId等于树节点的某个id则去除
187             if (ToolUtil.isNotEmpty(dictId) && dictId.equals(zTreeNode.getId())) {
188                 continue;
189             }
190             if (subIds.contains(zTreeNode.getId())) {
191                 continue;
192             }
193             resultTree.add(zTreeNode);
194         }
195
196         resultTree.add(ZTreeNode.createParent());
197
198         return resultTree;
199     }
200
201     /**
202      * 查看dict的详情
203      *
204      * @author fengshuonan
205      * @Date 2019/3/14 5:22 PM
206      */
207     public DictResult dictDetail(Long dictId) {
208         if (ToolUtil.isEmpty(dictId)) {
209             throw new RequestEmptyException();
210         }
211
212         DictResult dictResult = new DictResult();
213
214         //查询字典
215         RestDict detail = this.getById(dictId);
216         if (detail == null) {
217             throw new RequestEmptyException();
218         }
219
220         //查询父级字典
221         if (ToolUtil.isNotEmpty(detail.getParentId())) {
222             Long parentId = detail.getParentId();
223             RestDict dictType = this.getById(parentId);
224             if (dictType != null) {
225                 dictResult.setParentName(dictType.getName());
226             } else {
227                 dictResult.setParentName("无父级");
228             }
229         }
230
231         ToolUtil.copyProperties(detail, dictResult);
232
233         return dictResult;
234     }
235
236     /**
237      * 查询字典列表,通过字典类型
238      *
239      * @author fengshuonan
240      * @Date 2019-06-20 15:14
241      */
242     public List<RestDict> listDicts(Long dictTypeId) {
243
244         QueryWrapper<RestDict> objectQueryWrapper = new QueryWrapper<>();
245         objectQueryWrapper.eq("dict_type_id", dictTypeId);
246
247         List<RestDict> list = this.list(objectQueryWrapper);
248
249         if (list == null) {
250             return new ArrayList<>();
251         } else {
252             return list;
253         }
254
255     }
256
257     /**
258      * 查询字典列表,通过字典类型code
259      *
260      * @author fengshuonan
261      * @Date 2019-06-20 15:14
262      */
263     public List<RestDict> listDictsByCode(String dictTypeCode) {
264
265         QueryWrapper<RestDictType> wrapper = new QueryWrapper<>();
266         wrapper.eq("code", dictTypeCode);
267
268         RestDictType one = this.restDictTypeService.getOne(wrapper);
269         return listDicts(one.getDictTypeId());
270     }
271
272     /**
273      * 查询字典列表,通过字典类型code
274      *
275      * @author fengshuonan
276      * @Date 2019-06-20 15:14
277      */
278     public List<Map<String, Object>> getDictsByCodes(List<String> dictCodes) {
279
280         QueryWrapper<RestDict> wrapper = new QueryWrapper<>();
281         wrapper.in("code", dictCodes).orderByAsc("sort");
282
283         ArrayList<Map<String, Object>> results = new ArrayList<>();
284
285         //转成map
286         List<RestDict> list = this.list(wrapper);
287         for (RestDict dict : list) {
288             Map<String, Object> map = BeanUtil.beanToMap(dict);
289             results.add(map);
290         }
291
292         return results;
293     }
294
295     private Serializable getKey(DictParam param) {
296         return param.getDictId();
297     }
298
299     private Page getPageContext() {
300         return LayuiPageFactory.defaultPage();
301     }
302
303     private RestDict getOldEntity(DictParam param) {
304         return this.getById(getKey(param));
305     }
306
307     private RestDict getEntity(DictParam param) {
308         RestDict entity = new RestDict();
309         ToolUtil.copyProperties(param, entity);
310         return entity;
311     }
312
313     private List<Long> getSubIds(Long dictId) {
314
315         ArrayList<Long> longs = new ArrayList<>();
316
317         if (ToolUtil.isEmpty(dictId)) {
318             return longs;
319         } else {
320             List<RestDict> list = this.baseMapper.likeParentIds(dictId);
321             for (RestDict dict : list) {
322                 longs.add(dict.getDictId());
323             }
324             return longs;
325         }
326     }
327
328     private void dictSetPids(RestDict param) {
329         if (param.getParentId().equals(0L)) {
330             param.setParentIds("[0]");
331         } else {
332             //获取父级的pids
333             Long parentId = param.getParentId();
334             RestDict parent = this.getById(parentId);
335             if (parent == null) {
336                 param.setParentIds("[0]");
337             } else {
338                 param.setParentIds(parent.getParentIds() + "," + "[" + parentId + "]");
339             }
340         }
341     }
342
343 }