|
|
@@ -1,8 +1,10 @@
|
|
|
package com.etotem.cfc.service.impl;
|
|
|
|
|
|
+import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
|
+import com.etotem.cfc.dto.FamilyConversationVO;
|
|
|
+import com.etotem.cfc.dto.FamilyMessageVO;
|
|
|
import com.etotem.cfc.dto.FamilyWorkbenchCard;
|
|
|
import com.etotem.cfc.dto.FamilyWorkbenchDTO;
|
|
|
-import com.etotem.cfc.dto.FamilyWorkbenchSeriesPoint;
|
|
|
import com.etotem.cfc.entity.Family;
|
|
|
import com.etotem.cfc.mapper.FamilyMapper;
|
|
|
import com.etotem.cfc.mapper.FamilyWorkbenchMapper;
|
|
|
@@ -13,6 +15,7 @@ import org.springframework.stereotype.Service;
|
|
|
import javax.annotation.Resource;
|
|
|
import java.time.LocalDate;
|
|
|
import java.util.ArrayList;
|
|
|
+import java.util.Date;
|
|
|
import java.util.HashMap;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
@@ -20,6 +23,9 @@ import java.util.Map;
|
|
|
@Service
|
|
|
public class FamilyWorkbenchServiceImpl implements FamilyWorkbenchService {
|
|
|
|
|
|
+ private static final int CONVERSATION_PREVIEW_MAX = 50;
|
|
|
+ private static final int MESSAGE_CONTENT_MAX = 2000;
|
|
|
+
|
|
|
@Resource
|
|
|
private FamilyMapper familyMapper;
|
|
|
|
|
|
@@ -55,6 +61,56 @@ public class FamilyWorkbenchServiceImpl implements FamilyWorkbenchService {
|
|
|
return dto;
|
|
|
}
|
|
|
|
|
|
+ @Override
|
|
|
+ public Page<FamilyConversationVO> pageConversations(Long familyId, long page, long size) {
|
|
|
+ long safePage = Math.max(page, 1);
|
|
|
+ long safeSize = Math.min(Math.max(size, 1), 100);
|
|
|
+ List<Map<String, Object>> rows = workbenchMapper.conversationsByFamily(familyId);
|
|
|
+ long total = rows.size();
|
|
|
+ int fromIdx = (int) Math.min((safePage - 1) * safeSize, total);
|
|
|
+ int toIdx = (int) Math.min(fromIdx + safeSize, total);
|
|
|
+
|
|
|
+ List<FamilyConversationVO> records = new ArrayList<>();
|
|
|
+ for (Map<String, Object> r : rows.subList(fromIdx, toIdx)) {
|
|
|
+ FamilyConversationVO vo = new FamilyConversationVO();
|
|
|
+ vo.setId(asLong(r.get("id")));
|
|
|
+ vo.setTitle(asString(r.get("title")));
|
|
|
+ vo.setAssistantType(asString(r.get("assistantType")));
|
|
|
+ vo.setLastMessageAt(asDate(r.get("lastMessageAt")));
|
|
|
+ vo.setMessageCount(workbenchMapper.conversationMessageCount(vo.getId()));
|
|
|
+ vo.setLastMessagePreview(trim(workbenchMapper.conversationLastMessage(vo.getId()), CONVERSATION_PREVIEW_MAX));
|
|
|
+ records.add(vo);
|
|
|
+ }
|
|
|
+ Page<FamilyConversationVO> out = new Page<>(safePage, safeSize, total);
|
|
|
+ out.setRecords(records);
|
|
|
+ return out;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Page<FamilyMessageVO> pageConversationMessages(Long familyId, Long conversationId, long page, long size) {
|
|
|
+ long safePage = Math.max(page, 1);
|
|
|
+ long safeSize = Math.min(Math.max(size, 1), 200);
|
|
|
+ if (workbenchMapper.conversationInFamily(conversationId, familyId) <= 0) {
|
|
|
+ throw new RuntimeException("会话不属于该家庭");
|
|
|
+ }
|
|
|
+ long total = workbenchMapper.conversationMessageCount(conversationId);
|
|
|
+ long offset = (safePage - 1) * safeSize;
|
|
|
+ List<Map<String, Object>> rows = workbenchMapper.messagesOfConversation(conversationId, offset, safeSize);
|
|
|
+
|
|
|
+ List<FamilyMessageVO> records = new ArrayList<>();
|
|
|
+ for (Map<String, Object> r : rows) {
|
|
|
+ FamilyMessageVO vo = new FamilyMessageVO();
|
|
|
+ vo.setId(asLong(r.get("id")));
|
|
|
+ vo.setRole(asString(r.get("role")));
|
|
|
+ vo.setContent(trim(asString(r.get("content")), MESSAGE_CONTENT_MAX));
|
|
|
+ vo.setCreatedAt(asDate(r.get("createdAt")));
|
|
|
+ records.add(vo);
|
|
|
+ }
|
|
|
+ Page<FamilyMessageVO> out = new Page<>(safePage, safeSize, total);
|
|
|
+ out.setRecords(records);
|
|
|
+ return out;
|
|
|
+ }
|
|
|
+
|
|
|
private FamilyWorkbenchCard card(String key, String label, Long total,
|
|
|
List<Map<String, Object>> rows, LocalDate from, LocalDate to) {
|
|
|
FamilyWorkbenchCard c = new FamilyWorkbenchCard();
|
|
|
@@ -77,4 +133,21 @@ public class FamilyWorkbenchServiceImpl implements FamilyWorkbenchService {
|
|
|
f.put("createdAt", family.getCreatedAt());
|
|
|
return f;
|
|
|
}
|
|
|
+
|
|
|
+ private static String trim(String s, int max) {
|
|
|
+ if (s == null) return null;
|
|
|
+ return s.length() > max ? s.substring(0, max) + "…" : s;
|
|
|
+ }
|
|
|
+
|
|
|
+ private static Long asLong(Object o) {
|
|
|
+ return o == null ? null : ((Number) o).longValue();
|
|
|
+ }
|
|
|
+
|
|
|
+ private static String asString(Object o) {
|
|
|
+ return o == null ? null : o.toString();
|
|
|
+ }
|
|
|
+
|
|
|
+ private static Date asDate(Object o) {
|
|
|
+ return o instanceof Date ? (Date) o : null;
|
|
|
+ }
|
|
|
}
|