Bläddra i källkod

定向包~接口开发

zhaoxian 4 år sedan
förälder
incheckning
21f5ead514

+ 6 - 15
module-kuaishou/src/main/java/cn/com/ctop/kuaishou/modules/ai/controller/KuaishouDirectionalPackageController.java

@@ -1,7 +1,6 @@
 package cn.com.ctop.kuaishou.modules.ai.controller;
 
 import cn.com.ctop.common.module.annotation.AutoLog;
-import cn.com.ctop.common.module.entity.CtopOauthToken;
 import cn.com.ctop.common.module.service.ICtopOauthTokenService;
 import cn.com.ctop.common.module.utils.Check;
 import cn.com.ctop.common.module.utils.QueryGenerator;
@@ -188,7 +187,7 @@ public class KuaishouDirectionalPackageController {
                 return Result.error("请选择定向包");
             }
             PageHelper.startPage(pageNo, pageSize);
-            return kuaishouDirectionalPackageService.getAccountByProjectId(projectId,id);
+            return kuaishouDirectionalPackageService.getAccountByProjectId(projectId, id);
         } catch (Exception e) {
             log.error("查询异常,", e.getMessage());
             e.printStackTrace();
@@ -230,21 +229,13 @@ public class KuaishouDirectionalPackageController {
     public Result<Object> updateDirectionalPackage(@RequestBody JSONObject requestJson) {
         Result<JSONObject> result = new Result<>();
         try {
-            if (Check.isNull(requestJson.getString("templateName"))) {
-                return Result.error("定向模板名称为必传项");
-            }
-            if (Check.isNull(requestJson.getLong("templateId"))) {
-                return Result.error("定向模板id为必传项");
-            }
-            Long accountId = requestJson.getLong("accountId");
-            if (Check.isNull(accountId)) {
-                return Result.error("请选择账户id");
+            if (Check.isNull(requestJson.getString("id"))) {
+                return Result.error("请选择定向包");
             }
-            CtopOauthToken oauthToken = oauthTokenService.getTokenByAccountId(accountId);
-            if (Check.isNull(oauthToken)) {
-                return Result.error("未获取到账户信息");
+            if (Check.isNull(requestJson.getString("projectId"))) {
+                return Result.error("请选择项目");
             }
-            return kuaishouDirectionalPackageService.updateDirectionalPackage(oauthToken.getAccessToken(), requestJson);
+            return kuaishouDirectionalPackageService.updateDirectionalPackage(requestJson);
         } catch (Exception e) {
             result.setSuccess(false);
             result.setMessage(e.getMessage());

+ 2 - 0
module-kuaishou/src/main/java/cn/com/ctop/kuaishou/modules/ai/mapper/KuaishouDirectionalTemplateRelMapper.java

@@ -21,4 +21,6 @@ public interface KuaishouDirectionalTemplateRelMapper extends BaseMapper<Kuaisho
     List<JSONObject> queryPushResults(@Param("id")String id, @Param("accountIds")JSONArray accountIds);
 
     List<JSONObject> getAccountByProjectId(@Param("projectId") Long projectId,@Param("id") String id);
+
+    List<JSONObject> queryRelId(@Param("projectId") Long projectId,@Param("id") String id,@Param("type") String type);
 }

+ 7 - 0
module-kuaishou/src/main/java/cn/com/ctop/kuaishou/modules/ai/mapper/xml/KuaishouDirectionalTemplateRelMapper.xml

@@ -76,4 +76,11 @@
         where t1.account_status = 0
           AND t1.project_id = #{projectId}
     </select>
+
+    <select id="queryRelId" resultType="com.alibaba.fastjson.JSONObject">
+        SELECT rel_id,template_id FROM ctop_kuaishou_directional_template_rel
+        WHERE  project_id = #{projectId}
+          AND rel_type = #{type}
+          AND dir_id = #{id}
+    </select>
 </mapper>

+ 1 - 1
module-kuaishou/src/main/java/cn/com/ctop/kuaishou/modules/ai/service/IKuaishouDirectionalPackageService.java

@@ -16,7 +16,7 @@ public interface IKuaishouDirectionalPackageService extends IService<KuaishouDir
 
     Result<Object> createDirectionalPackage(JSONObject requestJson);
 
-    Result<Object> updateDirectionalPackage(String accessToken, JSONObject requestJson);
+    Result<Object> updateDirectionalPackage(JSONObject requestJson);
 
     Result<Object> queryList(KuaishouDirectionalPackage directionalPackage, Integer pageNo, Integer pageSize);
 

+ 79 - 26
module-kuaishou/src/main/java/cn/com/ctop/kuaishou/modules/ai/service/impl/KuaishouDirectionalPackageServiceImpl.java

@@ -13,7 +13,6 @@ import cn.com.ctop.kuaishou.modules.ai.mapper.KuaishouDirectionalTemplateRelMapp
 import cn.com.ctop.kuaishou.modules.ai.service.IKuaishouDirectionalPackageService;
 import cn.com.ctop.kuaishou.modules.ai.service.IKuaishouDirectionalTemplateRelService;
 import cn.com.ctop.kuaishou.modules.batch.service.IKuaishouTemplateService;
-import com.alibaba.fastjson.JSON;
 import com.alibaba.fastjson.JSONArray;
 import com.alibaba.fastjson.JSONObject;
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
@@ -28,6 +27,8 @@ import org.springframework.stereotype.Service;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
 
 /**
  * 自动投放-定向包详情
@@ -51,6 +52,8 @@ public class KuaishouDirectionalPackageServiceImpl extends ServiceImpl<KuaishouD
     @Autowired
     private ICtopOauthTokenService oauthTokenService;
 
+    static ExecutorService executorService = null;
+
     @Override
     public Result<Object> createDirectionalPackage(JSONObject requestJson) {
         KuaishouDirectionalPackage entity = packageMapper.queryEntity(requestJson);
@@ -58,7 +61,9 @@ public class KuaishouDirectionalPackageServiceImpl extends ServiceImpl<KuaishouD
             return Result.error("该账户下定向包名已存在");
         }
         KuaishouDirectionalPackage kuaishouDirectionalPackage = JSONObject.toJavaObject(requestJson, KuaishouDirectionalPackage.class);
-        kuaishouDirectionalPackage.setKeyword(JSONObject.toJSONString(requestJson.getJSONArray("keyword")));
+        if (!Check.isNull(requestJson.getJSONArray("keyword"))) {
+            kuaishouDirectionalPackage.setKeyword(JSONObject.toJSONString(requestJson.getJSONArray("keyword")));
+        }
         kuaishouDirectionalPackage.setDataStatus(1);
         boolean save = this.save(kuaishouDirectionalPackage);
         if (save) {
@@ -69,14 +74,76 @@ public class KuaishouDirectionalPackageServiceImpl extends ServiceImpl<KuaishouD
 
 
     @Override
-    public Result<Object> updateDirectionalPackage(String accessToken, JSONObject requestJson) {
-        JSONObject returnJson = new JSONObject();
-        String message = "";
-        int flag = -1;
+    public Result<Object> updateDirectionalPackage(JSONObject requestJson) {
+        KuaishouDirectionalPackage entity = packageMapper.selectById(requestJson.getString("id"));
+        if (Check.isNull(entity)) {
+            return Result.error("未获取到定向包");
+        }
+        KuaishouDirectionalPackage kuaishouDirectionalPackage = JSONObject.toJavaObject(requestJson, KuaishouDirectionalPackage.class);
+        if (!Check.isNull(requestJson.getJSONArray("keyword"))) {
+            kuaishouDirectionalPackage.setKeyword(JSONObject.toJSONString(requestJson.getJSONArray("keyword")));
+        }
+        this.saveOrUpdate(kuaishouDirectionalPackage);
+        //获取本项目绑定该定向的账户
+        List<JSONObject> accounts = relMapper.queryRelId(requestJson.getLong("projectId"), requestJson.getString("id"), "account");
+        if (!Check.isNull(accounts)) {
+            Thread thread = new Thread() {
+                @Override
+                public void run() {
+                    executorService = Executors.newFixedThreadPool(3);
+                    accounts.forEach(accountEntity -> {
+                        executorService.submit(new Runnable() {
+                            @Override
+                            public void run() {
+                                try {
+                                    updateAccountsDirectionalPackage(requestJson, accountEntity);
+                                } catch (Exception e) {
+                                    e.printStackTrace();
+                                }
+                            }
+                        });
+                    });
+                    log.info("快手物料数据同步完成");
+                }
+            };
+            thread.start();
+        }
+        //获取本项目绑定该定向的广告组
+        List<JSONObject> unitIds = relMapper.queryRelId(requestJson.getLong("projectId"), requestJson.getString("id"), "unit");
+        if (!Check.isNull(unitIds)) {
+            Thread thread = new Thread() {
+                @Override
+                public void run() {
+                    executorService = Executors.newFixedThreadPool(3);
+                    unitIds.forEach(unitId -> {
+                        executorService.submit(new Runnable() {
+                            @Override
+                            public void run() {
+                                try {
+                                    //TODO
+                                    System.out.println("修改广告组定向");
+    //                                    updateAccountsDirectionalPackage(requestJson, accountId);
+                                } catch (Exception e) {
+                                    e.printStackTrace();
+                                }
+                            }
+                        });
+                    });
+                    log.info("快手物料数据同步完成");
+                }
+            };
+            thread.start();
+        }
+        return Result.ok("该项目下定向包已做同步修改");
+    }
+
+    private void updateAccountsDirectionalPackage(JSONObject requestJson, JSONObject accounEntity) {
+        Long accountId = accounEntity.getLong("rel_id");
+        CtopOauthToken oauthToken = oauthTokenService.getTokenByAccountId(accountId);
         try {
             JSONObject param = new JSONObject();
-            param.put("template_id", requestJson.getLong("templateId"));
-            param.put("advertiser_id", requestJson.getLong("accountId"));
+            param.put("template_id", accounEntity.getLong("template_id"));
+            param.put("advertiser_id", accountId);
             if (!Check.isNull(requestJson.getString("templateName"))) {
                 param.put("template_name", requestJson.getString("templateName"));
             }
@@ -152,8 +219,6 @@ public class KuaishouDirectionalPackageServiceImpl extends ServiceImpl<KuaishouD
             if (!Check.isNull(requestJson.getJSONArray("excludePopulation"))) {
                 targetJson.put("exclude_population", requestJson.getJSONArray("excludePopulation"));
             }
-
-
             if (!Check.isNull(requestJson.getInteger("isOpen"))) {
                 JSONObject intelliExtendJson = new JSONObject();
                 intelliExtendJson.put("is_open", requestJson.getInteger("isOpen"));
@@ -205,35 +270,23 @@ public class KuaishouDirectionalPackageServiceImpl extends ServiceImpl<KuaishouD
                 param.put("behavior_interest", behaviorInterest);
             }
             Map<String, String> headers = new HashMap<String, String>();
-            headers.put("Access-Token", accessToken);
+            headers.put("Access-Token", oauthToken.getAccessToken());
             headers.put("Content-Type", " application/json");
             String url = PropertiesUtils.getConfig("kuaishou_api_url") + KuaishouInterfaceConstant.TEMPLATE_UPDATE;
-            log.info("入参:" + param);
             String result = HttpUtils.kuaiShouhttpPostRequest(url, param.toJSONString(), headers);
             JSONObject resultJson = JSONObject.parseObject(result);
             if (!Check.isNull(resultJson)) {
                 Integer code = resultJson.getInteger("code");
-                message = resultJson.getString("message");
                 if (code == 0) {
-                    kuaishouTemplateService.getTemplateByAccountId(requestJson.getLong("accountId"), accessToken, 1);
-                    flag = code;
-                    JSONObject data = resultJson.getJSONObject("data");
+                    kuaishouTemplateService.getTemplateByAccountId(accountId, oauthToken.getAccessToken(), 1);
                 }
             }
-            KuaishouDirectionalPackage kuaishouDirectionalPackage = JSONObject.toJavaObject(requestJson, KuaishouDirectionalPackage.class);
-            this.saveOrUpdate(kuaishouDirectionalPackage);
         } catch (Exception e) {
-            e.printStackTrace();
-            returnJson.put("code", -1);
-            returnJson.put("message", e.getMessage());
-        }
-        if (flag == 0) {
-            return Result.ok(message);
-        } else {
-            return Result.error(message);
+            log.error("修改账户定向包异常,账户Id:{}", accountId, e);
         }
     }
 
+
     @Override
     public Result<Object> queryList(KuaishouDirectionalPackage directionalPackage, Integer pageNo, Integer pageSize) {
         Long total = packageMapper.queryListTotal(directionalPackage);