public int InsertSystemUser(SystemUser entity)
        {
            //entity.CommonStatus = CommonStatus.Actived;
            CheckSystemUser(entity, true);
            int UserSysNo = 0;

            lock (insertLock)
            {
                if (SystemUserDA.CountLoginName(entity.LoginName, ConstValue.ApplicationID) > 0)
                {
                    throw new BusinessException(string.Format("手机号:{0} 在系统中已存在,请重新输入!", entity.LoginName));
                }
                UserSysNo = SystemUserDA.InsertSystemUser(entity);
            }
            entity.Applications.Clear();
            entity.Applications.Add(new SystemApplication {
                ApplicationID = ConstValue.ApplicationID
            });
            //批量插入用户系统对应表
            if (UserSysNo > 0 && entity.Applications != null && entity.Applications.Count() > 0)
            {
                entity.Applications.ForEach(x => SystemUserDA.InsertSystemUser_Application(UserSysNo, x.ApplicationID));
            }

            return(UserSysNo);
        }
        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));
        }