using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace LYFZ.Software.MainBusiness.SMSManagement.SmsSend
{
public partial class FrmSendSms : LYFZ.Software.UI.SMSManagement.SmsSend.FrmSendSms
{
private string _Phone;
///
/// 手机号
///
public string Phone
{
get { return _Phone; }
set { _Phone = value.Replace('/', ','); }
}
int SumCount = 0; //短信条数
private static int _Works64 = 67; //67字数分割点
///
/// 67字数分割点
///
public static int Works64
{
get { return FrmSendSms._Works64; }
set { FrmSendSms._Works64 = value; }
}
// int nTxetMsg = 70; //最后分割点
public FrmSendSms()
{
this.Load += new EventHandler(FrmSendSms_Load);
radN_M.Click += new EventHandler(radN_M_Click);
this.radN.Click += new EventHandler(radN_Click);
this.radM.Click += new EventHandler(radM_Click);
this.btnContentSelection.Click += new EventHandler(btnContentSelection_Click);
this.btnSendSMS.Click += new EventHandler(btnSendSMS_Click);
this.richNr.TextChanged += new EventHandler(richNr_TextChanged);
this.richTelPhone.MouseDoubleClick += richTelPhone_MouseDoubleClick;
this.richTelPhone.TextChanged += richTelPhone_TextChanged;
}
void richTelPhone_TextChanged(object sender, EventArgs e)
{
richNr_TextChanged(sender, e);
}
void richTelPhone_MouseDoubleClick(object sender, MouseEventArgs e)
{
this.richTelPhone.ReadOnly = !this.richTelPhone.ReadOnly;
}
public void SetDefaultSmsText(string text)
{
this.richNr.Text = text.Trim();
}
#region 加载
///
///
///
/// 短信内容总字数
/// 短信条数
///
public static string GetlblNameTextString(int i, int j)
{
string Smsa_AccountName = "";
if (frmSMSTemplateSet.CurrentSmsAccount != null) {
Smsa_AccountName = frmSMSTemplateSet.CurrentSmsAccount.Smsa_AccountName;
i = i + frmSMSTemplateSet.CurrentSmsAccount.Smsa_AccountName.Length;
}
return "短信内容(手机67字/条)(现" + i.ToString() + "字 加上签名:" + Smsa_AccountName + ",共" + j.ToString() + "条短信)";
}
protected void FrmSendSms_Load(object sender, EventArgs e)
{
try
{
lblName.Text = GetlblNameTextString(0, 0);
richTelPhone.Text = Phone.TrimEnd(',');
#region 检查手机合法性
string phone = richTelPhone.Text;
//筛选重复的数据
string[] sj = phone.Split(',');
List strReturn = new List();
foreach (string name in sj)
{
if (strReturn.Contains(name) == false) strReturn.Add(name);
}
string[] Sx = strReturn.ToArray();
//文本框获取新值
string Lj = "";
for (int i = 0; i < Sx.Length; i++)
{
Lj += Sx[i] + ",";
}
richTelPhone.Text = Lj.TrimEnd(',');
#endregion
int count = richTelPhone.Text.Length - richTelPhone.Text.Replace(",", "").Length;
txtPhoneCount.Text = Convert.ToInt32(count + 1).ToString();
}
catch (Exception ex) { MessageBoxCustom.Show(ex.Message); }
}
#endregion
#region 男+女
protected void radN_M_Click(object sender, EventArgs e)
{
txtPhoneCount.Text = "";
richTelPhone.Text = "";
richTelPhone.Text = Phone.TrimEnd(',');
int count = richTelPhone.Text.Length - richTelPhone.Text.Replace(",", "").Length;
txtPhoneCount.Text = Convert.ToInt32(count + 1).ToString();
}
#endregion
#region 男
protected void radN_Click(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(Phone))
{
txtPhoneCount.Text = "";
richTelPhone.Text = "";
LYFZ.BLL.BLL_ErpCustomer bll = new BLL.BLL_ErpCustomer();
string[] sz = Phone.TrimEnd(',').Split(',');
for (int i = 0; i < sz.Length; i++)
{
DataTable dt = bll.View_tb_ErpCustomer("Cus_Telephone='" + sz[i] + "'").Tables[0];
if (dt.Rows[0]["sex"].ToString() == "男")
{
richTelPhone.Text = sz[i];
int count = richTelPhone.Text.Length - richTelPhone.Text.Replace(",", "").Length;
txtPhoneCount.Text = Convert.ToInt32(count + 1).ToString();
}
}
}
}
#endregion
#region 女
protected void radM_Click(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(Phone))
{
txtPhoneCount.Text = "";
richTelPhone.Text = "";
LYFZ.BLL.BLL_ErpCustomer bll = new BLL.BLL_ErpCustomer();
string[] sz = Phone.TrimEnd(',').Split(',');
for (int i = 0; i < sz.Length; i++)
{
DataTable dt = bll.View_tb_ErpCustomer("Cus_Telephone='" + sz[i] + "'").Tables[0];
if (dt.Rows[0]["sex"].ToString() == "女")
{
richTelPhone.Text = sz[i];
int count = richTelPhone.Text.Length - richTelPhone.Text.Replace(",", "").Length;
txtPhoneCount.Text = Convert.ToInt32(count + 1).ToString();
}
}
}
}
#endregion
#region 选择
protected void btnContentSelection_Click(object sender, EventArgs e)
{
frmSelectSmsInfo selectInfo = new frmSelectSmsInfo();
if (selectInfo.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
if (selectInfo.SmsModel.ID > 0)
{
this.richNr.Text = selectInfo.SmsModel.ST_SMSContent;
SumCount = GetSmsSizeCount(selectInfo.SmsModel.ST_SMSContent, Works64);
lblName.Text = GetlblNameTextString(selectInfo.SmsModel.ST_SMSContent.Length, SumCount * richTelPhone.Text.Trim().Split(',').Length);
}
}
//LYFZ.Software.MainBusiness.SMSManagement.ShortSmsMessage.FrmSMSContent set = new ShortSmsMessage.FrmSMSContent();
//set.ShowDialog();
//string Nr = set.NR;
//if (!string.IsNullOrEmpty(Nr))
//{
// richNr.Text = Nr;
// #region 获取67字数
// SumCount = GetSmsSizeCount(Nr.ToString(), Works64);
// lblName.Text = GetlblNameTextString(Nr.Length, SumCount);// "短信内容(手机67字/条)(现" + Nr.Length + "字,共" + SumCount + "条短信)";
// #endregion
//}
}
///
/// 计算短信条数
///
/// 短信内容
/// 每条短信字数
///
public static int GetSmsSizeCount(string NR, int MSG_LENGTH)
{
int nNumberOfTextMsg = 0;
int nWCharLen = NR.Length;
if (frmSMSTemplateSet.CurrentSmsAccount != null)
{
nWCharLen = NR.Length + frmSMSTemplateSet.CurrentSmsAccount.Smsa_AccountName.Length;
}
if (MSG_LENGTH != 0)
{
// 魔数MSG_LENGTH = 67:
// 当短信字符数超过70时,短信服务商会分为两部分;
// 1.将前67个字符为一条短信;
// 2.将67后的为另一部分,若仍大于70,再次执行1;
// 3.若短信小于等于70,则为一条短信;
nNumberOfTextMsg = nWCharLen / MSG_LENGTH;
if (nWCharLen % MSG_LENGTH > 0)
nNumberOfTextMsg++;
}
return nNumberOfTextMsg;
}
#endregion
#region 内容输入事件
protected void richNr_TextChanged(object sender, EventArgs e)
{
#region 获取64字数
string Nr = richNr.Text;
lblName.Text = GetlblNameTextString(Nr.Length, GetSmsSizeCount(Nr.ToString(), Works64) * richTelPhone.Text.Trim().Trim(',').Split(',').Length);//"短信内容(手机67字/条)(现" + Nr.Length + "字,共" + GetSmsSizeCount(Nr.ToString(), Works64) + "条短信)";
this.txtPhoneCount.Text = richTelPhone.Text.Trim().Trim(',').Split(',').Length.ToString();
#endregion
}
#endregion
#region 发送
protected void btnSendSMS_Click(object sender, EventArgs e)
{
AddSendSMS(richTelPhone.Text, richNr.Text);
}
///
/// 发送短信
///
///
///
public static void AddSendSMS(string Phones, string smsContent)
{
try
{
#region 判断不能为空
if (string.IsNullOrEmpty(Phones))
{
MessageBoxCustom.Show("手机号码不能为空!");
return;
}
if (string.IsNullOrEmpty(smsContent))
{
MessageBoxCustom.Show("短信内容不能为空!");
return;
}
#endregion
#region 检查手机合法性
string phone = Phones.Trim().Trim(',').Replace(",,", ",");
string ret = LYFZ.WinAPI.CustomPublicMethod.ValidationPhones(phone);
if (ret.Trim().Length > 0)
{
MessageBoxCustom.Show("手机号无效:" + ret);
return;
}
//筛选重复的数据
string[] sj = phone.Split(',');
StringBuilder phoneString = new StringBuilder();
foreach (string name in sj)
{
if (!phoneString.ToString().Contains(name))
phoneString.Append(name + ",");
}
phone = phoneString.ToString().Trim(',');
#endregion
#region 发送
LYFZ.BLL.BLL_SMSTemplates smsTemplates = new BLL.BLL_SMSTemplates();
if (phone.Trim().Length > 0)
{
if (phone.Trim().Length >= 60 && (!smsContent.Contains("回TD退订") && !smsContent.Contains("退订回TD") && !smsContent.Contains("退订回N") && !smsContent.Contains("回N退订")))
{
string tempSms = smsContent + "回TD退订";
int nWCharLen =0;//签名长度
int tempcount = tempSms.Length;
int tempSumCount = 0;
if (frmSMSTemplateSet.CurrentSmsAccount != null)
{
nWCharLen =frmSMSTemplateSet.CurrentSmsAccount.Smsa_AccountName.Length;
if (!smsContent.StartsWith(frmSMSTemplateSet.CurrentSmsAccount.Smsa_AccountName) && !smsContent.EndsWith(frmSMSTemplateSet.CurrentSmsAccount.Smsa_AccountName))
{
tempcount = tempcount + nWCharLen;
tempSms = tempSms + frmSMSTemplateSet.CurrentSmsAccount.Smsa_AccountName;
}
}
tempSumCount = tempcount / Works64;
if (tempcount % Works64 > 0)
tempSumCount++;
if (MessageBoxCustom.Show("系统检查到您发送的短信为“群发(营销类)短信”,且未加入“回TD退订”字样,系统将自动加上该字样,此时实际短信字数为 " + tempcount + " 个字, " + (tempSumCount* phone.Split(',').Length).ToString() + " 条,是否确定发送此短信?", msgBoxButton: MessageBoxButtons.YesNo) == DialogResult.Yes)
{
smsContent = smsContent + "回TD退订";
}
else {
return;
}
}
string retMsg = smsTemplates.GenerateManuallySendSMS(phone, smsContent.Trim(), SDateTime.Now, LYFZ.Software.MainBusiness.CommonLogical.SuccessfulLogin.LoginUserModel.User_Name
, LYFZ.Software.MainBusiness.CommonLogical.SuccessfulLogin.GetCompanyInfoID());
MessageBoxCustom.Show(retMsg);
}
else
{
MessageBoxCustom.Show("没有找到有效的手机号");
}
#endregion
}
catch (Exception ex) { MessageBoxCustom.Show(ex.Message); }
}
#region 验证手机
private bool CheckPhoneNumber(string number)
{
return System.Text.RegularExpressions.Regex.IsMatch(number, @"^(1[3,5,8][0-9])\d{8}$");
}
#endregion
#endregion
}
}