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;
|
}
|
|
|
}
|