dengzedong
4 天以前 730d1944e3a13c517c77df2b0712df05645a38dd
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
package com.iailab.module.bpm.service.definition;
 
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.Assert;
import com.iailab.framework.common.pojo.PageResult;
import com.iailab.framework.common.util.json.JsonUtils;
import com.iailab.framework.common.util.object.BeanUtils;
import com.iailab.module.bpm.controller.admin.definition.vo.form.BpmFormPageReqVO;
import com.iailab.module.bpm.controller.admin.definition.vo.form.BpmFormSaveReqVO;
import com.iailab.module.bpm.dal.dataobject.definition.BpmFormDO;
import com.iailab.module.bpm.dal.mysql.definition.BpmFormMapper;
import com.iailab.module.bpm.enums.ErrorCodeConstants;
import com.iailab.module.bpm.service.definition.dto.BpmFormFieldRespDTO;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
 
import javax.annotation.Resource;
import java.util.*;
 
import static com.iailab.framework.common.exception.util.ServiceExceptionUtil.exception;
 
/**
 * 动态表单 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.selectBatchIds(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 iailab:兼容 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());
        }
    }
 
}