Browse Source

计划名称超过50个字符则截取素材

yangzian 3 năm trước cách đây
mục cha
commit
59e60dc838

+ 10 - 2
jeecg-boot-bytedance/src/main/java/org/jeecg/modules/bytedance/advertise/service/impl/AiBytedanceAdvertiserStrategyServiceImpl.java

@@ -159,6 +159,10 @@ public class AiBytedanceAdvertiserStrategyServiceImpl extends ServiceImpl<AiByte
                 }
                 //计划名称
                 String adName = getAdName(strategy,null,video,userAllocation.getProjectId());
+                log.info("计划原名称=============>>>>>>>>>>{};长度为{}",adName,StringUtils.length(adName));
+                //截取 计划名称
+                adName = StringUtils.adNameSubMaterial(adName);
+                log.info("计划截取后名称===========>>>>>>>>>>{};长度为{}",adName,StringUtils.length(adName));
                 //计算本次出价
                 BigDecimal currentCpaBid = StringUtils.getRandomAdCpaBid(strategy.getAdBidCreateType(),strategy.getAdMaxBid(),strategy.getAdMinBid(),strategy.getAdStepBid(),adDplinkInfo.getAdBid(),strategy.getAdCpaBid());
                 JSONObject adParams = getAdParams(strategy,adName,campaignId,currentCpaBid);
@@ -254,6 +258,10 @@ public class AiBytedanceAdvertiserStrategyServiceImpl extends ServiceImpl<AiByte
                 String dplinkCode = StringUtils.getParamFromUrl(strategy.getAdOpenUrl(),"bc_fl_src");
                 //计划名称
                 String adName = getAdName(strategy,dplinkCode,video,userAllocation.getProjectId());
+                log.info("计划原名称=============>>>>>>>>>>{};长度为{}",adName,StringUtils.length(adName));
+                //截取 计划名称
+                adName = StringUtils.adNameSubMaterial(adName);
+                log.info("计划截取后名称===========>>>>>>>>>>{};长度为{}",adName,StringUtils.length(adName));
                 //计算本次出价
                 BigDecimal currentCpaBid = StringUtils.getRandomAdCpaBid(strategy.getAdBidCreateType(),strategy.getAdMaxBid(),strategy.getAdMinBid(),strategy.getAdStepBid(),adDplinkInfo.getAdBid(),strategy.getAdCpaBid());
                 adDplinkInfo.setAdBid(currentCpaBid);
@@ -1034,10 +1042,10 @@ public class AiBytedanceAdvertiserStrategyServiceImpl extends ServiceImpl<AiByte
             if ("video".equals(videoInfo.getMaterialType())){
                 MaterialInfo info = materialInfoService.getParams(videoInfo.getSignature(),null,projectId);
                 if(null!=info&&null!=info.getMaterialName()){
-                    adName = adName.replace("{{素材名称}}",info.getMaterialName());
+                    adName = adName.replace("{{素材名称}}","["+info.getMaterialName()+"]");
                 }
             }else {
-                adName = adName.replace("{{素材名称}}",videoInfo.getFilename());
+                adName = adName.replace("{{素材名称}}","["+videoInfo.getFilename()+"]");
             }
         }
 

+ 100 - 0
jeecg-boot-bytedance/src/main/java/org/jeecg/modules/bytedance/common/utils/StringUtils.java

@@ -285,4 +285,104 @@ public class StringUtils {
         String newStr = str.substring(left+1, right);
         return newStr;
     }
+
+
+
+    /**
+     * 获取字符串的长度,如果有中文,则每个中文字符计为2位
+     * @param value 指定的字符串
+     * @return 字符串的长度
+     */
+    public static int length(String value) {
+        int valueLength = 0;
+        String chinese = "[\u0391-\uFFE5]";
+        Pattern number = Pattern.compile("^[-\\+]?[\\d]*$");
+        /* 获取字段值的长度,如果含中文字符,则每个中文字符长度为2,否则为1 */
+        for (int i = 0; i < value.length(); i++) {
+            /* 获取一个字符 */
+            String temp = value.substring(i, i + 1);
+            /* 判断是否为中文字符 */
+            if (temp.matches(chinese)) {
+                valueLength += 2;
+            }else {
+                /* 其他字符长度为1 */
+                valueLength += 1;
+            }
+        }
+        return valueLength;
+    }
+
+
+    /**
+     * 字符串(如果是中文则占2个字符 的 字符串) 截取
+     * @param str 如果是中文则占2个字符 的 字符串
+     * @param length 要截取的长度
+     * @return
+     * @throws Exception
+     */
+    public static String subStr50(String str, int length) throws Exception {
+        byte[] bytes = str.getBytes("Unicode");
+        int n = 0; // 表示当前的字节数
+        int i = 2; // 要截取的字节数,从第3个字节开始
+        for (; i < bytes.length && n < length; i++) {
+            // 奇数位置,如3、5、7等,为UCS2编码中两个字节的第二个字节
+            if (i % 2 == 1) {
+                n++; // 在UCS2第二个字节时n加1
+            }
+            else {
+                // 当UCS2编码的第一个字节不等于0时,该UCS2字符为汉字,一个汉字算两个字节
+                if (bytes[i] != 0) {
+                    n++;
+                }
+            }
+        }
+        // 如果i为奇数时,处理成偶数
+        if (i % 2 == 1) {
+            // 该UCS2字符是汉字时,去掉这个截一半的汉字
+            if (bytes[i - 1] != 0){
+                i = i - 1;
+                // 该UCS2字符是字母或数字,则保留该字符
+            }else{
+                i = i + 1;
+            }
+        }
+        return new String(bytes, 0, i, "Unicode");
+    }
+
+
+    /**
+     * 计划名称 中的 素材名称 截取
+     * @param adName
+     * @return
+     * @throws Exception
+     */
+    public static String adNameSubMaterial(String adName){
+        try {
+            //String a = "20210814-促活-站内-RTA-sf_jrtt_huichuangch_ptb_297571-ocpm-[这是这是这是这这是这是这是这是这是这是这是这是这这是这是这是这是这是]-0728";
+            //获取[] 中的内容 --素材名称
+            String materialName = adName.substring(adName.indexOf("["),adName.indexOf("]")+1);
+            //广告计划名称 总长度(1个中文 占 2个字符)
+            int adNameLength = length(adName.replaceAll("[\\[\\]]", ""));
+            //素材名称 长度 (1个中文 占 2个字符)
+            int materialLength = length(materialName.replaceAll("[\\[\\]]", ""));
+            // 计划名称 字符总长度
+            int totalLength = (adNameLength / 2) + (adNameLength % 2);
+            //不能超过 47个长度
+            if (totalLength > 47){
+                //要截取的素材名称的长度
+                int sub = (totalLength - 47) * 2 ;
+                // 去除 [] 并截取素材名称
+                String mateName = materialName.replaceAll("[\\[\\]]", "");
+                String rep = subStr50(mateName,materialLength - sub);
+                return adName.replace(materialName, rep);
+            }else {
+                return adName.replaceAll("[\\[\\]]", "");
+            }
+        }catch (Exception e){
+            e.printStackTrace();
+        }
+        return null;
+    }
+
+
 }