using System;
using System.Collections.Generic;
using System.Text;
using JuSNS.Factory;
using JuSNS.Model;
using System.Data.SqlClient;
using JuSNS.Profile;
using System.Data;
using JuSNS.Config;
namespace JuSNS.SQLServer
{
    public class OnlineUser : DbBase, IOnlineUser
    {
        /// <summary>
        /// 是否在线
        /// </summary>
        /// <param name="userid">用户ID</param>
        /// <returns></returns>
        public bool IsOnline(int userid)
        {
            SqlParameter parm = new SqlParameter("@UserID", SqlDbType.Int, 4);
            parm.Value = userid;
            int ri = Convert.ToInt32(DbHelper.ExecuteScalar(CommandType.StoredProcedure, "JuSNS_IsOnline", parm));
            if (ri > 0)
            {
                return true;
            }
            return false;
        }
        /// <summary>
        /// 取得登陆信息
        /// </summary>
        /// <returns></returns>
        public OnlineUserInfo getInfo(int id)
        {
            OnlineUserInfo info = new OnlineUserInfo();
            SqlParameter parm = new SqlParameter("@id", SqlDbType.Int, 4);
            parm.Value = id;
            IDataReader dr = DbHelper.ExecuteReader(CommandType.StoredProcedure, "JuSNS_OnlineGetInfo", parm);
            if (dr.Read())
            {
                info.UserID = dr.IsDBNull(0) ? 0 : dr.GetInt32(0);
                info.UserName = dr.IsDBNull(1) ? "" : dr.GetString(1);
                info.LastIP = dr.IsDBNull(2) ? "" : dr.GetString(2);
                info.LastURL = dr.IsDBNull(3) ? "" : dr.GetString(3);
                info.ID = dr.IsDBNull(4) ? 0 : dr.GetInt32(4);
                info.LastTime = dr.IsDBNull(5) ? DateTime.MinValue : dr.GetDateTime(5);
            }
            return info;
        }
        /// <summary>
        /// 取得登陆信息
        /// </summary>
        /// <returns></returns>
        public OnlineUserInfo getInfo(int userid,bool isuserid)
        {
            if (!isuserid)
                return getInfo(userid);
            OnlineUserInfo info = new OnlineUserInfo();
            SqlParameter parm = new SqlParameter("@UserID", SqlDbType.Int, 4);
            parm.Value = userid;
            IDataReader dr = DbHelper.ExecuteReader(CommandType.StoredProcedure, "JuSNS_OnlineGetInfo1", parm);
            if (dr.Read())
            {
                info.UserID = dr.IsDBNull(0) ? 0 : dr.GetInt32(0);
                info.UserName = dr.IsDBNull(1) ? "" : dr.GetString(1);
                info.LastIP = dr.IsDBNull(2) ? "" : dr.GetString(2);
                info.LastURL = dr.IsDBNull(3) ? "" : dr.GetString(3);
                info.ID = dr.IsDBNull(4) ? 0 : dr.GetInt32(4);
                info.LastTime = dr.IsDBNull(5) ? DateTime.MinValue : dr.GetDateTime(5);
            }
            return info;
        }
        /// <summary>
        /// 添加在线用户
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        public int Add(OnlineUserInfo info)
        {
            SqlParameter parm1 = new SqlParameter("@LastIP", SqlDbType.NVarChar, 18);
            SqlParameter[] parm = getParameters(info);
            SqlConnection Conn = new SqlConnection(DBConfig.CnString);
            Conn.Open();
            SqlTransaction trans = Conn.BeginTransaction();
            try
            {
                DbHelper.ExecuteNonQuery(trans, CommandType.StoredProcedure, "JuSNS_OnlineDeleteUser", parm1);
                int rn = DbHelper.ExecuteNonQuery(trans, CommandType.StoredProcedure, "JuSNS_OnlineAddUser", parm);                
                trans.Commit();
                return rn;
            }
            catch (SqlException e)
            {
                trans.Rollback();
                throw e;
            }
            finally
            {
                if (Conn.State == ConnectionState.Open)
                    Conn.Close();
            }              
        }
        /// <summary>
        /// 更新在线用户
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        public int Update(OnlineUserInfo info)
        {
            SqlParameter[] param = new SqlParameter[5];
            param[0] = new SqlParameter("@UserID", SqlDbType.Int, 4);
            param[0].Value = info.UserID;
            param[1] = new SqlParameter("@UserName", SqlDbType.NVarChar, 50);
            param[1].Value = info.UserName;
            param[2] = new SqlParameter("@LastIP", SqlDbType.NVarChar, 18);
            param[2].Value = info.LastIP;
            param[3] = new SqlParameter("@LastUrl", SqlDbType.NVarChar, 256);
            param[3].Value = info.LastURL;
            param[4] = new SqlParameter("@LastTime", SqlDbType.DateTime, 8);
            param[4].Value = info.LastTime;
            return DbHelper.ExecuteNonQuery(CommandType.StoredProcedure, "JuSNS_OnlineUserUpdate", param);
        }
        /// <summary>
        /// 删除在线用户
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int Delete(int id)
        {
            SqlParameter parm = new SqlParameter("@id", SqlDbType.Int, 4);
            parm.Value = id;
            return DbHelper.ExecuteNonQuery(CommandType.StoredProcedure, "JuSNS_OnlineDeleteUser1", parm);
        }
        /// <summary>
        /// 删除在线用户
        /// </summary>
        /// <param name="userid"></param>
        /// <param name="isuserid"></param>
        /// <returns></returns>
        public int Delete(int userid,bool isuserid)
        {
            if (!isuserid)
                return Delete(userid);
            SqlParameter parm = new SqlParameter("@userid", SqlDbType.Int, 4);
            parm.Value = userid;
            return DbHelper.ExecuteNonQuery(CommandType.StoredProcedure, " JuSNS_OnlineDeleteUser2", parm);
        }
        /// <summary>
        /// 取得在线人数
        /// </summary>
        /// <returns></returns>
        public int Count()
        {
            return Convert.ToInt32(DbHelper.ExecuteScalar(CommandType.StoredProcedure, "JuSNS_IsOnline1", null));
        }
        /// <summary>
        /// 检查在线
        /// </summary>
        /// <param name="userid"></param>
        /// <param name="lastip"></param>
        /// <returns></returns>
        public bool CheckOnline(int userid, string lastip)
        {
            SqlParameter[] param = new SqlParameter[2];
            param[0] = new SqlParameter("@UserID", SqlDbType.Int, 4);
            param[0].Value = userid;
            param[1] = new SqlParameter("@LastIP", SqlDbType.NVarChar, 18);
            param[1].Value = lastip;
            int rn = Convert.ToInt32(DbHelper.ExecuteScalar(CommandType.StoredProcedure, "JuSNS_OnlineCheckOnline", param));
            return rn > 0 ? true : false;
        }
        /// <summary>
        /// 删除过期用户
        /// </summary>
        public void DeleteExpires()
        {
            SqlParameter param = new SqlParameter("@LastTime", SqlDbType.DateTime, 8);
            param.Value = DateTime.Now.AddMinutes(-20);
            DbHelper.ExecuteNonQuery(CommandType.StoredProcedure, "JuSNS_DeleteUnOnlineUser", param);
        }
        /// <summary>
        /// 在线列表
        /// </summary>
        /// <returns></returns>
        public List<OnlineUserInfo> OnlineList()
        {
            List<OnlineUserInfo> infolist = new List<OnlineUserInfo>();
            SqlParameter param = new SqlParameter("@LastTime", SqlDbType.DateTime, 8);
            param.Value = DateTime.Now.AddMinutes(-20);
            IDataReader dr = DbHelper.ExecuteReader(CommandType.StoredProcedure, "JuSNS_OnlineList", param);
            while (dr.Read())
            {
                OnlineUserInfo info = new OnlineUserInfo();
                info.UserID = dr.IsDBNull(0) ? 0 : dr.GetInt32(0);
                info.UserName = dr.IsDBNull(1) ? "" : dr.GetString(1);
                info.LastIP = dr.IsDBNull(2) ? "" : dr.GetString(2);
                info.LastURL = dr.IsDBNull(3) ? "" : dr.GetString(3);
                info.ID = dr.IsDBNull(4) ? 0 : dr.GetInt32(4);
                info.LastTime = dr.IsDBNull(5) ? DateTime.MinValue : dr.GetDateTime(5);
                infolist.Add(info);
            }
            dr.Dispose();
            return infolist;
        }
        /// <summary>
        /// SQL参数
        /// </summary>
        /// <param name="Info">当前在线用户信息实体类</param>
        /// <returns></returns>
        private SqlParameter[] getParameters(OnlineUserInfo Info)
        {
            SqlParameter[] param = new SqlParameter[5];
            //param[0] = new SqlParameter("@ID", SqlDbType.Int, 4);
            //param[0].Value = Info.ID;
            param[0] = new SqlParameter("@UserID", SqlDbType.Int, 4);
            param[0].Value = Info.UserID;
            param[1] = new SqlParameter("@UserName", SqlDbType.NVarChar, 50);
            param[1].Value = Info.UserName;
            param[2] = new SqlParameter("@LastIP", SqlDbType.NVarChar, 18);
            param[2].Value = Info.LastIP;
            param[3] = new SqlParameter("@LastUrl", SqlDbType.NVarChar, 256);
            param[3].Value = Info.LastURL;
            param[4] = new SqlParameter("@LastTime", SqlDbType.DateTime, 8);
            param[4].Value = Info.LastTime;           
            return param;
        }
    }
}
