using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;

namespace LYFZ.WanYuKeFuData.DAL
{
   public class GTMsgRecord:BaseDataOperate<LYFZ.WanYuKeFuData.Model.GTMsgRecord>
    {
       public GTMsgRecord(LYFZ.WanYuKeFuData.Model.DBConnection dbConn)
       {
           base.DBConn = dbConn;
       }

       /// <summary>
       /// 删除历史消息记录 
       /// </summary>
       /// <param name="time"></param>
       /// <returns></returns>
       public bool DelMsgHistoryRecord(long time)
       {
           try
           {
               string sql = String.Format("delete [dbo].[tb_GTMsgRecord] where  [sendTime] <{0}", time);
               LYFZ.Helper.SQLHelper.ExecuteSql(sql, this.DBConn.GetDBConnectionString());

               string sql2 = String.Format("delete [dbo].[tb_MsgReadStatus] where  [ReadTime] <{0}", time);
               LYFZ.Helper.SQLHelper.ExecuteSql(sql2, this.DBConn.GetDBConnectionString());
               return true;
           }
           catch {
               return false;
           }
       }
       /// <summary>
       /// 获取未读消息记录
       /// </summary>
       /// <param name="eid"></param>
       /// <param name="uid"></param>
       /// <returns></returns>
       public int GetUnreadCount(string eid, string uid, string Platform)
       {
           string sql1 = String.Format("select count(1) as ReadCount from [tb_MsgReadStatus] left join [tb_GTMsgRecord] on [tb_MsgReadStatus].[msgID]=[tb_GTMsgRecord].ID where [UserID]='{0}' and ([EnterpriseID]='{1}' or [EnterpriseID]='E1000') and [Platform] like '%{2}%'", uid, eid, Platform);
           string eidAndUid = String.Format("{0}:{1}",eid,uid);
           string sql2 = String.Format("SELECT count(id) as msgCount FROM [tb_GTMsgRecord] where (EnterpriseID='{0}' or EnterpriseID='E1000') and (TargetUser <=1 or [targetList] like '%,{1},%' or [targetList] like '%,{3},%')  and [Platform] like '%{2}%'", eid, uid, Platform, eidAndUid);
           int msgCount = Convert.ToInt32(LYFZ.Helper.SQLHelper.GetSingle(sql2, this.DBConn.GetDBConnectionString()));
           int ReadCount = Convert.ToInt32(LYFZ.Helper.SQLHelper.GetSingle(sql1, this.DBConn.GetDBConnectionString()));
           int unreadCount = msgCount - ReadCount;
           return unreadCount >= 0 ? unreadCount : 0;
       }
       /// <summary>
       /// 获取指定用户已读消息iD集合
       /// </summary>
       /// <param name="eid"></param> 
       /// <param name="uid"></param>
       /// <returns></returns>
       public List<string> GetReadMsIDList(string eid, string uid, string Platform)
       {
           string sql = String.Format("select [msgID] from [tb_MsgReadStatus] left join [tb_GTMsgRecord] on [tb_MsgReadStatus].[msgID]=[tb_GTMsgRecord].ID where [UserID]='{0}' and ([EnterpriseID]='{1}'  or EnterpriseID='E1000') and [Platform] like '%{2}%'", uid, eid, Platform);
           DataTable tb = LYFZ.Helper.SQLHelper.Query(sql, this.DBConn.GetDBConnectionString()).Tables[0];
           List<string> msgIDList = new List<string>();
           foreach (DataRow row in tb.Rows)
           {
               msgIDList.Add(row["msgID"].ToString());
           }
           return msgIDList;
       }
    }
}