提交 | 用户 | 时间
|
e7c126
|
1 |
package com.iailab.module.system.service.dict; |
H |
2 |
|
|
3 |
import cn.hutool.core.util.StrUtil; |
|
4 |
import com.iailab.framework.common.pojo.PageResult; |
|
5 |
import com.iailab.framework.common.util.date.LocalDateTimeUtils; |
|
6 |
import com.iailab.framework.common.util.object.BeanUtils; |
|
7 |
import com.iailab.module.system.controller.admin.dict.vo.type.DictTypePageReqVO; |
|
8 |
import com.iailab.module.system.controller.admin.dict.vo.type.DictTypeSaveReqVO; |
|
9 |
import com.iailab.module.system.dal.dataobject.dict.DictTypeDO; |
|
10 |
import com.iailab.module.system.dal.mysql.dict.DictTypeMapper; |
|
11 |
import com.google.common.annotations.VisibleForTesting; |
|
12 |
import org.springframework.stereotype.Service; |
|
13 |
|
|
14 |
import javax.annotation.Resource; |
|
15 |
import java.time.LocalDateTime; |
|
16 |
import java.util.List; |
|
17 |
|
|
18 |
import static com.iailab.framework.common.exception.util.ServiceExceptionUtil.exception; |
|
19 |
import static com.iailab.module.system.enums.ErrorCodeConstants.*; |
|
20 |
|
|
21 |
/** |
|
22 |
* 字典类型 Service 实现类 |
|
23 |
* |
|
24 |
* @author iailab |
|
25 |
*/ |
|
26 |
@Service |
|
27 |
public class DictTypeServiceImpl implements DictTypeService { |
|
28 |
|
|
29 |
@Resource |
|
30 |
private DictDataService dictDataService; |
|
31 |
|
|
32 |
@Resource |
|
33 |
private DictTypeMapper dictTypeMapper; |
|
34 |
|
|
35 |
@Override |
|
36 |
public PageResult<DictTypeDO> getDictTypePage(DictTypePageReqVO pageReqVO) { |
|
37 |
return dictTypeMapper.selectPage(pageReqVO); |
|
38 |
} |
|
39 |
|
|
40 |
@Override |
|
41 |
public DictTypeDO getDictType(Long id) { |
|
42 |
return dictTypeMapper.selectById(id); |
|
43 |
} |
|
44 |
|
|
45 |
@Override |
|
46 |
public DictTypeDO getDictType(String type) { |
|
47 |
return dictTypeMapper.selectByType(type); |
|
48 |
} |
|
49 |
|
|
50 |
@Override |
|
51 |
public Long createDictType(DictTypeSaveReqVO createReqVO) { |
|
52 |
// 校验字典类型的名字的唯一性 |
|
53 |
validateDictTypeNameUnique(null, createReqVO.getName()); |
|
54 |
// 校验字典类型的类型的唯一性 |
|
55 |
validateDictTypeUnique(null, createReqVO.getType()); |
|
56 |
|
|
57 |
// 插入字典类型 |
|
58 |
DictTypeDO dictType = BeanUtils.toBean(createReqVO, DictTypeDO.class); |
|
59 |
dictType.setDeletedTime(LocalDateTimeUtils.EMPTY); // 唯一索引,避免 null 值 |
|
60 |
dictTypeMapper.insert(dictType); |
|
61 |
return dictType.getId(); |
|
62 |
} |
|
63 |
|
|
64 |
@Override |
|
65 |
public void updateDictType(DictTypeSaveReqVO updateReqVO) { |
|
66 |
// 校验自己存在 |
|
67 |
validateDictTypeExists(updateReqVO.getId()); |
|
68 |
// 校验字典类型的名字的唯一性 |
|
69 |
validateDictTypeNameUnique(updateReqVO.getId(), updateReqVO.getName()); |
|
70 |
// 校验字典类型的类型的唯一性 |
|
71 |
validateDictTypeUnique(updateReqVO.getId(), updateReqVO.getType()); |
|
72 |
|
|
73 |
// 更新字典类型 |
|
74 |
DictTypeDO updateObj = BeanUtils.toBean(updateReqVO, DictTypeDO.class); |
|
75 |
dictTypeMapper.updateById(updateObj); |
|
76 |
} |
|
77 |
|
|
78 |
@Override |
|
79 |
public void deleteDictType(Long id) { |
|
80 |
// 校验是否存在 |
|
81 |
DictTypeDO dictType = validateDictTypeExists(id); |
|
82 |
// 校验是否有字典数据 |
|
83 |
if (dictDataService.getDictDataCountByDictType(dictType.getType()) > 0) { |
|
84 |
throw exception(DICT_TYPE_HAS_CHILDREN); |
|
85 |
} |
|
86 |
// 删除字典类型 |
|
87 |
dictTypeMapper.updateToDelete(id, LocalDateTime.now()); |
|
88 |
} |
|
89 |
|
|
90 |
@Override |
|
91 |
public List<DictTypeDO> getDictTypeList() { |
|
92 |
return dictTypeMapper.selectList(); |
|
93 |
} |
|
94 |
|
|
95 |
@VisibleForTesting |
|
96 |
void validateDictTypeNameUnique(Long id, String name) { |
|
97 |
DictTypeDO dictType = dictTypeMapper.selectByName(name); |
|
98 |
if (dictType == null) { |
|
99 |
return; |
|
100 |
} |
|
101 |
// 如果 id 为空,说明不用比较是否为相同 id 的字典类型 |
|
102 |
if (id == null) { |
|
103 |
throw exception(DICT_TYPE_NAME_DUPLICATE); |
|
104 |
} |
|
105 |
if (!dictType.getId().equals(id)) { |
|
106 |
throw exception(DICT_TYPE_NAME_DUPLICATE); |
|
107 |
} |
|
108 |
} |
|
109 |
|
|
110 |
@VisibleForTesting |
|
111 |
void validateDictTypeUnique(Long id, String type) { |
|
112 |
if (StrUtil.isEmpty(type)) { |
|
113 |
return; |
|
114 |
} |
|
115 |
DictTypeDO dictType = dictTypeMapper.selectByType(type); |
|
116 |
if (dictType == null) { |
|
117 |
return; |
|
118 |
} |
|
119 |
// 如果 id 为空,说明不用比较是否为相同 id 的字典类型 |
|
120 |
if (id == null) { |
|
121 |
throw exception(DICT_TYPE_TYPE_DUPLICATE); |
|
122 |
} |
|
123 |
if (!dictType.getId().equals(id)) { |
|
124 |
throw exception(DICT_TYPE_TYPE_DUPLICATE); |
|
125 |
} |
|
126 |
} |
|
127 |
|
|
128 |
@VisibleForTesting |
|
129 |
DictTypeDO validateDictTypeExists(Long id) { |
|
130 |
if (id == null) { |
|
131 |
return null; |
|
132 |
} |
|
133 |
DictTypeDO dictType = dictTypeMapper.selectById(id); |
|
134 |
if (dictType == null) { |
|
135 |
throw exception(DICT_TYPE_NOT_EXISTS); |
|
136 |
} |
|
137 |
return dictType; |
|
138 |
} |
|
139 |
|
|
140 |
} |