Administrator
2018-10-31 cf2ec236f73f24ab089a25601dadfd5dbeabc3e1
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
package com.yeshi.fanli.util;
 
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Properties;
 
import javax.annotation.Resource;
 
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.core.task.TaskExecutor;
import org.springframework.stereotype.Component;
 
import com.yeshi.fanli.entity.bus.user.AlipayTransferResultInfo;
import com.yeshi.fanli.entity.bus.user.HongBao;
import com.yeshi.fanli.entity.push.PushQueueRecord;
import com.yeshi.fanli.entity.taobao.TaoBaoOrder;
import com.yeshi.fanli.entity.taobao.TaoBaoWeiQuanOrder;
import com.yeshi.fanli.exception.TaoBaoWeiQuanException;
import com.yeshi.fanli.log.LogHelper;
import com.yeshi.fanli.service.inter.lable.BoutiqueAutoRuleService;
import com.yeshi.fanli.service.inter.lable.LabelService;
import com.yeshi.fanli.service.inter.lable.QualityFactoryService;
import com.yeshi.fanli.service.inter.order.OrderProcessService;
import com.yeshi.fanli.service.inter.push.IOSPushService;
import com.yeshi.fanli.service.inter.user.ExtractService;
import com.yeshi.fanli.service.inter.user.UserInfoService;
import org.yeshi.utils.tencentcloud.COSManager;
import org.yeshi.utils.tencentcloud.entity.COSInitParams;
 
/**
 * 系统初始化
 * 
 * @author Administrator
 *
 */
@Component
public class SpringContext implements ApplicationListener<ContextRefreshedEvent> {
 
    @Resource
    private UserInfoService userInfoService;
 
    @Resource
    private BoutiqueAutoRuleService boutiqueAutoRuleService;
 
    @Resource
    private ExtractService extractService;
 
    @Resource
    private IOSPushService iosPushService;
 
    @Resource
    private LabelService labelService;
 
    @Resource(name = "taskExecutor")
    private TaskExecutor executor;
 
    @Resource
    private OrderProcessService orderProcessService;
 
    @Resource
    private QualityFactoryService qualityFactoryService;
 
    private static boolean isInited = false;
 
    public void onApplicationEvent(ContextRefreshedEvent arg0) {
        if (arg0.getApplicationContext().getParent() != null) {
            System.out.println(System.currentTimeMillis());
            onApplication(arg0);
        }
    }
 
    private synchronized void onApplication(ContextRefreshedEvent context) {
        if (!isInited) {
            isInited = true;
            System.out.println("系统初始化完成");
            init(context);
        }
    }
 
    private void init(ContextRefreshedEvent arg0) {
        initCOS();
        if (Constant.IS_TASK) {
            doExtractResult();// 处理提现结果
            doTaoBaoOrders();// 处理淘宝订单
            doOrderFanLi();// 处理订单返利
            doOrderTiChengFanLi();// 处理订单提成返利
            doWeiQuanOrder();// 处理维权订单
            doPushIOS();// 处理发送IOS消息
        } else if (!Constant.IS_TEST) {
            initScheduler();// 启动定时任务
            doUpdateGoodsJob();
        }
    }
 
    /**
     * 对象存储初始化
     */
    public static void initCOS() {
        // 载入cos参数
        Properties ps = org.yeshi.utils.PropertiesUtil
                .getProperties(SpringContext.class.getClassLoader().getResourceAsStream("cos.properties"));
        COSInitParams params = new COSInitParams();
        params.setAppId(Long.parseLong(ps.getProperty("appId")));
        params.setBucketName(ps.getProperty("bucketName"));
        params.setRegion(ps.getProperty("region"));
        params.setSecretId(ps.getProperty("secretId"));
        params.setSecretKey(ps.getProperty("secretKey"));
        // 初始化
        COSManager.getInstance().init(params);
    }
 
    /**
     * 处理提现结果的队列
     */
    public void doExtractResult() {
        executor.execute(new Runnable() {
            @Override
            public void run() {
                while (true) {
 
                    try {
 
                        Map<String, AlipayTransferResultInfo> map = CMQManager.getInstance().consumeExtractResultMsg(1);
 
                        if (map != null) {
                            Iterator<String> its = map.keySet().iterator();
                            while (its.hasNext()) {
 
                                String key = its.next();
                                try {
                                    extractService.processExtractResult(map.get(key));
                                    CMQManager.getInstance().deleteExtractResultMsg(key);
                                } catch (Exception e) {
                                    try {
                                        LogHelper.errorDetailInfo(e);
                                    } catch (Exception e1) {
                                        e1.printStackTrace();
                                    }
                                }
                            }
                        }
                    } catch (Exception e) {
                    }
                }
            }
        });
    }
 
    /**
     * 处理淘宝订单
     */
    public void doTaoBaoOrders() {
        // 同时开启3个线程处理
        for (int i = 0; i < 3; i++) {
            executor.execute(new Runnable() {
                @Override
                public void run() {
                    while (true) {
                        try {
                            // 取16个订单
                            Map<String, List<TaoBaoOrder>> map = CMQManager.getInstance().consumeTaoBaoOrderMsg(16);
                            if (map != null) {
                                orderProcessService.processOrder(map);
                            }
                        } catch (Exception e) {
 
                        }
                    }
                }
            });
        }
    }
 
    /**
     * 处理淘宝订单返利
     */
    public void doOrderFanLi() {
        executor.execute(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    try {
                        Map<String, HongBao> map = CMQManager.getInstance().consumeFanLiMsg(16);
                        if (map != null) {
                            Iterator<String> its = map.keySet().iterator();
                            while (its.hasNext()) {
                                String handler = its.next();
                                try {
                                    orderProcessService.fanli(map.get(handler));
                                    CMQManager.getInstance().deleteFanLiMsg(handler);
                                } catch (TaoBaoWeiQuanException e) {
                                    try {
                                        LogHelper.errorDetailInfo(e);
                                    } catch (Exception e1) {
                                        e1.printStackTrace();
                                    }
                                } catch (Exception e) {
                                    try {
                                        LogHelper.errorDetailInfo(e);
                                    } catch (Exception e1) {
                                        e1.printStackTrace();
                                    }
                                }
                            }
                        }
 
                    } catch (Exception e) {
 
                    }
                }
            }
        });
    }
 
    /**
     * 处理淘宝提成订单返利
     */
    public void doOrderTiChengFanLi() {
        executor.execute(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    try {
 
                        Map<String, Long> map = CMQManager.getInstance().consumeFanLiTiChengMsg(16);
                        if (map != null) {
                            Iterator<String> its = map.keySet().iterator();
                            while (its.hasNext()) {
                                String handler = its.next();
                                try {
                                    orderProcessService.fanliInvaiteAndShare(map.get(handler));
                                    CMQManager.getInstance().deleteFanLiTiChengMsg(handler);
                                } catch (TaoBaoWeiQuanException e) {
                                    try {
                                        LogHelper.errorDetailInfo(e);
                                    } catch (Exception e1) {
                                        e1.printStackTrace();
                                    }
                                } catch (Exception e) {
                                    try {
                                        LogHelper.errorDetailInfo(e);
                                    } catch (Exception e1) {
                                        e1.printStackTrace();
                                    }
                                }
                            }
                        }
                    } catch (Exception e) {
 
                    }
                }
            }
        });
    }
 
    /**
     * 处理淘宝订单维权
     */
    public void doWeiQuanOrder() {
        executor.execute(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    try {
 
                        Map<String, TaoBaoWeiQuanOrder> map = CMQManager.getInstance().consumeWeiQuanOrderMsg(16);
                        if (map != null) {
                            Iterator<String> its = map.keySet().iterator();
                            while (its.hasNext()) {
                                String handler = its.next();
                                try {
                                    orderProcessService.weiQuanOrder(map.get(handler));
                                    CMQManager.getInstance().deleteWeiQuanOrderMsg(handler);
                                } catch (Exception e) {
                                    try {
                                        LogHelper.errorDetailInfo(e);
                                    } catch (Exception e1) {
                                        e1.printStackTrace();
                                    }
                                }
                            }
                        }
                    } catch (Exception e) {
 
                    }
                }
            }
        });
    }
 
    /**
     * IOS 推送消息
     */
    public void doPushIOS() {
 
        executor.execute(new Runnable() {
            @Override
            public void run() {
 
                while (true) {
                    try {
 
                        Map<String, PushQueueRecord> map = CMQManager.getInstance().consumeIOSPushMsg(10);
 
                        if (map != null) {
 
                            Iterator<String> its = map.keySet().iterator();
                            while (its.hasNext()) {
 
                                String key = its.next();
                                try {
                                    iosPushService.readyPushIOS(map.get(key));
 
                                    CMQManager.getInstance().deleteIOSPushMsg(key);
 
                                } catch (Exception e) {
                                    try {
                                        LogHelper.errorDetailInfo(e);
                                    } catch (Exception e1) {
                                        e1.printStackTrace();
                                    }
                                }
                            }
                        }
                    } catch (Exception e) {
 
                    }
                }
            }
        });
 
    }
 
    /**
     * 创建Scheduler()执行自动爬取
     */
    private void initScheduler() {
        boutiqueAutoRuleService.startScheduler();
    }
 
    public void doUpdateGoodsJob() {
 
        // 采用4个线程做更新
        for (int i = 0; i < 1; i++)
            executor.execute(new Runnable() {
                @Override
                public void run() {
 
                    while (true) {
                        try {
 
                            Map<String, Long> map = CMQManager.getInstance().consumeNeedUpdateTaoBaoGoodsIdMsg(16);
 
                            if (map != null) {
 
                                Iterator<String> its = map.keySet().iterator();
                                while (its.hasNext()) {
 
                                    String key = its.next();
                                    try {
                                        Long goodsId = map.get(key);
                                        List<Long> list = new ArrayList<>();
                                        list.add(goodsId);
                                        qualityFactoryService.updateGoodsFactory(list);
                                        CMQManager.getInstance().deleteNeedUpdateTaoBaoGoodsIdMsg(key);
                                    } catch (Exception e) {
                                        try {
                                            LogHelper.errorDetailInfo(e);
                                        } catch (Exception e1) {
                                            e1.printStackTrace();
                                        }
                                    }
                                }
                            }
                        } catch (Exception e) {
                            LogHelper.error("更新商品出错:"+e.getMessage());
 
                        }
                    }
                }
            });
 
    }
}