Administrator
2018-12-10 94c29daf7cb722635c1735e8d8bd403105e999d8
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
package com.yeshi.fanli.service.impl.order;
 
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
 
import javax.annotation.Resource;
 
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import com.yeshi.fanli.dao.mybatis.AccountDetailsMapper;
import com.yeshi.fanli.dao.mybatis.AccountMessageMapper;
import com.yeshi.fanli.dao.mybatis.HongBaoManageMapper;
import com.yeshi.fanli.dao.mybatis.MoneyRecordMapper;
import com.yeshi.fanli.dao.mybatis.PidOrderMapper;
import com.yeshi.fanli.dao.mybatis.ThreeSaleGiftMapper;
import com.yeshi.fanli.dao.mybatis.UserInfoMapper;
import com.yeshi.fanli.dao.mybatis.hongbao.HongBaoMapper;
import com.yeshi.fanli.dao.mybatis.order.OrderItemMapper;
import com.yeshi.fanli.dao.mybatis.order.OrderMapper;
import com.yeshi.fanli.dao.mybatis.share.PidUserMapper;
import com.yeshi.fanli.dao.mybatis.taobao.TaoBaoWeiQuanOrderMapper;
import com.yeshi.fanli.entity.bus.user.AccountDetails;
import com.yeshi.fanli.entity.bus.user.HongBao;
import com.yeshi.fanli.entity.bus.user.HongBaoExtra;
import com.yeshi.fanli.entity.bus.user.MoneyRecord;
import com.yeshi.fanli.entity.bus.user.Order;
import com.yeshi.fanli.entity.bus.user.OrderItem;
import com.yeshi.fanli.entity.bus.user.UserInfo;
import com.yeshi.fanli.entity.taobao.PidOrder;
import com.yeshi.fanli.entity.taobao.PidUser;
import com.yeshi.fanli.entity.taobao.TaoBaoOrder;
import com.yeshi.fanli.entity.taobao.TaoBaoUnionConfig;
import com.yeshi.fanli.entity.taobao.TaoBaoWeiQuanOrder;
import com.yeshi.fanli.entity.taobao.TaobaoOrderHongBao;
import com.yeshi.fanli.exception.HongBaoException;
import com.yeshi.fanli.exception.ObjectStateException;
import com.yeshi.fanli.exception.OrderItemException;
import com.yeshi.fanli.exception.TaoBaoWeiQuanException;
import com.yeshi.fanli.log.LogHelper;
import com.yeshi.fanli.service.inter.hongbao.AccountDetailsHongBaoMapService;
import com.yeshi.fanli.service.inter.hongbao.HongBaoManageService;
import com.yeshi.fanli.service.inter.hongbao.HongBaoService;
import com.yeshi.fanli.service.inter.order.LostOrderService;
import com.yeshi.fanli.service.inter.order.OrderItemServcie;
import com.yeshi.fanli.service.inter.order.OrderProcessService;
import com.yeshi.fanli.service.inter.order.OrderService;
import com.yeshi.fanli.service.inter.order.PidOrderService;
import com.yeshi.fanli.service.inter.push.PushService;
import com.yeshi.fanli.service.inter.taobao.TaoBaoOrderService;
import com.yeshi.fanli.service.inter.taobao.TaoBaoUnionConfigService;
import com.yeshi.fanli.service.inter.taobao.TaoBaoWeiQuanDrawBackService;
import com.yeshi.fanli.service.inter.user.UserNotificationService;
import com.yeshi.fanli.util.CMQManager;
import com.yeshi.fanli.util.HongBaoUtil;
import com.yeshi.fanli.util.RedisManager;
import com.yeshi.fanli.util.StringUtil;
import com.yeshi.fanli.util.TimeUtil;
import com.yeshi.fanli.util.factory.AccountDetailsFactory;
import com.yeshi.fanli.util.taobao.TaoBaoOrderUtil;
 
@Service
public class OrderProcessServiceImpl implements OrderProcessService {
 
    @Resource
    private PidOrderMapper pidOrderMapper;
 
    @Resource
    private OrderMapper orderMapper;
 
    @Resource
    private PidUserMapper pidUserMapper;
 
    @Resource
    private HongBaoService hongBaoService;
 
    @Resource
    private OrderItemServcie orderItemService;
 
    @Resource
    private PidOrderService pidOrderService;
 
    @Resource
    private UserInfoMapper userInfoMapper;
 
    @Resource
    private OrderItemMapper orderItemMapper;
 
    @Resource
    private HongBaoMapper hongBaoMapper;
 
    @Resource
    private HongBaoManageMapper hongBaoManageMapper;
 
    @Resource
    private AccountDetailsMapper accountDetailsMapper;
 
    @Resource
    private AccountMessageMapper accountMessageMapper;
 
    @Resource
    private MoneyRecordMapper moneyRecordMapper;
 
    @Resource
    private ThreeSaleGiftMapper threeSaleGiftMapper;
 
    @Resource
    private OrderService orderService;
 
    @Resource
    private PushService pushService;
 
    @Resource
    private UserNotificationService userNotificationService;
 
    @Resource
    private TaoBaoUnionConfigService taoBaoUnionConfigService;
 
    @Resource
    private TaoBaoWeiQuanDrawBackService taoBaoWeiQuanDrawBackService;
 
    @Resource
    private TaoBaoOrderService taoBaoOrderService;
 
    @Resource
    private RedisManager redisManager;
 
    @Resource
    private LostOrderService lostOrderService;
 
    @Resource
    private TaoBaoWeiQuanOrderMapper taoBaoWeiQuanOrderMapper;
 
    @Resource
    private HongBaoManageService hongBaoManageService;
 
    @Resource
    private AccountDetailsHongBaoMapService accountDetailsHongBaoMapService;
 
    @Override
    public void processOrder(Map<String, List<TaoBaoOrder>> orders) {
        List<TaoBaoUnionConfig> configList = taoBaoUnionConfigService.getConfigByTypeCache(PidUser.TYPE_FANLI_ANDROID);
 
        // 分离出返利订单与分享赚订单
        Iterator<String> its = orders.keySet().iterator();
        Map<String, List<TaoBaoOrder>> fanliOrderMap = new HashMap<>();
        Map<String, List<PidOrder>> shareOrderMap = new HashMap<>();
        while (its.hasNext()) {
            String orderId = its.next();
            if (orders.get(orderId) != null && orders.get(orderId).size() > 0) {
                List<TaoBaoOrder> list = orders.get(orderId);
                String pid = String.format("mm_%s_%s_%s", configList.get(0).getAccountId(),
                        list.get(0).getSourceMediaId(), list.get(0).getAdPositionId());
                PidUser pidUser = pidUserMapper.selectByPid(pid);
                if (pidUser != null && pidUser.getType() == PidUser.TYPE_SHARE_GOODS) {// 商品分享订单
                    List<PidOrder> pidOrderList = new ArrayList<>();
                    for (TaoBaoOrder order : list) {
                        pidOrderList.add(TaoBaoOrderUtil.convertToPidOrder(order));
                    }
                    shareOrderMap.put(orderId, pidOrderList);
                } else {// 普通返利订单
                    fanliOrderMap.put(orderId, list);
                }
            }
        }
 
        // 处理返利订单
        processShopingFanliOrder(fanliOrderMap);
        // 处理分享赚订单
        processShareGoodsOrder(shareOrderMap);
    }
 
    /**
     * 利用redis的原子性将订单号锁住,不让其他处理
     * 
     * @param orderId
     */
    private void redisLockOrder(String orderId) {
        String key = "doorder-" + orderId;
        redisManager.cacheCommonString(key, "1");
    }
 
    /**
     * 订单解锁
     * 
     * @param orderId
     */
    private void redisUnlockOrder(String orderId) {
        String key = "doorder-" + orderId;
        redisManager.removeCommonString(key);
    }
 
    /**
     * 订单是否被锁住了
     * 
     * @param orderId
     * @return
     */
    private boolean isRedisLockOrder(String orderId) {
        String key = "doorder-" + orderId;
        String doing = redisManager.getCommonString(key);
        if (!StringUtil.isNullOrEmpty(doing))
            return true;
        else
            return false;
    }
 
    @Transactional
    @Override
    public void processShopingFanliOrder(Map<String, List<TaoBaoOrder>> orders) {
        // 查询在Order中是否存在该订单号
        Iterator<String> its = orders.keySet().iterator();
        while (its.hasNext()) {
            try {
                String orderId = its.next();
                // 处理红包与子红包
                // if (isRedisLockOrder(orderId))
                // continue;
                // redisLockOrder(orderId);
                processShopingFanliOrder(orderId, orders.get(orderId));
                // redisUnlockOrder(orderId);
                // 出错了就不处理该订单了,需要定期检查
 
                // 更改丢失订单的状态
                lostOrderService.processSuceess(orderId);
 
            } catch (HongBaoException e1) {
                e1.printStackTrace();
            } catch (OrderItemException e2) {
                e2.printStackTrace();
            } catch (Exception e) {
 
            }
        }
    }
 
    @Transactional
    @Override
    public void processShareGoodsOrder(Map<String, List<PidOrder>> orders) {
        pidOrderService.addPidOrderFromTaoBao(orders);
    }
 
    /**
     * 获取最大的创建时间与最小的创建时间
     * 
     * @param hongBaoList
     * @return
     */
    private Map<String, Long> getMaxAndMinCreateTime(List<HongBao> hongBaoList) {
        if (hongBaoList.size() > 0) {
            Map<String, Long> map = new HashMap<>();
            long min = hongBaoList.get(0).getCreatetime();
            long max = hongBaoList.get(0).getCreatetime();
            for (HongBao hb : hongBaoList) {
                if (hb.getCreatetime() > max)
                    max = hb.getCreatetime();
                if (hb.getCreatetime() < min)
                    min = hb.getCreatetime();
            }
            map.put("max", max);
            map.put("min", min);
            return map;
        }
        return null;
    }
 
    @Override
    public void fanli() {
        // 获取主红包
        List<HongBao> hongBaoList = hongBaoMapper.selectCanBalanceHongBaoByType(HongBao.TYPE_TAOBAO, 100);
        //
        // /**
        // * 需要实时更新待返利的订单
        // */
        //
        // // 计算最大的创建时间与最小的创建时间
        // Map<String, Long> map = getMaxAndMinCreateTime(hongBaoList);
        // if (map != null) {
        // long max = map.get("max");
        // long min = map.get("min");
        // // 最小时间提前10分钟
        // min = min - 1000 * 60 * 10L;
        // // 更新该段时间的订单
        // List<TaoBaoOrder> orderList = new ArrayList<>();
        // try {
        // orderList = TaoBaoOrderUtil.getOrderList(min, max);
        // } catch (TaoBaoOrderException e) {
        // e.printStackTrace();
        // }
        // // try {
        // if (orderList != null)
        // processOrder(TaoBaoOrderUtil.classifyTaoBaoOrderByOrderId(orderList));
        // // } catch (Exception e) {
        // // e.printStackTrace();
        // // try {
        // // LogHelper.errorDetailInfo(e);
        // // } catch (Exception e1) {
        // // e1.printStackTrace();
        // // }
        // // }
        // }
 
        /**
         * 开始返利
         */
        for (HongBao hb : hongBaoList) {
            //
            if (hb.getState() == HongBao.STATE_SHIXIAO || hb.getState() == HongBao.STATE_YILINGQU)
                continue;
 
            if (hb.getPreGettime() == null || hb.getPreGettime() == 0
                    || hb.getPreGettime() > System.currentTimeMillis())
                continue;
            try {
                CMQManager.getInstance().addFanLiMsg(hb);
            } catch (Exception e) {
                try {
                    LogHelper.errorDetailInfo(e);
                } catch (Exception e1) {
                    e1.printStackTrace();
                }
            }
        }
    }
 
    /**
     * 判断订单是否失效 全部订单失效才判断为失效
     * 
     * @param list
     * @return
     */
    private boolean isOrderValid(List<TaoBaoOrder> list) {
        int validCount = 0;
        for (TaoBaoOrder order : list) {
            if (order.getOrderState().equalsIgnoreCase("订单失效"))
                validCount++;
        }
        return validCount >= list.size();
    }
 
    @Override
    @Transactional
    public void processShopingFanliOrder(String orderId, List<TaoBaoOrder> orderList)
            throws HongBaoException, OrderItemException {
 
        List<TaoBaoUnionConfig> configList = taoBaoUnionConfigService.getConfigByTypeCache(PidUser.TYPE_FANLI_ANDROID);
 
        Order oldOrder = orderMapper.selectOrderByOrderId(orderId);
        // 原来不存在订单
        if (oldOrder == null && orderList.size() > 0) {
            // 获取产生订单的PID
            String mediaId = orderList.get(0).getSourceMediaId();
            String adzoneId = orderList.get(0).getAdPositionId();
            String pid = String.format("mm_%s_%s_%s", configList.get(0).getAccountId(), mediaId, adzoneId);
            // 查询该PID是否有对应的用户
            PidUser pidUser = pidUserMapper.selectByPid(pid);
            // 判断是否属于返利的PID
            if (pidUser != null && (pidUser.getType() == PidUser.TYPE_FANLI_ANDROID
                    || pidUser.getType() == PidUser.TYPE_FANLI_IOS)) {
                // 如果是订单失效就不处理
                // 2018-08-02开始生效
                if (isOrderValid(orderList))
                    if (TimeUtil.convertToTimeTemp(orderList.get(0).getCreateTime(), "yyyy-MM-dd HH:mm:ss") < TimeUtil
                            .convertToTimeTemp("2018-08-02", "yyyy-MM-dd"))
                        return;
 
                // 添加到订单中去
                Order order = new Order();
                order.setBeizhu("PID返利订单");
                order.setCreatetime(System.currentTimeMillis());
                order.setOrderId(orderId);
                order.setOrderType(1);
                order.setState(Order.STATE_YIZHIFU);
                order.setUserInfo(new UserInfo(pidUser.getUid()));
                order.setVersion(2);
 
                // 加入到订单表
                orderMapper.insertSelective(order);
                oldOrder = order;
            }
        }
 
        if (oldOrder != null) {// 该订单号可以返利
            // 原来失效的订单不再处理
            // if (oldOrder.getState() == Order.STATE_SHIXIAO)
            // return;
            // 更新订单的状态
            Order updateOrder = new Order();
            updateOrder.setId(oldOrder.getId());
            updateOrder.setThirdCreateTime(
                    new Date(TimeUtil.convertToTimeTemp(orderList.get(0).getCreateTime(), "yyyy-MM-dd HH:mm:ss")));
 
            if (isOrderValid(orderList)) {
                // 处理订单失效
                updateOrder.setState(Order.STATE_SHIXIAO);
                if (oldOrder.getState() == Order.STATE_YIZHIFU)
                    updateOrder.setDrawbackTime(System.currentTimeMillis());
                updateOrder.setThirdState("订单失效");
            } else// 计算订单的总金额
            {
                BigDecimal money = new BigDecimal(0);
                for (TaoBaoOrder o : orderList) {
                    if (o.getOrderState().equalsIgnoreCase("订单结算"))
                        money = money.add(o.getSettlement());
                    else {
                        money = money.add(o.getPayment());
                    }
                }
                updateOrder.setMoney(money);
                String thirdState = null;
                for (TaoBaoOrder o : orderList)
                    if (!o.getOrderState().equalsIgnoreCase("订单失效")) {
                        thirdState = o.getOrderState();
                        break;
                    }
                updateOrder.setThirdState(thirdState);
            }
            orderMapper.updateByPrimaryKeySelective(updateOrder);
            Order order = oldOrder;
            // 订单返利比例
            BigDecimal rate = hongBaoManageService.getFanLiRate();
 
            // 查找是否存在红包
            List<HongBao> hongBaoList = hongBaoMapper.selectByOid(order.getId());
            // 还没有加入有效的订单
            if ((hongBaoList == null || hongBaoList.size() == 0)) {
                for (TaoBaoOrder taoBaoOrder : orderList) {
                    // 创建OrderItem;
                    OrderItem orderItem = orderItemService.addOrderItem(taoBaoOrder, order, rate);
                    hongBaoService.addHongBao(order, taoBaoOrder, orderItem.getId());
                }
            } else if (hongBaoList.size() > 0)// 原来已经存在订单了
            {
                List<TaobaoOrderHongBao> sortList = HongBaoUtil.sortHongBaoAndTaoBaoOrder(orderList, hongBaoList);
                orderList = new ArrayList<>();
                hongBaoList = new ArrayList<>();
                if (sortList != null)
                    for (TaobaoOrderHongBao tb : sortList) {
                        orderList.add(tb.taoBaoOrder);
                        hongBaoList.add(tb.hongBao);
                    }
 
                if (orderList.size() != hongBaoList.size()) {
                    LogHelper.userOrder("红包数和订单数不对等:" + orderId);
                    return;
                }
 
                for (int i = 0; i < orderList.size(); i++) {
                    hongBaoService.updateHongBao(hongBaoList.get(i), orderList.get(i));
                    orderItemService.updateOrderItem(orderList.get(i), hongBaoList.get(i).getOrderItemId(), rate);
                }
            }
 
        }
    }
 
    @Override
    @Transactional
    public void fanli(HongBao hb) throws TaoBaoWeiQuanException {
        // 查询最新的红包数据
        hb = hongBaoMapper.selectByPrimaryKeyForUpdate(hb.getId());
        // 维权中的不返利
        List<TaoBaoWeiQuanOrder> taoBaoWeiQuanList = taoBaoWeiQuanOrderMapper
                .selectListByOrderIdAndState(hb.getOrderId(), "维权创建");
        if (taoBaoWeiQuanList != null && taoBaoWeiQuanList.size() > 0)
            return;
 
        taoBaoWeiQuanList = taoBaoWeiQuanOrderMapper.selectListByOrderIdAndState(hb.getOrderId(), "等待处理");
        if (taoBaoWeiQuanList != null && taoBaoWeiQuanList.size() > 0)
            return;
 
        long currentTimeMillis = System.currentTimeMillis();
        if (hb.getState() != HongBao.STATE_SHIXIAO) {
            // 设置为已返利
            // 处理主红包
            if (hb.getState() != HongBao.STATE_SHIXIAO && hb.getState() != HongBao.STATE_YILINGQU
                    && hb.getType() == HongBao.TYPE_TAOBAO) {
                OrderItem orderItem = null;
                if (hb.getOrderItemId() != null) {
                    orderItem = orderItemMapper.selectByPrimaryKey(hb.getOrderItemId());
                    OrderItem updateOrderItem = new OrderItem();
                    updateOrderItem.setId(orderItem.getId());
                    updateOrderItem.setFanTime(currentTimeMillis);
                    updateOrderItem.setState(OrderItem.STATE_DAOZHANG);
                    orderItemMapper.updateByPrimaryKeySelective(updateOrderItem);
                }
                // 如果红包没被领取
                HongBao updateHongBao = new HongBao();
                updateHongBao.setId(hb.getId());
                updateHongBao.setState(HongBao.STATE_YILINGQU);
                updateHongBao.setGetTime(currentTimeMillis);
                hongBaoMapper.updateByPrimaryKeySelective(updateHongBao);
                userInfoMapper.addHongBaoByUid(hb.getUserInfo().getId(), hb.getMoney());
 
                // ...消息、明细
                HongBaoExtra hongBaoExtra = HongBaoUtil.convert(hb);
                // 明细操作(如果服务器性好了后,可改成异步)
                MoneyRecord moneyRecord = new MoneyRecord(hb.getUserInfo(), hb, hb.getMoney(), hongBaoExtra.getTitle(),
                        "", currentTimeMillis, 1);
                moneyRecordMapper.insertSelective(moneyRecord);
                AccountDetails accountDetails = AccountDetailsFactory.create("+" + hb.getMoney(),
                        AccountDetailsFactory.FANLI, orderItem, null, hb.getUserInfo());
                accountDetailsMapper.insertSelective(accountDetails);
 
                // 添加资金明细与红包的映射关系
                accountDetailsHongBaoMapService.saveAccountDetailsHongBaoMap(hb.getId(), accountDetails.getId());
 
                // 维权扣款(2018-08-05后开始实行)
                if (hb.getBalanceTime() != null
                        && hb.getBalanceTime().getTime() >= TimeUtil.convertToTimeTemp("2018-08-05", "yyyy-MM-dd")) {
                    taoBaoWeiQuanDrawBackService.doWeiQuanFanli(hb.getOrderId());
                }
 
                // 站内信+推送
                try {
                    userNotificationService.orderFanliRecieved(hb.getUserInfo().getId(), orderItem.getOrderId(),
                            orderItem.getFanMoney());
                } catch (Exception e) {
                }
 
            }
        }
    }
 
    @Override
    public void weiQuanOrder(List<TaoBaoWeiQuanOrder> orderList) {
        if (orderList != null)
            for (TaoBaoWeiQuanOrder order : orderList) {
                if (order.getState().equalsIgnoreCase("维权成功")) {
                    CMQManager.getInstance().addWeiQuanOrderMsg(order);
                }
            }
    }
 
    @Transactional
    @Override
    public void weiQuanOrder(TaoBaoWeiQuanOrder order) {
        // 订单结算时间在2018-08-05后就不处理维权了
 
        List<TaoBaoOrder> orderList = taoBaoOrderService.getTaoBaoOrderByOrderId(order.getOrderId());
        if (orderList != null && orderList.size() > 0 & !StringUtil.isNullOrEmpty(orderList.get(0).getSettlementTime()))
            order.setJieSuanTime(
                    new Date(TimeUtil.convertToTimeTemp(orderList.get(0).getSettlementTime(), "yyyy-MM-dd HH:mm:ss")));
        if (order.getJieSuanTime().getTime() >= TimeUtil.convertToTimeTemp("2018-08-05", "yyyy-MM-dd")) {
            return;
        }
        try {
            orderService.failureOrderWithWeiQuan(order.getOrderId(), Order.ORDER_TYPE_TAOBAO);
        } catch (ObjectStateException e) {
            e.printStackTrace();
        }
        pidOrderService.weiQuan(order.getOrderId());
    }
 
    @Override
    public void fanliInvaiteAndShare() {
        // 查出邀请赚与分享赚该返利的用户ID
        List<Long> shareUids = pidOrderMapper.getUidCanBalanceList();
        List<Long> inviteUids = hongBaoMapper.selectUidCanBalanceHongBaoByChildHongBao();
        Set<Long> uidSets = new HashSet<>();
        if (shareUids != null)
            for (Long uid : shareUids)
                uidSets.add(uid);
        if (inviteUids != null)
            for (Long uid : inviteUids)
                uidSets.add(uid);
        // 根据用户ID
        Iterator<Long> its = uidSets.iterator();
        while (its.hasNext()) {
            Long uid = its.next();
            try {
                CMQManager.getInstance().addFanLiTiChengMsg(uid);
            } catch (Exception e) {
                e.printStackTrace();
                LogHelper.error(e);
            }
        }
    }
 
    private HongBao filterWeiQuanINGHongBao(HongBao hongBao) {
 
        List<TaoBaoWeiQuanOrder> taoBaoWeiQuanList = taoBaoWeiQuanOrderMapper
                .selectListByOrderIdAndState(hongBao.getOrderId(), "维权创建");
 
        List<TaoBaoWeiQuanOrder> taoBaoWeiQuanList1 = taoBaoWeiQuanOrderMapper
                .selectListByOrderIdAndState(hongBao.getOrderId(), "等待处理");
        if ((taoBaoWeiQuanList != null && taoBaoWeiQuanList.size() > 0)
                || (taoBaoWeiQuanList1 != null && taoBaoWeiQuanList1.size() > 0)) {
            return null;
        }
        return hongBao;
    }
 
    @Override
    @Transactional
    public void fanliInvaiteAndShare(Long uid) throws TaoBaoWeiQuanException {
        /**
         * 处理邀请赚订单
         */
        // 查询UID的邀请赚订单
        List<HongBao> hongBaoList = hongBaoMapper.selectCanBalanceHongBaoByChildHongBaoAndUid(uid);
 
        List<Long> hbIdList = new ArrayList<>();
 
        BigDecimal money = new BigDecimal(0);
        // 需要判断退款的订单号
        Set<String> drawBackOrders = new HashSet<String>();
        for (HongBao hongBao : hongBaoList) {
            hongBao = filterWeiQuanINGHongBao(hongBao);
            if (hongBao == null)
                continue;
 
            hongBao = hongBaoMapper.selectByPrimaryKeyForUpdate(hongBao.getId());
            if (hongBao.getState() == HongBao.STATE_BUKELINGQU || hongBao.getState() == HongBao.STATE_KELINGQU) {
                money = money.add(hongBao.getMoney());
                HongBao updateHongBao = new HongBao();
                updateHongBao.setId(hongBao.getId());
                updateHongBao.setGetTime(System.currentTimeMillis());
                updateHongBao.setState(HongBao.STATE_YILINGQU);
                hongBaoMapper.updateByPrimaryKeySelective(updateHongBao);
                // 添加到红包返利记录集合
                hbIdList.add(hongBao.getId());
                // 2018-08-05 过后的订单才处理维权
                Date balanceTime = null;
                if (hongBao.getParent() != null)
                    balanceTime = hongBao.getParent().getBalanceTime();
                else
                    balanceTime = hongBao.getBalanceTime();
                if (balanceTime != null
                        && balanceTime.getTime() >= TimeUtil.convertToTimeTemp("2018-08-05", "yyyy-MM-dd")) {
                    if (!StringUtil.isNullOrEmpty(hongBao.getOrderId()))
                        drawBackOrders.add(hongBao.getOrderId());
                    else if (hongBao.getParent() != null && !StringUtil.isNullOrEmpty(hongBao.getParent().getOrderId()))
                        drawBackOrders.add(hongBao.getParent().getOrderId());
                }
            }
        }
 
        /**
         * 处理顶级分享赚
         */
 
        // 查询UID的分享赚订单
        List<PidOrder> pidOrderList = pidOrderMapper.getCanBalanceListByUid(uid);
        for (PidOrder pidOrder : pidOrderList) {
            HongBao hongBao = hongBaoMapper.selectByPrimaryKeyForUpdate(pidOrder.getHongBao().getId());
            hongBao = filterWeiQuanINGHongBao(hongBao);
            if (hongBao == null)
                continue;
            if (hongBao.getState() == HongBao.STATE_BUKELINGQU || hongBao.getState() == HongBao.STATE_KELINGQU) {
                money = money.add(hongBao.getMoney());
                HongBao updateHongBao = new HongBao();
                updateHongBao.setId(hongBao.getId());
                updateHongBao.setGetTime(System.currentTimeMillis());
                updateHongBao.setState(HongBao.STATE_YILINGQU);
                hongBaoMapper.updateByPrimaryKeySelective(updateHongBao);
                // 添加到红包返利记录集合
                hbIdList.add(hongBao.getId());
                // 2018-08-05 过后的订单才处理维权
                Date balanceTime = null;
                if (hongBao.getParent() != null)
                    balanceTime = hongBao.getParent().getBalanceTime();
                else
                    balanceTime = hongBao.getBalanceTime();
                if (balanceTime != null
                        && balanceTime.getTime() >= TimeUtil.convertToTimeTemp("2018-08-05", "yyyy-MM-dd")) {
                    drawBackOrders.add(pidOrder.getOrderId());
                }
            }
        }
 
        /**
         * 处理一级二级分享赚
         */
 
        List<HongBao> totalHongBaoList = new ArrayList<>();
        // 查询UID的二级或者三级分享赚订单
        // TODO 暂时查询10000条数据,后面再做分页
        List<HongBao> hbList = hongBaoMapper.selectCanBalanceHongBaoByTypeAndUid(HongBao.TYPE_SHARE_YIJI, uid, 10000);
 
        if (hbList != null && hbList.size() > 0)
            totalHongBaoList.addAll(hbList);
 
        hbList = hongBaoMapper.selectCanBalanceHongBaoByTypeAndUid(HongBao.TYPE_SHARE_ERJI, uid, 10000);
        if (hbList != null && hbList.size() > 0)
            totalHongBaoList.addAll(hbList);
 
        for (HongBao hb : totalHongBaoList) {
            if (hb.getState() == HongBao.STATE_BUKELINGQU || hb.getState() == HongBao.STATE_KELINGQU) {
                hb = filterWeiQuanINGHongBao(hb);
                if (hb == null)
                    continue;
                money = money.add(hb.getMoney());
                HongBao updateHongBao = new HongBao();
                updateHongBao.setId(hb.getId());
                updateHongBao.setGetTime(System.currentTimeMillis());
                updateHongBao.setState(HongBao.STATE_YILINGQU);
                hongBaoMapper.updateByPrimaryKeySelective(updateHongBao);
                // 添加到红包返利记录集合
                hbIdList.add(hb.getId());
                if (!StringUtil.isNullOrEmpty(hb.getOrderId()))
                    drawBackOrders.add(hb.getOrderId());
            }
        }
 
        /**
         * 增加用户资金记录
         */
 
        if (money.compareTo(new BigDecimal(0)) > 0) {
            userInfoMapper.addHongBaoByUid(uid, money);
            // 添加记录
            AccountDetails accountDetails = AccountDetailsFactory.create("+" + money, AccountDetailsFactory.TICHENG,
                    null, null, new UserInfo(uid));
            accountDetailsMapper.insertSelective(accountDetails);
            // 记录返利红包与资金详情的对应关系
 
            // 添加到红包返利记录集合
            accountDetailsHongBaoMapService.saveAccountDetailsHongBaoMap(hbIdList, accountDetails.getId());
 
            // 发送推送
            try {
                // 提成到账消息通知
                userNotificationService.tiChengRecieved(uid, money);
            } catch (Exception e) {
                e.printStackTrace();
            }
            for (String orderId : drawBackOrders)
                taoBaoWeiQuanDrawBackService.doWeiQuanShare(orderId);
        }
    }
 
}