提交 | 用户 | 时间
|
fd2207
|
1 |
package com.jcdm.system.service.impl; |
懒 |
2 |
|
|
3 |
import java.util.Comparator; |
|
4 |
import java.util.List; |
|
5 |
import java.util.Map; |
|
6 |
import java.util.stream.Collectors; |
|
7 |
import javax.annotation.PostConstruct; |
|
8 |
import org.springframework.beans.factory.annotation.Autowired; |
|
9 |
import org.springframework.stereotype.Service; |
|
10 |
import org.springframework.transaction.annotation.Transactional; |
|
11 |
import com.jcdm.common.constant.UserConstants; |
|
12 |
import com.jcdm.common.core.domain.entity.SysDictData; |
|
13 |
import com.jcdm.common.core.domain.entity.SysDictType; |
|
14 |
import com.jcdm.common.exception.ServiceException; |
|
15 |
import com.jcdm.common.utils.DictUtils; |
|
16 |
import com.jcdm.common.utils.StringUtils; |
|
17 |
import com.jcdm.system.mapper.SysDictDataMapper; |
|
18 |
import com.jcdm.system.mapper.SysDictTypeMapper; |
|
19 |
import com.jcdm.system.service.ISysDictTypeService; |
|
20 |
|
|
21 |
/** |
|
22 |
* 字典 业务层处理 |
|
23 |
* |
|
24 |
* @author jc |
|
25 |
*/ |
|
26 |
@Service |
|
27 |
public class SysDictTypeServiceImpl implements ISysDictTypeService |
|
28 |
{ |
|
29 |
@Autowired |
|
30 |
private SysDictTypeMapper dictTypeMapper; |
|
31 |
|
|
32 |
@Autowired |
|
33 |
private SysDictDataMapper dictDataMapper; |
|
34 |
|
|
35 |
/** |
|
36 |
* 项目启动时,初始化字典到缓存 |
|
37 |
*/ |
|
38 |
@PostConstruct |
|
39 |
public void init() |
|
40 |
{ |
|
41 |
loadingDictCache(); |
|
42 |
} |
|
43 |
|
|
44 |
/** |
|
45 |
* 根据条件分页查询字典类型 |
|
46 |
* |
|
47 |
* @param dictType 字典类型信息 |
|
48 |
* @return 字典类型集合信息 |
|
49 |
*/ |
|
50 |
@Override |
|
51 |
public List<SysDictType> selectDictTypeList(SysDictType dictType) |
|
52 |
{ |
|
53 |
return dictTypeMapper.selectDictTypeList(dictType); |
|
54 |
} |
|
55 |
|
|
56 |
/** |
|
57 |
* 根据所有字典类型 |
|
58 |
* |
|
59 |
* @return 字典类型集合信息 |
|
60 |
*/ |
|
61 |
@Override |
|
62 |
public List<SysDictType> selectDictTypeAll() |
|
63 |
{ |
|
64 |
return dictTypeMapper.selectDictTypeAll(); |
|
65 |
} |
|
66 |
|
|
67 |
/** |
|
68 |
* 根据字典类型查询字典数据 |
|
69 |
* |
|
70 |
* @param dictType 字典类型 |
|
71 |
* @return 字典数据集合信息 |
|
72 |
*/ |
|
73 |
@Override |
|
74 |
public List<SysDictData> selectDictDataByType(String dictType) |
|
75 |
{ |
|
76 |
List<SysDictData> dictDatas = DictUtils.getDictCache(dictType); |
|
77 |
if (StringUtils.isNotEmpty(dictDatas)) |
|
78 |
{ |
|
79 |
return dictDatas; |
|
80 |
} |
|
81 |
dictDatas = dictDataMapper.selectDictDataByType(dictType); |
|
82 |
if (StringUtils.isNotEmpty(dictDatas)) |
|
83 |
{ |
|
84 |
DictUtils.setDictCache(dictType, dictDatas); |
|
85 |
return dictDatas; |
|
86 |
} |
|
87 |
return null; |
|
88 |
} |
|
89 |
|
|
90 |
/** |
|
91 |
* 根据字典类型ID查询信息 |
|
92 |
* |
|
93 |
* @param dictId 字典类型ID |
|
94 |
* @return 字典类型 |
|
95 |
*/ |
|
96 |
@Override |
|
97 |
public SysDictType selectDictTypeById(Long dictId) |
|
98 |
{ |
|
99 |
return dictTypeMapper.selectDictTypeById(dictId); |
|
100 |
} |
|
101 |
|
|
102 |
/** |
|
103 |
* 根据字典类型查询信息 |
|
104 |
* |
|
105 |
* @param dictType 字典类型 |
|
106 |
* @return 字典类型 |
|
107 |
*/ |
|
108 |
@Override |
|
109 |
public SysDictType selectDictTypeByType(String dictType) |
|
110 |
{ |
|
111 |
return dictTypeMapper.selectDictTypeByType(dictType); |
|
112 |
} |
|
113 |
|
|
114 |
/** |
|
115 |
* 批量删除字典类型信息 |
|
116 |
* |
|
117 |
* @param dictIds 需要删除的字典ID |
|
118 |
*/ |
|
119 |
@Override |
|
120 |
public void deleteDictTypeByIds(Long[] dictIds) |
|
121 |
{ |
|
122 |
for (Long dictId : dictIds) |
|
123 |
{ |
|
124 |
SysDictType dictType = selectDictTypeById(dictId); |
|
125 |
if (dictDataMapper.countDictDataByType(dictType.getDictType()) > 0) |
|
126 |
{ |
|
127 |
throw new ServiceException(String.format("%1$s已分配,不能删除", dictType.getDictName())); |
|
128 |
} |
|
129 |
dictTypeMapper.deleteDictTypeById(dictId); |
|
130 |
DictUtils.removeDictCache(dictType.getDictType()); |
|
131 |
} |
|
132 |
} |
|
133 |
|
|
134 |
/** |
|
135 |
* 加载字典缓存数据 |
|
136 |
*/ |
|
137 |
@Override |
|
138 |
public void loadingDictCache() |
|
139 |
{ |
|
140 |
SysDictData dictData = new SysDictData(); |
|
141 |
dictData.setStatus("0"); |
|
142 |
Map<String, List<SysDictData>> dictDataMap = dictDataMapper.selectDictDataList(dictData).stream().collect(Collectors.groupingBy(SysDictData::getDictType)); |
|
143 |
for (Map.Entry<String, List<SysDictData>> entry : dictDataMap.entrySet()) |
|
144 |
{ |
|
145 |
DictUtils.setDictCache(entry.getKey(), entry.getValue().stream().sorted(Comparator.comparing(SysDictData::getDictSort)).collect(Collectors.toList())); |
|
146 |
} |
|
147 |
} |
|
148 |
|
|
149 |
/** |
|
150 |
* 清空字典缓存数据 |
|
151 |
*/ |
|
152 |
@Override |
|
153 |
public void clearDictCache() |
|
154 |
{ |
|
155 |
DictUtils.clearDictCache(); |
|
156 |
} |
|
157 |
|
|
158 |
/** |
|
159 |
* 重置字典缓存数据 |
|
160 |
*/ |
|
161 |
@Override |
|
162 |
public void resetDictCache() |
|
163 |
{ |
|
164 |
clearDictCache(); |
|
165 |
loadingDictCache(); |
|
166 |
} |
|
167 |
|
|
168 |
/** |
|
169 |
* 新增保存字典类型信息 |
|
170 |
* |
|
171 |
* @param dict 字典类型信息 |
|
172 |
* @return 结果 |
|
173 |
*/ |
|
174 |
@Override |
|
175 |
public int insertDictType(SysDictType dict) |
|
176 |
{ |
|
177 |
int row = dictTypeMapper.insertDictType(dict); |
|
178 |
if (row > 0) |
|
179 |
{ |
|
180 |
DictUtils.setDictCache(dict.getDictType(), null); |
|
181 |
} |
|
182 |
return row; |
|
183 |
} |
|
184 |
|
|
185 |
/** |
|
186 |
* 修改保存字典类型信息 |
|
187 |
* |
|
188 |
* @param dict 字典类型信息 |
|
189 |
* @return 结果 |
|
190 |
*/ |
|
191 |
@Override |
|
192 |
@Transactional |
|
193 |
public int updateDictType(SysDictType dict) |
|
194 |
{ |
|
195 |
SysDictType oldDict = dictTypeMapper.selectDictTypeById(dict.getDictId()); |
|
196 |
dictDataMapper.updateDictDataType(oldDict.getDictType(), dict.getDictType()); |
|
197 |
int row = dictTypeMapper.updateDictType(dict); |
|
198 |
if (row > 0) |
|
199 |
{ |
|
200 |
List<SysDictData> dictDatas = dictDataMapper.selectDictDataByType(dict.getDictType()); |
|
201 |
DictUtils.setDictCache(dict.getDictType(), dictDatas); |
|
202 |
} |
|
203 |
return row; |
|
204 |
} |
|
205 |
|
|
206 |
/** |
|
207 |
* 校验字典类型称是否唯一 |
|
208 |
* |
|
209 |
* @param dict 字典类型 |
|
210 |
* @return 结果 |
|
211 |
*/ |
|
212 |
@Override |
|
213 |
public boolean checkDictTypeUnique(SysDictType dict) |
|
214 |
{ |
|
215 |
Long dictId = StringUtils.isNull(dict.getDictId()) ? -1L : dict.getDictId(); |
|
216 |
SysDictType dictType = dictTypeMapper.checkDictTypeUnique(dict.getDictType()); |
|
217 |
if (StringUtils.isNotNull(dictType) && dictType.getDictId().longValue() != dictId.longValue()) |
|
218 |
{ |
|
219 |
return UserConstants.NOT_UNIQUE; |
|
220 |
} |
|
221 |
return UserConstants.UNIQUE; |
|
222 |
} |
|
223 |
} |