批量发送(批量发信息怎么发)
蕞近在项目中遇到了群发短信的需求。
需求点包括:
1.给符合条件的人群发优惠券短信
2.并对发送短信做记录,成功或者失败。
短信接口:
api中有群发短信的接口,一组蕞大为200条。
思路:
1.发送的手机集合放进一个队列
2.依次读取队列,放到待发送列表,当满足200条时,调用接口发送,直到队列数据发完
3.发送结果放进已发送短信结果集合
4.把发送结果批量插入数据库
代码:
群发helper
/// <summary> /// 群发短信 /// </summary> public class SmsGroupHelper { //每次发送短信数量 private readonly static int SmsCount = Globals.SafeInt, 0); /// <summary> /// 群发短信 /// </summary> /// <param name="content">短信内容</param> /// <param name="phones">手机号组</param> /// <returns></returns> public static SmsResult SendGroupSms { var phoneQueue = new Queue<string>; var unSendSmsList = new List<string>; //待发送 var sendedSmsList = new List<Sms>; //已发送数量 var sendFailed = new List<string>; //发送失败数量 var sendSuccess = new List<string>; //发送成功数量 var smsResult = new SmsResult; smsResult.ErrMsg = "无"; try { //1.先发送短信 ,根据短信发送情况,保存数据库记录 foreach { phoneQueue.Enqueue; } //是否发送短信 var isSendFlag = false; while { if { var phoneNum = phoneQueue.Dequeue; unSendSmsList.Add; //如果达到一个发送短信包数量 if { isSendFlag = true; } } else { //如果还有短信没发出去 if { isSendFlag = true; } else { isSendFlag = false; break; } } if { var phoneArr = unSendSmsList.ToArray; var resposeMsg = ""; //发送短信短信接口 var flag = SMSHelper.SendSms; var sendResult = "短信发送成功"; if { sendSuccess.AddRange; } else { sendResult = "短信发送失败"; sendFailed.AddRange; } //记录总发送数量 sendedSmsList.AddRange); //清空未发送列表 unSendSmsList = new List<string>; isSendFlag = false; } } } catch { smsResult.ErrMsg = ex.Message; while { var p = phoneQueue.Dequeue; sendFailed.Add; sendedSmsList.Add { Phone = p, CreateTime = DateTime.Now, MessageStatus = "短信发送失败", MessageRemarks = content }); } } //批量插入短信结果到数据库 DapperHelper.BulkInsert; smsResult.Send = sendedSmsList.Count; smsResult.Success = sendSuccess.Count; smsResult.Failed = sendFailed.Count; return smsResult; }
批量插入数据
public class TableColumn { public string COLUMN_NAME { get; set; } public string DATA_TYPE { get; set; } }
#region Bulk批量插入 public static Type MapCommonType { if ) return Type.Missing.GetType; dbtype = dbtype.ToLower; Type commonType = typeof; switch { case "bigint": commonType = typeof; break; case "binary": commonType = typeof; break; case "bit": commonType = typeof; break; case "char": commonType = typeof; break; case "date": commonType = typeof; break; case "datetime": commonType = typeof; break; case "datetime2": commonType = typeof; break; case "datetimeoffset": commonType = typeof; break; case "decimal": commonType = typeof; break; case "float": commonType = typeof; break; case "image": commonType = typeof; break; case "int": commonType = typeof; break; case "money": commonType = typeof; break; case "nchar": commonType = typeof; break; case "ntext": commonType = typeof; break; case "numeric": commonType = typeof; break; case "nvarchar": commonType = typeof; break; case "real": commonType = typeof; break; case "smalldatetime": commonType = typeof; break; case "smallint": commonType = typeof; break; case "smallmoney": commonType = typeof; break; case "sql_variant": commonType = typeof; break; case "sysname": commonType = typeof; break; case "text": commonType = typeof; break; case "time": commonType = typeof; break; case "timestamp": commonType = typeof; break; case "tinyint": commonType = typeof; break; case "uniqueidentifier": commonType = typeof; break; case "varbinary": commonType = typeof; break; case "varchar": commonType = typeof; break; case "xml": commonType = typeof; break; default: commonType = typeof; break; } return commonType; } //获取表结构 public static List<TableColumn> GetTableColumns { var columns = DapperHelper.GetList<TableColumn>, null); return columns; } /// <summary> /// 获取表数据 /// </summary> /// <param name="tableName">数据表名称</param> /// <param name="lists">数据集合</param> /// <returns></returns> public static void BulkInsert<T> { var type = typeof ; var fields = type.GetProperties; var columns = GetTableColumns; DataTable dt = new DataTable; foreach { dt.Columns.Add)); } foreach { var row = dt.NewRow; foreach { var column1 = column; foreach )) { row[column.COLUMN_NAME] = field.GetValue ?? DBNull.Value; break; } } dt.Rows.Add; } BulkToDB; } //批量插入数据 public static void BulkToDB { SqlConnection con = DapperConnection.CreateCon as SqlConnection; SqlBulkCopy bulkCopy = new SqlBulkCopy; bulkCopy.DestinationTableName = tableName; bulkCopy.BatchSize = dt.Rows.Count; try { con.Open; if bulkCopy.WriteToServer; } catch { throw ex; } finally { con.Close; if bulkCopy.Close; } } #endregion
海外精品引流脚本–最强海外引流
查看演示与获取方案
读完本篇后,可通过下方入口查看演示视频、联系客服或访问主站。

