ソースを参照

添加异步批量复制定时任务
人群报表视图数据——商业维度数据取top10优化处理。

zhaoxian 4 年 前
コミット
154950503c

+ 25 - 0
module-job-kuaishou/src/main/java/cn/com/ctop/job/kuaishou/handler/KuaishouCampaignCopyDateJob.java

@@ -0,0 +1,25 @@
+package cn.com.ctop.job.kuaishou.handler;
+
+import cn.com.ctop.kuaishou.modules.batch.service.IKuaishouCampaignCopyDataService;
+import com.xxl.job.core.handler.annotation.XxlJob;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Component;
+
+/**
+ * 定时复制广告计划下的组+创意
+ *
+ * @param
+ * @author ZHAOXA
+ * @return
+ * @throws
+ */
+@Component
+public class KuaishouCampaignCopyDateJob {
+    @Autowired
+    private IKuaishouCampaignCopyDataService kuaishouCampaignCopyDataService;
+
+    @XxlJob("kuaishouCampaignCopyDateJob")
+    public void execute() throws Exception {
+        kuaishouCampaignCopyDataService.batchCopyCampaign();
+    }
+}

+ 13 - 2
module-kuaishou/src/main/java/cn/com/ctop/kuaishou/modules/batch/controller/KuaishouCampaignCopyDataController.java

@@ -1,17 +1,21 @@
 package cn.com.ctop.kuaishou.modules.batch.controller;
 
 import cn.com.ctop.common.module.annotation.AutoLog;
+import cn.com.ctop.common.module.utils.Check;
 import cn.com.ctop.kuaishou.modules.batch.entity.KuaishouCampaignCopyData;
 import cn.com.ctop.kuaishou.modules.batch.service.IKuaishouCampaignCopyDataService;
 import com.alibaba.fastjson.JSON;
+import com.alibaba.fastjson.JSONObject;
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.baomidou.mybatisplus.core.metadata.IPage;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import io.swagger.annotations.Api;
 import io.swagger.annotations.ApiOperation;
 import lombok.extern.slf4j.Slf4j;
+import org.apache.shiro.SecurityUtils;
 import org.jeecg.common.api.vo.Result;
 import org.jeecg.common.system.query.QueryGenerator;
+import org.jeecg.common.system.vo.LoginUser;
 import org.jeecg.common.util.oConvertUtils;
 import org.jeecgframework.poi.excel.ExcelImportUtil;
 import org.jeecgframework.poi.excel.def.NormalExcelConstants;
@@ -51,7 +55,7 @@ import java.util.Map;
 @Slf4j
 @Api(tags = "待复制广告计划数据")
 @RestController
-@RequestMapping("/kuaishou/kuaishouCampaignCopyData")
+@RequestMapping("/kuaishouCampaignCopyData")
 public class KuaishouCampaignCopyDataController {
     @Autowired
     private IKuaishouCampaignCopyDataService kuaishouCampaignCopyDataService;
@@ -90,9 +94,16 @@ public class KuaishouCampaignCopyDataController {
     @AutoLog(value = "待复制广告计划数据-添加")
     @ApiOperation(value = "待复制广告计划数据-添加", notes = "待复制广告计划数据-添加")
     @PostMapping(value = "/add")
-    public Result<KuaishouCampaignCopyData> add(@RequestBody KuaishouCampaignCopyData kuaishouCampaignCopyData) {
+    public Result<KuaishouCampaignCopyData> add(@RequestBody JSONObject data) {
         Result<KuaishouCampaignCopyData> result = new Result<>();
         try {
+            KuaishouCampaignCopyData kuaishouCampaignCopyData = JSONObject.toJavaObject(data, KuaishouCampaignCopyData.class);
+            if (Check.isNull(kuaishouCampaignCopyData.getCreaterId())) {
+                LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
+                if (!Check.isNull(sysUser)) {
+                    kuaishouCampaignCopyData.setCreaterId(sysUser.getId());
+                }
+            }
             kuaishouCampaignCopyDataService.save(kuaishouCampaignCopyData);
             result.success("添加成功!");
         } catch (Exception e) {

+ 66 - 56
module-kuaishou/src/main/java/cn/com/ctop/kuaishou/modules/batch/service/impl/KuaishouCampaignCopyDataServiceImpl.java

@@ -81,15 +81,15 @@ public class KuaishouCampaignCopyDataServiceImpl extends ServiceImpl<KuaishouCam
                 JSONObject data = getNewCampaigns(token.getAccessToken(), copyData);
                 data.put("copyData", copyData);
                 //根据被复制的组数据和模板、预算和排期数据调用快手创建组接口
-                createCopyUnits(token, data);
+                boolean isSuccess = createCopyUnits(token, data);
                 //更新成功,修改状态并发送通知
-                updateState(copyData.getId(), NoEn.NO2.valueStr(), "复制成功");
-                sendNotification(copyData.getOldCampaignName(), copyData.getCreaterId(), NoEn.NO2.valueStr());
+                if (isSuccess) {
+                    sendNotification(copyData.getId(), copyData.getOldCampaignName(), copyData.getCreaterId(), NoEn.NO2.valueStr(), "复制成功");
+                }
             } catch (Exception e) {
                 log.error("复制广告计划异常", e);
                 //复制过程中失败,更新状态并发送预警
-                updateState(copyData.getId(), NoEn.NO3.valueStr(), e.getMessage());
-                sendNotification(copyData.getOldCampaignName(), copyData.getCreaterId(), NoEn.NO3.valueStr());
+                sendNotification(copyData.getId(), copyData.getOldCampaignName(), copyData.getCreaterId(), NoEn.NO3.valueStr(), e.getMessage());
             }
 
 
@@ -133,8 +133,7 @@ public class KuaishouCampaignCopyDataServiceImpl extends ServiceImpl<KuaishouCam
             List<KuaiShouGroup> kuaiShouGroups = kuaiShouGroupService.queryUnitInfoByCampaignId(copyData.getAccountId(), campaignId);
             if (Check.isNull(kuaiShouGroups)) {
                 //获取广告组失败,更新状态并发送通知
-                updateState(copyData.getId(), NoEn.NO3.valueStr(), "被复制的计划更新广告组失败");
-                sendNotification(copyData.getOldCampaignName(), copyData.getCreaterId(), NoEn.NO3.valueStr());
+                sendNotification(copyData.getId(), copyData.getOldCampaignName(), copyData.getCreaterId(), NoEn.NO3.valueStr(), "被复制的计划更新广告组失败");
             }
             for (KuaiShouGroup kuaiShouGroup : kuaiShouGroups) {
                 kuaiShouGroup.setTarget(kuaiShouGroupTargetService.queryTargetInfoByunitId(copyData.getAccountId(), kuaiShouGroup.getUnitId()));
@@ -158,11 +157,13 @@ public class KuaishouCampaignCopyDataServiceImpl extends ServiceImpl<KuaishouCam
      * @throws
      * @author ZHAOXA
      */
-    private void createCopyUnits(CtopOauthToken token, JSONObject data) throws Exception {
-        try {
-            JSONArray groups = data.getJSONArray("groups");
-            JSONObject copyData = data.getJSONObject("copyData");
-            for (int i = 0; i < groups.size(); i++) {
+    private boolean createCopyUnits(CtopOauthToken token, JSONObject data) throws Exception {
+        int count = 0;
+        String msg = "";
+        JSONArray groups = data.getJSONArray("groups");
+        JSONObject copyData = data.getJSONObject("copyData");
+        for (int i = 0; i < groups.size(); i++) {
+            try {
                 JSONObject group = groups.getJSONObject(i);
                 JSONObject unitJson = new JSONObject();
                 unitJson.put("campaign_id", copyData.getLong("newCampaignId"));
@@ -175,14 +176,17 @@ public class KuaishouCampaignCopyDataServiceImpl extends ServiceImpl<KuaishouCam
                 // 资源创作方式
                 unitJson.put("unit_type", group.getInteger("unitType"));
                 // 转化目标id
-                unitJson.put("convert_id", group.getInteger("convertId"));
-
+                if (!Check.isNull(group.getInteger("convertId"))) {
+                    unitJson.put("convert_id", group.getInteger("convertId"));
+                }
                 // 优先从系统应用商店下载
-                unitJson.put("use_app_market", group.getInteger("useAppMarket"));
-
+                if (!Check.isNull(group.getInteger("useAppMarket"))) {
+                    unitJson.put("use_app_market", group.getInteger("useAppMarket"));
+                }
                 // 应用商店列表
-                unitJson.put("app_store", group.getJSONArray("appStore"));
-
+                if (!Check.isNull(group.getJSONArray("appStore"))) {
+                    unitJson.put("app_store", group.getJSONArray("appStore"));
+                }
                 //投放开始时间
                 String beginTime = copyData.getString("beginTime");
                 if (Check.isNull(beginTime)) {
@@ -361,43 +365,53 @@ public class KuaishouCampaignCopyDataServiceImpl extends ServiceImpl<KuaishouCam
                     unitJson.put("target", targetJson);
                 }
                 // 出价
-                String bid = copyData.getString("bid");
+                Long bid = copyData.getLong("bid");
                 if (Check.isNull(bid)) {
-                    bid = group.getString("bid");
+                    bid = group.getLong("bid");
+                }
+                if (!Check.isNull(bid)) {
+                    unitJson.put("bid", bid);
                 }
-                unitJson.put("bid", bid);
                 // 出价类型
-                String bidType = copyData.getString("bidType");
+                Integer bidType = copyData.getInteger("bidType");
                 if (Check.isNull(bidType)) {
-                    bidType = group.getString("bidType");
+                    bidType = group.getInteger("bidType");
+                }
+                if (!Check.isNull(bidType)) {
+                    unitJson.put("bid_type", bidType);
                 }
-                unitJson.put("bid_type", bidType);
                 // 深度转化出价
-                String cpaBid = copyData.getString("cpaBid");
+                Long cpaBid = copyData.getLong("cpaBid");
                 if (Check.isNull(cpaBid)) {
-                    cpaBid = group.getString("cpaBid");
+                    cpaBid = group.getLong("cpaBid");
+                }
+                if (!Check.isNull(cpaBid)) {
+                    unitJson.put("cpa_bid", cpaBid);
                 }
-                unitJson.put("cpa_bid", cpaBid);
                 // 深度转化目标出价
-                String deepConversionBid = copyData.getString("deepConversionBid");
+                Long deepConversionBid = copyData.getLong("deepConversionBid");
                 if (Check.isNull(deepConversionBid)) {
-                    deepConversionBid = group.getString("deepConversionBid");
+                    deepConversionBid = group.getLong("deepConversionBid");
                 }
                 if (!Check.isNull(deepConversionBid)) {
                     unitJson.put("deep_conversion_bid", deepConversionBid);
                 }
                 // 深度转化目标
-                String deepConversionType = copyData.getString("deepConversionType");
+                Integer deepConversionType = copyData.getInteger("deepConversionType");
                 if (Check.isNull(deepConversionType)) {
-                    deepConversionType = group.getString("deepConversionType");
+                    deepConversionType = group.getInteger("deepConversionType");
+                }
+                if (!Check.isNull(deepConversionType)) {
+                    unitJson.put("deep_conversion_type", deepConversionType);
                 }
-                unitJson.put("deep_conversion_type", deepConversionType);
                 // 优化目标
-                String ocpxActionType = copyData.getString("ocpxActionType");
+                Integer ocpxActionType = copyData.getInteger("ocpxActionType");
                 if (Check.isNull(ocpxActionType)) {
-                    ocpxActionType = group.getString("ocpxActionType");
+                    ocpxActionType = group.getInteger("ocpxActionType");
+                }
+                if (!Check.isNull(ocpxActionType)) {
+                    unitJson.put("ocpx_action_type", ocpxActionType);
                 }
-                unitJson.put("ocpx_action_type", ocpxActionType);
 
                 //快手广告组名
                 String unitName = group.getString("unitName");
@@ -424,23 +438,30 @@ public class KuaishouCampaignCopyDataServiceImpl extends ServiceImpl<KuaishouCam
                                     batchService.copyCreative(token.getAccountId(), unitId, group.getLong("unitId"), null);
                                 } catch (Exception e) {
                                     log.error("复制创建广告创意异常", e);
-                                    updateState(copyData.getLong("id"), NoEn.NO3.valueStr(), "复制创建广告创意异常");
-                                    sendNotification(copyData.getString("oldCampaignName"), copyData.getString("createrId"), NoEn.NO3.valueStr());
                                 }
                             }
                         };
                         thread.start();
+                    } else {
+                        count++;
+                        msg = (String) returnUnitMap.get("message");
                     }
                 } else {
-                    updateState(copyData.getLong("id"), NoEn.NO3.valueStr(), (String) returnUnitMap.get("message"));
-                    sendNotification(copyData.getString("oldCampaignName"), copyData.getString("createrId"), NoEn.NO3.valueStr());
+                    count++;
                 }
+            } catch (Exception e) {
+                count++;
+                log.error("复制过程调用快手接口异常", e);
             }
-        } catch (Exception e) {
-            log.error("复制过程调用快手接口异常", e);
-            throw new Exception("复制过程调用快手接口异常");
         }
+        //全部失败
 
+        if (count == 0) {
+            return true;
+        } else {
+            sendNotification(copyData.getLong("id"), copyData.getString("oldCampaignName"), copyData.getString("createrId"), NoEn.NO3.valueStr(), msg);
+        }
+        return false;
     }
 
     /**
@@ -464,24 +485,13 @@ public class KuaishouCampaignCopyDataServiceImpl extends ServiceImpl<KuaishouCam
      * @throws
      * @author ZHAOXA
      */
-    private void sendNotification(String name, String userId, String state) {
-        String msg = messageTemplate.getCopyCampaignMessage(name, state);
-        sendMessageService.sendMessage(userId, msg);
-    }
-
-    /**
-     * 更新预复制数据状态
-     *
-     * @param
-     * @return void
-     * @throws
-     * @author ZHAOXA
-     */
-    private void updateState(Long id, String state, String StateDetail) {
+    private void sendNotification(Long id, String name, String userId, String state, String StateDetail) {
         KuaishouCampaignCopyData copyData = new KuaishouCampaignCopyData();
         copyData.setId(id);
         copyData.setState(state);
         copyData.setStateDetail(StateDetail);
         this.updateById(copyData);
+        String msg = messageTemplate.getCopyCampaignMessage(name, state);
+        sendMessageService.sendMessage(userId, msg);
     }
 }

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

@@ -1,5 +1,6 @@
 package cn.com.ctop.kuaishou.modules.report.entity;
 
+import com.baomidou.mybatisplus.annotation.TableField;
 import com.baomidou.mybatisplus.annotation.TableName;
 import io.swagger.annotations.ApiModel;
 import lombok.Data;
@@ -23,4 +24,7 @@ public class KuaishouAudienceReportDailyAccount extends KuaishouAudienceReportDa
      * province: 省级地域分布,city: 地级地域分布,gender: 性别分布,ageSegment: 年龄分布,clientId: 系统分布,businessInterestTags: 商业兴趣分布
      */
     private java.lang.String audienceType;
+
+    @TableField(exist = false)
+    private java.lang.Integer count;
 }

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

@@ -18,4 +18,8 @@ public interface KuaishouAudienceReportDailyAccountMapper extends BaseMapper<Kua
     List<JSONObject> queryPageLists(KuaishouAudienceReportDailyAccount audienceReport);
 
     List<JSONObject> queryAllReportForm(KuaishouAudienceReportDailyAccount audienceReport);
+
+    int querybusinessTypeCount(KuaishouAudienceReportDailyAccount audienceReport);
+
+    List<JSONObject> querybusinessTypeReportForm(KuaishouAudienceReportDailyAccount audienceReport);
 }

+ 105 - 9
module-kuaishou/src/main/java/cn/com/ctop/kuaishou/modules/report/mapper/xml/KuaishouAudienceReportDailyAccountMapper.xml

@@ -4,10 +4,10 @@
 
     <select id="queryPageLists" resultType="com.alibaba.fastjson.JSONObject">
         SELECT
-        account_id,
+        account_id as accountId,
         province,
-        age_segment,
-        business_interest_tags,
+        age_segment as ageSegment,
+        business_interest_tags as businessInterestTags ,
         city,
         client,
         gender,
@@ -16,7 +16,10 @@
         SUM(event_next_day_stay) as eventNextDayStay,
         ROUND(SUM(event_next_day_stay)/SUM(activation)*100,2)as eventNextDayStayRatio
         FROM ctop_kuaishou_audience_report_daily_account
-        where account_id = #{accountId}
+        where 1=1
+        <if test="accountId != null">
+            and account_id = #{accountId}
+        </if>
         <if test="startTime != null">
             AND stat_date &gt;= #{startTime}
         </if>
@@ -58,11 +61,11 @@
 
     <select id="queryAllReportForm" resultType="com.alibaba.fastjson.JSONObject">
         SELECT
-        stat_date,
-        account_id,
+        stat_date as statDate,
+        account_id as accountId,
         province,
-        age_segment,
-        business_interest_tags,
+        age_segment as ageSegment,
+        business_interest_tags as businessInterestTags ,
         city,
         client,
         gender,
@@ -73,7 +76,10 @@
         ROUND(SUM(photo_click)/SUM(photo_show)*100,2) as photoClickRatio,
         ROUND(SUM(bclick)/SUM(aclick)*100,2) as actionRatio
         FROM ctop_kuaishou_audience_report_daily_account
-        where account_id = #{accountId}
+        where 1=1
+        <if test="accountId != null">
+            and account_id = #{accountId}
+        </if>
         <if test="startTime != null">
             AND stat_date &gt;= #{startTime}
         </if>
@@ -105,4 +111,94 @@
             GROUP BY business_interest_tags
         </if>
     </select>
+
+    <select id="querybusinessTypeReportForm" resultType="com.alibaba.fastjson.JSONObject">
+        SELECT
+        statDate as statDate,
+        accountId as accountId,
+        businessInterestTags as businessInterestTags,
+        SUM(photoShow) as photoShow,
+        SUM(photoClick) as photoClick,
+        SUM(aclick) as aclick,
+        SUM(bclick) as bclick,
+        ROUND(SUM(photoClick)/SUM(photoShow)*100,2) as photoClickRatio,
+        ROUND(SUM(bclick)/SUM(aclick)*100,2) as actionRatio
+        FROM(
+        SELECT
+        stat_date as statDate,
+        account_id as accountId,
+        '其他' as businessInterestTags,
+        SUM(photo_show) as photoShow,
+        SUM(photo_click) as photoClick,
+        SUM(aclick) as aclick,
+        SUM(bclick) as bclick
+        FROM ctop_kuaishou_audience_report_daily_account t
+        where 1=1
+        <if test="accountId != null">
+            and account_id = #{accountId}
+        </if>
+        <if test="startTime != null">
+            AND stat_date &gt;= #{startTime}
+        </if>
+        <if test="endTime != null">
+            AND stat_date &lt;= #{endTime}
+        </if>
+        AND audience_type = 'businessInterestTags'
+        GROUP BY business_interest_tags
+        ORDER BY photoShow
+        limit 0,#{count}
+        ) t1
+        union
+        (
+        SELECT
+        stat_date as statDate,
+        account_id as accountId,
+        business_interest_tags as businessInterestTags ,
+        SUM(photo_show) as photoShow,
+        SUM(photo_click) as photoClick,
+        SUM(aclick) as aclick,
+        SUM(bclick) as bclick,
+        ROUND(SUM(photo_click)/SUM(photo_show)*100,2) as photoClickRatio,
+        ROUND(SUM(bclick)/SUM(aclick)*100,2) as actionRatio
+        FROM ctop_kuaishou_audience_report_daily_account
+        where 1=1
+        AND business_interest_tags != '其他'
+        <if test="accountId != null">
+            and account_id = #{accountId}
+        </if>
+        <if test="startTime != null">
+            AND stat_date &gt;= #{startTime}
+        </if>
+        <if test="endTime != null">
+            AND stat_date &lt;= #{endTime}
+        </if>
+        AND audience_type = 'businessInterestTags'
+        GROUP BY business_interest_tags
+        ORDER BY photoShow  desc
+        limit 9
+        )
+    </select>
+
+
+    <select id="querybusinessTypeCount" resultType="java.lang.Integer">
+        SELECT count(1) FROM(
+        SELECT
+        1
+        FROM ctop_kuaishou_audience_report_daily_account
+        where 1=1
+        <if test="accountId != null">
+            and account_id = #{accountId}
+        </if>
+        <if test="startTime != null">
+            AND stat_date &gt;= #{startTime}
+        </if>
+        <if test="endTime != null">
+            AND stat_date &lt;= #{endTime}
+        </if>
+        <if test="audienceType == 'businessInterestTags' ">
+            AND audience_type = #{audienceType}
+            GROUP BY business_interest_tags
+        </if>
+        ) as tt
+    </select>
 </mapper>

+ 4 - 2
module-kuaishou/src/main/java/cn/com/ctop/kuaishou/modules/report/service/impl/KuaishouAudienceReportDailyAccountServiceImpl.java

@@ -80,12 +80,14 @@ public class KuaishouAudienceReportDailyAccountServiceImpl extends ServiceImpl<K
             data.put("client", clients);
         }
         audienceReport.setAudienceType("businessInterestTags");
-        List<JSONObject> bussiness = accountMapper.queryAllReportForm(audienceReport);
+        int count = accountMapper.querybusinessTypeCount(audienceReport);
+        audienceReport.setCount(count - 9);
+        List<JSONObject> bussiness = accountMapper.querybusinessTypeReportForm(audienceReport);
         if (!Check.isNull(bussiness)) {
             data.put("businessInterestTags", bussiness);
         }
         if (data.size() == 0) {
-            return Result.error("只能获取到两天前的数据,请正确选择时间");
+            return Result.error("未查询到数据");
         }
         return Result.ok(data);
     }