Просмотр исходного кода

优化看课小程序统计查询,每天凌晨定时去查询数据落表

luolinsong 2 недель назад
Родитель
Сommit
fce4f13e61

+ 6 - 2
fs-admin/src/main/java/com/fs/course/controller/FsCourseWatchLogController.java

@@ -10,10 +10,12 @@ import com.fs.common.constant.HttpStatus;
 import com.fs.common.core.domain.R;
 import com.fs.common.exception.CustomException;
 import com.fs.common.utils.ServletUtils;
+import com.fs.course.domain.StatisticsTable;
 import com.fs.course.param.FsCourseOverParam;
 import com.fs.course.param.FsCourseWatchLogListParam;
 import com.fs.course.param.FsCourseWatchLogParam;
 import com.fs.course.param.FsCourseWatchLogStatisticsListParam;
+import com.fs.course.service.IStatisticsTableService;
 import com.fs.course.vo.FsCoureseWatchLogVO;
 import com.fs.course.vo.FsCourseOverVO;
 import com.fs.course.vo.FsCourseWatchLogListVO;
@@ -56,6 +58,8 @@ public class FsCourseWatchLogController extends BaseController
 
     @Autowired
     private IQwWatchLogService qwWatchLogService;
+    @Autowired
+    private IStatisticsTableService iStatisticsTableService;
     /**
      * 查询短链课程看课记录列表
      */
@@ -238,8 +242,8 @@ public class FsCourseWatchLogController extends BaseController
     @GetMapping("/getAppIdList")
     public TableDataInfo getAppIdList( FsCourseWatchLogParam fsCourseWatchLog) {
         startPage();
-        List<FsCoureseWatchLogVO> list = fsCourseWatchLogService.selectAppIdList(fsCourseWatchLog);
-        return getDataTable(list);
+        List<StatisticsTable> statisticsTables = iStatisticsTableService.selectAppIdList(fsCourseWatchLog);
+        return getDataTable(statisticsTables);
     }
 
     /**

+ 23 - 0
fs-qw-task/src/main/java/com/fs/app/task/qwTask.java

@@ -1,7 +1,12 @@
 package com.fs.app.task;
 
+import cn.hutool.core.collection.CollectionUtil;
 import com.fs.app.taskService.*;
 import com.fs.common.utils.PubFun;
+import com.fs.course.domain.StatisticsTable;
+import com.fs.course.service.IFsCourseWatchLogService;
+import com.fs.course.service.IStatisticsTableService;
+import com.fs.course.vo.FsCoureseWatchLogVO;
 import com.fs.ipad.IpadSendUtils;
 import com.fs.qw.domain.QwExternalContact;
 import com.fs.qw.domain.QwGroupChat;
@@ -108,6 +113,12 @@ public class qwTask {
     @Autowired
     private SyncQwExternalContactService syncQwExternalContactService;
 
+    @Autowired
+    private IFsCourseWatchLogService iFsCourseWatchLogService;
+    @Autowired
+    private IStatisticsTableService iStatisticsTableService;
+
+
     /**
      * 定时任务:检查SOP规则时间
      * 执行时间:每天凌晨 1:10:00
@@ -339,6 +350,18 @@ public class qwTask {
         sopUserLogsService.repairSopUserLogsTimer();
     }
 
+    /**
+     * 定时任务获取前一天小程序看课统计
+     */
+    @Scheduled(cron = "0 10 0 * * ?")
+    public void getTatol() {
+        List<StatisticsTable> statisticsTables = iFsCourseWatchLogService.selectAppIdList();
+        if (CollectionUtil.isEmpty(statisticsTables)) {
+            log.warn("定时任务获取前一天小程序看课统计");
+            return; // 空集合直接返回,避免无效操作
+        }
+        iStatisticsTableService.saveBatch(statisticsTables, statisticsTables.size());
+    }
 
     /**
      * 凌晨 2点35开始,将营期小于3天中标记为 是否3天未看课的(E级) 客户的 但是看课了的恢复一下

+ 30 - 0
fs-service/src/main/java/com/fs/course/domain/StatisticsTable.java

@@ -0,0 +1,30 @@
+package com.fs.course.domain;
+
+import com.baomidou.mybatisplus.annotation.FieldFill;
+import com.baomidou.mybatisplus.annotation.IdType;
+import com.baomidou.mybatisplus.annotation.TableField;
+import com.baomidou.mybatisplus.annotation.TableId;
+import com.fasterxml.jackson.annotation.JsonFormat;
+import com.fs.common.core.domain.BaseEntity;
+import lombok.Data;
+
+import java.util.Date;
+
+@Data
+public class StatisticsTable {
+    private static final long serialVersionUID = 1L;
+    @TableId(type = IdType.AUTO)
+    private Long id;// id
+    private Long companyId;//公司id
+    private String companyName;//公司名称
+    private String appId;//小程序id
+    private String appName;//小程序名称
+    private Integer appAllNum;//总观看人数
+    /** 更新时间 */
+    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
+    @TableField(fill = FieldFill.INSERT_UPDATE)
+    private Date updateTime;
+    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
+    @TableField(fill = FieldFill.INSERT)
+    private Date createTime;
+}

+ 1 - 1
fs-service/src/main/java/com/fs/course/mapper/FsCourseWatchLogMapper.java

@@ -754,7 +754,7 @@ public interface FsCourseWatchLogMapper extends BaseMapper<FsCourseWatchLog> {
     })
     List<Long> getExContactIdsIdsByWatchLogIds(@Param("watchLogIds")List<Long> watchLogIds);
 
-    List<FsCoureseWatchLogVO> selectAppIdList(FsCourseWatchLogParam fsCourseWatchLog);
+    List<FsCoureseWatchLogVO> selectAppIdList();
     
     List<FsCoureseWatchLogVO> selectUserIdList(FsCourseWatchLogParam fsCourseWatchLog);
 }

+ 13 - 0
fs-service/src/main/java/com/fs/course/mapper/StatisticsTableMapper.java

@@ -0,0 +1,13 @@
+package com.fs.course.mapper;
+
+import com.baomidou.mybatisplus.core.mapper.BaseMapper;
+import com.fs.course.domain.FsCourseWatchLog;
+import com.fs.course.domain.StatisticsTable;
+import com.fs.course.param.FsCourseWatchLogParam;
+
+import java.util.List;
+
+public interface StatisticsTableMapper extends BaseMapper<StatisticsTable> {
+
+    List<StatisticsTable> selectAppIdList(FsCourseWatchLogParam fsCourseWatchLogParam);
+}

+ 3 - 2
fs-service/src/main/java/com/fs/course/service/IFsCourseWatchLogService.java

@@ -2,6 +2,7 @@ package com.fs.course.service;
 
 import com.baomidou.mybatisplus.extension.service.IService;
 import com.fs.course.domain.FsCourseWatchLog;
+import com.fs.course.domain.StatisticsTable;
 import com.fs.course.param.*;
 import com.fs.course.vo.*;
 import com.fs.qw.param.QwSidebarStatsParam;
@@ -154,7 +155,7 @@ public interface IFsCourseWatchLogService extends IService<FsCourseWatchLog> {
      * @return
      */
     List<Long> getExContactIdsIdsByWatchLogIds(List<Long> watchLogIds);
-    
-    List<FsCoureseWatchLogVO> selectAppIdList(FsCourseWatchLogParam fsCourseWatchLog);
+
+    List<StatisticsTable> selectAppIdList();
     List<FsCoureseWatchLogVO> selectUserIdList(FsCourseWatchLogParam fsCourseWatchLog);
 }

+ 10 - 0
fs-service/src/main/java/com/fs/course/service/IStatisticsTableService.java

@@ -0,0 +1,10 @@
+package com.fs.course.service;
+import com.baomidou.mybatisplus.extension.service.IService;
+import com.fs.course.domain.StatisticsTable;
+import com.fs.course.param.FsCourseWatchLogParam;
+
+import java.util.List;
+
+public interface IStatisticsTableService extends IService<StatisticsTable>{
+    List<StatisticsTable>  selectAppIdList(FsCourseWatchLogParam fsCourseWatchLog);
+}

+ 22 - 2
fs-service/src/main/java/com/fs/course/service/impl/FsCourseWatchLogServiceImpl.java

@@ -9,6 +9,7 @@ import com.alibaba.fastjson.JSONArray;
 import com.alibaba.fastjson.JSONObject;
 import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import com.fs.common.BeanCopyUtils;
 import com.fs.common.core.redis.RedisCache;
 import com.fs.common.utils.DateUtils;
 import com.fs.common.utils.DictUtils;
@@ -1343,8 +1344,27 @@ public class FsCourseWatchLogServiceImpl extends ServiceImpl<FsCourseWatchLogMap
     }
 
     @Override
-    public List<FsCoureseWatchLogVO> selectAppIdList(FsCourseWatchLogParam fsCourseWatchLog) {
-       return  fsCourseWatchLogMapper.selectAppIdList(fsCourseWatchLog);
+    public List<StatisticsTable> selectAppIdList() {
+        List<FsCoureseWatchLogVO> fsCoureseWatchLogVOS = fsCourseWatchLogMapper.selectAppIdList();
+        List<StatisticsTable> resultList = new ArrayList<>(fsCoureseWatchLogVOS.size());
+        if (CollectionUtil.isNotEmpty(fsCoureseWatchLogVOS)) {
+
+            fsCoureseWatchLogVOS.stream()
+                    .filter(Objects::nonNull)  // 过滤null值
+                    .forEach(vo -> {
+                        try {
+                            StatisticsTable table = new StatisticsTable();
+                            BeanCopyUtils.copy(vo, table);
+                            table.setCreateTime(new Date());
+                            table.setUpdateTime(new Date());
+                            resultList.add(table);
+                        } catch (Exception e) {
+                            // 记录日志,不中断处理
+                            log.warn("Bean拷贝失败,跳过该记录: {}", vo, e);
+                        }
+                    });
+        }
+        return resultList;
     }
     @Override
     public List<FsCoureseWatchLogVO> selectUserIdList(FsCourseWatchLogParam fsCourseWatchLog) {

+ 31 - 0
fs-service/src/main/java/com/fs/course/service/impl/IStatisticsTableServiceImpl.java

@@ -0,0 +1,31 @@
+package com.fs.course.service.impl;
+
+import com.baomidou.mybatisplus.core.conditions.Wrapper;
+import com.baomidou.mybatisplus.core.mapper.BaseMapper;
+import com.baomidou.mybatisplus.core.metadata.IPage;
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import com.fs.course.domain.StatisticsTable;
+import com.fs.course.mapper.StatisticsTableMapper;
+import com.fs.course.param.FsCourseWatchLogParam;
+import com.fs.course.service.IStatisticsTableService;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+
+import java.io.Serializable;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+import java.util.function.Function;
+@Service
+public class IStatisticsTableServiceImpl extends ServiceImpl<StatisticsTableMapper, StatisticsTable> implements IStatisticsTableService {
+
+    @Autowired
+    private StatisticsTableMapper statisticsTableMapper;
+    @Override
+    public List<StatisticsTable> selectAppIdList(FsCourseWatchLogParam fsCourseWatchLog) {
+        fsCourseWatchLog.setBeginTime(fsCourseWatchLog.getBeginTime() + " 00:00:00");
+        fsCourseWatchLog.setEndTime(fsCourseWatchLog.getEndTime() + " 23:59:59");
+        return statisticsTableMapper.selectAppIdList(fsCourseWatchLog);
+    }
+}

+ 2 - 2
fs-service/src/main/java/com/fs/course/vo/FsCoureseWatchLogVO.java

@@ -5,12 +5,12 @@ import lombok.Data;
 @Data
 public class FsCoureseWatchLogVO {
     
-    private String companyId;
+    private Long companyId;
     private String companyName;
     private String appName;
     private String appId;
     //总人数
-    private String appAllNum;
+    private Integer appAllNum;
     
     
     //个微完播人数

+ 5 - 17
fs-service/src/main/resources/mapper/course/FsCourseWatchLogMapper.xml

@@ -1072,8 +1072,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
             log_id DESC
     </select>
 
-    <select id="selectAppIdList" resultType="com.fs.course.vo.FsCoureseWatchLogVO"
-            parameterType="com.fs.course.param.FsCourseWatchLogParam">
+    <select id="selectAppIdList" resultType="com.fs.course.vo.FsCoureseWatchLogVO">
         SELECT
         c.company_id ,
         c.company_name,
@@ -1082,22 +1081,11 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
         count( l.app_id ) appAllNum
         FROM
         fs_course_watch_log l
-        INNER JOIN fs_course_play_source_config f ON l.app_id = f.appid
-        INNER JOIN company c ON l.company_id = c.company_id
+        INNER JOIN fs_course_play_source_config f ON l.app_id = f.appid and f.is_del ='0'
+        INNER JOIN company c ON l.company_id = c.company_id  and c.is_del ='0'
         WHERE
-        f.is_del ='0' and c.is_del ='0'
-        <if test="createTime != null">
-            AND DATE(l.create_time) = #{createTime}
-        </if>
-        <if test="companyId != null">
-            AND l.company_id = #{companyId}
-        </if>
-        <if test="appId != null">
-            AND l.app_id = #{appId}
-        </if>
-        <if test="companyName != null">
-            AND c.company_name like concat('%', #{companyName}, '%')
-        </if>
+        l.create_time &gt;= DATE_SUB(CURDATE(), INTERVAL 1 DAY)
+        AND l.create_time &lt; CURDATE()
         GROUP BY
         l.company_id,
         l.app_id

+ 39 - 0
fs-service/src/main/resources/mapper/course/StatisticsTableMapper.xml

@@ -0,0 +1,39 @@
+<?xml version="1.0" encoding="UTF-8" ?>
+<!DOCTYPE mapper
+PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
+"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
+<mapper namespace="com.fs.course.mapper.StatisticsTableMapper">
+    <resultMap type="StatisticsTable" id="StatisticsTableMapper">
+        <result property="companyId"    column="company_id"    />
+        <result property="companyName"    column="company_name"    />
+        <result property="appId"    column="app_id"    />
+        <result property="appName"    column="app_name"    />
+        <result property="appAllNum"    column="app_all_num"    />
+        <result property="createTime"    column="create_time"    />
+        <result property="updateTime"    column="update_time"    />
+    </resultMap>
+
+    <select id="selectAppIdList" resultType="com.fs.course.domain.StatisticsTable"
+            parameterType="com.fs.course.param.FsCourseWatchLogParam">
+        select company_id ,
+        company_name,
+        app_id,
+        app_name,
+        sum(app_all_num) appAllNum
+        FROM statistics_table
+        <if test="beginTime != null and endTime != null">
+            where create_time between #{beginTime} and #{endTime}
+        </if>
+        <if test="companyId != null ">
+            and company_id = #{companyId}
+        </if>
+        <if test="appId != null ">
+            and app_id = #{appId}
+        </if>
+        GROUP BY
+        company_id,
+        app_id
+        ORDER BY
+        company_id
+    </select>
+</mapper>