inleft
2022-02-15 d734432a8bbbf863dc3de305f56e831c56ac767a
commit | author | age
9bcb19 1 /*
I 2 Copyright [2020] [https://www.xiaonuo.vip]
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8   http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15
16 Snowy采用APACHE LICENSE 2.0开源协议,您在使用过程中,需要注意以下几点:
17
18 1.请不要删除和修改根目录下的LICENSE文件。
19 2.请不要删除和修改Snowy源码头部的版权声明。
20 3.请保留源码和相关描述文件的项目出处,作者声明等。
21 4.分发源码时候,请注明软件出处 https://gitee.com/xiaonuobase/snowy
22 5.在修改包名,模块名称,项目代码等时,请注明软件出处 https://gitee.com/xiaonuobase/snowy
23 6.若您的项目无法满足以上几点,可申请商业授权,获取Snowy商业授权许可,请在官网购买授权,地址为 https://www.xiaonuo.vip
24  */
25 package vip.xiaonuo.sys.modular.dict.service.impl;
26
27 import cn.hutool.core.bean.BeanUtil;
28 import cn.hutool.core.collection.CollectionUtil;
29 import cn.hutool.core.lang.Dict;
30 import cn.hutool.core.util.ObjectUtil;
31 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
32 import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
33 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
34 import org.springframework.stereotype.Service;
35 import org.springframework.transaction.annotation.Transactional;
36 import vip.xiaonuo.core.enums.CommonStatusEnum;
37 import vip.xiaonuo.core.exception.ServiceException;
38 import vip.xiaonuo.core.exception.enums.StatusExceptionEnum;
39 import vip.xiaonuo.core.factory.PageFactory;
40 import vip.xiaonuo.core.factory.TreeBuildFactory;
41 import vip.xiaonuo.core.pojo.page.PageResult;
42 import vip.xiaonuo.sys.modular.dict.entity.SysDictData;
43 import vip.xiaonuo.sys.modular.dict.entity.SysDictType;
44 import vip.xiaonuo.sys.modular.dict.enums.SysDictTypeExceptionEnum;
45 import vip.xiaonuo.sys.modular.dict.mapper.SysDictTypeMapper;
46 import vip.xiaonuo.sys.modular.dict.param.SysDictTypeParam;
47 import vip.xiaonuo.sys.modular.dict.result.SysDictTreeNode;
48 import vip.xiaonuo.sys.modular.dict.service.SysDictDataService;
49 import vip.xiaonuo.sys.modular.dict.service.SysDictTypeService;
50
51 import javax.annotation.Resource;
52 import java.util.List;
53
54 /**
55  * 系统字典类型service接口实现类
56  *
57  * @author xuyuxiang,xuyuxiang
58  * @date 2020/3/13 16:11
59  */
60 @Service
61 public class SysDictTypeServiceImpl extends ServiceImpl<SysDictTypeMapper, SysDictType> implements SysDictTypeService {
62
63     @Resource
64     private SysDictDataService sysDictDataService;
65
66     @Override
67     public PageResult<SysDictType> page(SysDictTypeParam sysDictTypeParam) {
68
69         //构造条件
70         LambdaQueryWrapper<SysDictType> queryWrapper = new LambdaQueryWrapper<>();
71
72         if (ObjectUtil.isNotNull(sysDictTypeParam)) {
73             //根据字典类型名称模糊查询
74             if (ObjectUtil.isNotEmpty(sysDictTypeParam.getName())) {
75                 queryWrapper.like(SysDictType::getName, sysDictTypeParam.getName());
76             }
77
78             //根据字典类型编码模糊查询
79             if (ObjectUtil.isNotEmpty(sysDictTypeParam.getCode())) {
80                 queryWrapper.like(SysDictType::getCode, sysDictTypeParam.getCode());
81             }
82         }
83
84         //查询未删除的
85         queryWrapper.ne(SysDictType::getStatus, CommonStatusEnum.DELETED.getCode());
86         //根据排序升序排列,序号越小越在前
87         queryWrapper.orderByAsc(SysDictType::getSort);
88         //查询分页结果
89         return new PageResult<>(this.page(PageFactory.defaultPage(), queryWrapper));
90     }
91
92     @Override
93     public List<SysDictType> list(SysDictTypeParam sysDictTypeParam) {
94
95         //构造条件
96         LambdaQueryWrapper<SysDictType> queryWrapper = new LambdaQueryWrapper<>();
97
98         //查询未删除的
99         queryWrapper.ne(SysDictType::getStatus, CommonStatusEnum.DELETED.getCode());
100
101         return this.list(queryWrapper);
102     }
103
104     @Override
105     public List<Dict> dropDown(SysDictTypeParam sysDictTypeParam) {
106         LambdaQueryWrapper<SysDictType> queryWrapper = new LambdaQueryWrapper<SysDictType>()
107                 .eq(SysDictType::getCode, sysDictTypeParam.getCode());
108
109         SysDictType sysDictType = this.getOne(queryWrapper);
110         if (ObjectUtil.isNull(sysDictType)) {
111             throw new ServiceException(SysDictTypeExceptionEnum.DICT_TYPE_NOT_EXIST);
112         }
113         return sysDictDataService.getDictDataListByDictTypeId(sysDictType.getId());
114     }
115
116     @Override
117     public void add(SysDictTypeParam sysDictTypeParam) {
118
119         //校验参数,检查是否存在重复的编码或者名称,不排除当前添加的这条记录
120         checkParam(sysDictTypeParam, false);
121
122         //将dto转为实体
123         SysDictType sysDictType = new SysDictType();
124         BeanUtil.copyProperties(sysDictTypeParam, sysDictType);
125
126         //设置状态为启用
127         sysDictType.setStatus(CommonStatusEnum.ENABLE.getCode());
128
129         this.save(sysDictType);
130     }
131
132     @Transactional(rollbackFor = Exception.class)
133     @Override
134     public void delete(SysDictTypeParam sysDictTypeParam) {
135
136         //根据id查询实体
137         SysDictType sysDictType = this.querySysDictType(sysDictTypeParam);
138
139         //逻辑删除,修改状态
140         sysDictType.setStatus(CommonStatusEnum.DELETED.getCode());
141
142         //更新实体
143         this.updateById(sysDictType);
144
145         //级联删除字典值
146         sysDictDataService.deleteByTypeId(sysDictType.getId());
147     }
148
149     @Override
150     public void edit(SysDictTypeParam sysDictTypeParam) {
151
152         //根据id查询实体
153         SysDictType sysDictType = this.querySysDictType(sysDictTypeParam);
154
155         //校验参数,检查是否存在重复的编码或者名称,排除当前编辑的这条记录
156         checkParam(sysDictTypeParam, true);
157
158         //请求参数转化为实体
159         BeanUtil.copyProperties(sysDictTypeParam, sysDictType);
160
161         //不能修改状态,用修改状态接口修改状态
162         sysDictType.setStatus(null);
163
164         this.updateById(sysDictType);
165     }
166
167     @Override
168     public SysDictType detail(SysDictTypeParam sysDictTypeParam) {
169         return this.querySysDictType(sysDictTypeParam);
170     }
171
172     @Override
173     public void changeStatus(SysDictTypeParam sysDictTypeParam) {
174         Long id = sysDictTypeParam.getId();
175         Integer status = sysDictTypeParam.getStatus();
176
177         //校验状态在不在枚举值里
178         CommonStatusEnum.validateStatus(status);
179
180         //更新枚举,更新只能更新未删除状态的
181         LambdaUpdateWrapper<SysDictType> updateWrapper = new LambdaUpdateWrapper<>();
182         updateWrapper.eq(SysDictType::getId, id)
183                 .and(i -> i.ne(SysDictType::getStatus, CommonStatusEnum.DELETED.getCode()))
184                 .set(SysDictType::getStatus, status);
185         boolean update = this.update(updateWrapper);
186         if (!update) {
187             throw new ServiceException(StatusExceptionEnum.UPDATE_STATUS_ERROR);
188         }
189     }
190
191     @Override
192     public List<SysDictTreeNode> tree() {
193         List<SysDictTreeNode> resultList = CollectionUtil.newArrayList();
194         LambdaQueryWrapper<SysDictType> queryWrapper = new LambdaQueryWrapper<>();
195         queryWrapper.ne(SysDictType::getStatus, CommonStatusEnum.DELETED.getCode());
196         this.list(queryWrapper).forEach(sysDictType -> {
197             SysDictTreeNode sysDictTreeNode = new SysDictTreeNode();
198             BeanUtil.copyProperties(sysDictType, sysDictTreeNode);
199             sysDictTreeNode.setPid(0L);
200             resultList.add(sysDictTreeNode);
201         });
202         sysDictDataService.list(new LambdaQueryWrapper<SysDictData>().ne(SysDictData::getStatus, CommonStatusEnum.DELETED.getCode()))
203                 .forEach(sysDictData -> {
204                     SysDictTreeNode sysDictTreeNode = new SysDictTreeNode();
205                     sysDictTreeNode.setId(sysDictData.getId());
206                     sysDictTreeNode.setPid(sysDictData.getTypeId());
207                     sysDictTreeNode.setCode(sysDictData.getCode());
208                     sysDictTreeNode.setName(sysDictData.getValue());
209                     resultList.add(sysDictTreeNode);
210                 });
211         return new TreeBuildFactory<SysDictTreeNode>().doTreeBuild(resultList);
212     }
213
214     /**
215      * 校验参数,检查是否存在重复的编码或者名称
216      *
217      * @author xuyuxiang,xuyuxiang
218      * @date 2020/3/25 21:23
219      */
220     private void checkParam(SysDictTypeParam sysDictTypeParam, boolean isExcludeSelf) {
221         Long id = sysDictTypeParam.getId();
222         String name = sysDictTypeParam.getName();
223         String code = sysDictTypeParam.getCode();
224
225         //构建带name和code的查询条件
226         LambdaQueryWrapper<SysDictType> queryWrapperByName = new LambdaQueryWrapper<>();
227         queryWrapperByName.eq(SysDictType::getName, name)
228                 .ne(SysDictType::getStatus, CommonStatusEnum.DELETED.getCode());
229
230         LambdaQueryWrapper<SysDictType> queryWrapperByCode = new LambdaQueryWrapper<>();
231         queryWrapperByCode.eq(SysDictType::getCode, code)
232                 .ne(SysDictType::getStatus, CommonStatusEnum.DELETED.getCode());
233
234         //如果排除自己,则增加查询条件主键id不等于本条id
235         if (isExcludeSelf) {
236             queryWrapperByName.ne(SysDictType::getId, id);
237             queryWrapperByCode.ne(SysDictType::getId, id);
238         }
239
240         //查询重复记录的数量
241         int countByName = this.count(queryWrapperByName);
242         int countByCode = this.count(queryWrapperByCode);
243
244         //如果存在重复的记录,抛出异常,直接返回前端
245         if (countByName >= 1) {
246             throw new ServiceException(SysDictTypeExceptionEnum.DICT_TYPE_NAME_REPEAT);
247         }
248         if (countByCode >= 1) {
249             throw new ServiceException(SysDictTypeExceptionEnum.DICT_TYPE_CODE_REPEAT);
250         }
251     }
252
253     /**
254      * 获取系统字典类型
255      *
256      * @author xuyuxiang,xuyuxiang
257      * @date 2020/3/31 20:38
258      */
259     private SysDictType querySysDictType(SysDictTypeParam sysDictTypeParam) {
260         SysDictType sysDictType = this.getById(sysDictTypeParam.getId());
261         if (ObjectUtil.isNull(sysDictType)) {
262             throw new ServiceException(SysDictTypeExceptionEnum.DICT_TYPE_NOT_EXIST);
263         }
264         return sysDictType;
265     }
266 }