提交 | 用户 | 时间
|
e7c126
|
1 |
package com.iailab.module.system.service.dict; |
H |
2 |
|
|
3 |
import cn.hutool.core.collection.CollUtil; |
|
4 |
import com.iailab.framework.common.enums.CommonStatusEnum; |
|
5 |
import com.iailab.framework.common.pojo.PageResult; |
|
6 |
import com.iailab.framework.common.util.collection.CollectionUtils; |
|
7 |
import com.iailab.framework.common.util.object.BeanUtils; |
|
8 |
import com.iailab.module.system.controller.admin.dict.vo.data.DictDataPageReqVO; |
|
9 |
import com.iailab.module.system.controller.admin.dict.vo.data.DictDataSaveReqVO; |
|
10 |
import com.iailab.module.system.dal.dataobject.dict.DictDataDO; |
|
11 |
import com.iailab.module.system.dal.dataobject.dict.DictTypeDO; |
|
12 |
import com.iailab.module.system.dal.mysql.dict.DictDataMapper; |
|
13 |
import com.google.common.annotations.VisibleForTesting; |
|
14 |
import lombok.extern.slf4j.Slf4j; |
|
15 |
import org.springframework.stereotype.Service; |
|
16 |
|
|
17 |
import javax.annotation.Resource; |
|
18 |
import java.util.Collection; |
|
19 |
import java.util.Comparator; |
|
20 |
import java.util.List; |
|
21 |
import java.util.Map; |
|
22 |
|
|
23 |
import static com.iailab.framework.common.exception.util.ServiceExceptionUtil.exception; |
|
24 |
import static com.iailab.module.system.enums.ErrorCodeConstants.*; |
|
25 |
|
|
26 |
/** |
|
27 |
* 字典数据 Service 实现类 |
|
28 |
* |
|
29 |
* @author ruoyi |
|
30 |
*/ |
|
31 |
@Service |
|
32 |
@Slf4j |
|
33 |
public class DictDataServiceImpl implements DictDataService { |
|
34 |
|
|
35 |
/** |
|
36 |
* 排序 dictType > sort |
|
37 |
*/ |
|
38 |
private static final Comparator<DictDataDO> COMPARATOR_TYPE_AND_SORT = Comparator |
|
39 |
.comparing(DictDataDO::getDictType) |
|
40 |
.thenComparingInt(DictDataDO::getSort); |
|
41 |
|
|
42 |
@Resource |
|
43 |
private DictTypeService dictTypeService; |
|
44 |
|
|
45 |
@Resource |
|
46 |
private DictDataMapper dictDataMapper; |
|
47 |
|
|
48 |
@Override |
|
49 |
public List<DictDataDO> getDictDataList(Integer status, String dictType) { |
|
50 |
List<DictDataDO> list = dictDataMapper.selectListByStatusAndDictType(status, dictType); |
|
51 |
list.sort(COMPARATOR_TYPE_AND_SORT); |
|
52 |
return list; |
|
53 |
} |
|
54 |
|
|
55 |
@Override |
|
56 |
public PageResult<DictDataDO> getDictDataPage(DictDataPageReqVO pageReqVO) { |
|
57 |
return dictDataMapper.selectPage(pageReqVO); |
|
58 |
} |
|
59 |
|
|
60 |
@Override |
|
61 |
public DictDataDO getDictData(Long id) { |
|
62 |
return dictDataMapper.selectById(id); |
|
63 |
} |
|
64 |
|
|
65 |
@Override |
|
66 |
public Long createDictData(DictDataSaveReqVO createReqVO) { |
|
67 |
// 校验字典类型有效 |
|
68 |
validateDictTypeExists(createReqVO.getDictType()); |
|
69 |
// 校验字典数据的值的唯一性 |
|
70 |
validateDictDataValueUnique(null, createReqVO.getDictType(), createReqVO.getValue()); |
|
71 |
|
|
72 |
// 插入字典类型 |
|
73 |
DictDataDO dictData = BeanUtils.toBean(createReqVO, DictDataDO.class); |
|
74 |
dictDataMapper.insert(dictData); |
|
75 |
return dictData.getId(); |
|
76 |
} |
|
77 |
|
|
78 |
@Override |
|
79 |
public void updateDictData(DictDataSaveReqVO updateReqVO) { |
|
80 |
// 校验自己存在 |
|
81 |
validateDictDataExists(updateReqVO.getId()); |
|
82 |
// 校验字典类型有效 |
|
83 |
validateDictTypeExists(updateReqVO.getDictType()); |
|
84 |
// 校验字典数据的值的唯一性 |
|
85 |
validateDictDataValueUnique(updateReqVO.getId(), updateReqVO.getDictType(), updateReqVO.getValue()); |
|
86 |
|
|
87 |
// 更新字典类型 |
|
88 |
DictDataDO updateObj = BeanUtils.toBean(updateReqVO, DictDataDO.class); |
|
89 |
dictDataMapper.updateById(updateObj); |
|
90 |
} |
|
91 |
|
|
92 |
@Override |
|
93 |
public void deleteDictData(Long id) { |
|
94 |
// 校验是否存在 |
|
95 |
validateDictDataExists(id); |
|
96 |
|
|
97 |
// 删除字典数据 |
|
98 |
dictDataMapper.deleteById(id); |
|
99 |
} |
|
100 |
|
|
101 |
@Override |
|
102 |
public long getDictDataCountByDictType(String dictType) { |
|
103 |
return dictDataMapper.selectCountByDictType(dictType); |
|
104 |
} |
|
105 |
|
|
106 |
@VisibleForTesting |
|
107 |
public void validateDictDataValueUnique(Long id, String dictType, String value) { |
|
108 |
DictDataDO dictData = dictDataMapper.selectByDictTypeAndValue(dictType, value); |
|
109 |
if (dictData == null) { |
|
110 |
return; |
|
111 |
} |
|
112 |
// 如果 id 为空,说明不用比较是否为相同 id 的字典数据 |
|
113 |
if (id == null) { |
|
114 |
throw exception(DICT_DATA_VALUE_DUPLICATE); |
|
115 |
} |
|
116 |
if (!dictData.getId().equals(id)) { |
|
117 |
throw exception(DICT_DATA_VALUE_DUPLICATE); |
|
118 |
} |
|
119 |
} |
|
120 |
|
|
121 |
@VisibleForTesting |
|
122 |
public void validateDictDataExists(Long id) { |
|
123 |
if (id == null) { |
|
124 |
return; |
|
125 |
} |
|
126 |
DictDataDO dictData = dictDataMapper.selectById(id); |
|
127 |
if (dictData == null) { |
|
128 |
throw exception(DICT_DATA_NOT_EXISTS); |
|
129 |
} |
|
130 |
} |
|
131 |
|
|
132 |
@VisibleForTesting |
|
133 |
public void validateDictTypeExists(String type) { |
|
134 |
DictTypeDO dictType = dictTypeService.getDictType(type); |
|
135 |
if (dictType == null) { |
|
136 |
throw exception(DICT_TYPE_NOT_EXISTS); |
|
137 |
} |
|
138 |
if (!CommonStatusEnum.ENABLE.getStatus().equals(dictType.getStatus())) { |
|
139 |
throw exception(DICT_TYPE_NOT_ENABLE); |
|
140 |
} |
|
141 |
} |
|
142 |
|
|
143 |
@Override |
|
144 |
public void validateDictDataList(String dictType, Collection<String> values) { |
|
145 |
if (CollUtil.isEmpty(values)) { |
|
146 |
return; |
|
147 |
} |
|
148 |
Map<String, DictDataDO> dictDataMap = CollectionUtils.convertMap( |
|
149 |
dictDataMapper.selectByDictTypeAndValues(dictType, values), DictDataDO::getValue); |
|
150 |
// 校验 |
|
151 |
values.forEach(value -> { |
|
152 |
DictDataDO dictData = dictDataMap.get(value); |
|
153 |
if (dictData == null) { |
|
154 |
throw exception(DICT_DATA_NOT_EXISTS); |
|
155 |
} |
|
156 |
if (!CommonStatusEnum.ENABLE.getStatus().equals(dictData.getStatus())) { |
|
157 |
throw exception(DICT_DATA_NOT_ENABLE, dictData.getLabel()); |
|
158 |
} |
|
159 |
}); |
|
160 |
} |
|
161 |
|
|
162 |
@Override |
|
163 |
public DictDataDO getDictData(String dictType, String value) { |
|
164 |
return dictDataMapper.selectByDictTypeAndValue(dictType, value); |
|
165 |
} |
|
166 |
|
|
167 |
@Override |
|
168 |
public DictDataDO parseDictData(String dictType, String label) { |
|
169 |
return dictDataMapper.selectByDictTypeAndLabel(dictType, label); |
|
170 |
} |
|
171 |
|
|
172 |
@Override |
|
173 |
public List<DictDataDO> getDictDataListByDictType(String dictType) { |
|
174 |
List<DictDataDO> list = dictDataMapper.selectList(DictDataDO::getDictType, dictType); |
|
175 |
list.sort(Comparator.comparing(DictDataDO::getSort)); |
|
176 |
return list; |
|
177 |
} |
|
178 |
|
|
179 |
} |