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

namespace JuSNS.SQLServer.ApiInterface
{
    public class App:DbBase,IApp
    {
        #region IApp 成员

        /// <summary>
        /// 锁定应用程序
        /// </summary>
        /// <param name="AppID">应用程序ID</param>
        /// <returns></returns>
        public int Lock(int AppID)
        {
            SqlParameter param = new SqlParameter("@AppID", AppID);
            string sql = "Update App_App set IsLock=1 where AppID=@AppID";
            return DbHelper.ExecuteNonQuery(CommandType.Text, sql, param);
        }
        /// <summary>
        /// 解锁应用程序
        /// </summary>
        /// <param name="AppID"></param>
        /// <returns></returns>
        public int UnLock(int AppID)
        {
            SqlParameter param = new SqlParameter("@AppID", AppID);
            string sql = "Update App_App set IsLock=0 where AppID=@AppID";
            return DbHelper.ExecuteNonQuery(CommandType.Text, sql, param);
        }
        /// <summary>
        /// 编辑应用程序
        /// </summary>
        /// <param name="info">应用程序ID</param>
        /// <returns></returns>
        public int EditApp(AppInfo info)
        {
            SqlParameter[] param = GetParameter(info);
            string sql = "update App_App set AppName=@AppName,Description=@Description,CreationTime=@CreationTime,UserID=@UserID,IsLock=@IsLock,Url=@Url,Icon=@Icon,[Type]=@Type,MidIcon=@MidIcon,BigIcon=@BigIcon,IsSys=@IsSys where AppID=@AppID";
            return DbHelper.ExecuteNonQuery(CommandType.Text, sql, param);
        }

        public int EditAppAdmin(AppInfo info)
        {
            SqlParameter[] param = GetParameter(info);
            string sql = "update App_App set AppName=@AppName,toadmin=@ToAdmin,Mobile=@Mobile,Tel=@Tel,Email=@Email where AppID=@AppID";
            return DbHelper.ExecuteNonQuery(CommandType.Text, sql, param);
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="AppID">应用程序ID</param>
        /// <returns></returns>
        public int Delete(int AppID)
        {
            SqlParameter param = new SqlParameter("@AppID", AppID);
            string sql = "delete from App_App where AppID=@AppID";
            return DbHelper.ExecuteNonQuery(CommandType.Text, sql, param);
        }
        /// <summary>
        /// 审核开发者
        /// </summary>
        /// <param name="UserID">用户ID</param>
        /// <returns></returns>
        public int VerifyDeveloper(int UserID)
        {
            SqlParameter param = new SqlParameter("@UserID", UserID);
            string sql = "Update App_Developer set Enabled=1 where UserID=@UserID";
            return DbHelper.ExecuteNonQuery(CommandType.Text, sql, param);
        }
        /// <summary>
        /// 锁定开发者
        /// </summary>
        /// <param name="UserID">用户ID</param>
        /// <returns></returns>
        public int LockDeveloper(int UserID)
        {
            SqlParameter param = new SqlParameter("@UserID", UserID);
            string sql = "Update App_Developer set Enabled=0 where UserID=@UserID";
            return DbHelper.ExecuteNonQuery(CommandType.Text, sql, param);
        }
        /// <summary>
        /// 验证AppKey
        /// </summary>
        /// <param name="key">AppKey</param>
        /// <returns></returns>
        public bool VerifyKey(string key,out int AppID)
        {
            string str = Common.DES.Decrypt(key, Config.SecretConfig.UserAppKey, Config.SecretConfig.UserAppIV);
            string[] arr = str.Split(Config.SecretConfig.UserAppSeparator);
            int UserID = 0;
            string dKey=string.Empty;
            AppID = 0;
            try
            {
                AppID = int.Parse(arr[0]);
                dKey = arr[1];
            }
            catch
            {
                return false;
            }
            SqlParameter[] param =
                {
                    new SqlParameter("@AppID",AppID),
                    new SqlParameter("@dKey",dKey)
                };
            string sql = "select count(*) from App_App where AppID=" + AppID + " and AppKey='" + dKey + "'";
            int n = Convert.ToInt32(DbHelper.ExecuteScalar(CommandType.Text, sql, null));
            if (n > 0)
            {
                return true;
            }
            return false;
        }
        /// <summary>
        /// 取得应用程序信息
        /// </summary>
        /// <param name="AppID"></param>
        /// <returns></returns>
        public AppInfo GetAppInfo(int AppID)
        {
            AppInfo info = new AppInfo();
            SqlParameter param = new SqlParameter("@AppID", AppID);
            string sql = "select AppID,AppName,Description,CreationTime,UserID,IsLock,Url,Icon,[Type],Hot,MidIcon,BigIcon,IsSys,ToAdmin,mobile,tel,email from App_App where AppID=@AppID";
            IDataReader dr = DbHelper.ExecuteReader(CommandType.Text, sql, param);
            if (dr.Read())
            {
                info.AppID = dr.GetInt32(0);
                info.AppName = dr.GetString(1);
                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);
                if (!dr.IsDBNull(13)) info.ToAdmin = Convert.ToInt32(dr["ToAdmin"]);
                if (!dr.IsDBNull(14)) info.Mobile = dr["Mobile"].ToString();
                if (!dr.IsDBNull(15)) info.Tel = dr["Tel"].ToString();
                if (!dr.IsDBNull(16)) info.Email = dr["Email"].ToString();
            }
            dr.Dispose();
            return info;
        }
        #endregion

        /// <summary>
        /// appSQL参数
        /// </summary>
        /// <param name="info">App信息</param>
        /// <returns></returns>
        public static SqlParameter[] GetParameter(AppInfo info)
        {
            SqlParameter[] param = new SqlParameter[17];
            param[0] = new SqlParameter("@AppID", SqlDbType.Int, 4);
            param[0].Value = info.AppID;
            param[1] = new SqlParameter("@AppName", SqlDbType.NVarChar, 200);
            param[1].Value = string.IsNullOrEmpty(info.AppName) ? "" : info.AppName;
            param[2] = new SqlParameter("@Description", SqlDbType.NVarChar, 500);
            param[2].Value = string.IsNullOrEmpty(info.Description) ? "" : info.Description;
            param[3] = new SqlParameter("@CreationTime", SqlDbType.DateTime, 8);
            param[3].Value = info.CreationTime;
            param[4] = new SqlParameter("@UserID", SqlDbType.Int, 4);
            param[4].Value = info.UserID;
            param[5] = new SqlParameter("@IsLock", SqlDbType.Int, 4);
            param[5].Value = info.IsLock;
            param[6] = new SqlParameter("@Url", SqlDbType.NVarChar, 500);
            param[6].Value = string.IsNullOrEmpty(info.Url) ? "" : info.Url;
            param[7] = new SqlParameter("@Icon", SqlDbType.NVarChar, 100);
            param[7].Value = string.IsNullOrEmpty(info.Icon) ? "" : info.Icon;
            param[8] = new SqlParameter("@Type", SqlDbType.Int, 4);
            param[8].Value = info.Type;
            param[9] = new SqlParameter("@Hot", SqlDbType.Int, 4);
            param[9].Value = info.Hot;
            param[10] = new SqlParameter("@MidIcon", SqlDbType.NVarChar, 100);
            param[10].Value = string.IsNullOrEmpty(info.MidIcon) ? "" : info.MidIcon;
            param[11] = new SqlParameter("@BigIcon", SqlDbType.NVarChar, 100);
            param[11].Value = string.IsNullOrEmpty(info.BigIcon) ? "" : info.BigIcon;
            param[12] = new SqlParameter("@IsSys", SqlDbType.Int,4);
            param[12].Value = info.ToAdmin;
            param[13] = new SqlParameter("@ToAdmin", SqlDbType.TinyInt);
            param[13].Value = info.ToAdmin;
            param[14] = new SqlParameter("@Mobile", SqlDbType.NVarChar, 30);
            param[14].Value = info.Mobile;
            param[15] = new SqlParameter("@Tel", SqlDbType.NVarChar, 20);
            param[15].Value = info.Tel;
            param[16] = new SqlParameter("@Email", SqlDbType.NVarChar, 50);
            param[16].Value = info.Email;
            return param;
        }
    }
}
