yj
2020-03-09 8c6123908fd6bc5a02938dd1e69750b277aa5c6a
fanli/src/main/java/com/yeshi/fanli/service/impl/dynamic/GoodsEvaluateServiceImpl.java
@@ -31,6 +31,7 @@
import com.yeshi.fanli.dto.jd.JDCouponInfo;
import com.yeshi.fanli.dto.pdd.PDDGoodsDetail;
import com.yeshi.fanli.entity.bus.activity.ActivityUser;
import com.yeshi.fanli.entity.bus.clazz.GoodsClass;
import com.yeshi.fanli.entity.dynamic.CommentInfo;
import com.yeshi.fanli.entity.dynamic.CommentInfo.CommentInfoEnum;
import com.yeshi.fanli.entity.dynamic.DynamicInfo;
@@ -44,6 +45,7 @@
import com.yeshi.fanli.entity.system.ConfigKeyEnum;
import com.yeshi.fanli.entity.taobao.TaoBaoGoodsBrief;
import com.yeshi.fanli.entity.taobao.TaoBaoLink;
import com.yeshi.fanli.entity.taobao.dataoke.DaTaoKeDetailV2;
import com.yeshi.fanli.exception.dynamic.ActivityUserException;
import com.yeshi.fanli.exception.dynamic.GoodsEvaluateException;
import com.yeshi.fanli.exception.goods.ConvertLinkExceptionException;
@@ -54,6 +56,7 @@
import com.yeshi.fanli.service.inter.dynamic.GoodsEvaluateService;
import com.yeshi.fanli.service.inter.goods.ShareGoodsService;
import com.yeshi.fanli.service.inter.order.config.HongBaoManageService;
import com.yeshi.fanli.service.inter.taobao.dataoke.DaTaoKeGoodsDetailV2Service;
import com.yeshi.fanli.service.manger.goods.ConvertLinkManager;
import com.yeshi.fanli.util.Constant;
import com.yeshi.fanli.util.FilePathEnum;
@@ -66,7 +69,9 @@
import com.yeshi.fanli.util.jd.JDApiUtil;
import com.yeshi.fanli.util.jd.JDUtil;
import com.yeshi.fanli.util.pinduoduo.PinDuoDuoApiUtil;
import com.yeshi.fanli.util.taobao.DaTaoKeUtil;
import com.yeshi.fanli.util.taobao.TaoBaoUtil;
import com.yeshi.fanli.util.taobao.TaoKeApiUtil;
import com.yeshi.fanli.vo.goods.CouponInfoVO;
import com.yeshi.fanli.vo.goods.GoodsDetailVO;
import com.yeshi.fanli.vo.msg.ClientTextStyleVO;
@@ -103,6 +108,11 @@
   @Resource
   private ConvertLinkManager convertLinkManager;
   @Resource
   private DaTaoKeGoodsDetailV2Service daTaoKeGoodsDetailV2Service;
   @Override
   public String saveHead(MultipartFile file, GoodsEvaluate record) throws GoodsEvaluateException {
@@ -110,6 +120,7 @@
      if (state == null) {
         state = 0;
      }
      record.setState(state);
      String tilte = record.getTitle();
      if (StringUtil.isNullOrEmpty(tilte)) {
@@ -161,6 +172,7 @@
         if (resultObj == null)
            throw new GoodsEvaluateException(1, "修改内容已不存在");
         resultObj.setState(record.getState());
         resultObj.setUser(record.getUser());
         resultObj.setShareNum(record.getShareNum());
         resultObj.setWeight(record.getWeight());
@@ -427,6 +439,8 @@
      if (totalImg > 0) {
         if (totalImg == 1) {
            lineNum = 1;
         } else if (totalImg == 3) {
            lineNum = 3;
         } else if (totalImg <= 4) {
            lineNum = 2;
         } else {
@@ -752,6 +766,8 @@
      if (totalImg > 0) {
         if (totalImg == 1) {
            lineNum = 1;
         } else if (totalImg == 3) {
            lineNum = 3;
         } else if (totalImg <= 4) {
            lineNum = 2;
         } else {
@@ -1103,6 +1119,8 @@
      if (totalImg > 0) {
         if (totalImg == 1) {
            lineNum = 1;
         } else if (totalImg == 3) {
            lineNum = 3;
         } else if (totalImg <= 4) {
            lineNum = 2;
         } else {
@@ -1241,7 +1259,7 @@
                     ImgInfo imgInfo0 = new ImgInfo();
                     imgInfo0.setId(UUID.randomUUID().toString().replace("-", ""));
                     imgInfo0.setLarge(true);
                     imgInfo0.setLarge(false);
                     imgInfo0.setPid(pid);
                     imgInfo0.setUrl(picLink);
                     imgInfo0.setUrlHD(picLink);
@@ -1289,6 +1307,8 @@
      if (totalImg > 0) {
         if (totalImg == 1) {
            lineNum = 1;
         } else if (totalImg == 3) {
            lineNum = 3;
         } else if (totalImg <= 4) {
            lineNum = 2;
         } else {
@@ -1472,14 +1492,14 @@
   }
   @Override
   @Cacheable(value = "dynamicCache", key = "'queryMaterialsCache-'+#start+'-'+#type")
//   @Cacheable(value = "dynamicCache", key = "'queryMaterialsCache-'+#start+'-'+#type")
   public List<GoodsEvaluate> queryMaterialsCache(int start, int count, int type) throws Exception {
      List<GoodsEvaluate> list = goodsEvaluateDao.queryValid(start, count, type);
      if (list == null) {
         list = new ArrayList<>();
      }
      Date now = new Date();
      SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
      SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
      List<GoodsEvaluate> listNew = new ArrayList<>();
      for (GoodsEvaluate goodsEvaluate : list) {
@@ -1527,23 +1547,26 @@
               if (!StringUtil.isNullOrEmpty(coupon) && !StringUtil.isNullOrEmpty(endTime)) {
                  Date endDay = sdf.parse(endTime);
                  if (endDay.getTime() > now.getTime()) {
                     int daysBetween = DateUtil.daysBetween(now, endDay);
                     if (daysBetween > 0) {
                     String daysBetween = DateUtil.dateDiff5(now, endDay);
                     if (!StringUtil.isNullOrEmpty(daysBetween)) {
                        List<ClientTextStyleVO> tagListNew = new ArrayList<>();
                        List<ClientTextStyleVO> tagList = commentInfoNew.getTagList();
                        if (tagList != null) {
                           tagListNew.addAll(tagList);
                        }
                        ClientTextStyleVO styleVO = new ClientTextStyleVO();
                        styleVO.setColor("#E5005C");
                        styleVO.setContent(daysBetween + "天后过期");
                        List<ClientTextStyleVO> tagList = commentInfoNew.getTagList();
                        tagList.add(styleVO);
                        commentInfoNew.setTagList(tagList);
                        styleVO.setContent(daysBetween + "后过期");
                        tagListNew.add(styleVO);
                        commentInfoNew.setTagList(tagListNew);
                        commentNew.add(commentInfoNew);
                        continue;
                     }
                  }
               }
               commentNew.add(commentInfo);
               commentNew.add(commentInfoNew);
            }
            evaluateNew.setComments(commentNew);
@@ -1790,7 +1813,9 @@
               }
               imgInfo.setGoods(simpleGoods);
               TaoBaoLink taoBaoLink = shareGoodsService.getTaoBaoLinkForShare(1L, goodsVO.getGoodsId(), null);
               TaoBaoLink taoBaoLink = shareGoodsService.getTaoBaoLinkForShare(1L, goodsVO.getGoodsId(), "0");
//               TaoBaoLink taoBaoLink = shareGoodsService.getTaoBaoLinkForShare(Constant.LINK_TOKEN_VERIFY_UID, goodsVO.getGoodsId(), null);
               String template = configService.get(ConfigKeyEnum.quickShareTBCommentText.getKey());
               String commentText = template.replace("[淘口令]", TaoBaoUtil.filterTaoToken(taoBaoLink.getTaoToken()));
@@ -1826,4 +1851,271 @@
      }
   }
   @Override
   public void addGoodsEvaluateByDaTaoKe() {
      try {
         boolean addSuccess = false;
         List<Long> listId = null;
         List<GoodsEvaluate> listExist = goodsEvaluateDao.querySingleExist();
         if (listExist != null && listExist.size() > 0) {
            listId = new ArrayList<Long>();
            for (GoodsEvaluate goodsEvaluate : listExist) {
               GoodsDetailVO goods = goodsEvaluate.getGoods();
               if (goods != null && goods.getGoodsType() == 1) {
                  listId.add(goods.getGoodsId());
               }
            }
         }
         // 热销单品的分类
         List<GoodsClass> listClass = DaTaoKeUtil.goodsClasses;
         int ci = (int) (Math.random() * listClass.size());
         Long subId = listClass.get(ci).getId();
         List<DaTaoKeDetailV2> list = daTaoKeGoodsDetailV2Service.getGoodsNotInList(subId, listId, 1);
         if (list == null || list.size() == 0) {
            return;
         }
         DaTaoKeDetailV2 daTaoKe = list.get(0);
         TaoBaoGoodsBrief goodsBrief = redisManager.getTaoBaoGoodsBrief(daTaoKe.getGoodsId());
         if (goodsBrief == null) {
            return;
         }
         BigDecimal fanLiRate = hongBaoManageService.getFanLiRate();
         BigDecimal shareRate = hongBaoManageService.getShareRate();
         BigDecimal vipFanLiRate = hongBaoManageService.getVIPFanLiRate();
         ConfigParamsDTO params = new ConfigParamsDTO(fanLiRate, shareRate, Constant.MAX_REWARD_RATE, vipFanLiRate);
         GoodsDetailVO goodsVO = GoodsDetailVOFactory.convertTaoBao(goodsBrief, params);
         // 发布用户
         ActivityUser user = activityUserService.getRandomByDaTaoKeCid(daTaoKe.getCid());
//         String desc = getDesc(daTaoKe.getDesc());
         String desc = daTaoKe.getDesc().replace(",", ",");
         System.out.println(desc);
         String[] split = desc.split(",");
         System.out.println(split.length);
         int max = 1;
         if (Math.random() > 0.5) {
            max = 2;
         }
         int e = 0;
         String descNew = "";
         if (split.length > 0) {
            for (int i = 0; i < split.length; i ++ ) {
               if (Math.random() > 0.5 && e < max) {
                  e++;
                  descNew += split[i] +  DaTaoKeUtil.getRandomCommonEmoji();
               } else {
                  descNew += split[i] + ",";
               }
            }
         } else {
            descNew = desc;
         }
         if(descNew.endsWith(",")) {
            descNew = descNew.substring(0, descNew.length()-1);
         }
         GoodsEvaluate goodsEvaluate = new GoodsEvaluate();
         goodsEvaluate.setId(UUID.randomUUID().toString().replace("-", ""));
         goodsEvaluate.setUser(user);
         goodsEvaluate.setTitle(descNew);
         goodsEvaluate.setState(1);
         goodsEvaluate.setDynamicType(1);
         goodsEvaluate.setType(EvaluateEnum.single);
         goodsEvaluate.setShareNum((int) (Math.random() * 5000) + 1000);
         goodsEvaluate.setShareNumReal(0);
         goodsEvaluate.setWeight(0.0);
         goodsEvaluate.setStartTime(new Date());
         goodsEvaluate.setEndTime(DateUtil.plusDayDate(3, new Date()));
         goodsEvaluate.setPublishTime(new Date());
         goodsEvaluate.setCreateTime(new Date());
         goodsEvaluate.setUpdateTime(new Date());
         int lineNum = 0;
         List<String> imgs = goodsBrief.getImgList();
         if (imgs.size() > 0) {
            if (imgs.size() == 1) {
               lineNum = 1;
            } else if (imgs.size() == 3) {
               lineNum = 3;
            } else if (imgs.size() <= 4) {
               lineNum = 2;
            } else {
               lineNum = 3;
            }
         }
         goodsEvaluate.setLineNum(lineNum);
         int i = 0;
         List<ImgInfo> imgList = new ArrayList<>();
         for (String img : imgs) {
            ImgInfo imgInfo = new ImgInfo();
            imgInfo.setH(1);
            imgInfo.setW(1);
            imgInfo.setLarge(false);
            imgInfo.setUrl(img);
            imgInfo.setUrlHD(img);
            if (i != 0) {
               imgInfo.setType(ImgEnum.img);
            } else {
               imgInfo.setType(ImgEnum.goods);
               SimpleGoods simpleGoods = new SimpleGoods();
               simpleGoods.setGoodsId(goodsVO.getGoodsId());
               simpleGoods.setGoodsType(goodsVO.getGoodsType());
               simpleGoods.setState(goodsVO.getState());
               CouponInfoVO couponInfo = goodsVO.getCouponInfo();
               if (couponInfo == null) {
                  simpleGoods.setPrice(goodsVO.getZkPrice());
               } else {
                  simpleGoods.setPrice(goodsVO.getCouponPrice());
                  simpleGoods.setAmount(couponInfo.getAmount());
               }
               imgInfo.setGoods(simpleGoods);
               //TaoBaoLink taoBaoLink = shareGoodsService.getTaoBaoLinkForShare(Constant.LINK_TOKEN_VERIFY_UID, goodsVO.getGoodsId(), null);
               String token = shareGoodsService.createTaoBaoToken(Constant.LINK_TOKEN_VERIFY_UID, goodsBrief);
               String template = configService.get(ConfigKeyEnum.quickShareTBCommentText.getKey());
               String commentText = template.replace("[淘口令]", TaoBaoUtil.filterTaoToken(token));
               commentText = commentText.replace("[原价]", goodsVO.getZkPrice().toString());
               if (!goodsVO.isHasCoupon()) {
                  commentText = commentText.replace("领券抢购", "抢购");
                  commentText = commentText.replace("【券后价】[券后价]元", "");
               } else {
                  commentText = commentText.replace("[券后价]", goodsVO.getCouponPrice().toString());
               }
               commentText = commentText.replace("\r\n\r\n", "\r\n").replace("\r\n\r\n", "\r\n")
                     .replace("\r\n\r\n", "\r\n");
               CommentInfo commentInfo = new CommentInfo();
               commentInfo.setId(UUID.randomUUID().toString().replace("-", ""));
               commentInfo.setContent(commentText);
               commentInfo.setTypeEnum(CommentInfoEnum.goodsCoupon);
               commentInfo.setType(CommentInfoEnum.goodsCoupon.getDesc());
               List<CommentInfo> commentsNew = new ArrayList<>();
               commentsNew.add(commentInfo);
               goodsEvaluate.setComments(commentsNew);
               goodsEvaluate.setGoods(goodsVO);
            }
            imgList.add(imgInfo);
            i ++;
         }
         goodsEvaluate.setImgList(imgList);
         goodsEvaluateDao.save(goodsEvaluate);
      } catch (Exception e) {
         e.printStackTrace();
         LogHelper.errorDetailInfo(e);
      }
   }
   private String getDesc(String desc) {
      System.out.println(desc);
      String newDesc = "";
      int emoji = 0;
      String content = desc.replace(",", ",");
      while (content.length() > 0) {
         int length = 0;
         boolean end = true;
         for (int i = 0; i < content.length(); i++) {
            if (content.substring(0, i).length() >= 18) {
               length = i;
               end = false;
               break;
            }
         }
         if (end) {
            length = content.length();
         }
         String introduce = content.substring(0, length);
         System.out.println(introduce);
         if (verify(introduce)) {
            if (emoji >= 4) {
               introduce = introduce.replace(",", "\n"); // 需要换行
            } else {
               emoji ++;
               introduce = introduce.replace(",", DaTaoKeUtil.getRandomCommonEmoji() + "\n"); // 需要换行
            }
         }
         // 随机替换emoji
         if(emoji <= 4 && Math.random() > 0.5) {
            introduce = introduce.replace(",", DaTaoKeUtil.getRandomCommonEmoji());
            emoji ++;
         }
         newDesc += introduce;
         content = content.substring(length);
      }
      return newDesc;
   }
   private boolean verify (String content) {
      // 18个字符内(包含标点也算一个字符)有多个逗号,则不换行
      char d = ',';
      int count = 0;
      char chs[]= content.toCharArray();//转换成char数组
      for(int i=0;i<chs.length;i++) {
         if(d == chs[i]) {
            count ++;
         }
      }
      if (count > 1) {
         return false;
      }
      // 书名号,大括号,小括号里面的内容不换行
      if (content.contains("[") && content.contains("]")) {
         return false;
      } else if (content.contains("【") && content.contains("】")) {
         return false;
      } else if (content.contains("{") && content.contains("}")) {
         return false;
      } else if (content.contains("(") && content.contains(")")) {
         return false;
      }
      int index = content.lastIndexOf(",");
      // (除开逗号以外的符号)感叹号/句号/问号前面有逗号,不换行
      if (content.contains("!") && index < content.lastIndexOf("!")) {
         return false;
      }
      if (content.contains("!") && index < content.lastIndexOf("!")) {
         return false;
      }
      if (content.contains("。") && index < content.lastIndexOf("。")) {
         return false;
      }
      if (content.contains("?") && index < content.lastIndexOf("?")) {
         return false;
      }
      if (content.contains("?") && index < content.lastIndexOf("?")) {
         return false;
      }
      if (content.contains(":")) {
         return false;
      }
      return true;
   }
}