浏览代码

修改内容:简化编写代码修改计算Roi方法

lth 1 年之前
父节点
当前提交
f3d527abe4

+ 48 - 49
game-data/game-data-serve/src/main/java/com/zanxiang/game/data/serve/service/impl/AdsOverallSummaryServiceImpl.java

@@ -46,14 +46,16 @@ public class AdsOverallSummaryServiceImpl implements IAdsOverallSummaryService {
         //查询条件转换为sql语句 设置映射 Entity为 AdsOverallSummary对象
         //查询条件转换为sql语句 设置映射 Entity为 AdsOverallSummary对象
         String totalDataConString = totalDataCon.toSql(dao.getEntity(AdsOverallSummary.class));
         String totalDataConString = totalDataCon.toSql(dao.getEntity(AdsOverallSummary.class));
         //自定义sql语句
         //自定义sql语句
-        Sql totalDataSql = Sqls.create("SELECT \n" +
-                "IFNULL(SUM(x.today_player_count),0) total_players ,\n" +
-                "IFNULL(SUM(x.today_total_cost),0) total_cost ,\n" +
-                "IFNULL(SUM(x.today_amount),0) total_amount ,\n" +
-                "IFNULL(SUM(x.today_agent_count),0) total_agent_count ,\n" +
-                "IFNULL(SUM(x.today_game_count),0) total_game_count ,\n" +
-                "IFNULL(SUM(x.today_new_player_amount)/SUM(x.today_total_cost),0) total_roi \n" +
-                "FROM ads_overall_summary x " + totalDataConString);
+        Sql totalDataSql = Sqls.create("""
+                SELECT
+                IFNULL(SUM(x.today_player_count),0) total_players ,
+                IFNULL(SUM(x.today_total_cost),0) total_cost ,
+                IFNULL(SUM(x.today_amount),0) total_amount ,
+                IFNULL(SUM(x.today_agent_count),0) total_agent_count ,
+                IFNULL(SUM(x.today_game_count),0) total_game_count ,
+                IFNULL(SUM(x.today_new_player_amount)/SUM(x.today_total_cost),0) total_roi
+                FROM ads_overall_summary x
+                """ + totalDataConString);
 
 
         //自定义回显对象 结果自动封装到给定的Entity对象中
         //自定义回显对象 结果自动封装到给定的Entity对象中
         totalDataSql.setCallback(Sqls.callback.entity());
         totalDataSql.setCallback(Sqls.callback.entity());
@@ -67,21 +69,23 @@ public class AdsOverallSummaryServiceImpl implements IAdsOverallSummaryService {
         Condition todayDataCon = myCondition(param, false, 1);
         Condition todayDataCon = myCondition(param, false, 1);
         //查询条件转换为sql语句 设置映射 Entity为 AdsOverallSummary对象
         //查询条件转换为sql语句 设置映射 Entity为 AdsOverallSummary对象
         String todayDataConString = todayDataCon.toSql(dao.getEntity(AdsOverallSummary.class));
         String todayDataConString = todayDataCon.toSql(dao.getEntity(AdsOverallSummary.class));
-        Sql todayDataSql = Sqls.create("SELECT  \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);
+        Sql todayDataSql = Sqls.create("""
+                SELECT
+                IFNULL(SUM(x.today_player_count),0) today_player_count ,
+                IFNULL(SUM(x.yesterday_player_count),0) yesterday_player_count ,
+                IFNULL(SUM(x.today_total_cost),0) today_total_cost ,
+                IFNULL(SUM(x.yesterday_total_cost),0) yesterday_total_cost ,
+                IFNULL(SUM(x.today_new_player_amount),0) today_new_player_amount ,
+                IFNULL(SUM(x.today_amount),0) today_amount ,
+                IFNULL(SUM(x.yesterday_new_player_amount),0) yesterday_new_player_amount ,
+                IFNULL(SUM(x.yesterday_amount),0) yesterday_amount ,
+                IFNULL(SUM(x.today_agent_count),0) today_agent_count ,
+                IFNULL(SUM(x.yesterday_agent_count),0) yesterday_agent_count ,
+                IFNULL(SUM(x.today_game_count),0) today_game_count ,
+                IFNULL(SUM(x.yesterday_game_count),0) yesterday_game_count ,
+                IFNULL(SUM(x.first_new_user_amount)/SUM(x.today_total_cost),0) first_roi
+                FROM ads_overall_summary x
+                """ + todayDataConString);
 
 
         //自定义回显对象 结果自动封装到给定的Entity对象中
         //自定义回显对象 结果自动封装到给定的Entity对象中
         todayDataSql.setCallback(Sqls.callback.entity());
         todayDataSql.setCallback(Sqls.callback.entity());
@@ -99,15 +103,16 @@ public class AdsOverallSummaryServiceImpl implements IAdsOverallSummaryService {
         BigDecimal d7NewPlayerAmount = (BigDecimal) dao.func2(AdsOverallSummary.class, "sum", "todayNewPlayerAmount", myCondition(param, false, 7));
         BigDecimal d7NewPlayerAmount = (BigDecimal) dao.func2(AdsOverallSummary.class, "sum", "todayNewPlayerAmount", myCondition(param, false, 7));
         //7日内总消耗
         //7日内总消耗
         BigDecimal d7TotalCost = (BigDecimal) dao.func2(AdsOverallSummary.class, "sum", "todayTotalCost", myCondition(param, false, 7));
         BigDecimal d7TotalCost = (BigDecimal) dao.func2(AdsOverallSummary.class, "sum", "todayTotalCost", myCondition(param, false, 7));
-        //计算7日Roi
-        adsOverallSummaryVO.setD7TotalRoi((BigDecimal.ZERO.compareTo(d7TotalCost) == 0) ? BigDecimal.ZERO : d7NewPlayerAmount.divide(d7TotalCost, 4, RoundingMode.HALF_UP));
+        //cost 查询出的结果为null或者为0.00 设置为默认值0
+        adsOverallSummaryVO.setD7TotalRoi((d7TotalCost == null || BigDecimal.ZERO.compareTo(d7TotalCost) == 0) ? BigDecimal.ZERO : d7NewPlayerAmount.divide(d7TotalCost, 4, RoundingMode.HALF_UP));
 
 
         //30日内新用户累计充值
         //30日内新用户累计充值
         BigDecimal d30NewPlayerAmount = (BigDecimal) dao.func2(AdsOverallSummary.class, "sum", "todayNewPlayerAmount", myCondition(param, false, 30));
         BigDecimal d30NewPlayerAmount = (BigDecimal) dao.func2(AdsOverallSummary.class, "sum", "todayNewPlayerAmount", myCondition(param, false, 30));
         //30日内总消耗
         //30日内总消耗
         BigDecimal d30TotalCost = (BigDecimal) dao.func2(AdsOverallSummary.class, "sum", "todayTotalCost", myCondition(param, false, 30));
         BigDecimal d30TotalCost = (BigDecimal) dao.func2(AdsOverallSummary.class, "sum", "todayTotalCost", myCondition(param, false, 30));
         //计算30日Roi
         //计算30日Roi
-        adsOverallSummaryVO.setD30TotalRoi((BigDecimal.ZERO.compareTo(d30TotalCost) == 0) ? BigDecimal.ZERO : d30NewPlayerAmount.divide(d30TotalCost, 4, RoundingMode.HALF_UP));
+        //cost 查询出的结果为null或者为0.00 设置为默认值0
+        adsOverallSummaryVO.setD30TotalRoi((d30TotalCost == null || BigDecimal.ZERO.compareTo(d30TotalCost) == 0) ? BigDecimal.ZERO : d30NewPlayerAmount.divide(d30TotalCost, 4, RoundingMode.HALF_UP));
 
 
         return adsOverallSummaryVO;
         return adsOverallSummaryVO;
     }
     }
@@ -119,16 +124,18 @@ public class AdsOverallSummaryServiceImpl implements IAdsOverallSummaryService {
         Condition lineCon = myCondition(param, true, -1);
         Condition lineCon = myCondition(param, true, -1);
         //查询条件变为字符串
         //查询条件变为字符串
         String lineConString = lineCon.toSql(dao.getEntity(AdsOverallSummary.class));
         String lineConString = lineCon.toSql(dao.getEntity(AdsOverallSummary.class));
-
         //自定义sql语句 加上条件字符串
         //自定义sql语句 加上条件字符串
-        Sql sql = Sqls.create("Select dt," +
-                "SUM(x.today_total_cost) cost, " +
-                "SUM(x.today_amount) amount, " +
-                "SUM(x.today_buy_amount) buy_amount," +
-                "SUM(x.today_nature_amount) nature_amount, " +
-                "IFNULL(SUM(x.first_new_user_amount)/SUM(x.today_total_cost),0) first_roi," +
-                "SUM(x.today_player_count) player_account " +
-                "from ads_overall_summary x " + lineConString);
+        Sql sql = Sqls.create("""
+                Select
+                dt,
+                SUM(x.today_total_cost) cost,
+                SUM(x.today_amount) amount,
+                SUM(x.today_buy_amount) buy_amount,
+                SUM(x.today_nature_amount) nature_amount,
+                IFNULL(SUM(x.first_new_user_amount)/SUM(x.today_total_cost),0) first_roi,
+                SUM(x.today_player_count) player_account
+                from ads_overall_summary x
+                """ + lineConString);
 
 
         //自定义回显对象 结果自动添加到一个List中 List<Entity>
         //自定义回显对象 结果自动添加到一个List中 List<Entity>
         sql.setCallback(Sqls.callback.entities());
         sql.setCallback(Sqls.callback.entities());
@@ -178,20 +185,11 @@ public class AdsOverallSummaryServiceImpl implements IAdsOverallSummaryService {
 
 
         //根据参数拼接条件
         //根据参数拼接条件
         switch (queryDays) {
         switch (queryDays) {
-            case 1:
-                cri.where().andEquals("dt", LocalDate.now());
-                break;
-
-            case 7:
-                cri.where().andBetween("dt", LocalDate.now().minusDays(7), LocalDate.now());
-                break;
-
-            case 30:
-                cri.where().andBetween("dt", LocalDate.now().minusDays(30), LocalDate.now());
-                break;
-
-            default:
-                break;
+            case 1 -> cri.where().andEquals("dt", LocalDate.now());
+            case 7 -> cri.where().andBetween("dt", LocalDate.now().minusDays(7), LocalDate.now());
+            case 30 -> cri.where().andBetween("dt", LocalDate.now().minusDays(30), LocalDate.now());
+            default -> {
+            }
         }
         }
 
 
         //折线数据需要拼接时间查询条件
         //折线数据需要拼接时间查询条件
@@ -242,6 +240,7 @@ public class AdsOverallSummaryServiceImpl implements IAdsOverallSummaryService {
             }
             }
         }
         }
         String[] notNullField = new String[notNullFieldSet.size()];
         String[] notNullField = new String[notNullFieldSet.size()];
+
         return notNullFieldSet.toArray(notNullField);
         return notNullFieldSet.toArray(notNullField);
     }
     }