private OPResult Update(SysUserBO user) { var roleIDs = RolesCurrentUserCover.Select(rs => rs.ID);//加上ToList()在下面执行时就抛出Contains不支持的异常,坑爹 List <SysUserRole> urs = new List <SysUserRole>(); foreach (var rs in user.Roles) { SysUserRole ur = new SysUserRole { UserId = user.ID, RoleId = rs.ID, CreatorID = VMGlobal.CurrentUser.ID }; urs.Add(ur); } var brandIDs = VMGlobal.PoweredBrands.Select(bs => bs.ID); List <UserBrand> ubs = new List <UserBrand>(); foreach (var bs in user.Brands) { UserBrand ub = new UserBrand { UserID = user.ID, BrandID = bs.ID, CreatorID = VMGlobal.CurrentUser.ID }; ubs.Add(ub); } using (TransactionScope scope = new TransactionScope()) { try { LinqOP.Update <SysUser>(user); LinqOP.Delete <SysUserRole>(ur => roleIDs.Contains(ur.RoleId) && ur.UserId == user.ID); VMGlobal.SysProcessQuery.LinqOP.Delete <UserBrand>(ub => brandIDs.Contains(ub.BrandID) && ub.UserID == user.ID); LinqOP.Add <SysUserRole>(urs); VMGlobal.SysProcessQuery.LinqOP.Add <UserBrand>(ubs); scope.Complete(); return(new OPResult { IsSucceed = true, Message = "更新成功." }); } catch (Exception e) { return(new OPResult { IsSucceed = false, Message = "更新失败,失败原因:\n" + e.Message }); } } }
//用户对应的各种数据集合,避免在UI绑定时频繁操作数据库 //private void RefreshAttachData() //{ // var users = (List<SysUser>)Users.SourceCollection; // var uIDs = users.Select<SysUser, int>(u => u.ID); // var urs = _query.QueryProvider.GetTable<SysUserRole>("SysUserRole"); // var queryUR = from ur in urs where uIDs.Contains(ur.UserId) select ur; // UserRoles = queryUR.ToList(); // var ubs = _queryDistribution.QueryProvider.GetTable<UserBrand>("UserBrand"); // var queryUB = from ub in ubs where uIDs.Contains(ub.UserID) select ub; // UserBrands = queryUB.ToList(); // var oIDs = users.Select<SysUser, int>(u => u.OrganizationID); // Organizations = _query.LinqOP.Search<SysOrganization>(o => oIDs.Contains(o.ID)).ToList(); //} private OPResult Add(SysUserBO user) { int?userpointlimit = null; try { userpointlimit = this.GetUserPointLimit(); if (userpointlimit == null) { return new OPResult { IsSucceed = false, Message = "保存失败,失败原因:\n无法确认用户点数是否存在上限,请联系软件公司。" } } ; if ((userpointlimit != -1) && userpointlimit <= LinqOP.Search <SysUser>().Count()) { return(new OPResult { IsSucceed = false, Message = "保存失败,失败原因:\n超过用户点数限制,若要增加用户点数,请联系软件公司。" }); } } catch (Exception e) { return(new OPResult { IsSucceed = false, Message = "保存失败,失败原因:\n" + e.Message }); } string oginalPWD = user.Password; user.CreatorID = VMGlobal.CurrentUser.ID; user.Password = oginalPWD.ToMD5String(); using (TransactionScope scope = new TransactionScope()) { try { int id = LinqOP.Add <SysUser, int>(user, r => r.ID); user.ID = id; List <SysUserRole> urs = new List <SysUserRole>(); foreach (var r in user.Roles) { SysUserRole ur = new SysUserRole { UserId = id, RoleId = r.ID, CreatorID = VMGlobal.CurrentUser.ID }; urs.Add(ur); } LinqOP.Add <SysUserRole>(urs); List <UserBrand> ubs = new List <UserBrand>(); foreach (var b in user.Brands) { UserBrand ub = new UserBrand { UserID = id, BrandID = b.ID, CreatorID = VMGlobal.CurrentUser.ID }; ubs.Add(ub); } VMGlobal.SysProcessQuery.LinqOP.Add <UserBrand>(ubs); scope.Complete(); return(new OPResult { IsSucceed = true, Message = "保存成功." }); } catch (Exception e) { user.ID = default(int); user.Password = oginalPWD; return(new OPResult { IsSucceed = false, Message = "保存失败,失败原因:\n" + e.Message }); } } }
public override OPResult AddOrUpdate(SysUser entity) { SysUserBO user = (SysUserBO)entity; return(user.ID == default(int) ? this.Add(user) : this.Update(user)); }