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

namespace JuSNS.SQLServer.ApiInterface
{
    public class Developer : DbBase, IDeveloper
    {
        #region IDeveloper 成员

        /// <summary>
        /// 申请成为开发者
        /// </summary>
        /// <param name="UserID">用户ID</param>
        /// <returns></returns>
        public int RequestDeveloper(int UserID, out int isDev)
        {
            string isql = "select count(*) from App_Developer where UserID=" + UserID;
            isDev = Convert.ToInt32(DbHelper.ExecuteScalar(CommandType.Text, isql, null));
            SqlParameter param = new SqlParameter("@UserID", UserID);
            string sql = "Insert into App_Developer(UserID,CreationTime,Enabled) values(@UserID,GetDate()," + Convert.ToInt32(JuSNS.Common.Public.GetXMLValue("isDev")) + ")";
            return DbHelper.ExecuteNonQuery(CommandType.Text, sql, param);
        }
        /// <summary>
        /// 是否是开发者
        /// </summary>
        /// <param name="UserID"></param>
        /// <returns></returns>
        public bool IsDeveloper(int UserID)
        {
            SqlParameter param = new SqlParameter("@UserID", UserID);
            string sql = "select Count(*) from App_Developer where UserID=@UserID";
            int n = Convert.ToInt32(DbHelper.ExecuteScalar(CommandType.Text, sql, param));
            return n > 0 ? true : false;
        }
        /// <summary>
        /// 是否开通开发者
        /// </summary>
        /// <param name="UserID"></param>
        /// <returns></returns>
        public bool IsEnabled(int UserID)
        {
            SqlParameter param = new SqlParameter("@UserID", UserID);
            string sql = "select Count(*) from App_Developer where UserID=@UserID and Enabled=1";
            int n = Convert.ToInt32(DbHelper.ExecuteScalar(CommandType.Text, sql, param));
            return n > 0 ? true : false;
        }
        /// <summary>
        /// 删除开发者
        /// </summary>
        /// <param name="ID">开发者ID</param>
        /// <returns></returns>
        public int DeleteDeveloper(int ID)
        {
            SqlParameter param = new SqlParameter("@ID", ID);
            string sql = "Delete from App_Developer where ID=@ID";
            return DbHelper.ExecuteNonQuery(CommandType.Text, sql, param);
        }
        /// <summary>
        /// 通过开发者
        /// </summary>
        /// <param name="ID">开发者ID</param>
        /// <returns></returns>
        public int PassDeveloper(int ID)
        {
            SqlParameter param = new SqlParameter("@ID", ID);
            string sql = "Update App_Developer set Enabled=1 where ID=@ID";
            return DbHelper.ExecuteNonQuery(CommandType.Text, sql, param);
        }
        /// <summary>
        /// 关闭开发者
        /// </summary>
        /// <param name="ID">开发者ID</param>
        /// <returns></returns>
        public int DisableDeveloper(int ID)
        {
            SqlParameter param = new SqlParameter("@ID", ID);
            string sql = "Update App_Developer set Enabled=0 where ID=@ID";
            return DbHelper.ExecuteNonQuery(CommandType.Text, sql, param);
        }
        /// <summary>
        /// 建立应用程序
        /// </summary>
        /// <param name="info">应用程序信息</param>
        /// <returns></returns>
        public int CreateApp(AppInfo info)
        {
            SqlParameter[] param = App.GetParameter(info);
            string sql = "Insert into App_App(AppName,Description,CreationTime,UserID,IsLock,Url,Icon,[Type],Hot,MidIcon,BigIcon,IsSys) values(@AppName,@Description,@CreationTime,@UserID,@IsLock,@Url,@Icon,@Type,@Hot,@MidIcon,@BigIcon,@IsSys)";
            return DbHelper.ExecuteNonQuery(CommandType.Text, sql, param);
        }
        /// <summary>
        /// 删除应用
        /// </summary>
        /// <param name="userid">用户ID</param>
        /// <param name="appid">应用ID</param>
        /// <returns></returns>
        public int DeleteApp(int userid, int appid)
        {
            SqlParameter[] param ={
                new SqlParameter("@UserID",userid),
                new SqlParameter("@AppID",appid)
            };
            string sql = "Delete from App_App where UserID=@UserID and AppID=@AppID";
            return DbHelper.ExecuteNonQuery(CommandType.Text, sql, param);
        }
        /// <summary>
        /// 编辑应用程序信息
        /// </summary>
        /// <param name="info">应用程序信息</param>
        /// <returns></returns>
        public int EditApp(AppInfo info)
        {
            return new App().EditApp(info);
        }
        public int EditAppAdmin(AppInfo info)
        {
            return new App().EditAppAdmin(info);
        }
        /// <summary>
        /// 生成appkey
        /// </summary>
        /// <param name="AppID"></param>
        /// <returns></returns>
        public int ResetAppKey(int AppID)
        {
            string keycode=JuSNS.Common.Rand.Str(15);
            string sql = "Update App_App set AppKey='"+keycode+"' where AppID="+AppID+"";
            return DbHelper.ExecuteNonQuery(CommandType.Text, sql, null);
        }
        /// <summary>
        /// 取得Appkey
        /// </summary>
        /// <param name="AppID"></param>
        /// <returns></returns>
        public string GetAppKey(int AppID)
        {
            SqlParameter[] param ={
                new SqlParameter("@AppID",AppID)
            };
            string sql = "select AppKey as idandkey from App_App where AppID=@AppID";
            string key = "";
            object tkey=DbHelper.ExecuteScalar(CommandType.Text, sql, param);
            if (tkey != DBNull.Value && tkey != null)
            {
                key = tkey.ToString();
            }
            if (string.IsNullOrEmpty(key))
            {
                return "";
            }
            else
            {
                return DES.Encrypt(AppID.ToString() + JuSNS.Config.SecretConfig.UserAppSeparator.ToString() + key, Config.SecretConfig.UserAppKey, Config.SecretConfig.UserAppIV);
            }
        }
        #endregion
    }
}
