Forráskód Böngészése

修复用户统计 bug

tianyunperfect 3 éve
szülő
commit
84c6cad0b6

+ 4 - 4
book-dao/src/main/resources/mapper/polar/SunUser2Mapper.xml

@@ -3,25 +3,25 @@
 <mapper namespace="com.book.dao.polardb.mapper.SunUser2Mapper">
   <select id="selectByDay" resultType="java.util.HashMap">
     <![CDATA[
-    SELECT COUNT(DISTINCT(openid)) as new_recharge_user,IFNULL(sum(recharge_amount),0) as new_recharge_sum,count(recharge_num) as new_recharge_count FROM sun_user WHERE start_time>#{startDate} and end_time<=#{endDate} and recharge_num>0;
+    SELECT COUNT(DISTINCT(openid)) as new_recharge_user,IFNULL(sum(recharge_amount),0) as new_recharge_sum,count(recharge_num) as new_recharge_count FROM sun_user WHERE start_time>=#{startDate} and end_time < #{endDate} and recharge_num>0;
     ]]>
 </select>
   <select id="selectByDayChannel" resultType="java.util.HashMap">
     <![CDATA[
     SELECT t.*,t2.channel_name FROM
       (
-        SELECT channel_id,COUNT(DISTINCT(openid)) as new_recharge_user,IFNULL(sum(recharge_amount),0) as new_recharge_sum,count(recharge_num) as new_recharge_count  FROM sun_user WHERE start_time>#{startDate} and end_time<=#{endDate} and recharge_num>0 GROUP BY channel_id
+        SELECT channel_id,COUNT(DISTINCT(openid)) as new_recharge_user,IFNULL(sum(recharge_amount),0) as new_recharge_sum,count(recharge_num) as new_recharge_count  FROM sun_user WHERE start_time>=#{startDate} and end_time<#{endDate} and recharge_num>0 GROUP BY channel_id
       ) t LEFT JOIN sun_channel t2 on t.channel_id=t2.channel_id;
     ]]>
   </select>
   <select id="selectCountUserByDay" resultType="java.lang.Integer">
     <![CDATA[
-    SELECT COUNT(1) FROM sun_user WHERE start_time > #{startDate} and end_time <= #{endDate};
+    SELECT COUNT(1) FROM sun_user WHERE start_time >= #{startDate} and end_time < #{endDate};
     ]]>
   </select>
     <select id="selectCountByDayChannel" resultType="java.util.HashMap">
       <![CDATA[
-    SELECT channel_id,COUNT(DISTINCT(openid)) as user_count FROM sun_user WHERE start_time>#{startDate} and end_time<=#{endDate} GROUP BY channel_id
+    SELECT channel_id,COUNT(DISTINCT(openid)) as user_count FROM sun_user WHERE start_time>=#{startDate} and end_time<#{endDate} GROUP BY channel_id
       ]]>
     </select>
 </mapper>

+ 2 - 2
book-dao/src/main/resources/mapper/polar/SunUserActive2Mapper.xml

@@ -7,7 +7,7 @@
   </select>
   <select id="selectRechargeByDate" resultType="java.util.HashMap">
     <![CDATA[
-    SELECT COUNT(DISTINCT(t.openid)) as recharge_user_count,COUNT(1) as recharge_count, SUM(t.money) as recharge_sum FROM sun_order t WHERE t.create_time > #{startDate} and t.create_time <= #{endDate} and t.state='完成';
+    SELECT COUNT(DISTINCT(t.openid)) as recharge_user_count,COUNT(1) as recharge_count, SUM(t.money) as recharge_sum FROM sun_order t WHERE t.create_time >= #{startDate} and t.create_time < #{endDate} and t.state='完成';
     ]]>
 </select>
   <select id="selectRechargeByDateGroupByChannel" resultType="java.util.HashMap">
@@ -15,7 +15,7 @@
     SELECT t1.*,t2.channel_name FROM
       (
         SELECT t.channel_id, COUNT(DISTINCT (t.openid)) AS recharge_user_count,COUNT(1) AS recharge_count,SUM(t.money) AS recharge_sum FROM sun_order t
-        WHERE t.create_time> #{startDate} AND t.create_time<=#{endDate} AND t.state='完成'
+        WHERE t.create_time>= #{startDate} AND t.create_time<#{endDate} AND t.state='完成'
         GROUP BY
           t.channel_id
       ) t1 left JOIN sun_channel t2 on t2.channel_id=t1.channel_id;

+ 11 - 0
book-server/src/main/java/com/book/server/service/CacheService.java

@@ -10,6 +10,8 @@ import com.book.dao.cps.pojo.example.BookCategoryExample;
 import com.book.dao.cps.pojo.example.ManageBlockExample;
 import com.book.dao.cps.pojo.example.SearchKeywordExample;
 import com.book.dao.cps.pojo.example.UploadBookExample;
+import com.book.dao.polardb.mapper.SunChannelMapper;
+import com.book.dao.polardb.pojo.example.SunChannelExample;
 import com.book.server.annotation.CacheFind;
 import com.book.server.config.Const;
 import com.book.server.service.impl.BookServiceImpl;
@@ -285,6 +287,7 @@ public class CacheService {
     public BookContent getBookContentCache(Long bookId, Long chapterId) {
         return bookService.getBookContent(bookId, chapterId);
     }
+
     @CacheFind(key = "guide_chapter")
     public Integer getBook_guide_chapter_idx(String appId, Integer bookId) {
         return bookService.getBook_guide_chapter_idx(appId, bookId);
@@ -294,4 +297,12 @@ public class CacheService {
     public String getQrcodeUrl(String appId) {
         return bookService.getQrcodeUrl(appId);
     }
+
+    @Autowired
+    private SunChannelMapper sunChannelMapper;
+
+    @CacheFind(key = "channelNameById", seconds = 60 * 60 * 24 * 365)
+    public String getChannelName(String channelId) {
+        return sunChannelMapper.selectOneByExample(SunChannelExample.newAndCreateCriteria().andChannelIdEqualTo(channelId).example()).getChannelName();
+    }
 }

+ 52 - 30
book-server/src/main/java/com/book/server/service/impl/SunDataServiceImpl.java

@@ -9,6 +9,7 @@ import com.book.server.common.entity.PageResult;
 import com.book.server.common.entity.Result;
 import com.book.server.common.util.JsonUtils;
 import com.book.server.config.SunTypeEnum;
+import com.book.server.service.CacheService;
 import com.book.server.service.SunDataService;
 import com.book.server.utils.DataUtil;
 import com.book.server.utils.HttpTool;
@@ -460,7 +461,7 @@ public class SunDataServiceImpl implements SunDataService {
 
     @Override
     public List<SunStaticUserDayChannel> getTodayUserStaticByChannel() {
-        return getSunUserByDateChannels(TimeUtil.getYesterdayStr(),TimeUtil.getTodayStr());
+        return getSunUserByDateChannels(TimeUtil.getTodayStr(),TimeUtil.getTomorrowStr());
     }
 
     @Override
@@ -528,43 +529,64 @@ public class SunDataServiceImpl implements SunDataService {
     }
 
     private List<SunStaticUserDayChannel> getSunUserByDateChannels(String startDate,String endDate) {
-        HashMap<String, Integer> activeMap = new HashMap<>();
         List<HashMap<String, String>> list = sunUser2Mapper.selectCountByDayChannel(startDate,endDate);
-        for (HashMap<String, String> hashMap : list) {
-            activeMap.put(hashMap.get("channel_id"), getIntFromDb(hashMap.get("user_count")));
-        }
-
         List<HashMap<String, String>> recharges = sunUser2Mapper.selectByDayChannel(startDate, endDate);
         ArrayList<SunStaticUserDayChannel> sunStaticDayChannels = new ArrayList<>();
-        for (HashMap<String, String> recharge : recharges) {
-            int new_recharge_user = getIntFromDb(recharge.get("new_recharge_user"));
-            float new_recharge_sum = getFloatFromDb(recharge.get("new_recharge_sum"));
-            int i = activeMap.get(recharge.get("channel_id"));
-            float avg = 0;
-            //付费率
-            float rate = 0;
-            if (new_recharge_sum > 0) {
-                avg = new_recharge_sum / new_recharge_user;
-                rate = (float)new_recharge_user / i;
+
+        for (HashMap<String, String> hashMap : list) {
+            String channel_id = hashMap.get("channel_id");
+            int user_count = getIntFromDb(hashMap.get("user_count"));
+            Optional<HashMap<String, String>> optionalHashMap = recharges.stream().filter(x -> x.get("channel_id").equals(channel_id)).findFirst();
+
+            if (optionalHashMap.isPresent()) {
+                HashMap<String, String> recharge = recharges.stream().filter(x -> x.get("channel_id").equals(channel_id)).findFirst().get();
+
+                int new_recharge_user = getIntFromDb(recharge.get("new_recharge_user"));
+                float new_recharge_sum = getFloatFromDb(recharge.get("new_recharge_sum"));
+                float avg = 0;
+                //付费率
+                float rate = 0;
+                if (new_recharge_sum > 0) {
+                    avg = new_recharge_sum / new_recharge_user;
+                    rate = (float) new_recharge_user / user_count;
+                }
+                SunStaticUserDayChannel day = SunStaticUserDayChannel.builder()
+                        .staticDate(startDate)
+                        .channelId(recharge.get("channel_id"))
+                        .channelName(recharge.get("channel_name"))
+                        .newAllUser(user_count)
+                        .newRechargeUser(new_recharge_user)
+                        .newRechargeSum(String.format("%.2f", new_recharge_sum))
+                        .newRechargeCount(getIntFromDb(recharge.get("new_recharge_count")))
+                        .rechargeAvg(String.format("%.2f", avg))
+                        .rechargeRate(String.format("%.2f", rate))
+                        .insertTime(TimeUtil.getCurrentTimestamp())
+                        .price("0") // TODO
+                        .build();
+                sunStaticDayChannels.add(day);
+            } else {
+                SunStaticUserDayChannel day = SunStaticUserDayChannel.builder()
+                        .staticDate(startDate)
+                        .channelId(channel_id)
+                        .channelName(cacheService.getChannelName(channel_id))
+                        .newAllUser(user_count)
+                        .newRechargeUser(0)
+                        .newRechargeSum("0")
+                        .newRechargeCount(0)
+                        .rechargeAvg("0")
+                        .rechargeRate("0")
+                        .insertTime(TimeUtil.getCurrentTimestamp())
+                        .price("0") // TODO
+                        .build();
+                sunStaticDayChannels.add(day);
             }
-            SunStaticUserDayChannel day = SunStaticUserDayChannel.builder()
-                    .staticDate(startDate)
-                    .channelId(recharge.get("channel_id"))
-                    .channelName(recharge.get("channel_name"))
-                    .newAllUser(i)
-                    .newRechargeUser(new_recharge_user)
-                    .newRechargeSum(String.format("%.2f", new_recharge_sum))
-                    .newRechargeCount(getIntFromDb(recharge.get("new_recharge_count")))
-                    .rechargeAvg(String.format("%.2f", avg))
-                    .rechargeRate(String.format("%.2f", rate))
-                    .insertTime(TimeUtil.getCurrentTimestamp())
-                    .price("0") // TODO
-                    .build();
-            sunStaticDayChannels.add(day);
         }
         return sunStaticDayChannels;
     }
 
+    @Autowired
+    private CacheService cacheService;
+
     private void handleOrder(List<String> list, Integer taskId) {
         SunRequest request = taskMap.get(taskId);
         List<SunOrder> result = new ArrayList<>();