admin
2022-07-15 3727469752a977dde6327e2c48d761b20b565d3d
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
package com.yeshi.fanli.service.manger.goods.tb;
 
import com.yeshi.common.entity.taobao.TaoKeAppInfo;
import com.yeshi.fanli.entity.SystemEnum;
import com.yeshi.fanli.entity.SystemPIDInfo;
import com.yeshi.fanli.entity.bus.user.UserExtraTaoBaoInfo;
import com.yeshi.fanli.entity.taobao.TaoBaoLink;
import com.yeshi.fanli.exception.taobao.TaoBaoAuthException;
import com.yeshi.fanli.exception.taobao.TaoBaoBuyRelationMapException;
import com.yeshi.fanli.exception.taobao.TaoBaoConvertLinkException;
import com.yeshi.fanli.exception.taobao.TaobaoGoodsDownException;
import com.yeshi.fanli.service.inter.taobao.TaoBaoBuyRelationMapService;
import com.yeshi.fanli.service.inter.user.tb.UserExtraTaoBaoInfoService;
import com.yeshi.fanli.service.manger.PIDManager;
import com.yeshi.fanli.util.Constant;
import com.yeshi.fanli.util.StringUtil;
import com.yeshi.fanli.util.TaoBaoConstant;
import com.yeshi.fanli.util.cache.TaoBaoGoodsCacheUtil;
import com.yeshi.fanli.util.taobao.TaoBaoUtil;
import com.yeshi.fanli.util.taobao.TaoKeApiUtil;
import com.yeshi.goods.facade.entity.taobao.TaoBaoGoodsBrief;
import com.yeshi.goods.facade.utils.taobao.DaTaoKeApiUtil;
import org.springframework.stereotype.Component;
 
import javax.annotation.Resource;
 
/**
 * 淘宝转链
 */
@Component
public class TBConvertLinkManager {
 
 
    @Resource
    private PIDManager pidManager;
 
    @Resource
    private TaoBaoGoodsCacheUtil taoBaoGoodsCacheUtil;
 
    @Resource
    private UserExtraTaoBaoInfoService userExtraTaoBaoInfoService;
 
    @Resource
    private TaoBaoBuyRelationMapService taoBaoBuyRelationMapService;
 
 
    /**
     * 商品转链
     *
     * @param auctionId
     * @param uid
     * @param system
     * @param pidType
     * @return
     * @throws TaobaoGoodsDownException
     * @throws TaoBaoAuthException
     * @throws TaoBaoConvertLinkException
     */
    public TaoBaoLink convertGoods(String auctionId, Long uid, SystemEnum system, SystemPIDInfo.PidType pidType) throws TaobaoGoodsDownException, TaoBaoAuthException, TaoBaoConvertLinkException {
 
        //验证商品是否在线
        TaoBaoGoodsBrief goods = null;
        try {
            goods = TaoKeApiUtil.getSimpleGoodsInfo(auctionId);
        } catch (TaobaoGoodsDownException e) {
            throw e;
        }
 
 
        //--------判断淘宝是否授权-------------
        String specialId = null;
        String relationId = null;
        // 查询用户ID是否绑定了会员运营ID
        if (uid != null) {
            UserExtraTaoBaoInfo userInfo = userExtraTaoBaoInfoService.getByUid(uid);
            if (userInfo != null && !StringUtil.isNullOrEmpty(userInfo.getSpecialId()) && userInfo.getSpecialValid() != null
                    && userInfo.getSpecialValid() == true) {
                specialId = userInfo.getSpecialId();
            }
 
            if (userInfo != null && !StringUtil.isNullOrEmpty(userInfo.getRelationId()) && userInfo.getRelationId() != null
                    && userInfo.getRelationValid() == true) {
                relationId = userInfo.getRelationId();
            }
        }
 
        if (pidType == SystemPIDInfo.PidType.fanli) {
            if (StringUtil.isNullOrEmpty(specialId)) {
                throw new TaoBaoAuthException(10, "淘宝未授权");
            }
        } else if (pidType == SystemPIDInfo.PidType.share) {
            if (StringUtil.isNullOrEmpty(relationId)) {
                throw new TaoBaoAuthException(11, "淘宝未授权");
            }
        }
 
 
        //领券
        if (pidType == SystemPIDInfo.PidType.coupon) {
            return convertGoods(auctionId, specialId, null, system, SystemPIDInfo.PidType.coupon);
        }
 
        if (pidType == SystemPIDInfo.PidType.share) {
            return convertGoods(auctionId, null, relationId, system, SystemPIDInfo.PidType.share);
        }
 
        if (pidType == SystemPIDInfo.PidType.fanli) {
            boolean specialConvert = false;
            if (TaoBaoUtil.isSpecialGoods(goods.getMaterialLibType())) {
                specialConvert = true;
            } else
                specialConvert = false;
            if (specialConvert)
                return convertGoods(auctionId, specialId, null, system, SystemPIDInfo.PidType.fanli);
            else {
                try {
                    relationId = taoBaoBuyRelationMapService.getRelationId(uid);
                } catch (TaoBaoBuyRelationMapException e) {
                    throw new TaoBaoConvertLinkException(101, "非返利库商品分配pid出错");
                }
                return convertGoods(auctionId, null, relationId, system, SystemPIDInfo.PidType.fanliChannel);
            }
        }
 
        throw new TaoBaoConvertLinkException(201, "pid类型不存在");
    }
 
 
    public TaoBaoLink convertGoods(String auctionId, String specialId, String relationId, SystemEnum system, SystemPIDInfo.PidType pidType) throws TaoBaoConvertLinkException {
        String pid = pidManager.getPidCache(system, Constant.SOURCE_TYPE_TAOBAO, pidType);
        TaoKeAppInfo app = new TaoKeAppInfo();
        app.setAppKey(TaoBaoConstant.TAOBAO_AUTH_APPKEY);
        app.setAppSecret(TaoBaoConstant.TAOBAO_AUTH_APPSECRET);
        app.setPid(pid);
 
        TaoBaoLink taoBaoLink = new TaoBaoLink();
        taoBaoLink.setAuctionId(auctionId);
 
        String baseUrl = taoBaoGoodsCacheUtil.getBaseConvertLink(auctionId, app.getPid());
        if (!StringUtil.isNullOrEmpty(baseUrl)) {
            taoBaoLink.setClickUrl(baseUrl);
            taoBaoLink.setCouponLink(baseUrl);
            return taoBaoLink;
        }
 
        // 查询是否为大淘客商品
 
        String link = null;
        try {
            link = DaTaoKeApiUtil.convertLink(auctionId, app.getPid());
        } catch (Exception e) {
        }
 
        if (!StringUtil.isNullOrEmpty(link)) {
            taoBaoGoodsCacheUtil.cacheBaseConvertLink(auctionId, app.getPid(), link);
        }
 
        if (!StringUtil.isNullOrEmpty(link)) {
            taoBaoLink.setCouponLink(link);
            return taoBaoLink;
        }
 
        TaoBaoGoodsBrief goods = TaoKeApiUtil.specialConvertCoupon(auctionId, app, specialId, relationId);
        if (goods == null) {
            throw new TaoBaoConvertLinkException(1, "");
        }
 
        String clickUrl = goods.getAuctionUrl();
        String couponLink = goods.getCouponLink();
        if (!StringUtil.isNullOrEmpty(relationId)) {
            clickUrl = TaoBaoUtil.concatRelationId(clickUrl, relationId);
            couponLink = TaoBaoUtil.concatRelationId(couponLink, relationId);
        }
 
 
        taoBaoLink.setClickUrl(clickUrl);
        taoBaoLink.setCouponLink(couponLink);
 
        if (!StringUtil.isNullOrEmpty(goods.getYsylClickUrl()))
            taoBaoLink.setCouponLink(goods.getYsylClickUrl());
        taoBaoLink.setGoods(goods);
        //缓存
        goods.setCouponLink(taoBaoLink.getCouponLink());
        return taoBaoLink;
    }
 
 
}