private async Task DoRun() { IDBHelper db = new MssqlHelper(JobConfig.ConnectionStrings["YiliscrmDb"]); var accounts = await db.FindToListAsync <MpAccountDto>("SELECT * FROM MpAccounts WHERE ISDELETED=0 ", null, false); var Messages = await db.FindToListAsync <MpMessageDto>($"SELECT * FROM MpMessages WHERE ISDELETED=0 AND ISTASK=1 AND ExecTaskTime<=GETDATE() AND SendState={(int)MpMessageTaskState.Wait}", null, false); List <string> successList = new List <string>(); foreach (var args in Messages) { var account = accounts.Where(m => m.Id == args.MpID).FirstOrDefault(); args.WxMsgID = Guid.NewGuid().ToString(); await db.ExcuteNonQueryAsync($"Update MpMessages Set SendState='{(int)MpMessageTaskState.Doing}' Where ID='{args.Id}'", null, false);//正在发送 if (!string.IsNullOrWhiteSpace(args.GroupIds)) { if (args.IsMember == IsMemberEnum.ALL.ToString()) { var fancount = await db.ExecuteScalarAsync("SELECT count(*) FROM MpFans WHERE ISDELETED=0 AND MpID=" + args.MpID, null, false); await SendAllAsync(account, args, Convert.ToInt64(fancount)); } else if (args.IsMember == IsMemberEnum.Tag.ToString()) { var tagids = new List <int>(); var tagidstr = args.TargetID.Split(',', StringSplitOptions.RemoveEmptyEntries); int tagid = -1; foreach (var item in tagidstr) { if (int.TryParse(item, out tagid)) { tagids.Add(tagid); } } if (tagids.Count == 0) { tagids.Add(-1); } var _fan = await db.FindToListAsync <MpFanDto>("SELECT a.OpenID FROM MpFans a inner join MpFansTagItems b on a.Id=b.FansId WHERE a.ISDELETED=0 AND a.MpID=" + args.MpID + " AND b.TagId in (" + string.Join(",", tagids) + ")", null, false); await SaveIntoGroupMessage(args, _fan.Select(m => m.OpenID)); await SendMessageAsync(account, args, _fan.LongCount()); } else if (args.IsMember == IsMemberEnum.NotMember.ToString()) { var _fan = await db.FindToListAsync <MpFanDto>("SELECT OpenID FROM MpFans WHERE ISDELETED=0 AND MemberID=0 AND MpID=" + args.MpID, null, false); await SaveIntoGroupMessage(args, _fan.Select(m => m.OpenID)); await SendMessageAsync(account, args, _fan.LongCount()); } else { var messageResult = await StaticObjects.GetMessageResponse(account.Token, account.Id, args.Id); await SaveIntoGroupMessage(args, messageResult.OpenIDs); await SendMessageAsync(account, args, messageResult.OpenIDs.LongCount()); } } else { var fancount = await db.ExecuteScalarAsync("SELECT count(*) FROM MpFans WHERE ISDELETED=0 AND MpID=" + args.MpID, null, false); await SendAllAsync(account, args, Convert.ToInt64(fancount)); } } }