提交 | 用户 | 时间
|
e7c126
|
1 |
package ${basePackage}.module.${table.moduleName}.service.${table.businessName}; |
H |
2 |
|
|
3 |
import org.springframework.stereotype.Service; |
|
4 |
import ${jakartaPackage}.annotation.Resource; |
|
5 |
import org.springframework.validation.annotation.Validated; |
|
6 |
import org.springframework.transaction.annotation.Transactional; |
|
7 |
|
|
8 |
import java.util.*; |
|
9 |
import ${basePackage}.module.${table.moduleName}.controller.${sceneEnum.basePackage}.${table.businessName}.vo.*; |
|
10 |
import ${basePackage}.module.${table.moduleName}.dal.dataobject.${table.businessName}.${table.className}DO; |
|
11 |
## 特殊:主子表专属逻辑 |
|
12 |
#foreach ($subTable in $subTables) |
|
13 |
import ${basePackage}.module.${subTable.moduleName}.dal.dataobject.${subTable.businessName}.${subTable.className}DO; |
|
14 |
#end |
|
15 |
import ${PageResultClassName}; |
|
16 |
import ${PageParamClassName}; |
|
17 |
import ${BeanUtils}; |
|
18 |
|
|
19 |
import ${basePackage}.module.${table.moduleName}.dal.mysql.${table.businessName}.${table.className}Mapper; |
|
20 |
## 特殊:主子表专属逻辑 |
|
21 |
#foreach ($subTable in $subTables) |
|
22 |
#set ($index = $foreach.count - 1) |
|
23 |
import ${basePackage}.module.${subTable.moduleName}.dal.mysql.${subTable.businessName}.${subTable.className}Mapper; |
|
24 |
#end |
|
25 |
|
|
26 |
import static ${ServiceExceptionUtilClassName}.exception; |
|
27 |
import static ${basePackage}.module.${table.moduleName}.enums.ErrorCodeConstants.*; |
|
28 |
|
|
29 |
/** |
|
30 |
* ${table.classComment} Service 实现类 |
|
31 |
* |
|
32 |
* @author ${table.author} |
|
33 |
*/ |
|
34 |
@Service |
|
35 |
@Validated |
|
36 |
public class ${table.className}ServiceImpl implements ${table.className}Service { |
|
37 |
|
|
38 |
@Resource |
|
39 |
private ${table.className}Mapper ${classNameVar}Mapper; |
|
40 |
## 特殊:主子表专属逻辑 |
|
41 |
#foreach ($subTable in $subTables) |
|
42 |
#set ($index = $foreach.count - 1) |
|
43 |
@Resource |
|
44 |
private ${subTable.className}Mapper ${subClassNameVars.get($index)}Mapper; |
|
45 |
#end |
|
46 |
|
|
47 |
@Override |
|
48 |
## 特殊:主子表专属逻辑(非 ERP 模式) |
|
49 |
#if ( $subTables && $subTables.size() > 0 && $table.templateType != 11 ) |
|
50 |
@Transactional(rollbackFor = Exception.class) |
|
51 |
#end |
|
52 |
public ${primaryColumn.javaType} create${simpleClassName}(${sceneEnum.prefixClass}${table.className}SaveReqVO createReqVO) { |
|
53 |
## 特殊:树表专属逻辑 |
|
54 |
#if ( $table.templateType == 2 ) |
|
55 |
#set ($TreeParentJavaField = $treeParentColumn.javaField.substring(0,1).toUpperCase() + ${treeParentColumn.javaField.substring(1)})##首字母大写 |
|
56 |
#set ($TreeNameJavaField = $treeNameColumn.javaField.substring(0,1).toUpperCase() + ${treeNameColumn.javaField.substring(1)})##首字母大写 |
|
57 |
// 校验${treeParentColumn.columnComment}的有效性 |
|
58 |
validateParent${simpleClassName}(null, createReqVO.get${TreeParentJavaField}()); |
|
59 |
// 校验${treeNameColumn.columnComment}的唯一性 |
|
60 |
validate${simpleClassName}${TreeNameJavaField}Unique(null, createReqVO.get${TreeParentJavaField}(), createReqVO.get${TreeNameJavaField}()); |
|
61 |
|
|
62 |
#end |
|
63 |
// 插入 |
|
64 |
${table.className}DO ${classNameVar} = BeanUtils.toBean(createReqVO, ${table.className}DO.class); |
|
65 |
${classNameVar}Mapper.insert(${classNameVar}); |
|
66 |
## 特殊:主子表专属逻辑(非 ERP 模式) |
|
67 |
#if ( $subTables && $subTables.size() > 0 && $table.templateType != 11 ) |
|
68 |
|
|
69 |
// 插入子表 |
|
70 |
#foreach ($subTable in $subTables) |
|
71 |
#set ($index = $foreach.count - 1) |
|
72 |
#set ($subSimpleClassName = $subSimpleClassNames.get($index)) |
|
73 |
#set ($subJoinColumn = $subJoinColumns.get($index))##当前 join 字段 |
|
74 |
#set ($SubJoinColumnName = $subJoinColumn.javaField.substring(0,1).toUpperCase() + ${subJoinColumn.javaField.substring(1)})##首字母大写 |
|
75 |
#if ( $subTable.subJoinMany) |
|
76 |
create${subSimpleClassName}List(${classNameVar}.getId(), createReqVO.get${subSimpleClassNames.get($index)}s()); |
|
77 |
#else |
|
78 |
create${subSimpleClassName}(${classNameVar}.getId(), createReqVO.get${subSimpleClassNames.get($index)}()); |
|
79 |
#end |
|
80 |
#end |
|
81 |
#end |
|
82 |
// 返回 |
|
83 |
return ${classNameVar}.getId(); |
|
84 |
} |
|
85 |
|
|
86 |
@Override |
|
87 |
## 特殊:主子表专属逻辑(非 ERP 模式) |
|
88 |
#if ( $subTables && $subTables.size() > 0 && $table.templateType != 11 ) |
|
89 |
@Transactional(rollbackFor = Exception.class) |
|
90 |
#end |
|
91 |
public void update${simpleClassName}(${sceneEnum.prefixClass}${table.className}SaveReqVO updateReqVO) { |
|
92 |
// 校验存在 |
|
93 |
validate${simpleClassName}Exists(updateReqVO.getId()); |
|
94 |
## 特殊:树表专属逻辑 |
|
95 |
#if ( $table.templateType == 2 ) |
|
96 |
#set ($TreeParentJavaField = $treeParentColumn.javaField.substring(0,1).toUpperCase() + ${treeParentColumn.javaField.substring(1)})##首字母大写 |
|
97 |
#set ($TreeNameJavaField = $treeNameColumn.javaField.substring(0,1).toUpperCase() + ${treeNameColumn.javaField.substring(1)})##首字母大写 |
|
98 |
// 校验${treeParentColumn.columnComment}的有效性 |
|
99 |
validateParent${simpleClassName}(updateReqVO.getId(), updateReqVO.get${TreeParentJavaField}()); |
|
100 |
// 校验${treeNameColumn.columnComment}的唯一性 |
|
101 |
validate${simpleClassName}${TreeNameJavaField}Unique(updateReqVO.getId(), updateReqVO.get${TreeParentJavaField}(), updateReqVO.get${TreeNameJavaField}()); |
|
102 |
|
|
103 |
#end |
|
104 |
// 更新 |
|
105 |
${table.className}DO updateObj = BeanUtils.toBean(updateReqVO, ${table.className}DO.class); |
|
106 |
${classNameVar}Mapper.updateById(updateObj); |
|
107 |
## 特殊:主子表专属逻辑(非 ERP 模式) |
|
108 |
#if ( $subTables && $subTables.size() > 0 && $table.templateType != 11) |
|
109 |
|
|
110 |
// 更新子表 |
|
111 |
#foreach ($subTable in $subTables) |
|
112 |
#set ($index = $foreach.count - 1) |
|
113 |
#set ($subSimpleClassName = $subSimpleClassNames.get($index)) |
|
114 |
#set ($subJoinColumn = $subJoinColumns.get($index))##当前 join 字段 |
|
115 |
#set ($SubJoinColumnName = $subJoinColumn.javaField.substring(0,1).toUpperCase() + ${subJoinColumn.javaField.substring(1)})##首字母大写 |
|
116 |
#if ( $subTable.subJoinMany) |
|
117 |
update${subSimpleClassName}List(updateReqVO.getId(), updateReqVO.get${subSimpleClassNames.get($index)}s()); |
|
118 |
#else |
|
119 |
update${subSimpleClassName}(updateReqVO.getId(), updateReqVO.get${subSimpleClassNames.get($index)}()); |
|
120 |
#end |
|
121 |
#end |
|
122 |
#end |
|
123 |
} |
|
124 |
|
|
125 |
@Override |
|
126 |
## 特殊:主子表专属逻辑 |
|
127 |
#if ( $subTables && $subTables.size() > 0) |
|
128 |
@Transactional(rollbackFor = Exception.class) |
|
129 |
#end |
|
130 |
public void delete${simpleClassName}(${primaryColumn.javaType} id) { |
|
131 |
// 校验存在 |
|
132 |
validate${simpleClassName}Exists(id); |
|
133 |
## 特殊:树表专属逻辑 |
|
134 |
#if ( $table.templateType == 2 ) |
|
135 |
#set ($ParentJavaField = $treeParentColumn.javaField.substring(0,1).toUpperCase() + ${treeParentColumn.javaField.substring(1)})##首字母大写 |
|
136 |
// 校验是否有子${table.classComment} |
|
137 |
if (${classNameVar}Mapper.selectCountBy${ParentJavaField}(id) > 0) { |
|
138 |
throw exception(${simpleClassName_underlineCase.toUpperCase()}_EXITS_CHILDREN); |
|
139 |
} |
|
140 |
#end |
|
141 |
// 删除 |
|
142 |
${classNameVar}Mapper.deleteById(id); |
|
143 |
## 特殊:主子表专属逻辑 |
|
144 |
#if ( $subTables && $subTables.size() > 0) |
|
145 |
|
|
146 |
// 删除子表 |
|
147 |
#foreach ($subTable in $subTables) |
|
148 |
#set ($index = $foreach.count - 1) |
|
149 |
#set ($subSimpleClassName = $subSimpleClassNames.get($index)) |
|
150 |
#set ($subJoinColumn = $subJoinColumns.get($index))##当前 join 字段 |
|
151 |
#set ($SubJoinColumnName = $subJoinColumn.javaField.substring(0,1).toUpperCase() + ${subJoinColumn.javaField.substring(1)})##首字母大写 |
|
152 |
delete${subSimpleClassName}By${SubJoinColumnName}(id); |
|
153 |
#end |
|
154 |
#end |
|
155 |
} |
|
156 |
|
|
157 |
private void validate${simpleClassName}Exists(${primaryColumn.javaType} id) { |
|
158 |
if (${classNameVar}Mapper.selectById(id) == null) { |
|
159 |
throw exception(${simpleClassName_underlineCase.toUpperCase()}_NOT_EXISTS); |
|
160 |
} |
|
161 |
} |
|
162 |
|
|
163 |
## 特殊:树表专属逻辑 |
|
164 |
#if ( $table.templateType == 2 ) |
|
165 |
#set ($TreeParentJavaField = $treeParentColumn.javaField.substring(0,1).toUpperCase() + ${treeParentColumn.javaField.substring(1)})##首字母大写 |
|
166 |
#set ($TreeNameJavaField = $treeNameColumn.javaField.substring(0,1).toUpperCase() + ${treeNameColumn.javaField.substring(1)})##首字母大写 |
|
167 |
private void validateParent${simpleClassName}(Long id, Long ${treeParentColumn.javaField}) { |
|
168 |
if (${treeParentColumn.javaField} == null || ${simpleClassName}DO.${treeParentColumn_javaField_underlineCase.toUpperCase()}_ROOT.equals(${treeParentColumn.javaField})) { |
|
169 |
return; |
|
170 |
} |
|
171 |
// 1. 不能设置自己为父${table.classComment} |
|
172 |
if (Objects.equals(id, ${treeParentColumn.javaField})) { |
|
173 |
throw exception(${simpleClassName_underlineCase.toUpperCase()}_PARENT_ERROR); |
|
174 |
} |
|
175 |
// 2. 父${table.classComment}不存在 |
|
176 |
${simpleClassName}DO parent${simpleClassName} = ${classNameVar}Mapper.selectById(${treeParentColumn.javaField}); |
|
177 |
if (parent${simpleClassName} == null) { |
|
178 |
throw exception(${simpleClassName_underlineCase.toUpperCase()}_PARENT_NOT_EXITS); |
|
179 |
} |
|
180 |
// 3. 递归校验父${table.classComment},如果父${table.classComment}是自己的子${table.classComment},则报错,避免形成环路 |
|
181 |
if (id == null) { // id 为空,说明新增,不需要考虑环路 |
|
182 |
return; |
|
183 |
} |
|
184 |
for (int i = 0; i < Short.MAX_VALUE; i++) { |
|
185 |
// 3.1 校验环路 |
|
186 |
${treeParentColumn.javaField} = parent${simpleClassName}.get${TreeParentJavaField}(); |
|
187 |
if (Objects.equals(id, ${treeParentColumn.javaField})) { |
|
188 |
throw exception(${simpleClassName_underlineCase.toUpperCase()}_PARENT_IS_CHILD); |
|
189 |
} |
|
190 |
// 3.2 继续递归下一级父${table.classComment} |
|
191 |
if (${treeParentColumn.javaField} == null || ${simpleClassName}DO.${treeParentColumn_javaField_underlineCase.toUpperCase()}_ROOT.equals(${treeParentColumn.javaField})) { |
|
192 |
break; |
|
193 |
} |
|
194 |
parent${simpleClassName} = ${classNameVar}Mapper.selectById(${treeParentColumn.javaField}); |
|
195 |
if (parent${simpleClassName} == null) { |
|
196 |
break; |
|
197 |
} |
|
198 |
} |
|
199 |
} |
|
200 |
|
|
201 |
private void validate${simpleClassName}${TreeNameJavaField}Unique(Long id, Long ${treeParentColumn.javaField}, String ${treeNameColumn.javaField}) { |
|
202 |
${simpleClassName}DO ${classNameVar} = ${classNameVar}Mapper.selectBy${TreeParentJavaField}And${TreeNameJavaField}(${treeParentColumn.javaField}, ${treeNameColumn.javaField}); |
|
203 |
if (${classNameVar} == null) { |
|
204 |
return; |
|
205 |
} |
|
206 |
// 如果 id 为空,说明不用比较是否为相同 id 的${table.classComment} |
|
207 |
if (id == null) { |
|
208 |
throw exception(${simpleClassName_underlineCase.toUpperCase()}_${treeNameColumn_javaField_underlineCase.toUpperCase()}_DUPLICATE); |
|
209 |
} |
|
210 |
if (!Objects.equals(${classNameVar}.getId(), id)) { |
|
211 |
throw exception(${simpleClassName_underlineCase.toUpperCase()}_${treeNameColumn_javaField_underlineCase.toUpperCase()}_DUPLICATE); |
|
212 |
} |
|
213 |
} |
|
214 |
|
|
215 |
#end |
|
216 |
@Override |
|
217 |
public ${table.className}DO get${simpleClassName}(${primaryColumn.javaType} id) { |
|
218 |
return ${classNameVar}Mapper.selectById(id); |
|
219 |
} |
|
220 |
|
|
221 |
## 特殊:树表专属逻辑(树不需要分页接口) |
|
222 |
#if ( $table.templateType != 2 ) |
|
223 |
@Override |
|
224 |
public PageResult<${table.className}DO> get${simpleClassName}Page(${sceneEnum.prefixClass}${table.className}PageReqVO pageReqVO) { |
|
225 |
return ${classNameVar}Mapper.selectPage(pageReqVO); |
|
226 |
} |
|
227 |
#else |
|
228 |
@Override |
|
229 |
public List<${table.className}DO> get${simpleClassName}List(${sceneEnum.prefixClass}${table.className}ListReqVO listReqVO) { |
|
230 |
return ${classNameVar}Mapper.selectList(listReqVO); |
|
231 |
} |
|
232 |
#end |
|
233 |
|
|
234 |
## 特殊:主子表专属逻辑 |
|
235 |
#foreach ($subTable in $subTables) |
|
236 |
#set ($index = $foreach.count - 1) |
|
237 |
#set ($subSimpleClassName = $subSimpleClassNames.get($index)) |
|
238 |
#set ($simpleClassNameUnderlineCase = $simpleClassNameUnderlineCases.get($index)) |
|
239 |
#set ($subPrimaryColumn = $subPrimaryColumns.get($index))##当前 primary 字段 |
|
240 |
#set ($subJoinColumn = $subJoinColumns.get($index))##当前 join 字段 |
|
241 |
#set ($SubJoinColumnName = $subJoinColumn.javaField.substring(0,1).toUpperCase() + ${subJoinColumn.javaField.substring(1)})##首字母大写 |
|
242 |
#set ($subClassNameVar = $subClassNameVars.get($index)) |
|
243 |
// ==================== 子表($subTable.classComment) ==================== |
|
244 |
|
|
245 |
## 情况一:MASTER_ERP 时,需要分查询页子表 |
|
246 |
#if ( $table.templateType == 11 ) |
|
247 |
@Override |
|
248 |
public PageResult<${subTable.className}DO> get${subSimpleClassName}Page(PageParam pageReqVO, ${subJoinColumn.javaType} ${subJoinColumn.javaField}) { |
|
249 |
return ${subClassNameVars.get($index)}Mapper.selectPage(pageReqVO, ${subJoinColumn.javaField}); |
|
250 |
} |
|
251 |
|
|
252 |
## 情况二:非 MASTER_ERP 时,需要列表查询子表 |
|
253 |
#else |
|
254 |
#if ( $subTable.subJoinMany ) |
|
255 |
@Override |
|
256 |
public List<${subTable.className}DO> get${subSimpleClassName}ListBy${SubJoinColumnName}(${subJoinColumn.javaType} ${subJoinColumn.javaField}) { |
|
257 |
return ${subClassNameVars.get($index)}Mapper.selectListBy${SubJoinColumnName}(${subJoinColumn.javaField}); |
|
258 |
} |
|
259 |
|
|
260 |
#else |
|
261 |
@Override |
|
262 |
public ${subTable.className}DO get${subSimpleClassName}By${SubJoinColumnName}(${subJoinColumn.javaType} ${subJoinColumn.javaField}) { |
|
263 |
return ${subClassNameVars.get($index)}Mapper.selectBy${SubJoinColumnName}(${subJoinColumn.javaField}); |
|
264 |
} |
|
265 |
|
|
266 |
#end |
|
267 |
#end |
|
268 |
## 情况一:MASTER_ERP 时,支持单个的新增、修改、删除操作 |
|
269 |
#if ( $table.templateType == 11 ) |
|
270 |
@Override |
|
271 |
public ${subPrimaryColumn.javaType} create${subSimpleClassName}(${subTable.className}DO ${subClassNameVar}) { |
|
272 |
## 特殊:一对一时,需要保证只有一条,不能重复插入 |
|
273 |
#if ( !$subTable.subJoinMany) |
|
274 |
// 校验是否已经存在 |
|
275 |
if (${subClassNameVars.get($index)}Mapper.selectBy${SubJoinColumnName}(${subClassNameVar}.get${SubJoinColumnName}()) != null) { |
|
276 |
throw exception(${simpleClassNameUnderlineCase.toUpperCase()}_EXISTS); |
|
277 |
} |
|
278 |
// 插入 |
|
279 |
#end |
|
280 |
${subClassNameVars.get($index)}Mapper.insert(${subClassNameVar}); |
|
281 |
return ${subClassNameVar}.getId(); |
|
282 |
} |
|
283 |
|
|
284 |
@Override |
|
285 |
public void update${subSimpleClassName}(${subTable.className}DO ${subClassNameVar}) { |
|
286 |
// 校验存在 |
|
287 |
validate${subSimpleClassName}Exists(${subClassNameVar}.getId()); |
|
288 |
// 更新 |
|
289 |
${subClassNameVars.get($index)}Mapper.updateById(${subClassNameVar}); |
|
290 |
} |
|
291 |
|
|
292 |
@Override |
|
293 |
public void delete${subSimpleClassName}(${subPrimaryColumn.javaType} id) { |
|
294 |
// 校验存在 |
|
295 |
validate${subSimpleClassName}Exists(id); |
|
296 |
// 删除 |
|
297 |
${subClassNameVars.get($index)}Mapper.deleteById(id); |
|
298 |
} |
|
299 |
|
|
300 |
@Override |
|
301 |
public ${subTable.className}DO get${subSimpleClassName}(${subPrimaryColumn.javaType} id) { |
|
302 |
return ${subClassNameVars.get($index)}Mapper.selectById(id); |
|
303 |
} |
|
304 |
|
|
305 |
private void validate${subSimpleClassName}Exists(${subPrimaryColumn.javaType} id) { |
|
306 |
if (${subClassNameVar}Mapper.selectById(id) == null) { |
|
307 |
throw exception(${simpleClassNameUnderlineCase.toUpperCase()}_NOT_EXISTS); |
|
308 |
} |
|
309 |
} |
|
310 |
|
|
311 |
## 情况二:非 MASTER_ERP 时,支持批量的新增、修改操作 |
|
312 |
#else |
|
313 |
#if ( $subTable.subJoinMany) |
|
314 |
private void create${subSimpleClassName}List(${primaryColumn.javaType} ${subJoinColumn.javaField}, List<${subTable.className}DO> list) { |
|
315 |
list.forEach(o -> o.set$SubJoinColumnName(${subJoinColumn.javaField})); |
|
316 |
${subClassNameVars.get($index)}Mapper.insertBatch(list); |
|
317 |
} |
|
318 |
|
|
319 |
private void update${subSimpleClassName}List(${primaryColumn.javaType} ${subJoinColumn.javaField}, List<${subTable.className}DO> list) { |
|
320 |
delete${subSimpleClassName}By${SubJoinColumnName}(${subJoinColumn.javaField}); |
|
321 |
list.forEach(o -> o.setId(null).setUpdater(null).setUpdateTime(null)); // 解决更新情况下:1)id 冲突;2)updateTime 不更新 |
|
322 |
create${subSimpleClassName}List(${subJoinColumn.javaField}, list); |
|
323 |
} |
|
324 |
|
|
325 |
#else |
|
326 |
private void create${subSimpleClassName}(${primaryColumn.javaType} ${subJoinColumn.javaField}, ${subTable.className}DO ${subClassNameVar}) { |
|
327 |
if (${subClassNameVar} == null) { |
|
328 |
return; |
|
329 |
} |
|
330 |
${subClassNameVar}.set$SubJoinColumnName(${subJoinColumn.javaField}); |
|
331 |
${subClassNameVars.get($index)}Mapper.insert(${subClassNameVar}); |
|
332 |
} |
|
333 |
|
|
334 |
private void update${subSimpleClassName}(${primaryColumn.javaType} ${subJoinColumn.javaField}, ${subTable.className}DO ${subClassNameVar}) { |
|
335 |
if (${subClassNameVar} == null) { |
|
336 |
return; |
|
337 |
} |
|
338 |
${subClassNameVar}.set$SubJoinColumnName(${subJoinColumn.javaField}); |
|
339 |
${subClassNameVar}.setUpdater(null).setUpdateTime(null); // 解决更新情况下:updateTime 不更新 |
|
340 |
${subClassNameVars.get($index)}Mapper.insertOrUpdate(${subClassNameVar}); |
|
341 |
} |
|
342 |
|
|
343 |
#end |
|
344 |
#end |
|
345 |
private void delete${subSimpleClassName}By${SubJoinColumnName}(${primaryColumn.javaType} ${subJoinColumn.javaField}) { |
|
346 |
${subClassNameVars.get($index)}Mapper.deleteBy${SubJoinColumnName}(${subJoinColumn.javaField}); |
|
347 |
} |
|
348 |
|
|
349 |
#end |
|
350 |
} |