/// <summary> /// 同步微信用户 /// </summary> private async Task <bool> SyncWeChatUsers(bool isUserSync = false, string createBy = null) { try { _db.Database.ExecuteSqlCommand("Delete from MWC.WeiChat_User where TenantId={0}", _tenantId); logger.Log(LoggerLevels.Debug, "已清除租户【" + _tenantId + "】的粉丝信息。"); //TODO:暂时不支持批量移除 ////批量移除 //_db.BathRemoveBy(_db.WeiChat_Users, p => p.TenantId == _tenantId); var userApi = new UserApi(); userApi.SetKey(_tenantId); var opendIds = new List <string>(); //递归获取所有的OPENID GetOpenIds(opendIds, userApi); if (opendIds.Count == 0) { return(await System.Threading.Tasks.Task.FromResult(false)); } logger.Log(LoggerLevels.Debug, "待同步粉丝数:" + opendIds.Count); var distinctOpendIds = opendIds.Distinct().ToList(); if (distinctOpendIds.Count != opendIds.Count) { logger.Log(LoggerLevels.Debug, "粉丝数存在重复项。去重后粉丝数:" + distinctOpendIds.Count); opendIds = distinctOpendIds; } var userInfoList = new List <UserBatchGetApiResult.UserInfo>(); while (opendIds.Count > 0) { var taskCount = opendIds.Count > 10000 ? 100 : opendIds.Count / 100 + 1; var successList = new List <string>(); GetUserInfoList(userApi, opendIds, userInfoList, taskCount, successList); //opendIds.RemoveAll(p => successList.Any(p1 => p1 == p)); var hs = new HashSet <string>(opendIds); var successhs = new HashSet <string>(successList); hs.RemoveWhere(p => successhs.Contains(p)); opendIds = hs.ToList(); logger.Log(LoggerLevels.Debug, "successList:【" + string.Join(",", successList) + "】。"); } logger.Log(LoggerLevels.Debug, "获取粉丝数据完成。数量:" + userInfoList.Count); var userList = userInfoList.Select(userInfo => new WeiChat_User { City = userInfo.City, Country = userInfo.Country, GroupId = userInfo.GroupId, HeadImgUrl = userInfo.Headimgurl, Language = userInfo.Language, NickName = userInfo.NickName, OpenId = userInfo.OpenId, Province = userInfo.Province, Remark = userInfo.Remark, Sex = userInfo.Sex, Subscribe = userInfo.Subscribe, SubscribeTime = userInfo.SubscribeTime == default(DateTime) ? DateTime.Parse("2011-05-10") : userInfo.SubscribeTime, UnionId = userInfo.Unionid, TenantId = _tenantId }).ToList(); logger.Log(LoggerLevels.Debug, "待插入的粉丝数:【" + string.Join(",", userList.Count) + "】。"); //TODO:插入之前判断是否在处理期间有用户关注,如果有,则进行处理 //批量插入 _db.BathInsert(_db.WeiChat_Users, userList); logger.Log(LoggerLevels.Debug, "批量插入已完成。"); var log = new WeiChat_SyncLog { Type = WeiChat_SyncTypes.Sync_WeiChat_User, IsUserSync = isUserSync, TenantId = _tenantId, CreateBy = createBy, Success = true, Message = string.Format("同步成功!同步数量:{0}。", userList.Count) }; _db.WeiChat_SyncLogs.Add(log); await _db.SaveChangesAsync(); } catch (DbEntityValidationException ex) { var sb = new StringBuilder(); var validationException = ex; sb.AppendLine("实体验证错误。错误数:" + validationException.EntityValidationErrors.Count() + " "); foreach (var validationResult in validationException.EntityValidationErrors) { foreach (var item in validationResult.ValidationErrors) { sb.AppendFormat("({0}:{1})", item.PropertyName, item.ErrorMessage); } } logger.Log(LoggerLevels.Error, sb.ToString()); var log = new WeiChat_SyncLog { Type = WeiChat_SyncTypes.Sync_WeiChat_User, IsUserSync = isUserSync, TenantId = _tenantId, CreateBy = createBy, Message = string.Format("同步失败!具体异常:{0}。", sb), Success = false }; _db.WeiChat_SyncLogs.Add(log); //为了兼容VS2013编译器语法 //await _db.SaveChangesAsync(); //return await System.Threading.Tasks.Task.FromResult(false); _db.SaveChanges(); return(false); } catch (Exception ex) { logger.Log(LoggerLevels.Debug, "粉丝信息同步失败。"); logger.LogException(ex); var log = new WeiChat_SyncLog { Type = WeiChat_SyncTypes.Sync_WeiChat_User, IsUserSync = isUserSync, TenantId = _tenantId, CreateBy = createBy, Message = string.Format("同步失败!具体异常:{0}。", ex), Success = false }; _db.WeiChat_SyncLogs.Add(log); //为了兼容VS2013编译器语法 //await _db.SaveChangesAsync(); //return await System.Threading.Tasks.Task.FromResult(false); _db.SaveChanges(); return(false); } return(await System.Threading.Tasks.Task.FromResult(true)); }