/// <summary> /// 写入定时发送短信任务计划 /// </summary> /// <param name="planInfo">任务计划信息业务实体</param> /// <returns></returns> public virtual bool InsertSendPlan(EyouSoft.Model.SMSStructure.SendPlanInfo planInfo) { DbCommand cmd = base.SMSStore.GetSqlStringCommand(SQL_INSERT_InsertSendPlan); StringBuilder mobiles = new StringBuilder(); StringBuilder encryptMobiles = new StringBuilder(); if (planInfo.Mobiles != null && planInfo.Mobiles.Count > 0) { /*if (planInfo.Mobiles[0].IsEncrypt) * { * encryptMobiles.Append(planInfo.Mobiles[0].Mobile); * } * else * { * mobiles.Append(planInfo.Mobiles[0].Mobile); * } * * for (int i = 1; i < planInfo.Mobiles.Count; i++) * { * if (planInfo.Mobiles[i].IsEncrypt) * { * encryptMobiles.AppendFormat(",{0}", planInfo.Mobiles[i].Mobile); * } * else * { * mobiles.AppendFormat(",{0}", planInfo.Mobiles[i].Mobile); * } * * }*/ foreach (var mobile in planInfo.Mobiles) { if (mobile.IsEncrypt) { encryptMobiles.AppendFormat("{0},", mobile.Mobile); } else { mobiles.AppendFormat("{0},", mobile.Mobile); } } } base.SMSStore.AddInParameter(cmd, "ID", DbType.AnsiStringFixedLength, planInfo.PlanId); base.SMSStore.AddInParameter(cmd, "CompanyID", DbType.AnsiStringFixedLength, planInfo.CompanyId); base.SMSStore.AddInParameter(cmd, "CompanyName", DbType.String, planInfo.CompanyName); base.SMSStore.AddInParameter(cmd, "UserID", DbType.AnsiStringFixedLength, planInfo.UserId); base.SMSStore.AddInParameter(cmd, "ContactName", DbType.String, planInfo.ContactName); base.SMSStore.AddInParameter(cmd, "SMSType", DbType.Int32, planInfo.SMSType); base.SMSStore.AddInParameter(cmd, "SMSContent", DbType.String, planInfo.SMSContent); base.SMSStore.AddInParameter(cmd, "MobileList", DbType.String, mobiles.ToString().Trim(',')); base.SMSStore.AddInParameter(cmd, "IssueTime", DbType.DateTime, planInfo.IssueTime); base.SMSStore.AddInParameter(cmd, "SendTime", DbType.DateTime, planInfo.SendTime); base.SMSStore.AddInParameter(cmd, "SendChannel", DbType.Int32, planInfo.SendChannel.Index); base.SMSStore.AddInParameter(cmd, "EncryptMobiles", DbType.String, encryptMobiles.ToString().Trim(',')); return(DbHelper.ExecuteSql(cmd, base.SMSStore) == 1 ? true : false); }
/// <summary> /// 获得要发送的短信 /// </summary> /// <returns></returns> public Queue <EyouSoft.Model.SMSStructure.SendPlanInfo> GetSends() { Queue <EyouSoft.Model.SMSStructure.SendPlanInfo> queue = new Queue <EyouSoft.Model.SMSStructure.SendPlanInfo>(); DbCommand cmd = this._db.GetSqlStringCommand(SQL_SELECT_GetSends); using (IDataReader rdr = DbHelper.ExecuteReader(cmd, this._db)) { while (rdr.Read()) { EyouSoft.Model.SMSStructure.SendPlanInfo item = new EyouSoft.Model.SMSStructure.SendPlanInfo(); item.PlanId = rdr.GetString(rdr.GetOrdinal("ID")); item.CompanyId = rdr.GetInt32(rdr.GetOrdinal("CompanyID")); item.CompanyName = rdr.IsDBNull(rdr.GetOrdinal("CompanyName")) ? "" : rdr.GetString(rdr.GetOrdinal("CompanyName")); item.UserId = rdr.GetInt32(rdr.GetOrdinal("UserID")); item.ContactName = rdr.IsDBNull(rdr.GetOrdinal("ContactName")) ? "" : rdr.GetString(rdr.GetOrdinal("ContactName")); item.SMSType = rdr.GetInt32(rdr.GetOrdinal("SMSType")); item.SMSContent = rdr.IsDBNull(rdr.GetOrdinal("SMSContent")) ? "" : rdr.GetString(rdr.GetOrdinal("SMSContent")); string mobile = rdr.IsDBNull(rdr.GetOrdinal("MobileList")) ? "" : rdr.GetString(rdr.GetOrdinal("MobileList")); string encryptMobiles = rdr.IsDBNull(rdr.GetOrdinal("EncryptMobiles")) ? "" : rdr.GetString(rdr.GetOrdinal("EncryptMobiles")); //显示时不加密手机号 item.Mobiles = this.GetAcceptMobiles(mobile, false); item.IssueTime = rdr.GetDateTime(rdr.GetOrdinal("IssueTime")); item.SendTime = rdr.GetDateTime(rdr.GetOrdinal("SendTime")); item.SendChannel = new EyouSoft.Model.SMSStructure.SMSChannel(); item.SendChannel.Index = rdr.GetInt32(rdr.GetOrdinal("SendChannel")); queue.Enqueue(item); } } //更新是否发送状态 if (queue != null && queue.Count > 0) { foreach (var item in queue) { SaveSendResult(item.PlanId, EyouSoft.Model.EnumType.SmsStructure.PlanStatus.发送成功, ""); } } return(queue); }
/// <summary> /// 发送短信 /// </summary> /// <param name="info">计划信息</param> private void SendSMS(EyouSoft.Model.SMSStructure.SendPlanInfo info) { EyouSoft.BackgroundServices.SMSSOAP.SMSChannel channel = SmsUtils.GetSmsSendChannel(info.SendChannel.Index); EyouSoft.BackgroundServices.SMSSOAP.SendMessageInfo messageInfo = new EyouSoft.BackgroundServices.SMSSOAP.SendMessageInfo(); EyouSoft.BackgroundServices.SMSSOAP.AcceptMobileInfo[] mobiles = new EyouSoft.BackgroundServices.SMSSOAP.AcceptMobileInfo[info.Mobiles.Count]; for (int i = 0; i < info.Mobiles.Count; i++) { mobiles[i] = new EyouSoft.BackgroundServices.SMSSOAP.AcceptMobileInfo(); mobiles[i].Mobile = info.Mobiles[i].Mobile; mobiles[i].IsEncrypt = info.Mobiles[i].IsEncrypt; } messageInfo.Mobiles = mobiles; messageInfo.CompanyId = info.CompanyId; messageInfo.CompanyName = info.CompanyName; messageInfo.SendChannel = channel; messageInfo.SendTime = info.SendTime; messageInfo.SendType = EyouSoft.BackgroundServices.SMSSOAP.SendType.直接发送; messageInfo.SMSContent = info.SMSContent; messageInfo.SMSType = info.SMSType; messageInfo.UserFullName = info.ContactName; messageInfo.UserId = info.UserId; EyouSoft.BackgroundServices.SMSSOAP.SMSAPI api = EyouSoft.Services.BackgroundServices.SmsUtils.GetSmsApi(); EyouSoft.BackgroundServices.SMSSOAP.SendResultInfo result = api.Send(messageInfo); /* * EyouSoft.Model.EnumType.SmsStructure.PlanStatus state = EyouSoft.Model.EnumType.SmsStructure.PlanStatus.未发送; * string stateDesc = string.Empty; * * if (result.IsSucceed) * { * state = EyouSoft.Model.EnumType.SmsStructure.PlanStatus.发送成功; * } * else * { * state = EyouSoft.Model.EnumType.SmsStructure.PlanStatus.发送失败; * stateDesc = result.ErrorMessage; * } * * dal.SaveSendResult(info.PlanId, state, stateDesc);*/ }
/// <summary> /// 写入定时发送短信任务计划 /// </summary> /// <param name="planInfo"></param> /// <returns></returns> public bool InsertSendPlan(EyouSoft.Model.SMSStructure.SendPlanInfo planInfo) { DbCommand cmd = this._db.GetSqlStringCommand(SQL_INSERT_InsertSendPlan); StringBuilder mobiles = new StringBuilder(); StringBuilder encryptMobiles = new StringBuilder(); if (planInfo.Mobiles != null && planInfo.Mobiles.Count > 0) { foreach (var mobile in planInfo.Mobiles) { if (mobile.IsEncrypt) { encryptMobiles.AppendFormat("{0},", mobile.Mobile); } else { mobiles.AppendFormat("{0},", mobile.Mobile); } } } this._db.AddInParameter(cmd, "ID", DbType.AnsiStringFixedLength, Guid.NewGuid().ToString()); this._db.AddInParameter(cmd, "CompanyID", DbType.AnsiStringFixedLength, planInfo.CompanyId); this._db.AddInParameter(cmd, "CompanyName", DbType.String, planInfo.CompanyName); this._db.AddInParameter(cmd, "UserID", DbType.AnsiStringFixedLength, planInfo.UserId); this._db.AddInParameter(cmd, "ContactName", DbType.String, planInfo.ContactName); this._db.AddInParameter(cmd, "SMSType", DbType.Int32, planInfo.SMSType); this._db.AddInParameter(cmd, "SMSContent", DbType.String, planInfo.SMSContent); this._db.AddInParameter(cmd, "MobileList", DbType.String, mobiles.ToString().Trim(',')); this._db.AddInParameter(cmd, "IssueTime", DbType.DateTime, planInfo.IssueTime); this._db.AddInParameter(cmd, "SendTime", DbType.DateTime, planInfo.SendTime); this._db.AddInParameter(cmd, "SendChannel", DbType.Int32, planInfo.SendChannel.Index); this._db.AddInParameter(cmd, "EncryptMobiles", DbType.String, encryptMobiles.ToString().Trim(',')); return(EyouSoft.Toolkit.DAL.DbHelper.ExecuteSql(cmd, this._db) == 1 ? true : false); }
/// <summary> /// 获得要发送的短信 /// </summary> /// <returns></returns> public Queue <EyouSoft.Model.SMSStructure.SendPlanInfo> Get() { Queue <EyouSoft.Model.SMSStructure.SendPlanInfo> queue = new Queue <EyouSoft.Model.SMSStructure.SendPlanInfo>(); DbCommand dc = this._dataBase.GetSqlStringCommand(SQL_SMS_SendPlan_SELECT); EyouSoft.Model.SMSStructure.SMSChannelList smschannel = new EyouSoft.Model.SMSStructure.SMSChannelList(); using (IDataReader rdr = DbHelper.ExecuteReader(dc, this._dataBase)) { while (rdr.Read()) { EyouSoft.Model.SMSStructure.SendPlanInfo item = new EyouSoft.Model.SMSStructure.SendPlanInfo(); item.PlanId = rdr.GetString(rdr.GetOrdinal("ID")); item.CompanyId = rdr.GetString(rdr.GetOrdinal("CompanyID")); item.CompanyName = rdr.IsDBNull(rdr.GetOrdinal("CompanyName")) ? "" : rdr.GetString(rdr.GetOrdinal("CompanyName")); item.UserId = rdr.GetString(rdr.GetOrdinal("UserID")); item.ContactName = rdr.IsDBNull(rdr.GetOrdinal("ContactName")) ? "" : rdr.GetString(rdr.GetOrdinal("ContactName")); item.SMSType = rdr.GetInt32(rdr.GetOrdinal("SMSType")); item.SMSContent = rdr.IsDBNull(rdr.GetOrdinal("SMSContent")) ? "" : rdr.GetString(rdr.GetOrdinal("SMSContent")); string mobile = rdr.IsDBNull(rdr.GetOrdinal("MobileList")) ? "" : rdr.GetString(rdr.GetOrdinal("MobileList")); string encryptMobiles = rdr.IsDBNull(rdr.GetOrdinal("EncryptMobiles")) ? "" : rdr.GetString(rdr.GetOrdinal("EncryptMobiles")); //显示时不加密手机号 item.Mobiles = this.GetAcceptMobiles(mobile, false); //显示时要加密手机号 item.Mobiles.AddRange(this.GetAcceptMobiles(encryptMobiles, true)); item.IssueTime = rdr.GetDateTime(rdr.GetOrdinal("IssueTime")); item.SendTime = rdr.GetDateTime(rdr.GetOrdinal("SendTime")); item.SendChannel = smschannel[rdr.GetInt32(rdr.GetOrdinal("SendChannel"))]; queue.Enqueue(item); } } return(queue); }
/// <summary> /// 发送短信 /// </summary> /// <param name="sms">待发送的短信实体</param> public void Send(EyouSoft.Model.SMSStructure.SendPlanInfo sms) { EyouSoft.BackgroundServices.smsSoap.SMSChannel smsChannel = new EyouSoft.BackgroundServices.smsSoap.SMSChannel(); smsChannel.ChannelName = sms.SendChannel.ChannelName; smsChannel.Index = sms.SendChannel.Index; smsChannel.IsLong = sms.SendChannel.IsLong; smsChannel.PriceOne = sms.SendChannel.PriceOne; smsChannel.Pw = sms.SendChannel.Pw; smsChannel.UserName = sms.SendChannel.UserName; EyouSoft.BackgroundServices.smsSoap.SendMessageInfo sendmsg = new EyouSoft.BackgroundServices.smsSoap.SendMessageInfo(); EyouSoft.BackgroundServices.smsSoap.AcceptMobileInfo[] MobilesArr = new EyouSoft.BackgroundServices.smsSoap.AcceptMobileInfo[sms.Mobiles.Count]; for (int i = 0; i < sms.Mobiles.Count; i++) { MobilesArr[i] = new EyouSoft.BackgroundServices.smsSoap.AcceptMobileInfo(); MobilesArr[i].Mobile = sms.Mobiles[i].Mobile; MobilesArr[i].IsEncrypt = sms.Mobiles[i].IsEncrypt; } sendmsg.Mobiles = MobilesArr; sendmsg.CompanyId = sms.CompanyId; sendmsg.CompanyName = sms.CompanyName; sendmsg.SendChannel = smsChannel; sendmsg.SendTime = sms.SendTime; sendmsg.SendType = EyouSoft.BackgroundServices.smsSoap.SendType.直接发送; sendmsg.SMSContent = sms.SMSContent; sendmsg.SMSType = sms.SMSType; sendmsg.UserFullName = sms.ContactName; sendmsg.UserId = sms.UserId; ////using (System.Transactions.TransactionScope tran = new System.Transactions.TransactionScope()) ////{ //定义api EyouSoft.BackgroundServices.smsSoap.SmsAPI api = new EyouSoft.BackgroundServices.smsSoap.SmsAPI(); //定义认证header EyouSoft.BackgroundServices.smsSoap.APISoapHeader header = new EyouSoft.BackgroundServices.smsSoap.APISoapHeader(); //header赋值,TMIS_APIKey为配置文件中配置的api调用密钥 header.SecretKey = System.Configuration.ConfigurationManager.AppSettings.Get("TMIS_APIKey"); api.APISoapHeaderValue = header; //发送短信 EyouSoft.BackgroundServices.smsSoap.SendResultInfo result = api.Send(sendmsg); sendmsg = null; char SendState = '2'; string error = ""; if (result.IsSucceed) //发送成功 { SendState = '1'; } else { error = result.ErrorMessage; } DbCommand dc = this._dataBase.GetSqlStringCommand(SQL_SMS_SendPlan_UPDATE); this._dataBase.AddInParameter(dc, "SendState", DbType.AnsiStringFixedLength, SendState); this._dataBase.AddInParameter(dc, "StateText", DbType.String, error); this._dataBase.AddInParameter(dc, "ID", DbType.AnsiStringFixedLength, sms.PlanId); //执行 DbHelper.ExecuteSql(dc, this._dataBase); // result = null; // //tran.Complete(); ////} }
/// <summary> /// 发送短信 /// </summary> /// <param name="sendMessageInfo">发送短信提交的业务实体</param> /// <returns></returns> public EyouSoft.Model.SMSStructure.SendResultInfo Send(EyouSoft.Model.SMSStructure.SendMessageInfo sendMessageInfo) { #region 发送短信验证 //发送短信验证 EyouSoft.Model.SMSStructure.SendResultInfo validateResultInfo = this.ValidateSend(sendMessageInfo); if (!validateResultInfo.IsSucceed) { return(validateResultInfo); } #endregion #region 定时发送任务处理 //定时发送任务处理 if (sendMessageInfo.SendType == EyouSoft.Model.SMSStructure.SendType.定时发送) { EyouSoft.Model.SMSStructure.SendPlanInfo plan = new EyouSoft.Model.SMSStructure.SendPlanInfo(); plan.CompanyId = sendMessageInfo.CompanyId; plan.CompanyName = sendMessageInfo.CompanyName; plan.ContactName = sendMessageInfo.UserFullName; plan.IssueTime = DateTime.Now; plan.Mobiles = sendMessageInfo.Mobiles; plan.PlanId = Guid.NewGuid().ToString(); plan.SendChannel = sendMessageInfo.SendChannel; plan.SendTime = sendMessageInfo.SendTime; plan.SMSContent = sendMessageInfo.SMSContent; plan.SMSType = sendMessageInfo.SMSType; plan.UserId = sendMessageInfo.UserId; if (Dal.InsertSendPlan(plan)) { validateResultInfo.IsSucceed = true; } else { validateResultInfo.IsSucceed = false; validateResultInfo.ErrorMessage = "写入定时发送计划任务时发生了错误。"; } return(validateResultInfo); } #endregion #region 发送短信处理 validateResultInfo.TempFeeTakeId = Guid.NewGuid().ToString(); validateResultInfo.SendTotalId = Guid.NewGuid().ToString(); validateResultInfo.SuccessCount = validateResultInfo.ErrorCount = validateResultInfo.TimeoutCount = 0; #region 扣除账户金额 //扣除账户金额 bool deductAccountMoneyResult = DalAccount.DeductAccountMoney(sendMessageInfo.CompanyId, sendMessageInfo.UserId.ToString(), validateResultInfo.CountFee, 0, validateResultInfo.TempFeeTakeId, validateResultInfo.SendTotalId); if (!deductAccountMoneyResult) { validateResultInfo.IsSucceed = false; validateResultInfo.ErrorMessage = "扣除账户金额时产生了错误,请重试"; return(validateResultInfo); } #endregion IList <EyouSoft.Model.SMSStructure.SendDetail> sendDetails = new List <EyouSoft.Model.SMSStructure.SendDetail>(); //发送[移动、联通]内容实际计算费用短信条数 int mobielFactCount = this.GetSmsTotalCount(sendMessageInfo.SMSContentSendComplete, EyouSoft.Model.SMSStructure.SMSNoType.Mobiel, sendMessageInfo.SendChannel); //发送[小灵通]内容实际计算费用短信条数 int phsFactCount = this.GetSmsTotalCount(sendMessageInfo.SMSContentSendComplete, EyouSoft.Model.SMSStructure.SMSNoType.PHS, sendMessageInfo.SendChannel); #region 调用发送短信接口 //每次调用发送接口时待发送的手机号码 StringBuilder waitMobiles = new StringBuilder(); //每次调用发送接口时的最大发送号码个数 int waitCanMobilesMax = 100; //总的要发送的手机号码个数 int waitMobileLength = sendMessageInfo.Mobiles.Count; int indexStart = 0; for (int indexC = 0; indexC < waitMobileLength; indexC++) { EyouSoft.Model.SMSStructure.SendDetail sendDetailInfo = new EyouSoft.Model.SMSStructure.SendDetail(); EyouSoft.Model.SMSStructure.AcceptMobileInfo mobile = sendMessageInfo.Mobiles[indexC]; sendDetailInfo.ID = Guid.NewGuid().ToString(); sendDetailInfo.MobileNumber = mobile.Mobile; sendDetailInfo.IsPHS = this.IsPHS(mobile.Mobile); sendDetailInfo.IsEncrypt = mobile.IsEncrypt; if (!sendDetailInfo.IsPHS) { sendDetailInfo.FactCount = mobielFactCount; } else { sendDetailInfo.FactCount = phsFactCount; } //添加到集合中 sendDetails.Add(sendDetailInfo); waitMobiles.AppendFormat("{0},", mobile.Mobile); #region 判断是否开始发送短信 if ((indexC + 1) % waitCanMobilesMax == 0 || (indexC + 1) == waitMobileLength) { int sendResult = 0; string sendMsg = ""; try { string sendMessageContent = sendMessageInfo.SMSContentSendComplete; sms.Timeout = 1000; //发送结果返回值 返回int类型的0时成功 返回对应的负数时失败 sendResult = sms.SendSms(this.EnterpriseId, waitMobiles.ToString().TrimEnd(",".ToCharArray()), sendMessageInfo.SMSContentSendComplete, sendMessageInfo.SendChannel.UserName, sendMessageInfo.SendChannel.Pw); sendMsg = this.GetServicesState(sendResult); } catch { sendMsg = "超时"; sendResult = this.SendTimeOutEventCode; } //要发送的号码清空 waitMobiles.Remove(0, waitMobiles.Length); #region 处理发送后的结果(处理当次在批量发送内的所有号码) for (int j = indexStart; j <= indexC; j++) { sendDetails[j].ReturnMsg = sendMsg; sendDetails[j].ReturnResult = sendResult; if (!sendDetails[j].IsPHS) { if (sendResult == this.SendTimeOutEventCode) { validateResultInfo.TimeoutCount = validateResultInfo.TimeoutCount + 1; } else if (sendResult == 0) { validateResultInfo.SuccessCount = validateResultInfo.SuccessCount + 1; } else { validateResultInfo.ErrorCount = validateResultInfo.ErrorCount + 1; } } else { if (sendResult == this.SendTimeOutEventCode) { validateResultInfo.PHSTimeoutCount = validateResultInfo.PHSTimeoutCount + 1; } else if (sendResult == 0) { validateResultInfo.PHSSuccessCount = validateResultInfo.PHSSuccessCount + 1; } else { validateResultInfo.PHSErrorCount = validateResultInfo.PHSErrorCount + 1; } } } #endregion 处理发送后的结果 //记录下一批的开始索引号 indexStart = indexC + 1; } #endregion 判断是否开始发送短信 } #endregion //写入短信发送明细及统计信息,同时更新账户余额 Dal.InsertSendInfo(sendMessageInfo, sendDetails, validateResultInfo); //计算发送短信后实际扣除的消费金额(预扣除金额-发送超时的手机或者小灵通短信条数*1个短信的实际条数*单价) validateResultInfo.SendFee = validateResultInfo.CountFee - 0.01M * validateResultInfo.TimeoutCount * validateResultInfo.FactCount * sendMessageInfo.SendChannel.PriceOne - 0.01M * validateResultInfo.PHSTimeoutCount * validateResultInfo.PHSFactCount * sendMessageInfo.SendChannel.PriceOne; #endregion return(validateResultInfo); }