1

    /// <summary>
    /// 聊天室实体
    /// </summary>
    public class ChatDTO
    {
        /// <summary>
        /// 聊天室ID
        /// </summary>
        public Guid ChatID { get; set; }
        /// <summary>
        /// 访客
        /// </summary>
        public Visitor Visitor { get; set; }
        /// <summary>
        /// 客服
        /// </summary>
        public List<Agent> Agents { get; set; }
        public DateTime CreateTime { get; set; }
    }

    /// <summary>
    /// visitor
    /// </summary>
    public class Visitor
    {
        public int ID { get; set; }
        public string Name { get; set; }
    }

    /// <summary>
    /// agent
    /// </summary>
    public class Agent
    {
        public int ID { get; set; }
        public string Name { get; set; }
    }

    /// <summary>
    /// 消息实体
    /// </summary>
    public class Messages
    {
        //主键
        public Guid MID { get; set; }
        //聊天室ID
        public Guid ChatID { get; set; }
        //发送者的ID
        public int ID { get; set; }
        //发送者角色,客服or访客
        public int Role { get; set; }
        //消息内容
        public string Content { get; set; }
        //发送时间
        public DateTime SendTime { get; set; }
    }

    public interface IChatService
    {
        /// <summary>
        /// 创建聊天室
        /// </summary>
        /// <returns></returns>
        Guid CreateChat(Visitor visitor);

        /// <summary>
        /// 关闭聊天室
        /// </summary>
        /// <param name="chatid"></param>
        /// <returns></returns>
        bool CloseChat(Guid chatid);

        /// <summary>
        /// 添加客服
        /// </summary>
        /// <param name="agent"></param>
        /// <returns></returns>
        bool AddAgent(Agent agent);

        /// <summary>
        /// 客服离开
        /// </summary>
        /// <param name="id">客服ID</param>
        /// <returns></returns>
        bool RemoveAgent(int id);

        /// <summary>
        /// 发送消息
        /// </summary>
        /// <param name="chatid">聊天室ID</param>
        /// <param name="id">发送者的ID</param>
        /// <param name="role">发送者角色,客服or访客</param>
        /// <param name="content">消息内容</param>
        void SendMsg(Guid chatid, int id, int role, string content);
    }

    public class ChatService : IChatService
    {
        //当前存在的聊天室
        List<ChatDTO> chatDTOs = new List<ChatDTO>();
        //已发送的消息
        List<Messages> messages = new List<Messages>();

        public bool AddAgent(Agent agent)
        {
            throw new NotImplementedException();
        }

        public bool CloseChat(Guid chatid)
        {
            throw new NotImplementedException();
        }

        public Guid CreateChat(Visitor visitor)
        {
            var curChat = chatDTOs.Where(c => c.Visitor == visitor).FirstOrDefault();
            if (curChat != null)
            {
                return curChat.ChatID;
            }
            else
            {
                ChatDTO chat = new ChatDTO()
                {
                    ChatID = Guid.NewGuid(),
                    Visitor = visitor,
                    CreateTime = DateTime.Now
                };
                chatDTOs.Add(chat);
                return chat.ChatID;
            }
        }

        public bool RemoveAgent(int id)
        {
            throw new NotImplementedException();
        }

        public void SendMsg(Guid chatid, int id, int role, string content)
        {
            throw new NotImplementedException();
        }
    }
原文地址:https://www.cnblogs.com/2333hh/p/13357120.html