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 vip.xiaonuo.core.consts.CommonConstant; |
|
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.pojo.page.PageResult; |
|
41 |
import vip.xiaonuo.sys.modular.dict.entity.SysDictData; |
|
42 |
import vip.xiaonuo.sys.modular.dict.enums.SysDictDataExceptionEnum; |
|
43 |
import vip.xiaonuo.sys.modular.dict.mapper.SysDictDataMapper; |
|
44 |
import vip.xiaonuo.sys.modular.dict.param.SysDictDataParam; |
|
45 |
import vip.xiaonuo.sys.modular.dict.service.SysDictDataService; |
|
46 |
|
|
47 |
import java.util.List; |
|
48 |
|
|
49 |
/** |
|
50 |
* 系统字典值service接口实现类 |
|
51 |
* |
|
52 |
* @author xuyuxiang |
|
53 |
* @date 2020/3/13 16:11 |
|
54 |
*/ |
|
55 |
@Service |
|
56 |
public class SysDictDataServiceImpl extends ServiceImpl<SysDictDataMapper, SysDictData> implements SysDictDataService { |
|
57 |
|
|
58 |
@Override |
|
59 |
public PageResult<SysDictData> page(SysDictDataParam sysDictDataParam) { |
|
60 |
|
|
61 |
//构造条件 |
|
62 |
LambdaQueryWrapper<SysDictData> queryWrapper = new LambdaQueryWrapper<>(); |
|
63 |
if (ObjectUtil.isNotNull(sysDictDataParam)) { |
|
64 |
//根据字典类型查询 |
|
65 |
if (ObjectUtil.isNotEmpty(sysDictDataParam.getTypeId())) { |
|
66 |
queryWrapper.eq(SysDictData::getTypeId, sysDictDataParam.getTypeId()); |
|
67 |
} |
|
68 |
//根据字典值的编码模糊查询 |
|
69 |
if (ObjectUtil.isNotEmpty(sysDictDataParam.getCode())) { |
|
70 |
queryWrapper.like(SysDictData::getCode, sysDictDataParam.getCode()); |
|
71 |
} |
|
72 |
//根据字典值的内容模糊查询 |
|
73 |
if (ObjectUtil.isNotEmpty(sysDictDataParam.getValue())) { |
|
74 |
queryWrapper.like(SysDictData::getValue, sysDictDataParam.getValue()); |
|
75 |
} |
|
76 |
} |
|
77 |
//查询未删除的 |
|
78 |
queryWrapper.ne(SysDictData::getStatus, CommonStatusEnum.DELETED.getCode()); |
|
79 |
//根据排序升序排列,序号越小越在前 |
|
80 |
queryWrapper.orderByAsc(SysDictData::getSort); |
|
81 |
//返回分页查询结果 |
|
82 |
return new PageResult<>(this.page(PageFactory.defaultPage(), queryWrapper)); |
|
83 |
} |
|
84 |
|
|
85 |
@Override |
|
86 |
public List<SysDictData> list(SysDictDataParam sysDictDataParam) { |
|
87 |
//构造条件,查询某个字典类型下的 |
|
88 |
LambdaQueryWrapper<SysDictData> queryWrapper = new LambdaQueryWrapper<>(); |
|
89 |
if (ObjectUtil.isNotNull(sysDictDataParam)) { |
|
90 |
if (ObjectUtil.isNotEmpty(sysDictDataParam.getTypeId())) { |
|
91 |
queryWrapper.eq(SysDictData::getTypeId, sysDictDataParam.getTypeId()); |
|
92 |
} |
|
93 |
} |
|
94 |
//查询未删除的 |
|
95 |
queryWrapper.ne(SysDictData::getStatus, CommonStatusEnum.DELETED.getCode()); |
|
96 |
//根据排序升序排列,序号越小越在前 |
|
97 |
queryWrapper.orderByAsc(SysDictData::getSort); |
|
98 |
return this.list(queryWrapper); |
|
99 |
} |
|
100 |
|
|
101 |
@Override |
|
102 |
public void add(SysDictDataParam sysDictDataParam) { |
|
103 |
|
|
104 |
//校验参数,检查是否存在重复的编码,不排除当前添加的这条记录 |
|
105 |
checkParam(sysDictDataParam, false); |
|
106 |
|
|
107 |
//将dto转为实体 |
|
108 |
SysDictData sysDictData = new SysDictData(); |
|
109 |
BeanUtil.copyProperties(sysDictDataParam, sysDictData); |
|
110 |
|
|
111 |
//设置状态为启用 |
|
112 |
sysDictData.setStatus(CommonStatusEnum.ENABLE.getCode()); |
|
113 |
|
|
114 |
this.save(sysDictData); |
|
115 |
} |
|
116 |
|
|
117 |
@Override |
|
118 |
public void delete(SysDictDataParam sysDictDataParam) { |
|
119 |
|
|
120 |
//根据id查询实体 |
|
121 |
SysDictData sysDictData = this.querySysDictData(sysDictDataParam); |
|
122 |
|
|
123 |
//逻辑删除,修改状态 |
|
124 |
sysDictData.setStatus(CommonStatusEnum.DELETED.getCode()); |
|
125 |
|
|
126 |
//更新实体 |
|
127 |
this.updateById(sysDictData); |
|
128 |
} |
|
129 |
|
|
130 |
@Override |
|
131 |
public void edit(SysDictDataParam sysDictDataParam) { |
|
132 |
|
|
133 |
//根据id查询实体 |
|
134 |
SysDictData sysDictData = this.querySysDictData(sysDictDataParam); |
|
135 |
|
|
136 |
//校验参数,检查是否存在重复的编码或者名称,排除当前编辑的这条记录 |
|
137 |
checkParam(sysDictDataParam, true); |
|
138 |
|
|
139 |
//请求参数转化为实体 |
|
140 |
BeanUtil.copyProperties(sysDictDataParam, sysDictData); |
|
141 |
|
|
142 |
//不能修改状态,用修改状态接口修改状态 |
|
143 |
sysDictData.setStatus(null); |
|
144 |
|
|
145 |
this.updateById(sysDictData); |
|
146 |
} |
|
147 |
|
|
148 |
@Override |
|
149 |
public SysDictData detail(SysDictDataParam sysDictDataParam) { |
|
150 |
return this.querySysDictData(sysDictDataParam); |
|
151 |
} |
|
152 |
|
|
153 |
@Override |
|
154 |
public List<Dict> getDictDataListByDictTypeId(Long dictTypeId) { |
|
155 |
|
|
156 |
//构造查询条件 |
|
157 |
LambdaQueryWrapper<SysDictData> queryWrapper = new LambdaQueryWrapper<SysDictData>(); |
|
158 |
queryWrapper.eq(SysDictData::getTypeId, dictTypeId).ne(SysDictData::getStatus, CommonStatusEnum.DELETED.getCode()); |
|
159 |
//根据排序升序排列,序号越小越在前 |
|
160 |
queryWrapper.orderByAsc(SysDictData::getSort); |
|
161 |
//查询dictTypeId下所有的字典项 |
|
162 |
List<SysDictData> results = this.list(queryWrapper); |
|
163 |
|
|
164 |
//抽取code和value封装到map返回 |
|
165 |
List<Dict> dictList = CollectionUtil.newArrayList(); |
|
166 |
results.forEach(sysDictData -> { |
|
167 |
Dict dict = Dict.create(); |
|
168 |
dict.put(CommonConstant.CODE, sysDictData.getCode()); |
|
169 |
dict.put(CommonConstant.VALUE, sysDictData.getValue()); |
|
170 |
dictList.add(dict); |
|
171 |
}); |
|
172 |
|
|
173 |
return dictList; |
|
174 |
} |
|
175 |
|
|
176 |
@Override |
|
177 |
public void deleteByTypeId(Long typeId) { |
|
178 |
//将所有typeId为某值的记录全部置为delete状态 |
|
179 |
LambdaUpdateWrapper<SysDictData> queryWrapper = new LambdaUpdateWrapper<>(); |
|
180 |
queryWrapper.eq(SysDictData::getTypeId, typeId) |
|
181 |
.set(SysDictData::getStatus, CommonStatusEnum.DELETED.getCode()); |
|
182 |
this.update(queryWrapper); |
|
183 |
} |
|
184 |
|
|
185 |
@Override |
|
186 |
public void changeStatus(SysDictDataParam sysDictDataParam) { |
|
187 |
//根据id查询实体 |
|
188 |
SysDictData sysDictData = this.querySysDictData(sysDictDataParam); |
|
189 |
Long id = sysDictData.getId(); |
|
190 |
|
|
191 |
Integer status = sysDictDataParam.getStatus(); |
|
192 |
|
|
193 |
//校验状态在不在枚举值里 |
|
194 |
CommonStatusEnum.validateStatus(status); |
|
195 |
|
|
196 |
//更新枚举,更新只能更新未删除状态的 |
|
197 |
LambdaUpdateWrapper<SysDictData> updateWrapper = new LambdaUpdateWrapper<>(); |
|
198 |
updateWrapper.eq(SysDictData::getId, id) |
|
199 |
.and(i -> i.ne(SysDictData::getStatus, CommonStatusEnum.DELETED.getCode())) |
|
200 |
.set(SysDictData::getStatus, status); |
|
201 |
boolean update = this.update(updateWrapper); |
|
202 |
if (!update) { |
|
203 |
throw new ServiceException(StatusExceptionEnum.UPDATE_STATUS_ERROR); |
|
204 |
} |
|
205 |
} |
|
206 |
|
|
207 |
@Override |
|
208 |
public List<String> getDictCodesByDictTypeCode(String... dictTypeCodes) { |
|
209 |
return this.baseMapper.getDictCodesByDictTypeCode(dictTypeCodes); |
|
210 |
} |
|
211 |
|
|
212 |
/** |
|
213 |
* 校验参数,校验是否存在相同的编码 |
|
214 |
* |
|
215 |
* @author xuyuxiang |
|
216 |
* @date 2020/3/31 20:56 |
|
217 |
*/ |
|
218 |
private void checkParam(SysDictDataParam sysDictDataParam, boolean isExcludeSelf) { |
|
219 |
Long id = sysDictDataParam.getId(); |
|
220 |
Long typeId = sysDictDataParam.getTypeId(); |
|
221 |
String code = sysDictDataParam.getCode(); |
|
222 |
|
|
223 |
//构建带code的查询条件 |
|
224 |
LambdaQueryWrapper<SysDictData> queryWrapper = new LambdaQueryWrapper<>(); |
|
225 |
queryWrapper.eq(SysDictData::getTypeId, typeId) |
|
226 |
.eq(SysDictData::getCode, code) |
|
227 |
.ne(SysDictData::getStatus, CommonStatusEnum.DELETED.getCode()); |
|
228 |
|
|
229 |
//如果排除自己,则增加查询条件主键id不等于本条id |
|
230 |
if (isExcludeSelf) { |
|
231 |
queryWrapper.ne(SysDictData::getId, id); |
|
232 |
} |
|
233 |
|
|
234 |
//查询重复记录的数量 |
|
235 |
int countByCode = this.count(queryWrapper); |
|
236 |
|
|
237 |
//如果存在重复的记录,抛出异常,直接返回前端 |
|
238 |
if (countByCode >= 1) { |
|
239 |
throw new ServiceException(SysDictDataExceptionEnum.DICT_DATA_CODE_REPEAT); |
|
240 |
} |
|
241 |
} |
|
242 |
|
|
243 |
/** |
|
244 |
* 获取系统字典值 |
|
245 |
* |
|
246 |
* @author xuyuxiang |
|
247 |
* @date 2020/3/31 20:56 |
|
248 |
*/ |
|
249 |
private SysDictData querySysDictData(SysDictDataParam sysDictDataParam) { |
|
250 |
SysDictData sysDictData = this.getById(sysDictDataParam.getId()); |
|
251 |
if (ObjectUtil.isNull(sysDictData)) { |
|
252 |
throw new ServiceException(SysDictDataExceptionEnum.DICT_DATA_NOT_EXIST); |
|
253 |
} |
|
254 |
return sysDictData; |
|
255 |
} |
|
256 |
} |