admin
2019-09-25 28a0efc6ec16f3f82eb8e16e87269300d6744fd4
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
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
package com.yeshi.fanli.util;
 
import java.math.BigDecimal;
import java.util.HashMap;
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 org.yeshi.utils.NumberUtil;
import org.yeshi.utils.tencentcloud.COSManager;
import org.yeshi.utils.tencentcloud.entity.COSInitParams;
 
import com.yeshi.fanli.dto.ad.DouYinDeviceActiveQueueDTO;
import com.yeshi.fanli.dto.money.UserMoneyChangeDTO;
import com.yeshi.fanli.entity.ad.DouYinClickEvent;
import com.yeshi.fanli.entity.bus.user.AlipayTransferResultInfo;
import com.yeshi.fanli.entity.bus.user.HongBaoV2;
import com.yeshi.fanli.entity.bus.user.Order;
import com.yeshi.fanli.entity.bus.user.ThreeSale;
import com.yeshi.fanli.entity.bus.user.UserInfo;
import com.yeshi.fanli.entity.jd.JDOrder;
import com.yeshi.fanli.entity.order.CommonOrder;
import com.yeshi.fanli.entity.order.HongBaoOrder;
import com.yeshi.fanli.entity.pdd.PDDOrder;
import com.yeshi.fanli.entity.push.DeviceActive;
import com.yeshi.fanli.entity.push.PushQueueRecord;
import com.yeshi.fanli.entity.taobao.TaoBaoGoodsBrief;
import com.yeshi.fanli.entity.taobao.TaoBaoOrder;
import com.yeshi.fanli.entity.taobao.TaoBaoWeiQuanOrder;
import com.yeshi.fanli.exception.order.TaoBaoWeiQuanException;
import com.yeshi.fanli.log.LogHelper;
import com.yeshi.fanli.service.inter.ad.DouYinClickEventService;
import com.yeshi.fanli.service.inter.lable.BoutiqueAutoRuleService;
import com.yeshi.fanli.service.inter.lable.LabelService;
import com.yeshi.fanli.service.inter.money.UserMoneyDebtService;
import com.yeshi.fanli.service.inter.money.extract.ExtractService;
import com.yeshi.fanli.service.inter.order.CommonOrderService;
import com.yeshi.fanli.service.inter.order.HongBaoOrderService;
import com.yeshi.fanli.service.inter.order.HongBaoV2Service;
import com.yeshi.fanli.service.inter.order.OrderProcessService;
import com.yeshi.fanli.service.inter.order.jd.JDOrderService;
import com.yeshi.fanli.service.inter.order.pdd.PDDOrderService;
import com.yeshi.fanli.service.inter.order.tb.TaoBaoOrderService;
import com.yeshi.fanli.service.inter.push.DeviceActiveService;
import com.yeshi.fanli.service.inter.push.IOSPushService;
import com.yeshi.fanli.service.inter.taobao.TaoBaoGoodsUpdateService;
import com.yeshi.fanli.service.inter.user.UserInfoService;
import com.yeshi.fanli.service.inter.user.UserSystemCouponService;
import com.yeshi.fanli.service.inter.user.integral.IntegralGetService;
import com.yeshi.fanli.service.inter.user.integral.IntegralTaskRecordService;
import com.yeshi.fanli.service.inter.user.invite.ThreeSaleSerivce;
import com.yeshi.fanli.util.ad.DouYinAdUtil;
import com.yeshi.fanli.util.cmq.DouYinDeviceActiveCMQManager;
import com.yeshi.fanli.util.cmq.HongBaoRecieveCMQManager;
import com.yeshi.fanli.util.cmq.JDOrderCMQManager;
import com.yeshi.fanli.util.cmq.PDDOrderCMQManager;
import com.yeshi.fanli.util.cmq.PlaceOrderCMQManager;
import com.yeshi.fanli.util.cmq.ThreeSaleCMQManager;
import com.yeshi.fanli.util.cmq.UserMoneyChangeCMQManager;
import com.yeshi.fanli.util.taobao.TaoKeApiUtil;
 
/**
 * 系统初始化
 * 
 * @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 TaoBaoGoodsUpdateService taoBaoGoodsUpdateService;
 
    @Resource
    private UserSystemCouponService userSystemCouponService;
 
    @Resource
    private ThreeSaleSerivce threeSaleSerivce;
 
    @Resource
    private TaoBaoOrderService taoBaoOrderService;
 
    @Resource
    private RedisManager redisManager;
 
    @Resource
    private UserMoneyDebtService userMoneyDebtService;
 
    @Resource
    private JDOrderService jdOrderService;
 
    @Resource
    private PDDOrderService pddOrderService;
 
    @Resource
    private HongBaoV2Service hongBaoV2Service;
 
    @Resource
    private IntegralGetService integralGetService;
 
    @Resource
    private CommonOrderService commonOrderService;
 
    @Resource
    private HongBaoOrderService hongBaoOrderService;
 
    @Resource
    private IntegralTaskRecordService integralTaskRecordService;
 
    @Resource
    private DeviceActiveService deviceActiveService;
 
    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();// 处理淘宝订单
            doTaoBaoNewOrders();// 处理淘宝订单(刚刚产生的)
            doOrderFanLiNew();// 新版返利
            doOrderTiChengFanLi();// 处理订单提成返利
            doWeiQuanOrder();// 处理维权订单
            doPushIOS();// 处理发送IOS消息
            doThreeSaleUserCouponJob();// 处理邀请队列
            doUserMoneyDebtJob();// 债务偿还
            initScheduler();// 启动商品更新定时任务
            doUpdateGoodsJob(); // 更新商品队列
            doJDOrderJob();// 京东订单处理
            doPDDOrderJob();// 拼多多订单处理
            doImportantTaoBaoGoodsUpdateJob();// 淘宝重要商品的信息更新
            doHongBaoRecieveIntegralGetJob();// 返利到账,金币增加
//            doPlaceOrderIntegralJob();// 下单赠送金币任务
//            doDouYinDeviceActiveJob();// 抖音设备激活广告监测
        }
 
    }
 
    /**
     * 对象存储初始化
     */
    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个订单
                            List<String> orderIds = CMQManager.getInstance().consumeTaoBaoOrderMsg(16);
                            if (orderIds != null) {
                                for (String orderId : orderIds) {
                                    if (NumberUtil.isNumeric(orderId.trim())) {
                                        Map<String, List<TaoBaoOrder>> map = new HashMap<>();
                                        List<TaoBaoOrder> orderList = taoBaoOrderService
                                                .getTaoBaoOrderByOrderId(orderId);
                                        map.put(orderId, orderList);
                                        LogHelper.orderInfo("开始处理订单:" + orderId);
                                        orderProcessService.processOrder(map);
                                        LogHelper.orderInfo("结束处理订单:" + orderId);
                                    }
                                }
                            }
                        } catch (Exception e) {
                            try {
                                LogHelper.errorDetailInfo(e);
                            } catch (Exception e1) {
 
                            }
                        }
                    }
                }
            });
        }
    }
 
    /**
     * 刚刚产生的订单
     */
    public void doTaoBaoNewOrders() {
        // 同时开启3个线程处理
        for (int i = 0; i < 1; i++) {
            executor.execute(new Runnable() {
                @Override
                public void run() {
                    while (true) {
                        try {
                            // 取16个订单
                            List<String> orderIds = CMQManager.getInstance().consumeTaoBaoNewOrderMsg(16);
                            Map<String, List<TaoBaoOrder>> map = new HashMap<>();
                            if (orderIds != null) {
                                for (String orderId : orderIds) {
                                    List<TaoBaoOrder> orderList = taoBaoOrderService.getTaoBaoOrderByOrderId(orderId);
                                    map.put(orderId, orderList);
 
                                    LogHelper.orderInfo("新订单-开始处理订单:" + orderId);
                                    long startTime = System.currentTimeMillis();
                                    orderProcessService.processOrder(map);
                                    LogHelper.orderInfo("新订单-结束处理订单:" + orderId + "-  处理时间:"
                                            + (System.currentTimeMillis() - startTime));
                                }
                            }
                        } catch (Exception e) {
                            try {
                                LogHelper.errorDetailInfo(e);
                            } catch (Exception e1) {
 
                            }
                        }
                    }
                }
            });
        }
    }
 
    /**
     * 处理新版淘宝订单返利
     */
    public void doOrderFanLiNew() {
        executor.execute(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    try {
                        Map<String, HongBaoV2> map = CMQManager.getInstance().consumeFanLiMsgNew(16);
                        if (map != null) {
                            Iterator<String> its = map.keySet().iterator();
                            while (its.hasNext()) {
                                String handler = its.next();
                                try {
                                    HongBaoV2 hongbao = map.get(handler);
                                    if (hongbao != null) {
                                        String key = "hongbao-fanli-" + hongbao.getId();
                                        if (StringUtil.isNullOrEmpty(redisManager.getCommonString(key))) {
                                            // 处理之后要隔2小时再次进行处理
                                            redisManager.cacheCommonString(key, "1", 60 * 60 * 2);
                                            orderProcessService.fanli(map.get(handler));
                                        }
                                    }
                                    CMQManager.getInstance().deleteFanLiMsgNew(handler);
                                } catch (TaoBaoWeiQuanException e) {
                                    try {
                                        LogHelper.errorDetailInfo(e);
                                    } catch (Exception e1) {
                                        e1.printStackTrace();
                                    }
                                } catch (Exception e) {
                                    try {
                                        LogHelper.errorDetailInfo(e, "HongBaoV2-ID:" + map.get(handler).getId(), "");
                                    } 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, map.get(handler).toString(), "");
                                    } 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) {
                        LogHelper.errorDetailInfo(e);
                    }
                }
            }
        });
 
    }
 
    /**
     * 创建Scheduler()执行自动爬取
     */
    private void initScheduler() {
        boutiqueAutoRuleService.startScheduler();
    }
 
    /**
     * 更新精选库商品
     */
    public void doUpdateGoodsJob() {
        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();
                                    taoBaoGoodsUpdateService.updateByTaoKeGoodsDetail(Long.parseLong(key));
                                    CMQManager.getInstance().deleteNeedUpdateTaoBaoGoodsIdMsg(key);
                                }
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                            LogHelper.error("更新商品出错:" + e.getMessage());
                        }
                    }
                }
            });
    }
 
    /**
     * 邀请关系变化后券的更新
     */
    public void doThreeSaleUserCouponJob() {
 
        // 采用2个线程做更新
        for (int i = 0; i < 2; i++)
            executor.execute(new Runnable() {
                @Override
                public void run() {
 
                    while (true) {
                        try {
                            Map<String, ThreeSale> map = ThreeSaleCMQManager.getInstance()
                                    .consumeQueueMsg(ThreeSaleCMQManager.QUEUE_USER_COUPON, 16);
                            if (map != null) {
 
                                Iterator<String> its = map.keySet().iterator();
                                while (its.hasNext()) {
                                    String key = its.next();
                                    try {
                                        ThreeSale threeSale = map.get(key);
                                        if (threeSale != null)
                                            threeSale = threeSaleSerivce.selectByPrimaryKey(threeSale.getId());
                                        if (threeSale != null) {
                                            if (threeSale.getState() != null && threeSale.getState() == true) {
                                                // 邀请成功 - 券激活生效
                                                LogHelper.test("doThreeSaleUserCouponJob-" + threeSale.getId());
 
                                                Long bossId = threeSale.getBoss().getId();
                                                // 下级昵称
 
                                                String workerNickName = userInfoService
                                                        .selectByPKey(threeSale.getWorker().getId()).getNickName();
 
                                                userSystemCouponService.activatedWelfareFreeCoupon(bossId,
                                                        workerNickName);
                                            }
                                        }
                                        ThreeSaleCMQManager.getInstance()
                                                .deleteQueueMsg(ThreeSaleCMQManager.QUEUE_USER_COUPON, key);
                                    } catch (Exception e) {
                                        try {
                                            LogHelper.errorDetailInfo(e);
                                        } catch (Exception e1) {
                                            e1.printStackTrace();
                                        }
                                    }
                                }
                            }
                        } catch (Exception e) {
                            LogHelper.error("更新商品出错:" + e.getMessage());
 
                        }
                    }
                }
            });
 
    }
 
    /**
     * 用户借贷处理
     */
    public void doUserMoneyDebtJob() {
        executor.execute(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    try {
                        Map<String, UserMoneyChangeDTO> map = UserMoneyChangeCMQManager.getInstance()
                                .consumeQueueMsg(UserMoneyChangeCMQManager.QUEUE_DEBT, 16);
                        if (map != null) {
 
                            Iterator<String> its = map.keySet().iterator();
                            while (its.hasNext()) {
                                String key = its.next();
                                try {
                                    UserMoneyChangeDTO dto = map.get(key);
                                    if (dto != null && dto.getChangeMoney().compareTo(new BigDecimal(0)) > 0) {
                                        userMoneyDebtService.repayDebt(dto.getUid());
                                    }
                                    UserMoneyChangeCMQManager.getInstance()
                                            .deleteQueueMsg(UserMoneyChangeCMQManager.QUEUE_DEBT, key);
                                } catch (Exception e) {
                                    try {
                                        LogHelper.errorDetailInfo(e);
                                    } catch (Exception e1) {
                                        e1.printStackTrace();
                                    }
                                }
                            }
                        }
                    } catch (Exception e) {
                        LogHelper.error("还款出错:" + e.getMessage());
                    }
                }
            }
        });
    }
 
    public void doJDOrderJob() {
        executor.execute(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    try {
                        Map<String, String> map = JDOrderCMQManager.getInstance().consumeJDOrder(16);
                        if (map != null) {
                            Iterator<String> its = map.keySet().iterator();
                            while (its.hasNext()) {
                                String key = its.next();
                                try {
                                    String orderId = map.get(key);
                                    if (!StringUtil.isNullOrEmpty(orderId)) {
                                        JDOrder order = jdOrderService.selectDetailByOrderId(Long.parseLong(orderId));
                                        orderProcessService.processJDOrder(order);
                                    }
                                    JDOrderCMQManager.getInstance().deleteJDOrder(key);
                                } catch (Exception e) {
                                    try {
                                        LogHelper.errorDetailInfo(e);
                                    } catch (Exception e1) {
                                        e1.printStackTrace();
                                    }
                                }
                            }
                        }
                    } catch (Exception e) {
                        LogHelper.error("拼多多订单出错:" + e.getMessage());
 
                    }
                }
            }
        });
 
    }
 
    public void doPDDOrderJob() {
        executor.execute(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    try {
                        Map<String, String> map = PDDOrderCMQManager.getInstance().consumePDDOrder(16);
                        if (map != null) {
                            Iterator<String> its = map.keySet().iterator();
                            while (its.hasNext()) {
                                String key = its.next();
                                try {
                                    String orderId = map.get(key);
                                    if (!StringUtil.isNullOrEmpty(orderId)) {
                                        List<PDDOrder> orderList = pddOrderService.selectByOrderSn(orderId);
                                        if (orderList != null)
                                            for (PDDOrder order : orderList)
                                                orderProcessService.processPDDOrder(order);
                                    }
                                    PDDOrderCMQManager.getInstance().deletePDDOrder(key);
                                } catch (Exception e) {
                                    LogHelper.errorDetailInfo(e);
                                }
                            }
                        }
                    } catch (Exception e) {
                        LogHelper.error("拼多多订单:" + e.getMessage());
                    }
                }
            }
        });
 
    }
 
    /**
     * 重要的淘宝商品信息更新
     */
    public void doImportantTaoBaoGoodsUpdateJob() {
        // 开启2个线程做更新
        for (int i = 0; i < 2; i++)
            executor.execute(new Runnable() {
                @Override
                public void run() {
                    while (true) {
                        Map<String, Long> map = CMQManager.getInstance().consumeTBImpGoodsUpdateMsg(16);
                        if (map != null) {
                            Iterator<String> its = map.keySet().iterator();
                            while (its.hasNext()) {
                                String key = its.next();
                                try {
                                    long startTime = System.currentTimeMillis();
                                    TaoBaoGoodsBrief goods = TaoKeApiUtil.searchGoodsDetail(map.get(key));
                                    // 更新一条数据需要的时间
                                    taoBaoGoodsUpdateService.updateTaoBaoGoods(goods);
                                    long time = System.currentTimeMillis() - startTime;
                                    LogHelper.test("淘宝重要商品库更新时间:" + time);
                                } catch (Exception e) {
                                    try {
                                        LogHelper.errorDetailInfo(e);
                                    } catch (Exception e1) {
                                        e1.printStackTrace();
                                    }
                                } finally {
                                    CMQManager.getInstance().deleteTBImpGoodsUpdateMsg(key);
                                }
                            }
                        }
                    }
                }
            });
 
    }
 
    /**
     * 红包到账后的金币获取
     */
    public void doHongBaoRecieveIntegralGetJob() {
        executor.execute(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    Map<String, Long> map = HongBaoRecieveCMQManager.getInstance()
                            .consumeQueueMsg(HongBaoRecieveCMQManager.QUEUE_INTEGRAL, 16);
                    if (map != null) {
                        Iterator<String> its = map.keySet().iterator();
                        while (its.hasNext()) {
                            String key = its.next();
                            Long hongBaoId = map.get(key);
                            try {
                                HongBaoV2 hongBaoV2 = hongBaoV2Service.selectByPrimaryKey(hongBaoId);
                                if (hongBaoV2 != null && hongBaoV2.getState() == HongBaoV2.STATE_YILINGQU
                                        && hongBaoV2.getMoney().compareTo(new BigDecimal(0)) > 0) {
                                    if (hongBaoV2.getType() == HongBaoV2.TYPE_ZIGOU) {// 自购
                                        integralGetService.addRebateOrder(hongBaoV2.getUserInfo().getId());
                                    } else if (hongBaoV2.getType() == HongBaoV2.TYPE_SHARE_GOODS) {// 分享赚
                                        integralGetService.addShareOrder(hongBaoV2.getUserInfo().getId());
                                    } else if (hongBaoV2.getType() == HongBaoV2.TYPE_YIJI
                                            || hongBaoV2.getType() == HongBaoV2.TYPE_SHARE_YIJI) {// 一级邀请赚
                                        integralGetService.addInviteOrderLevelOne(hongBaoV2.getUserInfo().getId());
                                    } else if (hongBaoV2.getType() == HongBaoV2.TYPE_ERJI
                                            || hongBaoV2.getType() == HongBaoV2.TYPE_SHARE_ERJI) {// 二级邀请赚
                                        integralGetService.addInviteOrderLevelOne(hongBaoV2.getUserInfo().getId());
                                    }
                                }
                                HongBaoRecieveCMQManager.getInstance()
                                        .deleteQueueMsg(HongBaoRecieveCMQManager.QUEUE_INTEGRAL, key);
                            } catch (Exception e) {
                                try {
                                    LogHelper.errorDetailInfo(e);
                                } catch (Exception e1) {
                                    e1.printStackTrace();
                                }
                            } finally {
 
                            }
                        }
                    }
                }
            }
        });
    }
 
    private boolean isFirstValidOrder(String orderId, int sourceType, Long uid) {
        CommonOrder commonOrder = commonOrderService.selectLatestValidByUid(uid);
        if (commonOrder != null && commonOrder.getOrderNo().equalsIgnoreCase(orderId)
                && sourceType == commonOrder.getSourceType())
            return true;
        else
            return false;
    }
 
    // 下单获取金币任务
    public void doPlaceOrderIntegralJob() {
        executor.execute(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    Map<String, Order> map = PlaceOrderCMQManager.getInstance()
                            .consumeQueueMsg(PlaceOrderCMQManager.QUEUE_INTEGRAL, 16);
                    if (map != null) {
                        // 是否是首单
                        for (Iterator<String> its = map.keySet().iterator(); its.hasNext();) {
                            try {
                                String key = its.next();
                                Order order = map.get(key);
                                // 统计订单下的用户所获得的返利金额
                                List<CommonOrder> list = commonOrderService
                                        .listBySourceTypeAndOrderId(order.getOrderType(), order.getOrderId());
                                Map<Long, BigDecimal> hongBaoMoney = new HashMap<>();
                                if (list != null)
                                    for (CommonOrder commonOrder : list) {
                                        HongBaoOrder hongBaoOrder = hongBaoOrderService
                                                .selectDetailByCommonOrderId(commonOrder.getId());
                                        if (hongBaoOrder != null && hongBaoOrder.getHongBaoV2() != null) {
                                            Long mainUid = hongBaoOrder.getHongBaoV2().getUserInfo().getId();
                                            if (hongBaoMoney.get(mainUid) == null)
                                                hongBaoMoney.put(mainUid, new BigDecimal(0));
                                            hongBaoMoney.put(mainUid, hongBaoMoney.get(mainUid)
                                                    .add(hongBaoOrder.getHongBaoV2().getMoney()));
                                            // 查询上级
                                            List<HongBaoV2> children = hongBaoV2Service
                                                    .listChildrenById(hongBaoOrder.getHongBaoV2().getId());
                                            if (children != null) {
                                                for (HongBaoV2 hongBao : children) {
                                                    Long uid = hongBao.getUserInfo().getId();
                                                    if (hongBaoMoney.get(uid) == null)
                                                        hongBaoMoney.put(uid, new BigDecimal(0));
                                                    hongBaoMoney.put(uid,
                                                            hongBaoMoney.get(uid).add(hongBao.getMoney()));
                                                }
                                            }
                                        }
                                    }
 
                                if (isFirstValidOrder(order.getOrderId(), order.getOrderType(),
                                        order.getUserInfo().getId())) {// 有效的首单
                                    if (hongBaoMoney.get(order.getUserInfo().getId()) == null || hongBaoMoney
                                            .get(order.getUserInfo().getId()).compareTo(new BigDecimal("0")) <= 0) {
                                        // 分享奖金是0
                                        integralTaskRecordService.firstShareOrderReward(order.getUserInfo().getId(),
                                                null);
                                    }
 
                                    // 获取上两级数据
                                    UserInfo boss = threeSaleSerivce.getBoss(order.getUserInfo().getId());
                                    if (boss != null) {// 判断上级的红包
                                        if (hongBaoMoney.get(boss.getId()) == null
                                                || hongBaoMoney.get(boss.getId()).compareTo(new BigDecimal("0")) <= 0) {
                                            // 补偿金币
                                            integralTaskRecordService.firstSharerOrderRewardBoss(boss.getId(),
                                                    order.getUserInfo().getId(), null);
                                        }
                                    }
 
                                } else {// 自购订单
                                    UserInfo boss = threeSaleSerivce.getBoss(order.getUserInfo().getId());
                                    if (boss != null) {// 判断上级的红包
                                        if (hongBaoMoney.get(boss.getId()) == null
                                                || hongBaoMoney.get(boss.getId()).compareTo(new BigDecimal("0")) <= 0) {
                                            // 补偿金币1级
                                            integralTaskRecordService.firstRebateOrderRewardBoss(boss.getId(),
                                                    order.getUserInfo().getId(), null);
                                        }
                                    }
 
                                    boss = threeSaleSerivce.getBoss(boss.getId());
                                    if (boss != null) {// 判断上级的红包
                                        if (hongBaoMoney.get(boss.getId()) == null
                                                || hongBaoMoney.get(boss.getId()).compareTo(new BigDecimal("0")) <= 0) {
                                            // 补偿金币2级
                                            integralTaskRecordService.firstRebateOrderRewardBossSuper(boss.getId(),
                                                    order.getUserInfo().getId(), null);
                                        }
                                    }
                                }
                                PlaceOrderCMQManager.getInstance().deleteQueueMsg(PlaceOrderCMQManager.QUEUE_INTEGRAL,
                                        key);
                            } catch (Exception e) {
                                LogHelper.errorDetailInfo(e);
                            }
                        }
 
                    }
                }
            }
        });
    }
 
    // 抖音广告监控
    public void doPlaceOrderAdJob() {
        executor.execute(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    Map<String, Order> map = PlaceOrderCMQManager.getInstance()
                            .consumeQueueMsg(PlaceOrderCMQManager.QUEUE_AD, 16);
                    if (map != null) {
                        // 是否是首单
                        for (Iterator<String> its = map.keySet().iterator(); its.hasNext();) {
                            String key = its.next();
                            Order order = map.get(key);
                            if (isFirstValidOrder(order.getOrderId(), order.getOrderType(),
                                    order.getUserInfo().getId())) {// 有效的首单
                                // TODO 抖音的数据回调
                            }
                        }
                    }
                }
            }
        });
    }
 
    @Resource
    private DouYinClickEventService douYinClickEventService;
 
    // 抖音
    public void doDouYinDeviceActiveJob() {
        LogHelper.test("开始执行:doDouYinDeviceActiveJob");
        executor.execute(new Runnable() {
            @Override
            public void run() {
                LogHelper.test("开始执行:doDouYinDeviceActiveJob-run");
                while (true) {
                    Map<String, DouYinDeviceActiveQueueDTO> map = DouYinDeviceActiveCMQManager.getInstance()
                            .consume(16);
                    if (map != null) {
                        // 是否是首单
                        for (Iterator<String> its = map.keySet().iterator(); its.hasNext();) {
                            String key = its.next();
                            DouYinDeviceActiveQueueDTO active = map.get(key);
                            if (active.getType() == DouYinDeviceActiveQueueDTO.TYPE_AD) {// 抖音
                                DouYinClickEvent event = douYinClickEventService.selectByAid(active.getAid());
                                if (event != null) {
                                    if (event.getOs() == 0) {
                                        DeviceActive deviceActive = deviceActiveService
                                                .getFirstActiveInfoByImei(event.getUuid());
                                        if (deviceActive != null) {
                                            if (event != null) {
                                                if (event.getOs() == 0)
                                                    DouYinAdUtil.activeAndroid(event.getCallback(), event.getImei(),
                                                            deviceActive.getCreateTime().getTime());
                                                else if (event.getOs() == 1)
                                                    DouYinAdUtil.activeIOS(event.getCallback(), event.getIdfa(),
                                                            deviceActive.getCreateTime().getTime());
                                            }
                                        }
                                    } else {// TODO 处理IOS
 
                                    }
                                }
 
                            } else if (active.getType() == DouYinDeviceActiveQueueDTO.TYPE_DEVICE) {// 设备
 
                                if (active.getPlatform() == 1) {
                                    DeviceActive deviceActive = deviceActiveService
                                            .getFirstActiveInfo(active.getDevice());
                                    if (deviceActive != null && deviceActive.getImei() != null) {
                                        DouYinClickEvent event = douYinClickEventService
                                                .selectByUuid(deviceActive.getImei());
                                        if (event != null) {// 回调
                                            if (deviceActive != null)
                                                DouYinAdUtil.activeAndroid(event.getCallback(), event.getImei(),
                                                        deviceActive.getCreateTime().getTime());
                                        }
                                    }
                                } else {
                                    // TODO 兼容IOS
                                }
                            }
                            DouYinDeviceActiveCMQManager.getInstance().delete(key);
                        }
                    }
                }
            }
        });
    }
 
}