From 9f9e043849b6f497e00399900ff234cf097eba82 Mon Sep 17 00:00:00 2001
From: kongdeqiang <123456>
Date: 星期三, 14 十月 2026 17:04:33 +0800
Subject: [PATCH] fix: 修改上下文
---
src/main/java/com/custom/webui/service/impl/ChatServiceImpl.java | 104 +++++++++++++++++++++++++++++++++++++++++++++++----
1 files changed, 95 insertions(+), 9 deletions(-)
diff --git a/src/main/java/com/custom/webui/service/impl/ChatServiceImpl.java b/src/main/java/com/custom/webui/service/impl/ChatServiceImpl.java
index 246a831..d3cb235 100644
--- a/src/main/java/com/custom/webui/service/impl/ChatServiceImpl.java
+++ b/src/main/java/com/custom/webui/service/impl/ChatServiceImpl.java
@@ -15,6 +15,7 @@
import com.custom.webui.mapper.MessageMapper;
import com.custom.webui.mapper.UploadFileMapper;
import com.custom.webui.service.ChatService;
+import com.custom.webui.service.ChatGroupService;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
@@ -50,13 +51,20 @@
private final MessageMapper messageMapper;
private final UploadFileMapper uploadFileMapper;
private final ChatCollectionMapper chatCollectionMapper;
+ private final ChatGroupService chatGroupService;
private final ObjectMapper objectMapper;
@Override
- public List<ChatDTO> listByUser(Long userId) {
+ public List<ChatDTO> listByUser(Long userId, boolean archived) {
List<Chat> chats = chatMapper.selectList(
Wrappers.<Chat>lambdaQuery()
.eq(Chat::getUserId, userId)
+ .eq(Chat::getArchived, archived)
+ // 缃《浼樺厛锛屽叾娆$疆椤舵椂闂村�掑簭锛屾渶鍚庢椿璺冩椂闂村�掑簭銆�
+ // 闈炵疆椤惰鐨� pinned_at 鍏ㄤ负 NULL锛岀浜屾帓搴忛敭绛夊悓鏃犳晥锛�
+ // 浜庢槸鑷劧閫�鍖栨垚銆屾寜鏈�杩戞椿璺冨�掑簭銆嶃��
+ .orderByDesc(Chat::getPinned)
+ .orderByDesc(Chat::getPinnedAt)
.orderByDesc(Chat::getUpdatedAt));
if (chats.isEmpty()) {
return Collections.emptyList();
@@ -107,8 +115,22 @@
public ChatDTO update(Long userId, Long chatId, ChatUpdateRequest request) {
Chat existing = requireOwnedChat(userId, chatId);
- Chat update = new Chat();
- update.setId(chatId);
+ /*
+ * 杩欓噷鍒绘剰鐢� UpdateWrapper 閫愬垪鏄惧紡 SET锛岃�屼笉鏄� updateById(entity)锛�
+ *
+ * 1) updateById 榛樿**蹇界暐 null 瀛楁**锛岃�屻�屽彇娑堢疆椤躲�嶆伆鎭拌鎶�
+ * pinned_at 鍐欏洖 NULL锛岀敤瀹炰綋浼� null 琛ㄨ揪涓嶄簡杩欎釜鎰忓浘銆�
+ * 2) updated_at 杩欎竴鍒楀湪 MySQL 閲屽甫 ON UPDATE CURRENT_TIMESTAMP锛�
+ * 鍙 UPDATE 璇彞涓嶆樉寮忔彁鍒板畠锛屽畠灏变細琚暟鎹簱鑷姩鍒锋柊銆�
+ * 鑰岀疆椤�/褰掓。灞炰簬**瑙嗗浘鏁寸悊**鎿嶄綔锛屼笉璇ヨ褰撴垚銆屽垰鍒氭椿璺冦�嶁�斺��
+ * 鍚﹀垯缃《涓�涓嬶紝浼氳瘽灏变細鍦ㄦ椂闂村垎缁勯噷浠庛�屾洿鏃┿�嶈烦鍒般�屼粖澶┿�嶃��
+ * 鏄惧紡 SET updated_at = 鍘熷�� 鏄敮涓�鑳藉帇浣忚鑷姩琛屼负鐨勬柟寮�
+ * 锛圡ySQL 鏂囨。锛氳鏄惧紡璧嬪�肩殑鍒椾笉浼氬啀璧� ON UPDATE锛夈��
+ */
+ LocalDateTime now = LocalDateTime.now();
+ var wrapper = Wrappers.<Chat>lambdaUpdate().eq(Chat::getId, chatId);
+
+ boolean contentChanged = false;
if (request.getTitle() != null) {
String title = request.getTitle().trim();
@@ -116,16 +138,34 @@
throw BusinessException.badRequest("鏍囬涓嶈兘涓虹┖");
}
// 闃插尽瓒呴暱鏍囬锛氬垪瀹� 255锛岃秴鍑洪儴鍒嗙洿鎺ユ埅鏂�屼笉鏄姤鏁版嵁搴撻敊璇�
- update.setTitle(title.length() > 255 ? title.substring(0, 255) : title);
+ wrapper.set(Chat::getTitle, title.length() > 255 ? title.substring(0, 255) : title);
+ contentChanged = true;
}
if (request.getMeta() != null) {
// meta 鏄暣浣撹鐩栬涔夛細鍓嶇搴旀彁浜ゅ畬鏁寸殑鍙傛暟瀵硅薄
- update.setMeta(writeMeta(request.getMeta()));
+ wrapper.set(Chat::getMeta, writeMeta(request.getMeta()));
+ contentChanged = true;
}
- update.setUpdatedAt(LocalDateTime.now());
- chatMapper.updateById(update);
+ // ---- 缃《 ----
+ // 缃《鏃惰褰� pinned_at锛岀疆椤跺尯鍥犳鎸夈�岄拤涓婂幓鐨勫厛鍚庛�嶆帓搴忥紱
+ // 鍙栨秷缃《鍒欐竻鍥� NULL锛屼笉璁╂棫鏃堕棿娈嬬暀涓嬪幓褰卞搷鍚庣画鎺掑簭銆�
+ if (request.getPinned() != null) {
+ boolean pinned = request.getPinned();
+ wrapper.set(Chat::getPinned, pinned);
+ wrapper.set(Chat::getPinnedAt, pinned ? now : null);
+ }
+
+ // ---- 褰掓。 ----
+ if (request.getArchived() != null) {
+ boolean archived = request.getArchived();
+ wrapper.set(Chat::getArchived, archived);
+ wrapper.set(Chat::getArchivedAt, archived ? now : null);
+ }
+
+ wrapper.set(Chat::getUpdatedAt, contentChanged ? now : existing.getUpdatedAt());
+ chatMapper.update(null, wrapper);
Chat refreshed = chatMapper.selectById(chatId);
Long messageCount = messageMapper.selectCount(
@@ -134,7 +174,9 @@
Wrappers.<ChatCollection>lambdaQuery().eq(ChatCollection::getChatId, chatId))
.stream().map(ChatCollection::getCollectionName).toList();
- log.debug("鏇存柊浼氳瘽 chatId={} 鍘焟eta={} 鏂癿eta={}", chatId, existing.getMeta(), refreshed.getMeta());
+ log.debug("鏇存柊浼氳瘽 chatId={} 鍘焟eta={} 鏂癿eta={} pinned={} archived={}",
+ chatId, existing.getMeta(), refreshed.getMeta(),
+ refreshed.getPinned(), refreshed.getArchived());
return toDto(refreshed, messageCount, collections);
}
@@ -163,6 +205,38 @@
chatMapper.deleteById(chatId);
log.info("鍒犻櫎浼氳瘽 userId={} chatId={} 杩炲甫鍒犻櫎娑堟伅 {} 鏉°�佽В缁戞枃浠� {} 涓�",
userId, chatId, msgDeleted, boundFiles.size());
+ }
+
+ @Override
+ @Transactional(rollbackFor = Exception.class)
+ public ChatDTO moveToGroup(Long userId, Long chatId, Long groupId) {
+ Chat chat = requireOwnedChat(userId, chatId);
+
+ if (groupId != null) {
+ // 鐩爣鍒嗙粍蹇呴』鏄嚜宸辩殑 鈥斺�� 鍚﹀垯鐢ㄦ埛鍙互鎶婁細璇濄�岀Щ杩涖�嶅埆浜虹殑鍒嗙粍 id锛�
+ // 铏界劧鐪嬩笉鍒板鏂圭殑鍐呭锛屼絾浼氬湪瀵规柟鐨勫垪琛ㄩ噷鍑┖澶氬嚭涓�鏉′笉灞炰簬浠栫殑浼氳瘽
+ chatGroupService.requireOwnedGroup(userId, groupId);
+ }
+
+ // 涓�娆� UPDATE 鍚屾椂澶勭悊涓や欢浜嬶細
+ // 1. group_id 瑕佽兘鍐欐垚 NULL锛堢Щ鍑哄垎缁勶級锛屾墍浠ョ敤 UpdateWrapper 鏄惧紡 SET锛�
+ // 瀹炰綋閲岀殑 null 瀛楁浼氳 MyBatis-Plus 蹇界暐锛岃〃杈句笉浜嗐�岀疆绌恒��
+ // 2. updated_at 鏄惧紡鍐欏洖鍘熷�� 鈥斺�� 鏁寸悊鍔ㄤ綔涓嶈琚綋鎴愩�屽垰鍒氭椿璺冦�嶏紝
+ // 鍚﹀垯浼氳瘽浼氬湪鏃堕棿鍒嗙粍閲屼粠銆屾洿鏃┿�嶈烦鍒般�屼粖澶┿��
+ chatMapper.update(null,
+ Wrappers.<Chat>lambdaUpdate()
+ .eq(Chat::getId, chatId)
+ .set(Chat::getGroupId, groupId)
+ .set(Chat::getUpdatedAt, chat.getUpdatedAt()));
+
+ Long messageCount = messageMapper.selectCount(
+ Wrappers.<Message>lambdaQuery().eq(Message::getChatId, chatId));
+ List<String> collections = chatCollectionMapper.selectList(
+ Wrappers.<ChatCollection>lambdaQuery().eq(ChatCollection::getChatId, chatId))
+ .stream().map(ChatCollection::getCollectionName).toList();
+
+ log.info("浼氳瘽绉诲姩鍒嗙粍 userId={} chatId={} groupId={}", userId, chatId, groupId);
+ return toDto(chatMapper.selectById(chatId), messageCount, collections);
}
@Override
@@ -209,7 +283,7 @@
}
@Override
- public List<ChatDTO> search(Long userId, String keyword, int limit) {
+ public List<ChatDTO> search(Long userId, String keyword, int limit, boolean archived) {
if (keyword == null || keyword.isBlank()) {
// 绌哄叧閿瘝鐩存帴杩斿洖绌猴紝鑰屼笉鏄繑鍥炲叏閮ㄤ細璇� 鈥斺��
// 鍚﹀垯鎼滅储妗嗕細閫�鍖栨垚銆屾棤杩囨护鍒楄〃銆嶏紝鍜屼晶杈规爮鏈韩閲嶅
@@ -220,9 +294,14 @@
// 鍏堝彇璇ョ敤鎴峰叏閮ㄤ細璇濓紙涓汉浼氳瘽閲忕骇閫氬父鍦ㄦ暟鐧撅紝涓�娆℃煡瀹屽啀鍋氬唴瀛樺尮閰嶏紝
// 姣斾袱娆� LIKE 鏌ヨ + 鍚堝苟鏇寸畝鍗曪紝涔熼伩鍏嶄簡 title/message 涓ゆ寰�杩旓級銆�
+ //
+ // 鎼滅储鑼冨洿涓庡綋鍓嶈鍥句竴鑷达細涓诲垪琛ㄦ悳绱笉鎶婂綊妗e璇濇贩杩涙潵锛�
+ // 鍦ㄣ�屽綊妗e璇濄�嶉噷鎼滅储鍒欏彧鎼滃綊妗g殑 鈥斺�� 鍚﹀垯鐢ㄦ埛浼氭悳鍒颁竴鍫�
+ // 鑷繁鍒氬綊妗h捣鏉ャ�佸苟涓嶆兂鐪嬪埌鐨勫璇濄��
List<Chat> chats = chatMapper.selectList(
Wrappers.<Chat>lambdaQuery()
.eq(Chat::getUserId, userId)
+ .eq(Chat::getArchived, archived)
.orderByDesc(Chat::getUpdatedAt));
if (chats.isEmpty()) {
return Collections.emptyList();
@@ -346,6 +425,13 @@
dto.setMeta(chat.getMeta() == null || chat.getMeta().isBlank() ? "{}" : chat.getMeta());
dto.setCreatedAt(chat.getCreatedAt());
dto.setUpdatedAt(chat.getUpdatedAt());
+ // 缃《/褰掓。鍒楀彲鑳戒负 NULL锛堜緥濡傝縼绉诲墠鐨勮�佹暟鎹鏄惧紡鏌ュ嚭锛夛紝缁熶竴鍏滃簳鎴� false锛�
+ // 鍏嶅緱鍓嶇鎷垮埌 null 杩樿鍒板鍒ょ┖
+ dto.setPinned(Boolean.TRUE.equals(chat.getPinned()));
+ dto.setPinnedAt(chat.getPinnedAt());
+ dto.setArchived(Boolean.TRUE.equals(chat.getArchived()));
+ dto.setArchivedAt(chat.getArchivedAt());
+ dto.setGroupId(chat.getGroupId());
dto.setMessageCount(messageCount == null ? 0L : messageCount);
dto.setCollectionNames(collections == null ? new ArrayList<>() : collections);
return dto;
--
Gitblit v1.9.1