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

namespace JuSNS.SQLServer.ApiInterface
{
    public class UtilUser:DbBase,IUtilUser
    {
        #region IUtilUser 成员
        /// <summary>
        /// 添加应用
        /// </summary>
        /// <param name="UserID"></param>
        /// <param name="AppID"></param>
        /// <returns></returns>
        public int AddApp(int UserID, int AppID)
        {
            SqlConnection cn = new SqlConnection(DBConfig.CnString);
            cn.Open();
            SqlTransaction trans = cn.BeginTransaction();
            try
            {
                SqlParameter[] param ={
                    new SqlParameter("@UserID",UserID),
                    new SqlParameter("@AppID",AppID)
                };
                string sql = "select count(*) from App_App where AppID=@AppID and islock=1";
                int n = Convert.ToInt32(DbHelper.ExecuteScalar(trans, CommandType.Text, sql, param));
                if (n > 0)
                {
                    return -3;
                }
                sql = "insert into App_UserApp(UserID,AppID,CreationTime) values(@UserID,@AppID,GetDate());select @@Identity";
                n = Convert.ToInt32(DbHelper.ExecuteScalar(trans, CommandType.Text, sql, param));
                JUser ju = new JUser();
                if (ju.CheckPrivacyInfo(trans, UserID, "ActSecede"))
                {
                    UserDyn(trans, 0, AppID, "AddApp", UserID, 0, AppID, "添加应用", "");
                }
                sql = "update App_App set hot=hot+1 where AppID=@AppID";
                n = DbHelper.ExecuteNonQuery(trans, CommandType.Text, sql, param);

                trans.Commit();
                return n;
            }
            catch
            {
                trans.Rollback();
                throw;
            }
            finally
            {
                if (cn.State == ConnectionState.Open)
                    cn.Close();
            }
        }
        /// <summary>
        /// 删除某应用
        /// </summary>
        /// <param name="UserID"></param>
        /// <param name="AppID"></param>
        /// <returns></returns>
        public int DeleteApp(int UserID, int AppID)
        {
            SqlConnection cn = new SqlConnection(DBConfig.CnString);
            cn.Open();
            SqlTransaction trans = cn.BeginTransaction();
            try
            {
                SqlParameter[] param ={
                    new SqlParameter("@UserID",UserID),
                    new SqlParameter("@AppID",AppID)
                };
                string sql = "Delete from App_UserApp where UserID=@UserID and AppID=@AppID";
                int n = DbHelper.ExecuteNonQuery(trans, CommandType.Text, sql, param);
                JUser ju = new JUser();
                if (ju.CheckPrivacyInfo(trans, UserID, "ActSecede"))
                {
                    UserDyn(trans, 0, AppID, "OutApp", UserID, 0, AppID, "删除应用", "");
                }
                sql = "update App_App set hot=hot-1 where AppID=@AppID";
                n = DbHelper.ExecuteNonQuery(trans, CommandType.Text, sql, param);

                trans.Commit();
                return n;
            }
            catch
            {
                trans.Rollback();
                throw;
            }
            finally
            {
                if (cn.State == ConnectionState.Open)
                    cn.Close();
            }
        }
        /// <summary>
        /// 取得用户的应用程序
        /// </summary>
        /// <param name="UserID">用户ID</param>
        /// <returns></returns>
        public List<AppInfo> GetUserApp(int UserID)
        {
            List<AppInfo> infolist = new List<AppInfo>();
            SqlParameter param = new SqlParameter("@UserID", UserID);
            string sql = "select AppID,AppName,Description,CreationTime,UserID,IsLock,Url,Icon,[Type],Hot,MidIcon,BigIcon,IsSys from App_App where IsLock=0 and AppID in (Select AppID from App_UserApp where UserID=@UserID)";
            IDataReader dr = DbHelper.ExecuteReader(CommandType.Text, sql, param);
            while (dr.Read())
            {
                AppInfo info = new AppInfo();
                info.AppID = dr.GetInt32(0);
                info.AppName = dr.GetString(1);
                if (!dr.IsDBNull(2)) info.Description = dr.GetString(2);
                info.CreationTime = dr.GetDateTime(3);
                info.UserID = dr.GetInt32(4);
                info.IsLock = dr.GetInt32(5);
                info.Url = dr.GetString(6);
                info.Icon = dr.GetString(7);
                info.Type = dr.GetInt32(8);
                info.TypeStr = JuSNS.Config.UserApiConfig.GetType(info.Type);
                info.Hot = dr.GetInt32(9);
                info.MidIcon = dr.GetString(10);
                info.BigIcon = dr.GetString(11);
                info.IsSys = dr.GetInt32(12);
                infolist.Add(info);
            }
            dr.Dispose();
            return infolist;
        }
        #endregion
    }
}
