using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; using System.Data.Entity; using System.Text; using System.Web; using Autofac; using Bowin.Common; using Bowin.Common.Linq.Entity; using EMIS.Utility; using EMIS.Entities; using EMIS.ViewModel; using EMIS.DataLogic; using EMIS.DataLogic.SystemDAL; namespace EMIS.CommonLogic { public class BaseServices : IBaseServices { public UnitOfWork UnitOfWork { get; set; } public HRUnitOfWork HRUnitOfWork { get; set; } public LogUnitOfWork LogUnitOfWork { get; set; } private DataRangeDAL DataRangeDAL { get; set; } /// /// /// public BaseServices() { DataRangeDAL = AutofacHelper.RequestLifetimeScope.Resolve(); } /// /// /// /// /// /// protected Expression> GetDataRageExpression(Expression> userColumn) { ParameterExpression paramter; var userColumnExp = userColumn; paramter = userColumnExp.Parameters[0]; SYS_DataRange dataRange; var curUser = EMIS.Utility.FormValidate.CustomPrincipal.Current; var curUserID = curUser.UserID; if (curUser.IsStudent) { dataRange = SYS_DataRange.Teacher; } var menuNo = HttpHelper.GetMenuNo(); if (string.IsNullOrEmpty(menuNo)) { menuNo = HttpContext.Current.Request.UrlReferrer.GetQueryStringByPath("FromMNU"); } dataRange = DataRangeDAL.GetRoleDataRange(curUser.RoleID, menuNo); IQueryable userIDQueryable; switch (dataRange) { case SYS_DataRange.All: default: return (x => true); case SYS_DataRange.Campus: userIDQueryable = UnitOfWork.CF_StaffManageCampus.Where(x => x.UserID == curUserID) .SelectMany(x => x.CF_Campus.CF_College).SelectMany(x => x.CF_Staff).Select(x => (Guid?)x.UserID); break; case SYS_DataRange.College: userIDQueryable = UnitOfWork.CF_StaffManageCollege.Where(x => x.UserID == curUserID) .SelectMany(x => x.CF_College.CF_Staff).Select(x => (Guid?)x.UserID); break; case SYS_DataRange.Department: userIDQueryable = UnitOfWork.CF_StaffManageDepartment.Where(x => x.UserID == curUserID) .SelectMany(x => x.CF_Department.CF_Staff).Select(x => (Guid?)x.UserID); break; case SYS_DataRange.Assistant: return (x => false); case SYS_DataRange.Teacher: return Expression.Lambda>(Expression.Equal(userColumnExp.Body, Expression.Constant(curUserID, typeof(Guid?))), paramter); } return Expression.Lambda>(Expression.Call(typeof(Queryable), "Contains", new Type[] { typeof(Guid?) }, userIDQueryable.Expression, userColumnExp.Body), paramter); } /// /// /// /// /// /// protected Expression> GetDataRageExpressionByCollege(Expression> collegeColumn) { ParameterExpression paramter; var collegeColumnExp = collegeColumn; paramter = collegeColumnExp.Parameters[0]; SYS_DataRange dataRange; var curUser = EMIS.Utility.FormValidate.CustomPrincipal.Current; var curUserID = curUser.UserID; if (curUser.IsStudent) { dataRange = SYS_DataRange.Teacher; } var menuNo = HttpHelper.GetMenuNo(); if (string.IsNullOrEmpty(menuNo)) { menuNo = HttpContext.Current.Request.UrlReferrer.GetQueryStringByPath("FromMNU"); } dataRange = DataRangeDAL.GetRoleDataRange(curUser.RoleID, menuNo); IQueryable collegeIDQueryable; switch (dataRange) { case SYS_DataRange.All: default: return (x => true); case SYS_DataRange.Campus: collegeIDQueryable = UnitOfWork.CF_StaffManageCampus.Where(x => x.UserID == curUserID) .SelectMany(x => x.CF_Campus.CF_College).Select(x => (Guid?)x.CollegeID); break; case SYS_DataRange.College: collegeIDQueryable = UnitOfWork.CF_StaffManageCollege.Where(x => x.UserID == curUserID) .Select(x => (Guid?)x.CollegeID); break; case SYS_DataRange.Department: collegeIDQueryable = UnitOfWork.CF_StaffManageDepartment.Where(x => x.UserID == curUserID) .Select(x => (Guid?)x.CF_Department.CollegeID); break; case SYS_DataRange.Assistant: return (x => false); case SYS_DataRange.Teacher: return Expression.Lambda>(Expression.Equal(collegeColumnExp.Body, Expression.Constant(curUser.CollegeID, typeof(Guid?))), paramter); } return Expression.Lambda>(Expression.Call(typeof(Queryable), "Contains", new Type[] { typeof(Guid?) }, collegeIDQueryable.Expression, collegeColumnExp.Body), paramter); } /// /// /// /// /// /// /// /// protected IQueryable GetQueryByAssistant(IQueryable query, Expression> classmajorColumn = null, IQueryable defaultQuery = null) { ExpressionFactory factory = new ExpressionFactory(query); var outerExp = classmajorColumn; if (classmajorColumn == null) { ParameterExpression paramter = Expression.Parameter(typeof(T)); outerExp = Expression.Lambda>(Expression.Convert(Expression.Property(paramter, "ClassmajorID"), typeof(Guid?)), paramter); } var curUser = EMIS.Utility.FormValidate.CustomPrincipal.Current; var curUserID = curUser.UserID; if (curUser.IsStudent) { return query; } var menuNo = HttpHelper.GetMenuNo(); if (string.IsNullOrEmpty(menuNo)) { menuNo = HttpContext.Current.Request.UrlReferrer.GetQueryStringByPath("FromMNU"); } SYS_DataRange dataRange = DataRangeDAL.GetRoleDataRange(curUser.RoleID, menuNo); if (dataRange == SYS_DataRange.Assistant) { var queryWithClassmajor = query.Join( UnitOfWork.CF_Classmajor.AsQueryable().Where(x => x.UserID == curUserID), outerExp, (x => x.ClassmajorID), ((x, y) => x)); return queryWithClassmajor; } else { if (defaultQuery != null) { return defaultQuery; } else { return query; } } } /// /// /// /// /// /// /// protected IQueryable GetQueryByDataRange(IQueryable query, Expression> userColumn = null) { ExpressionFactory factory = new ExpressionFactory(query); var outerExp = userColumn; if (userColumn == null) { ParameterExpression paramter = Expression.Parameter(typeof(T)); outerExp = Expression.Lambda>(Expression.Property(paramter, "CreateUserID"), paramter); } var curUser = EMIS.Utility.FormValidate.CustomPrincipal.Current; var curUserID = curUser.UserID; if (curUser.IsStudent) { return query; } var menuNo = HttpHelper.GetMenuNo(); if (string.IsNullOrEmpty(menuNo)) { menuNo = HttpContext.Current.Request.UrlReferrer.GetQueryStringByPath("FromMNU"); } SYS_DataRange dataRange = DataRangeDAL.GetRoleDataRange(curUser.RoleID, menuNo); var queryWithUserAndDepartment = query .Join(UnitOfWork.CF_Staff.AsQueryable(), outerExp, (x => x.UserID), ((x, y) => new { result = x, user = y })); switch (dataRange) { case SYS_DataRange.All: default: return query; case SYS_DataRange.Campus: return query .Join( UnitOfWork.CF_Campus.SelectMany(x => x.CF_StaffManageCampus) .Where(x => x.UserID == curUser.UserID) .SelectMany(x => x.CF_Campus.CF_College.SelectMany(w => w.CF_Staff)), outerExp, (x => x.UserID), ((x, y) => x)); case SYS_DataRange.College: return query .Join( UnitOfWork.CF_College.SelectMany(x => x.CF_StaffManageCollege) .Where(x => x.UserID == curUser.UserID) .SelectMany(x => x.CF_College.CF_Staff), outerExp, (x => x.UserID), ((x, y) => x)); case SYS_DataRange.Department: return query .Join( UnitOfWork.CF_Department.SelectMany(x => x.CF_StaffManageDepartment) .Where(x => x.UserID == curUser.UserID) .SelectMany(x => x.CF_Department.CF_Staff), outerExp, (x => x.UserID), ((x, y) => x)); case SYS_DataRange.Assistant: return query.Where(x => false); case SYS_DataRange.Teacher: return query .Join( UnitOfWork.CF_Staff.Where(x => x.UserID == curUser.UserID), outerExp, (x => x.UserID), ((x, y) => x)); } } /// /// /// /// /// /// /// protected IQueryable GetQueryByDataRangeByCampus(IQueryable query, Expression> campusColumn = null) { ExpressionFactory factory = new ExpressionFactory(query); var outerExp = campusColumn; if (campusColumn == null) { ParameterExpression paramter = Expression.Parameter(typeof(T)); outerExp = Expression.Lambda>(Expression.Convert(Expression.Property(paramter, "CampusID"), typeof(Guid?)), paramter); } var curUser = EMIS.Utility.FormValidate.CustomPrincipal.Current; var curUserID = curUser.UserID; if (curUser.IsStudent) { return query; } var menuNo = HttpHelper.GetMenuNo(); if (string.IsNullOrEmpty(menuNo)) { menuNo = HttpContext.Current.Request.UrlReferrer.GetQueryStringByPath("FromMNU"); } SYS_DataRange dataRange = DataRangeDAL.GetRoleDataRange(curUser.RoleID, menuNo); var queryWithCampus = query .Join(UnitOfWork.CF_Campus.AsQueryable(), outerExp, (x => x.CampusID), ((x, y) => new { result = x, campus = y })); switch (dataRange) { case SYS_DataRange.All: default: return query; case SYS_DataRange.Campus: return queryWithCampus .Where(x => x.campus.CF_StaffManageCampus.Any(w => w.UserID == curUserID)) .Select(x => x.result); case SYS_DataRange.College: return queryWithCampus .Where(x => x.campus.CF_College.SelectMany(w => w.CF_StaffManageCollege).Any(w => w.UserID == curUserID)) .Select(x => x.result); case SYS_DataRange.Department: return queryWithCampus .Where(x => x.campus.CF_College.SelectMany(w => w.CF_Department).SelectMany(w => w.CF_StaffManageDepartment) .Any(w => w.UserID == curUserID)) .Select(x => x.result); case SYS_DataRange.Assistant: return query.Where(x => false); case SYS_DataRange.Teacher: return queryWithCampus .Where(x => x.campus.CF_College.SelectMany(w => w.CF_Staff).Any(w => w.UserID == curUserID)) .Select(x => x.result); } } /// /// /// /// /// /// /// protected IQueryable GetQueryByDataRangeByCollege(IQueryable query, Expression> collegeColumn = null) { ExpressionFactory factory = new ExpressionFactory(query); var outerExp = collegeColumn; if (collegeColumn == null) { ParameterExpression paramter = Expression.Parameter(typeof(T)); outerExp = Expression.Lambda>(Expression.Convert(Expression.Property(paramter, "CollegeID"), typeof(Guid?)), paramter); } var curUser = EMIS.Utility.FormValidate.CustomPrincipal.Current; var curUserID = curUser.UserID; if (curUser.IsStudent) { return query; } var menuNo = HttpHelper.GetMenuNo(); if (string.IsNullOrEmpty(menuNo)) { menuNo = HttpContext.Current.Request.UrlReferrer.GetQueryStringByPath("FromMNU"); } SYS_DataRange dataRange = DataRangeDAL.GetRoleDataRange(curUser.RoleID, menuNo); var queryWithCollege = query .Join(UnitOfWork.CF_College.AsQueryable(), outerExp, (x => x.CollegeID), ((x, y) => new { result = x, college = y })); switch (dataRange) { case SYS_DataRange.All: default: return query; case SYS_DataRange.Campus: return queryWithCollege .Where(x => x.college.CF_Campus.CF_StaffManageCampus.Any(w => w.UserID == curUserID)) .Select(x => x.result); case SYS_DataRange.College: return queryWithCollege .Where(x => x.college.CF_StaffManageCollege.Any(w => w.UserID == curUserID)) .Select(x => x.result); case SYS_DataRange.Department: return queryWithCollege .Where(x => x.college.CF_Department.SelectMany(w => w.CF_StaffManageDepartment).Any(w => w.UserID == curUserID)) .Select(x => x.result); case SYS_DataRange.Assistant: return query.Where(x => false); case SYS_DataRange.Teacher: return queryWithCollege .Where(x => x.college.CF_Staff.Any(w => w.UserID == curUserID)) .Select(x => x.result); } } /// /// /// /// /// /// /// protected IQueryable GetQueryByDataRangeByDepartment(IQueryable query, Expression> departmentColumn = null) { ExpressionFactory factory = new ExpressionFactory(query); var outerExp = departmentColumn; if (departmentColumn == null) { ParameterExpression paramter = Expression.Parameter(typeof(T)); outerExp = Expression.Lambda>(Expression.Convert(Expression.Property(paramter, "DepartmentID"), typeof(Guid?)), paramter); } var curUser = EMIS.Utility.FormValidate.CustomPrincipal.Current; var curUserID = curUser.UserID; if (curUser.IsStudent) { return query; } var menuNo = HttpHelper.GetMenuNo(); if (string.IsNullOrEmpty(menuNo)) { menuNo = HttpContext.Current.Request.UrlReferrer.GetQueryStringByPath("FromMNU"); } SYS_DataRange dataRange = DataRangeDAL.GetRoleDataRange(curUser.RoleID, menuNo); var queryWithDepartment = query .Join(UnitOfWork.CF_Department.AsQueryable(), outerExp, (x => x.DepartmentID), ((x, y) => new { result = x, department = y })); switch (dataRange) { case SYS_DataRange.All: default: return query; case SYS_DataRange.Campus: return queryWithDepartment .Where(x => x.department.CF_College.CF_Campus.CF_StaffManageCampus.Any(w => w.UserID == curUserID)) .Select(x => x.result); case SYS_DataRange.College: return queryWithDepartment .Where(x => x.department.CF_College.CF_StaffManageCollege.Any(w => w.UserID == curUserID)) .Select(x => x.result); case SYS_DataRange.Department: return queryWithDepartment .Where(x => x.department.CF_StaffManageDepartment.Any(w => w.UserID == curUserID)) .Select(x => x.result); case SYS_DataRange.Assistant: return query.Where(x => false); case SYS_DataRange.Teacher: return queryWithDepartment .Where(x => x.department.CF_Staff.Any(w => w.UserID == curUserID)) .Select(x => x.result); } } /// /// /// /// /// /// /// /// protected bool IsUserInDataRange(List formIDList, Guid userID, Expression> userColumn = null) where T : class { var outerExp = userColumn; ParameterExpression paramter = Expression.Parameter(typeof(T)); if (userColumn == null) { outerExp = Expression.Lambda>(Expression.Convert(Expression.Property(paramter, "CreateUserID"), typeof(Guid?)), paramter); } var containMethodInfo = formIDList.GetType().GetMethod("Contains"); var keyExp = Expression.Lambda>( Expression.Call(Expression.Constant(formIDList), containMethodInfo, Expression.Property(paramter, TableKeyDictionary.GetKeyName())), paramter ); var menuNo = HttpHelper.GetMenuNo(); if (string.IsNullOrEmpty(menuNo)) { menuNo = HttpContext.Current.Request.UrlReferrer.GetQueryStringByPath("FromMNU"); } SYS_DataRange dataRange = DataRangeDAL.GetUserDataRange(userID, menuNo); switch (dataRange) { case SYS_DataRange.All: default: return true; case SYS_DataRange.Campus: return UnitOfWork.Set().Where(keyExp) .Join( UnitOfWork.CF_Campus.SelectMany(x => x.CF_StaffManageCampus).Select(x => x.CF_Staff) .Where(x => x.UserID == userID), outerExp, (x => x.UserID), ((x, y) => x)) .Count() == formIDList.Count; case SYS_DataRange.College: return UnitOfWork.Set().Where(keyExp) .Join( UnitOfWork.CF_College.SelectMany(x => x.CF_StaffManageCollege).Select(x => x.CF_Staff) .Where(x => x.UserID == userID), outerExp, (x => x.UserID), ((x, y) => x)) .Count() == formIDList.Count; case SYS_DataRange.Department: return UnitOfWork.Set().Where(keyExp) .Join( UnitOfWork.CF_Department.SelectMany(x => x.CF_StaffManageDepartment).Select(x => x.CF_Staff) .Where(x => x.UserID == userID), outerExp, (x => x.UserID), ((x, y) => x)) .Count() == formIDList.Count; case SYS_DataRange.Assistant: return false; case SYS_DataRange.Teacher: return UnitOfWork.Set().Where(keyExp) .Join( UnitOfWork.CF_Staff.Where(x => x.UserID == userID), outerExp, (x => x.UserID), ((x, y) => x)) .Count() == formIDList.Count; } } /// /// /// /// /// /// /// /// protected bool IsUserInDataRangeByDepartment(List formIDList, Guid userID, Expression> departmentColumn = null) where T : class { var outerExp = departmentColumn; ParameterExpression paramter = Expression.Parameter(typeof(T)); if (departmentColumn == null) { outerExp = Expression.Lambda>(Expression.Convert(Expression.Property(paramter, "DepartmentID"), typeof(Guid?)), paramter); } var containMethodInfo = formIDList.GetType().GetMethod("Contains"); var keyExp = Expression.Lambda>( Expression.Call(Expression.Constant(formIDList), containMethodInfo, Expression.Property(paramter, TableKeyDictionary.GetKeyName())), paramter ); var menuNo = HttpHelper.GetMenuNo(); if (string.IsNullOrEmpty(menuNo)) { menuNo = HttpContext.Current.Request.UrlReferrer.GetQueryStringByPath("FromMNU"); } SYS_DataRange dataRange = DataRangeDAL.GetUserDataRange(userID, menuNo); var queryWithDepartment = UnitOfWork.Set().Where(keyExp) .Join(UnitOfWork.CF_Department.AsQueryable(), outerExp, (x => x.DepartmentID), ((x, y) => new { result = x, department = y })); switch (dataRange) { case SYS_DataRange.All: default: return true; case SYS_DataRange.Campus: return queryWithDepartment .Where(x => x.department.CF_College.CF_Campus.CF_StaffManageCampus.Any(w => w.UserID == userID)) .Count() == formIDList.Count; case SYS_DataRange.College: return queryWithDepartment .Where(x => x.department.CF_College.CF_StaffManageCollege.Any(w => w.UserID == userID)) .Count() == formIDList.Count; case SYS_DataRange.Department: return queryWithDepartment .Where(x => x.department.CF_StaffManageDepartment.Any(w => w.UserID == userID)) .Count() == formIDList.Count; case SYS_DataRange.Assistant: return false; case SYS_DataRange.Teacher: return queryWithDepartment .Where(x => x.department.CF_Staff.Any(w => w.UserID == userID)) .Count() == formIDList.Count; } } /// /// /// /// /// /// /// /// protected bool IsUserInDataRangeByCollege(List formIDList, Guid userID, Expression> collegeColumn = null) where T : class { var outerExp = collegeColumn; ParameterExpression paramter = Expression.Parameter(typeof(T)); if (collegeColumn == null) { outerExp = Expression.Lambda>(Expression.Convert(Expression.Property(paramter, "CollegeID"), typeof(Guid?)), paramter); } var containMethodInfo = formIDList.GetType().GetMethod("Contains"); var keyExp = Expression.Lambda>( Expression.Call(Expression.Constant(formIDList), containMethodInfo, Expression.Property(paramter, TableKeyDictionary.GetKeyName())), paramter ); var menuNo = HttpHelper.GetMenuNo(); if (string.IsNullOrEmpty(menuNo)) { menuNo = HttpContext.Current.Request.UrlReferrer.GetQueryStringByPath("FromMNU"); } SYS_DataRange dataRange = DataRangeDAL.GetUserDataRange(userID, menuNo); var queryWithCollege = UnitOfWork.Set().Where(keyExp) .Join(UnitOfWork.CF_College.AsQueryable(), outerExp, (x => x.CollegeID), ((x, y) => new { result = x, college = y })); switch (dataRange) { case SYS_DataRange.All: default: return true; case SYS_DataRange.Campus: return queryWithCollege .Where(x => x.college.CF_Campus.CF_StaffManageCampus.Any(w => w.UserID == userID)) .Count() == formIDList.Count; case SYS_DataRange.College: return queryWithCollege .Where(x => x.college.CF_StaffManageCollege.Any(w => w.UserID == userID)) .Count() == formIDList.Count; case SYS_DataRange.Department: return queryWithCollege .Where(x => x.college.CF_Department.SelectMany(w => w.CF_StaffManageDepartment).Any(w => w.UserID == userID)) .Count() == formIDList.Count; case SYS_DataRange.Assistant: return false; case SYS_DataRange.Teacher: return queryWithCollege .Where(x => x.college.CF_Staff.Any(w => w.UserID == userID)) .Count() == formIDList.Count; } } /// /// /// /// /// /// /// /// protected bool IsUserInDataRangeByCampusID(List formIDList, Guid userID, Expression> campusColumn = null) where T : class { var outerExp = campusColumn; ParameterExpression paramter = Expression.Parameter(typeof(T)); if (campusColumn == null) { outerExp = Expression.Lambda>(Expression.Convert(Expression.Property(paramter, "CampusID"), typeof(Guid?)), paramter); } var containMethodInfo = formIDList.GetType().GetMethod("Contains"); var keyExp = Expression.Lambda>( Expression.Call(Expression.Constant(formIDList), containMethodInfo, Expression.Property(paramter, TableKeyDictionary.GetKeyName())), paramter ); var menuNo = HttpHelper.GetMenuNo(); if (string.IsNullOrEmpty(menuNo)) { menuNo = HttpContext.Current.Request.UrlReferrer.GetQueryStringByPath("FromMNU"); } SYS_DataRange dataRange = DataRangeDAL.GetUserDataRange(userID, menuNo); var queryWithCampus = UnitOfWork.Set().Where(keyExp) .Join(UnitOfWork.CF_Campus.AsQueryable(), outerExp, (x => x.CampusID), ((x, y) => new { result = x, campus = y })); switch (dataRange) { case SYS_DataRange.All: default: return true; case SYS_DataRange.Campus: return queryWithCampus .Where(x => x.campus.CF_StaffManageCampus.Any(w => w.UserID == userID)) .Count() == formIDList.Count; case SYS_DataRange.College: return queryWithCampus .Where(x => x.campus.CF_College.SelectMany(w => w.CF_StaffManageCollege).Any(w => w.UserID == userID)) .Count() == formIDList.Count; case SYS_DataRange.Department: return queryWithCampus .Where(x => x.campus.CF_College.SelectMany(w => w.CF_Department).SelectMany(w => w.CF_StaffManageDepartment).Any(w => w.UserID == userID)) .Count() == formIDList.Count; case SYS_DataRange.Assistant: return false; case SYS_DataRange.Teacher: return queryWithCampus .Where(x => x.campus.CF_College.SelectMany(w => w.CF_Staff).Any(w => w.UserID == userID)) .Count() == formIDList.Count; } } /// /// Add /// /// /// public void SetNewStatus(T entity) { var curUser = EMIS.Utility.FormValidate.CustomPrincipal.Current; var curUserID = curUser.UserID; //typeof(T).GetProperty(TableKeyDictionary.GetKeyName(entity)) // .SetValue(entity, Guid.NewGuid(), null); dynamic dyEntity = entity; dyEntity.RecordStatus = (int)SYS_STATUS.USABLE; dyEntity.CreateUserID = curUserID; dyEntity.ModifyUserID = curUserID; dyEntity.ModifyTime = dyEntity.CreateTime = DateTime.Now; } /// /// Add /// /// /// /// public void SetNewStatus(T entity, int recordStatus) { var curUser = EMIS.Utility.FormValidate.CustomPrincipal.Current; var curUserID = curUser.UserID; //typeof(T).GetProperty(TableKeyDictionary.GetKeyName(entity)) // .SetValue(entity, Guid.NewGuid(), null); dynamic dyEntity = entity; dyEntity.RecordStatus = recordStatus; dyEntity.CreateUserID = curUserID; dyEntity.ModifyUserID = curUserID; dyEntity.ModifyTime = dyEntity.CreateTime = DateTime.Now; } /// /// Update /// /// /// public void SetModifyStatus(T entity) { var curUser = EMIS.Utility.FormValidate.CustomPrincipal.Current; var curUserID = curUser.UserID; dynamic dyEntity = entity; dyEntity.ModifyUserID = curUserID; dyEntity.ModifyTime = DateTime.Now; } } }