package com.snack.server.module.chat.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.snack.server.module.chat.entity.ChatSession;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Update;

/**
 * 客服会话 Mapper
 */
@Mapper
public interface ChatSessionMapper extends BaseMapper<ChatSession> {

    /**
     * 原子递增会话内消息序号
     *
     * 使用 SQL 自增保证并发安全，避免在应用层加锁
     */
    @Update("UPDATE chat_session SET ${column} = ${column} + 1 WHERE id = #{id}")
    int incrSeq(@Param("id") Long id, @Param("column") String column);

    /**
     * 累加未读数（避免读改写并发覆盖）
     */
    @Update("UPDATE chat_session SET user_unread = user_unread + #{delta} WHERE id = #{id}")
    int incrUserUnread(@Param("id") Long id, @Param("delta") int delta);

    @Update("UPDATE chat_session SET admin_unread = admin_unread + #{delta} WHERE id = #{id}")
    int incrAdminUnread(@Param("id") Long id, @Param("delta") int delta);
}
