admin
2019-02-18 ed732dea219670c75c4664e522e10c90469a2b33
fanli-service-goods/src/main/java/com/yeshi/fanli/goods/service/impl/usergoods/ShareGoodsServiceImpl.java
@@ -11,7 +11,6 @@
import javax.annotation.Resource;
import org.fanli.facade.goods.dto.taoke.TaoKeAppInfo;
import org.fanli.facade.goods.entity.taobao.TBPid;
import org.fanli.facade.goods.entity.taobao.TaoBaoLink;
import org.fanli.facade.goods.entity.taobao.TaoBaoUnionConfig;
@@ -25,14 +24,13 @@
import org.fanli.facade.goods.service.usergoods.UserShareGoodsRecordService;
import org.fanli.facade.goods.utils.ImageUtil;
import org.fanli.facade.goods.utils.RedisGoodsManager;
import org.fanli.facade.goods.utils.dataoke.TaoKeApiUtil;
import org.fanli.facade.goods.utils.taobao.TaoBaoUtil;
import org.fanli.facade.order.service.hongbao.HongBaoManageService;
import org.fanli.facade.goods.utils.taobao.TaoKeGoodsApiUtil;
import org.fanli.facade.system.dto.taobao.TaoKeAppInfo;
import org.fanli.facade.system.service.common.ConfigService;
import org.fanli.facade.system.service.hongbao.HongBaoManageService;
import org.fanli.facade.user.service.UserAccountService;
import org.fanli.facade.user.service.UserInfoService;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.stereotype.Service;
import org.yeshi.utils.FileUtil;
import org.yeshi.utils.HttpUtil;
import org.yeshi.utils.JsonUtil;
@@ -51,7 +49,7 @@
import com.yeshi.fanli.goods.dao.usergoods.PidUserMapper;
import com.yeshi.fanli.goods.dao.usergoods.UserShareGoodsHistoryMapper;
@Service(version = "1.0.0")
@Service
public class ShareGoodsServiceImpl implements ShareGoodsService {
   @Resource
@@ -62,12 +60,6 @@
   @Resource
   private UserShareGoodsHistoryMapper userShareGoodsHistoryMapper;
   @Resource
   private HongBaoManageMapper hongBaoManageMapper;
   @Resource
   private UserInfoService userInfoService;
   @Resource
   private RedisGoodsManager redisManager;
@@ -105,8 +97,8 @@
   }
   @Override
   public UserShareGoodsHistory addShareGoodsHistory(Long uid, Long auctionId) throws ShareGoodsException {
      TBPid tbPid = tbPidService.getTBPid(uid, PidUser.TYPE_SHARE_GOODS);
   public UserShareGoodsHistory addShareGoodsHistory(UserInfo user, Long auctionId) throws ShareGoodsException {
      TBPid tbPid = tbPidService.getTBPid(user.getId(), PidUser.TYPE_SHARE_GOODS);
      if (tbPid == null) {
         throw new ShareGoodsException(1, "获取PID失败");
      }
@@ -122,7 +114,7 @@
      TaoBaoGoodsBrief goods = null;
      long startTime = System.currentTimeMillis();
      try {
         goods = TaoKeApiUtil.searchGoodsDetail(auctionId, info);
         goods = TaoKeGoodsApiUtil.searchGoodsDetail(auctionId, info);
      } catch (TaobaoGoodsDownException e) {
         throw new ShareGoodsException(3, "商品已下架");
      } finally {
@@ -141,11 +133,11 @@
         goods.setCouponLink(taoBaoLink.getCouponLink());
         goods.setAuctionUrl(taoBaoLink.getClickUrl());
      }
      return addShareGoodsHistory(uid, goods);
      return addShareGoodsHistory(user, goods);
   }
   @Override
   public UserShareGoodsHistory addShareGoodsHistory(Long uid, TaoBaoGoodsBrief goods) throws ShareGoodsException {
   public UserShareGoodsHistory addShareGoodsHistory(UserInfo user, TaoBaoGoodsBrief goods) throws ShareGoodsException {
      if (StringUtil.isNullOrEmpty(goods.getCouponLink()) && StringUtil.isNullOrEmpty(goods.getAuctionUrl())) {
         throw new ShareGoodsException(10, "转链失败");
      }
@@ -161,12 +153,13 @@
      tbLink.setCouponLink(goods.getCouponLink());
      if (!StringUtil.isNullOrEmpty(tbLink.getCouponLink())) {// 通过立即推广方式获取淘口令成功
         String quanToken = TaoKeApiUtil.getTKToken(goods.getPictUrl(), goods.getTitle(), tbLink.getCouponLink());
         String quanToken = TaoKeGoodsApiUtil.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());
         String quanToken = TaoKeGoodsApiUtil.getTKToken(goods.getPictUrl(), goods.getTitle(), tbLink.getClickUrl());
         if (!StringUtil.isNullOrEmpty(quanToken)) {
            tbLink.setTaoToken(quanToken);
         }
@@ -187,21 +180,21 @@
         imgList = new ArrayList<>();
      }
      history.setPictures(JsonUtil.getGson().toJson(imgList));
      history.setUser(new UserInfo(uid));
      history.setUser(user);
      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),
            Constant.systemCommonConfig.getShareGoodsPagePath(), AESUtil.encrypt(user.getId() + "", 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";
      String destPath = FileUtil.getCacheDir() + "/ercode_" + user.getId() + "_" + goods.getAuctionId() + ".jpg";
      String shareImgPath = FileUtil.getCacheDir() + "/share_" + user.getId() + "_" + goods.getAuctionId() + ".png";
      InputStream erCodeInputStream = null;
      try {
@@ -215,13 +208,12 @@
         // throw new ShareGoodsException(4, "生成二维码失败");
         // }
         // 获取到用户的头像
         UserInfo user = userInfoService.selectByPKey(uid);
         InputStream portrait = null;
         if (!StringUtil.isNullOrEmpty(user.getPortrait())) {
            try {
               portrait = HttpUtil.downLoadImg(user.getPortrait());
            } catch (HttpException e) {
               String portraitUrl = userAccountService.repairPortrait(uid);
               String portraitUrl = userAccountService.repairPortrait(user.getId());
               if (StringUtil.isNullOrEmpty(portraitUrl))
                  throw new ShareGoodsException(11, "获取头像出错");
               else {
@@ -271,7 +263,7 @@
         }
         FileUploadResult shareImgResult = COSManager.getInstance().uploadInputStream(shareImgInputStream,
               "sharegoods/share_" + uid + "_" + goods.getAuctionId() + "_" + System.currentTimeMillis() + ".png");
               "sharegoods/share_" + user.getId() + "_" + goods.getAuctionId() + "_" + System.currentTimeMillis() + ".png");
         if (shareImgResult == null || StringUtil.isNullOrEmpty(shareImgResult.getUrl())) {
            throw new ShareGoodsException(6, "分享图片保存失败");
@@ -282,7 +274,7 @@
         // 查询之前是否存在,存在就替换掉
         UserShareGoodsHistory old = userShareGoodsHistoryMapper.selectByUidAndAuctionId(uid, history.getGoodsId());
         UserShareGoodsHistory old = userShareGoodsHistoryMapper.selectByUidAndAuctionId(user.getId(), history.getGoodsId());
         if (old != null) {
            history.setId(old.getId());
            userShareGoodsHistoryMapper.updateByPrimaryKeySelective(history);
@@ -304,15 +296,15 @@
   }
   @Override
   public UserShareGoodsHistory getShareGoodsHistoryDetail(Long uid, Long auctionId) throws ShareGoodsException {
      UserShareGoodsHistory userShareGoodsHistory = userShareGoodsHistoryMapper.selectByUidAndAuctionId(uid,
   public UserShareGoodsHistory getShareGoodsHistoryDetail(UserInfo user, Long auctionId) throws ShareGoodsException {
      UserShareGoodsHistory userShareGoodsHistory = userShareGoodsHistoryMapper.selectByUidAndAuctionId(user.getId(),
            auctionId);
      if (userShareGoodsHistory == null)
         throw new ShareGoodsException(10, "分享不存在");
      // 淘口令不存在
      if (StringUtil.isNullOrEmpty(userShareGoodsHistory.getTkCode())) {
         TBPid tbPid = tbPidService.getTBPid(uid, PidUser.TYPE_SHARE_GOODS);
         TBPid tbPid = tbPidService.getTBPid(user.getId(), PidUser.TYPE_SHARE_GOODS);
         if (tbPid == null)
            throw new ShareGoodsException(1, "获取PID失败");
@@ -322,7 +314,7 @@
         if (tbLink == null) {
            TaoBaoGoodsBrief goods = null;
            try {
               goods = TaoKeApiUtil.searchGoodsDetail(auctionId);
               goods = TaoKeGoodsApiUtil.searchGoodsDetail(auctionId);
            } catch (TaobaoGoodsDownException e) {
               throw new ShareGoodsException(4, "商品已下架");
            }
@@ -387,7 +379,7 @@
      }
      TaoBaoGoodsBrief goods = null;
      try {
         goods = TaoKeApiUtil.searchGoodsDetail(auctionId, info);
         goods = TaoKeGoodsApiUtil.searchGoodsDetail(auctionId, info);
      } catch (TaobaoGoodsDownException e) {
         throw new ShareGoodsException(4, "商品已下架");
      } finally {
@@ -423,12 +415,13 @@
      tbLink.setCouponLink(goods.getCouponLink());
      if (!StringUtil.isNullOrEmpty(tbLink.getCouponLink())) {// 通过立即推广方式获取淘口令成功
         String quanToken = TaoKeApiUtil.getTKToken(goods.getPictUrl(), goods.getTitle(), tbLink.getCouponLink());
         String quanToken = TaoKeGoodsApiUtil.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());
         String quanToken = TaoKeGoodsApiUtil.getTKToken(goods.getPictUrl(), goods.getTitle(), tbLink.getClickUrl());
         if (!StringUtil.isNullOrEmpty(quanToken)) {
            tbLink.setTaoToken(quanToken);
         }
@@ -461,6 +454,7 @@
      return getTaoBaoLink(uid, auctionId, tbPid.getPid());
   }
   @Override
   public TaoBaoLink getTaoBaoLink(Long uid, Long auctionId, String pid) throws ShareGoodsException {
      if (uid == null || uid <= 0) {
@@ -486,10 +480,11 @@
      info.setPid(pid);
      TaoBaoGoodsBrief goods = null;
      try {
         goods = TaoKeApiUtil.searchGoodsDetailForConvert(auctionId, info);
         goods = TaoKeGoodsApiUtil.searchGoodsDetailForConvert(auctionId, info);
      } catch (TaobaoGoodsDownException e) {
         throw new ShareGoodsException(4, "商品已下架");
      } finally {
      }
      if (goods == null) {
         throw new ShareGoodsException(5, "获取商品详情失败");
@@ -523,8 +518,22 @@
         throw new ShareGoodsException(2, "商品ID不能为空");
      }
      return getTaoBaoLink(uid, auctionId, Constant.TAOBAO_SPECIAL_PID_DEFAULT);
   }
      TaoKeAppInfo app = new TaoKeAppInfo();
      app.setAppKey(Constant.TAOBAO_AUTH_APPKEY);
      app.setAppSecret(Constant.TAOBAO_AUTH_APPSECRET);
      app.setPid(Constant.TAOBAO_SPECIAL_PID_DEFAULT);
      TaoBaoGoodsBrief goods = TaoKeGoodsApiUtil.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;
   }
}