许鹏程
2023-06-30 3bbfaa3d7d416afbd154576453c8ee9e7e2f8899
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
package com.product.kt.test.service;
 
import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.thread.ThreadUtil;
import com.product.common.lang.StringUtils;
import com.product.core.entity.DataTableEntity;
import com.product.core.entity.FieldSetEntity;
import com.product.core.spring.context.SpringMVCContextHolder;
import com.product.datasource.config.DataBaseType;
import com.product.datasource.dao.Dao;
import com.product.datasource.entity.DataBaseEntity;
import com.product.datasource.entity.UpdateFilterEntity;
import com.product.datasource.service.RedisService;
import redis.clients.jedis.Jedis;
 
import java.util.*;
import java.util.concurrent.*;
import java.util.stream.Collectors;
 
/**
 * @Author cheng
 * @Date 2022/12/21 17:57
 * @Desc
 */
public class InitTProductSn {
 
 
    private static String[] sourceUuid = {"10.13.1.36", "10.13.1.40", "10.13.1.41", "10.13.1.42"};
 
 
    private Dao dao;
 
    private DataBaseEntity dbe;
 
    private static RedisService redisService;
 
    private final int pageSize = 100000;
 
    private ExecutorService executorService = Executors.newFixedThreadPool(1);
 
    private Queue<DataTableEntity> queue = new LinkedBlockingQueue();
 
    private String ip;
 
    public void initDao(String ip) {
        DataBaseEntity dbe = new DataBaseEntity(DataBaseType.ORACLE.getValue());
        dbe.setUserName("CHKTADMIN");
        dbe.setPassWord("CHKTADMIN");
        dbe.setPort("1521");
        dbe.setServerName("ORCL");
        dbe.setIp(ip);
        this.dbe = dbe;
        this.dao = dbe.getDao();
    }
 
    public synchronized void getRedisBase() {
        if (redisService == null) {
            DataBaseEntity dbe = new DataBaseEntity(5);
            dbe.setIp("127.0.0.1");
            dbe.setPort("6379");
            dbe.setDbInstance("0");
            this.redisService = new RedisService(dbe);
        }
    }
 
    public static void main(String[] args) throws InterruptedException {
        ExecutorService executorService = Executors.newFixedThreadPool(sourceUuid.length);
        for (String ip : sourceUuid) {
            executorService.submit(() -> {
                InitTProductSn initTProductSn = new InitTProductSn();
                initTProductSn.main(ip);
            });
        }
        executorService.awaitTermination(Integer.MAX_VALUE, TimeUnit.DAYS);
    }
 
    private boolean selectFlag = false;
 
    public void main(String ip) {
        this.ip = ip;
        this.initDao(ip);
        this.getRedisBase();
        String maxValue = null;
        DataTableEntity data;
 
        executorService.submit(() -> {
            updateData();
        });
        this.executorService.shutdown();
        int i = 1;
        try {
            do {
                while (queue.size() >= 10) {
//                    print("已查询到10批次等待更新处理.....");
//                    print("更新线程状态:" + executorService.isTerminated());
                    ThreadUtil.sleep(3000);
                }
                print("开始查询 第 " + i + " 次,最大值为:" + maxValue);
                data = getData(maxValue);
                if (DataTableEntity.isEmpty(data)) {
                    break;
                }
                print("第 " + i + "次查询,总条数:" + data.getRows());
                queue.add(data);
                maxValue = data.getData().stream().max(Comparator.comparing(item -> item.getString("product_sn"))).get().getString("product_sn");
                i++;
            } while (!DataTableEntity.isEmpty(data));
        } catch (Exception e) {
            SpringMVCContextHolder.getSystemLogger().error(e);
            e.printStackTrace();
        }
        this.selectFlag = true;
        this.dao.closeConnection();
    }
 
    public void updateData() {
        Dao dao = null;
        Long time = System.currentTimeMillis();
        int updateCount = 1;
        while (!selectFlag) {
            try {
 
                DataTableEntity data = queue.poll();
                if (DataTableEntity.isEmpty(data)) {
//                    print("更新线程等待查询数据...");
                    ThreadUtil.sleep(3000);
                    continue;
                }
                if (dao == null || (System.currentTimeMillis() - time > 1000 * 60 * 10)) {
                    if (dao != null) {
                        dao.closeConnection();
                    } else {
                        dao = dbe.newDao();
                    }
                }
                int rows = data.getRows();
                List<List<FieldSetEntity>> split = ListUtil.split(data.getData(), 1000);
                for (List<FieldSetEntity> fieldSetEntities : split) {
                    data = new DataTableEntity();
 
                    data.setData(fieldSetEntities.stream().filter(item -> {
                        item.setValue("row_id", getRowId(item.getString("product_sn"), item.getInteger("row_id")));
                        item.setValue("pre_master_key", "");
                        return true;
                    }).collect(Collectors.toList()));
                    if (this.ip.equals(sourceUuid[0])) {
                        data.getMeta().setFields(new Object[]{"product_sn", "row_id", "pre_master_key"});
                    } else {
                        data.getMeta().setFields(new Object[]{"product_sn", "row_id", "update_date", "pre_master_key"});
                    }
                    data.getMeta().setTableName(new Object[]{"T_PM_PRODUCT_SN"});
                    dao.updateBatch(data, new UpdateFilterEntity(" PRODUCT_SN =? ", new String[]{"product_sn"}), true);
                }
                print("更新完成,更新条数:" + rows + ", 更新次数:" + updateCount++);
            } catch (Exception e) {
                e.printStackTrace();
                SpringMVCContextHolder.getSystemLogger().error(e);
            }
        }
 
        if (dao != null) {
            dao.closeConnection();
        }
        print("更新线程结束----------------------------------------------------------------------------------------------------------------------------------------------------------------");
    }
 
 
    private static int rowId = 19144409;
 
    private static boolean initFlag = false;
 
    private static synchronized void initRow() {
        if (!initFlag) {
            redisService.set("T_PM_PRODUCT_SN", rowId, false);
            initFlag = true;
        }
    }
 
    public static int getRowId(String uniqueValue, Integer row) {
        if (!initFlag) {
            initRow();
        }
        synchronized (uniqueValue.intern()) {
            Jedis jedis = redisService.getJedis();
            try {
                String o = jedis.get(uniqueValue);
                if (StringUtils.isEmpty(o)) {
                    row = Integer.valueOf(o);
                } else {
                    if (row == null || row.intValue() <= 0) {
                        row = jedis.incr("T_PM_PRODUCT_SN").intValue();
                    }
                    jedis.set(uniqueValue, row.toString());
                }
                return row;
            } catch (Exception e) {
                e.printStackTrace();
                SpringMVCContextHolder.getSystemLogger().error(e);
            } finally {
                jedis.close();
            }
            return -1;
        }
    }
 
//    public static int getRowId(String uniqueValue, Integer row) {
//        synchronized (uniqueValue.intern()) {
//            Num n = (Num) redisService.get(uniqueValue, false);
//            if (row != null && row > 0) {
//                if (n == null) {
//                    n = new Num();
//                    n.num = row;
//                    n.count = 1;
//                } else {
//                    int num = n.num;
//                    if (num != row.intValue()) {
//                    } else {
//                        row = num;
//                    }
//                    n.num++;
//                }
//                redisService.set(uniqueValue, n, false);
//                return row;
//            }
//            if (n != null) {
//                int num =n.num;
//                if (n.count >= 2) {
//                    map.remove(uniqueValue);
//                } else {
//                    jsonObject.put("count", jsonObject.getInt("count") + 1);
//                }
//                return num;
//            } else {
//                jsonObject = new JSONObject();
//                jsonObject.put("num", rowId);
//                jsonObject.put("count", 1);
//                map.put(uniqueValue, jsonObject);
//                return rowId++;
//            }
//        }
//    }
 
//    private String ThreadName = Thread.currentThread().getName();
 
    private void print(String msg) {
//        msg = "[" + ip + "] [" + DateUtils.getDateTime() + "]  [" + ThreadName + "] " + msg;
        SpringMVCContextHolder.getSystemLogger().info(msg);
    }
 
 
    public DataTableEntity getData(String maxValue) {
        StringBuilder sql = new StringBuilder();
        sql.append(" SELECT SN.ROW_ID,SN.PRODUCT_SN,NVL(TRACK.IN_STATION_TIME,TRACK_BACK.IN_STATION_TIME) UPDATE_DATE,'' PRE_MASTER_KEY ");
        sql.append(" FROM (SELECT *  ");
        sql.append(" FROM ( SELECT PRODUCT_SN,ROW_ID ");
        sql.append(" FROM T_PM_PRODUCT_SN ");
        sql.append(" ORDER BY   PRODUCT_SN ) SN ");
 
 
        sql.append(" WHERE ROWNUM<= ").append(this.pageSize);
        if (!StringUtils.isEmpty(maxValue)) {
            sql.append(" AND PRODUCT_SN> '").append(maxValue).append("'");
        }
        sql.append(" ) SN ");
        sql.append(" LEFT JOIN T_WIP_TRACKING TRACK ON SN.PRODUCT_SN=TRACK.SERIAL_NUMBER    ");
        sql.append(" LEFT JOIN T_WIP_TRACKING_BAK20220421  TRACK_BACK ON SN.PRODUCT_SN=TRACK_BACK.SERIAL_NUMBER    ");
        return dao.getList(sql.toString());
    }
 
}