using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Reflection;
using System.Web.Mvc;
using Autofac;
using Bowin.Web.Controls.Mvc;
using Bowin.Common;
using Bowin.Common.Utility;
using EMIS.Utility;
using EMIS.Entities;
using EMIS.ViewModel;
using EMIS.ViewModel.WorkflowManage;
using EMIS.ViewModel.AccountManage;
using EMIS.CommonLogic.StudentManage.StudentProfile;
using EMIS.CommonLogic.CalendarManage;
using EMIS.CommonLogic.SystemServices;
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;
}
}
///
/// 查询对应的工作流程中开始流程环节的状态Pid(根据对应的tableName)
///
///
///
public static int? GetStartFlowStatus(string tableName)
{
using (var scope = AutofacHelper.Container.BeginLifetimeScope())
{
IWorkflowServices WorkflowServices = scope.Resolve();
return WorkflowServices.GetStartStatus(tableName);
}
}
///
/// 查询对应的接入流程表已退回流程环节Pid(注:不包含开始环节、结束环节且为[BP]标识)
///
///
///
public static int? GetSendBackFlowStatus(string tableName)
{
using (var scope = AutofacHelper.Container.BeginLifetimeScope())
{
IWorkflowServices WorkflowServices = scope.Resolve();
return WorkflowServices.GetSendBackStatus(tableName);
}
}
///
/// 查询对应的接入流程表已通过流程环节Pid(注:不包含开始环节、结束环节且为[PASS]标识)
///
///
///
public static int? GetPassNoEndFlowStatus(string tableName)
{
using (var scope = AutofacHelper.Container.BeginLifetimeScope())
{
IWorkflowServices WorkflowServices = scope.Resolve();
return WorkflowServices.GetPassNoEndStatus(tableName);
}
}
///
/// 查询对应的工作流程中结束流程环节的状态Pid(根据对应的tableName,结束环节且不为[BP]标识的)
///
///
///
public static int? GetEndFlowStatus(string tableName)
{
using (var scope = AutofacHelper.Container.BeginLifetimeScope())
{
IWorkflowServices WorkflowServices = scope.Resolve();
return WorkflowServices.GetCorrectEndStatus(tableName);
}
}
///
/// 查询对应的接入流程表非正常结束的流程环节Pid(注:通常为结束环节且为[BP]标识的)
///
///
///
public static List GetBackpointStatus(string tableName)
{
using (var scope = AutofacHelper.Container.BeginLifetimeScope())
{
IWorkflowServices WorkflowServices = scope.Resolve();
return WorkflowServices.GetBackpointStatus(tableName);
}
}
///
/// 根据当前流程环节ID获取下一步审批动作
///
///
///
///
///
public static List GetCurrentActionViewList(string tableName, Guid formID, Guid userID)
{
using (var scope = AutofacHelper.Container.BeginLifetimeScope())
{
IWorkflowServices WorkflowServices = scope.Resolve();
return WorkflowServices.GetActionView(tableName, formID, userID);
}
}
///
/// 登录成功
///
///
///
///
public static void LoginSureccessful(this ControllerBase controller, LogOnModel model, string userName)
{
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();
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[11] = 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())
{
IStudentServices studentServices = scope.Resolve();
return studentServices.GetDefaultLearnformForList();
}
}
///
/// 查询入学学期ID(Web.config)
///
///
public static int? GetEntranceSemesterID()
{
int? entranceSemesterID = null;
if (!string.IsNullOrEmpty(EMIS.Utility.Const.LOCAL_SETTING_ENTRANCESEMESTERID))
{
entranceSemesterID = Convert.ToInt32(EMIS.Utility.Const.LOCAL_SETTING_ENTRANCESEMESTERID);
}
return entranceSemesterID;
}
///
/// List转换为DataTable
///
///
///
///
public static DataTable ToDataTable(this List items)
{
DataTable dataTable = new DataTable();
PropertyInfo[] Props = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);
foreach (PropertyInfo prop in Props)
{
dataTable.Columns.Add(prop.Name);
}
foreach (T obj in items)
{
var values = new object[Props.Length];
for (int i = 0; i < Props.Length; i++)
{
values[i] = Props[i].GetValue(obj, null);
}
dataTable.Rows.Add(values);
}
return dataTable;
}
}
}