shikeyin
2024-01-11 65da8373531677b1c37a98f53eaa30c892f35e5a
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
package com.ishop.model;
 
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.walker.infrastructure.utils.JsonUtils;
import com.walker.infrastructure.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
 
import java.io.Serializable;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.text.NumberFormat;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
 
/**
 * 采集商品时使用的商品信息解析对象。
 * @author 时克英
 * @date 2023-06-21
 */
public class MyRecord implements Serializable {
 
    private static final Logger logger = LoggerFactory.getLogger(MyRecord.class);
 
    private Map<String, Object> columns;
 
    void setColumnsMap(Map<String, Object> columns) {
        this.columns = columns;
    }
 
    /**
     * Return columns map.
     */
    public Map<String, Object> getColumns() {
        if (null == columns) {
            columns = new HashMap<String, Object>();
        }
        return columns;
    }
 
    /**
     * Set columns value with map.
     *
     * @param columns the columns map
     */
    public MyRecord setColumns(Map<String, Object> columns) {
        this.getColumns().putAll(columns);
        return this;
    }
 
    /**
     * Set columns value with MyRecord.
     *
     * @param record the MyRecord object
     */
    public MyRecord setColumns(MyRecord record) {
        this.getColumns().putAll(record.getColumns());
        return this;
    }
 
    /**
     * Set columns value with JSONObject.
     *
     * @param jsonObject the MyRecord object
     */
    public MyRecord setColumns(String jsonObject) {
//        Map<String, Object> columns = this.getColumns();
        try {
            JsonNode jsonNode = null;
            String key = null;
            ObjectNode objectNode = JsonUtils.jsonStringToObjectNode(jsonObject);
            for(Iterator<String> it = objectNode.fieldNames(); it.hasNext();){
                key = it.next();
                jsonNode = objectNode.get(key);
                columns.put(key, jsonNode.toString());
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
//        jsonObject.forEach(columns::put);
        return this;
    }
 
    /**
     * Set columns value with Model object.
     * @param t
     * @param <T>
     * @return
     */
    public <T> MyRecord setColumns(T t) {
        Map<String, Object> columns = this.getColumns();
 
        String[] fieldNames = getFiledName(t);
 
        for (int i = 0; i < fieldNames.length; i++) {
            String name = fieldNames[i];
            if (StringUtils.isNotEmpty(name) && "serialVersionUID".equals(name)) {
                continue ;
            }
            Object value = getFieldValueByName(name, t);
            if (null != value) {
//                if (getFiledType(name, t).equals(Date.class)) {
//                    value = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format((Date)value);
//                }
                columns.put(name, value);
            }
        }
        return this;
    }
 
    /**
     * Remove attribute of this myRecord.
     *
     * @param column the column name of the myRecord
     */
    public MyRecord remove(String column) {
        getColumns().remove(column);
        return this;
    }
 
    /**
     * Remove columns of this myRecord.
     *
     * @param columns the column names of the myRecord
     */
    public MyRecord remove(String... columns) {
        if (columns != null)
            for (String c : columns)
                this.getColumns().remove(c);
        return this;
    }
 
    /**
     * Remove columns if it is null.
     */
    public MyRecord removeNullValueColumns() {
        for (java.util.Iterator<Map.Entry<String, Object>> it = getColumns().entrySet().iterator(); it.hasNext(); ) {
            Map.Entry<String, Object> e = it.next();
            if (e.getValue() == null) {
                it.remove();
            }
        }
        return this;
    }
 
    /**
     * Keep columns of this record and remove other columns.
     *
     * @param columns the column names of the record
     */
    public MyRecord keep(String... columns) {
        if (columns != null && columns.length > 0) {
            Map<String, Object> newColumns = new HashMap<String, Object>(columns.length);    // getConfig().containerFactory.getColumnsMap();
            for (String c : columns)
                if (this.getColumns().containsKey(c))    // prevent put null value to the newColumns
                    newColumns.put(c, this.getColumns().get(c));
 
            this.getColumns().clear();
            this.getColumns().putAll(newColumns);
        } else
            this.getColumns().clear();
        return this;
    }
 
    /**
     * Keep column of this record and remove other columns.
     *
     * @param column the column names of the record
     */
    public MyRecord keep(String column) {
        if (getColumns().containsKey(column)) {    // prevent put null value to the newColumns
            Object keepIt = getColumns().get(column);
            getColumns().clear();
            getColumns().put(column, keepIt);
        } else
            getColumns().clear();
        return this;
    }
 
    /**
     * Remove all columns of this record.
     */
    public MyRecord clear() {
        getColumns().clear();
        return this;
    }
 
    /**
     * Set column to record.
     *
     * @param column the column name
     * @param value  the value of the column
     */
    public MyRecord set(String column, Object value) {
        getColumns().put(column, value);
        return this;
    }
 
    /**
     * Set column to record.
     *
     * @param column the column name
     * @param valueList  the value of the column
     */
    public MyRecord set(String column, List<MyRecord> valueList) {
        List<HashMap<String, Object>> value = new ArrayList<>();
        valueList.forEach(o -> {
            HashMap<String, Object> va = new HashMap<>(o.getColumns());
            value.add(va);
        });
        getColumns().put(column, value);
        return this;
    }
 
    /**
     * Get column of any mysql type
     */
    public <T> T get(String column) {
        return (T) getColumns().get(column);
    }
 
    /**
     * Get column of any mysql type. Returns defaultValue if null.
     */
    public <T> T get(String column, Object defaultValue) {
        Object result = getColumns().get(column);
        return (T) (result != null ? result : defaultValue);
    }
 
    public Object getObject(String column) {
        return getColumns().get(column);
    }
 
    public Object getObject(String column, Object defaultValue) {
        Object result = getColumns().get(column);
        return result != null ? result : defaultValue;
    }
 
    /**
     * Get column of mysql type: varchar, char, enum, set, text, tinytext, mediumtext, longtext
     */
    public String getStr(String column) {
        // return (String)getColumns().get(column);
        Object s = getColumns().get(column);
        return s != null ? s.toString() : null;
    }
 
    /**
     * Get column of mysql type: int, integer, tinyint(n) n > 1, smallint, mediumint
     */
    public Integer getInt(String column) {
        Number n = getNumber(column);
        return n != null ? n.intValue() : null;
    }
 
    /**
     * Get column of mysql type: bigint
     */
    public Long getLong(String column) {
        Number n = getNumber(column);
        return n != null ? n.longValue() : null;
    }
 
    /**
     * Get column of mysql type: unsigned bigint
     */
    public java.math.BigInteger getBigInteger(String column) {
        return (java.math.BigInteger) getColumns().get(column);
    }
 
    /**
     * Get column of mysql type: date, year
     */
    public java.util.Date getDate(String column) {
        return (java.util.Date) getColumns().get(column);
    }
 
    /**
     * Get column of mysql type: time
     */
    public java.sql.Time getTime(String column) {
        return (java.sql.Time) getColumns().get(column);
    }
 
    /**
     * Get column of mysql type: timestamp, datetime
     */
    public java.sql.Timestamp getTimestamp(String column) {
        return (java.sql.Timestamp) getColumns().get(column);
    }
 
    /**
     * Get column of mysql type: real, double
     */
    public Double getDouble(String column) {
        Number n = getNumber(column);
        return n != null ? n.doubleValue() : null;
    }
 
    /**
     * Get column of mysql type: float
     */
    public Float getFloat(String column) {
        Number n = getNumber(column);
        return n != null ? n.floatValue() : null;
    }
 
    public Short getShort(String column) {
        Number n = getNumber(column);
        return n != null ? n.shortValue() : null;
    }
 
    public Byte getByte(String column) {
        Number n = getNumber(column);
        return n != null ? n.byteValue() : null;
    }
 
    /**
     * Get column of mysql type: bit, tinyint(1)
     */
    public Boolean getBoolean(String column) {
        return (Boolean) getColumns().get(column);
    }
 
    /**
     * Get column of mysql type: decimal, numeric
     */
    public BigDecimal getBigDecimal(String column) {
        Object n = getColumns().get(column);
        if (n instanceof BigDecimal) {
            return (BigDecimal) n;
        } else if (n != null) {
            return new BigDecimal(n.toString());
        } else {
            return null;
        }
    }
 
    /**
     * Get column of mysql type: binary, varbinary, tinyblob, blob, mediumblob, longblob
     * I have not finished the test.
     */
    public byte[] getBytes(String column) {
        return (byte[]) getColumns().get(column);
    }
 
    /**
     * Get column of any type that extends from Number
     */
    public Number getNumber(String column) {
        if (getColumns().get(column) instanceof String) {
            try {
                return NumberFormat.getInstance().parse(getColumns().get(column).toString());
            } catch (ParseException e) {
                System.out.println("类型转换错误e = " + e.getMessage());
                e.printStackTrace();
            }
        }
        return (Number) getColumns().get(column);
    }
 
    public String toString() {
        if (columns == null) {
            return "{}";
        }
        StringBuilder sb = new StringBuilder();
        sb.append('{');
        boolean first = true;
        for (Map.Entry<String, Object> e : getColumns().entrySet()) {
            if (first) {
                first = false;
            } else {
                sb.append(", ");
            }
            Object value = e.getValue();
            if (value != null) {
                value = value.toString();
            }
            sb.append(e.getKey()).append(':').append(value);
        }
        sb.append('}');
        return sb.toString();
    }
 
    public boolean equals(Object o) {
        if (!(o instanceof MyRecord))
            return false;
        if (o == this)
            return true;
        return getColumns().equals(((MyRecord) o).getColumns());
    }
 
    public int hashCode() {
        return getColumns().hashCode();
    }
 
    /**
     * Return column names of this record.
     */
    public String[] getColumnNames() {
        Set<String> attrNameSet = getColumns().keySet();
        return attrNameSet.toArray(new String[attrNameSet.size()]);
    }
 
    /**
     * Return column values of this record.
     */
    public Object[] getColumnValues() {
        java.util.Collection<Object> attrValueCollection = getColumns().values();
        return attrValueCollection.toArray(new Object[attrValueCollection.size()]);
    }
 
    /**
     * Return json string of this record.
     */
    public String toJson() {
//        return JSON.toJSONString(getColumns());
        try {
            return JsonUtils.objectToJsonString(getColumns());
        } catch (Exception e) {
            throw new RuntimeException("Record转换json错误:" + e.getMessage(), e);
        }
    }
 
    /**
     * @param o
     * @return
     * @desc 获取属性名数组
     */
    private static String[] getFiledName(Object o) {
        Field[] fields = o.getClass().getDeclaredFields();
        String[] fieldNames = new String[fields.length];
        for (int i = 0; i < fields.length; i++) {
            fieldNames[i] = fields[i].getName();
        }
        return fieldNames;
    }
 
    /**
     * @param fieldName
     * @param o
     * @return
     * @desc 根据属性名获取属性值
     */
    private static Object getFieldValueByName(String fieldName, Object o) {
        try {
            String firstLetter = fieldName.substring(0, 1).toUpperCase();
            String getter = "get" + firstLetter + fieldName.substring(1);
            Method method = o.getClass().getMethod(getter);
            Object value = method.invoke(o);
            return value;
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("获取属性值失败!" + e, e);
        }
        return null;
    }
 
    /**
     * @param fieldName
     * @param o
     * @return
     * @desc 获取属性的数据类型
     */
    private static Object getFiledType(String fieldName, Object o) {
        Field[] fields = o.getClass().getDeclaredFields();
        for (Field field : fields) {
            if (Objects.equals(fieldName, field.getName())) {
                return field.getType();
            }
        }
        return null;
    }
}