admin
2019-09-16 cc55ffcea74c7d23dadaa0860befbcdf98f35341
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
package com.yeshi.fanli.service.impl.goods;
 
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
 
import javax.annotation.Resource;
 
import org.springframework.stereotype.Service;
import org.yeshi.utils.HttpUtil;
import org.yeshi.utils.JsonUtil;
import org.yeshi.utils.QRCodeUtil;
import org.yeshi.utils.entity.FileUploadResult;
import org.yeshi.utils.exception.HttpException;
import org.yeshi.utils.tencentcloud.COSManager;
 
import com.yeshi.fanli.dao.mybatis.HongBaoManageMapper;
import com.yeshi.fanli.dao.mybatis.UserInfoMapper;
import com.yeshi.fanli.dao.mybatis.UserShareGoodsHistoryMapper;
import com.yeshi.fanli.dao.mybatis.share.PidUserMapper;
import com.yeshi.fanli.dto.taobao.TaoLiJinDTO;
import com.yeshi.fanli.entity.bus.share.UserShareGoodsHistory;
import com.yeshi.fanli.entity.bus.tlj.UserTaoLiJinRecord;
import com.yeshi.fanli.entity.bus.user.UserInfo;
import com.yeshi.fanli.entity.bus.user.UserMoneyExtra;
import com.yeshi.fanli.entity.taobao.PidUser;
import com.yeshi.fanli.entity.taobao.TBPid;
import com.yeshi.fanli.entity.taobao.TaoBaoGoodsBrief;
import com.yeshi.fanli.entity.taobao.TaoBaoLink;
import com.yeshi.fanli.entity.taobao.TaoBaoUnionConfig;
import com.yeshi.fanli.entity.taobao.TaoKeAppInfo;
import com.yeshi.fanli.entity.taobao.UserTLJBuyHistory;
import com.yeshi.fanli.exception.share.ShareGoodsException;
import com.yeshi.fanli.exception.taobao.TaoKeApiException;
import com.yeshi.fanli.exception.taobao.TaobaoGoodsDownException;
import com.yeshi.fanli.exception.tlj.TaoLiJinCreateException;
import com.yeshi.fanli.exception.tlj.UserTaoLiJinRecordException;
import com.yeshi.fanli.log.LogHelper;
import com.yeshi.fanli.service.inter.config.ConfigService;
import com.yeshi.fanli.service.inter.goods.CommonGoodsService;
import com.yeshi.fanli.service.inter.goods.ShareGoodsService;
import com.yeshi.fanli.service.inter.hongbao.HongBaoManageService;
import com.yeshi.fanli.service.inter.money.UserMoneyExtraService;
import com.yeshi.fanli.service.inter.monitor.BusinessEmergent110Service;
import com.yeshi.fanli.service.inter.taobao.TLJBuyGoodsService;
import com.yeshi.fanli.service.inter.taobao.TaoBaoUnionConfigService;
import com.yeshi.fanli.service.inter.taobao.UserTLJBuyHistoryService;
import com.yeshi.fanli.service.inter.tlj.UserTaoLiJinRecordService;
import com.yeshi.fanli.service.inter.user.TBPidService;
import com.yeshi.fanli.service.inter.user.UserAccountService;
import com.yeshi.fanli.service.inter.user.UserShareGoodsRecordService;
import com.yeshi.fanli.util.AESUtil;
import com.yeshi.fanli.util.Constant;
import com.yeshi.fanli.util.FileUtil;
import com.yeshi.fanli.util.ImageUtil;
import com.yeshi.fanli.util.RedisManager;
import com.yeshi.fanli.util.StringUtil;
import com.yeshi.fanli.util.TaoBaoConstant;
import com.yeshi.fanli.util.TimeUtil;
import com.yeshi.fanli.util.taobao.TaoBaoUtil;
import com.yeshi.fanli.util.taobao.TaoKeApiUtil;
 
@Service
public class ShareGoodsServiceImpl implements ShareGoodsService {
 
    @Resource
    private PidUserMapper pidUserMapper;
 
    @Resource
    private TBPidService tbPidService;
 
    @Resource
    private UserShareGoodsHistoryMapper userShareGoodsHistoryMapper;
 
    @Resource
    private HongBaoManageMapper hongBaoManageMapper;
 
    @Resource
    private UserInfoMapper userinfoMapper;
 
    @Resource
    private RedisManager redisManager;
 
    @Resource
    private ConfigService configService;
 
    @Resource
    private TaoBaoUnionConfigService taoBaoUnionConfigService;
 
    @Resource
    private UserAccountService userAccountService;
 
    @Resource
    private HongBaoManageService hongBaoManageService;
 
    @Resource
    private UserShareGoodsRecordService userShareGoodsRecordService;
 
    @Resource
    private CommonGoodsService commonGoodsService;
 
    @Resource
    private UserMoneyExtraService userMoneyExtraService;
 
    @Resource
    private UserTaoLiJinRecordService userTaoLiJinRecordService;
 
    @Resource
    private UserTLJBuyHistoryService userTLJBuyHistoryService;
 
    @Resource
    private TLJBuyGoodsService tljBuyGoodsService;
 
    @Resource
    private BusinessEmergent110Service businessEmergent110Service;
 
    @Override
    public void addShareGoodsHistory(UserShareGoodsHistory history) {
        UserShareGoodsHistory userShareGoodsHistory = userShareGoodsHistoryMapper
                .selectByUidAndAuctionId(history.getUser().getId(), history.getGoodsId());
        if (userShareGoodsHistory != null && !StringUtil.isNullOrEmpty(history.getTkCode())) {
            UserShareGoodsHistory update = new UserShareGoodsHistory();
            update.setId(userShareGoodsHistory.getId());
            userShareGoodsHistory.setTkCode(history.getTkCode());
            userShareGoodsHistory.setCreateTime(new Date());
            userShareGoodsHistoryMapper.updateByPrimaryKeySelective(userShareGoodsHistory);
        } else
            userShareGoodsHistoryMapper.insertSelective(history);
    }
 
    @Override
    public UserShareGoodsHistory addShareGoodsHistory(Long uid, Long auctionId) throws ShareGoodsException {
        TBPid tbPid = tbPidService.getTBPid(uid, PidUser.TYPE_SHARE_GOODS);
        if (tbPid == null) {
            throw new ShareGoodsException(1, "获取PID失败");
        }
        String appId = tbPid.getPid().split("_")[2];
        String adzoneId = tbPid.getPid().split("_")[3];
        TaoBaoUnionConfig taoBaoUnionConfig = taoBaoUnionConfigService.getConfigByAppIdCache(appId);
 
        TaoKeAppInfo info = new TaoKeAppInfo();
        info.setAdzoneId(adzoneId);
        info.setAppKey(taoBaoUnionConfig.getAppKey());
        info.setAppSecret(taoBaoUnionConfig.getAppSecret());
        info.setPid(tbPid.getPid());
        TaoBaoGoodsBrief goods = null;
        long startTime = System.currentTimeMillis();
        try {
            goods = TaoKeApiUtil.searchGoodsDetail(auctionId, info);
        } catch (TaobaoGoodsDownException e) {
            throw new ShareGoodsException(3, "商品已下架");
        } finally {
            LogHelper.test(auctionId + "-获取商品详情耗时为:" + (System.currentTimeMillis() - startTime));
        }
        if (goods == null) {
            throw new ShareGoodsException(3, "商品已下架");
        }
 
        // 没有获取到转过链的商品详情
        if (StringUtil.isNullOrEmpty(goods.getCouponLink()) && !goods.getAuctionUrl().contains("s.click.taobao.com")) {
            TaoBaoGoodsBrief taoBaoLink = TaoKeApiUtil.specialConvertCoupon(auctionId, info); // TaoBaoUtil.getTbLinkForShare(auctionId,
                                                                                                // tbPid.getPid(),
                                                                                                // taoBaoUnionConfig);
            if (taoBaoLink == null) {
                throw new ShareGoodsException(201, "转链失败");
            }
            goods.setCouponLink(taoBaoLink.getCouponLink());
            goods.setAuctionUrl(taoBaoLink.getAuctionUrl());
        }
        return addShareGoodsHistory(uid, goods);
    }
 
    @Override
    public UserShareGoodsHistory addShareGoodsHistory(Long uid, TaoBaoGoodsBrief goods) throws ShareGoodsException {
        if (StringUtil.isNullOrEmpty(goods.getCouponLink()) && StringUtil.isNullOrEmpty(goods.getAuctionUrl())) {
            throw new ShareGoodsException(10, "转链失败");
        }
 
        BigDecimal rate = hongBaoManageService.getShareRate();
 
        // 创建淘口令
        TaoBaoLink tbLink = new TaoBaoLink(); // TaoBaoUtil.getTbLinkForShare(goods.getAuctionId(),
                                                // tbPid.getPid(),
                                                // config.get(0));
        tbLink.setAuctionId(goods.getAuctionId());
        tbLink.setClickUrl(goods.getAuctionUrl());
        tbLink.setCouponLink(goods.getCouponLink());
 
        if (!StringUtil.isNullOrEmpty(tbLink.getCouponLink())) {// 通过立即推广方式获取淘口令成功
            String quanToken = TaoKeApiUtil.getTKToken(goods.getPictUrl(), goods.getTitle(), tbLink.getCouponLink());
            if (!StringUtil.isNullOrEmpty(quanToken)) {
                tbLink.setTaoToken(quanToken);
            }
        } else if (!StringUtil.isNullOrEmpty(tbLink.getClickUrl())) {
            String quanToken = TaoKeApiUtil.getTKToken(goods.getPictUrl(), goods.getTitle(), tbLink.getClickUrl());
            if (!StringUtil.isNullOrEmpty(quanToken)) {
                tbLink.setTaoToken(quanToken);
            }
        }
 
        UserShareGoodsHistory history = new UserShareGoodsHistory();
        history.setTkCode(tbLink.getTaoToken());
        history.setLink(tbLink.getClickUrl());
        history.setQuanLink(tbLink.getCouponLink());
        history.setTkCode(tbLink.getTaoToken());
 
        history.setCreateTime(new Date());
        history.setGoodsId(goods.getAuctionId());
        history.setGoodsType(UserShareGoodsHistory.TYPE_TAOBAO);
        history.setHongbao(TaoBaoUtil.getShareGoodsHongBaoInfo(goods, rate));
        List<String> imgList = goods.getImgList();
        if (imgList == null) {
            imgList = new ArrayList<>();
        }
        history.setPictures(JsonUtil.getGson().toJson(imgList));
        history.setUser(new UserInfo(uid));
        history.setPostPicture(goods.getPictUrl());
        // 生成二维码信息
        // 包含用户ID与商品ID
        // 新版商品分享
        String url = String.format("http://%s%s?uid=%s&id=%s", configService.getH5Host(),
                Constant.systemCommonConfig.getShareGoodsPagePath(), AESUtil.encrypt(uid + "", Constant.UIDAESKEY),
                goods.getAuctionId() + "");
 
        String shortLink = HttpUtil.getShortLink(url);
        if (!StringUtil.isNullOrEmpty(shortLink)) {
            url = shortLink;
        }
        String destPath = FileUtil.getCacheDir() + "/ercode_" + uid + "_" + goods.getAuctionId() + ".jpg";
        String shareImgPath = FileUtil.getCacheDir() + "/share_" + uid + "_" + goods.getAuctionId() + ".png";
        InputStream erCodeInputStream = null;
 
        try {
            try {
                erCodeInputStream = QRCodeUtil.getInstance(500).encode(url);
            } catch (Exception e) {
                e.printStackTrace();
            }
 
            // if (!new File(destPath).exists()) {
            // throw new ShareGoodsException(4, "生成二维码失败");
            // }
            // 获取到用户的头像
            UserInfo user = userinfoMapper.selectAvailableByPrimaryKey(uid);
            InputStream portrait = null;
            if (!StringUtil.isNullOrEmpty(user.getPortrait())) {
                try {
                    portrait = HttpUtil.downLoadImg(user.getPortrait());
                } catch (HttpException e) {
                    String portraitUrl = userAccountService.repairPortrait(uid);
                    if (StringUtil.isNullOrEmpty(portraitUrl))
                        throw new ShareGoodsException(11, "获取头像出错");
                    else {
                        // 再次获取头像
                        try {
                            portrait = HttpUtil.downLoadImg(portraitUrl);
                        } catch (HttpException e1) {
                            throw new ShareGoodsException(11, "获取头像出错");
                        }
                    }
                }
            }
 
            InputStream shareImgInputStream = null;
 
            try {
                // 海草街的分享二维码不一样
                if (Constant.systemCommonConfig.getProjectChineseName().contains("海草街")) {
                    try {
                        shareImgInputStream = ImageUtil.drawGoodsShareImgHCJ(new FileInputStream(new File(destPath)),
                                portrait, goods);
                    } catch (Exception e) {
                        try {
                            LogHelper.errorDetailInfo(e);
                        } catch (Exception e1) {
                            e1.printStackTrace();
                        }
                        throw new ShareGoodsException(5, "分享图绘制失败");
                    }
                } else {
                    shareImgInputStream = ImageUtil.drawGoodsShareImg(erCodeInputStream, portrait, goods);
                }
            } catch (Exception e) {
                try {
                    LogHelper.errorDetailInfo(e, "", "分享图无法找到");
                } catch (Exception e1) {
                    e1.printStackTrace();
                }
            }
 
            try {
                if (shareImgInputStream != null && shareImgInputStream.available() <= 0) {
                    throw new ShareGoodsException(5, "生成分享图失败");
                }
            } catch (IOException e) {
                throw new ShareGoodsException(5, "读取分享图失败");
            }
 
            FileUploadResult shareImgResult = COSManager.getInstance().uploadInputStream(shareImgInputStream,
                    "sharegoods/share_" + uid + "_" + goods.getAuctionId() + "_" + System.currentTimeMillis() + ".png");
 
            if (shareImgResult == null || StringUtil.isNullOrEmpty(shareImgResult.getUrl())) {
                throw new ShareGoodsException(6, "分享图片保存失败");
            }
 
            history.setShareImg(shareImgResult.getUrl());
            history.setShareImgMD5(shareImgResult.getMd5());
 
            // 查询之前是否存在,存在就替换掉
 
            UserShareGoodsHistory old = userShareGoodsHistoryMapper.selectByUidAndAuctionId(uid, history.getGoodsId());
            if (old != null) {
                history.setId(old.getId());
                userShareGoodsHistoryMapper.updateByPrimaryKeySelective(history);
                // 删除原来的图片
                COSManager.getInstance().deleteFile(old.getShareImg());
            } else {
                userShareGoodsHistoryMapper.insertSelective(history);
            }
            history.setLink(url);
            return history;
        } finally {
            if (new File(destPath).exists()) {
                new File(destPath).delete();
            }
            if (new File(shareImgPath).exists()) {
                new File(shareImgPath).delete();
            }
        }
    }
 
    @Override
    public UserShareGoodsHistory getShareGoodsHistoryDetail(Long uid, Long auctionId) throws ShareGoodsException {
        UserShareGoodsHistory userShareGoodsHistory = userShareGoodsHistoryMapper.selectByUidAndAuctionId(uid,
                auctionId);
        if (userShareGoodsHistory == null)
            throw new ShareGoodsException(10, "分享不存在");
 
        // 淘口令不存在
        if (StringUtil.isNullOrEmpty(userShareGoodsHistory.getTkCode())) {
            TBPid tbPid = tbPidService.getTBPid(uid, PidUser.TYPE_SHARE_GOODS);
            if (tbPid == null)
                throw new ShareGoodsException(1, "获取PID失败");
 
            List<TaoBaoUnionConfig> config = taoBaoUnionConfigService.getConfigByTypeCache(PidUser.TYPE_SHARE_GOODS);
            // 创建淘口令
            TaoBaoLink tbLink = TaoBaoUtil.getTbLink(auctionId, tbPid.getPid(), config.get(0));
            if (tbLink == null) {
                TaoBaoGoodsBrief goods = null;
                try {
                    goods = TaoKeApiUtil.searchGoodsDetail(auctionId);
                } catch (TaobaoGoodsDownException e) {
                    throw new ShareGoodsException(4, "商品已下架");
                }
                tbLink = TaoBaoUtil.getTbLinkWithCoupon(goods.getPictUrl(), goods.getTitle(),
                        goods.getCouponActivityId(), auctionId, tbPid.getPid());
                if (tbLink == null || StringUtil.isNullOrEmpty(tbLink.getTaoToken()))
                    throw new ShareGoodsException(2, "获取淘口令失败,请稍后重试");
            }
            // 更新淘口令
 
            UserShareGoodsHistory history = new UserShareGoodsHistory();
            history.setId(userShareGoodsHistory.getId());
            history.setTkCode(tbLink.getTaoToken());
            history.setLink(tbLink.getClickUrl());
            history.setQuanLink(tbLink.getCouponLink());
            history.setTkCode(tbLink.getTaoToken());
            userShareGoodsHistoryMapper.updateByPrimaryKeySelective(history);
 
            userShareGoodsHistory.setTkCode(tbLink.getTaoToken());
            userShareGoodsHistory.setLink(tbLink.getClickUrl());
            userShareGoodsHistory.setQuanLink(tbLink.getCouponLink());
            userShareGoodsHistory.setTkCode(tbLink.getTaoToken());
        }
 
        return userShareGoodsHistory;
    }
 
    @Override
    public TaoBaoLink getTaoBaoLinkForShare(Long uid, Long auctionId, String relationId) throws ShareGoodsException {
 
        if (uid == null || uid <= 0) {
            throw new ShareGoodsException(1, "用户ID不能为空");
        }
 
        if (auctionId == null || auctionId <= 0) {
            throw new ShareGoodsException(2, "商品ID不能为空");
        }
 
        TaoKeAppInfo info = new TaoKeAppInfo();
 
        if (!StringUtil.isNullOrEmpty(relationId)) {
            String pid = TaoBaoConstant.TAOBAO_RELATION_PID_DEFAULT;
            String appId = pid.split("_")[2];
            String adzoneId = pid.split("_")[3];
            TaoBaoUnionConfig taoBaoUnionConfig = taoBaoUnionConfigService.getConfigByAppIdCache(appId);
            info.setAdzoneId(adzoneId);
            info.setAppKey(taoBaoUnionConfig.getAppKey());
            info.setAppSecret(taoBaoUnionConfig.getAppSecret());
            info.setPid(pid);
        } else {
            TBPid tbPid = tbPidService.getTBPid(uid, PidUser.TYPE_SHARE_GOODS);
            if (tbPid == null) {
                throw new ShareGoodsException(3, "获取推广位失败");
            }
            String appId = tbPid.getPid().split("_")[2];
            TaoBaoUnionConfig taoBaoUnionConfig = taoBaoUnionConfigService.getConfigByAppIdCache(appId);
            String adzoneId = tbPid.getPid().split("_")[3];
            info.setAdzoneId(adzoneId);
            info.setAppKey(taoBaoUnionConfig.getAppKey());
            info.setAppSecret(taoBaoUnionConfig.getAppSecret());
            info.setPid(tbPid.getPid());
        }
        TaoBaoGoodsBrief goods = null;
        try {
            // 测试
            // info.setAdzoneId("123");
            goods = TaoKeApiUtil.searchGoodsDetail(auctionId, info);
        } catch (TaobaoGoodsDownException e) {
            throw new ShareGoodsException(4, "商品已下架");
        } finally {
        }
        if (goods == null) {
            throw new ShareGoodsException(5, "获取商品详情失败");
        }
 
        if (StringUtil.isNullOrEmpty(goods.getCouponLink()) && !goods.getAuctionUrl().contains("s.click.taobao.com")) {
            TaoBaoUnionConfig taoBaoUnionConfig = taoBaoUnionConfigService
                    .getConfigByAppIdCache(info.getPid().split("_")[2]);
            TaoBaoGoodsBrief taoBaoLink = TaoKeApiUtil.specialConvertCoupon(auctionId, info);
            if (taoBaoLink == null) {
                throw new ShareGoodsException(201, "商品转链失败");
            }
            goods.setCouponLink(taoBaoLink.getCouponLink());
            goods.setAuctionUrl(taoBaoLink.getAuctionUrl());
        }
 
        if (!StringUtil.isNullOrEmpty(relationId)) {
            if (!StringUtil.isNullOrEmpty(goods.getAuctionUrl()))
                goods.setAuctionUrl(goods.getAuctionUrl() + "&relationId=" + relationId);
            if (!StringUtil.isNullOrEmpty(goods.getCouponLink()))
                goods.setCouponLink(goods.getCouponLink() + "&relationId=" + relationId);
        }
 
        // 保存商品详情
 
        // 创建淘口令
        TaoBaoLink tbLink = new TaoBaoLink();
        tbLink.setAuctionId(goods.getAuctionId());
        tbLink.setClickUrl(goods.getAuctionUrl());
        tbLink.setCouponLink(goods.getCouponLink());
 
        if (!StringUtil.isNullOrEmpty(tbLink.getCouponLink())) {// 通过立即推广方式获取淘口令成功
            String quanToken = TaoKeApiUtil.getTKToken(goods.getPictUrl(), goods.getTitle(), tbLink.getCouponLink());
            if (!StringUtil.isNullOrEmpty(quanToken)) {
                tbLink.setTaoToken(quanToken);
            }
        } else if (!StringUtil.isNullOrEmpty(tbLink.getClickUrl())) {
            String quanToken = TaoKeApiUtil.getTKToken(goods.getPictUrl(), goods.getTitle(), tbLink.getClickUrl());
            if (!StringUtil.isNullOrEmpty(quanToken)) {
                tbLink.setTaoToken(quanToken);
            }
        }
 
        tbLink.setGoods(goods);
 
        // 存储分享商品的信息,用于加快分享网页接口请求速度
        if (Constant.IS_OUTNET)
            redisManager.saveTaoBaoGoodsBriefTemp(goods);
 
        return tbLink;
    }
 
    @Override
    public TaoBaoLink getTaoBaoLinkForBuy(Long uid, Long auctionId, int pidType) throws ShareGoodsException {
 
        if (uid == null || uid <= 0) {
            throw new ShareGoodsException(1, "用户ID不能为空");
        }
 
        if (auctionId == null || auctionId <= 0) {
            throw new ShareGoodsException(2, "商品ID不能为空");
        }
 
        TBPid tbPid = tbPidService.getTBPid(uid, pidType);
        if (tbPid == null) {
            throw new ShareGoodsException(3, "获取推广位失败");
        }
        return getTaoBaoLink(uid, auctionId, tbPid.getPid());
    }
 
    @Override
    public TaoBaoLink getTaoBaoLink(Long uid, Long auctionId, String pid) throws ShareGoodsException {
 
        if (uid == null || uid <= 0) {
            throw new ShareGoodsException(1, "用户ID不能为空");
        }
 
        if (auctionId == null || auctionId <= 0) {
            throw new ShareGoodsException(2, "商品ID不能为空");
        }
 
        if (StringUtil.isNullOrEmpty(pid)) {
            throw new ShareGoodsException(3, "无推广位");
        }
 
        String appId = pid.split("_")[2];
        String adzoneId = pid.split("_")[3];
        TaoBaoUnionConfig taoBaoUnionConfig = taoBaoUnionConfigService.getConfigByAppIdCache(appId);
 
        TaoKeAppInfo info = new TaoKeAppInfo();
        info.setAdzoneId(adzoneId);
        info.setAppKey(taoBaoUnionConfig.getAppKey());
        info.setAppSecret(taoBaoUnionConfig.getAppSecret());
        info.setPid(pid);
        TaoBaoGoodsBrief goods = null;
        try {
            goods = TaoKeApiUtil.searchGoodsDetailForConvert(auctionId, info);
        } catch (TaobaoGoodsDownException e) {
            throw new ShareGoodsException(4, "商品已下架");
        } finally {
 
        }
        if (goods == null) {
            throw new ShareGoodsException(5, "获取商品详情失败");
        }
 
        if (StringUtil.isNullOrEmpty(goods.getCouponLink()) && !goods.getAuctionUrl().contains("s.click.taobao.com")) {
            TaoBaoGoodsBrief taoBaoLink = TaoKeApiUtil.specialConvertCoupon(auctionId, info);
            if (taoBaoLink == null) {
                throw new ShareGoodsException(201, "商品转链失败");
            }
            goods.setCouponLink(taoBaoLink.getCouponLink());
            goods.setAuctionUrl(taoBaoLink.getAuctionUrl());
        }
 
        // 创建淘口令
        TaoBaoLink tbLink = new TaoBaoLink();
        tbLink.setAuctionId(goods.getAuctionId());
        tbLink.setClickUrl(goods.getAuctionUrl());
        tbLink.setCouponLink(goods.getCouponLink());
        return tbLink;
    }
 
    @Override
    public TaoBaoLink getTaoBaoLinkForBuyWithSpecial(Long uid, String specialId, Long auctionId)
            throws ShareGoodsException {
        if (uid == null || uid <= 0) {
            throw new ShareGoodsException(1, "用户ID不能为空");
        }
 
        if (auctionId == null || auctionId <= 0) {
            throw new ShareGoodsException(2, "商品ID不能为空");
        }
 
        TaoKeAppInfo app = new TaoKeAppInfo();
        app.setAppKey(TaoBaoConstant.TAOBAO_AUTH_APPKEY);
        app.setAppSecret(TaoBaoConstant.TAOBAO_AUTH_APPSECRET);
        app.setPid(TaoBaoConstant.TAOBAO_SPECIAL_PID_DEFAULT);
        TaoBaoGoodsBrief goods = TaoKeApiUtil.specialConvertCoupon(auctionId, app);
        if (goods == null) {
            throw new ShareGoodsException(1, "");
        }
 
        TaoBaoLink taoBaoLink = new TaoBaoLink();
        taoBaoLink.setAuctionId(auctionId);
        taoBaoLink.setClickUrl(goods.getAuctionUrl());
        taoBaoLink.setCouponLink(goods.getCouponLink());
        taoBaoLink.setGoods(goods);
 
        return taoBaoLink;
    }
 
    @Override
    public UserShareGoodsHistory getShareGoodsHistory(Long uid, Long auctionId) {
        return userShareGoodsHistoryMapper.selectByUidAndAuctionId(uid, auctionId);
    }
 
    @Override
    public TaoBaoLink getTaoBaoLinkForShare(Long uid, Long auctionId, String relationId, String pid)
            throws ShareGoodsException {
        if (uid == null || uid <= 0) {
            throw new ShareGoodsException(1, "用户ID不能为空");
        }
 
        if (auctionId == null || auctionId <= 0) {
            throw new ShareGoodsException(2, "商品ID不能为空");
        }
 
        TaoKeAppInfo info = new TaoKeAppInfo();
 
        if (!StringUtil.isNullOrEmpty(relationId)) {
            String appId = pid.split("_")[2];
            String adzoneId = pid.split("_")[3];
            TaoBaoUnionConfig taoBaoUnionConfig = taoBaoUnionConfigService.getConfigByAppIdCache(appId);
            info.setAdzoneId(adzoneId);
            info.setAppKey(taoBaoUnionConfig.getAppKey());
            info.setAppSecret(taoBaoUnionConfig.getAppSecret());
            info.setPid(pid);
        } else {
            TBPid tbPid = tbPidService.getTBPid(uid, PidUser.TYPE_SHARE_GOODS);
            if (tbPid == null) {
                throw new ShareGoodsException(3, "获取推广位失败");
            }
            String appId = tbPid.getPid().split("_")[2];
            TaoBaoUnionConfig taoBaoUnionConfig = taoBaoUnionConfigService.getConfigByAppIdCache(appId);
            String adzoneId = tbPid.getPid().split("_")[3];
            info.setAdzoneId(adzoneId);
            info.setAppKey(taoBaoUnionConfig.getAppKey());
            info.setAppSecret(taoBaoUnionConfig.getAppSecret());
            info.setPid(tbPid.getPid());
        }
        TaoBaoGoodsBrief goods = null;
        try {
            goods = TaoKeApiUtil.searchGoodsDetail(auctionId, info);
        } catch (TaobaoGoodsDownException e) {
            throw new ShareGoodsException(4, "商品已下架");
        } finally {
        }
        if (goods == null) {
            throw new ShareGoodsException(5, "获取商品详情失败");
        }
 
        if (StringUtil.isNullOrEmpty(goods.getCouponLink()) && !goods.getAuctionUrl().contains("s.click.taobao.com")) {
            TaoBaoGoodsBrief taoBaoLink = TaoKeApiUtil.specialConvertCoupon(auctionId, info);
            if (taoBaoLink == null) {
                throw new ShareGoodsException(201, "商品转链失败");
            }
            goods.setCouponLink(taoBaoLink.getCouponLink());
            goods.setAuctionUrl(taoBaoLink.getAuctionUrl());
        }
 
        if (!StringUtil.isNullOrEmpty(relationId)) {
            if (!StringUtil.isNullOrEmpty(goods.getAuctionUrl()))
                goods.setAuctionUrl(goods.getAuctionUrl() + "&relationId=" + relationId);
            if (!StringUtil.isNullOrEmpty(goods.getCouponLink()))
                goods.setCouponLink(goods.getCouponLink() + "&relationId=" + relationId);
        }
 
        // 保存商品详情
 
        // 创建淘口令
        TaoBaoLink tbLink = new TaoBaoLink();
        tbLink.setAuctionId(goods.getAuctionId());
        tbLink.setClickUrl(goods.getAuctionUrl());
        tbLink.setCouponLink(goods.getCouponLink());
 
        if (!StringUtil.isNullOrEmpty(tbLink.getCouponLink())) {// 通过立即推广方式获取淘口令成功
            String quanToken = TaoKeApiUtil.getTKToken(goods.getPictUrl(), goods.getTitle(), tbLink.getCouponLink());
            if (!StringUtil.isNullOrEmpty(quanToken)) {
                tbLink.setTaoToken(quanToken);
            }
        } else if (!StringUtil.isNullOrEmpty(tbLink.getClickUrl())) {
            String quanToken = TaoKeApiUtil.getTKToken(goods.getPictUrl(), goods.getTitle(), tbLink.getClickUrl());
            if (!StringUtil.isNullOrEmpty(quanToken)) {
                tbLink.setTaoToken(quanToken);
            }
        }
 
        tbLink.setGoods(goods);
 
        // 存储分享商品的信息,用于加快分享网页接口请求速度
        if (Constant.IS_OUTNET)
            redisManager.saveTaoBaoGoodsBriefTemp(goods);
 
        return tbLink;
    }
 
    @Override
    public TaoBaoLink getTaoLiJinLinkForBuy(Long uid, String specialId, Long auctionId, String pid)
            throws ShareGoodsException {
        if (uid == null || uid <= 0) {
            throw new ShareGoodsException(1, "用户ID不能为空");
        }
 
        if (auctionId == null || auctionId <= 0) {
            throw new ShareGoodsException(2, "商品ID不能为空");
        }
 
        // 用户剩余可以淘礼金验证
        UserMoneyExtra userMoneyExtra = userMoneyExtraService.selectByPrimaryKey(uid);
        if (userMoneyExtra == null || userMoneyExtra.getTlj() == null) {
            throw new ShareGoodsException(101, "红包余额不足");
        }
 
        TaoKeAppInfo app = new TaoKeAppInfo();
        app.setAppKey(TaoBaoConstant.TAOBAO_AUTH_APPKEY);
        app.setAppSecret(TaoBaoConstant.TAOBAO_AUTH_APPSECRET);
        if (pid == null)
            app.setPid(TaoBaoConstant.TAOBAO_SPECIAL_PID_DEFAULT);
        else
            app.setPid(pid);
        TaoBaoGoodsBrief goods = TaoKeApiUtil.specialConvertCoupon(auctionId, app);
        if (goods == null) {
            throw new ShareGoodsException(1, "");
        }
 
        String taoLiJinLink = null;
        try {
            goods.setAuctionId(auctionId);
            UserTaoLiJinRecord createUserTaoLiJin = userTaoLiJinRecordService.createSelfBuyTaoLiJin(uid, 1, goods);
            taoLiJinLink = createUserTaoLiJin.getSendUrl();
        } catch (UserTaoLiJinRecordException e) {
            throw new ShareGoodsException(e.getCode(), e.getMsg());
        }
 
        TaoBaoLink taoBaoLink = new TaoBaoLink();
        taoBaoLink.setAuctionId(auctionId);
        taoBaoLink.setClickUrl(goods.getAuctionUrl());
        taoBaoLink.setCouponLink(taoLiJinLink);
        taoBaoLink.setGoods(goods);
 
        return taoBaoLink;
    }
 
    @Override
    public TaoBaoLink getTaoLiJinLinkForShare(Long uid, Long auctionId, String relationId) throws ShareGoodsException {
 
        if (uid == null || uid <= 0) {
            throw new ShareGoodsException(1, "用户ID不能为空");
        }
 
        if (auctionId == null || auctionId <= 0) {
            throw new ShareGoodsException(2, "商品ID不能为空");
        }
 
        TaoKeAppInfo info = new TaoKeAppInfo();
 
        if (!StringUtil.isNullOrEmpty(relationId)) {
            String pid = TaoBaoConstant.TAOBAO_RELATION_PID_DEFAULT;
            String appId = pid.split("_")[2];
            String adzoneId = pid.split("_")[3];
            TaoBaoUnionConfig taoBaoUnionConfig = taoBaoUnionConfigService.getConfigByAppIdCache(appId);
            info.setAdzoneId(adzoneId);
            info.setAppKey(taoBaoUnionConfig.getAppKey());
            info.setAppSecret(taoBaoUnionConfig.getAppSecret());
            info.setPid(pid);
        } else {
            TBPid tbPid = tbPidService.getTBPid(uid, PidUser.TYPE_SHARE_GOODS);
            if (tbPid == null) {
                throw new ShareGoodsException(3, "获取推广位失败");
            }
            String appId = tbPid.getPid().split("_")[2];
            TaoBaoUnionConfig taoBaoUnionConfig = taoBaoUnionConfigService.getConfigByAppIdCache(appId);
            String adzoneId = tbPid.getPid().split("_")[3];
            info.setAdzoneId(adzoneId);
            info.setAppKey(taoBaoUnionConfig.getAppKey());
            info.setAppSecret(taoBaoUnionConfig.getAppSecret());
            info.setPid(tbPid.getPid());
        }
        TaoBaoGoodsBrief goods = null;
        try {
            goods = TaoKeApiUtil.searchGoodsDetail(auctionId, info);
        } catch (TaobaoGoodsDownException e) {
            throw new ShareGoodsException(4, "商品已下架");
        } finally {
        }
        if (goods == null) {
            throw new ShareGoodsException(5, "获取商品详情失败");
        }
 
        if (StringUtil.isNullOrEmpty(goods.getCouponLink()) && !goods.getAuctionUrl().contains("s.click.taobao.com")) {
            TaoBaoUnionConfig taoBaoUnionConfig = taoBaoUnionConfigService
                    .getConfigByAppIdCache(info.getPid().split("_")[2]);
            TaoBaoGoodsBrief taoBaoLink = TaoKeApiUtil.specialConvertCoupon(auctionId, info);
            if (taoBaoLink == null) {
                throw new ShareGoodsException(201, "商品转链失败");
            }
            goods.setCouponLink(taoBaoLink.getCouponLink());
            goods.setAuctionUrl(taoBaoLink.getAuctionUrl());
        }
 
        if (!StringUtil.isNullOrEmpty(relationId)) {
            if (!StringUtil.isNullOrEmpty(goods.getAuctionUrl()))
                goods.setAuctionUrl(goods.getAuctionUrl() + "&relationId=" + relationId);
            if (!StringUtil.isNullOrEmpty(goods.getCouponLink()))
                goods.setCouponLink(goods.getCouponLink() + "&relationId=" + relationId);
        }
 
        // 创建淘口令
        TaoBaoLink tbLink = new TaoBaoLink();
        tbLink.setAuctionId(goods.getAuctionId());
        tbLink.setClickUrl(goods.getAuctionUrl());
        tbLink.setCouponLink(goods.getCouponLink());
        tbLink.setGoods(goods);
 
        // 存储分享商品的信息,用于加快分享网页接口请求速度
        if (Constant.IS_OUTNET)
            redisManager.saveTaoBaoGoodsBriefTemp(goods);
 
        return tbLink;
    }
 
    @Override
    public TaoBaoLink getTaoLiJinLinkForBuyWithOutFanLi(Long uid, Long auctionId, TaoKeAppInfo app)
            throws ShareGoodsException, TaoLiJinCreateException {
        if (uid == null || uid <= 0) {
            throw new ShareGoodsException(1, "用户ID不能为空");
        }
 
        if (auctionId == null || auctionId <= 0) {
            throw new ShareGoodsException(2, "商品ID不能为空");
        }
 
        boolean canBuy = userTLJBuyHistoryService.canBuy(uid, auctionId);
        if (!canBuy) {
            throw new ShareGoodsException(3, "立减红包已抢光,请稍后再试");
        }
 
        long currentTime = System.currentTimeMillis();
        Date sendEndTime = new Date(TimeUtil.convertToTimeTemp(
                TimeUtil.getGernalTime(currentTime + 1000 * 60 * 60 * 24L, "yyyy-MM-dd"), "yyyy-MM-dd"));
        TaoBaoGoodsBrief goods = null;
        try {
            goods = TaoKeApiUtil.searchGoodsDetail(auctionId);
        } catch (TaobaoGoodsDownException e) {
            throw new ShareGoodsException(4, "商品已下架");
        }
 
        BigDecimal perface = TaoBaoUtil.getGoodsHongBaoMoney(goods, new BigDecimal(70));
        TaoLiJinDTO taoLiJinDTO = null;
        try {
            taoLiJinDTO = TaoKeApiUtil.createTaoLiJin(auctionId, "自购立减", perface, 1,
                    new Date(currentTime - 1000 * 60 * 10L), sendEndTime, new Date(currentTime - 1000 * 60 * 10L),
                    sendEndTime, app);
        } catch (TaoLiJinCreateException e1) {
            if (e1.getCode() == TaoLiJinCreateException.CODE_TLJ_NO_MONEY) {// 资金不足
                TaoBaoUnionConfig config = taoBaoUnionConfigService.getConfigByAppKeyCache(app.getAppKey());
                businessEmergent110Service.tljNoMoney(config.getAccount());
            } else if (e1.getCode() == TaoLiJinCreateException.CODE_TLJ_FORBIDDEN) {// 商品不允许创建淘礼金
                tljBuyGoodsService.removeGoods(auctionId);
            }
            LogHelper.errorDetailInfo(e1, e1.getMessage(), null);
        } catch (TaoKeApiException e) {
            LogHelper.errorDetailInfo(e, e.getMessage(), null);
            throw new ShareGoodsException(5, "生成推广链接失败");
        }
 
        if (taoLiJinDTO == null)
            throw new ShareGoodsException(5, "生成推广链接失败");
 
        // 减去剩余个数(随机减去8到10个)
        tljBuyGoodsService.subHongBaoLeftCount(auctionId, TimeUtil.getGernalTime(currentTime, "yyyy-MM-dd"),
                8 + (int) (3 * Math.random()));
 
        // 插入记录
        try {
            userTLJBuyHistoryService.addHistory(new UserTLJBuyHistory(null, uid, auctionId,
                    TimeUtil.getGernalTime(currentTime, "yyyy-MM-dd"), taoLiJinDTO.getRightsId(), new Date()));
        } catch (Exception e) {
            LogHelper.errorDetailInfo(e);
        }
 
        TaoBaoLink taoBaoLink = new TaoBaoLink();
        taoBaoLink.setAuctionId(auctionId);
        taoBaoLink.setClickUrl(goods.getAuctionUrl());
        taoBaoLink.setCouponLink(taoLiJinDTO.getSendUrl());
        taoBaoLink.setGoods(goods);
        return taoBaoLink;
    }
}