PC
2024-03-31 608f20e0d5d8f95d9bbb917e95e2913682deb77d
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
package com.qianwen.core.coderule.builder;
 
import cn.hutool.core.convert.Convert;
import java.math.BigDecimal;
import java.text.ParseException;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.qianwen.core.coderule.constant.CodeRuleConstant;
import com.qianwen.core.coderule.constant.enums.ElementTypeEnum;
import com.qianwen.core.coderule.constant.enums.SourcePropertyEnum;
import com.qianwen.core.coderule.dto.DropDownDTO;
import com.qianwen.core.coderule.dto.RefObjectTypeInfoDTO;
import com.qianwen.core.coderule.handler.DictHandler;
import com.qianwen.core.coderule.handler.MasterlinkCodeRuleModelHandler;
import com.qianwen.core.coderule.handler.MasterlinkDictHandler;
import com.qianwen.core.coderule.handler.MasterlinkMetaObjectTypeFieldEnumHandler;
import com.qianwen.core.coderule.handler.MasterlinkObjectTypeHandler;
import com.qianwen.core.coderule.handler.ObjectTypeHandler;
import com.qianwen.core.coderule.model.CodeRuleContext;
import com.qianwen.core.coderule.model.CodeRuleEntryModel;
import com.qianwen.core.coderule.model.CodeRuleModel;
import com.qianwen.core.coderule.util.CodeRuleHelper;
import com.qianwen.core.sequence.builder.SequenceBuilderFactory;
import com.qianwen.core.sequence.sequence.Sequence;
import com.qianwen.core.tool.utils.ConvertUtil;
import com.qianwen.core.tool.utils.DateUtil;
import com.qianwen.core.tool.utils.Func;
import com.qianwen.core.tool.utils.StringUtil;
import org.springframework.jdbc.core.JdbcTemplate;
 
/* loaded from: blade-starter-coderule-9.3.0.0-SNAPSHOT.jar:org/springblade/core/coderule/builder/DefaultConcreateContextBuilder.class */
public class DefaultConcreateContextBuilder implements CodeRuleContextBuilder {
    private static final Logger log = LoggerFactory.getLogger(DefaultConcreateContextBuilder.class);
    private static String[] parsePatterns = {"yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm"};
    private JdbcTemplate jdbcTemplate;
    private CodeRuleContext context = new CodeRuleContext();
 
    public DefaultConcreateContextBuilder(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }
 
    @Override // com.qianwen.core.coderule.builder.CodeRuleContextBuilder
    public CodeRuleContextBuilder codeRuleModel(Long codeRuleId) {
        MasterlinkCodeRuleModelHandler codeRuleModelHandler = new MasterlinkCodeRuleModelHandler(this.jdbcTemplate);
        this.context.setCodeRuleModel(codeRuleModelHandler.getCodeRuleById(codeRuleId));
        return this;
    }
 
    @Override // com.qianwen.core.coderule.builder.CodeRuleContextBuilder
    public CodeRuleContextBuilder objectData(Object object) {
        this.context.setObjectData(CodeRuleHelper.getDeclaredFieldsInfoByAnnotation(object));
        return this;
    }
 
    @Override // com.qianwen.core.coderule.builder.CodeRuleContextBuilder
    public CodeRuleContextBuilder constant() {
        List<CodeRuleEntryModel> constants = execute(ElementTypeEnum.CONSTANT.getType());
        if (Func.isNotEmpty(constants)) {
            Map<Integer, String> map = new HashMap<>();
            for (CodeRuleEntryModel constant : constants) {
                map.put(constant.getSeq(), handleValueByOtherSetting(constant, constant.getElementValue()));
            }
            this.context.getEntryData().putAll(map);
        }
        return this;
    }
 
    @Override // com.qianwen.core.coderule.builder.CodeRuleContextBuilder
    public CodeRuleContextBuilder date() {
        List<CodeRuleEntryModel> dates = execute(ElementTypeEnum.DATE.getType());
        if (Func.isNotEmpty(dates)) {
            Map<Integer, String> map = new HashMap<>();
            for (CodeRuleEntryModel date : dates) {
                String format = Func.isNotEmpty(date.getFormat()) ? date.getFormat() : "yyyyMMdd";
                Object value = this.context.getObjectData().get(date.getElementSource());
                String result = "";
                if (value == null) {
                    value = Long.valueOf(new Date().getTime());
                }
                if (Func.isNotEmpty(value)) {
                    if (value instanceof Long) {
                        result = DateUtil.format((Date) ConvertUtil.convert(value, Date.class), format);
                    } else {
                        try {
                            result = DateUtil.format(DateUtils.parseDate(value.toString(), parsePatterns), format);
                        } catch (ParseException e) {
                            e.printStackTrace();
                        }
                    }
                }
                map.put(date.getSeq(), handleValueByOtherSetting(date, result));
            }
            this.context.getEntryData().putAll(map);
        }
        return this;
    }
 
    @Override // com.qianwen.core.coderule.builder.CodeRuleContextBuilder
    public CodeRuleContextBuilder number() {
        List<CodeRuleEntryModel> numbers = execute(ElementTypeEnum.NUMBER.getType());
        if (Func.isNotEmpty(numbers)) {
            Map<Integer, String> map = new HashMap<>();
            for (CodeRuleEntryModel number : numbers) {
                Object value = this.context.getObjectData().get(number.getElementSource());
                String result = "";
                if (Func.isNotEmpty(value)) {
                    result = CodeRuleHelper.getRealData((BigDecimal) ConvertUtil.convert(value, BigDecimal.class));
                }
                map.put(number.getSeq(), handleValueByOtherSetting(number, result));
            }
            this.context.getEntryData().putAll(map);
        }
        return this;
    }
 
    @Override // com.qianwen.core.coderule.builder.CodeRuleContextBuilder
    public CodeRuleContextBuilder dropdown(String objectId) {
        List<CodeRuleEntryModel> dropdowns = execute(ElementTypeEnum.DROPDOWN.getType());
        if (Func.isNotEmpty(dropdowns)) {
            Map<Integer, String> map = new HashMap<>();
            for (CodeRuleEntryModel dropdown : dropdowns) {
                Object value = this.context.getObjectData().get(dropdown.getElementSource());
                String result = value.toString();
                if (Func.isNotEmpty(value)) {
                    Boolean isCode = true;
                    if (Func.isNotEmpty(dropdown.getSourceProperty())) {
                        isCode = Boolean.valueOf(dropdown.getSourceProperty().equals(SourcePropertyEnum.CODE.getType()));
                    }
                    if (!isCode.booleanValue()) {
                        DictHandler dictHandler = new MasterlinkDictHandler(this.jdbcTemplate);
                        MasterlinkMetaObjectTypeFieldEnumHandler metaObjectTypeFieldEnumHandler = new MasterlinkMetaObjectTypeFieldEnumHandler(this.jdbcTemplate);
                        String dictCodeForField = metaObjectTypeFieldEnumHandler.getDictCodeForField(objectId, dropdown.getElementSource());
                        if (Func.isNotEmpty(dictCodeForField)) {
                            DropDownDTO dropDownDTO = dictHandler.getDictInfo(dictCodeForField, value.toString());
                            if (Func.isNotEmpty(dropDownDTO)) {
                                result = dropDownDTO.getDictValue();
                            }
                        }
                    }
                }
                map.put(dropdown.getSeq(), handleValueByOtherSetting(dropdown, result));
            }
            this.context.getEntryData().putAll(map);
        }
        return this;
    }
 
    @Override // com.qianwen.core.coderule.builder.CodeRuleContextBuilder
    public CodeRuleContextBuilder baseData(String objectId) {
        List<CodeRuleEntryModel> baseDatas = execute(ElementTypeEnum.BASE_DATA.getType());
        if (Func.isNotEmpty(baseDatas)) {
            ObjectTypeHandler objectTypeHandler = new MasterlinkObjectTypeHandler(this.jdbcTemplate);
            Map<Integer, String> map = new HashMap<>();
            for (CodeRuleEntryModel baseData : baseDatas) {
                Object value = this.context.getObjectData().get(baseData.getElementSource());
                String result = "";
                if (Func.isNotEmpty(value)) {
                    RefObjectTypeInfoDTO refObjectTypeInfo = objectTypeHandler.getRefObjectTypeInfo(objectId, baseData.getElementSource(), value.toString());
                    Boolean isCode = Boolean.valueOf(baseData.getSourceProperty().equals(SourcePropertyEnum.CODE.getType()));
                    if (Func.isNotEmpty(refObjectTypeInfo)) {
                        result = isCode.booleanValue() ? refObjectTypeInfo.getCode() : refObjectTypeInfo.getName();
                    }
                }
                map.put(baseData.getSeq(), handleValueByOtherSetting(baseData, result));
            }
            this.context.getEntryData().putAll(map);
        }
        return this;
    }
 
    @Override // com.qianwen.core.coderule.builder.CodeRuleContextBuilder
    public CodeRuleContextBuilder baseDataProperty() {
        List<CodeRuleEntryModel> baseDataProperties = execute(ElementTypeEnum.BASE_DATA_PROPERTY.getType());
        if (Func.isNotEmpty(baseDataProperties)) {
            Map<Integer, String> map = new HashMap<>();
            for (CodeRuleEntryModel baseDataProperty : baseDataProperties) {
                Object value = this.context.getObjectData().getOrDefault(baseDataProperty.getElementSource(), "");
                map.put(baseDataProperty.getSeq(), handleValueByOtherSetting(baseDataProperty, checkCops(value.toString(), baseDataProperty.getFormat())));
            }
            this.context.getEntryData().putAll(map);
        }
        return this;
    }
 
    @Override // com.qianwen.core.coderule.builder.CodeRuleContextBuilder
    public CodeRuleContextBuilder sequence(String objectId) {
        List<CodeRuleEntryModel> sequences = execute(ElementTypeEnum.SEQUENCE.getType());
        if (Func.isEmpty(sequences)) {
            return this;
        }
        CodeRuleEntryModel sequenceEntry = sequences.iterator().next();
        String bizName = objectId.concat(getCodeDependOn());
        Integer start = sequenceEntry.getSeed();
        Integer step = sequenceEntry.getIncrementStep();
        if (Func.isNotEmpty(start)) {
            start = Integer.valueOf(start.intValue() - 1);
        }
        Sequence sequence = SequenceBuilderFactory.getSeqBuilder(bizName, step, start);
        Map<Integer, String> map = new HashMap<>();
        map.put(sequenceEntry.getSeq(), handleValueByOtherSetting(sequenceEntry, Convert.toStr(Long.valueOf(sequence.nextValue()))));
        this.context.getEntryData().putAll(map);
        return this;
    }
 
    @Override // com.qianwen.core.coderule.builder.CodeRuleContextBuilder
    public CodeRuleContext createCodeRuleContext() {
        return this.context;
    }
 
    private List<CodeRuleEntryModel> execute(Integer elementType) {
        List<CodeRuleEntryModel> result = null;
        CodeRuleModel codeRuleModel = this.context.getCodeRuleModel();
        if (Func.isNotEmpty(codeRuleModel) && Func.isNotEmpty(codeRuleModel.getCodeRuleEntryModelList())) {
            result = (List) codeRuleModel.getCodeRuleEntryModelList().stream().filter(x -> {
                return x.getElementType().equals(elementType);
            }).collect(Collectors.toList());
        }
        return result;
    }
 
    private String getCodeDependOn() {
        StringBuilder codeDependOn = new StringBuilder();
        CodeRuleModel codeRuleModel = this.context.getCodeRuleModel();
        codeDependOn.append(codeRuleModel.getId());
        if (Func.isNotEmpty(codeRuleModel) && Func.isNotEmpty(codeRuleModel.getCodeRuleEntryModelList())) {
            List<CodeRuleEntryModel> collect = (List) codeRuleModel.getCodeRuleEntryModelList().stream().filter(x -> {
                return x.getCodeOnlyBy().equals(true);
            }).filter(x2 -> {
                return !x2.getElementType().equals(ElementTypeEnum.SEQUENCE.getType());
            }).sorted(Comparator.comparing((v0) -> {
                return v0.getSeq();
            })).collect(Collectors.toList());
            if (Func.isNotEmpty(collect)) {
                for (CodeRuleEntryModel codeRuleEntryModel : collect) {
                    String s = this.context.getEntryData().get(codeRuleEntryModel.getSeq());
                    if (Func.isNotEmpty(s)) {
                        codeDependOn.append(s);
                    }
                }
            }
        }
        return codeDependOn.toString();
    }
 
    private String handleValueByOtherSetting(CodeRuleEntryModel codeRuleEntryModel, String value) {
        String result = value;
        if (Func.isEmpty(value)) {
            String reChar = codeRuleEntryModel.getReChar();
            if (Func.isNotEmpty(reChar)) {
                result = reChar;
            }
        }
        Integer length = codeRuleEntryModel.getLength();
        if (!length.equals(0)) {
            if (result.length() >= length.intValue()) {
                result = codeRuleEntryModel.getCutStyle().booleanValue() ? StringUtil.sub(result, 0, length.intValue()) : result.substring(result.length() - length.intValue());
            } else {
                result = codeRuleEntryModel.getAddStyle().booleanValue() ? StringUtils.rightPad(result, length.intValue(), codeRuleEntryModel.getAddChar()) : StringUtils.leftPad(result, length.intValue(), codeRuleEntryModel.getAddChar());
            }
        }
        if (Func.isNotEmpty(codeRuleEntryModel.getSeparatorChar())) {
            result = result + codeRuleEntryModel.getSeparatorChar();
        }
        return result;
    }
 
    private String checkCops(String value, String format) {
        if (Func.isNotEmpty(format)) {
            if (format.equals(CodeRuleConstant.CAPS_TYPE_CODE_U)) {
                value = value.toUpperCase();
            } else {
                value = value.toLowerCase();
            }
        }
        return value;
    }
}