Переглянути джерело

项目自动化调试,添加定时任务

zhaoxian 3 роки тому
батько
коміт
6fc853c501

+ 57 - 5
module-kuaishou/src/main/java/cn/com/ctop/kuaishou/modules/ai/controller/AiKuaishouProjectCreateCreativeController.java

@@ -5,14 +5,15 @@ import cn.com.ctop.common.module.utils.Check;
 import cn.com.ctop.kuaishou.modules.ai.entity.KuaishouProjectStrategy;
 import cn.com.ctop.kuaishou.modules.ai.service.IAiKuaishouProjectCreateCreativeService;
 import cn.com.ctop.kuaishou.modules.ai.service.IKuaishouProjectStrategyService;
+import com.alibaba.fastjson.JSONObject;
 import io.swagger.annotations.Api;
 import lombok.extern.slf4j.Slf4j;
-import org.jeecg.common.api.vo.Result;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.web.bind.annotation.GetMapping;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RestController;
 
+import java.text.ParseException;
 import java.util.List;
 import java.util.concurrent.ExecutorService;
 import java.util.concurrent.Executors;
@@ -31,6 +32,7 @@ public class AiKuaishouProjectCreateCreativeController {
 
 
     static ExecutorService executorService = Executors.newFixedThreadPool(5);
+    static ExecutorService kuaishouCustomCreativeSupplementExecutorService = Executors.newFixedThreadPool(3);
 
     /**
      * 项目自动创建
@@ -39,8 +41,10 @@ public class AiKuaishouProjectCreateCreativeController {
      * @return
      */
     @GetMapping(value = "/projectAutomaticCreates")
-    public Result<Object> projectAutomaticCreates(String id, Integer operationType) {
+    public JSONObject projectAutomaticCreates(String id, Integer operationType) {
+        JSONObject returnJson = new JSONObject();
         try {
+
             if (Check.isNull(id) || Check.isNull(operationType)) {
                 throw new Exception("参数为空");
             }
@@ -51,7 +55,7 @@ public class AiKuaishouProjectCreateCreativeController {
             Long projectId = strategy.getProjectId();
             List<Long> list = userAllocationService.queryAutomaticAccounts(projectId);
             if (Check.isNull(list)) {
-                return Result.ok("项目:" + projectId + "中未配置自动投放账户!");
+                throw new Exception("项目:" + projectId + "中未配置自动投放账户!");
             }
             list.forEach(accountId -> executorService.submit(() -> {
                 Boolean unitOverrun = projectCreateCreativeService.getUnitOverrun(projectId.toString().concat(accountId.toString()));
@@ -61,10 +65,58 @@ public class AiKuaishouProjectCreateCreativeController {
                     projectCreateCreativeService.projectAutomaticCreates(strategy, operationType);
                 }
             }));
-            return Result.ok("异步创建中...");
+            returnJson.put("code", 0);
+            returnJson.put("message", "异步创建中");
         } catch (Exception e) {
             log.error("项目自动创建异常", e.fillInStackTrace());
-            return Result.error(e.getMessage());
+            returnJson.put("code", -1);
+            returnJson.put("message", e.getMessage());
+        }
+        return returnJson;
+    }
+
+    /**
+     * 自定义创意补充
+     *
+     * @param id
+     * @param hour
+     * @return
+     */
+    @GetMapping(value = "/kuaishouCustomCreativeSupplement")
+    public JSONObject kuaishouCustomCreativeSupplement(String id, Integer hour) {
+        JSONObject returnJson = new JSONObject();
+        try {
+            if (Check.isNull(id)) {
+                throw new Exception("主键id不能为空");
+            }
+            KuaishouProjectStrategy strategy = kuaishouProjectStrategyService.getById(id);
+            if (Check.isNull(strategy)) {
+                throw new Exception("根据id获取详细信息为空");
+            }
+            Long projectId = strategy.getProjectId();
+            List<Long> list = userAllocationService.queryAutomaticAccounts(projectId);
+            if (Check.isNull(list)) {
+                throw new Exception("项目:" + projectId + "中未配置自动投放账户!");
+            }
+            list.forEach(accountId -> executorService.submit(() -> {
+                Boolean unitOverrun = projectCreateCreativeService.getUnitOverrun(projectId.toString().concat(accountId.toString()));
+                if (!unitOverrun) {
+                    strategy.setAccountId(accountId);
+                    try {
+                        projectCreateCreativeService.customCreativeSupplement(strategy, hour);
+                    } catch (ParseException e) {
+                        e.printStackTrace();
+                    }
+                }
+            }));
+            returnJson.put("code", 0);
+            returnJson.put("message", "异步创建中");
+        } catch (Exception e) {
+            returnJson.put("code", -1);
+            returnJson.put("message", e.getMessage());
         }
+        return returnJson;
+
     }
+
 }

+ 4 - 0
module-kuaishou/src/main/java/cn/com/ctop/kuaishou/modules/ai/entity/KuaishouProjectStrategy.java

@@ -78,6 +78,10 @@ public class KuaishouProjectStrategy {
      */
     private Integer groupStatus;
     /**
+     * 是否开启素材挖掘
+     */
+    private Integer assetMining;
+    /**
      * 计划单日预算
      */
     @Excel(name = "计划单日预算", width = 15)

+ 5 - 0
module-kuaishou/src/main/java/cn/com/ctop/kuaishou/modules/ai/service/IAiKuaishouProjectCreateCreativeService.java

@@ -1,9 +1,14 @@
 package cn.com.ctop.kuaishou.modules.ai.service;
 
+import cn.com.ctop.kuaishou.modules.ai.entity.AiKuaishouAdvertiserStrategy;
 import cn.com.ctop.kuaishou.modules.ai.entity.KuaishouProjectStrategy;
 
+import java.text.ParseException;
+
 public interface IAiKuaishouProjectCreateCreativeService {
 
+    void customCreativeSupplement(KuaishouProjectStrategy strategy, Integer hour) throws ParseException;
+
     void projectAutomaticCreates(KuaishouProjectStrategy strategy,Integer operationType);
 
     // 组创建是否超限

+ 62 - 21
module-kuaishou/src/main/java/cn/com/ctop/kuaishou/modules/ai/service/impl/AiKuaishouProjectCreateCreativeServiceImpl.java

@@ -39,6 +39,7 @@ import cn.com.ctop.kuaishou.modules.batch.entity.KuaiShouVideoGet;
 import cn.com.ctop.kuaishou.modules.batch.service.IKuaiShouAppListService;
 import cn.com.ctop.kuaishou.modules.batch.service.IKuaiShouCampaignService;
 import cn.com.ctop.kuaishou.modules.batch.service.IKuaiShouCreativeService;
+import cn.com.ctop.kuaishou.modules.batch.service.IKuaiShouGroupService;
 import cn.com.ctop.kuaishou.modules.batch.service.IKuaiShouGroupTemplateService;
 import cn.com.ctop.kuaishou.modules.batch.service.IKuaiShouImageGetService;
 import cn.com.ctop.kuaishou.modules.batch.service.IKuaiShouMaterialUploadService;
@@ -59,6 +60,7 @@ import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
 import java.math.BigDecimal;
+import java.text.ParseException;
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.Date;
@@ -117,9 +119,45 @@ public class AiKuaishouProjectCreateCreativeServiceImpl implements IAiKuaishouPr
     private IKuaiShouAppListService appListService;
     @Autowired
     private IKuaiShouCampaignService campaignService;
-
+    @Autowired
+    private IKuaiShouGroupService groupService;
     static ExecutorService executorService = Executors.newFixedThreadPool(2);
 
+    @Override
+    public void customCreativeSupplement(KuaishouProjectStrategy strategy, Integer hour) throws ParseException {
+        Integer customUnitCnt = strategy.getCustomUnitCnt();
+        if (Check.isNull(customUnitCnt)) {
+            Integer imageCnt = strategy.getImageCnt();
+            if (imageCnt <= 1) {
+                customUnitCnt = 600;
+            } else {
+                customUnitCnt = 2000 / imageCnt;
+            }
+        }
+
+        int unitNum = customUnitCnt * hour / 20;
+        log.info("{}当前时间内需要创建组总数:{}", strategy.getAccountId(), unitNum);
+        //查询当前账户创建自定义类型组数量
+        int unitCreateCnt = groupService.queryToDayBuiltCount(strategy.getAccountId());
+        log.info("{}截止目前创建组总数:{}", strategy.getAccountId(), unitCreateCnt);
+        int remindCnt = unitNum - unitCreateCnt;
+        if (remindCnt >= 1) {
+            log.info("{}组创建不足,剩余需要创建个数{},使用历史遗漏素材创建", strategy.getAccountId(), remindCnt);
+            //优先创建历史遗漏素材
+            remindCnt = this.autoCreateCreative(strategy, 3, remindCnt, 2);
+        }
+        if (remindCnt >= 1) {
+            log.info("{}组创建不足,剩余需要创建个数{},使用高质量素材创建", strategy.getAccountId(), remindCnt);
+            //高质量素材
+            remindCnt = this.autoCreateCreative(strategy, 2, remindCnt, 2);
+        }
+        if (remindCnt >= 1) {
+            //历史打捞素材
+            log.info("{}组创建不足,剩余需要创建个数{},使用历史打捞素材创建", strategy.getAccountId(), remindCnt);
+            this.autoCreateCreative(strategy, 4, remindCnt, 2);
+        }
+    }
+
     /**
      * 项目自动创建
      *
@@ -157,8 +195,8 @@ public class AiKuaishouProjectCreateCreativeServiceImpl implements IAiKuaishouPr
                 } else if (strategy.getUnitType() == 7) {
                     this.autoCreateProgramCreative(strategy, createType, programUnitCnt, operationType);
                 } else {
-                    this.autoCreateProgramCreative(strategy, createType, programUnitCnt, operationType);
                     this.autoCreateCreative(strategy, createType, customUnitCnt, operationType);
+                    this.autoCreateProgramCreative(strategy, createType, programUnitCnt, operationType);
                 }
             }
         } catch (Exception e) {
@@ -173,7 +211,7 @@ public class AiKuaishouProjectCreateCreativeServiceImpl implements IAiKuaishouPr
      * @param createType 创意创建类型 1:素材上新 2:历史高质量 3:历史遗漏素材 4:历史打捞
      * @param videoCnt   视频数量
      */
-    private void autoCreateCreative(KuaishouProjectStrategy strategy, Integer createType, Integer videoCnt, Integer operationType) {
+    private Integer autoCreateCreative(KuaishouProjectStrategy strategy, Integer createType, Integer videoCnt, Integer operationType) {
         try {
             String strategyUuid = UUID.randomUUID().toString();
             strategy.setStrategyUuid(strategyUuid);
@@ -183,7 +221,7 @@ public class AiKuaishouProjectCreateCreativeServiceImpl implements IAiKuaishouPr
             if (null == token) {
                 log.info("token获取失败=>accountId:{}", accountId);
                 insertAccountOperationRecord(strategy, "获取token失败", operationType, 0);
-                return;
+                return videoCnt;
             }
             //1:获取视频信息
             kuaishouInterfaceService.getVideoList(token, DateUtils.date2Str(), DateUtils.date2Str());
@@ -202,7 +240,7 @@ public class AiKuaishouProjectCreateCreativeServiceImpl implements IAiKuaishouPr
                 if (null == newVideos || newVideos.isEmpty()) {
                     log.info("账户:{},{} 到 {} 未获取到相应素材({})", accountId, startTime, endTime, replaceString);
                     insertAccountOperationRecord(strategy, "账户在" + startTime + "~" + endTime + "时间段内未获取到相应素材(" + replaceString + ")", operationType, 0);
-                    return;
+                    return videoCnt;
                 }
                 Long appId = null;
                 //appId,当计划类型为2时必填
@@ -210,12 +248,12 @@ public class AiKuaishouProjectCreateCreativeServiceImpl implements IAiKuaishouPr
                     Long appTemId = Check.isNull(strategy.getAppId()) ? JSONArray.parseArray(strategy.getAppIdArray()).getLong(0) : strategy.getAppId();
                     if (Check.isNull(appTemId)) {
                         insertAccountOperationRecord(strategy, "未获取到项目应用模板", operationType, 0);
-                        return;
+                        return videoCnt;
                     }
                     JSONObject app = appPackageService.queryAppId(appTemId, token.getAccountId());
                     if (Check.isNull(app) || Check.isNull(app.getLong("app_id"))) {
                         insertAccountOperationRecord(strategy, "未获取到appId", operationType, 0);
-                        return;
+                        return videoCnt;
                     }
                     appId = app.getLong("app_id");
                     strategy.setClickTrackUrl(app.getString("track_url"));
@@ -226,7 +264,7 @@ public class AiKuaishouProjectCreateCreativeServiceImpl implements IAiKuaishouPr
                 Long newCampaignId = getCampaignId(strategy, token, replaceString, "TODAY", createType, appId, strategyUuid);
                 if (null == newCampaignId) {
                     insertAccountOperationRecord(strategy, "创建广告计划失败", operationType, 0);
-                    return;
+                    return videoCnt;
                 }
                 strategy.setCampaignId(newCampaignId);
                 //3、在“上新”计划中新增广告组,一个素材搭配n张封面,组成一个广告组
@@ -234,12 +272,12 @@ public class AiKuaishouProjectCreateCreativeServiceImpl implements IAiKuaishouPr
                     Boolean unitOverrun = getUnitOverrun(accountId.toString());
                     if (unitOverrun) {
                         log.error("组创建超限,accountId:{}", accountId);
-                        return;
+                        return 0;
                     }
                     //快手账户创意创建超限记录信息,查到数据则说明已经超限
                     KuaishouAccountCreativeOverrunInfo overrunInfo = overrunInfoService.getByParams(accountId, DateUtils.formatDate(new Date()));
                     if (null != overrunInfo) {
-                        return;
+                        return 0;
                     }
                     // 每个组,搭配5个创意
                     List<MaterialCutFrame> cutFrameList = materialCutFrameService.getListByVideoSignature(videoItem.getSignature());
@@ -276,9 +314,7 @@ public class AiKuaishouProjectCreateCreativeServiceImpl implements IAiKuaishouPr
                                 videoCnt--;
                                 JSONObject creativeParams = creativeParams(token, unitId, videoItem, strategy);
                                 createCreativeByImage(cutFrameList, token, creativeParams, newCampaignId, unitId, videoItem, strategy.getImageCnt(), strategy, strategyUuid, null);
-                                if (videoCnt <= 0) {
-                                    return;
-                                }
+
                             }
                             if (unitCode != 0) {
                                 unitLevelOperationRecordService.saveOrUpdate(unitLevelOperationRecord);
@@ -288,6 +324,9 @@ public class AiKuaishouProjectCreateCreativeServiceImpl implements IAiKuaishouPr
                             unitLevelOperationRecord.setMessage(msg);
                             unitLevelOperationRecordService.saveOrUpdate(unitLevelOperationRecord);
                         }
+                        if (videoCnt <= 0) {
+                            return videoCnt;
+                        }
                     }
                 }
             }
@@ -296,6 +335,7 @@ public class AiKuaishouProjectCreateCreativeServiceImpl implements IAiKuaishouPr
             insertAccountOperationRecord(strategy, "创建自定义创意异常", operationType, 0);
         }
         insertAccountOperationRecord(strategy, "success", operationType, 1);
+        return videoCnt;
     }
 
     /**
@@ -369,7 +409,7 @@ public class AiKuaishouProjectCreateCreativeServiceImpl implements IAiKuaishouPr
                 }
                 strategy.setCampaignId(newCampaignId);
                 //创意
-                List<List<KuaiShouVideoGet>> splitVideos = Lists.partition(allVideos, 5);
+                List<List<KuaiShouVideoGet>> splitVideos = Lists.partition(allVideos, strategy.getImageCnt());
                 for (int j = 1; j < splitVideos.size() + 1; j++) {
                     Boolean unitOverrun = getUnitOverrun(accountId.toString());
                     if (unitOverrun) {
@@ -873,13 +913,13 @@ public class AiKuaishouProjectCreateCreativeServiceImpl implements IAiKuaishouPr
                     unitParams.put("smart_cover", strategy.getSmartCover());
                 }
                 //程序化创意2.0素材挖掘
-                //            if (!Check.isNull(strategy.getAssetMining())) {
-                //                if (strategy.getAssetMining() == 1) {
-                //                    unitParams.put("asset_mining", true);
-                //                } else if (strategy.getAssetMining() == 0) {
-                //                    unitParams.put("asset_mining", false);
-                //                }
-                //            }
+                if (!Check.isNull(strategy.getAssetMining())) {
+                    if (strategy.getAssetMining() == 1) {
+                        unitParams.put("asset_mining", true);
+                    } else if (strategy.getAssetMining() == 0) {
+                        unitParams.put("asset_mining", false);
+                    }
+                }
             }
             if (!Check.isNull(strategy.getTemplateIdArray())) {
                 //获取项目定向模板数据
@@ -1379,6 +1419,7 @@ public class AiKuaishouProjectCreateCreativeServiceImpl implements IAiKuaishouPr
             wildcard = wildcard + "-" + count;
         }
         wildcard = wildcard.replace("--", "-");
+        wildcard = wildcard.replace("--", "-");
         if (wildcard.startsWith("-")) {
             wildcard = wildcard.substring(1, wildcard.length());
         }