public int UpdateSystemUser(SystemUser entity, string ApplicationID)
        {
            //if (entity.SysNo == 1)
            //{
            //    throw new BusinessException("超级用户不能修改");
            //}
            CheckSystemUser(entity, false);
            int    EditUserSysNo = DataContext.GetContextItemInt("UserSysNo", 0);
            string EditUserName  = DataContext.GetContextItemString("UserDisplayName");

            entity.EditUserSysNo = EditUserSysNo;
            entity.EditUserName  = EditUserName;
            // lock ("check_app")
            //{
            var hasApp     = AuthDA.GetSystemApplicationsByUserSysNo(new int[] { entity.SysNo });
            var roleHasApp = AuthDA.GetSystemApplicationsByUserRole(entity.SysNo);

            var needInsert = entity.Applications.Except(hasApp, new SystemApplicationComparer());
            var needDelete = hasApp.Except(entity.Applications, new SystemApplicationComparer());

            StringBuilder sb = new StringBuilder();

            foreach (var item in needDelete)
            {
                if (roleHasApp.FirstOrDefault(x => x.ApplicationID == item.ApplicationID) != null)
                {
                    sb.AppendLine(string.Format("用户还拥有系统{0}中的角色,不能移除所属的系统{0}", item.Name));
                }
            }
            if (sb.ToString().Length > 0)
            {
                throw new BusinessException(sb.ToString());
            }
            foreach (var item in needInsert)
            {
                SystemUserDA.InsertSystemUser_Application(entity.SysNo, item.ApplicationID);
            }
            foreach (var item in needDelete)
            {
                SystemUserDA.DeleteSystemUser_Application(entity.SysNo, item.ApplicationID);
            }
            // }
            return(SystemUserDA.UpdateSystemUser(entity, ApplicationID));
        }
        public int UpdateSystemUser(SystemUser entity)
        {
            //if (entity.SysNo == 1)
            //{
            //    throw new BusinessException("超级用户不能修改");
            //}
            CheckSystemUser(entity, false);

            if (entity.Applications == null)
            {
                entity.Applications = new List <SystemApplication>();
            }
            var hasApp     = AuthDA.GetSystemApplicationsByUserSysNo(new int[] { entity.SysNo });
            var roleHasApp = AuthDA.GetSystemApplicationsByUserRole(entity.SysNo);

            var needInsert = entity.Applications.Except(hasApp, new SystemApplicationComparer());
            var needDelete = hasApp.Except(entity.Applications, new SystemApplicationComparer());

            StringBuilder sb = new StringBuilder();

            foreach (var item in needDelete)
            {
                if (roleHasApp.FirstOrDefault(x => x.ApplicationID == item.ApplicationID) != null)
                {
                    sb.AppendLine(string.Format("用户还拥有系统{0}中的角色,不能移除所属的系统{0}", item.Name));
                }
            }
            if (sb.ToString().Length > 0)
            {
                throw new BusinessException(sb.ToString());
            }

            foreach (var item in needInsert)
            {
                SystemUserDA.InsertSystemUser_Application(entity.SysNo, item.ApplicationID);
            }
            foreach (var item in needDelete)
            {
                SystemUserDA.DeleteSystemUser_Application(entity.SysNo, item.ApplicationID);
            }
            return(SystemUserDA.UpdateSystemUser(entity));
        }