admin
2019-07-09 531d93708df8017e59830f15b41f3cc42d6126e6
fanli/src/main/java/com/yeshi/fanli/controller/client/v1/ShareController.java
@@ -50,7 +50,6 @@
import com.yeshi.fanli.service.inter.monitor.BusinessEmergent110Service;
import com.yeshi.fanli.service.inter.monitor.MonitorService;
import com.yeshi.fanli.service.inter.tlj.ConfigTaoLiJinService;
import com.yeshi.fanli.service.inter.tlj.GoodsTaoLiJinRateService;
import com.yeshi.fanli.service.inter.tlj.UserTaoLiJinRecordService;
import com.yeshi.fanli.service.inter.user.SpreadUserImgService;
import com.yeshi.fanli.service.inter.user.UserExtraTaoBaoInfoService;
@@ -69,6 +68,7 @@
import com.yeshi.fanli.util.factory.MonitorFactory;
import com.yeshi.fanli.util.taobao.TaoBaoUtil;
import com.yeshi.fanli.util.taobao.TaoKeApiUtil;
import com.yeshi.fanli.util.taobao.TaoLiJinUtil;
import com.yeshi.fanli.vo.msg.ClientTextStyleVO;
import net.sf.json.JSONArray;
@@ -131,16 +131,12 @@
   @Resource
   private BusinessEmergent110Service businessEmergent110Service;
   @Resource
   private ConfigTaoLiJinService configTaoLiJinService;
   @Resource
   private GoodsTaoLiJinRateService goodsTaoLiJinRateService;
   @Resource
   private UserTaoLiJinRecordService userTaoLiJinRecordService;
   // 获取商品分享链接
   @RequestMapping(value = "getGoodsShareUrl")
@@ -161,7 +157,7 @@
   @RequestMapping(value = "gettaobaosharelink")
   public void getTaobaoShareLink(AcceptData acceptData, Long uid, Long auctionId, String source, Integer totalNum,
         HttpServletRequest request, PrintWriter out) {
      if (uid == null || uid <= 0) {
         out.print(JsonUtil.loadFalseResult(1, "用户ID不能为空"));
         return;
@@ -179,61 +175,29 @@
      }
      UserExtraTaoBaoInfo extraInfo = userExtraTaoBaoInfoService.getByUid(uid);
      String relationId = null;
      if (extraInfo != null && extraInfo.getRelationId() != null && extraInfo.getRelationValid() != null
            && extraInfo.getRelationValid() == true)
         relationId = extraInfo.getRelationId();
      if (StringUtil.isNullOrEmpty(relationId)) {
         out.print(JsonUtil.loadFalseResult(3, "淘宝未授权,请前往\"我的\"绑定淘宝账号"));
         return;
      }
      if (source != null && "taolijin".equals(source)) {
         createTaoLijin(uid, auctionId, totalNum, relationId, request, out);
         return;
      }
      try {
         String relationId = null;
         if (extraInfo != null && extraInfo.getRelationId() != null && extraInfo.getRelationValid() != null
               && extraInfo.getRelationValid() == true)
            relationId = extraInfo.getRelationId();
         TaoBaoLink taoBaoLink  = new TaoBaoLink();
         if (source != null && "taolijin".equals(source)) {
            taoBaoLink = shareGoodsService.getTaoLiJinLinkForShare(uid, auctionId, relationId);
         } else {
            taoBaoLink = shareGoodsService.getTaoBaoLinkForShare(uid, auctionId, relationId);
         }
         TaoBaoLink taoBaoLink = shareGoodsService.getTaoBaoLinkForShare(uid, auctionId, relationId);
         JSONObject data = new JSONObject();
         String url = String.format("http://%s%s?uid=%s&id=%s&appType=flq", configService.getH5Host(),
               Constant.systemCommonConfig.getShareGoodsPagePath(), AESUtil.encrypt(uid + "", Constant.UIDAESKEY),
               auctionId + "");
         if (source != null && "taolijin".equals(source)) {
            BigDecimal spreadMoney = goodsTaoLiJinRateService.getGoodsSpreadMoney(taoBaoLink.getGoods());
            // 推广红包 不能小于1
            if (spreadMoney.compareTo(new BigDecimal(1)) < 0) {
               out.print(JsonUtil.loadFalseResult(1, "该商品淘礼金不足1元"));
               return;
            }
            data.put("spreadMoney", "淘礼金 ¥" + spreadMoney.toString());
            String taoLiJinLink = null;
            try {
               UserTaoLiJinRecord record = userTaoLiJinRecordService.createShareTaoLiJin(uid, totalNum, taoBaoLink.getGoods());
               taoLiJinLink = record.getSendUrl();
               data.put("tljId", record.getId());
               data.put("surplusMoney", record.getSurplusMoney());
            } catch (UserTaoLiJinRecordException e) {
               out.print(JsonUtil.loadFalseResult(e.getCode(), e.getMsg()));
               return;
            }
            // 创建淘口令
            TaoBaoGoodsBrief goods = taoBaoLink.getGoods();
            if (!StringUtil.isNullOrEmpty(taoLiJinLink)) {// 通过立即推广方式获取淘口令成功
               String quanToken = TaoKeApiUtil.getTKToken(goods.getPictUrl(), goods.getTitle(), taoLiJinLink);
               if (!StringUtil.isNullOrEmpty(quanToken)) {
                  taoBaoLink.setTaoToken(quanToken);
               }
            } else if (!StringUtil.isNullOrEmpty(taoBaoLink.getClickUrl())) {
               String quanToken = TaoKeApiUtil.getTKToken(goods.getPictUrl(), goods.getTitle(), taoBaoLink.getClickUrl());
               if (!StringUtil.isNullOrEmpty(quanToken)) {
                  taoBaoLink.setTaoToken(quanToken);
               }
            }
         }
         String shortLink = HttpUtil.getShortLink(url);
         if (!StringUtil.isNullOrEmpty(shortLink)) {
            url = shortLink;
@@ -249,7 +213,6 @@
         // 无券
         String shopType = taoBaoLink.getGoods().getUserType() == 0 ? "淘宝价" : "天猫价";
         if (StringUtil.isNullOrEmpty(taoBaoLink.getGoods().getCouponInfo())) {
            String text = shareGoodsTextTemplateService.getCommonTemplate(uid);
            if (StringUtil.isNullOrEmpty(text))
@@ -258,33 +221,17 @@
                  MoneyBigDecimalUtil.getWithNoZera(taoBaoLink.getGoods().getZkPrice()) + "");
         } else// 有券
         {
            if (source != null && "taolijin".equals(source)) {
               String text = shareGoodsTextTemplateService.getTaoLiJinTemplate(uid);
               if (StringUtil.isNullOrEmpty(text))
                  text = configTaoLiJinService.getValueByKey("goods_share_text");
               shareText = text.replace("{标题}", taoBaoLink.getGoods().getTitle())
                     .replace("{商品原价}", MoneyBigDecimalUtil.getWithNoZera(taoBaoLink.getGoods().getZkPrice()) + "")
                     .replace("{优惠券面额}",
                           MoneyBigDecimalUtil.getWithNoZera(taoBaoLink.getGoods().getCouponAmount()).toString())
                     .replace("{淘礼金面额}", MoneyBigDecimalUtil.getWithNoZera(goodsTaoLiJinRateService.getGoodsSpreadMoney(taoBaoLink.getGoods())).toString())
                     .replace("{优惠券价}", TaoBaoUtil.getAfterUseCouplePrice(taoBaoLink.getGoods()) + "");
            } else {
               String text = shareGoodsTextTemplateService.geteCouponTemplate(uid);
               if (StringUtil.isNullOrEmpty(text))
                  text = configService.get("goods_share_text_coupon");
               shareText = text.replace("{标题}", taoBaoLink.getGoods().getTitle())
                     .replace("{商品原价}", MoneyBigDecimalUtil.getWithNoZera(taoBaoLink.getGoods().getZkPrice()) + "")
                     .replace("{优惠券面额}",
                           MoneyBigDecimalUtil.getWithNoZera(taoBaoLink.getGoods().getCouponAmount()).toString())
                     .replace("{优惠券价}", TaoBaoUtil.getAfterUseCouplePrice(taoBaoLink.getGoods()) + "");
            }
            String text = shareGoodsTextTemplateService.geteCouponTemplate(uid);
            if (StringUtil.isNullOrEmpty(text))
               text = configService.get("goods_share_text_coupon");
            shareText = text.replace("{标题}", taoBaoLink.getGoods().getTitle())
                  .replace("{商品原价}", MoneyBigDecimalUtil.getWithNoZera(taoBaoLink.getGoods().getZkPrice()) + "")
                  .replace("{优惠券面额}",
                        MoneyBigDecimalUtil.getWithNoZera(taoBaoLink.getGoods().getCouponAmount()).toString())
                  .replace("{优惠券价}", TaoBaoUtil.getAfterUseCouplePrice(taoBaoLink.getGoods()) + "");
         }
         shareText = shareText.replace("{店铺类型}", shopType)
               .replace("{月销量}", TaoBaoUtil.getSaleCount(taoBaoLink.getGoods().getBiz30day()))
               .replace("{领券短链}", shortLink).replace("{淘口令}", taoBaoLink.getTaoToken());
@@ -345,7 +292,7 @@
         });
         return;
      } catch (ShareGoodsException e) {
         LogHelper.errorDetailInfo(e, "分享出错:uid:"+uid+"auctionId:"+auctionId, "");
         LogHelper.errorDetailInfo(e, "分享出错:uid:" + uid + "auctionId:" + auctionId, "");
         // 分享出错报警
         try {
            monitorService.addClientAPIMonitor(MonitorFactory.createClientAPI(request, e.getCode(), 0, "分享出错"));
@@ -354,7 +301,172 @@
         out.print(JsonUtil.loadFalseResult(e.getCode(), e.getMessage()));
         businessEmergent110Service.shareTaoBaoGoodsError(StringUtil.Md5(auctionId + ""));
      }
   }
   /**
    * 淘礼金创建分享
    *
    * @param uid
    * @param auctionId
    * @param totalNum
    * @param relationId
    * @param request
    * @param out
    */
   public void createTaoLijin(Long uid, Long auctionId, int totalNum, String relationId, HttpServletRequest request,
         PrintWriter out) {
      try {
         TaoBaoLink taoBaoLink = shareGoodsService.getTaoLiJinLinkForShare(uid, auctionId, relationId);
         TaoBaoGoodsBrief goods = taoBaoLink.getGoods();
         // 计算推广红包
         String warningRate = configTaoLiJinService.getValueByKey("warning_value");
         BigDecimal spreadMoney = TaoLiJinUtil.getSpreadMoney(warningRate, goods);
         // 推广红包 不能小于1
         if (spreadMoney.compareTo(new BigDecimal(1.1)) < 0) {
            out.print(JsonUtil.loadFalseResult(1, "该商品淘礼金不足"));
            return;
         }
         JSONObject data = new JSONObject();
         data.put("spreadMoney", " ¥" + spreadMoney.toString());
         String taoLiJinLink = null;
         Long tljId = null;
         try {
            UserTaoLiJinRecord record = userTaoLiJinRecordService.createShareTaoLiJin(uid, totalNum, goods);
            tljId = record.getId();
            taoLiJinLink = record.getSendUrl();
            data.put("tljId", tljId);
            data.put("surplusMoney", record.getSurplusMoney());
         } catch (UserTaoLiJinRecordException e) {
            out.print(JsonUtil.loadFalseResult(e.getCode(), e.getMsg()));
            return;
         }
         String url = String.format("http://%s%s?uid=%s&id=%s&appType=flq&tid=%s", configService.getH5Host(),
               Constant.systemCommonConfig.getShareGoodsPagePath(), AESUtil.encrypt(uid + "", Constant.UIDAESKEY),
               auctionId + "", AESUtil.encrypt(tljId + "", Constant.UIDAESKEY));
         String shortLink = HttpUtil.getShortLink(url);
         if (!StringUtil.isNullOrEmpty(shortLink)) {
            url = shortLink;
         }
         data.put("clickUrl", url);
         // 创建淘口令
         if (!StringUtil.isNullOrEmpty(taoLiJinLink)) {// 通过立即推广方式获取淘口令成功
            String quanToken = TaoKeApiUtil.getTKToken(goods.getPictUrl(), goods.getTitle(), taoLiJinLink);
            if (!StringUtil.isNullOrEmpty(quanToken)) {
               taoBaoLink.setTaoToken(quanToken);
            }
         } else if (!StringUtil.isNullOrEmpty(taoBaoLink.getClickUrl())) {
            String quanToken = TaoKeApiUtil.getTKToken(goods.getPictUrl(), goods.getTitle(),
                  taoBaoLink.getClickUrl());
            if (!StringUtil.isNullOrEmpty(quanToken)) {
               taoBaoLink.setTaoToken(quanToken);
            }
         }
         data.put("token", taoBaoLink.getTaoToken());
         data.put("rule",
               "http://apph5.yeshitv.com/apppage/all_help_content.html?id=148&from=singlemessage&isappinstalled=0");
         data.put("pictUrl", TbImgUtil.getTBSizeImg(taoBaoLink.getGoods().getPictUrl(), 500));
         // 无券
         String shopType = taoBaoLink.getGoods().getUserType() == 0 ? "淘宝价" : "天猫价";
         String shareText = "";
         if (StringUtil.isNullOrEmpty(taoBaoLink.getGoods().getCouponInfo())) {
            String text = shareGoodsTextTemplateService.getCommonTemplate(uid);
            if (StringUtil.isNullOrEmpty(text))
               text = configService.get("goods_share_text_nocoupon");
            shareText = text.replace("{标题}", taoBaoLink.getGoods().getTitle()).replace("{商品原价}",
                  MoneyBigDecimalUtil.getWithNoZera(taoBaoLink.getGoods().getZkPrice()) + "");
         } else// 有券
         {
            String text = shareGoodsTextTemplateService.getTaoLiJinTemplate(uid);
            if (StringUtil.isNullOrEmpty(text))
               text = configTaoLiJinService.getValueByKey("goods_share_text");
            shareText = text.replace("{标题}", taoBaoLink.getGoods().getTitle())
                  .replace("{商品原价}", MoneyBigDecimalUtil.getWithNoZera(taoBaoLink.getGoods().getZkPrice()) + "")
                  .replace("{优惠券面额}",
                        MoneyBigDecimalUtil.getWithNoZera(taoBaoLink.getGoods().getCouponAmount()).toString())
                  .replace("{淘礼金面额}", MoneyBigDecimalUtil.getWithNoZera(spreadMoney).toString())
                  .replace("{优惠券价}", TaoBaoUtil.getAfterUseCouplePrice(taoBaoLink.getGoods()) + "");
         }
         shareText = shareText.replace("{店铺类型}", shopType)
               .replace("{月销量}", TaoBaoUtil.getSaleCount(taoBaoLink.getGoods().getBiz30day()))
               .replace("{领券短链}", shortLink).replace("{淘口令}", taoBaoLink.getTaoToken());
         data.put("shareText", shareText);
         String descText = shareText.replace(taoBaoLink.getGoods().getTitle(), "").trim();
         if (descText.startsWith("\\r\\n"))
            descText = descText.substring(0);
         data.put("descText", descText);
         String imgs = configService.get("goods_share_notify_imgs");
         JSONArray array = JSONArray.fromObject(imgs);
         int p = (int) (array.size() * Math.random());
         if (p < array.size())
            data.put("notifyPicture", array.optString(p));
         data.put("notifyDesc", configService.get("goods_share_notify"));
         // 添加分享记录
         BigDecimal rate = hongBaoManageService.getShareRate();
         BigDecimal shareMoney = TaoBaoUtil.getShareGoodsHongBaoInfo(taoBaoLink.getGoods(), rate);
         data.put("shareMoney", "¥" + shareMoney.toString());
         try {
            ShareGoodsRecordDTO shareRecord = userShareGoodsRecordService.addRecordGoodsDetail(uid, auctionId);
            data.put("shareId", shareRecord.getRedisKey());
         } catch (Exception e) {
            try {
               LogHelper.errorDetailInfo(e);
            } catch (Exception e1) {
               e1.printStackTrace();
            }
         }
         out.print(JsonUtil.loadTrueResult(data));
         final TaoBaoLink taobaoLink = taoBaoLink;
         // 异步操作
         com.yeshi.fanli.util.ThreadUtil.run(new Runnable() {
            @Override
            public void run() {
               // 异步操作 添加分享记录
               UserShareGoodsHistory history = new UserShareGoodsHistory();
               history.setUser(new UserInfo(uid));
               history.setHongbao(shareMoney);
               history.setCreateTime(new Date());
               history.setGoodsType(UserShareGoodsHistory.TYPE_TAOBAO);
               history.setTkCode(taobaoLink.getTaoToken());
               history.setLink(taobaoLink.getClickUrl());
               history.setQuanLink(taobaoLink.getCouponLink());
               history.setGoodsId(taobaoLink.getGoods().getAuctionId());
               history.setPostPicture(taobaoLink.getGoods().getPictUrl());
               List<String> imgList = taobaoLink.getGoods().getImgList();
               if (imgList == null) {
                  imgList = new ArrayList<>();
               }
               history.setPictures(JsonUtil.getGson().toJson(imgList));
               shareGoodsService.addShareGoodsHistory(history);
            }
         });
         return;
      } catch (ShareGoodsException e) {
         LogHelper.errorDetailInfo(e, "分享出错:uid:" + uid + "auctionId:" + auctionId, "");
         // 分享出错报警
         try {
            monitorService.addClientAPIMonitor(MonitorFactory.createClientAPI(request, e.getCode(), 0, "分享出错"));
         } catch (Exception e1) {
         }
         out.print(JsonUtil.loadFalseResult(e.getCode(), e.getMessage()));
         businessEmergent110Service.shareTaoBaoGoodsError(StringUtil.Md5(auctionId + ""));
      }
   }
   /**
@@ -533,7 +645,7 @@
    * @param out
    */
   @RequestMapping(value = "viewShareTextTemplate", method = RequestMethod.POST)
   public void preViewShareTextTemplate(AcceptData acceptData, Long uid, String template, Long goodsId, Long tljId,
   public void preViewShareTextTemplate(AcceptData acceptData, Long uid, String template, Long goodsId, Long tljId,
         Boolean hasCoupon, PrintWriter out) {
      if (uid == null) {
         out.print(JsonUtil.loadFalseResult(1, "用户未登录"));
@@ -557,7 +669,7 @@
         out.print(JsonUtil.loadFalseResult(4, "商品已下架"));
         return;
      }
      if (StringUtil.isNullOrEmpty(template))
         template = getShareTemplate(uid, hasCoupon, tljId);
@@ -602,10 +714,9 @@
               Constant.systemCommonConfig.getShareGoodsPagePath(), AESUtil.encrypt(uid + "", Constant.UIDAESKEY),
               goods.getAuctionId() + "");
      }
      String shortLink = HttpUtil.getShortLink(url);
      // 查询是否有分享
      UserShareGoodsHistory userShareHistory = shareGoodsService.getShareGoodsHistory(uid, goods.getAuctionId());
      String token = "";
@@ -700,7 +811,7 @@
         out.print(JsonUtil.loadFalseResult(1, "用户未登录"));
         return;
      }
      if (hasCoupon) {
         if (tljId != null) {
            shareGoodsTextTemplateService.resetTaoLijinTemplate(uid);
@@ -732,8 +843,7 @@
               template = configService.get("goods_share_text_coupon");
            return template;
         }
      } else {
         String template = shareGoodsTextTemplateService.getCommonTemplate(uid);
         if (StringUtil.isNullOrEmpty(template))
@@ -756,12 +866,12 @@
         out.print(JsonUtil.loadFalseResult(1, "用户未登录"));
         return;
      }
      if (hasCoupon == null) {
         out.print(JsonUtil.loadFalseResult(1, "是否有券字段为空"));
         return;
      }
      JSONObject data = new JSONObject();
      data.put("template", getShareTemplate(uid, hasCoupon, tljId));
      out.print(JsonUtil.loadTrueResult(data));
@@ -776,7 +886,7 @@
   @RequestMapping(value = "getShareTextTemplateRules", method = RequestMethod.POST)
   public void getShareTextTemplateRules(AcceptData acceptData, Long tljId, PrintWriter out) {
      if (tljId != null) {
         out.print(JsonUtil.loadTrueResult( configTaoLiJinService.getValueByKey("share_goods_rules")));
         out.print(JsonUtil.loadTrueResult(configTaoLiJinService.getValueByKey("share_goods_rules")));
      } else {
         out.print(JsonUtil.loadTrueResult(configService.get("share_goods_template_rules")));
      }
@@ -841,9 +951,8 @@
                  title += textStyleVO.getContent();
               }
            }
            if(StringUtil.isNullOrEmpty(title)) {
            if (StringUtil.isNullOrEmpty(title)) {
               title = "给你推荐精选好物优惠券,购物前先领券,别错过优惠哦~";
            }
            data.put("title", title);