yujian
2019-04-22 90050a9e07429e9692187fdb0b68ab608ddb6400
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
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
package com.yeshi.fanli.service.impl.order;
 
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
import javax.annotation.Resource;
 
import org.apache.commons.beanutils.PropertyUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.yeshi.utils.taobao.TbImgUtil;
 
import com.yeshi.fanli.dao.mybatis.order.CommonOrderGoodsMapper;
import com.yeshi.fanli.dao.mybatis.order.CommonOrderMapper;
import com.yeshi.fanli.dao.mybatis.taobao.TaoBaoWeiQuanOrderMapper;
import com.yeshi.fanli.entity.bus.user.HongBaoV2;
import com.yeshi.fanli.entity.bus.user.UserInfo;
import com.yeshi.fanli.entity.bus.user.UserSystemCouponRecord;
import com.yeshi.fanli.entity.order.CommonOrder;
import com.yeshi.fanli.entity.order.CommonOrderGoods;
import com.yeshi.fanli.entity.system.SystemCoupon.CouponTypeEnum;
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.CommonOrderException;
import com.yeshi.fanli.exception.taobao.TaobaoGoodsDownException;
import com.yeshi.fanli.service.inter.common.JumpDetailV2Service;
import com.yeshi.fanli.service.inter.config.ConfigService;
import com.yeshi.fanli.service.inter.config.SystemCouponService;
import com.yeshi.fanli.service.inter.hongbao.HongBaoV2Service;
import com.yeshi.fanli.service.inter.order.CommonOrderService;
import com.yeshi.fanli.service.inter.user.UserSystemCouponRecordService;
import com.yeshi.fanli.service.inter.user.UserSystemCouponService;
import com.yeshi.fanli.util.Constant;
import com.yeshi.fanli.util.MoneyBigDecimalUtil;
import com.yeshi.fanli.util.StringUtil;
import com.yeshi.fanli.util.factory.CommonOrderGoodsFactory;
import com.yeshi.fanli.util.taobao.TaoBaoOrderUtil;
import com.yeshi.fanli.util.taobao.TaoBaoUtil;
import com.yeshi.fanli.util.taobao.TaoKeApiUtil;
import com.yeshi.fanli.vo.msg.ClientTextStyleVO;
import com.yeshi.fanli.vo.order.CommonOrderGoodsVO;
import com.yeshi.fanli.vo.order.CommonOrderVO;
 
import net.sf.json.JSONObject;
 
@Service
public class CommonOrderServiceImpl implements CommonOrderService {
 
    @Resource
    private CommonOrderMapper commonOrderMapper;
 
    @Resource
    private TaoBaoWeiQuanOrderMapper taoBaoWeiQuanOrderMapper;
 
    @Resource
    private CommonOrderGoodsMapper commonOrderGoodsMapper;
    
    @Resource
    private JumpDetailV2Service jumpDetailV2Service;
    
    @Resource
    private ConfigService configService;
    
    @Resource
    private UserSystemCouponService userSystemCouponService;
    
    @Resource
    private UserSystemCouponRecordService userSystemCouponRecordService;
    
    @Resource
    private SystemCouponService systemCouponService;
    
    @Resource
    private HongBaoV2Service hongBaoV2Service;
    
    
    // 奖励订单图片
    public final static String PIC_REWARD= "http://img.flqapp.com/resource/order/order_state_reward.png";
    
    // 免单状态图片
    public final static String PIC_FREE_ON = "http://img.flqapp.com/resource/order/order_state_freeing.png";
    public final static String PIC_FREE_FAIL =  "http://img.flqapp.com/resource/order/order_state_free_fail.png";
    public final static String PIC_FREE_SUCCEED =  "http://img.flqapp.com/resource/order/order_state_free_suc.png";
 
 
    @Override
    public int insert(CommonOrder record) {
        return commonOrderMapper.insert(record);
    }
 
    @Override
    public int insertSelective(CommonOrder record) {
        return commonOrderMapper.insertSelective(record);
    }
 
    @Override
    public int updateByPrimaryKeySelective(CommonOrder record) {
        return commonOrderMapper.updateByPrimaryKeySelective(record);
    }
 
    @Override
    public int updateByPrimaryKey(CommonOrder record) {
        return commonOrderMapper.updateByPrimaryKey(record);
    }
 
    @Override
    public int deleteByPrimaryKey(Long id) {
        return commonOrderMapper.deleteByPrimaryKey(id);
    }
 
    @Override
    public CommonOrder selectByPrimaryKey(Long id) {
        return commonOrderMapper.selectByPrimaryKey(id);
    }
 
    @Override
    public List<CommonOrderVO> listGroupOrderNoByUid(long start, int count, Long uid, Integer state, Integer type,
            Integer orderState, String orderNo, String startTime, String endTime, Integer dateType)
            throws CommonOrderException {
        return commonOrderMapper.listUserOrder(start, count, uid, state, type, orderState, orderNo, startTime, endTime,
                dateType);
    }
 
    @Override
    public long countGroupOrderNoByUid(Long uid, Integer state, Integer type, Integer orderState, String orderNo,
            String startTime, String endTime, Integer dateType) throws CommonOrderException {
        return commonOrderMapper.countUserOrder(uid, state, type, orderState, orderNo, startTime, endTime, dateType);
    }
 
    @Override
    public List<CommonOrderVO> getOrderByUid(Integer page, Long uid, Integer state, Integer type, Integer orderState,
            String orderNo, String startTime, String endTime, Integer dateType) throws CommonOrderException {
 
        int pageSize = Constant.PAGE_SIZE;
 
        List<CommonOrderVO> listOrder = listGroupOrderNoByUid((page - 1) * pageSize, pageSize, uid, state, type,
                orderState, orderNo, startTime, endTime, dateType);
 
        // 订单信息为空
        if (listOrder == null || listOrder.size() == 0) {
            listOrder = new ArrayList<CommonOrderVO>();
            return listOrder;
        }
 
        // 商品信息
        List<CommonOrderVO> listGoods = commonOrderMapper.listOrderGoodsInfo(listOrder);
        // 订单商品为空
        if (listGoods == null || listGoods.size() == 0) {
            return listOrder;
        }
 
        // 数据加工重新组织
        listDataFactory(listOrder, listGoods, uid);
 
        return listOrder;
    }
    
    
    @Override
    public Map<String, BigDecimal> countHistoryOrder(Long uid, Integer day) {
        return commonOrderMapper.countHistoryOrder(uid, day);
    }
 
    @Override
    public long countBonusOrderNumber(Long uid, Integer type, Integer day, String startTime, String endTime) {
        return commonOrderMapper.countBonusOrderNumber(uid, type, day, startTime, endTime);
    }
 
    @Override
    public BigDecimal countBonusOrderMoney(Long uid, Integer type, Integer day, String startTime, String endTime) {
        return commonOrderMapper.countBonusOrderMoney(uid, type, day, startTime, endTime);
    }
 
    @Override
    public Map<String, Object> countBonusOrderMoneyAndNumber(Long uid, Integer type, Integer day, String startTime,
            String endTime) {
        return commonOrderMapper.countBonusOrderMoneyAndNumber(uid, type, day, startTime, endTime);
    }
 
    @Override
    public Map<String, BigDecimal> countByUidAndOrderState(Long uid, Integer type, String startTime, String endTime,
            Integer day) {
        return commonOrderMapper.countByUidAndOrderState(uid, type, startTime, endTime, day);
    }
 
    public void listDataFactory(List<CommonOrderVO> listOrder, List<CommonOrderVO> listGoods, Long uid) {
 
        List<String> listNo = new ArrayList<String>();
        for (CommonOrderVO commonOrderVO:  listOrder) {
            listNo.add(commonOrderVO.getOrderNo());
        }
        
        // 已经使用券订单
        List<UserSystemCouponRecord> couponRecordList = userSystemCouponRecordService.getRecordByOrderNoList(listNo);
        
        
        /* 组合商品信息 */
        for (CommonOrderVO commonOrder : listGoods) {
 
            CommonOrderGoods goods = commonOrder.getCommonOrderGoods();
            if (goods == null) {
                continue;
            }
 
            String orderNo1 = commonOrder.getOrderNo();
            Integer sourceType = commonOrder.getSourceType();
 
            for (CommonOrderVO order : listOrder) {
                String orderNo2 = order.getOrderNo();
                Integer sourceType2 = order.getSourceType();
 
                // 来源、订单号相同
                if (sourceType.equals(sourceType2) && orderNo1.equals(orderNo2)) {
                    // 加入商品信息
                    List<CommonOrderGoodsVO> listOrderGoods = order.getListOrderGoods();
 
                    CommonOrderGoodsVO commonGoodsVO = new CommonOrderGoodsVO();
                    try {
                        PropertyUtils.copyProperties(commonGoodsVO, goods);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
 
                    String picture = commonGoodsVO.getPicture();
                    if (!StringUtil.isNullOrEmpty(picture) && !picture.contains("320x320")) {
                        commonGoodsVO.setPicture(TbImgUtil.getTBSize320Img(picture));
                    }
 
                    Integer hongBaoType = order.getHongBaoType();
                    // 邀请订单信息保护
                    if (HongBaoV2.TYPE_YAOQING == hongBaoType || HongBaoV2.TYPE_YIJI == hongBaoType
                            || HongBaoV2.TYPE_ERJI == hongBaoType || HongBaoV2.TYPE_SHARE_YIJI == hongBaoType
                            || HongBaoV2.TYPE_SHARE_ERJI == hongBaoType) {
                        Map<String, String> titleMap = new HashMap<String, String>();
                        titleMap.put("content", "为保障用户隐私,商品信息已隐藏!");
                        titleMap.put("fontColor", "#888888");
                        titleMap.put("bottomColor", "#E9E9E9");
                        commonGoodsVO.setTitle(null);
                        commonGoodsVO.setGoodsTitle(titleMap);
                    }
 
                    // 购买数量
                    commonGoodsVO.setActualCount(commonOrder.getTotalCount() + "件");
 
                    BigDecimal totalSettlement = commonOrder.getTotalSettlement();
                    if (totalSettlement == null || totalSettlement.compareTo(new BigDecimal(0)) <= 0) {
                        totalSettlement = commonOrder.getTotalPayment();
                    }
                    // 实付款
                    commonGoodsVO.setActualPay("付款金额:¥" + totalSettlement);
 
                    listOrderGoods.add(commonGoodsVO);
 
                    Integer orderType = commonOrder.getOrderType();
                    if (orderType == null) {
                        String shopType = commonGoodsVO.getShopType();
                        if (CommonOrderGoodsVO.TYPE_TAOBAO.equalsIgnoreCase(shopType)) {
                            commonOrder.setOrderType(1);
                        } else if (CommonOrderGoodsVO.TYPE_TMALL.equalsIgnoreCase(shopType)) {
                            commonOrder.setOrderType(2);
                        }
                    }
 
                    break;
                }
            }
        }
 
        SimpleDateFormat format = new SimpleDateFormat("yyyy.MM.dd HH:mm");
        SimpleDateFormat formatday = new SimpleDateFormat("yyyy.MM.dd");
        
        // 是否存在奖励券
        boolean hasRewardCoupon = userSystemCouponService.hasRewardCoupon(uid);
        
        /* 组织订单状态 等信息 */
        for (CommonOrderVO order : listOrder) {
 
            Date thirdCreateTime = order.getThirdCreateTime();
            if (thirdCreateTime != null) {
                order.setDownTime("下单时间:" + format.format(thirdCreateTime));
                order.setObtainTime(thirdCreateTime.getTime());
            }
 
            Date settleTime = order.getSettleTime();
            if (settleTime != null) {
                order.setReceiveTime("收货时间:" + format.format(settleTime));
            }
 
            /* 订单状态 转换处理 */
            String orderStateContent = "";
            Map<String, String> orderStateMap = new HashMap<String, String>();
 
            // 订单状态
            Integer orderState = order.getState();
            // 红包状态
            Integer hongBaoState = order.getHongBaoState();
 
            Integer stateWholeOrder = order.getStateWholeOrder();
            if (CommonOrder.STATE_WHOLE_ORDER_BUFENYOUXIAO == stateWholeOrder) {
                // 整个订单部分失效:判断真实状态 订单、红包
                CommonOrderVO buFenOrder = commonOrderMapper.getBuFenOrderState(uid, order.getOrderNo());
                if (buFenOrder != null) {
                    // 有效的订单状态
                    orderState = buFenOrder.getState();
                    // 有效的红包状态
                    hongBaoState = buFenOrder.getHongBaoState();
                }
            }
 
            if (CommonOrder.STATE_FK == orderState) {
                orderStateContent = "已付款";
            } else if (CommonOrder.STATE_JS == orderState) {
                orderStateContent = "已收货";
            } else if (CommonOrder.STATE_SX == orderState) {
                orderStateContent = "未付款/已退款";
            } else if (CommonOrder.STATE_WQ == orderState) {
                orderStateContent = "已维权";
 
                /* 订单维权 判断是否全部维权 */
                List<TaoBaoWeiQuanOrder> listWQ = taoBaoWeiQuanOrderMapper
                        .selectListByOrderIdAndState(order.getOrderNo(), "维权成功");
 
                boolean isPart = false;// 默认失效
 
                if (listWQ != null && listWQ.size() > 0) {
                    BigDecimal fanTotalMoney = new BigDecimal(0);
                    for (TaoBaoWeiQuanOrder weiQuanOrder : listWQ) {
                        BigDecimal fanMoney = weiQuanOrder.getFanMoney();
                        if (fanMoney != null) {
                            fanTotalMoney = MoneyBigDecimalUtil.add(fanTotalMoney, fanMoney);
                        }
                    }
 
                    if (fanTotalMoney.compareTo(order.getHongBao()) < 0) {
                        isPart = true;
                    }
                }
 
                if (isPart) {
                    hongBaoState = HongBaoV2.STATE_BUFENSHIXIAO; // 部分失效
                } else {
                    hongBaoState = HongBaoV2.STATE_SHIXIAO; // 全部失效
                }
            }
            orderStateMap.put("content", orderStateContent);
            orderStateMap.put("fontColor", "#666666");
            order.setOrderState(orderStateMap);
 
            String hongbaoInfo = "";
            // 订单标识
            List<String> signList = new ArrayList<String>();
            /* 订单返利类型 转换 */
            Integer hongBaoType = order.getHongBaoType();
            if (HongBaoV2.TYPE_ZIGOU == hongBaoType || 2 == hongBaoType) {
                // 自购
                hongbaoInfo = "返利";
                order.setOrderOrigin("1");
                order.setHongBaoTypePic(CommonOrder.TYPE_FANLI);
                signList.add(CommonOrder.TYPE_FANLI);
            } else if (HongBaoV2.TYPE_SHARE_GOODS == hongBaoType) {
                // 分享
                hongbaoInfo = "奖金";
                order.setOrderOrigin("2");
                order.setHongBaoTypePic(CommonOrder.TYPE_SHARE);
                signList.add(CommonOrder.TYPE_SHARE);
                
            } else if (HongBaoV2.TYPE_YAOQING == hongBaoType || HongBaoV2.TYPE_YIJI == hongBaoType
                    || HongBaoV2.TYPE_ERJI == hongBaoType || HongBaoV2.TYPE_SHARE_YIJI == hongBaoType
                    || HongBaoV2.TYPE_SHARE_ERJI == hongBaoType) {
                // 邀请
                hongbaoInfo = "奖金";
                order.setOrderOrigin("3");
                order.setHongBaoTypePic(CommonOrder.TYPE_INVITE);
                signList.add(CommonOrder.TYPE_INVITE);
            }
 
            String hongBaoDate = null;
            String hongBaoState_Str = "";
            String hongbaoInfoFontColor = "#E5005C";
 
            /* 红包状态 转换 */
            String stateContent = "";
            String stateFontColor = "#E5005C";
            Integer orderHongBaoState = null;
 
            if (HongBaoV2.STATE_KELINGQU == hongBaoState || HongBaoV2.STATE_BUKELINGQU == hongBaoState) {
                orderHongBaoState = 1;
                stateContent = "未到账";
                hongBaoState_Str = "预估";
 
                Date preAccountTime = order.getPreAccountTime();
                if (preAccountTime != null) {
                    hongBaoDate = "预计到账时间:" + formatday.format(preAccountTime);
                }
 
            } else if (HongBaoV2.STATE_YILINGQU == hongBaoState) {
                orderHongBaoState = 3;
                stateContent = "已到账";
 
                Date accountTime = order.getAccountTime();
                if (accountTime != null) {
                    hongBaoDate = "到账时间:" + formatday.format(accountTime);
                }
 
            } else if (HongBaoV2.STATE_BUFENSHIXIAO == hongBaoState) {
                stateContent = "部分失效";
                orderHongBaoState = 3;
                Date accountTime = order.getAccountTime();
                if (accountTime != null) {
                    hongBaoDate = "到账时间:" + formatday.format(accountTime);
                }
 
            } else if (HongBaoV2.STATE_SHIXIAO == hongBaoState) {
                orderHongBaoState = 4;
                stateContent = "已失效";
                hongbaoInfoFontColor = "#888888";
            }
            order.setHongBaoState(orderHongBaoState);
            order.setHongBaoDate(hongBaoDate);
 
            Map<String, String> stateMap = new HashMap<String, String>();
            stateMap.put("content", stateContent);
            stateMap.put("fontColor", stateFontColor);
            order.setAccountState(stateMap);
 
            hongbaoInfo = hongBaoState_Str + hongbaoInfo;
 
            BigDecimal hongBao = order.getHongBao();
            if (hongBao == null) {
                hongBao = new BigDecimal(0);
            }
            Map<String, String> hongBaoMap = new HashMap<String, String>();
            hongBaoMap.put("content", hongbaoInfo + " ¥" + hongBao.setScale(2, BigDecimal.ROUND_DOWN).toString());
            hongBaoMap.put("fontColor", hongbaoInfoFontColor);
            order.setHongBaoInfo(hongBaoMap);
            
            // 非自购的订单 不显示返利、免单详情
            if (HongBaoV2.TYPE_ZIGOU != hongBaoType) {
                // 订单标识
                order.setSignList(signList);
                
            } else {
            
                // 是否免单商品
                boolean freeOrder = false;
                // 是否奖励成功
                boolean rewardSuccess = false;
                
                // 奖励订单、免单 使用记录
                if (couponRecordList != null && couponRecordList.size() > 0) {
                    
                    for (UserSystemCouponRecord couponRecord: couponRecordList) {
                        // 订单号匹配
                        if (order.getOrderNo().equals(couponRecord.getOrderNo())) {
                            
                            String systemCouponType = couponRecord.getCouponType();
                            Integer state = couponRecord.getState();
                            
                            // 奖励订单: 且成功
                            if (CouponTypeEnum.rebatePercentCoupon.name().equals(systemCouponType)
                                    && UserSystemCouponRecord.STATE_SUCCESS == state) {
                                rewardSuccess = true;
                                signList.add(PIC_REWARD); // 加入奖励成功图片
                                
                            } else if (CouponTypeEnum.freeCoupon.name().equals(systemCouponType)
                                    || CouponTypeEnum.welfareFreeCoupon.name().equals(systemCouponType)) {
                                
                                if (UserSystemCouponRecord.STATE_FREE_ON == state) {
                                    // 免单中
                                    freeOrder = true;
                                    signList.add(PIC_FREE_ON);
                                } else if (UserSystemCouponRecord.STATE_SUCCESS == state) {
                                    // 免单成功
                                    freeOrder = true;
                                    signList.add(PIC_FREE_SUCCEED);
                                } else if (UserSystemCouponRecord.STATE_FAIL_RULE == state || UserSystemCouponRecord.STATE_FAIL_DRAWBACK == state) {
                                    // 规则不匹配、退款
                                    freeOrder = true;
                                    signList.add(PIC_FREE_FAIL);
                                } 
                            }
                            
                            break;
                        }
                    }
                }
                
                // 订单标识
                order.setSignList(signList);
                
                boolean rewardOrder = false;
                
                int type = 1; // 1 常规跳转页面  2弹出选项(奖励券)
                if (rewardSuccess) {
                    // 奖励成功
                    rewardOrder = true; 
                } else if(hasRewardCoupon && !freeOrder && HongBaoV2.TYPE_ZIGOU == hongBaoType &&
                        HongBaoV2.STATE_YILINGQU == hongBaoState) {
                    // 有可用的奖励券 、非免单订单 、且已到账的 、返利订单
                    type = 2;
                    rewardOrder = true; 
                }
                
                if (rewardOrder) {  
                    ClientTextStyleVO rewardStyleVO = new ClientTextStyleVO();
                    // 奖励券进度详情
                    rewardStyleVO.setContent("返利奖励 >");
                    rewardStyleVO.setColor( "#E5005C");
                    rewardStyleVO.setBottomColor("#FFDCEA");
                    
                    Map<String, Object> jumpLink = new HashMap<String,Object>();
                    jumpLink.put("orderNo", order.getOrderNo());
                    
                    Map<String, Object> jump = new HashMap<String,Object>();
                    jump.put("type", type); 
                    jump.put("params", jumpLink);
                    jump.put("jumpDetail", jumpDetailV2Service.getByTypeCache("rewardCouponDetail"));
                    
                    Map<String, Object> rewardMap = new HashMap<String,Object>();
                    rewardMap.put("text", rewardStyleVO);
                    rewardMap.put("jump", jump);
                    
                    order.setRewardDetail(rewardMap);
                }
                
                if (freeOrder) {  
                    ClientTextStyleVO rewardStyleVO = new ClientTextStyleVO();
                    // 返利、免单详情
                    rewardStyleVO.setContent("免单详情 >");
                    rewardStyleVO.setColor( "#E5005C");
                    rewardStyleVO.setBottomColor("#FFDCEA");
                    
                    Map<String, Object> jumpLink = new HashMap<String,Object>();
                    jumpLink.put("orderNo", order.getOrderNo());
                    
                    Map<String, Object> jump = new HashMap<String,Object>();
                    jump.put("type", 1); // 页面跳转
                    jump.put("params", jumpLink);
                    jump.put("jumpDetail", jumpDetailV2Service.getByTypeCache("freeCouponDetail"));
                    
                    Map<String, Object> rewardMap = new HashMap<String,Object>();
                    rewardMap.put("text", rewardStyleVO);
                    rewardMap.put("jump", jump);
                    
                    order.setRewardDetail(rewardMap);
                }
            }
        }
    }
 
    @Override
    public List<CommonOrder> listBySourceTypeAndOrderId(int sourceType, String orderId) {
        return commonOrderMapper.listBySourceTypeAndOrderNo(sourceType, orderId);
    }
 
    @Transactional
    @Override
    public List<CommonOrder> addTaoBaoOrder(List<TaoBaoOrder> taoBaoOrders, Long uid) throws CommonOrderException {
        List<CommonOrder> commonOrderList = new ArrayList<>();
        // 判断所有的订单状态
        int invalidCount = 0;
        for (TaoBaoOrder tb : taoBaoOrders) {
            if ("订单失效".equalsIgnoreCase(tb.getOrderState())) {
                invalidCount++;
            }
        }
 
        // 获取整体订单的状态
        int wholeOrderState = 0;
        if (taoBaoOrders.size() == invalidCount)
            wholeOrderState = CommonOrder.STATE_WHOLE_ORDER_SHIXIAO;
        else if (invalidCount == 0)
            wholeOrderState = CommonOrder.STATE_WHOLE_ORDER_YOUXIAO;
        else
            wholeOrderState = CommonOrder.STATE_WHOLE_ORDER_BUFENYOUXIAO;
 
        for (TaoBaoOrder taoBaoOrder : taoBaoOrders) {
            CommonOrder commonOrder = commonOrderMapper.selectByOrderNoAndOrderTypeAndOrderBy(taoBaoOrder.getOrderId(),
                    Constant.SOURCE_TYPE_TAOBAO, taoBaoOrder.getOrderBy());
            if (commonOrder == null)// 新增
            {
                commonOrder = TaoBaoOrderUtil.convert(taoBaoOrder);
                CommonOrderGoods goods = new CommonOrderGoods();
                goods.setGoodsId(taoBaoOrder.getAuctionId() + "");
                goods.setGoodsType(Constant.SOURCE_TYPE_TAOBAO);
 
                // 商品是否已经插入
                List<CommonOrderGoods> commonGoodsList = commonOrderGoodsMapper
                        .listByGoodsIdAndGoodsType(goods.getGoodsId(), goods.getGoodsType());
                if (commonGoodsList.size() <= 0)// 不存在就插入商品
                {
                    TaoBaoGoodsBrief taoBaoGoods = null;
                    try {
                        taoBaoGoods = TaoKeApiUtil.getSimpleGoodsInfo(taoBaoOrder.getAuctionId());
                    } catch (TaobaoGoodsDownException e) {
                        e.printStackTrace();
                        taoBaoGoods=TaoBaoUtil.getSimpleGoodsBrief(taoBaoOrder.getAuctionId());
                    }
                    if (taoBaoGoods != null) {
                        goods = CommonOrderGoodsFactory.create(taoBaoGoods);
                    }
                    goods.setCreateTime(new Date());
                    goods.setUpdateTime(new Date());
                    commonOrderGoodsMapper.insertSelective(goods);
                } else
                    goods.setId(commonGoodsList.get(commonGoodsList.size() - 1).getId());
                commonOrder.setCommonOrderGoods(goods);
                commonOrder.setUserInfo(new UserInfo(uid));
                commonOrder.setCreateTime(new Date());
                commonOrder.setStateWholeOrder(wholeOrderState);
                commonOrderMapper.insertSelective(commonOrder);
 
                commonOrderList.add(commonOrder);
                continue;
            } else {// 修改
                // 已经结算或者已经失效的订单不处理
                if (commonOrder.getState() == CommonOrder.STATE_JS || commonOrder.getState() == CommonOrder.STATE_SX) {
                    commonOrderList.add(commonOrder);
                    continue;
                }
                CommonOrder newCommonOrder = TaoBaoOrderUtil.convert(taoBaoOrder);
                CommonOrder updateCommonOrder = new CommonOrder(commonOrder.getId());
                updateCommonOrder.seteIncome(newCommonOrder.geteIncome());
                updateCommonOrder.setEstimate(newCommonOrder.getEstimate());
                updateCommonOrder.setPayment(newCommonOrder.getPayment());
                updateCommonOrder.setSettlement(newCommonOrder.getSettlement());
                updateCommonOrder.setSettleTime(newCommonOrder.getSettleTime());
                updateCommonOrder.setState(newCommonOrder.getState());
                updateCommonOrder.setStateWholeOrder(wholeOrderState);
                updateCommonOrder.setUpdateTime(new Date());
                updateCommonOrder.setTradeId(newCommonOrder.getTradeId());
                commonOrderMapper.updateByPrimaryKeySelective(updateCommonOrder);
                commonOrderList.add(commonOrderMapper.selectByPrimaryKey(updateCommonOrder.getId()));
                continue;
            }
        }
        return commonOrderList;
    }
 
    @Override
    public long countByUidAndOrderStateWithOrderBalanceTime(Long uid, int state, Date minDate, Date maxDate) {
        return commonOrderMapper.countByUidAndOrderStateWithOrderBalanceTime(uid, state, minDate, maxDate);
    }
 
    @Override
    public List<CommonOrderVO> listQueryByUid(long start, int count, Long uid, Integer state, Integer type,
            Integer orderState, String orderNo, String startTime, String endTime, Integer dateType)
            throws CommonOrderException {
 
        List<CommonOrderVO> list = commonOrderMapper.listQueryByUid(start, count, uid, state, type, orderState, orderNo,
                startTime, endTime, dateType);
 
        if (list == null) {
            list = new ArrayList<CommonOrderVO>();
        }
 
        if (list.size() == 0) {
            return list;
        }
 
        SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        for (CommonOrderVO order : list) {
 
            // 下单时间
            Date thirdCreateTime = order.getThirdCreateTime();
            if (thirdCreateTime != null) {
                order.setDownTime(format.format(thirdCreateTime));
            }
            // 收货时间
            Date settleTime = order.getSettleTime();
            if (settleTime != null) {
                order.setReceiveTime(format.format(settleTime));
            }
 
            // 到账时间
            Date accountTime = order.getAccountTime();
            if (accountTime != null) {
                order.setHongBaoDate(format.format(accountTime));
            }
 
            BigDecimal settlement = order.getSettlement();
            if (settlement != null && settlement.compareTo(new BigDecimal(0)) > 0) {
                order.setPayment(settlement); // 实际付款金额
            }
 
            /* 订单返利类型 转换 */
            Integer hongBaoType = order.getHongBaoType();
            if (HongBaoV2.TYPE_ZIGOU == hongBaoType || 2 == hongBaoType) {
                // 自购
                order.setHongBaoType(1);
            } else if (HongBaoV2.TYPE_SHARE_GOODS == hongBaoType) {
                // 分享
                order.setHongBaoType(2);
            } else if (HongBaoV2.TYPE_YAOQING == hongBaoType || HongBaoV2.TYPE_YIJI == hongBaoType
                    || HongBaoV2.TYPE_ERJI == hongBaoType || HongBaoV2.TYPE_SHARE_YIJI == hongBaoType
                    || HongBaoV2.TYPE_SHARE_ERJI == hongBaoType) {
                // 邀请
                order.setHongBaoType(3);
            }
 
            Integer hongBaoState = order.getHongBaoState();
            if (HongBaoV2.STATE_KELINGQU == hongBaoState || HongBaoV2.STATE_BUKELINGQU == hongBaoState) {
                // 未到账
                order.setHongBaoState(1);
 
            } else if (HongBaoV2.STATE_YILINGQU == hongBaoState) {
                // 已到账
                order.setHongBaoState(2);
 
            } else if (HongBaoV2.STATE_SHIXIAO == hongBaoState) {
                // 已失效
                order.setHongBaoState(3);
            }
 
        }
 
        return list;
 
    }
 
    @Override
    public long countQueryByUid(Long uid, Integer state, Integer type, Integer orderState, String orderNo,
            String startTime, String endTime, Integer dateType) throws CommonOrderException {
        return commonOrderMapper.countQueryByUid(uid, state, type, orderState, orderNo, startTime, endTime, dateType);
    }
 
    @Override
    public List<CommonOrderVO> listQuery(long start, int count, Integer keyType, String key, Integer state,
            Integer type, Integer orderState, String startTime, String endTime) throws CommonOrderException {
 
        List<CommonOrderVO> list = commonOrderMapper.listQuery(start, count, keyType, key, state, type, orderState,
                startTime, endTime);
 
        if (list == null) {
            list = new ArrayList<CommonOrderVO>();
        }
 
        if (list.size() == 0) {
            return list;
        }
 
        SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        for (CommonOrderVO order : list) {
 
            // 下单时间
            Date thirdCreateTime = order.getThirdCreateTime();
            if (thirdCreateTime != null) {
                order.setDownTime(format.format(thirdCreateTime));
            }
            // 收货时间
            Date settleTime = order.getSettleTime();
            if (settleTime != null) {
                order.setReceiveTime(format.format(settleTime));
            }
 
            // 到账时间
            Date accountTime = order.getAccountTime();
            if (accountTime != null) {
                order.setHongBaoDate(format.format(accountTime));
            }
 
            BigDecimal settlement = order.getSettlement();
            if (settlement != null && settlement.compareTo(new BigDecimal(0)) > 0) {
                order.setPayment(settlement); // 实际付款金额
            }
 
            /* 订单返利类型 转换 */
            Integer hongBaoType = order.getHongBaoType();
            if (HongBaoV2.TYPE_ZIGOU == hongBaoType || 2 == hongBaoType) {
                // 自购
                order.setHongBaoType(1);
            } else if (HongBaoV2.TYPE_SHARE_GOODS == hongBaoType) {
                // 分享
                order.setHongBaoType(2);
            } else if (HongBaoV2.TYPE_YAOQING == hongBaoType || HongBaoV2.TYPE_YIJI == hongBaoType
                    || HongBaoV2.TYPE_ERJI == hongBaoType || HongBaoV2.TYPE_SHARE_YIJI == hongBaoType
                    || HongBaoV2.TYPE_SHARE_ERJI == hongBaoType) {
                // 邀请
                order.setHongBaoType(3);
            }
 
            Integer hongBaoState = order.getHongBaoState();
            if (HongBaoV2.STATE_KELINGQU == hongBaoState || HongBaoV2.STATE_BUKELINGQU == hongBaoState) {
                // 未到账
                order.setHongBaoState(1);
 
            } else if (HongBaoV2.STATE_YILINGQU == hongBaoState) {
                // 已到账
                order.setHongBaoState(2);
 
            } else if (HongBaoV2.STATE_SHIXIAO == hongBaoState) {
                // 已失效
                order.setHongBaoState(3);
            }
            
            List<HongBaoV2> listhb = hongBaoV2Service.listChildrenById(order.getHongbaoId());
            if (listhb != null && listhb.size() > 0) {
                if (listhb.size() == 1) {
                    UserInfo userInfo = listhb.get(0).getUserInfo();
                    if (userInfo != null) {
                        order.setLevelOneId(userInfo.getId()+"");
                        order.setLevelOneMoney(listhb.get(0).getMoney() + "");
                    }
                } else if (listhb.size() == 2) {
                    UserInfo userInfo = listhb.get(0).getUserInfo();
                    if (userInfo != null) {
                        order.setLevelOneId(userInfo.getId()+"");
                        order.setLevelOneMoney(listhb.get(0).getMoney() + "");
                    }
                    
                    UserInfo userInfo2 = listhb.get(1).getUserInfo();
                    if (userInfo2 != null) {
                        order.setLevelTwoId(userInfo2.getId()+"");
                        order.setLevelTwoMoney(listhb.get(1).getMoney() + "");
                    }
                }
            }
 
        }
 
        return list;
 
    }
 
    @Override
    public long countQuery(Integer keyType, String key, Integer state, Integer type, Integer orderState,
            String startTime, String endTime) throws CommonOrderException {
        return commonOrderMapper.countQuery(keyType, key, state, type, orderState, startTime, endTime);
    }
 
    @Override
    public CommonOrderVO getInfoByOrderNo(Long uid,String orderNo) throws CommonOrderException{
        List<CommonOrderVO> list = getOrderByUid(1, uid, null, null, null, orderNo, null, null, null);
        if (list == null || list.size() == 0) {
            return null;
        } else {
            return list.get(0);
        }
    }
    
    @Override
    public CommonOrderVO getCommonOrderByOrderNo(Long uid, String orderNo, Integer orderState) throws CommonOrderException {
 
        CommonOrderVO commonOrderVO = commonOrderMapper.getCommonOrderByOrderNo(uid, orderNo, orderState);
        // 订单信息为空
        if (commonOrderVO == null) {
            return null;
        }
 
        List<CommonOrderVO> listOrder = new ArrayList<CommonOrderVO>();
        listOrder.add(commonOrderVO);
 
        // 商品信息
        List<CommonOrderVO> listGoods = commonOrderMapper.listOrderGoodsInfo(listOrder);
        // 订单商品为空
        if (listGoods == null || listGoods.size() == 0) {
            return null;
        }
 
        /* 组合商品信息 */
        for (CommonOrderVO commonOrder : listGoods) {
 
            CommonOrderGoods goods = commonOrder.getCommonOrderGoods();
            if (goods == null) {
                continue;
            }
 
            String orderNo1 = commonOrder.getOrderNo();
            Integer sourceType = commonOrder.getSourceType();
 
            String orderNo2 = commonOrderVO.getOrderNo();
            Integer sourceType2 = commonOrderVO.getSourceType();
 
            // 来源、订单号相同
            if (sourceType.equals(sourceType2) && orderNo1.equals(orderNo2)) {
                // 加入商品信息
                List<CommonOrderGoodsVO> listOrderGoods = commonOrderVO.getListOrderGoods();
 
                CommonOrderGoodsVO commonGoodsVO = new CommonOrderGoodsVO();
                try {
                    PropertyUtils.copyProperties(commonGoodsVO, goods);
                } catch (Exception e) {
                    e.printStackTrace();
                }
 
                String picture = commonGoodsVO.getPicture();
                if (!StringUtil.isNullOrEmpty(picture) && !picture.contains("320x320")) {
                    commonGoodsVO.setPicture(TbImgUtil.getTBSize320Img(picture));
                }
 
                Integer hongBaoType = commonOrderVO.getHongBaoType();
                // 邀请订单信息保护
                if (HongBaoV2.TYPE_YAOQING == hongBaoType || HongBaoV2.TYPE_YIJI == hongBaoType
                        || HongBaoV2.TYPE_ERJI == hongBaoType || HongBaoV2.TYPE_SHARE_YIJI == hongBaoType
                        || HongBaoV2.TYPE_SHARE_ERJI == hongBaoType) {
                    Map<String, String> titleMap = new HashMap<String, String>();
                    titleMap.put("content", "为保障用户隐私,商品信息已隐藏!");
                    titleMap.put("fontColor", "#888888");
                    titleMap.put("bottomColor", "#E9E9E9");
                    commonGoodsVO.setTitle(null);
                    commonGoodsVO.setGoodsTitle(titleMap);
                }
 
                // 购买数量
                commonGoodsVO.setActualCount(commonOrder.getTotalCount() + "件");
 
                BigDecimal totalSettlement = commonOrder.getTotalSettlement();
                if (totalSettlement == null || totalSettlement.compareTo(new BigDecimal(0)) <= 0) {
                    totalSettlement = commonOrder.getTotalPayment();
                }
                // 实付款
                commonGoodsVO.setActualPay("付款金额:¥" + totalSettlement);
 
                listOrderGoods.add(commonGoodsVO);
 
                Integer orderType = commonOrder.getOrderType();
                if (orderType == null) {
                    String shopType = commonGoodsVO.getShopType();
                    if (CommonOrderGoodsVO.TYPE_TAOBAO.equalsIgnoreCase(shopType)) {
                        commonOrder.setOrderType(1);
                    } else if (CommonOrderGoodsVO.TYPE_TMALL.equalsIgnoreCase(shopType)) {
                        commonOrder.setOrderType(2);
                    }
                }
 
                break;
            }
        }
 
        return commonOrderVO;
    }
    
    @Override
    public List<CommonOrderVO> getCouponHongbaoByOrderNo(Long uid, Integer hongBaoState, String orderNo) throws CommonOrderException {
 
        List<CommonOrderVO> listOrder = commonOrderMapper.getCouponHongbaoByOrderNo(uid, hongBaoState, orderNo);
 
        // 订单信息为空
        if (listOrder == null || listOrder.size() == 0) {
            listOrder = new ArrayList<CommonOrderVO>();
            return listOrder;
        }
 
        // 商品信息
        List<CommonOrderVO> listGoods = commonOrderMapper.listOrderGoodsInfo(listOrder);
        // 订单商品为空
        if (listGoods == null || listGoods.size() == 0) {
            return listOrder;
        }
 
        // 数据加工重新组织
        listDataFactory(listOrder, listGoods, uid);
 
        return listOrder;
    }
 
    
    @Override
    public JSONObject getRewardJumpInfo(String orderNo) {
        
        JSONObject map = new JSONObject();
        // 订单标识
        List<String> signList = new ArrayList<String>();
        signList.add(CommonOrder.TYPE_FANLI);
        signList.add(PIC_REWARD); // 加入奖励成功图片
        map.put("signList", signList);
        
        
        ClientTextStyleVO rewardStyleVO = new ClientTextStyleVO();
        // 奖励券进度详情
        rewardStyleVO.setContent("返利奖励 >");
        rewardStyleVO.setColor( "#E5005C");
        rewardStyleVO.setBottomColor("#FFDCEA");
        
        Map<String, Object> jumpLink = new HashMap<String,Object>();
        jumpLink.put("orderNo", orderNo);
        
        Map<String, Object> jump = new HashMap<String,Object>();
        jump.put("type", 1); 
        jump.put("params", jumpLink);
        jump.put("jumpDetail", jumpDetailV2Service.getByTypeCache("rewardCouponDetail"));
        
        Map<String, Object> rewardMap = new HashMap<String,Object>();
        rewardMap.put("text", rewardStyleVO);
        rewardMap.put("jump", jump);
        
        map.put("rewardDetail", rewardMap);
        
        return map;
    }
}