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;
@@ -1854,7 +1855,27 @@
   @Override
   public void addGoodsEvaluateByDaTaoKe() {
      try {
         List<DaTaoKeDetailV2> list = daTaoKeGoodsDetailV2Service.getGoodsNotInList(1L, null, 1);
         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;
         }
@@ -1873,19 +1894,40 @@
         // 发布用户
         ActivityUser user = activityUserService.getRandomByDaTaoKeCid(daTaoKe.getCid());
         
         String desc = DaTaoKeUtil.getDesc(daTaoKe);
         if (!StringUtil.isNullOrEmpty(desc)) {
            int index = desc.lastIndexOf("\n");
            String newDesc = desc.substring(index + 1);
            if (StringUtil.isNullOrEmpty(newDesc)) {
               desc = desc.substring(0, index);
            }
//         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(desc);
         goodsEvaluate.setTitle(descNew);
         goodsEvaluate.setState(1);
         goodsEvaluate.setDynamicType(1);
         goodsEvaluate.setType(EvaluateEnum.single);
@@ -1899,16 +1941,14 @@
         goodsEvaluate.setCreateTime(new Date());
         goodsEvaluate.setUpdateTime(new Date());
         
         List<ImgInfo> imgList = new ArrayList<>();
         int lineNum = 0;
         if (imgList.size() > 0) {
            if (imgList.size() == 1) {
         List<String> imgs = goodsBrief.getImgList();
         if (imgs.size() > 0) {
            if (imgs.size() == 1) {
               lineNum = 1;
            } else if (imgList.size() == 3) {
            } else if (imgs.size() == 3) {
               lineNum = 3;
            } else if (imgList.size() <= 4) {
            } else if (imgs.size() <= 4) {
               lineNum = 2;
            } else {
               lineNum = 3;
@@ -1917,7 +1957,7 @@
         goodsEvaluate.setLineNum(lineNum);
         
         int i = 0;
         List<String> imgs = goodsBrief.getImgList();
         List<ImgInfo> imgList = new ArrayList<>();
         for (String img : imgs) {
            ImgInfo imgInfo = new ImgInfo();
            imgInfo.setH(1);
@@ -1982,4 +2022,100 @@
         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;
   }
}