xiaoyi
7 天以前 481d8165f26fc7e37f0e87f6444ac0340bb563a3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package cn.iocoder.yudao.module.bpm.service.definition;
 
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import cn.iocoder.yudao.framework.common.exception.ServiceException;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.json.JsonUtils;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.bpm.controller.admin.definition.vo.form.BpmFormImportExcelVO;
import cn.iocoder.yudao.module.bpm.controller.admin.definition.vo.form.BpmFormImportReqVO;
import cn.iocoder.yudao.module.bpm.controller.admin.definition.vo.form.BpmFormImportRespVO;
import cn.iocoder.yudao.module.bpm.controller.admin.definition.vo.form.BpmFormPageReqVO;
import cn.iocoder.yudao.module.bpm.controller.admin.definition.vo.form.BpmFormSaveReqVO;
import cn.iocoder.yudao.module.bpm.dal.dataobject.definition.BpmFormDO;
import cn.iocoder.yudao.module.bpm.dal.mysql.definition.BpmFormMapper;
import cn.iocoder.yudao.module.bpm.enums.ErrorCodeConstants;
import cn.iocoder.yudao.module.bpm.service.definition.dto.BpmFormFieldRespDTO;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
 
import java.util.*;
 
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.filterList;
 
/**
 * 动态表单 Service 实现类
 *
 * @author 风里雾里
 */
@Service
@Validated
public class BpmFormServiceImpl implements BpmFormService {
 
    @Resource
    private BpmFormMapper formMapper;
 
    @Override
    public Long createForm(BpmFormSaveReqVO createReqVO) {
        this.validateFields(createReqVO.getFields());
        // 插入
        BpmFormDO form = BeanUtils.toBean(createReqVO, BpmFormDO.class);
        formMapper.insert(form);
        // 返回
        return form.getId();
    }
 
    @Override
    public void updateForm(BpmFormSaveReqVO updateReqVO) {
        validateFields(updateReqVO.getFields());
        // 校验存在
        validateFormExists(updateReqVO.getId());
        // 更新
        BpmFormDO updateObj = BeanUtils.toBean(updateReqVO, BpmFormDO.class);
        formMapper.updateById(updateObj);
    }
 
    @Override
    public void deleteForm(Long id) {
        // 校验存在
        this.validateFormExists(id);
        // 删除
        formMapper.deleteById(id);
    }
 
    private void validateFormExists(Long id) {
        if (formMapper.selectById(id) == null) {
            throw exception(ErrorCodeConstants.FORM_NOT_EXISTS);
        }
    }
 
    @Override
    public BpmFormDO getForm(Long id) {
        return formMapper.selectById(id);
    }
 
    @Override
    public List<BpmFormDO> getFormList() {
        return formMapper.selectList();
    }
 
    @Override
    public List<BpmFormDO> getFormList(Collection<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return Collections.emptyList();
        }
        return formMapper.selectByIds(ids);
    }
 
    @Override
    public PageResult<BpmFormDO> getFormPage(BpmFormPageReqVO pageReqVO) {
        return formMapper.selectPage(pageReqVO);
    }
 
    /**
     * 校验 Field,避免 field 重复
     *
     * @param fields field 数组
     */
    private void validateFields(List<String> fields) {
        if (true) { // TODO 芋艿:兼容 Vue3 工作流:因为采用了新的表单设计器,所以暂时不校验
            return;
        }
        Map<String, String> fieldMap = new HashMap<>(); // key 是 vModel,value 是 label
        for (String field : fields) {
            BpmFormFieldRespDTO fieldDTO = JsonUtils.parseObject(field, BpmFormFieldRespDTO.class);
            Assert.notNull(fieldDTO);
            String oldLabel = fieldMap.put(fieldDTO.getVModel(), fieldDTO.getLabel());
            // 如果不存在,则直接返回
            if (oldLabel == null) {
                continue;
            }
            // 如果存在,则报错
            throw exception(ErrorCodeConstants.FORM_FIELD_REPEAT, oldLabel, fieldDTO.getLabel(), fieldDTO.getVModel());
        }
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public BpmFormImportRespVO importFormList(List<BpmFormImportExcelVO> importForms, BpmFormImportReqVO importReqVO) {
        // 1. 校验非空
        importForms = filterList(importForms, item -> StrUtil.isNotBlank(item.getName()));
        if (CollUtil.isEmpty(importForms)) {
            throw exception(ErrorCodeConstants.FORM_IMPORT_LIST_IS_EMPTY);
        }
        // 2. 逐条处理
        BpmFormImportRespVO respVO = BpmFormImportRespVO.builder()
                .createFormNames(new ArrayList<>())
                .updateFormNames(new ArrayList<>())
                .failureFormNames(new LinkedHashMap<>())
                .build();
        importForms.forEach(importForm -> {
            try {
                // 2.1 按名字匹配已存在记录
                BpmFormDO existForm = formMapper.selectOne(BpmFormDO::getName, importForm.getName());
                if (existForm == null) {
                    BpmFormDO form = BeanUtils.toBean(importForm, BpmFormDO.class);
                    form.setFields(parseFields(importForm.getFieldsJson()));
                    formMapper.insert(form);
                    respVO.getCreateFormNames().add(importForm.getName());
                    return;
                }
                // 2.2 已存在,判断是否允许更新
                if (!importReqVO.getUpdateSupport()) {
                    respVO.getFailureFormNames().put(importForm.getName(),
                            StrUtil.format(ErrorCodeConstants.FORM_IMPORT_EXISTS.getMsg(), importForm.getName()));
                    return;
                }
                BpmFormDO updateForm = BeanUtils.toBean(importForm, BpmFormDO.class)
                        .setId(existForm.getId());
                updateForm.setFields(parseFields(importForm.getFieldsJson()));
                formMapper.updateById(updateForm);
                respVO.getUpdateFormNames().add(importForm.getName());
            } catch (ServiceException ex) {
                respVO.getFailureFormNames().put(importForm.getName(), ex.getMessage());
            }
        });
        return respVO;
    }
 
    /**
     * 解析表单项 JSON 数组字符串
     *
     * @param fieldsJson 表单项 JSON 数组字符串
     * @return 表单项数组
     */
    private List<String> parseFields(String fieldsJson) {
        if (StrUtil.isBlank(fieldsJson)) {
            return Collections.emptyList();
        }
        return JsonUtils.parseArray(fieldsJson, String.class);
    }
 
}