Explorar o código

fix to bytedance homepage on 2020.6.3

jiequan.bi %!s(int64=5) %!d(string=hai) anos
pai
achega
56ff07ca47

+ 17 - 3
module-toutiao/src/main/java/cn/com/ctop/toutiao/modules/report/controller/BytedanceHomepageReportCtrl.java

@@ -39,14 +39,14 @@ public class BytedanceHomepageReportCtrl {
     }
 
     @PostMapping("/report/chart")
-    public Result<List<JSONObject>> getChartReport(@RequestBody JSONObject requestBody) {
-        Result<List<JSONObject>> result = new Result<>();
+    public Result<Map<String,List<JSONObject>>> getChartReport(@RequestBody JSONObject requestBody) {
+        Result<Map<String,List<JSONObject>>> result = new Result<>();
         if(requestBody.isEmpty()){
             log.error("request body is empty");
             result.setSuccess(false);
         }
         try {
-            List<JSONObject> chartReport=bytedanceHomepageReportService.getChartByDate(requestBody);
+            Map<String,List<JSONObject>> chartReport=bytedanceHomepageReportService.getChartByDate(requestBody);
             result.setResult(chartReport);
             result.setSuccess(true);
         } catch (Exception e) {
@@ -55,6 +55,20 @@ public class BytedanceHomepageReportCtrl {
         }
 
         return result;
+    }
+    @PostMapping("/report/compare")
+    public Result<Map<String,JSONObject>> getCompareReport(@RequestBody JSONObject requestBody) {
+        Result<Map<String,JSONObject>> result = new Result<>();
+        try {
+            Map<String,JSONObject> chartReport=bytedanceHomepageReportService.getCompareByDate(requestBody);
+            result.setResult(chartReport);
+            result.setSuccess(true);
+        } catch (Exception e) {
+            log.error(e.getMessage(),e);
+            result.setSuccess(false);
+        }
 
+        return result;
     }
+
 }

+ 7 - 4
module-toutiao/src/main/java/cn/com/ctop/toutiao/modules/report/mapper/BytedanceHomepageMapper.java

@@ -13,22 +13,25 @@ public interface BytedanceHomepageMapper {
     //今日汇总数据
     JSONObject queryTodaySumReportBy(@Param("statDate") String statDate);
 
+    //根据小时区间查询今日汇总数据
+    List<JSONObject> queryTodaySumByHour(@Param("hour") int hour);
+
     //今日明细数据
     List<JSONObject> queryTodayDetailReportBy(@Param("statDate") String statDate);
 
     //根据开始、结束时间 查询汇总数据
-    JSONObject querySumByStartEndDate(@Param("startDate") String anotherDay, @Param("endDate") String endDate);
+    JSONObject querySumByStartEndDate(@Param("endDate") String endDate, @Param("startDate") String startDate);
 
     //根据开始、结束时间 查询明细数据
-     List<JSONObject> queryDetailByStartEndDate(@Param("startDate") String anotherDay, @Param("endDate") String endDate);
+    List<JSONObject> queryDetailByStartEndDate(@Param("endDate") String endDate, @Param("startDate") String startDate);
 
     //按月分组查询明细数据
-    List<JSONObject> queryDetailGroupMonthByDate(@Param("startDate") String anotherDay, @Param("endDate") String endDate);
+    List<JSONObject> queryDetailGroupMonthByDate(@Param("endDate") String endDate, @Param("startDate") String startDate);
 
     //查询固定月的明细数据
     List<JSONObject> queryDetailGroupDayByMonth(@Param("month") String month);
 
     //项目消耗排行榜
-    List<JSONObject> queryTopCost(@Param("startDate") String anotherDay, @Param("endDate") String endDate, @Param("rank") int rank);
+    List<JSONObject> queryTopCost(@Param("endDate") String endDate, @Param("startDate") String startDate, @Param("rank") int rank);
 
 }

+ 143 - 25
module-toutiao/src/main/java/cn/com/ctop/toutiao/modules/report/mapper/xml/BytedanceHomepageMapper.xml

@@ -7,7 +7,7 @@
        SELECT
 	     ROUND(sum(cost),2) cost,
 	     sum(show_num) showNum,
-	     sum(stat_datetime) click,
+	     sum(click) click,
 	     max(stat_hour) maxHour
        FROM
 	      ctop_bytedance_report_advertiser_hourly
@@ -15,22 +15,59 @@
 	      stat_datetime = #{statDate}
       </select>
 
+    <select id="queryTodaySumByHour" resultType="com.alibaba.fastjson.JSONObject">
+        SELECT
+            ROUND(sum(cost),2) cost,
+            sum(show_num) showNum,
+            sum(click) click
+        FROM
+            ctop_bytedance_report_advertiser_hourly
+        WHERE
+            stat_hour &lt;= #{hour}
+    </select>
 
     <select id="queryTodayDetailReportBy" resultType="com.alibaba.fastjson.JSONObject">
-       SELECT
-         stat_datetime statDate,
-         stat_hour statHour,
-	     ROUND(sum(cost),2) cost,
-	     sum(show_num) showNum,
-	     sum(click) click
-	   FROM
-	      ctop_bytedance_report_advertiser_hourly
-       WHERE
-	      stat_datetime = #{statDate}
-       GROUP BY
-	      stat_hour
-       ORDER BY
-	      stat_hour ASC
+        SELECT
+            stat_datetime statDate,
+            stat_hour statHour,
+            ROUND(sum(cost), 2) cost,
+            sum(show_num) showNum,
+            sum(click) click,
+            CASE
+                WHEN (click / show_num) IS NULL THEN
+                    0
+                ELSE
+                    CONCAT(
+                            ROUND(
+                                    (click / show_num) * 100,
+                                    2
+                                ),
+                            '%'
+                        )
+                END AS 'clickRate',
+            CASE
+                WHEN (cost / show_num) IS NULL THEN
+                    0
+                ELSE
+                    ROUND(
+                            (cost / show_num) * 1000,
+                            2
+                        )
+                END AS 'cpm',
+            CASE
+                WHEN (click / cost) IS NULL THEN
+                    0
+                ELSE
+                    ROUND((cost / click), 2)
+                END AS 'cp'
+        FROM
+            ctop_bytedance_report_advertiser_hourly
+        WHERE
+            stat_datetime = #{statDate}
+        GROUP BY
+            stat_hour
+        ORDER BY
+            stat_hour ASC
 
     </select>
 
@@ -49,9 +86,36 @@
 
     <select id="queryDetailByStartEndDate" resultType="com.alibaba.fastjson.JSONObject">
        SELECT
-         ROUND(sum(cost), 2) cost,
-         sum(show_num) showNum,
-         sum(click) click,
+           ROUND(sum(cost), 2) cost,
+           sum(show_num) showNum,
+           sum(click) click,
+           CASE
+               WHEN (sum(click) / sum(show_num)) IS NULL THEN
+                   0
+               ELSE
+                   CONCAT(
+                           ROUND(
+                                   (sum(click) / sum(show_num)) * 100,
+                                   2
+                               ),
+                           '%'
+                       )
+               END AS 'clickRate',
+           CASE
+               WHEN (sum(cost) / sum(show_num)) IS NULL THEN
+                   0
+               ELSE
+                   ROUND(
+                           (sum(cost) / sum(show_num)) * 1000,
+                           2
+                       )
+               END AS 'cpm',
+           CASE
+               WHEN (sum(click) / sum(cost)) IS NULL THEN
+                   0
+               ELSE
+                   ROUND((sum(cost) / sum(click)), 2)
+               END AS 'cp',
          DATE_FORMAT(stat_datetime,'%Y-%m-%d') statDate
        FROM
          ctop_bytedance_report_advertiser_daily
@@ -64,9 +128,36 @@
 
     <select id="queryDetailGroupMonthByDate" resultType="com.alibaba.fastjson.JSONObject">
        SELECT
-         ROUND(sum(cost), 2) cost,
-         sum(show_num) showNum,
-         sum(click) click,
+           ROUND(sum(cost), 2) cost,
+           sum(show_num) showNum,
+           sum(click) click,
+           CASE
+               WHEN (sum(click) / sum(show_num)) IS NULL THEN
+                   0
+               ELSE
+                   CONCAT(
+                           ROUND(
+                                   (sum(click) / sum(show_num)) * 100,
+                                   2
+                               ),
+                           '%'
+                       )
+               END AS 'clickRate',
+           CASE
+               WHEN (sum(cost) / sum(show_num)) IS NULL THEN
+                   0
+               ELSE
+                   ROUND(
+                           (sum(cost) / sum(show_num)) * 1000,
+                           2
+                       )
+               END AS 'cpm',
+           CASE
+               WHEN (sum(click) / sum(cost)) IS NULL THEN
+                   0
+               ELSE
+                   ROUND((sum(cost) / sum(click)), 2)
+               END AS 'cp',
          DATE_FORMAT(stat_datetime,'%Y-%m') statDate
        FROM
          ctop_bytedance_report_advertiser_daily
@@ -79,10 +170,37 @@
     </select>
 
     <select id="queryDetailGroupDayByMonth" resultType="com.alibaba.fastjson.JSONObject">
-       SELECT
-         ROUND(sum(cost), 2) cost,
-         sum(show_num) showNum,
-         sum(click) click,
+        SELECT
+            ROUND(sum(cost), 2) cost,
+            sum(show_num) showNum,
+            sum(click) click,
+            CASE
+                WHEN (sum(click) / sum(show_num)) IS NULL THEN
+                    0
+                ELSE
+                    CONCAT(
+                            ROUND(
+                                    (sum(click) / sum(show_num)) * 100,
+                                    2
+                                ),
+                            '%'
+                        )
+                END AS 'clickRate',
+            CASE
+                WHEN (sum(cost) / sum(show_num)) IS NULL THEN
+                    0
+                ELSE
+                    ROUND(
+                            (sum(cost) / sum(show_num)) * 1000,
+                            2
+                        )
+                END AS 'cpm',
+            CASE
+                WHEN (sum(click) / sum(cost)) IS NULL THEN
+                    0
+                ELSE
+                    ROUND((sum(cost) / sum(click)), 2)
+                END AS 'cp',
          DATE_FORMAT(stat_datetime,'%Y-%m-%d') statDate,
          SUBSTR(DATE_FORMAT(stat_datetime,'%Y-%m-%d') FROM 9 FOR 2) day
        FROM

+ 4 - 3
module-toutiao/src/main/java/cn/com/ctop/toutiao/modules/report/service/IBytedanceHomepageReportService.java

@@ -7,9 +7,10 @@ import java.util.Map;
 
 public interface IBytedanceHomepageReportService {
 
-    Map<String,JSONObject> getWeekReport(JSONObject json);
+    Map<String,JSONObject> getWeekReport(JSONObject requestBody);
 
-    List<JSONObject> getChartByDate(JSONObject json);
+    Map<String,List<JSONObject>> getChartByDate(JSONObject requestBody);
+
+    Map<String,JSONObject> getCompareByDate(JSONObject requestBody);
 
-    JSONObject getTopCostByDate(JSONObject json);
 }

+ 100 - 41
module-toutiao/src/main/java/cn/com/ctop/toutiao/modules/report/service/impl/BytedanceHomepageReportServiceImpl.java

@@ -44,23 +44,29 @@ public class BytedanceHomepageReportServiceImpl implements IBytedanceHomepageRep
         top.put("top7", costTop);
         //周明细环比数据拼接
         JSONObject costWeekData = new JSONObject();
+        JSONObject costWeekLink = new JSONObject();
         JSONObject clickWeekData = new JSONObject();
+        JSONObject clickWeekLink = new JSONObject();
         JSONObject showNumWeekData = new JSONObject();
+        JSONObject showNumWeekLink = new JSONObject();
         weekDetailData.forEach(day -> {
             costWeekData.put((String) day.get("statDate"), day.get("cost"));
             clickWeekData.put((String) day.get("statDate"), day.get("click"));
             showNumWeekData.put((String) day.get("statDate"), day.get("showNum"));
         });
-        costWeekData.put("weekCostSum", weekSumData.get("cost"));
-        costWeekData.put("weekCostSumLink", countLink(weekSumData.getBigDecimal("cost"), weekBeforeSumData.getBigDecimal("cost")));
-        clickWeekData.put("weekClickSum", weekSumData.get("click"));
-        clickWeekData.put("weekClickSumLink", countLink(BigDecimal.valueOf(weekSumData.getLongValue("click")), BigDecimal.valueOf(weekBeforeSumData.getLong("click"))));
-        showNumWeekData.put("weekCostSum", weekSumData.get("showNum"));
-        showNumWeekData.put("weekShowNumSumLink", countLink(BigDecimal.valueOf(weekSumData.getLongValue("showNum")), BigDecimal.valueOf(weekBeforeSumData.getLong("showNum"))));
+        costWeekLink.put("weekCostSum", weekSumData.get("cost"));
+        costWeekLink.put("weekCostSumLink", countLink(weekSumData.getBigDecimal("cost"), weekBeforeSumData.getBigDecimal("cost")));
+        clickWeekLink.put("weekClickSum", weekSumData.get("click"));
+        clickWeekLink.put("weekClickSumLink", countLink(BigDecimal.valueOf(weekSumData.getLongValue("click")), BigDecimal.valueOf(weekBeforeSumData.getLong("click"))));
+        showNumWeekLink.put("weekShowNumSum", weekSumData.get("showNum"));
+        showNumWeekLink.put("weekShowNumSumLink", countLink(BigDecimal.valueOf(weekSumData.getLongValue("showNum")), BigDecimal.valueOf(weekBeforeSumData.getLong("showNum"))));
 
         result.put("costWeek", costWeekData);
+        result.put("costWeekLink", costWeekLink);
         result.put("clickWeek", clickWeekData);
+        result.put("clickWeekLink", clickWeekLink);
         result.put("showNumWeek", showNumWeekData);
+        result.put("showNumWeekLink", showNumWeekLink);
         result.put("top", top);
 
         return result;
@@ -70,12 +76,14 @@ public class BytedanceHomepageReportServiceImpl implements IBytedanceHomepageRep
      * timeType :1,2,3,4,5,6,7,8,9  前端约束入参类型
      */
     @Override
-    public List<JSONObject> getChartByDate(JSONObject requestBody) {
+    public Map<String,List<JSONObject>> getChartByDate(JSONObject requestBody) {
         int timeType = requestBody.getInteger("type");
-        List<JSONObject> resultBody = new ArrayList<>();
+        Map<String,List<JSONObject>> resultBody=new HashMap<>();
         if (requestBody.isEmpty()) {
             return resultBody;
         }
+        List<JSONObject> resultChart = new ArrayList<>();
+        List<JSONObject> resultList = new ArrayList<>();
         //当天日期
         String nowDate = DateUtils.getNowDate(SystemDateConstant.yyyy_MM_dd);
         //startTime 为取什么时间段数据的开始时间,默认昨天
@@ -83,69 +91,128 @@ public class BytedanceHomepageReportServiceImpl implements IBytedanceHomepageRep
         switch (timeType) {
             //今天
             case 1:
-                resultBody = getCompareGroupByHour(nowDate, anotherDay);
+                resultChart = getCompareGroupByHour(nowDate, anotherDay);
+                resultList = getFormDetailByDate(nowDate,nowDate,"hour");
                 break;
             //昨天
             case 2:
-                resultBody = bytedanceHomepageMapper.queryTodayDetailReportBy(anotherDay);
+                resultChart = getCompareGroupByHour(anotherDay, DateUtils.getAnotherDay(SystemDateConstant.yyyy_MM_dd, anotherDay, -1));
+                resultList = getFormDetailByDate(anotherDay,anotherDay,"hour");
                 break;
             //近一周
             case 3:
-                resultBody = bytedanceHomepageMapper.queryDetailByStartEndDate(nowDate,
+                resultChart = bytedanceHomepageMapper.queryDetailByStartEndDate(nowDate,
                         DateUtils.getAnotherDay(SystemDateConstant.yyyy_MM_dd, anotherDay, -6));
+                resultList = getFormDetailByDate(nowDate, DateUtils.getAnotherDay(SystemDateConstant.yyyy_MM_dd, anotherDay, -6),"day");
                 break;
             //近半个月(15天)
             case 4:
-                resultBody = bytedanceHomepageMapper.queryDetailByStartEndDate(nowDate,
+                resultChart = bytedanceHomepageMapper.queryDetailByStartEndDate(nowDate,
                         DateUtils.getAnotherDay(SystemDateConstant.yyyy_MM_dd, anotherDay, -14));
+                resultList = getFormDetailByDate(nowDate, DateUtils.getAnotherDay(SystemDateConstant.yyyy_MM_dd, anotherDay, -14),"day");
                 break;
             //近一个月
             case 5:
-                resultBody = bytedanceHomepageMapper.queryDetailByStartEndDate(nowDate,
+                resultChart = bytedanceHomepageMapper.queryDetailByStartEndDate(nowDate,
                         DateUtils.getMonthBefore(SystemDateConstant.yyyy_MM_dd, anotherDay, -1));
+                resultList = getFormDetailByDate(nowDate, DateUtils.getMonthBefore(SystemDateConstant.yyyy_MM_dd, anotherDay, -1),"day");
                 break;
             //近三个月
             case 6:
-                resultBody = bytedanceHomepageMapper.queryDetailByStartEndDate(nowDate,
+                resultChart = bytedanceHomepageMapper.queryDetailByStartEndDate(nowDate,
                         DateUtils.getMonthBefore(SystemDateConstant.yyyy_MM_dd, anotherDay, -3));
+                resultList = getFormDetailByDate(nowDate, DateUtils.getMonthBefore(SystemDateConstant.yyyy_MM_dd, anotherDay, -3),"day");
                 break;
             //近半年
             case 7:
-                resultBody = bytedanceHomepageMapper.queryDetailByStartEndDate(nowDate,
+                resultChart = bytedanceHomepageMapper.queryDetailByStartEndDate(nowDate,
                         DateUtils.getMonthBefore(SystemDateConstant.yyyy_MM_dd, anotherDay, -6));
+                resultList = getFormDetailByDate(nowDate, DateUtils.getMonthBefore(SystemDateConstant.yyyy_MM_dd, anotherDay, -6),"day");
                 break;
-            //全部数据
+            //近一年
             case 8:
-                resultBody = bytedanceHomepageMapper.queryDetailGroupMonthByDate(SystemDateConstant.BEGIN_TIME, nowDate);
+                resultChart = bytedanceHomepageMapper.queryDetailGroupMonthByDate( nowDate, SystemDateConstant.BEGIN_TIME);
+                resultList = getFormDetailByDate(nowDate, SystemDateConstant.BEGIN_TIME,"day");
                 break;
             //自定义起始时间
             case 9:
-                resultBody = bytedanceHomepageMapper.queryDetailByStartEndDate(requestBody.getString("startDate"), requestBody.getString("endDate"));
-                break;
-            //对比
-            case 10:
-                resultBody = getChartByType_10(requestBody.getString("sign"), requestBody.getString("smallDate"), requestBody.getString("bigDate"));
+                resultChart = bytedanceHomepageMapper.queryDetailByStartEndDate(requestBody.getString("endDate"), requestBody.getString("startDate"));
+                resultList = getFormDetailByDate(requestBody.getString("endDate"), requestBody.getString("startDate"),"day");
                 break;
             default:
         }
+        resultBody.put("chart",resultChart);
+        resultBody.put("list",resultList);
         return resultBody;
     }
 
     @Override
-    public JSONObject getTopCostByDate(JSONObject requestBody) {
-        return null;
+    public Map<String, JSONObject> getCompareByDate(JSONObject requestBody) {
+        Map<String,JSONObject> resultBody=new HashMap<>();
+        if (requestBody.isEmpty()) {
+            return resultBody;
+        }
+        String sign= requestBody.getString("sign");
+        String smallDate= requestBody.getString("smallDate");
+        String bigDate= requestBody.getString("bigDate");
+        resultBody.put("chart",this.getCompareChart(sign,smallDate,bigDate));
+        resultBody.put("list",this.getCompareList(sign,smallDate,bigDate));
+
+        return resultBody;
+    }
+
+
+    //表格数据查询
+    private List<JSONObject> getFormDetailByDate(String endDate, String startDate, String group){
+        List<JSONObject> result =null;
+        if("day".equals(group)){
+            result= bytedanceHomepageMapper.queryDetailByStartEndDate(endDate,startDate);
+        }else {
+            result= bytedanceHomepageMapper.queryTodayDetailReportBy(endDate);
+        }
+        return getListLink(result);
     }
 
     //自定义日期对比(日对比,月对比)
-    private List<JSONObject> getChartByType_10(String sign, String smallDate, String bigDate) {
-        List<JSONObject> result = new ArrayList<>();
+    private JSONObject getCompareChart(String sign, String smallDate, String bigDate) {
+        JSONObject result = new JSONObject();
         if (!sign.isEmpty() && sign.equals("month")) {
-            result = this.getCompareGroupByDay(smallDate, bigDate);
+            result.put("compare",this.getCompareGroupByDay(smallDate, bigDate));
         } else if (!sign.isEmpty() && sign.equals("day")) {
-            result = this.getCompareGroupByHour(bigDate, smallDate);
+            result.put("compare",this.getCompareGroupByHour(bigDate, smallDate));
+        }
+        return result;
+    }
+    private JSONObject getCompareList(String sign, String smallDate, String bigDate){
+        JSONObject result = new JSONObject();
+        if (!sign.isEmpty() && sign.equals("month")) {
+            result.put("smallDate",getListLink(bytedanceHomepageMapper.queryDetailGroupDayByMonth(smallDate)));
+            result.put("bigDate",getListLink(bytedanceHomepageMapper.queryDetailGroupDayByMonth(bigDate)));
+        } else if (!sign.isEmpty() && sign.equals("day")) {
+            result.put("smallDate",getListLink(bytedanceHomepageMapper.queryTodayDetailReportBy(smallDate)));
+            result.put("bigDate",getListLink(bytedanceHomepageMapper.queryTodayDetailReportBy(bigDate)));
         }
         return result;
     }
+    //List环比计算
+    private List<JSONObject> getListLink(List<JSONObject> result){
+        if(result.size()>1){
+            for(int i=1;i<result.size();i++){
+                if(i==1){result.get(i).put("costProportion",BigDecimal.ZERO);}
+                result.get(i).put("costProportion",countLink((BigDecimal) result.get(i).get("cost"),(BigDecimal) result.get(i-1).get("cost")));
+            }
+        }
+        return result;
+    }
+    /*private JSONObject getCompareSum(String sign, String smallDate, String bigDate){
+        JSONObject result = new JSONObject();
+        if (!sign.isEmpty() && sign.equals("month")) {
+            bytedanceHomepageMapper.querySumByStartEndDate()
+        } else if (!sign.isEmpty() && sign.equals("day")) {
+            result = this.getCompareGroupByHour(bigDate, smallDate);
+        }
+        return result;
+    }*/
 
     //两个月份的数据对比
     private List<JSONObject> getCompareGroupByDay(String smallDate, String bigDate) {
@@ -153,15 +220,8 @@ public class BytedanceHomepageReportServiceImpl implements IBytedanceHomepageRep
         List<JSONObject> afterDetail = bytedanceHomepageMapper.queryDetailGroupDayByMonth(bigDate);
         //小月份数据
         List<JSONObject> beforeDetail = bytedanceHomepageMapper.queryDetailGroupDayByMonth(smallDate);
-        List<JSONObject> beforeData;List<JSONObject> afterData;
-        if (afterDetail.size() >= beforeDetail.size()) {
-            beforeData = afterDetail;
-            afterData = beforeData;
-        } else {
-            beforeData = beforeDetail;
-            afterData = afterDetail;
-        }
-        return this.getCompareDate(beforeData,afterData,"day");
+
+        return this.getCompareDate(beforeDetail,afterDetail,"day");
     }
 
     //今天昨天数据对比
@@ -175,9 +235,9 @@ public class BytedanceHomepageReportServiceImpl implements IBytedanceHomepageRep
     private List<JSONObject> getCompareDate(List<JSONObject> beforeData, List<JSONObject> AfterData, String standard) {
         List<JSONObject> result = new ArrayList<>();
         beforeData.forEach(before -> {
-            String yesStatHour = String.valueOf(before.get(standard));
+            String yesStat = String.valueOf(before.get(standard));
             AfterData.forEach(after -> {
-                if ((String.valueOf(after.get(standard))).equals(yesStatHour)) {
+                if ((String.valueOf(after.get(standard))).equals(yesStat)) {
                     //消耗较昨日环比
                     after.put("costProportion", countLink(after.getBigDecimal("cost"), before.getBigDecimal("cost")));
                     //展示量较昨日环比
@@ -201,5 +261,4 @@ public class BytedanceHomepageReportServiceImpl implements IBytedanceHomepageRep
         return link;
     }
 
-}
-
+}