Bladeren bron

优化定向包查询速度

yumeng 4 jaren geleden
bovenliggende
commit
08a82706ce

+ 3 - 11
module-kuaishou/src/main/java/cn/com/ctop/kuaishou/modules/batch/controller/KuaishouTemplateController.java

@@ -21,15 +21,7 @@ import org.jeecgframework.poi.excel.entity.ExportParams;
 import org.jeecgframework.poi.excel.entity.ImportParams;
 import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
 import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.web.bind.annotation.DeleteMapping;
-import org.springframework.web.bind.annotation.GetMapping;
-import org.springframework.web.bind.annotation.PostMapping;
-import org.springframework.web.bind.annotation.PutMapping;
-import org.springframework.web.bind.annotation.RequestBody;
-import org.springframework.web.bind.annotation.RequestMapping;
-import org.springframework.web.bind.annotation.RequestMethod;
-import org.springframework.web.bind.annotation.RequestParam;
-import org.springframework.web.bind.annotation.RestController;
+import org.springframework.web.bind.annotation.*;
 import org.springframework.web.multipart.MultipartFile;
 import org.springframework.web.multipart.MultipartHttpServletRequest;
 import org.springframework.web.servlet.ModelAndView;
@@ -65,9 +57,9 @@ public class KuaishouTemplateController {
     @AutoLog(value = "快手-定向模板-分页列表查询")
     @ApiOperation(value = "快手-定向模板-分页列表查询", notes = "快手-定向模板-分页列表查询")
     @GetMapping(value = "/list")
-    public Result<List<JSONObject>> list(Long accountId, Integer platformOs) {
+    public Result<List<JSONObject>> list(Long accountId, Integer platformOs, Boolean autoTarget) {
         Result<List<JSONObject>> result = new Result<>();
-        List<JSONObject> list = kuaishouTemplateService.getJsonArrByAccountId(accountId, platformOs);
+        List<JSONObject> list = kuaishouTemplateService.getJsonArrByAccountId(accountId, platformOs, autoTarget);
         result.setSuccess(true);
         result.setResult(list);
         return result;

+ 1 - 0
module-kuaishou/src/main/java/cn/com/ctop/kuaishou/modules/batch/entity/KuaishouTemplate.java

@@ -53,6 +53,7 @@ public class KuaishouTemplate {
     @Excel(name = "绑定组个数", width = 15)
     @ApiModelProperty(value = "绑定组个数")
     private Integer unitCount;
+    private Boolean autoTarget;
     /**
      * 定向模板创建时间
      */

+ 3 - 1
module-kuaishou/src/main/java/cn/com/ctop/kuaishou/modules/batch/mapper/KuaishouTemplateMapper.java

@@ -15,5 +15,7 @@ import java.util.List;
  * @cersion: V1.0
  */
 public interface KuaishouTemplateMapper extends BaseMapper<KuaishouTemplate> {
-    List<JSONObject> getJsonArrByAccountId(@Param("accountId") Long accountId,@Param("platformOs") Integer platformOs);
+    List<JSONObject> getJsonArrByAccountId(@Param("accountId") Long accountId, @Param("platformOs") Integer platformOs, @Param("autoTarget") Boolean autoTarget);
+
+    void replaceBatch(@Param("templateList") List<KuaishouTemplate> templateList);
 }

+ 4 - 0
module-kuaishou/src/main/java/cn/com/ctop/kuaishou/modules/batch/mapper/KuaishouTemplateTargetMapper.java

@@ -2,6 +2,9 @@ package cn.com.ctop.kuaishou.modules.batch.mapper;
 
 import cn.com.ctop.kuaishou.modules.batch.entity.KuaishouTemplateTarget;
 import com.baomidou.mybatisplus.core.mapper.BaseMapper;
+import org.apache.ibatis.annotations.Param;
+
+import java.util.List;
 
 /**
  * 快手-定向模板-详情
@@ -12,4 +15,5 @@ import com.baomidou.mybatisplus.core.mapper.BaseMapper;
  */
 public interface KuaishouTemplateTargetMapper extends BaseMapper<KuaishouTemplateTarget> {
 
+    void templateTargetList(@Param("templateTargetList") List<KuaishouTemplateTarget> templateTargetList);
 }

+ 38 - 9
module-kuaishou/src/main/java/cn/com/ctop/kuaishou/modules/batch/mapper/xml/KuaishouTemplateMapper.xml

@@ -1,22 +1,51 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
 <mapper namespace="cn.com.ctop.kuaishou.modules.batch.mapper.KuaishouTemplateMapper">
+    <insert id="replaceBatch">
+
+        replace into ctop_kuaishou_template(
+        id,
+        account_id,
+        template_id,
+        template_name,
+        unit_count,
+        auto_target,
+        create_time,
+        update_time)
+        values
+        <foreach collection="templateList" item="template" separator=",">
+            (
+            #{template.id},
+            #{template.accountId},
+            #{template.templateId},
+            #{template.templateName},
+            #{template.unitCount},
+            #{template.autoTarget},
+            #{template.createTime},
+            #{template.updateTime}
+            )
+        </foreach>
+
+    </insert>
 
     <select id="getJsonArrByAccountId" resultType="com.alibaba.fastjson.JSONObject">
         SELECT
-            t1.account_id AS 'accountId',
-            t1.template_id AS 'templateId',
-            t1.template_name AS 'templateName',
-            t1.unit_count AS 'unitCount',
-            t2.platform_os AS 'platformOs'
+        t1.account_id AS 'accountId',
+        t1.template_id AS 'templateId',
+        t1.template_name AS 'templateName',
+        t1.unit_count AS 'unitCount',
+        t2.platform_os AS 'platformOs'
         FROM
-            ctop_kuaishou_template t1
-                LEFT JOIN ctop_kuaishou_template_target t2 ON t1.template_id = t2.template_id
+        ctop_kuaishou_template t1
+        LEFT JOIN ctop_kuaishou_template_target t2 ON t1.template_id = t2.template_id
         WHERE
-            t1.account_id = #{accountId}
-        <if test="platformOs != null">
+        t1.account_id = #{accountId}
+        <if test="platformOs != null and platformOs!= '' ">
             AND t2.platform_os = #{platformOs}
         </if>
+        <if test="autoTarget != null and autoTarget != '' ">
+            AND t1.auto_target = #{autoTarget}
+        </if>
     </select>
 
 </mapper>

+ 70 - 0
module-kuaishou/src/main/java/cn/com/ctop/kuaishou/modules/batch/mapper/xml/KuaishouTemplateTargetMapper.xml

@@ -2,4 +2,74 @@
 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
 <mapper namespace="cn.com.ctop.kuaishou.modules.batch.mapper.KuaishouTemplateTargetMapper">
 
+    <insert id="templateTargetList">
+        replace into ctop_kuaishou_template_target(
+        id,
+        template_id,
+        region,
+        district_ids,
+        user_type,
+        ages_range,
+        min,
+        max,
+        gender,
+        platform_os,
+        android_osv,
+        ios_osv,
+        network,
+        filter_converted_level,
+        device_brand,
+        device_price,
+        business_interest_type,
+        business_interest,
+        fans_star,
+        interest_video,
+        app_interest,
+        app_ids,
+        population,
+        paid_audience,
+        exclude_population,
+        is_open,
+        no_age_break,
+        no_gender_break,
+        no_area_break,
+        create_time,
+        update_time)
+        values
+        <foreach collection="templateTargetList" item="templateTarget" separator=",">
+            (
+            #{templateTarget.id},
+            #{templateTarget.templateId},
+            #{templateTarget.region},
+            #{templateTarget.districtIds},
+            #{templateTarget.userType},
+            #{templateTarget.agesRange},
+            #{templateTarget.min},
+            #{templateTarget.max},
+            #{templateTarget.gender},
+            #{templateTarget.platformOs},
+            #{templateTarget.androidOsv},
+            #{templateTarget.iosOsv},
+            #{templateTarget.network},
+            #{templateTarget.filterConvertedLevel},
+            #{templateTarget.deviceBrand},
+            #{templateTarget.devicePrice},
+            #{templateTarget.businessInterestType},
+            #{templateTarget.businessInterest},
+            #{templateTarget.fansStar},
+            #{templateTarget.interestVideo},
+            #{templateTarget.appInterest},
+            #{templateTarget.appIds},
+            #{templateTarget.population},
+            #{templateTarget.paidAudience},
+            #{templateTarget.excludePopulation},
+            #{templateTarget.isOpen},
+            #{templateTarget.noAgeBreak},
+            #{templateTarget.noGenderBreak},
+            #{templateTarget.noAreaBreak},
+            #{templateTarget.createTime},
+            #{templateTarget.updateTime}
+            )
+        </foreach>
+    </insert>
 </mapper>

+ 1 - 1
module-kuaishou/src/main/java/cn/com/ctop/kuaishou/modules/batch/service/IKuaishouTemplateService.java

@@ -21,7 +21,7 @@ public interface IKuaishouTemplateService extends IService<KuaishouTemplate> {
 
     JSONObject updateTemplate(Long accountId, String accessToken, JSONObject requestJson);
 
-    List<JSONObject> getJsonArrByAccountId(Long accountId, Integer platformOs);
+    List<JSONObject> getJsonArrByAccountId(Long accountId, Integer platformOs, Boolean autoTarget);
 
     Result<Object> getbehaviorInterest(Long accountId);
 

+ 25 - 95
module-kuaishou/src/main/java/cn/com/ctop/kuaishou/modules/batch/service/impl/KuaishouTemplateServiceImpl.java

@@ -1,7 +1,5 @@
 package cn.com.ctop.kuaishou.modules.batch.service.impl;
 
-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.HttpUtils;
 import cn.com.ctop.common.module.utils.KuaishouInterfaceConstant;
@@ -9,16 +7,16 @@ import cn.com.ctop.common.module.utils.PropertiesUtils;
 import cn.com.ctop.kuaishou.modules.batch.entity.KuaishouTemplate;
 import cn.com.ctop.kuaishou.modules.batch.entity.KuaishouTemplateTarget;
 import cn.com.ctop.kuaishou.modules.batch.mapper.KuaishouTemplateMapper;
+import cn.com.ctop.kuaishou.modules.batch.mapper.KuaishouTemplateTargetMapper;
 import cn.com.ctop.kuaishou.modules.batch.service.IKuaishouTemplateService;
-import cn.com.ctop.kuaishou.modules.batch.service.IKuaishouTemplateTargetService;
 import com.alibaba.fastjson.JSONArray;
 import com.alibaba.fastjson.JSONObject;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 import lombok.extern.slf4j.Slf4j;
-import org.jeecg.common.api.vo.Result;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
+import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
@@ -33,16 +31,16 @@ import java.util.Map;
 @Slf4j
 @Service
 public class KuaishouTemplateServiceImpl extends ServiceImpl<KuaishouTemplateMapper, KuaishouTemplate> implements IKuaishouTemplateService {
+
     @Autowired
-    private IKuaishouTemplateTargetService templateTargetService;
+    private KuaishouTemplateTargetMapper templateTargetMapper;
+
     @Autowired
     private KuaishouTemplateMapper templateMapper;
-    @Autowired
-    private ICtopOauthTokenService tokenService;
 
     @Override
-    public List<JSONObject> getJsonArrByAccountId(Long accountId, Integer platformOs) {
-        return templateMapper.getJsonArrByAccountId(accountId, platformOs);
+    public List<JSONObject> getJsonArrByAccountId(Long accountId, Integer platformOs, Boolean autoTarget) {
+        return templateMapper.getJsonArrByAccountId(accountId, platformOs, autoTarget);
     }
 
     @Override
@@ -52,6 +50,7 @@ public class KuaishouTemplateServiceImpl extends ServiceImpl<KuaishouTemplateMap
         param.put("advertiser_id", accountId);
         param.put("page", page);
         param.put("page_size", 500);
+
         Map<String, String> headers = new HashMap<String, String>();
         headers.put("Access-Token", accessToken);
         headers.put("Content-Type", " application/json");
@@ -59,9 +58,6 @@ public class KuaishouTemplateServiceImpl extends ServiceImpl<KuaishouTemplateMap
         JSONObject resultJson = JSONObject.parseObject(result);
         if (!Check.isNull(resultJson)) {
             Integer code = resultJson.getInteger("code");
-         /*   Map<String, Object> deleteMap = new HashMap<>();
-            deleteMap.put("account_id", accountId);
-            this.removeByMap(deleteMap);*/
             if (code == 0) {
                 JSONObject dataJson = resultJson.getJSONObject("data");
                 if (Check.isNull(dataJson)) {
@@ -71,6 +67,9 @@ public class KuaishouTemplateServiceImpl extends ServiceImpl<KuaishouTemplateMap
                 if (Check.isNull(details)) {
                     return;
                 }
+
+                List<KuaishouTemplate> templateList = new ArrayList<>();
+                List<KuaishouTemplateTarget> templateTargetList = new ArrayList<>();
                 for (int i = 0; i < details.size(); i++) {
                     JSONObject jsonObject = details.getJSONObject(i);
                     Long templateId = jsonObject.getLong("template_id");
@@ -80,9 +79,10 @@ public class KuaishouTemplateServiceImpl extends ServiceImpl<KuaishouTemplateMap
                     template.setTemplateId(templateId);
                     template.setTemplateName(jsonObject.getString("template_name"));
                     template.setUnitCount(jsonObject.getInteger("unit_count"));
+                    template.setAutoTarget(jsonObject.getBoolean("auto_target"));
                     template.setCreateTime(jsonObject.getDate("create_time"));
                     template.setUpdateTime(jsonObject.getDate("updateTime"));
-                    this.saveOrUpdate(template);
+                    templateList.add(template);
                     JSONObject targetJson = jsonObject.getJSONObject("target");
                     if (!Check.isNull(targetJson)) {
                         KuaishouTemplateTarget templateTarget = new KuaishouTemplateTarget();
@@ -171,10 +171,18 @@ public class KuaishouTemplateServiceImpl extends ServiceImpl<KuaishouTemplateMap
                                 templateTarget.setNoAreaBreak(intelliExtendJson.getInteger("no_area_break"));
                             }
                         }
-                        templateTargetService.saveOrUpdate(templateTarget);
+
+
+                        templateTarget.setCreateTime(jsonObject.getDate("create_time"));
+                        templateTarget.setUpdateTime(jsonObject.getDate("updateTime"));
+                        templateTargetList.add(templateTarget);
                     }
 
                 }
+                if (!Check.isNull(templateList)) {
+                    templateMapper.replaceBatch(templateList);
+                    templateTargetMapper.templateTargetList(templateTargetList);
+                }
 
                 getTemplateByAccountId(accountId, accessToken, page + 1);
             } else {
@@ -208,10 +216,12 @@ public class KuaishouTemplateServiceImpl extends ServiceImpl<KuaishouTemplateMap
             if (Check.isNull(requestJson.getString("templateName"))) {
                 throw new Exception("定向模板名称为必传项");
             }
-
             JSONObject param = new JSONObject();
             param.put("advertiser_id", accountId);
             param.put("template_name", requestJson.getString("templateName"));
+            if (!Check.isNull(requestJson.getBoolean("autoTarget"))) {
+                param.put("auto_target", requestJson.getBoolean("autoTarget"));
+            }
             JSONObject targetJson = new JSONObject();
             if (!Check.isNull(requestJson.getJSONArray("region"))) {
                 targetJson.put("region", requestJson.getJSONArray("region"));
@@ -471,7 +481,6 @@ public class KuaishouTemplateServiceImpl extends ServiceImpl<KuaishouTemplateMap
             headers.put("Access-Token", accessToken);
             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)) {
@@ -499,83 +508,4 @@ public class KuaishouTemplateServiceImpl extends ServiceImpl<KuaishouTemplateMap
         }
         return returnJson;
     }
-
-    /**
-     * 查询加速探索信息
-     *
-     * @param
-     * @throws
-     * @author ZHAOXA
-     */
-    @Override
-    public Result<Object> getbehaviorInterest(Long accountId) {
-        try {
-            CtopOauthToken token = tokenService.getTokenByAccountId(accountId);
-            String url = PropertiesUtils.getConfig("kuaishou_api_url") + KuaishouInterfaceConstant.BEHAVIOR_INTEREST;
-            Map<String, String> header = new HashMap<>();
-            header.put("Access-Token", token.getAccessToken());
-            header.put("Content-Type", "application/json");
-            JSONObject params = new JSONObject();
-            params.put("advertiser_id", accountId);
-            String result = HttpUtils.httpPostRequest(url, params, header);
-            JSONObject resultJson = JSONObject.parseObject(result);
-            if (!Check.isNull(resultJson)) {
-                Integer code = resultJson.getInteger("code");
-                if (code == 0) {
-                    return Result.ok(resultJson.getJSONObject("data"));
-                } else {
-                    log.info(" 获取行为与兴趣类目失败,accountId:{},返回信息:{}", accountId, resultJson);
-                    return Result.error("获取行为与兴趣类目失败," + resultJson.getString("message"));
-                }
-            }
-        } catch (Exception e) {
-            log.error("获取行为与兴趣类目失败", e);
-        }
-        return Result.error("获取行为与兴趣类目失败");
-    }
-
-    @Override
-    public Result<Object> getKeywordQuery(JSONObject data) {
-        try {
-            Long accountId = data.getLong("accountId");
-            String query_str = data.getString("query_str");
-            Integer type = data.getInteger("type");
-            JSONArray ids = data.getJSONArray("ids");
-            if (Check.isNull(accountId) || Check.isNull(type)) {
-                return Result.error("缺失参数");
-            }
-            if (Check.isNull(query_str) && Check.isNull(ids)) {
-                return Result.error("缺失参数");
-            }
-            CtopOauthToken token = tokenService.getTokenByAccountId(accountId);
-            String url = PropertiesUtils.getConfig("kuaishou_api_url") + KuaishouInterfaceConstant.KEYWORD_QUERY;
-            Map<String, String> header = new HashMap<>();
-            header.put("Access-Token", token.getAccessToken());
-            header.put("Content-Type", "application/json");
-            JSONObject params = new JSONObject();
-            params.put("advertiser_id", accountId);
-            params.put("type", type);
-            if (!Check.isNull(query_str)) {
-                params.put("query_str", query_str);
-            }
-            if (!Check.isNull(ids)) {
-                params.put("ids", ids);
-            }
-            String result = HttpUtils.httpPostRequest(url, params, header);
-            JSONObject resultJson = JSONObject.parseObject(result);
-            if (!Check.isNull(resultJson)) {
-                Integer code = resultJson.getInteger("code");
-                if (code == 0) {
-                    return Result.ok(resultJson.getJSONObject("data"));
-                } else {
-                    log.info("获取行为兴趣关键词失败,accountId:{},返回信息:{}", accountId, resultJson);
-                    return Result.error("获取行为兴趣关键词失败," + resultJson.getString("message"));
-                }
-            }
-        } catch (Exception e) {
-            log.error("获取行为兴趣关键词失败", e);
-        }
-        return Result.error("获取行为兴趣关键词失败");
-    }
-
 }