//MessageId,MasterUserId,GuestUserId,MessageContent,MessageTime,MessageDelFlag
        /// <summary>
        /// 根据用户传来的id,返回在此id下的最近三条留言
        /// </summary>
        /// <param name="userId">用户id</param>
        /// <returns>MessageInfo集合</returns>
        public List<MessageInfo> GetLastThreeMessageByUserId(int userId)
        {
            string sql = "select top 3 * from MessageInfo where MasterUserId=@MasterUserId and MessageDelFlag=0  order by MessageTime DESC";//根据MessageId进行降序排序
            DataTable dt = SqlHelper.ExecuteTable(sql, new OleDbParameter("@MasterUserId", userId));
            List<MessageInfo> list = new List<MessageInfo>();

            if (dt.Rows.Count>0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    MessageInfo message = new MessageInfo();
                    message.GuestUserId = Convert.ToInt32(dr["GuestUserId"]);
                    message.MasterUserId = Convert.ToInt32(dr["MasterUserId"]);
                    message.MessageContent = dr["MessageContent"].ToString();
                    message.MessageId = Convert.ToInt32(dr["MessageId"]);
                    message.MessageTime = Convert.ToDateTime(dr["MessageTime"]);
                    list.Add(message);
                }
            }
            return list;
        }
 /// <summary>
 /// 向数据库中插入一条留言
 /// </summary>
 /// <param name="mesInfo">留言信息</param>
 /// <returns>受影响的行数</returns>
 public int InsertMessageInfo(MessageInfo mesInfo)
 {
     return messDAL.InsertMessageInfo(mesInfo);
 }
        /// <summary>
        /// 根据用户id和以经显示的条数,来查找将要显示的消息
        /// </summary>
        /// <param name="topNum">以经显示的条数</param>
        /// <param name="userId">用户id</param>
        /// <returns>将要显示的消息</returns>
        public List<MessageInfo> GetMessageByPageNum(int topNum,int userId)
        {
            string sql = null;
            if (topNum>0)
            {
                sql = "select top 3 MessageId,MasterUserId,GuestUserId,MessageContent,MessageTime from [select * from messageinfo where MasterUserId=@MasterUserId and MessageDelFlag=0 order by messagetime desc ]. as a where messageid not in (select top " + topNum + " messageid from messageinfo where MasterUserId=@mUserId and MessageDelFlag=0 order by messagetime desc ) ";
            }
            else
            {
                sql = "select top 3 MessageId,MasterUserId,GuestUserId,MessageContent,MessageTime from MessageInfo where  MasterUserId=@MasterUserId and MessageDelFlag=0 order by MessageTime desc";
            }

            OleDbParameter[] param = {
                                     new OleDbParameter("@MasterUserId",userId),
                                     new OleDbParameter("@mUserId",userId)

                                     };
            List<MessageInfo> list = new List<MessageInfo>();
            DataTable dt=SqlHelper.ExecuteTable(sql,param);
            if (dt.Rows.Count>0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    MessageInfo mes = new MessageInfo();
                    mes = RowToMessageInfo(dr);
                    list.Add(mes);
                }
            }
            return list;
        }
 /// <summary>
 /// 将messageinfo行转换为messageInfo对象
 /// </summary>
 /// <param name="dr">messageinfo行</param>
 /// <returns>messageInfo对象</returns>
 private MessageInfo RowToMessageInfo(DataRow dr)
 {
     MessageInfo mes = new MessageInfo();
     mes.GuestUserId = Convert.ToInt32(dr["GuestUserId"]);
     mes.MasterUserId = Convert.ToInt32(dr["MasterUserId"]);
     //mes.MessageDelFlag = Convert.ToInt32(dr["MessageDelFlag"]);
     mes.MessageId = Convert.ToInt32(dr["MessageId"]);
     mes.MessageTime = Convert.ToDateTime(dr["MessageTime"]);
     mes.MessageContent = dr["MessageContent"].ToString();
     return mes;
 }
 /// <summary>
 /// 向数据库中插入一条留言
 /// </summary>
 /// <param name="mesInfo">留言信息</param>
 /// <returns>受影响的行数</returns>
 public int InsertMessageInfo(MessageInfo mesInfo)
 {
     // string sql = "insert into MessageInfo (MasterUserId,GuestUserId,MessageContent,MessageTime,MessageDelFlag) values (@MasterUserId,@GuestUserId,@MessageContent,@MessageTime,@MessageDelFlag)";
     string sql = "insert into MessageInfo (MasterUserId,GuestUserId,MessageContent,MessageTime,MessageDelFlag) values (" + mesInfo.MasterUserId + "," + mesInfo.GuestUserId + ",'" + mesInfo.MessageContent + "','" + mesInfo.MessageTime + "'," + mesInfo.MessageDelFlag + ")";
     //OleDbParameter[] param = {
     //                         new OleDbParameter("@MasterUserId",mesInfo.MasterUserId),
     //                         new OleDbParameter("@GuestUserId",mesInfo.GuestUserId),
     //                         new OleDbParameter("@MessageContent",mesInfo.MessageContent),
     //                         new OleDbParameter("@MessageTime",mesInfo.MessageTime),
     //                         new OleDbParameter("@MessageDelFlag",mesInfo.MessageDelFlag)
     //                         };
     return SqlHelper.ExecuteNonQuery(sql);
 }
        /// <summary>
        /// 跟据返回的留言信息想第三个group中添加信息
        /// </summary>
        /// <param name="messageInfo">留言详细信息</param>
        private void GetThirdGroupSetText(MessageInfo messageInfo,int count)
        {
            int userId = messageInfo.GuestUserId;
            lab3MesId.Text = messageInfo.MessageId.ToString();
            lab3GuestName.Text = GetUserNameByUserId(userId);
            lab3Content.Text = messageInfo.MessageContent;
            lab3Time.Text = messageInfo.MessageTime.ToString();

            int floorNum = 0;//PageNum == 0 &&
            if (Convert.ToInt32(labMesCount.Text) <= 3)
            {
                floorNum = count - 2;
            }
            else
            {
                floorNum = Convert.ToInt32(labMesCount.Text) - PageNum * 3-2;

            }
            lab3Floor.Text = "第" + floorNum + "楼";
        }
 /// <summary>
 /// 跟据返回的留言信息想第一个group中添加信息
 /// </summary>
 /// <param name="messageInfo">留言详细信息</param>
 private void GetFirstGroupSetText(MessageInfo messageInfo,int count)
 {
     int userId = messageInfo.GuestUserId;
     labMesId.Text = messageInfo.MessageId.ToString();
     labGuestName.Text = GetUserNameByUserId(userId);
     labContent.Text = messageInfo.MessageContent;
     labTime.Text = messageInfo.MessageTime.ToString();
     //判断PageNum是否为0
     int floorNum = 0;//PageNum == 0 &&
     if (Convert.ToInt32(labMesCount.Text) <= 3)
     {
         floorNum = count;
     }
     else
     {
         floorNum = Convert.ToInt32(labMesCount.Text) - PageNum * 3;
     }
     labFloor.Text = "第"+floorNum+"楼";
 }
        /// <summary>
        /// 发表留言
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnPublish_Click(object sender, EventArgs e)
        {
            if (cmbUser.SelectedIndex>=1)
            {
                //获得用户给谁留言  获得留言用户的id
                int masterUserId = Convert.ToInt32(cmbUser.SelectedValue);
                //获得用户输入的信息
                string mes = txtContent.Text;
                if (!string.IsNullOrEmpty(mes))
                {
                    //获得到当前用户的id
                    int guestUserId = Convert.ToInt32(labId.Text);
                    MessageInfo mesInfo = new MessageInfo();
                    mesInfo.GuestUserId = guestUserId;
                    mesInfo.MasterUserId = masterUserId;
                    mesInfo.MessageContent = mes;
                    mesInfo.MessageDelFlag = 0;
                    mesInfo.MessageTime = DateTime.Now;

                    string msg = mesBLL.InsertMessageInfo(mesInfo) > 0 ? "留言成功" : "留言失败";
                    txtContent.Text = "";
                    MessageBox.Show(msg);
                    //加载消息
                    LoadMessageInfo(Convert.ToInt32(labId.Text));
                }
                else
                {
                    MessageBox.Show("请输入留言内容");
                }
            }
            else
            {
                MessageBox.Show("请选择您想要留言的用户");
            }
        }