using PushPlatform.Common.Model;
using PushPlatform.Common.Util;
using PushPlatform.Data.MvcModels;
using System;
using System.Collections.Generic;
using System.Linq;

namespace PushPlatform.Data.Bll
{
    public partial class BllSysUser
    {
        private static BllProjectInfo bllProInfo = new BllProjectInfo();
        #region Service Bll

        #region 登录逻辑
        public ResultEntity<string> Login(string loginName, string loginPwd)
        {
            ResultEntity<string> r = new ResultEntity<string>();
            if (string.IsNullOrEmpty(loginName) || string.IsNullOrEmpty(loginPwd))
            {
                r.Msg = "登录名和密码不能为空";
                r.Result = 0;
                return r;
            }
            SysUser user = Fetch(new Criteria.CriteriaSysUser { EqIsDeleted = 0,EqLoginname = loginName }).FirstOrDefault();
            if (user == null)
            {
                r.Msg = "用户名不存在";
                r.Result = 0;
                return r;
            }
            if (user.LoginPwd != loginPwd)
            {
                r.Msg = "密码错误";
                r.Result = 0;
                return r;
            }
            r.Msg = "登录成功";
            r.Result = 1;
            List<ProjectInfo> list = bllProInfo.GetUserProject(user.Id);
            SetCurrentUser(user, list);
            return r;
        }
        #endregion

        #region 设置用户登录信息
        public void SetCurrentUser(SysUser user, List<ProjectInfo> list)
        {
            DateTime expires = DateTime.Now.AddDays(1);
            //cookie
            CookieHelper.SetCookie("userId", user.Id, expires); //用户ID
            CookieHelper.SetCookie("loginName", user.LoginName, expires); //账号
            CookieHelper.SetCookie("currentProjectId", list.FirstOrDefault() != null ? list.FirstOrDefault().Id : string.Empty, expires);
            CookieHelper.SetCookie("currentProjectName", list.FirstOrDefault() != null ? list.FirstOrDefault().ProjectName : string.Empty, expires);
            //session
            SessionHelper.SetSession("projectList", list);
        }
        #endregion

        #region 清除用户登陆信息
        /// <summary>
        /// 清除用户登陆信息
        /// </summary>
        public static void ClearCurrentUser()
        {
            CookieHelper.ClearCookie("userId");
            CookieHelper.ClearCookie("loginName");
            CookieHelper.ClearCookie("currentProjectId");
            CookieHelper.ClearCookie("currentProjectName");
            SessionHelper.Del("projectList");
            SessionHelper.Del("currentUser");
        }
        #endregion

        #region 获取用户登陆信息
        /// <summary>
        /// 获取用户登陆信息
        /// </summary>
        /// <returns></returns>
        public static CurrentUserModel GetCurrentUser()
        {
            if (string.IsNullOrEmpty(CookieHelper.GetCookieValue("userId")))
            {
                ClearCurrentUser();
                return null;
            }

            CurrentUserModel currentUser = (CurrentUserModel)SessionHelper.Get("currentUser");

            if (currentUser == null)
            {
                currentUser = new CurrentUserModel
                {
                    UserId = CookieHelper.GetCookieValue("userId"),
                    LoginName = CookieHelper.GetCookieValue("loginName"),
                    CurrentProjectId = CookieHelper.GetCookieValue("currentProjectId"),
                    CurrentProjectName = CookieHelper.GetCookieValue("currentProjectName"),
                    ChargeProject = (List<ProjectInfo>)SessionHelper.Get("projectList")
                };
                //SESSION 过期重新查一遍
                if (currentUser.ChargeProject == null || currentUser.ChargeProject.Count == 0)
                {
                    currentUser.ChargeProject = bllProInfo.GetUserProject(currentUser.UserId);
                    SessionHelper.SetSession("projectList", currentUser.ChargeProject);
                }
            }
            return currentUser;
        }
        #endregion


        #region 切换项目重置Cookie
        public static void ChangeProject(string userId, string loginName, string projectId, string projectName)
        {
            DateTime expires = DateTime.Today.AddDays(1);
            CookieHelper.SetCookie("userId", userId, expires); //用户ID
            CookieHelper.SetCookie("loginName", loginName, expires); //账号
            CookieHelper.SetCookie("currentProjectId", projectId, expires);
            CookieHelper.SetCookie("currentProjectName", projectName, expires);
            SessionHelper.Del("currentUser");
        }
        #endregion
        #endregion
    }
}