瀏覽代碼

自动投放,添加缓存校验组数量

zhaoxian 3 年之前
父節點
當前提交
b5eb6b2e4a

+ 64 - 25
jeecg-boot-module-system/src/main/java/cn/com/ctop/kuaishou/modules/ai/service/impl/AiKuaishouCreateCreativeServiceImpl.java

@@ -193,6 +193,7 @@ public class AiKuaishouCreateCreativeServiceImpl implements IAiKuaishouCreateCre
                                 unitLevelOperationRecordService.saveOrUpdate(unitLevelOperationRecord);
                                 continue;
                             }
+                            createUnitCnt(strategy.getAccountId() + DateUtils.date2Str() + "_old_custom_cnt");
                             Long unitId = (Long) unitCreateResult.get("unitId");
                             unitLevelOperationRecord.setUnitId(unitId);
                             unitLevelOperationRecordService.saveOrUpdate(unitLevelOperationRecord);
@@ -273,6 +274,7 @@ public class AiKuaishouCreateCreativeServiceImpl implements IAiKuaishouCreateCre
                             unitLevelOperationRecordService.saveOrUpdate(unitLevelOperationRecord);
                             continue;
                         }
+                        createUnitCnt(strategy.getAccountId() + DateUtils.date2Str() + "_old_custom_cnt");
                         Long unitId = (Long) unitCreateResult.get("unitId");
                         unitLevelOperationRecord.setUnitId(unitId);
                         unitLevelOperationRecordService.saveOrUpdate(unitLevelOperationRecord);
@@ -410,6 +412,7 @@ public class AiKuaishouCreateCreativeServiceImpl implements IAiKuaishouCreateCre
                         unitLevelOperationRecordService.saveOrUpdate(unitLevelOperationRecord);
                         continue;
                     }
+                    createUnitCnt(strategy.getAccountId() + DateUtils.date2Str() + "_old_custom_cnt");
                     Long unitId = (Long) unitCreateResult.get("unitId");
                     unitLevelOperationRecord.setUnitId(unitId);
                     unitLevelOperationRecordService.saveOrUpdate(unitLevelOperationRecord);
@@ -547,6 +550,7 @@ public class AiKuaishouCreateCreativeServiceImpl implements IAiKuaishouCreateCre
                                 unitLevelOperationRecordService.saveOrUpdate(unitLevelOperationRecord);
                                 continue;
                             }
+                            createUnitCnt(strategy.getAccountId() + DateUtils.date2Str() + "_old_program_cnt");
                             Long unitId = (Long) unitCreateResult.get("unitId");
                             unitLevelOperationRecord.setUnitId(unitId);
                             unitLevelOperationRecordService.saveOrUpdate(unitLevelOperationRecord);
@@ -626,6 +630,7 @@ public class AiKuaishouCreateCreativeServiceImpl implements IAiKuaishouCreateCre
                                 unitLevelOperationRecordService.saveOrUpdate(unitLevelOperationRecord);
                                 continue;
                             }
+                            createUnitCnt(strategy.getAccountId() + DateUtils.date2Str() + "_old_program_cnt");
                             Long unitId = (Long) unitCreateResult.get("unitId");
                             unitLevelOperationRecord.setUnitId(unitId);
                             unitLevelOperationRecordService.saveOrUpdate(unitLevelOperationRecord);
@@ -767,6 +772,7 @@ public class AiKuaishouCreateCreativeServiceImpl implements IAiKuaishouCreateCre
                         unitLevelOperationRecordService.saveOrUpdate(unitLevelOperationRecord);
                         continue;
                     }
+                    createUnitCnt(strategy.getAccountId() + DateUtils.date2Str() + "_old_program_cnt");
                     Long unitId = (Long) unitCreateResult.get("unitId");
                     unitLevelOperationRecord.setUnitId(unitId);
                     unitLevelOperationRecordService.saveOrUpdate(unitLevelOperationRecord);
@@ -795,32 +801,61 @@ public class AiKuaishouCreateCreativeServiceImpl implements IAiKuaishouCreateCre
                 customUnitCnt = 2000L / imageCnt;
             }
         }
-
-        Long unitNum = customUnitCnt * hour / 20;
-        log.info("{}当前时间内需要创建组总数:{}", strategy.getAccountId(), unitNum);
+        String date = DateUtils.formatDate(new Date());
         //查询当前账户创建自定义类型组数量
-        int unitCreateCnt = groupService.queryToDayBuiltCount(strategy.getAccountId(), 4);
+        Integer unitCreateCnt = queryUnitCnt(strategy.getAccountId() + date + "_old_custom_cnt");
+        Long unitNum = 0L;
+        if (!Check.isNull(unitCreateCnt)) {
+            unitNum = customUnitCnt - unitCreateCnt;
+        }
+        unitNum = unitNum * hour / 20;
+        log.info("{}当前时间内需要创建组总数:{}", strategy.getAccountId(), unitNum);
+
+//        int unitCreateCnt = groupService.queryToDayBuiltCount(strategy.getAccountId(), 4);
         log.info("{}截止目前创建组总数:{}", strategy.getAccountId(), unitCreateCnt);
-        long remindCnt = unitNum - unitCreateCnt;
-        if (remindCnt >= 1) {
-            log.info("{}组创建不足,剩余需要创建个数{},使用历史遗漏素材创建", strategy.getAccountId(), remindCnt);
+        if (unitNum >= 1) {
+            log.info("{}组创建不足,剩余需要创建个数{},使用历史遗漏素材创建", strategy.getAccountId(), unitNum);
             //优先创建历史遗漏素材
-            remindCnt = this.autoCreateCreative(strategy, 3, remindCnt);
+            unitNum = this.autoCreateCreative(strategy, 3, unitNum);
         }
-        if (remindCnt >= 1) {
-            log.info("{}组创建不足,剩余需要创建个数{},使用高质量素材创建", strategy.getAccountId(), remindCnt);
+        if (unitNum >= 1) {
+            log.info("{}组创建不足,剩余需要创建个数{},使用高质量素材创建", strategy.getAccountId(), unitNum);
             //高质量素材
-            remindCnt = this.autoCreateCreative(strategy, 2, remindCnt);
+            unitNum = this.autoCreateCreative(strategy, 2, unitNum);
         }
-        if (remindCnt >= 1) {
+        if (unitNum >= 1) {
             //历史打捞素材
-            log.info("{}组创建不足,剩余需要创建个数{},使用历史打捞素材创建", strategy.getAccountId(), remindCnt);
-            this.autoCreateCreative(strategy, 4, remindCnt);
+            log.info("{}组创建不足,剩余需要创建个数{},使用历史打捞素材创建", strategy.getAccountId(), unitNum);
+            this.autoCreateCreative(strategy, 4, unitNum);
+        }
+    }
+
+    /**
+     * 自定义key格式:【accountId + date+"_old_custom_cnt"】
+     * 程序化key格式:【accountId + date+"_old_program_cnt"】
+     * 查询当日创建广告组数量
+     */
+    private Integer queryUnitCnt(String key) {
+        return (Integer) redisUtil.get(key);
+    }
+
+    /**
+     * 自定义key格式:【accountId + date+"_old_custom_cnt"】
+     * 程序化key格式:【accountId + date+"_old_program_cnt"】
+     * 缓存当日创建广告组数量
+     */
+    private void createUnitCnt(String key) {
+        Integer unitCnt = (Integer) redisUtil.get(key);
+        if (Check.isNull(unitCnt)) {
+            redisUtil.set(key, 1, 100000L);
+        } else {
+            redisUtil.set(key, unitCnt + 1, 100000L);
         }
     }
 
     @Override
     public void customCreativeLimit(AiKuaishouAdvertiserStrategy strategy) {
+        String date = DateUtils.formatDate(new Date());
         Long unitNum = strategy.getCustomUnitCnt();
         if (Check.isNull(unitNum)) {
             Integer imageCnt = strategy.getImageCnt();
@@ -832,12 +867,16 @@ public class AiKuaishouCreateCreativeServiceImpl implements IAiKuaishouCreateCre
         }
         log.info("{}当天可创建组总数:{}", strategy.getAccountId(), unitNum);
         //查询当前账户创建自定义类型组数量
-        int unitCreateCnt = groupService.queryToDayBuiltCount(strategy.getAccountId(), 4);
+        Integer unitCreateCnt = queryUnitCnt(strategy.getAccountId() + date + "_old_custom_cnt");
+        if (!Check.isNull(unitCreateCnt)) {
+            unitNum = unitNum - unitCreateCnt;
+        }
+
+//        int unitCreateCnt = groupService.queryToDayBuiltCount(strategy.getAccountId(), 4);
         log.info("{}截止目前创建组总数:{}", strategy.getAccountId(), unitCreateCnt);
-        long remindCnt = unitNum - unitCreateCnt;
-        if (remindCnt >= 1) {
-            log.info("{}组创建不足,剩余需要创建个数{},开始上新素材创建", strategy.getAccountId(), remindCnt);
-            this.autoCreateCreative(strategy, 1, remindCnt);
+        if (unitNum >= 1) {
+            log.info("{}组创建不足,剩余需要创建个数{},开始上新素材创建", strategy.getAccountId(), unitNum);
+            this.autoCreateCreative(strategy, 1, unitNum);
         }
     }
 
@@ -868,7 +907,7 @@ public class AiKuaishouCreateCreativeServiceImpl implements IAiKuaishouCreateCre
                 String key = token.getAccountId() + "_ProgramCreative" + "_" + nowDate;
                 redisUtil.set(key, 1, 100000L);
                 updateService.updateUnitStatus(token.getAccessToken(), token.getAccountId(), unitId, 3, "e9ca23d68d884d4ebb19d07889727dae");
-                deleteBy(token.getAccountId(),unitId);
+                deleteBy(token.getAccountId(), unitId);
             }
         }
     }
@@ -1684,16 +1723,16 @@ public class AiKuaishouCreateCreativeServiceImpl implements IAiKuaishouCreateCre
 
         if (creativeCode == -1 && "/rest/openapi/v2/creative/create调用已超日限,次日可恢复调用".equals(creativeMessage)) {
             updateService.updateUnitStatus(token.getAccessToken(), token.getAccountId(), unitId, 3, "e9ca23d68d884d4ebb19d07889727dae");
-            deleteBy(token.getAccountId(),unitId);
+            deleteBy(token.getAccountId(), unitId);
         } else if (creativeCode == -1 && creativeMessage.contains("超过可关联的有效创意的上限")) {
             updateService.updateUnitStatus(token.getAccessToken(), token.getAccountId(), unitId, 3, "e9ca23d68d884d4ebb19d07889727dae");
-            deleteBy(token.getAccountId(),unitId);
+            deleteBy(token.getAccountId(), unitId);
         } else if (creativeCode == -1 && creativeMessage.contains("视频不存在")) {
             updateService.updateUnitStatus(token.getAccessToken(), token.getAccountId(), unitId, 3, "e9ca23d68d884d4ebb19d07889727dae");
-            deleteBy(token.getAccountId(),unitId);
-        }else if (creativeCode == -1 && creativeMessage.contains("视频非可用状态ID")) {
+            deleteBy(token.getAccountId(), unitId);
+        } else if (creativeCode == -1 && creativeMessage.contains("视频非可用状态ID")) {
             updateService.updateUnitStatus(token.getAccessToken(), token.getAccountId(), unitId, 3, "e9ca23d68d884d4ebb19d07889727dae");
-            deleteBy(token.getAccountId(),unitId);
+            deleteBy(token.getAccountId(), unitId);
         }
 
         AiKuaishouCreativeLevelOperationRecord record = new AiKuaishouCreativeLevelOperationRecord();