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

1、优化红包记录调用慢问题

yys 2 hete
szülő
commit
23837aab75

+ 13 - 28
fs-admin/src/main/java/com/fs/course/controller/FsCourseRedPacketLogController.java

@@ -75,17 +75,11 @@ public class FsCourseRedPacketLogController extends BaseController
         if (fsCourseRedPacketLog.getPhoneMk() != null && fsCourseRedPacketLog.getPhoneMk() != "") {
             fsCourseRedPacketLog.setPhone(encryptPhone(fsCourseRedPacketLog.getPhoneMk()));
         }
-        PageHelper.startPage(fsCourseRedPacketLog.getPageNum(), fsCourseRedPacketLog.getPageSize());
+//        PageHelper.startPage(fsCourseRedPacketLog.getPageNum(), fsCourseRedPacketLog.getPageSize());
 
-        List<FsCourseRedPacketLogListPVO> list = fsCourseRedPacketLogService.selectFsCourseRedPacketLogListVO(fsCourseRedPacketLog);
-        for (FsCourseRedPacketLogListPVO fsCourseRedPacketLogListPVO : list) {
-            if (ObjectUtil.isNotEmpty(fsCourseRedPacketLogListPVO.getPeriodId())){
-                FsUserCoursePeriod fsUserCoursePeriod = fsUserCoursePeriodService.selectFsUserCoursePeriodById(fsCourseRedPacketLogListPVO.getPeriodId().longValue());
-                fsCourseRedPacketLogListPVO.setPeriodName(fsUserCoursePeriod.getPeriodName());
-            }
-            fsCourseRedPacketLogListPVO.setPhone(PhoneUtil.decryptAutoPhoneMk(fsCourseRedPacketLogListPVO.getPhone()));
-        }
-        return R.ok().put("data",new PageInfo<>(list));
+        PageInfo<FsCourseRedPacketLogListPVO> pageInfo = fsCourseRedPacketLogService.selectFsCourseRedPacketLogListVOPage(fsCourseRedPacketLog);
+
+        return R.ok().put("data", pageInfo);
     }
 
     /**
@@ -101,18 +95,9 @@ public class FsCourseRedPacketLogController extends BaseController
             fsCourseRedPacketLog.setPhone(encryptPhone(fsCourseRedPacketLog.getPhoneMk()));
         }
 
-        PageHelper.startPage(fsCourseRedPacketLog.getPageNum(), fsCourseRedPacketLog.getPageSize());
-
-        List<FsCourseRedPacketLogListPVO> list = fsCourseRedPacketLogService.selectFsCourseRedPacketLogListVO(fsCourseRedPacketLog);
-        for (FsCourseRedPacketLogListPVO fsCourseRedPacketLogListPVO : list) {
-            if (ObjectUtil.isNotEmpty(fsCourseRedPacketLogListPVO.getPeriodId())){
-                FsUserCoursePeriod fsUserCoursePeriod = fsUserCoursePeriodService.selectFsUserCoursePeriodById(fsCourseRedPacketLogListPVO.getPeriodId().longValue());
-                fsCourseRedPacketLogListPVO.setPeriodName(fsUserCoursePeriod.getPeriodName());
-            }
-            fsCourseRedPacketLogListPVO.setPhone(PhoneUtil.decryptAutoPhoneMk(fsCourseRedPacketLogListPVO.getPhone()));
-        }
+        PageInfo<FsCourseRedPacketLogListPVO> pageInfo = fsCourseRedPacketLogService.selectFsCourseRedPacketLogListVOPage(fsCourseRedPacketLog);
 
-        return R.ok().put("data", new PageInfo<>(list));
+        return R.ok().put("data", pageInfo);
     }
 
     /**
@@ -127,13 +112,13 @@ public class FsCourseRedPacketLogController extends BaseController
             fsCourseRedPacketLog.setPhone(encryptPhone(fsCourseRedPacketLog.getPhoneMk()));
         }
         List<FsCourseRedPacketLogListPVO> list = fsCourseRedPacketLogService.selectFsCourseRedPacketLogListVO(fsCourseRedPacketLog);
-        for (FsCourseRedPacketLogListPVO fsCourseRedPacketLogListPVO : list) {
-            if (ObjectUtil.isNotEmpty(fsCourseRedPacketLogListPVO.getPeriodId())){
-                FsUserCoursePeriod fsUserCoursePeriod = fsUserCoursePeriodService.selectFsUserCoursePeriodById(fsCourseRedPacketLogListPVO.getPeriodId().longValue());
-                fsCourseRedPacketLogListPVO.setPeriodName(fsUserCoursePeriod.getPeriodName());
-            }
-            fsCourseRedPacketLogListPVO.setPhone(PhoneUtil.decryptAutoPhoneMk(fsCourseRedPacketLogListPVO.getPhone()));
-        }
+//        for (FsCourseRedPacketLogListPVO fsCourseRedPacketLogListPVO : list) {
+//            if (ObjectUtil.isNotEmpty(fsCourseRedPacketLogListPVO.getPeriodId())){
+//                FsUserCoursePeriod fsUserCoursePeriod = fsUserCoursePeriodService.selectFsUserCoursePeriodById(fsCourseRedPacketLogListPVO.getPeriodId().longValue());
+//                fsCourseRedPacketLogListPVO.setPeriodName(fsUserCoursePeriod.getPeriodName());
+//            }
+//            fsCourseRedPacketLogListPVO.setPhone(PhoneUtil.decryptAutoPhoneMk(fsCourseRedPacketLogListPVO.getPhone()));
+//        }
         ExcelUtil<FsCourseRedPacketLogListPVO> util = new ExcelUtil<FsCourseRedPacketLogListPVO>(FsCourseRedPacketLogListPVO.class);
         return util.exportExcel(list, "短链课程看课记录数据");
     }

+ 4 - 10
fs-company/src/main/java/com/fs/company/controller/course/FsCourseRedPacketLogController.java

@@ -25,6 +25,7 @@ import com.fs.framework.service.TokenService;
 import com.fs.his.utils.PhoneUtil;
 import com.fs.his.vo.OptionsVO;
 import com.fs.system.service.ISysConfigService;
+import com.github.pagehelper.PageInfo;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.security.access.prepost.PreAuthorize;
 import org.springframework.web.bind.annotation.*;
@@ -63,7 +64,7 @@ public class FsCourseRedPacketLogController extends BaseController
     @GetMapping("/list")
     public TableDataInfo list(FsCourseRedPacketLogParam fsCourseRedPacketLog)
     {
-        startPage();
+//        startPage();
         if (fsCourseRedPacketLog.getPhoneMk() != null && fsCourseRedPacketLog.getPhoneMk() != "") {
             fsCourseRedPacketLog.setPhone(encryptPhone(fsCourseRedPacketLog.getPhoneMk()));
         }
@@ -76,16 +77,9 @@ public class FsCourseRedPacketLogController extends BaseController
             fsCourseRedPacketLog.setCompanyId( loginUser.getCompany().getCompanyId());
         }
 
-        List<FsCourseRedPacketLogListPVO> list = fsCourseRedPacketLogService.selectFsCourseRedPacketLogListVO(fsCourseRedPacketLog);
-        for (FsCourseRedPacketLogListPVO fsCourseRedPacketLogListPVO : list) {
-            if (ObjectUtil.isNotEmpty(fsCourseRedPacketLogListPVO.getPeriodId())){
-                FsUserCoursePeriod fsUserCoursePeriod = fsUserCoursePeriodService.selectFsUserCoursePeriodById(fsCourseRedPacketLogListPVO.getPeriodId().longValue());
-                fsCourseRedPacketLogListPVO.setPeriodName(fsUserCoursePeriod.getPeriodName());
-            }
+        PageInfo<FsCourseRedPacketLogListPVO> list = fsCourseRedPacketLogService.selectFsCourseRedPacketLogListVOPage(fsCourseRedPacketLog);
 
-            fsCourseRedPacketLogListPVO.setPhone(PhoneUtil.decryptAutoPhoneMk(fsCourseRedPacketLogListPVO.getPhone()));
-        }
-        return getDataTable(list);
+        return getDataTable(list.getList());
     }
 
     /**

+ 5 - 33
fs-service/src/main/java/com/fs/course/mapper/FsCourseRedPacketLogMapper.java

@@ -114,39 +114,11 @@ public interface FsCourseRedPacketLogMapper
             "</script>"})
     List<FsCourseRedPacketLogListPVO> selectRedPacketLogListVO(@Param("maps") FsCourseRedPacketLogParam param);
 
-    @Select({"<script> " +
-            "select l.*,v.title,u.nick_name as fsNickName,u.avatar as fsAvatar,u.phone,cu.nick_name company_user_name,c.company_name,qu.qw_user_name,fuc.course_name,u.phone as phoneNumber,cu.dept_id   from fs_course_red_packet_log l  \n" +
-            "left join fs_user_course_video v on v.video_id = l.video_id \n" +
-            "left join fs_user u on u.user_id = l.user_id \n" +
-            "left join fs_user_course fuc on fuc.course_id = l.course_id \n" +
-            "left join company_user cu on cu.user_id=l.company_user_id \n" +
-            "left join company c on c.company_id=cu.company_id \n" +
-            "LEFT JOIN qw_user qu on qu.id= l.qw_user_id  \n" +
-            "where 1=1   " +
-            "<if test = ' maps.userId !=null '> and l.user_id = #{maps.userId} </if>" +
-            "<if test = ' maps.logId !=null '> and l.log_id = #{maps.logId} </if>" +
-            "<if test = ' maps.watchLogId !=null '> and l.watch_log_id = #{maps.watchLogId} </if>" +
-            "<if test = ' maps.companyId !=null '> and l.company_id = #{maps.companyId} </if>" +
-            "<if test = ' maps.companyUserId !=null '> and l.company_user_id = #{maps.companyUserId} </if>" +
-            "<if test = ' maps.companyUserName !=null '> and cu.nick_name  like concat('%', #{maps.companyUserName}, '%') </if>" +
-            "<if test = ' maps.nickName !=null '> and u.nick_name  like concat('%', #{maps.nickName}, '%') </if>" +
-            "<if test = ' maps.courseId !=null '> and l.course_id = #{maps.courseId} </if>" +
-            "<if test = ' maps.videoId !=null '> and l.video_id = #{maps.videoId} </if>" +
-            "<if test = ' maps.periodId !=null '> and l.period_id = #{maps.periodId} </if>" +
-            "<if test = ' maps.status !=null '> and l.status = #{maps.status} </if>" +
-            "<if test = \"maps.phone !=null and maps.phone != '' \"> and u.phone = #{maps.phone} </if>" +
-            "<if test = ' maps.qwUserId !=null '> and l.qw_user_id = #{maps.qwUserId} </if>" +
-            "<if test=\"maps.sTime != null \">  and DATE(l.create_time) &gt;= DATE(#{maps.sTime})</if>\n" +
-            "<if test=\"maps.eTime != null \">  and DATE(l.create_time) &lt;= DATE(#{maps.eTime})</if>\n" +
-            "<if test=\"maps.userIds != null and maps.userIds.size() > 0\">\n" +
-            "                AND l.company_user_id IN\n" +
-            "                <foreach collection=\"maps.userIds\" open=\"(\" close=\")\" separator=\",\" item=\"item\">\n" +
-            "                    ${item}\n" +
-            "                </foreach>\n" +
-            "            </if>" +
-            " order by l.log_id desc  "+
-            "</script>"})
-    List<FsCourseRedPacketLogListPVO> selectFsCourseRedPacketLogListVO(@Param("maps")FsCourseRedPacketLogParam fsCourseRedPacketLog);
+    List<FsCourseRedPacketLogListPVO> selectFsCourseRedPacketLogListVO(@Param("maps") FsCourseRedPacketLogParam fsCourseRedPacketLog);
+
+    List<FsCourseRedPacketLogListPVO> selectFsCourseRedPacketLogListVOPage(@Param("maps") FsCourseRedPacketLogParam fsCourseRedPacketLog);
+
+    long selectFsCourseRedPacketLogListVOCount(@Param("maps") FsCourseRedPacketLogParam fsCourseRedPacketLog);
 
     @Select("SELECT company_id, SUM(amount) as money  FROM fs_course_red_packet_log    WHERE status in (0,1) and create_time >= DATE_SUB(CURDATE(), INTERVAL 1 DAY)   AND create_time < CURDATE()  GROUP BY company_id " )
     List<RedPacketMoneyVO> selectFsCourseRedPacketLogByCompany();

+ 4 - 0
fs-service/src/main/java/com/fs/course/param/FsCourseRedPacketLogParam.java

@@ -47,6 +47,10 @@ public class FsCourseRedPacketLogParam implements Serializable {
     private Integer pageNum;
     private Integer pageSize;
 
+    public int getOffset() {
+        return (pageNum != null ? pageNum : 1) * (pageSize != null ? pageSize : 10);
+    }
+
     private List<String> userIds;
 
     public List<String> getUserIds() {

+ 3 - 0
fs-service/src/main/java/com/fs/course/service/IFsCourseRedPacketLogService.java

@@ -8,6 +8,7 @@ import com.fs.course.domain.FsCourseRedPacketLog;
 import com.fs.course.param.FsCourseRedPacketLogParam;
 import com.fs.course.vo.FsCourseRedPacketLogListPVO;
 import com.fs.course.vo.FsCourseRedPacketLogListVO;
+import com.github.pagehelper.PageInfo;
 
 /**
  * 短链课程看课记录Service接口
@@ -105,4 +106,6 @@ public interface IFsCourseRedPacketLogService
      * @Date 2026/2/26 9:12
      */
     BigDecimal getTodaySumAmountByCompanyUserId(Long userId);
+
+    PageInfo<FsCourseRedPacketLogListPVO> selectFsCourseRedPacketLogListVOPage(FsCourseRedPacketLogParam fsCourseRedPacketLog);
 }

+ 23 - 13
fs-service/src/main/java/com/fs/course/service/impl/FsCourseRedPacketLogServiceImpl.java

@@ -13,7 +13,7 @@ import com.alibaba.fastjson.JSON;
 import com.fs.common.core.domain.R;
 import com.fs.common.utils.DateUtils;
 import com.fs.common.utils.StringUtils;
-import com.fs.company.cache.ICompanyDeptCacheService;
+
 import com.fs.company.domain.Company;
 import com.fs.company.domain.CompanyMoneyLogs;
 import com.fs.company.domain.CompanyRedPacketBalanceLogs;
@@ -34,6 +34,7 @@ import com.fs.his.domain.FsUser;
 import com.fs.his.mapper.FsUserMapper;
 import com.fs.his.param.WxSendRedPacketParam;
 import com.fs.his.service.IFsStorePaymentService;
+import com.fs.his.utils.PhoneUtil;
 import com.fs.system.service.ISysConfigService;
 import com.github.binarywang.wxpay.bean.transfer.QueryTransferBatchesRequest;
 import com.github.binarywang.wxpay.bean.transfer.QueryTransferBatchesResult;
@@ -45,6 +46,7 @@ import com.github.binarywang.wxpay.service.TransferService;
 import com.github.binarywang.wxpay.service.WxPayService;
 import com.github.binarywang.wxpay.service.impl.WxPayServiceImpl;
 import com.github.pagehelper.PageHelper;
+import com.github.pagehelper.PageInfo;
 import lombok.extern.slf4j.Slf4j;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -75,8 +77,6 @@ public class FsCourseRedPacketLogServiceImpl implements IFsCourseRedPacketLogSer
     @Autowired
     private ISysConfigService configService;
     @Autowired
-    private ICompanyDeptCacheService companyDeptCacheService;
-    @Autowired
     private CompanyRedPacketBalanceLogsMapper companyRedPacketBalanceLogsMapper;
     /**
      * 查询短链课程看课记录
@@ -186,20 +186,30 @@ public class FsCourseRedPacketLogServiceImpl implements IFsCourseRedPacketLogSer
     @Override
     public List<FsCourseRedPacketLogListPVO> selectFsCourseRedPacketLogListVO(FsCourseRedPacketLogParam fsCourseRedPacketLog) {
         List<FsCourseRedPacketLogListPVO> list = fsCourseRedPacketLogMapper.selectFsCourseRedPacketLogListVO(fsCourseRedPacketLog);
-        // 应用到每个对象
-        if(CollectionUtil.isNotEmpty(list)){
-            list.forEach(item -> {
-                if(item.getDeptId() != null) {
-                    String deptNameById = companyDeptCacheService.getDeptNameById(item.getDeptId());
-                    if(StringUtils.isNotBlank(deptNameById)) {
-                        item.setDeptName(deptNameById);
-                    }
-                }
-            });
+        if (CollectionUtil.isNotEmpty(list)) {
+            list.forEach(item -> item.setPhone(PhoneUtil.decryptAutoPhoneMk(item.getPhone())));
         }
         return list;
     }
 
+    @Override
+    public PageInfo<FsCourseRedPacketLogListPVO> selectFsCourseRedPacketLogListVOPage(FsCourseRedPacketLogParam param) {
+        // 快速COUNT:只查主表,走索引
+        long total = fsCourseRedPacketLogMapper.selectFsCourseRedPacketLogListVOCount(param);
+        // 延迟JOIN数据查询:内层子查询只取10条ID,外层只JOIN 10条
+        List<FsCourseRedPacketLogListPVO> list = fsCourseRedPacketLogMapper.selectFsCourseRedPacketLogListVOPage(param);
+        if (CollectionUtil.isNotEmpty(list)) {
+            list.forEach(item -> item.setPhone(PhoneUtil.decryptAutoPhoneMk(item.getPhone())));
+        }
+        // 手动构建PageInfo(兼容前端字段)
+        PageInfo<FsCourseRedPacketLogListPVO> pageInfo = new PageInfo<>(list);
+        pageInfo.setTotal(total);
+        pageInfo.setPageNum(param.getPageNum() != null ? param.getPageNum() : 1);
+        pageInfo.setPageSize(param.getPageSize() != null ? param.getPageSize() : 10);
+        pageInfo.setPages((int) Math.ceil((double) total / pageInfo.getPageSize()));
+        return pageInfo;
+    }
+
     @Override
     public List<FsCourseRedPacketLogListPVO> selectFsCourseRedPacketLogListVONew(FsCourseRedPacketLogParam fsCourseRedPacketLog) {
         return fsCourseRedPacketLogMapper.selectFsCourseRedPacketLogListVONew(fsCourseRedPacketLog);

+ 71 - 0
fs-service/src/main/resources/mapper/course/FsCourseRedPacketLogMapper.xml

@@ -386,4 +386,75 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
             #{id}
         </foreach>
     </delete>
+
+    <!-- 延迟JOIN分页查询:内层子查询只查主表ID+LIMIT(走索引极快),外层只JOIN 10条数据 -->
+    <select id="selectFsCourseRedPacketLogListVOPage" resultType="com.fs.course.vo.FsCourseRedPacketLogListPVO">
+        SELECT
+            l.log_id, l.course_id, l.user_id, l.video_id, l.company_user_id, l.company_id,
+            l.amount, l.create_time, l.qw_user_id, l.out_batch_no, l.status, l.update_time,
+            l.watch_log_id, l.remark, l.period_id, l.batch_id, l.app_id,
+            u.nick_name AS fsNickName, u.avatar AS fsAvatar, u.phone,
+            cu.nick_name AS company_user_name, cu.dept_id,
+            (SELECT company_name FROM company WHERE company_id = l.company_id) AS company_name,
+            (SELECT qw_user_name FROM qw_user WHERE id = l.qw_user_id) AS qw_user_name,
+            (SELECT title FROM fs_user_course_video WHERE video_id = l.video_id) AS video_name,
+            (SELECT period_name FROM fs_user_course_period WHERE period_id = l.period_id) AS period_name,
+            (SELECT dept_name FROM company_dept WHERE dept_id = cu.dept_id) AS dept_name,
+            (SELECT course_name FROM fs_user_course WHERE course_id = l.course_id) AS course_name
+        FROM fs_course_red_packet_log l
+        INNER JOIN (
+            SELECT log_id FROM fs_course_red_packet_log
+            <where>
+                <if test="maps.userId != null"> AND user_id = #{maps.userId} </if>
+                <if test="maps.logId != null"> AND log_id = #{maps.logId} </if>
+                <if test="maps.watchLogId != null"> AND watch_log_id = #{maps.watchLogId} </if>
+                <if test="maps.companyId != null"> AND company_id = #{maps.companyId} </if>
+                <if test="maps.companyUserId != null"> AND company_user_id = #{maps.companyUserId} </if>
+                <if test="maps.courseId != null"> AND course_id = #{maps.courseId} </if>
+                <if test="maps.videoId != null"> AND video_id = #{maps.videoId} </if>
+                <if test="maps.periodId != null"> AND period_id = #{maps.periodId} </if>
+                <if test="maps.status != null"> AND status = #{maps.status} </if>
+                <if test="maps.qwUserId != null"> AND qw_user_id = #{maps.qwUserId} </if>
+                <if test="maps.sTime != null"> AND create_time &gt;= #{maps.sTime} </if>
+                <if test="maps.eTime != null"> AND create_time &lt;= #{maps.eTime} </if>
+                <if test="maps.userIds != null and maps.userIds.size() > 0">
+                    AND company_user_id IN
+                    <foreach collection="maps.userIds" open="(" close=")" separator="," item="item">${item}</foreach>
+                </if>
+            </where>
+            ORDER BY log_id DESC
+            LIMIT #{maps.offset}, #{maps.pageSize}
+        ) AS t ON t.log_id = l.log_id
+        LEFT JOIN fs_user u ON u.user_id = l.user_id
+        LEFT JOIN company_user cu ON cu.user_id = l.company_user_id
+        <where>
+            <if test="maps.companyUserName != null"> AND cu.nick_name LIKE concat('%', #{maps.companyUserName}, '%') </if>
+            <if test="maps.nickName != null"> AND u.nick_name LIKE concat('%', #{maps.nickName}, '%') </if>
+            <if test="maps.phone != null and maps.phone != ''"> AND u.phone = #{maps.phone} </if>
+        </where>
+        ORDER BY l.log_id DESC
+    </select>
+
+    <!-- 快速COUNT:仅查主表,不JOIN,走索引 -->
+    <select id="selectFsCourseRedPacketLogListVOCount" resultType="long">
+        SELECT COUNT(1) FROM fs_course_red_packet_log
+        <where>
+            <if test="maps.userId != null"> AND user_id = #{maps.userId} </if>
+            <if test="maps.logId != null"> AND log_id = #{maps.logId} </if>
+            <if test="maps.watchLogId != null"> AND watch_log_id = #{maps.watchLogId} </if>
+            <if test="maps.companyId != null"> AND company_id = #{maps.companyId} </if>
+            <if test="maps.companyUserId != null"> AND company_user_id = #{maps.companyUserId} </if>
+            <if test="maps.courseId != null"> AND course_id = #{maps.courseId} </if>
+            <if test="maps.videoId != null"> AND video_id = #{maps.videoId} </if>
+            <if test="maps.periodId != null"> AND period_id = #{maps.periodId} </if>
+            <if test="maps.status != null"> AND status = #{maps.status} </if>
+            <if test="maps.qwUserId != null"> AND qw_user_id = #{maps.qwUserId} </if>
+            <if test="maps.sTime != null"> AND create_time &gt;= #{maps.sTime} </if>
+            <if test="maps.eTime != null"> AND create_time &lt;= #{maps.eTime} </if>
+            <if test="maps.userIds != null and maps.userIds.size() > 0">
+                AND company_user_id IN
+                <foreach collection="maps.userIds" open="(" close=")" separator="," item="item">${item}</foreach>
+            </if>
+        </where>
+    </select>
 </mapper>