using System;
using System.Collections.Generic;
using System.Text;
using JuSNS.Factory.ApiInterface;
using JuSNS.Model.ApiInterface;
using System.Data.SqlClient;
using System.Data;
using JuSNS.Profile;
using JuSNS.Config;
namespace JuSNS.SQLServer.ApiInterface
{
    public class Activity:DbBase,IActivity
    {
        #region IActivity 成员

        /// <summary>
        /// 添加动态信息
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        public int Add(AppActiveInfo info)
        {
            SqlConnection Conn = new SqlConnection(DBConfig.CnString);
            Conn.Open();
            SqlTransaction trans = Conn.BeginTransaction();
            try
            {
                int n = 0;
                SqlParameter[] param = GetParameters(info);
                string sql = "Insert into App_Activity(UserID,Content,CreationTime,AppID,Params) values(@UserID,@Content,@CreationTime,@AppID,@Params)";
                n = Convert.ToInt32(DbHelper.ExecuteNonQuery(trans, CommandType.Text, sql, param));
                JUser ju = new JUser();
                if (ju.CheckPrivacyInfo(trans, info.UserID, "ActSecede"))
                {
                    UserDyn(trans, 0, info.AppID, "App", info.UserID, 0, n, info.Content, n.ToString());
                }
                trans.Commit();
                return n;
            }
            catch
            {
                trans.Rollback();
                throw;
            }
            finally
            {
                if (Conn.State == ConnectionState.Open)
                    Conn.Close();
            }
        }
        /// <summary>
        /// 删除动态
        /// </summary>
        /// <param name="AppID"></param>
        /// <param name="ActiveID"></param>
        /// <returns></returns>
        public int Del(int AppID,int ActiveID)
        {
            SqlParameter[] param =
                {
                    new SqlParameter("@ActiveID", ActiveID),
                    new SqlParameter("@AppID",AppID)
                };
            string sql = "delete from App_Activity where ActiveID=@ActiveID and AppID=@AppID";
            return DbHelper.ExecuteNonQuery(CommandType.Text, sql, param);
        }
        /// <summary>
        /// 取得某人的动态
        /// </summary>
        /// <param name="AppID"></param>
        /// <param name="UserID"></param>
        /// <param name="Num"></param>
        /// <returns></returns>
        public List<AppActiveInfo> GetList(int AppID, int UserID, int Num)
        {
            List<AppActiveInfo> infolist = new List<AppActiveInfo>();
            SqlParameter[] param =
                {
                    new SqlParameter("@AppID",AppID),
                    new SqlParameter("@UserID",UserID)
                };
            string topStr = string.Empty;
            if (Num != 0)
            {
                topStr = " top " + Num + " ";
            }
            string sql = "select " + topStr + " ActiveID,UserID,Content,CreationTime,AppID,Params from App_Activity where AppID=@AppID and UserID=@UserID ";
            IDataReader dr = DbHelper.ExecuteReader(CommandType.Text, sql, param);
            while (dr.Read())
            {
                AppActiveInfo info = new AppActiveInfo();
                info.ActiveID = dr.GetInt32(0);
                info.UserID = dr.GetInt32(1);
                info.Content = dr.GetString(2);
                info.CreationTime = dr.GetDateTime(3);
                info.AppID = dr.GetInt32(4);
                info.Params = dr.GetString(5);
                info.Content = ParseActive(info.UserID, info.Content, info.Params);
                infolist.Add(info);
            }
            dr.Dispose();
            return infolist;
        }
        /// <summary>
        /// 取得某人包括其好友的动态
        /// </summary>
        /// <param name="AppID"></param>
        /// <param name="UserID"></param>
        /// <param name="Num"></param>
        /// <returns></returns>
        public List<AppActiveInfo> GetFriendActivity(int AppID, int UserID, int Num)
        {
            List<AppActiveInfo> infolist = new List<AppActiveInfo>();
            SqlParameter[] param =
                {
                    new SqlParameter("@AppID",AppID),
                    new SqlParameter("@UserID",UserID)
                };
            string topStr = string.Empty;
            if (Num != 0)
            {
                topStr = " top " + Num + " ";
            }
            string sql = "select Distinct " + topStr + " ActiveID,A.UserID,Content,CreationTime,AppID,Params from App_Activity A,NT_Friend B where A.AppID=@AppID and B.UserID=@UserID and A.UserID=B.FriendID Order by A.ActiveID desc ";
            IDataReader dr = DbHelper.ExecuteReader(CommandType.Text, sql, param);
            while (dr.Read())
            {
                AppActiveInfo info = new AppActiveInfo();
                info.ActiveID = dr.GetInt32(0);
                info.UserID = dr.GetInt32(1);
                info.Content = dr.GetString(2);
                info.CreationTime = dr.GetDateTime(3);
                info.AppID = dr.GetInt32(4);
                info.Params = dr.GetString(5);                
                info.ParseContent = ParseActive(info.UserID, info.Content, info.Params);
                infolist.Add(info);
            }
            dr.Dispose();
            return infolist;
        }
        /// <summary>
        /// 取得动态详细信息
        /// </summary>
        /// <param name="AppID">应用程序ID</param>
        /// <param name="ActiveID">动态ID</param>
        /// <returns></returns>
        public AppActiveInfo GetInfo(int AppID,int ActiveID)
        {
            AppActiveInfo info = new AppActiveInfo();
            SqlParameter param = new SqlParameter("@ActiveID", ActiveID);
            string sql = "select ActiveID,UserID,Content,CreationTime,AppID,Params from App_Activity where ActiveID=@ActiveID and AppID=@AppID";
            IDataReader dr = DbHelper.ExecuteReader(CommandType.Text, sql, param);
            if (dr.Read())
            {
                info.ActiveID = dr.GetInt32(0);
                info.UserID = dr.GetInt32(1);
                info.Content = dr.GetString(2);
                info.CreationTime = dr.GetDateTime(3);
                info.AppID = dr.GetInt32(4);
                info.Params = dr.GetString(5);
                info.Content = ParseActive(info.UserID, info.Content, info.Params);
            }
            dr.Dispose();
            return info;
        }

        #endregion

        /// <summary>
        /// 取得动态详细信息
        /// </summary>
        /// <param name="AppID">应用程序ID</param>
        /// <returns></returns>
        public AppActiveInfo GetInfo(int ActiveID)
        {
            AppActiveInfo info = new AppActiveInfo();
            SqlParameter param = new SqlParameter("@ActiveID", ActiveID);
            string sql = "select ActiveID,UserID,Content,CreationTime,AppID,Params from App_Activity where ActiveID=@ActiveID";
            IDataReader dr = DbHelper.ExecuteReader(CommandType.Text, sql, param);
            if (dr.Read())
            {
                info.ActiveID = dr.GetInt32(0);
                info.UserID = dr.GetInt32(1);
                info.Content = dr.GetString(2);
                info.CreationTime = dr.GetDateTime(3);
                info.AppID = dr.GetInt32(4);
                if (dr[5] != DBNull.Value) info.Params = dr.GetString(5);
                info.ParseContent = ParseActive(info.UserID, info.Content, info.Params);
            }
            dr.Dispose();
            return info;
        }

        private static SqlParameter[] GetParameters(AppActiveInfo info)
        {
            SqlParameter[] param=new SqlParameter[6];
            param[0] = new SqlParameter("@ActiveID", SqlDbType.Int, 4);
            param[0].Value = info.ActiveID;
            param[1] = new SqlParameter("@UserID", SqlDbType.Int, 4);
            param[1].Value = info.UserID;
            param[2] = new SqlParameter("@Content", SqlDbType.NVarChar, 500);
            param[2].Value = info.Content == null ? "" : info.Content;
            param[3] = new SqlParameter("@CreationTime", SqlDbType.DateTime, 8);
            param[3].Value = info.CreationTime;
            param[4] = new SqlParameter("@AppID", SqlDbType.Int, 4);
            param[4].Value = info.AppID;
            param[5] = new SqlParameter("@Params", SqlDbType.NVarChar, 300);
            param[5].Value = info.Params == null ? "" : info.Params;
            return param;
        }
        /// <summary>
        /// 解析动态
        /// </summary>
        /// <param name="UserID">用户ID</param>
        /// <param name="Content">内容</param>
        /// <param name="Params">参数</param>
        /// <returns></returns>
        private string ParseActive(int UserID, string Content, string Params)
        {
            SqlParameter param = new SqlParameter("@UserID", UserID);
            string sql = "select UserName from Nt_User where UserID=@UserID";
            string username = string.Empty;
            try
            {
                username = Convert.ToString(DbHelper.ExecuteScalar(CommandType.Text, sql, param));
            }
            catch { }
            username = "<a href=\"" + JuSNS.Config.UiConfig.spaceDir + "?r=" + UserID + "\">" + username + "</a>";
            string s = Content;
            s = s.Replace("{UserID}", username);
            if (!string.IsNullOrEmpty(Params))
            {
                if (Params.IndexOf("|") > -1)
                    s = string.Format(s, Params.Split('|'));
            }
            return s;
        }
    }
}
