提交 | 用户 | 时间
|
e7c126
|
1 |
package com.iailab.module.bpm.service.definition; |
H |
2 |
|
|
3 |
import cn.hutool.core.collection.CollUtil; |
|
4 |
import cn.hutool.core.lang.Assert; |
|
5 |
import com.iailab.framework.common.pojo.PageResult; |
|
6 |
import com.iailab.framework.common.util.json.JsonUtils; |
|
7 |
import com.iailab.framework.common.util.object.BeanUtils; |
|
8 |
import com.iailab.module.bpm.controller.admin.definition.vo.form.BpmFormPageReqVO; |
|
9 |
import com.iailab.module.bpm.controller.admin.definition.vo.form.BpmFormSaveReqVO; |
|
10 |
import com.iailab.module.bpm.dal.dataobject.definition.BpmFormDO; |
|
11 |
import com.iailab.module.bpm.dal.mysql.definition.BpmFormMapper; |
|
12 |
import com.iailab.module.bpm.enums.ErrorCodeConstants; |
|
13 |
import com.iailab.module.bpm.service.definition.dto.BpmFormFieldRespDTO; |
|
14 |
import org.springframework.stereotype.Service; |
|
15 |
import org.springframework.validation.annotation.Validated; |
|
16 |
|
|
17 |
import javax.annotation.Resource; |
|
18 |
import java.util.*; |
|
19 |
|
|
20 |
import static com.iailab.framework.common.exception.util.ServiceExceptionUtil.exception; |
|
21 |
|
|
22 |
/** |
|
23 |
* 动态表单 Service 实现类 |
|
24 |
* |
|
25 |
* @author 风里雾里 |
|
26 |
*/ |
|
27 |
@Service |
|
28 |
@Validated |
|
29 |
public class BpmFormServiceImpl implements BpmFormService { |
|
30 |
|
|
31 |
@Resource |
|
32 |
private BpmFormMapper formMapper; |
|
33 |
|
|
34 |
@Override |
|
35 |
public Long createForm(BpmFormSaveReqVO createReqVO) { |
|
36 |
this.validateFields(createReqVO.getFields()); |
|
37 |
// 插入 |
|
38 |
BpmFormDO form = BeanUtils.toBean(createReqVO, BpmFormDO.class); |
|
39 |
formMapper.insert(form); |
|
40 |
// 返回 |
|
41 |
return form.getId(); |
|
42 |
} |
|
43 |
|
|
44 |
@Override |
|
45 |
public void updateForm(BpmFormSaveReqVO updateReqVO) { |
|
46 |
validateFields(updateReqVO.getFields()); |
|
47 |
// 校验存在 |
|
48 |
validateFormExists(updateReqVO.getId()); |
|
49 |
// 更新 |
|
50 |
BpmFormDO updateObj = BeanUtils.toBean(updateReqVO, BpmFormDO.class); |
|
51 |
formMapper.updateById(updateObj); |
|
52 |
} |
|
53 |
|
|
54 |
@Override |
|
55 |
public void deleteForm(Long id) { |
|
56 |
// 校验存在 |
|
57 |
this.validateFormExists(id); |
|
58 |
// 删除 |
|
59 |
formMapper.deleteById(id); |
|
60 |
} |
|
61 |
|
|
62 |
private void validateFormExists(Long id) { |
|
63 |
if (formMapper.selectById(id) == null) { |
|
64 |
throw exception(ErrorCodeConstants.FORM_NOT_EXISTS); |
|
65 |
} |
|
66 |
} |
|
67 |
|
|
68 |
@Override |
|
69 |
public BpmFormDO getForm(Long id) { |
|
70 |
return formMapper.selectById(id); |
|
71 |
} |
|
72 |
|
|
73 |
@Override |
|
74 |
public List<BpmFormDO> getFormList() { |
|
75 |
return formMapper.selectList(); |
|
76 |
} |
|
77 |
|
|
78 |
@Override |
|
79 |
public List<BpmFormDO> getFormList(Collection<Long> ids) { |
|
80 |
if (CollUtil.isEmpty(ids)) { |
|
81 |
return Collections.emptyList(); |
|
82 |
} |
|
83 |
return formMapper.selectBatchIds(ids); |
|
84 |
} |
|
85 |
|
|
86 |
@Override |
|
87 |
public PageResult<BpmFormDO> getFormPage(BpmFormPageReqVO pageReqVO) { |
|
88 |
return formMapper.selectPage(pageReqVO); |
|
89 |
} |
|
90 |
|
|
91 |
/** |
|
92 |
* 校验 Field,避免 field 重复 |
|
93 |
* |
|
94 |
* @param fields field 数组 |
|
95 |
*/ |
|
96 |
private void validateFields(List<String> fields) { |
|
97 |
if (true) { // TODO iailab:兼容 Vue3 工作流:因为采用了新的表单设计器,所以暂时不校验 |
|
98 |
return; |
|
99 |
} |
|
100 |
Map<String, String> fieldMap = new HashMap<>(); // key 是 vModel,value 是 label |
|
101 |
for (String field : fields) { |
|
102 |
BpmFormFieldRespDTO fieldDTO = JsonUtils.parseObject(field, BpmFormFieldRespDTO.class); |
|
103 |
Assert.notNull(fieldDTO); |
|
104 |
String oldLabel = fieldMap.put(fieldDTO.getVModel(), fieldDTO.getLabel()); |
|
105 |
// 如果不存在,则直接返回 |
|
106 |
if (oldLabel == null) { |
|
107 |
continue; |
|
108 |
} |
|
109 |
// 如果存在,则报错 |
|
110 |
throw exception(ErrorCodeConstants.FORM_FIELD_REPEAT, oldLabel, fieldDTO.getLabel(), fieldDTO.getVModel()); |
|
111 |
} |
|
112 |
} |
|
113 |
|
|
114 |
} |