using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Autofac;
using Bowin.Common;
using EMIS.Utility;
using EMIS.CommonLogic.CalendarManage;
using EMIS.CommonLogic.SystemServices;
using Bowin.Web.Controls.Mvc;
using Bowin.Common.Utility;
using EMIS.ViewModel.Account;
using EMIS.Entities;
using System.Web.Mvc;
using EMIS.CommonLogic.Students;
using EMIS.ViewModel;
namespace EMIS.Web.Controls
{
public static class BaseExtensions
{
///
/// 查询用户字典信息
///
public static void GetContextUser()
{
FormsAuthenticationHelper fahelper = new FormsAuthenticationHelper();
var cookieName = EMIS.Utility.Const.LOCAL_SETTING_LOGIN_COOKIENAME;
fahelper.AuthenticateRequest(HttpContext.Current, cookieName, Const.LOCAL_AUTH_EXCEPTURL);
}
///
/// 查询星期下拉列表
///
public static List WeekdayDropdownListItemList
{
get
{
return WeekHelper.WeekDictionary.OrderBy(x => (x.Key == 0 ? 7 : x.Key))
.Select(x => new DropdownListItem
{
Text = x.Value,
Value = x.Key
}).ToList();
}
}
///
/// 查询当前启用校历对应的学年学期ID(主键ID)
///
///
public static Guid GetCurrentSchoolYearID()
{
using (var scope = AutofacHelper.Container.BeginLifetimeScope())
{
ISchoolYearServices SchoolYearServices = scope.Resolve();
var schoolYearView = SchoolYearServices.GetCurrentSchoolYear();
return schoolYearView.SchoolYearID.Value;
}
}
///
/// 查询当前启用校历对应的学年学期Code(学年学期Code)
///
///
public static string GetCurrentSchoolYearCode()
{
using (var scope = AutofacHelper.Container.BeginLifetimeScope())
{
ISchoolYearServices SchoolYearServices = scope.Resolve();
var schoolYearView = SchoolYearServices.GetCurrentSchoolYear();
return schoolYearView.Code;
}
}
///
///查询当前启用校历对应的下一个学年学期ID(主键ID)
///
///
public static Guid GetNextSchoolYearID()
{
using (var scope = AutofacHelper.Container.BeginLifetimeScope())
{
ISchoolYearServices SchoolYearServices = scope.Resolve();
var nextSchoolYear = SchoolYearServices.GetNextSchoolYear();
return nextSchoolYear.SchoolYearID.Value;
}
}
///
/// 查询当前启用校历对应的下一个学年学期Code(学年学期Code)
///
///
public static string GetNextSchoolYearCode()
{
using (var scope = AutofacHelper.Container.BeginLifetimeScope())
{
ISchoolYearServices SchoolYearServices = scope.Resolve();
var nextSchoolYear = SchoolYearServices.GetNextSchoolYear();
return nextSchoolYear.Code;
}
}
///
/// 查询当前启用校历对应的学年(学年)
///
///
public static int? GetCurrentYearID()
{
using (var scope = AutofacHelper.Container.BeginLifetimeScope())
{
ISchoolYearServices SchoolYearServices = scope.Resolve();
var schoolYear = SchoolYearServices.GetCurrentSchoolYear();
return schoolYear.Years;
}
}
///
/// 查询当前启用校历的对应的学年的下一学年(学年)
///
///
public static int? GetNextYearID()
{
using (var scope = AutofacHelper.Container.BeginLifetimeScope())
{
ISchoolYearServices SchoolYearServices = scope.Resolve();
var currentSchoolYear = SchoolYearServices.GetCurrentSchoolYear();
var nextSchoolYear = SchoolYearServices.GetNextSchoolYear();
if (nextSchoolYear.Years <= currentSchoolYear.Years)
{
return nextSchoolYear.Years + 1;
}
else
{
return nextSchoolYear.Years;
}
}
}
///
/// 查询当前启用的毕业学期ID(学年学期对应的ID)
///
///
public static Guid? GetGradSchoolYearID()
{
using (var scope = AutofacHelper.Container.BeginLifetimeScope())
{
IParameterServices ParameterServices = scope.Resolve();
//查询设置的毕业学期信息
var graduationSchoolyear = ParameterServices.GetParameterValue(CF_ParameterType.GraduationSchoolyear);
if (!string.IsNullOrEmpty(graduationSchoolyear))
{
ISchoolYearServices SchoolYearServices = scope.Resolve();
var schoolYearView = SchoolYearServices.GetSchoolYearView(Guid.Parse(graduationSchoolyear));
if (schoolYearView != null)
{
return schoolYearView.SchoolYearID;
}
}
return null;
}
}
///
/// 查询当前启用的毕业学期Code(学年学期对应的Code)
///
///
public static string GetGradSchoolYearCode()
{
using (var scope = AutofacHelper.Container.BeginLifetimeScope())
{
IParameterServices ParameterServices = scope.Resolve();
//查询设置的毕业学期信息
var graduationSchoolyear = ParameterServices.GetParameterValue(CF_ParameterType.GraduationSchoolyear);
if (!string.IsNullOrEmpty(graduationSchoolyear))
{
ISchoolYearServices SchoolYearServices = scope.Resolve();
var schoolYearView = SchoolYearServices.GetSchoolYearView(Guid.Parse(graduationSchoolyear));
if (schoolYearView != null)
{
return schoolYearView.Code;
}
}
return null;
}
}
///
/// 登录成功
///
///
///
///
public static void LoginSureccessful(this ControllerBase controller, LogOnModel model, string userName, Guid? userID = null)
{
FormsAuthenticationHelper fahelper = new FormsAuthenticationHelper();
var cookieName = EMIS.Utility.Const.LOCAL_SETTING_LOGIN_COOKIENAME;
List userData = new List();
Sys_User userpageResult;
Sys_Role defaultRole = null;
using (var scope = AutofacHelper.Container.BeginLifetimeScope())
{
IUserServices userServices = scope.Resolve();
if (userID != null)
{
userpageResult = userServices.GetUserByUserID(userID, true);
}
else {
userpageResult = userServices.GetUserByLoginID(userName, true);
}
if (userpageResult == null)
{
throw new Exception("获取用户失败。");
}
//只要不是学生,就需要获取角色,如果实在没有角色,则不用选,依然允许进入系统,反正他什么都看不到
if (userpageResult.CF_Student == null)
{
defaultRole = userServices.GetMaxPrivilegeRoleByUserID(userpageResult.UserID);
}
}
if (model.RememberMe)
{
controller.RemeberUserCookies(model);
}
else
{
controller.RemoveUserCookies(model);
}
userData.Add(userpageResult.LoginID);
userData.Add(userpageResult.UserID.ToString());
userData.Add(userpageResult.Name);
userData.Add((userpageResult.CF_Staff != null) ? userpageResult.UserID.ToString() : "");//教职工ID
userData.Add((userpageResult.CF_Student != null) ? userpageResult.UserID.ToString() : "");//学生ID
userData.Add((userpageResult.CF_Staff != null && userpageResult.CF_Staff.CF_Department != null) ? userpageResult.CF_Staff.DepartmentID.ToString() : "");//部门(教研室)ID
userData.Add((userpageResult.CF_Staff != null && userpageResult.CF_Staff.CF_Department != null) ? userpageResult.CF_Staff.CF_Department.HierarchyID : "");//部门(教研室)树结构ID
userData.Add((userpageResult.CF_Staff != null && userpageResult.CF_Staff.CF_Department != null) ? userpageResult.CF_Staff.CF_Department.Name : "");//部门(教研室)名称
if (userpageResult.CF_Student == null)
{
if (userpageResult.CF_Staff != null && userpageResult.CF_Staff.CF_College != null)
{
userData.Add(userpageResult.CF_Staff.CollegeID.ToString());//学院ID
//userData.Add(userpageResult.CF_Staff.CF_College.CampusID.ToString());//校区ID
//userData.Add(userpageResult.CF_Staff.CF_College.CF_Campus.UniversityID.ToString());//学校ID
}
else
{
userData.Add("");
//userData.Add("");
//userData.Add("");
}
if (defaultRole != null)
{
userData.Add(defaultRole.RoleID.ToString());//默认角色ID
}
else
{
userData.Add("");
}
}
else
{
//学生的后续再加
userData.Add("");
//userData.Add("");
//userData.Add("");
userData.Add("");
}
fahelper.loginFormsAuthentication(controller.ControllerContext.HttpContext, cookieName, userData);
}
///
/// 角色切换
///
///
///
public static void SwitchUserRole(this ControllerBase controller, Guid roleID)
{
FormsAuthenticationHelper fahelper = new FormsAuthenticationHelper();
var userData = EMIS.Utility.FormValidate.CustomPrincipal.Current.GetUserData();
var cookieName = EMIS.Utility.Const.LOCAL_SETTING_LOGIN_COOKIENAME;
userData[9] = roleID.ToString();
fahelper.loginFormsAuthentication(controller.ControllerContext.HttpContext, cookieName, userData);
}
///
///
///
///
///
public static void RemeberUserCookies(this ControllerBase controller, LogOnModel model)
{
HttpCookie cookie = new HttpCookie("username");
cookie.Value = model.UserName;
cookie.Expires = DateTime.Now.AddDays(1);
controller.ControllerContext.HttpContext.Response.AppendCookie(cookie);
HttpCookie cookie2 = new HttpCookie("password");
cookie2.Value = model.Password;
cookie2.Expires = DateTime.Now.AddDays(1);
controller.ControllerContext.HttpContext.Response.AppendCookie(cookie2);
HttpCookie cookie3 = new HttpCookie("rememberme");
cookie3.Value = model.RememberMe.ToString();
cookie3.Expires = DateTime.Now.AddDays(1);
controller.ControllerContext.HttpContext.Response.AppendCookie(cookie3);
}
///
///
///
///
///
public static void RemoveUserCookies(this ControllerBase controller, LogOnModel model)
{
HttpCookie cookie = new HttpCookie("username");
cookie.Value = model.UserName;
cookie.Expires = DateTime.Now.AddDays(-1);
controller.ControllerContext.HttpContext.Response.AppendCookie(cookie);
HttpCookie cookie2 = new HttpCookie("password");
cookie2.Value = model.Password;
cookie2.Expires = DateTime.Now.AddDays(-1);
controller.ControllerContext.HttpContext.Response.AppendCookie(cookie2);
HttpCookie cookie3 = new HttpCookie("rememberme");
cookie3.Value = model.RememberMe.ToString();
cookie3.Expires = DateTime.Now.AddDays(-1);
controller.ControllerContext.HttpContext.Response.AppendCookie(cookie3);
}
///
///
///
///
///
public static LogOnModel GetUserCookies(this ControllerBase controller)
{
LogOnModel model = new LogOnModel();
HttpCookie cookie = controller.ControllerContext.HttpContext.Request.Cookies["username"];
if (cookie != null)
{
model.UserName = cookie.Value;
}
HttpCookie cookie2 = controller.ControllerContext.HttpContext.Request.Cookies["password"];
if (cookie2 != null)
{
model.Password = cookie2.Value;
}
HttpCookie cookie3 = controller.ControllerContext.HttpContext.Request.Cookies["rememberme"];
if (cookie3 != null)
{
model.RememberMe = cookie3.Value.ToLower() == "true";
}
return model;
}
///
///
///
///
public static int? GetDefaultLearnformForList()
{
using (var scope = AutofacHelper.Container.BeginLifetimeScope())
{
IStudentsServices studentServices = scope.Resolve();
return studentServices.GetDefaultLearnformForList();
}
}
}
}