Browse Source

1.优化语句

jzp 3 days ago
parent
commit
f200a99069

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

@@ -222,6 +222,10 @@ public interface FsCourseWatchLogMapper extends BaseMapper<FsCourseWatchLog> {
             "AND finish_time < CURRENT_DATE + INTERVAL 1 DAY")
     List<FsCourseWatchLog> selectFsCourseWatchLogFinish();
 
+    @DataSource(DataSourceType.SLAVE)
+    List<FsCourseWatchLogStatisticsListVO> selectFsCourseWatchLogStatisticsListVO_COUNT(FsCourseWatchLogStatisticsListParam param);
+
+
     @Select({"<script> " +
             "SELECT \n" +
             "o.video_id,o.company_id,o.qw_user_id,DATE(o.create_time) create_time," +

+ 92 - 0
fs-service/src/main/resources/mapper/course/FsCourseWatchLogMapper.xml

@@ -962,5 +962,97 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
         inner join fs_course_watch_log cwl on ucp.period_id = cwl.period_id
         where ucp.training_camp_id = #{trainingCampId}
     </select>
+    <select id="selectFsCourseWatchLogStatisticsListVO_COUNT"
+            resultType="java.lang.Long">
+        SELECT COUNT(*) FROM (
+        SELECT
+        o.video_id,
+        <!-- 用choose保证sendType逻辑互斥,避免GROUP BY字段缺失 -->
+        <choose>
+            <when test="sendType != 1">
+                o.qw_user_id,
+            </when>
+            <when test="sendType == 1">
+                o.company_user_id,
+            </when>
+            <!-- sendType为null时兜底,避免空指针 -->
+            <otherwise>
+                o.qw_user_id,
+            </otherwise>
+        </choose>
+        DATE(o.create_time) create_time
+        FROM fs_course_watch_log o
+        <!-- 动态关联qw_user表 -->
+        <if test="sendType != 1">
+            LEFT JOIN qw_user qu ON qu.id = o.qw_user_id
+        </if>
+        LEFT JOIN fs_user_course_video v ON v.video_id = o.video_id
+        LEFT JOIN fs_user_course uc ON uc.course_id = v.course_id
+        <!-- 动态关联company_user表 -->
+        <if test="sendType == 1">
+            LEFT JOIN company_user cu ON cu.user_id = o.company_user_id
+        </if>
+        WHERE o.company_id = #{companyId}
+        <!-- 发送类型筛选 -->
+        <if test="sendType != null">
+            AND send_type = #{sendType}
+        </if>
+        <!-- 开始时间筛选:区分String/Date类型,避免类型比较异常 -->
+        <if test="sTime != null">
+            <choose>
+                <!-- sTime是String类型(yyyy-MM-dd) -->
+                <when test="sTime instanceof java.lang.String and sTime.trim() != ''">
+                    AND o.create_time &gt;= STR_TO_DATE(#{sTime}, '%Y-%m-%d')
+                </when>
+                <!-- sTime是Date类型 -->
+                <otherwise>
+                    AND o.create_time &gt;= #{sTime}
+                </otherwise>
+            </choose>
+        </if>
+        <!-- 结束时间筛选:区分String/Date类型 -->
+        <if test="eTime != null">
+            <choose>
+                <when test="eTime instanceof java.lang.String and eTime.trim() != ''">
+                    AND o.create_time &lt; DATE_ADD(STR_TO_DATE(#{eTime}, '%Y-%m-%d'), INTERVAL 1 DAY)
+                </when>
+                <otherwise>
+                    AND o.create_time &lt; DATE_ADD(#{eTime}, INTERVAL 1 DAY)
+                </otherwise>
+            </choose>
+        </if>
+        <!-- 昵称筛选:sendType!=1时关联qw_user -->
+        <if test="sendType != 1 and nickName != null and nickName.trim() != ''">
+            AND qu.qw_user_name LIKE CONCAT(#{nickName}, '%')
+        </if>
+        <!-- 昵称筛选:sendType==1时关联company_user -->
+        <if test="sendType == 1 and nickName != null and nickName.trim() != ''">
+            AND cu.nick_name LIKE CONCAT(#{nickName}, '%')
+        </if>
+        <!-- 课程ID筛选:加>0判断,避免null值拼接 -->
+        <if test="courseId != null and courseId > 0">
+            AND o.course_id = #{courseId}
+        </if>
+        <!-- 视频ID筛选:加>0判断 -->
+        <if test="videoId != null and videoId > 0">
+            AND o.video_id = #{videoId}
+        </if>
+        <!-- 分组条件:与子查询字段一致 -->
+        GROUP BY
+        o.video_id,
+        <choose>
+            <when test="sendType != 1">
+                o.qw_user_id,
+            </when>
+            <when test="sendType == 1">
+                o.company_user_id,
+            </when>
+            <otherwise>
+                o.qw_user_id,
+            </otherwise>
+        </choose>
+        DATE(o.create_time)
+        ) AS t
+    </select>
 
 </mapper>