刘光辉
4 小时以前 06f314545ac3514d7ce28939920866ae8188d31c
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
package jnpf.elnService.template.impl;
 
import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import jnpf.elnEntity.template.ElnTemplateRepeatGroupEntity;
import jnpf.elnEntity.template.ElnTemplateVos;
import jnpf.elnMapper.ElnTemplateRepeatGroupMapper;
import jnpf.elnService.template.ElnTemplateRepeatGroupService;
import jnpf.exception.DataException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
 
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.Supplier;
 
@Service
public class ElnTemplateRepeatGroupServiceImpl implements ElnTemplateRepeatGroupService {
 
    public static final int DEFAULT_MIN_ROWS = 0;
    public static final int DEFAULT_MAX_ROWS = 100;
    public static final int HARD_MAX_ROWS = 1000;
 
    private final Store store;
    private final Supplier<String> idSupplier;
 
    @Autowired
    public ElnTemplateRepeatGroupServiceImpl(ElnTemplateRepeatGroupMapper mapper) {
        this(new MyBatisStore(mapper), IdUtil::getSnowflakeNextIdStr);
    }
 
    public ElnTemplateRepeatGroupServiceImpl(Store store, Supplier<String> idSupplier) {
        this.store = store;
        this.idSupplier = idSupplier;
    }
 
    @Override
    public List<ElnTemplateVos.RepeatGroup> list(String tenantId, String templateId) {
        requireIdentity(tenantId, templateId);
        List<ElnTemplateVos.RepeatGroup> result = new ArrayList<>();
        for (ElnTemplateRepeatGroupEntity entity : store.list(tenantId.trim(), templateId.trim())) {
            result.add(toVo(entity));
        }
        result.sort(Comparator.comparing(ElnTemplateVos.RepeatGroup::getGroupCode));
        return result;
    }
 
    @Override
    @Transactional
    public ElnTemplateVos.RepeatGroup ensureConfigured(String tenantId, String templateId,
            String groupCode, Integer minRows, Integer maxRows, String stepId,
            String operatorId) {
        requireIdentity(tenantId, templateId);
        String normalizedCode = requireGroupCode(groupCode);
        String normalizedStepId = requireStepId(stepId);
        ElnTemplateRepeatGroupEntity existing = store.find(
                tenantId.trim(), templateId.trim(), normalizedCode);
        if (existing != null) {
            validateRange(existing.getMinRows(), existing.getMaxRows());
            if ((minRows != null && !Objects.equals(minRows, existing.getMinRows()))
                    || (maxRows != null && !Objects.equals(maxRows, existing.getMaxRows()))
                    || !normalizedStepId.equals(existing.getStepId())) {
                throw new DataException("重复组配置与已有配置不一致");
            }
            return toVo(existing);
        }
        int normalizedMin = minRows == null ? DEFAULT_MIN_ROWS : minRows;
        int normalizedMax = maxRows == null ? DEFAULT_MAX_ROWS : maxRows;
        validateRange(normalizedMin, normalizedMax);
        ElnTemplateRepeatGroupEntity created = new ElnTemplateRepeatGroupEntity();
        created.setId(requireId(idSupplier.get()));
        created.setFTenantId(tenantId.trim());
        created.setMubanId(templateId.trim());
        created.setRepeatGroupCode(normalizedCode);
        created.setMinRows(normalizedMin);
        created.setMaxRows(normalizedMax);
        created.setStepId(normalizedStepId);
        created.setFVersion(0);
        created.setFCreatorUserId(trim(operatorId));
        created.setFCreatorTime(new Date());
        created.setFDeleteMark(0);
        store.insert(created);
        return toVo(created);
    }
 
    @Override
    @Transactional
    public void afterBindingsActivated(String tenantId, String sourceTemplateId,
            String targetTemplateId, Map<String, String> bindingIdMap) {
        copyForVersion(tenantId, sourceTemplateId, targetTemplateId);
    }
 
    @Override
    @Transactional
    public void copyForVersion(String tenantId, String sourceTemplateId,
            String targetTemplateId) {
        requireIdentity(tenantId, sourceTemplateId);
        requireIdentity(tenantId, targetTemplateId);
        for (ElnTemplateRepeatGroupEntity source
                : store.list(tenantId.trim(), sourceTemplateId.trim())) {
            ElnTemplateVos.RepeatGroup validated = toVo(source);
            ElnTemplateRepeatGroupEntity existing = store.find(tenantId.trim(),
                    targetTemplateId.trim(), validated.getGroupCode());
            if (existing != null) {
                ElnTemplateVos.RepeatGroup current = toVo(existing);
                if (!Objects.equals(validated.getMinRows(), current.getMinRows())
                        || !Objects.equals(validated.getMaxRows(), current.getMaxRows())) {
                    throw new DataException("目标模板重复组配置与来源版本不一致");
                }
                continue;
            }
            ElnTemplateRepeatGroupEntity copied = new ElnTemplateRepeatGroupEntity();
            copied.setId(requireId(idSupplier.get()));
            copied.setFTenantId(tenantId.trim());
            copied.setMubanId(targetTemplateId.trim());
            copied.setRepeatGroupCode(validated.getGroupCode());
            copied.setMinRows(validated.getMinRows());
            copied.setMaxRows(validated.getMaxRows());
            copied.setFVersion(0);
            copied.setFCreatorTime(new Date());
            copied.setFDeleteMark(0);
            store.insert(copied);
        }
    }
 
    private static ElnTemplateVos.RepeatGroup toVo(ElnTemplateRepeatGroupEntity entity) {
        if (entity == null) {
            throw new DataException("重复组配置不存在");
        }
        String code = requireGroupCode(entity.getRepeatGroupCode());
        validateRange(entity.getMinRows(), entity.getMaxRows());
        ElnTemplateVos.RepeatGroup result = new ElnTemplateVos.RepeatGroup();
        result.setGroupCode(code);
        result.setMinRows(entity.getMinRows());
        result.setMaxRows(entity.getMaxRows());
        result.setStepId(entity.getStepId());
        return result;
    }
 
    private static void validateRange(Integer minRows, Integer maxRows) {
        if (minRows == null || maxRows == null || minRows < 0
                || maxRows < minRows || maxRows > HARD_MAX_ROWS) {
            throw new DataException("重复组行数约束无效");
        }
    }
 
    private static String requireGroupCode(String value) {
        String code = trim(value);
        if (code == null || !code.matches("[A-Za-z0-9_-]{1,80}")) {
            throw new DataException("重复组编码无效");
        }
        return code;
    }
 
    private static void requireIdentity(String tenantId, String templateId) {
        if (!StringUtils.hasText(tenantId) || !StringUtils.hasText(templateId)) {
            throw new DataException("租户或模板标识无效");
        }
    }
 
    private static String requireId(String value) {
        if (!StringUtils.hasText(value)) {
            throw new DataException("重复组配置标识生成失败");
        }
        return value.trim();
    }
 
    private static String requireStepId(String value) {
        if (!StringUtils.hasText(value)) {
            throw new DataException("重复组所属步骤无效");
        }
        return value.trim();
    }
 
    private static String trim(String value) {
        return StringUtils.hasText(value) ? value.trim() : null;
    }
 
    public interface Store {
        ElnTemplateRepeatGroupEntity find(String tenantId, String templateId, String groupCode);
        List<ElnTemplateRepeatGroupEntity> list(String tenantId, String templateId);
        void insert(ElnTemplateRepeatGroupEntity entity);
    }
 
    private static final class MyBatisStore implements Store {
        private final ElnTemplateRepeatGroupMapper mapper;
 
        private MyBatisStore(ElnTemplateRepeatGroupMapper mapper) {
            this.mapper = mapper;
        }
 
        @Override
        public ElnTemplateRepeatGroupEntity find(String tenantId, String templateId,
                String groupCode) {
            return mapper.selectOne(Wrappers.<ElnTemplateRepeatGroupEntity>lambdaQuery()
                    .eq(ElnTemplateRepeatGroupEntity::getFTenantId, tenantId)
                    .eq(ElnTemplateRepeatGroupEntity::getMubanId, templateId)
                    .eq(ElnTemplateRepeatGroupEntity::getRepeatGroupCode, groupCode)
                    .and(q -> q.isNull(ElnTemplateRepeatGroupEntity::getFDeleteMark)
                            .or().eq(ElnTemplateRepeatGroupEntity::getFDeleteMark, 0)));
        }
 
        @Override
        public List<ElnTemplateRepeatGroupEntity> list(String tenantId, String templateId) {
            return mapper.selectList(Wrappers.<ElnTemplateRepeatGroupEntity>lambdaQuery()
                    .eq(ElnTemplateRepeatGroupEntity::getFTenantId, tenantId)
                    .eq(ElnTemplateRepeatGroupEntity::getMubanId, templateId)
                    .and(q -> q.isNull(ElnTemplateRepeatGroupEntity::getFDeleteMark)
                            .or().eq(ElnTemplateRepeatGroupEntity::getFDeleteMark, 0))
                    .orderByAsc(ElnTemplateRepeatGroupEntity::getRepeatGroupCode));
        }
 
        @Override
        public void insert(ElnTemplateRepeatGroupEntity entity) {
            mapper.insert(entity);
        }
    }
}