using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel.Activation;
using System.ServiceModel;
using System.Text;
using System.Configuration;
using QMFrameWork.ServiceInterface;
using QMFrameWork.ServiceLibrary;
using QMAPP.Entity.Sys;
using QMAPP.BLL.Sys;

namespace QM.Exchange.Core
{
    /// <summary>
    /// 登录服务
    /// </summary>
    public class LoginService : ILoginService
    {
        #region 用户登录验(用户名和密码登录)

        /// <summary>
        /// 用户登录验(用户名和密码登录)
        /// </summary>
        /// <param name="login">登录信息</param>
        /// <param name="c">登录凭据</param>
        /// <returns>登录信息</returns>
        public LoginInfo IsLogin(LoginInfo login, CredentialInfo c)
        {
            try
            {
                login = new LoginBLL().IsLogin(login);

                if (login == null)
                {
                    return login;
                }

                ////是否限制超级用户权限
                //if (ConfigurationManager.AppSettings["LimitSuperPower"] != null)
                //{
                //    if (ConfigurationManager.AppSettings["LimitSuperPower"].ToString().ToLower() != "false")
                //    {
                //        //限制超级用户权限
                //        login.LimitSuperPower = true;
                //    }
                //}

                c.UserName = login.UserName;
                SessionManager.Add(c.CredentialID, new ServiceSession { SessionID = c.CredentialID, UserCredential = c, UserLogin = login }, new TimeSpan(0, 60, 0));

                return login;
            }
            catch (Exception ex)
            {
                QMFrameWork.Log.LogManager.LogHelper.Error(
                        new QMFrameWork.Log.LogInfo { ClientIP = "localhost", UserName = "admin", Info = "执行服务异常", ErrorInfo = ex });
                throw new FaultException(ex.Message);
            }
        }

        #endregion

        #region 获取登录信息

        /// <summary>
        /// 获取登录信息
        /// </summary>
        /// <param name="c">登录凭据</param>
        /// <returns>登录信息</returns>
        public LoginInfo GetLoginInfo(CredentialInfo c)
        {
            try
            {
                ServiceSession session = SessionManager.GetSession(c.CredentialID);
                if (session != null)
                {
                    return session.UserLogin;
                }
                else
                {
                    return null;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #endregion
    }
}