using LYFZ.CloudServerData.Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;

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

        public bool BadgeClear(string cid, int badge = 0)
        {
            string sql = String.Format("update [dbo].[tb_GTMessagePushUser] set Badge={1} where GTClientID='{0}'", cid, badge);
            if (LYFZ.Helper.SQLHelper.ExecuteSql(sql, this.DBConn.GetDBConnectionString()) > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 用户未读消息统计 (Badge累加 1)
        /// </summary>
        /// <param name="cidList"></param>
        /// <returns></returns>
        public bool AddBadgeNumber(string[] cidList)
        {
            if (cidList.Length > 0)
            {
                string cidString = String.Join(",", cidList).Trim(',').Trim();
                if (!String.IsNullOrEmpty(cidString))
                {
                    string sql = String.Format("update [dbo].[tb_GTMessagePushUser] set Badge=Badge+1 where GTClientID in ('{0}')", cidString.Trim(',').Trim()).Replace(",", "','");
                    if (LYFZ.Helper.SQLHelper.ExecuteSql(sql, this.DBConn.GetDBConnectionString()) > 0)
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
                else
                {
                    return true;
                }
            }
            else
            {
                return true;
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="targetUser">目标用户 0 为全部用户 1 为企业用户 2 为特定用户 3 为指定用户(UserID) 的用户</param>
        /// <returns></returns>
        public List<PushSingleUser> GetPushSingleUserList(int targetUser, string targetList, string eid)
        {
            List<PushSingleUser> sUserList = new List<PushSingleUser>();
            //E1000
            targetList = targetList.Trim(',');
            string sql = String.Format("SELECT [GTType],[Badge],[UserID],[GTClientID] FROM [dbo].[tb_GTMessagePushUser]");
            DataTable tb = null;
            if (eid.ToLower() == "E1000".ToLower())
            {
                if (targetUser == 0)
                {
                    tb = LYFZ.Helper.SQLHelper.Query(String.Format("{0}  where UpdateTime>{1}", sql, DateTime.Now.AddDays(-90).ToJavaScriptTimeStamp()), this.DBConn.GetDBConnectionString()).Tables[0];
                }
                else
                {
                    tb = LYFZ.Helper.SQLHelper.Query(String.Format("{0}  where (EnterpriseID='{1}')", sql, targetList.Replace(":", "' and userid='").Replace(",", "') or ([EnterpriseID]='")), this.DBConn.GetDBConnectionString()).Tables[0];
                }
            }
            else
            {
                if (targetList.Trim(',').Trim().Length > 0 || targetUser == 0)
                {
                    switch (targetUser)
                    {
                        case 0:
                            tb = LYFZ.Helper.SQLHelper.Query(String.Format("{0}  where UpdateTime>{1}", sql, DateTime.Now.AddDays(-90).ToJavaScriptTimeStamp()), this.DBConn.GetDBConnectionString()).Tables[0];
                            break;
                        case 1:
                            tb = LYFZ.Helper.SQLHelper.Query(String.Format("{0}  where EnterpriseID in ('{1}')", sql, targetList.Replace(",", "','")), this.DBConn.GetDBConnectionString()).Tables[0];
                            break;
                        case 2:
                            tb = LYFZ.Helper.SQLHelper.Query(String.Format("{0}  where GTClientID in ('{1}')", sql, targetList.Replace(",", "','")), this.DBConn.GetDBConnectionString()).Tables[0];
                            break;
                        case 3:
                            tb = LYFZ.Helper.SQLHelper.Query(String.Format("{0}  where EnterpriseID='{2}' and UserID in ('{1}')", sql, targetList.Replace(",", "','"), eid), this.DBConn.GetDBConnectionString()).Tables[0];
                            break;
                    }
                }
            }
            if (tb != null)
            {
                foreach (DataRow row in tb.Rows)
                {
                    PushSingleUser sUser = new PushSingleUser();
                    sUser.GTType = Convert.ToInt32(row["GTType"].ToString());
                    sUser.GTClientID = row["GTClientID"].ToString();
                    sUser.Badge = Convert.ToInt32(row["Badge"].ToString());
                    sUser.UserID = row["UserID"].ToString();
                    sUserList.Add(sUser);
                }
            }
            return sUserList;
        }

    }
}