Browse Source

查询日期不存在的数据添加默认值

lth 1 year ago
parent
commit
01aae434f1

+ 72 - 30
game-data/game-data-serve/src/main/java/com/zanxiang/game/data/serve/service/impl/AdsOverallSummaryServiceImpl.java

@@ -20,12 +20,10 @@ import org.springframework.stereotype.Service;
 
 import java.beans.PropertyDescriptor;
 import java.math.BigDecimal;
+import java.math.BigInteger;
 import java.math.RoundingMode;
 import java.time.LocalDate;
-import java.util.HashSet;
-import java.util.List;
-import java.util.Objects;
-import java.util.Set;
+import java.util.*;
 
 /**
  * @author tianhua
@@ -64,24 +62,23 @@ public class AdsOverallSummaryServiceImpl implements IAdsOverallSummaryService {
         //得到一个包含有总数据的AdsOverallSummaryVO对象
         AdsOverallSummaryVO totalDataVO = totalDataSql.getObject(AdsOverallSummaryVO.class);
 
-
         //获取查询条件 查询今日数据 需要按当前日期查询
         Condition todayDataCon = myCondition(param, false, 1);
         //查询条件转换为sql语句 设置映射 Entity为 AdsOverallSummary对象
         String todayDataConString = todayDataCon.toSql(dao.getEntity(AdsOverallSummary.class));
         Sql todayDataSql = Sqls.create("SELECT  \n" +
-                "SUM(x.today_player_count) today_player_count ,\n" +
-                "SUM(x.yesterday_player_count) yesterday_player_count ,\n" +
-                "SUM(x.today_total_cost) today_total_cost ,\n" +
-                "SUM(x.yesterday_total_cost) yesterday_total_cost ,\n" +
-                "SUM(x.today_new_player_amount) today_new_player_amount ,\n" +
-                "SUM(x.today_amount) today_amount ,\n" +
-                "SUM(x.yesterday_new_player_amount) yesterday_new_player_amount ,\n" +
-                "SUM(x.yesterday_amount) yesterday_amount ,\n" +
-                "SUM(x.today_agent_count) today_agent_count ,\n" +
-                "SUM(x.yesterday_agent_count) yesterday_agent_count ,\n" +
-                "SUM(x.today_game_count) today_game_count ,\n" +
-                "SUM(x.yesterday_game_count) yesterday_game_count ,\n" +
+                "IFNULL(SUM(x.today_player_count),0) today_player_count ,\n" +
+                "IFNULL(SUM(x.yesterday_player_count),0) yesterday_player_count ,\n" +
+                "IFNULL(SUM(x.today_total_cost),0) today_total_cost ,\n" +
+                "IFNULL(SUM(x.yesterday_total_cost),0) yesterday_total_cost ,\n" +
+                "IFNULL(SUM(x.today_new_player_amount),0) today_new_player_amount ,\n" +
+                "IFNULL(SUM(x.today_amount),0) today_amount ,\n" +
+                "IFNULL(SUM(x.yesterday_new_player_amount),0) yesterday_new_player_amount ,\n" +
+                "IFNULL(SUM(x.yesterday_amount),0) yesterday_amount ,\n" +
+                "IFNULL(SUM(x.today_agent_count),0) today_agent_count ,\n" +
+                "IFNULL(SUM(x.yesterday_agent_count),0) yesterday_agent_count ,\n" +
+                "IFNULL(SUM(x.today_game_count),0) today_game_count ,\n" +
+                "IFNULL(SUM(x.yesterday_game_count),0) yesterday_game_count ,\n" +
                 "IFNULL(SUM(x.first_new_user_amount)/SUM(x.today_total_cost),0) first_roi \n" +
                 "FROM ads_overall_summary x" + todayDataConString);
 
@@ -139,7 +136,8 @@ public class AdsOverallSummaryServiceImpl implements IAdsOverallSummaryService {
         //执行自定义sql语句
         dao.execute(sql);
 
-        return sql.getList(AdsOverallSummaryLineDataVO.class);
+        //判断查询日期的数据是否都存在 结果返回
+        return checkAllDateData(sql.getList(AdsOverallSummaryLineDataVO.class), param);
     }
 
     /**
@@ -172,10 +170,10 @@ public class AdsOverallSummaryServiceImpl implements IAdsOverallSummaryService {
         }
 
         /*
-        * 如果是组长 则需要排除掉 pitcher = -2 的所有数据
-        * cri.where().andNotEquals("pithcerId", -2);
-        * 如果是管理员账号 则不需要排除 pitcher = -2 的数据
-        * */
+         * 如果是组长 则需要排除掉 pitcher = -2 的所有数据
+         * cri.where().andNotEquals("pithcerId", -2);
+         * 如果是管理员账号 则不需要排除 pitcher = -2 的数据
+         * */
 
         //根据参数拼接条件
         switch (queryDays) {
@@ -197,14 +195,13 @@ public class AdsOverallSummaryServiceImpl implements IAdsOverallSummaryService {
 
         //折线数据需要拼接时间查询条件
         if (needLocalDate) {
-            if (param.getBeginDate() != null && param.getEndDate() != null) {
-                //如果是折线则需要给定时间内的数据
-                cri.where().andBetween("dt", param.getBeginDate(), param.getEndDate());
-                //日期排序
-            } else {
-                //没有具体时间默认查询至今30日的数据
-                cri.where().andBetween("dt", LocalDate.now().minusDays(30), LocalDate.now());
+            //如果传入时间区间参数为空 设置默认值
+            if (param.getBeginDate() == null || param.getEndDate() == null) {
+                param.setBeginDate(LocalDate.now().minusDays(30));
+                param.setEndDate(LocalDate.now());
             }
+            //添加日期条件
+            cri.where().andBetween("dt", param.getBeginDate(), param.getEndDate());
             //按日期分组
             cri.getGroupBy().groupBy("dt");
             //按日期排序 升序
@@ -220,7 +217,7 @@ public class AdsOverallSummaryServiceImpl implements IAdsOverallSummaryService {
      * @param source 原数据
      * @param target 目标数据
      */
-    public static void copyNullProperties(Object source, Object target) {
+    private void copyNullProperties(Object source, Object target) {
         BeanUtils.copyProperties(source, target, getNullField(source));
     }
 
@@ -247,5 +244,50 @@ public class AdsOverallSummaryServiceImpl implements IAdsOverallSummaryService {
         return notNullFieldSet.toArray(notNullField);
     }
 
+    /**
+     * 检查前端查询日期内的数据是否在数据库中都存在
+     * 不存在要设置默认值 0
+     *
+     * @param list  返回给前端的折线图数据 list
+     * @param param 前端传递的查询参数实体
+     */
+    private List<AdsOverallSummaryLineDataVO> checkAllDateData(List<AdsOverallSummaryLineDataVO> list, AdsOverallSummaryParam param) {
+
+        //创建一个Map用来存储日期数据
+        Map<LocalDate, Integer> map = new HashMap<>();
+        //添加日期数据
+        LocalDate tempDate = param.getBeginDate();
+        while (!tempDate.isAfter(param.getEndDate())) {
+            map.put(tempDate, 1);
+            tempDate = tempDate.plusDays(1);
+        }
+        //循环遍历list 查询vo里的日期参数
+        for (AdsOverallSummaryLineDataVO vo : list) {
+            Integer value = map.get(vo.getDt());
+            if (value != null) {
+                map.put(vo.getDt(), ++value);
+            }
+        }
+        //遍历map数据 value为 1 的表示 list中没有该日期的实体
+        for (Map.Entry<LocalDate, Integer> entry : map.entrySet()) {
+            if (entry.getValue() == 1) {
+                //list数据添加一个默认VO对象
+                list.add(AdsOverallSummaryLineDataVO.builder()
+                        .dt(entry.getKey())
+                        .amount(BigDecimal.ZERO)
+                        .buyAmount(BigDecimal.ZERO)
+                        .natureAmount(BigDecimal.ZERO)
+                        .cost(BigDecimal.ZERO)
+                        .firstRoi(BigDecimal.ZERO)
+                        .playerAccount(BigInteger.ZERO)
+                        .build());
+            }
+        }
+        //排序 lambda表达式 按日期升序排序
+        list.sort((vo1, vo2) -> vo1.getDt().isBefore(vo2.getDt()) ? -1 : 1);
+
+        return list;
+    }
+
 
 }