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)); }
public void UserGroupApiTest_CURD() { if (TestOpenIdList.Count == 0) { Assert.Fail("测试失败,必须设置测试账户,见WeiChat_User中的AllowTest!"); } #region 查询测试 var getResult = api.Get(); if (getResult.Groups.Count == 0) { Assert.Fail("创建组失败或查询失败,返回结果如下:" + getResult.DetailResult); } #endregion #region 测试创建 var createResult = api.Create("Test"); if (!createResult.IsSuccess()) { Assert.Fail("测试失败,返回结果如下:" + createResult.DetailResult); } #endregion #region 查询测试(ById) var getByIdResult = api.GetById(TestOpenIdList.First()); if (!getByIdResult.IsSuccess()) { Assert.Fail("查询用户所在组失败,返回结果如下:" + getByIdResult.DetailResult); } #endregion Thread.Sleep(10000); #region 修改测试 var updateResult = api.Update(createResult.Group.Id, "Rename Test"); if (!updateResult.IsSuccess()) { Assert.Fail("用户组信息修改失败,返回结果如下:" + updateResult.DetailResult); } #endregion //该接口不耐操,需要等待 Thread.Sleep(20000); #region 移动用户分组测试 var memeberUpdateResult = api.MemeberUpdate(TestOpenIdList.First(), createResult.Group.Id); if (!memeberUpdateResult.IsSuccess()) { Assert.Fail("移动用户分组失败,返回结果如下:{0},输入信息:{1}", memeberUpdateResult.DetailResult, createResult.Group.Id); } #endregion //该接口不耐操,需要等待 Thread.Sleep(15000); #region 批量移动用户分组测试 getResult = api.Get(); var memeberUpdateResult_ = api.MemeberUpdate(TestOpenIdList.ToArray(), createResult.Group.Id); if (!memeberUpdateResult_.IsSuccess()) { Assert.Fail("批量移动用户分组测试,返回结果如下:" + memeberUpdateResult_.DetailResult); } #endregion //该接口不耐操,需要等待 Thread.Sleep(10000); #region 除测试 getResult = api.Get(); foreach (var item in getResult.Groups) { if ((item.Name == "Test") || (item.Name == "Rename Test")) { TestContext.WriteLine("删除信息id:{0},name:{1}", item.Id, item.Name); var deleteResult = api.Delete(item.Id); if (!deleteResult.IsSuccess() && (deleteResult.DetailResult != "{}")) { Assert.Fail("删除测试分组失败,返回结果如下:{0},输入参数:{1}", deleteResult.DetailResult, item.Id); } } } #endregion }