private void SyncWeChatUserGroup() { var notify = NotifyInfo; var notifier = Notifier; var strs = Parameter.ToString().Split(';'); var tenantId = int.Parse(strs[0]); var userId = strs[1]; var logger = Logger; try { ReportProgress(1, "正在初始化同步信息"); var userGroupApi = new UserGroupApi(); userGroupApi.SetKey(tenantId); var result = userGroupApi.Get(); if (result.IsSuccess()) { ReportProgress(30, "已成功获取数据:" + result.Groups.Count); using (var db = new AppDbContext()) { TenantManager.Current.EnableTenantFilter(db, tenantId); db.WeiChat_UserGroups.RemoveRange( db.WeiChat_UserGroups.Where(p => !db.WeiChat_Users.Any(p1 => p1.GroupIds.Any(d => d == p.GroupId)))); db.SaveChanges(); var tenantGroups = db.WeiChat_UserGroups.ToList(); var groups = (from item in result.Groups //where !tenantGroups.Any(p => p.GroupIds == item.Id) where !tenantGroups.Any(p => p.GroupId == item.Id) select new WeiChat_UserGroup { //GroupIds = item.Id, Name = item.Name, UsersCount = item.Count, TenantId = tenantId }).ToList(); db.WeiChat_UserGroups.AddRange(groups); var count = result.Groups == null ? 0 : result.Groups.Count; ReportProgress(90, "正在写入数据,写入数量(" + count + ")..."); db.SaveChanges(); ReportProgress(100, "同步成功!同步数量(" + count + ")。"); } } else { ReportProgress(100, "同步失败!" + result.GetFriendlyMessage()); } } catch (Exception ex) { logger.Log(LoggerLevels.Debug, Title + " 失败!"); logger.LogException(ex); ReportProgress(100, "同步失败!" + ex.Message); return; } }
/// <summary> /// 同步粉丝组 /// </summary> private async Task <bool> SyncWeChatGroups(bool isUserSync = false, string createBy = null) { var userGroupApi = new UserGroupApi(); userGroupApi.SetKey(_tenantId); var getResult = userGroupApi.Get(); if (!getResult.IsSuccess()) { return(await System.Threading.Tasks.Task.FromResult(false)); } _db.WeiChat_UserGroups.RemoveRange( _db.WeiChat_UserGroups.Where(p => !_db.WeiChat_Users.Any(p1 => p1.GroupId == p.GroupId))); await _db.SaveChangesAsync(); var tenantGroups = _db.WeiChat_UserGroups.ToList(); var groups = (from item in getResult.Groups where !tenantGroups.Any(p => p.GroupId == item.Id) select new WeiChat_UserGroup { GroupId = item.Id, Name = item.Name, UsersCount = item.Count, TenantId = _tenantId }).ToList(); _db.WeiChat_UserGroups.AddRange(groups); var log = new WeiChat_SyncLog { Type = WeiChat_SyncTypes.Sync_WeiChat_UserGroup, IsUserSync = isUserSync, TenantId = _tenantId, CreateBy = createBy, Message = string.Format("同步成功!同步组数:{0}。", groups.Count) }; _db.WeiChat_SyncLogs.Add(log); await _db.SaveChangesAsync(); return(await System.Threading.Tasks.Task.FromResult(true)); }