|
@@ -99,10 +99,17 @@ class ChatRecord extends BaseModel
|
|
|
*/
|
|
*/
|
|
|
public static function markAsRead(int $userId, int $fromUserId): bool
|
|
public static function markAsRead(int $userId, int $fromUserId): bool
|
|
|
{
|
|
{
|
|
|
- return self::where('user_id', $fromUserId)
|
|
|
|
|
|
|
+ $affected = \think\facade\Db::name('chat_record')
|
|
|
|
|
+ ->where('user_id', $fromUserId)
|
|
|
->where('to_user_id', $userId)
|
|
->where('to_user_id', $userId)
|
|
|
->where('is_read', self::UNREAD)
|
|
->where('is_read', self::UNREAD)
|
|
|
- ->update(['is_read' => self::READ]) !== false;
|
|
|
|
|
|
|
+ ->update(['is_read' => self::READ]);
|
|
|
|
|
+
|
|
|
|
|
+ // 调试:记录执行的SQL和影响行数
|
|
|
|
|
+ $sql = \think\facade\Db::name('chat_record')->getLastSql();
|
|
|
|
|
+ trace('【markAsRead】SQL: ' . $sql . ' | 影响行数: ' . $affected, 'chat_debug');
|
|
|
|
|
+
|
|
|
|
|
+ return $affected > 0;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -113,16 +120,21 @@ class ChatRecord extends BaseModel
|
|
|
*/
|
|
*/
|
|
|
public static function getUnreadCount(int $userId, ?int $fromUserId = null): int
|
|
public static function getUnreadCount(int $userId, ?int $fromUserId = null): int
|
|
|
{
|
|
{
|
|
|
- $where = [
|
|
|
|
|
- 'to_user_id' => $userId,
|
|
|
|
|
- 'is_read' => self::UNREAD,
|
|
|
|
|
- ];
|
|
|
|
|
|
|
+ $query = \think\facade\Db::name('chat_record')
|
|
|
|
|
+ ->where('to_user_id', $userId)
|
|
|
|
|
+ ->where('is_read', self::UNREAD);
|
|
|
|
|
|
|
|
- if ($fromUserId !== null) {
|
|
|
|
|
- $where['user_id'] = $fromUserId;
|
|
|
|
|
|
|
+ if ($fromUserId !== null && $fromUserId > 0) {
|
|
|
|
|
+ $query->where('user_id', $fromUserId);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- return self::where($where)->count();
|
|
|
|
|
|
|
+ $count = $query->count();
|
|
|
|
|
+
|
|
|
|
|
+ // 调试:记录执行的SQL和结果
|
|
|
|
|
+ $sql = \think\facade\Db::name('chat_record')->getLastSql();
|
|
|
|
|
+ trace('【getUnreadCount】SQL: ' . $sql . ' | 结果: ' . $count, 'chat_debug');
|
|
|
|
|
+
|
|
|
|
|
+ return $count;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -132,9 +144,16 @@ class ChatRecord extends BaseModel
|
|
|
*/
|
|
*/
|
|
|
public static function getTotalUnreadCount(int $userId): int
|
|
public static function getTotalUnreadCount(int $userId): int
|
|
|
{
|
|
{
|
|
|
- return self::where('to_user_id', $userId)
|
|
|
|
|
|
|
+ $count = \think\facade\Db::name('chat_record')
|
|
|
|
|
+ ->where('to_user_id', $userId)
|
|
|
->where('is_read', self::UNREAD)
|
|
->where('is_read', self::UNREAD)
|
|
|
->count();
|
|
->count();
|
|
|
|
|
+
|
|
|
|
|
+ // 调试:记录执行的SQL和结果
|
|
|
|
|
+ $sql = \think\facade\Db::name('chat_record')->getLastSql();
|
|
|
|
|
+ trace('【getTotalUnreadCount】SQL: ' . $sql . ' | 结果: ' . $count, 'chat_debug');
|
|
|
|
|
+
|
|
|
|
|
+ return $count;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|