using System;
using System.Collections.Generic;
using System.Text;
using JuSNS.Factory;
using System.Data;
using System.Data.SqlClient;
using JuSNS.Profile;
using JuSNS.Config;
using JuSNS.Model;
using JuSNS.Common;

namespace JuSNS.SQLServer
{
    public class JUser : DbBase, IJUser
    {
        public IDataReader GetMoodInfo(int UserID)
        {
            SqlParameter param = new SqlParameter("@UserID", UserID);
            return DbHelper.ExecuteReader(CommandType.StoredProcedure, "JuSNS_GetTopMood", param);
        }

        public DataTable GetMoodIDInfo(int ID)
        {
            SqlParameter param = new SqlParameter("@ID", ID);
            return DbHelper.ExecuteTable(CommandType.StoredProcedure, "JuSNS_GetTopIDMood", param);
        }

        public DataTable GetBlogIDInfo(int ID)
        {
            SqlParameter param = new SqlParameter("@ID", ID);
            return DbHelper.ExecuteTable(CommandType.StoredProcedure, "JuSNS_GetTopIDBlog", param);
        }

        public DataTable GetDynInfoId(int ID, int flg)
        {
            SqlParameter[] param = new SqlParameter[2];
            param[0] = new SqlParameter("@ID", SqlDbType.Int, 4);
            param[0].Value = ID;
            param[1] = new SqlParameter("@Flg", SqlDbType.Int, 4);
            param[1].Value = flg;
            return DbHelper.ExecuteTable(CommandType.StoredProcedure, "JuSNS_GetDynInfoId", param);
        }

        /// <summary>
        /// 获取通知数量（未读）
        /// </summary>
        /// <param name="UserID"></param>
        /// <returns></returns>
        public int NoticeInfo(int UserID)
        {
            SqlParameter param = new SqlParameter("@UserID", UserID);
            return Convert.ToInt32(DbHelper.ExecuteScalar(CommandType.StoredProcedure, "JuSNS_noteinfo", param));
        }

        /// <summary>
        /// 获取好友请求数量
        /// </summary>
        /// <param name="UserID">被请求的用户ID</param>
        public int FriendRequest(int UserID, int sType)
        {
            SqlParameter[] param = new SqlParameter[2];
            param[0] = new SqlParameter("@UserID", SqlDbType.Int, 4);
            param[0].Value = UserID;
            param[1] = new SqlParameter("@type", SqlDbType.Int, 4);
            param[1].Value = sType;

            return Convert.ToInt32(DbHelper.ExecuteScalar(CommandType.StoredProcedure, "JuSNS_FriendNote", param));
        }

        public int GetNoteNumber(int UserID)
        {
            SqlParameter[] param = new SqlParameter[2];
            param[0] = new SqlParameter("@UserID", SqlDbType.Int, 4);
            param[0].Value = UserID;
            param[1] = new SqlParameter("@noteDate", SqlDbType.Int, 4);
            param[1].Value = Convert.ToInt32(JuSNS.Config.UiConfig.noteDate);
            int n = Convert.ToInt32(DbHelper.ExecuteScalar(CommandType.StoredProcedure, "JuSNS_GetNoteNumber", param));
            return n;
        }

        /// <summary>
        /// 获取日志足迹
        /// </summary>
        /// <param name="UserID">用户名</param>
        /// <param name="Num">调用数量</param>
        /// <returns></returns>
        public IDataReader BlogFootList(int UserID, int Num)
        {
            SqlParameter[] param = new SqlParameter[2];
            param[0] = new SqlParameter("@UserID", SqlDbType.Int, 4);
            param[0].Value = UserID;
            param[1] = new SqlParameter("@Num", SqlDbType.Int, 4);
            param[1].Value = Num;
            return DbHelper.ExecuteReader(CommandType.StoredProcedure, "JuSNS_BlogFoot", param);
        }

        public IDataReader blogFootID(int BlogID, int Num, int UserID)
        {
            SqlParameter[] param = new SqlParameter[3];
            param[0] = new SqlParameter("@BlogID", SqlDbType.Int, 4);
            param[0].Value = BlogID;
            param[1] = new SqlParameter("@Num", SqlDbType.Int, 4);
            param[1].Value = Num;
            param[2] = new SqlParameter("@UserID", SqlDbType.Int, 4);
            param[2].Value = UserID;
            return DbHelper.ExecuteReader(CommandType.StoredProcedure, "JuSNS_BlogFootID", param);
        }

        /// <summary>
        /// 获得某用户的其他日志
        /// </summary>
        /// <param name="UserID">用户ID</param>
        /// <param name="blogID">当前所在的日志ID</param>
        /// <param name="Num">调用数量</param>
        /// <returns></returns>
        public IDataReader BlogMoreID(int UserID, int blogID, int Num)
        {
            SqlParameter[] param = new SqlParameter[3];
            param[0] = new SqlParameter("@BlogID", SqlDbType.Int, 4);
            param[0].Value = blogID;
            param[1] = new SqlParameter("@UserID", SqlDbType.Int, 4);
            param[1].Value = UserID;
            param[2] = new SqlParameter("@Num", SqlDbType.Int, 4);
            param[2].Value = Num;
            return DbHelper.ExecuteReader(CommandType.StoredProcedure, "JuSNS_BlogMore", param);
        }

        /// <summary>
        /// 获取用户实体类
        /// </summary>
        public UserInfo GetUserInfo(int UserID)
        {
            UserInfo UInfo = new UserInfo();
            UInfo.UserID = UserID;
            SqlParameter param = new SqlParameter("@UserID", UserID);
            IDataReader rd = DbHelper.ExecuteReader(CommandType.StoredProcedure, "JuSNS_UserInfo", param);
            if (rd.Read())
            {
                UInfo.Email = rd["Email"].ToString();
                UInfo.Password = rd["Password"].ToString();
                UInfo.Username = rd["Username"].ToString();
                UInfo.State = (EnumUserState)(int)rd["State"];
                if (rd["LastLoginTime"] != DBNull.Value) UInfo.LastLoginTime = (DateTime)rd["LastLoginTime"];
                if (rd["LastLoginIp"] != DBNull.Value) UInfo.LastLoginIp = rd["LastLoginIp"].ToString();
                UInfo.LoginTimes = (int)rd["LoginTimes"];
                UInfo.Province = (int)rd["Province"];
                if (rd["City"] != DBNull.Value) UInfo.City = (int)rd["City"];
                UInfo.RegTime = (DateTime)rd["RegTime"];
                UInfo.RegIP = rd["RegIP"].ToString();
                if (rd["Portrait"] != DBNull.Value) UInfo.Portrait = (int)rd["Portrait"];
                if (rd["FilePath"] != DBNull.Value) UInfo.PortraitDir = (string)rd["FilePath"];
            }
            rd.Close();
            return UInfo;
        }

        public int AddPoke(int UserID, int FriendID, int gType, int isPub, string poketo, string pokefrom)
        {
            SqlConnection Conn = new SqlConnection(DBConfig.CnString);
            Conn.Open();
            SqlTransaction trans = Conn.BeginTransaction();
            try
            {
                int n = 0;
                SqlParameter[] param = new SqlParameter[9];
                param[0] = new SqlParameter("@UserID", SqlDbType.Int, 4);
                param[0].Value = UserID;
                param[1] = new SqlParameter("@FriendID", SqlDbType.Int, 4);
                param[1].Value = FriendID;
                param[2] = new SqlParameter("@PostTime", SqlDbType.DateTime, 8);
                param[2].Value = DateTime.Now;
                param[3] = new SqlParameter("@PostIP", SqlDbType.Char, 15);
                param[3].Value = JuSNS.Common.Public.GetClientIP();
                param[4] = new SqlParameter("@IsRead", SqlDbType.TinyInt);
                param[4].Value = 0;
                param[5] = new SqlParameter("@gType", SqlDbType.Int, 4);
                param[5].Value = gType;
                param[6] = new SqlParameter("@isPub", SqlDbType.TinyInt);
                param[6].Value = isPub;
                param[7] = new SqlParameter("@poketo", SqlDbType.NVarChar, 50);
                param[7].Value = poketo;
                param[8] = new SqlParameter("@pokefrom", SqlDbType.NVarChar, 50);
                param[8].Value = pokefrom;
                SqlParameter par = new SqlParameter("@UserID", UserID);
                int PokeNum = Convert.ToInt32(DbHelper.ExecuteScalar(trans, CommandType.StoredProcedure, "JuSNS_PokeNum", par));
                if (PokeNum > Convert.ToInt32(JuSNS.Common.Public.GetXMLValue("pokeNumber")))
                {
                    return -100;
                }
                else
                {
                    n= Convert.ToInt32(DbHelper.ExecuteScalar(trans, CommandType.StoredProcedure, "JuSNS_AddPoke", param).ToString());
                    if (isPub == 1)
                    {
                        JUser ju = new JUser();
                        if (ju.CheckPrivacyInfo(trans, UserID, "ActLeaveWord"))
                        {
                            int m = UserDyn(trans, 0, 0, "poke", UserID, FriendID, n, "打招呼", gType.ToString());
                        }
                    }
                }
                trans.Commit();
                return n;
            }
            catch (SqlException e)
            {
                trans.Rollback();
                throw e;
            }
            finally
            {
                if (Conn.State == ConnectionState.Open)
                    Conn.Close();
            }

        }

        public int ChanagePokeState(int ID,int UserID)
        {
            SqlParameter[] param = new SqlParameter[2];
            param[0] = new SqlParameter("@ID", SqlDbType.Int, 4);
            param[0].Value = ID;
            param[1] = new SqlParameter("@UserID", SqlDbType.Int, 4);
            param[1].Value = UserID;
            return DbHelper.ExecuteNonQuery(CommandType.StoredProcedure, "JuSNS_PokeUpdateChangeState", param);
        }

        public int DelPoke(int ID, int UserID)
        {
            SqlParameter[] param = new SqlParameter[2];
            param[0] = new SqlParameter("@UserID", SqlDbType.Int, 4);
            param[0].Value = UserID;
            param[1] = new SqlParameter("@ID", SqlDbType.Int, 4);
            param[1].Value = ID;
            return DbHelper.ExecuteNonQuery(CommandType.StoredProcedure, "JuSNS_DelPoke", param);
        }

        /// <summary>
        /// 得到在线好友。
        /// </summary>
        /// <param name="UserID"></param>
        /// <returns></returns>
        public DataTable GetOnlineNumber(int UserID)
        {
            SqlParameter[] param = new SqlParameter[2];
            param[0] = new SqlParameter("@UserID", SqlDbType.Int, 4);
            param[0].Value = UserID;
            param[1] = new SqlParameter("@Num", SqlDbType.Int, 4);
            param[1].Value = 20;
            return DbHelper.ExecuteTable(CommandType.StoredProcedure, "JuSNS_OnlineNumber", param);
        }

        public bool CheckPrivacyInfo(SqlTransaction trans,int UserID,string Type)
        {
            string sql = "select " + Type + " from NT_usersetting where UserID=" + UserID;
            int n = Convert.ToInt32(DbHelper.ExecuteScalar(trans,CommandType.Text, sql, null));
            if (n == 1)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public int GetUserCount()
        {
            string sql = "select count(UserID) from NT_user";
            return Convert.ToInt32(DbHelper.ExecuteScalar(CommandType.Text, sql, null));
        }
    }
}
