From 9f86fc9f7660a773fe66104d7368b3d291e6a29c Mon Sep 17 00:00:00 2001 From: jqb Date: Fri, 10 Jul 2026 16:44:07 +0800 Subject: [PATCH] =?UTF-8?q?refactor(archive-service):=20=E9=87=8D=E6=9E=84?= =?UTF-8?q?=E5=91=98=E5=B7=A5=E4=BC=9A=E8=AF=9D=E5=AD=98=E6=A1=A3=E7=BB=9F?= =?UTF-8?q?=E8=AE=A1=E9=80=BB=E8=BE=91=EF=BC=8C=E4=BC=98=E5=8C=96=E6=9F=A5?= =?UTF-8?q?=E8=AF=A2=E6=80=A7=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1. 新增StaffMapper分页查询与计数方法,替换原MyBatis-Plus分页写法 2. 新增批量统计方法,合并多轮单员工查询为批量SQL查询 3. 重构员工列表查询逻辑,减少数据库交互次数 4. 拆分基础VO转换与统计填充方法,优化代码结构 --- .../archive/mapper/ArchiveMessageMapper.java | 15 ++++ .../artedu/archive/mapper/CustomerMapper.java | 16 ++++ .../artedu/archive/mapper/StaffMapper.java | 34 ++++++++ .../service/ArchiveSessionService.java | 82 +++++++++++-------- 4 files changed, 114 insertions(+), 33 deletions(-) diff --git a/backend/archive-service/src/main/java/com/artedu/archive/mapper/ArchiveMessageMapper.java b/backend/archive-service/src/main/java/com/artedu/archive/mapper/ArchiveMessageMapper.java index eed85ee..e7f614c 100644 --- a/backend/archive-service/src/main/java/com/artedu/archive/mapper/ArchiveMessageMapper.java +++ b/backend/archive-service/src/main/java/com/artedu/archive/mapper/ArchiveMessageMapper.java @@ -106,6 +106,21 @@ public interface ArchiveMessageMapper extends BaseMapper { @Select("SELECT DISTINCT from_user FROM archive_messages WHERE corp_id = #{corpId} AND from_role = 'INTERNAL'") List selectStaffIdsWithArchive(@Param("corpId") String corpId); + @Select("") + List> selectStaffArchiveStats(@Param("corpId") String corpId, + @Param("staffIds") List staffIds); + /** * 查询尚未上传到 COS 的媒体消息 */ diff --git a/backend/archive-service/src/main/java/com/artedu/archive/mapper/CustomerMapper.java b/backend/archive-service/src/main/java/com/artedu/archive/mapper/CustomerMapper.java index d6be636..fb49866 100644 --- a/backend/archive-service/src/main/java/com/artedu/archive/mapper/CustomerMapper.java +++ b/backend/archive-service/src/main/java/com/artedu/archive/mapper/CustomerMapper.java @@ -6,6 +6,9 @@ import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Param; import org.apache.ibatis.annotations.Select; +import java.util.List; +import java.util.Map; + /** * 客户数据访问层 */ @@ -14,4 +17,17 @@ public interface CustomerMapper extends BaseMapper { @Select("SELECT * FROM customers WHERE customer_id = #{customerId} AND corp_id = #{corpId} LIMIT 1") Customer selectByCustomerId(@Param("customerId") String customerId, @Param("corpId") String corpId); + + @Select("") + List> countCustomersByStaffIds(@Param("corpId") String corpId, + @Param("staffIds") List staffIds); } diff --git a/backend/archive-service/src/main/java/com/artedu/archive/mapper/StaffMapper.java b/backend/archive-service/src/main/java/com/artedu/archive/mapper/StaffMapper.java index 6defc1f..5c9bd1f 100644 --- a/backend/archive-service/src/main/java/com/artedu/archive/mapper/StaffMapper.java +++ b/backend/archive-service/src/main/java/com/artedu/archive/mapper/StaffMapper.java @@ -6,9 +6,43 @@ import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Param; import org.apache.ibatis.annotations.Select; +import java.util.List; + @Mapper public interface StaffMapper extends BaseMapper { @Select("SELECT * FROM staffs WHERE staff_id = #{staffId} AND corp_id = #{corpId} LIMIT 1") Staff selectByStaffId(@Param("staffId") String staffId, @Param("corpId") String corpId); + + @Select("") + List selectStaffPage(@Param("corpId") String corpId, + @Param("keyword") String keyword, + @Param("hasArchive") boolean hasArchive, + @Param("offset") int offset, + @Param("size") int size); + + @Select("") + Long countStaffPage(@Param("corpId") String corpId, + @Param("keyword") String keyword, + @Param("hasArchive") boolean hasArchive); } diff --git a/backend/archive-service/src/main/java/com/artedu/archive/service/ArchiveSessionService.java b/backend/archive-service/src/main/java/com/artedu/archive/service/ArchiveSessionService.java index a6ada85..83c3286 100644 --- a/backend/archive-service/src/main/java/com/artedu/archive/service/ArchiveSessionService.java +++ b/backend/archive-service/src/main/java/com/artedu/archive/service/ArchiveSessionService.java @@ -51,24 +51,17 @@ public class ArchiveSessionService { * 员工列表 */ public PageResult listStaffs(String keyword, int page, int size, boolean hasArchive) { - LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>(); - wrapper.eq(Staff::getCorpId, corpId); - if (keyword != null && !keyword.isEmpty()) { - wrapper.and(w -> w.like(Staff::getStaffId, keyword).or().like(Staff::getName, keyword)); + int offset = (page - 1) * size; + Long total = staffMapper.countStaffPage(corpId, keyword, hasArchive); + if (total == null || total == 0) { + return PageResult.of(page, size, 0L, Collections.emptyList()); } - // 只展示有会话存档的员工 - if (hasArchive) { - List archiveStaffIds = archiveMessageMapper.selectStaffIdsWithArchive(corpId); - if (archiveStaffIds == null || archiveStaffIds.isEmpty()) { - return PageResult.of(page, size, 0L, Collections.emptyList()); - } - wrapper.in(Staff::getStaffId, archiveStaffIds); - } - wrapper.orderByDesc(Staff::getCreatedAt); - Page pageResult = staffMapper.selectPage(new Page<>(page, size), wrapper); - List list = pageResult.getRecords().stream().map(this::convertStaffVO).collect(Collectors.toList()); - return PageResult.of(page, size, pageResult.getTotal(), list); + List staffList = staffMapper.selectStaffPage(corpId, keyword, hasArchive, offset, size); + List list = staffList.stream().map(this::toBaseStaffVO).collect(Collectors.toList()); + List staffIds = staffList.stream().map(Staff::getStaffId).collect(Collectors.toList()); + fillStaffStats(list, staffIds); + return PageResult.of(page, size, total, list); } /** @@ -228,6 +221,12 @@ public class ArchiveSessionService { // ============ 私有方法 ============ private StaffSessionVO convertStaffVO(Staff staff) { + StaffSessionVO vo = toBaseStaffVO(staff); + fillStaffStats(Collections.singletonList(vo), Collections.singletonList(staff.getStaffId())); + return vo; + } + + private StaffSessionVO toBaseStaffVO(Staff staff) { StaffSessionVO vo = new StaffSessionVO(); vo.setStaffId(staff.getStaffId()); vo.setName(staff.getName()); @@ -237,26 +236,43 @@ public class ArchiveSessionService { vo.setStatus(staff.getStatus()); vo.setCreatedAt(staff.getCreatedAt()); vo.setUpdatedAt(staff.getUpdatedAt()); - - // 查询统计 - try { - Long msgCount = archiveMessageMapper.selectCount( - new com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper() - .eq(ArchiveMessage::getCorpId, corpId) - .eq(ArchiveMessage::getFromUser, staff.getStaffId()) - ); - vo.setMsgCount(msgCount != null ? msgCount.intValue() : 0); - vo.setTodayMsgCount(archiveMessageMapper.countTodayMsgByStaff(staff.getStaffId(), corpId)); - vo.setCustomerCount(archiveMessageMapper.countCustomersByStaff(staff.getStaffId(), corpId)); - Long lastMsgTime = archiveMessageMapper.selectLastMsgTimeByUser(staff.getStaffId(), corpId); - vo.setLastMsgTime(lastMsgTime); - vo.setLastMsgTimeStr(formatTime(lastMsgTime)); - } catch (Exception e) { - log.debug("查询员工统计失败: {}", e.getMessage()); - } return vo; } + private void fillStaffStats(List vos, List staffIds) { + if (vos == null || vos.isEmpty() || staffIds == null || staffIds.isEmpty()) { + return; + } + try { + List> statsList = archiveMessageMapper.selectStaffArchiveStats(corpId, staffIds); + Map> statsMap = statsList.stream() + .collect(Collectors.toMap(m -> (String) m.get("staff_id"), m -> m, (a, b) -> a)); + + List> custList = customerMapper.countCustomersByStaffIds(corpId, staffIds); + Map custMap = custList.stream() + .collect(Collectors.toMap(m -> (String) m.get("staff_id"), m -> ((Number) m.get("customer_count")).intValue(), (a, b) -> a)); + + for (StaffSessionVO vo : vos) { + Map stats = statsMap.get(vo.getStaffId()); + if (stats != null) { + vo.setMsgCount(((Number) stats.get("msg_count")).intValue()); + vo.setTodayMsgCount(((Number) stats.get("today_msg_count")).intValue()); + Long lastMsgTime = stats.get("last_msg_time") instanceof Number ? ((Number) stats.get("last_msg_time")).longValue() : null; + vo.setLastMsgTime(lastMsgTime); + vo.setLastMsgTimeStr(formatTime(lastMsgTime)); + } else { + vo.setMsgCount(0); + vo.setTodayMsgCount(0); + vo.setLastMsgTime(null); + vo.setLastMsgTimeStr(formatTime(null)); + } + vo.setCustomerCount(custMap.getOrDefault(vo.getStaffId(), 0)); + } + } catch (Exception e) { + log.warn("查询员工统计失败: {}", e.getMessage(), e); + } + } + private CustomerSessionVO convertCustomerVO(Customer customer) { CustomerSessionVO vo = new CustomerSessionVO(); vo.setCustomerId(customer.getCustomerId());