using CommonUtil;
using CommonUtil.Web;
using Operater.DAL;
using Operater.DbModel;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Text;
namespace SmsSendTool
{
public class SendMain
{
public bool SendSms(string id)
{
try
{
//根据ID获取任务
var smssend = new SmsSendDAL().GetSingle(s => s.Id == id);
if (smssend == null)
{
Console.WriteLine($"任务ID:{id},任务不存在");
// UpdateSmsSendStatus(6, "任务不存在", id);
return true;
}
//判断任务状态是否是审核通过
if (smssend.Sendstatus != 1)
{
Console.WriteLine($"任务ID:{id},任务不处于审核成功状态");
return true;
}
//判断任务是否被删除
if (smssend.Isshowsend == 1)
{
Console.WriteLine($"任务ID:{id},任务已被删除");
// UpdateSmsSendStatus(6, "任务已被删除", id);
return true;
}
//判断定时时间是否大于当前时间
if (smssend.Wantsendtime > DateTime.Now)
{
Console.WriteLine($"任务ID:{id},任务未到发送时间");
return true;
}
//修改发送状态为发送中
UpdateSmsSendStatus(4, "", id, DateTime.Now);
//发送短信
Send(smssend);
}
catch (Exception ex)
{
LogUtil.Error(ex);
return false;
}
return true;
}
///
/// 短信发送
///
///
private void Send(SmsSend smsSend)
{
//获取短信设置
var smsset = new SmsSetDAL().GetFirst("");
//短信内容拼接
string smscontent = string.Empty;
if (smsset.Issuffix == 0)
{
smscontent= "【" + smsset.Smsname + "】" + smsSend.Smscontent;
}
else
{
smscontent = "【" + smsset.Smsname + "】" + smsSend.Smscontent +" "+ smsset.Suffix;
}
if (!MobileUtils.IsPhone(smsSend.Mobile))
{
Console.WriteLine($"任务ID:{smsSend.Id},手机号不正确");
UpdateSmsSendStatus(7, "手机号不正确", smsSend.Id, DateTime.Now);
SendQwGroup.SendMessage($"手机号不正确:{smsSend.Id}");
return;
}
if (smscontent.Contains("?????"))
{
LogUtil.Error($"发送异常,id:{smsSend.Id}");
UpdateSmsSendStatus(7, "短信异常", smsSend.Id, DateTime.Now);
SendQwGroup.SendMessage($"文案异常:{smsSend.Id}");
return;
}
int thisSmsCount= smscontent.Length <= 70 ? 1 : smscontent.Length % 70 == 0 ? (smscontent.Length / 70) : (smscontent.Length / 70 + 1);//短信计费
int realSmsCount= smscontent.Length <= 70 ? 1 : smscontent.Length % 67 == 0 ? (smscontent.Length / 67) : (smscontent.Length / 67 + 1);//短信真实计费
//判断短信余额是否满足
var member_where_expression = Expressionable.Create().And(m => m.Id == smsSend.Memberid).And(m => m.Isblack == 0).ToExpression();
var memberinfo = new SmsMemberDAL().GetSingle(member_where_expression);
if (memberinfo == null)
{
LogUtil.Error($"用户不存在,id:{smsSend.Id}");
UpdateSmsSendStatus(7, "用户不存在", smsSend.Id, DateTime.Now);
SendQwGroup.SendMessage($"用户不存在:{smsSend.Id}");
return;
}
else
{
if(memberinfo.Smscount< thisSmsCount)
{
LogUtil.Error($"余额不足,id:{smsSend.Id}");
UpdateSmsSendStatus(7, "余额不足", smsSend.Id, DateTime.Now);
SendQwGroup.SendMessage($"余额不足:{smsSend.Id}");
return;
}
}
//检查短信状态
//根据ID获取任务
var smssend = new SmsSendDAL().GetSingle(s => s.Id == smsSend.Id);
if (smssend.Sendstatus != 4)
{
Console.WriteLine($"手机号:{smsSend.Mobile},状态已变更,不执行");
return;
}
string jsonsend= "{mobile:\"" + smsSend.Mobile + "\",content:\"" + smscontent.Replace("\"", "“").Replace("'", "’") + "\",appendid:\"\",\"uid\":\"\"}";
SmsSendApi smsSendApi = new SmsSendApi(jsonsend, smsset.Username, smsset.Pwd);
string strResult = string.Empty;
smsSendApi.SendKTApi(ref strResult);
if (strResult.Contains("1,"))
{
Console.WriteLine($"手机号:{smsSend.Mobile},发送成功");
//扣除短信
int scount= new SmsMemberDAL().ExecuteCommand($"update sms_member set smscount=smscount-{thisSmsCount} where id='{smsSend.Memberid}'");
if (scount == 0)
{
LogUtil.Error($",id:{smsSend.Id}");
UpdateSmsSendStatus(7, "扣费失败", smsSend.Id, DateTime.Now);
return;
}
string sendid = strResult.Split(',')[1];
//插入发送成功记录
string inserthistorysql = $"INSERT INTO `sms_sender`.`sms_sendhistory`(`id`, `mobile`, `succedsmscount`, `sendstatus`, `sendtime`, `smscontent`, `memo`, `memberid`, `isshowsend`, `mainid`, `sendid`, `isread`) VALUES ('{Guid.NewGuid().ToString()}', '{smsSend.Mobile}', {thisSmsCount}, 0, '{DateTime.Now}', '{smscontent}', '1', '{smsSend.Memberid}', 0, '{smsSend.Id}', '{sendid}', 0);";
//查看是否存在发送记录
var record_where_expression = Expressionable.Create().And(m => m.Mainid == smsSend.Id).And(m=>m.Memo=="1").ToExpression();
var exist_record = new SmsSendhistoryDAL().ListGet(record_where_expression);
if (exist_record != null&&exist_record.Count>0)
{
//删除记录
foreach (var record in exist_record)
{
new SmsSendhistoryDAL().Delete("", record.Id);
}
}
SmsSendhistory sendhistory = new SmsSendhistory
{
Id=Guid.NewGuid().ToString(),
Mobile=smsSend.Mobile,
Succedsmscount=thisSmsCount,
Sendstatus=0,
Sendtime=DateTime.Now,
Smscontent=smscontent,
Memo="0",
Memberid=smsSend.Memberid,
Isshowsend=0,
Mainid=smsSend.Id,
Sendid=sendid,
IsRead=0,
ReaLsuccedsmscount=realSmsCount
};
bool issuccess = new SmsSendhistoryDAL().Insert(sendhistory);
if (issuccess)
{
//修改短信任务状态
// UpdateSmsSendStatus(7, "", smsSend.Id);
SmsSend updateSmssend = new SmsSend
{
Sendstatus = 6,
Succedcount=smsSend.Sendcount,
Succedsmscount= thisSmsCount
};
int successcount = new SmsSendDAL().Update(updateSmssend, m => new { m.Sendstatus, m.Succedcount,m.Succedsmscount }, s => s.Id == smsSend.Id);
}
}
else
{
Console.WriteLine($"手机号:{smsSend.Mobile},发送失败");
//查看是否存在发送记录
var record_where_expression = Expressionable.Create().And(m => m.Mainid == smsSend.Id).And(m => m.Memo == "1").ToExpression();
var exist_record = new SmsSendhistoryDAL().ListGet(record_where_expression);
if (exist_record != null && exist_record.Count > 0)
{
//删除记录
foreach (var record in exist_record)
{
new SmsSendhistoryDAL().Delete("", record.Id);
}
}
SmsSendhistory sendhistory = new SmsSendhistory
{
Id = Guid.NewGuid().ToString(),
Mobile = smsSend.Mobile,
Succedsmscount = thisSmsCount,
Sendstatus = 1,
Sendtime = DateTime.Now,
Smscontent = smscontent,
Memo = "0",
Memberid = smsSend.Memberid,
Isshowsend = 0,
Mainid = smsSend.Id,
Sendid = "",
IsRead = 0,
ReaLsuccedsmscount = realSmsCount
};
bool issuccess = new SmsSendhistoryDAL().Insert(sendhistory);
//修改短信任务状态
UpdateSmsSendStatus(7, "发送失败", smsSend.Id,DateTime.Now);
SendQwGroup.SendMessage($"短信异常:{smsSend.Id}"+ "strResult:"+ strResult);
LogUtil.Error($"短信异常:{smsSend.Id}", "公共号短信异常");
}
}
public bool UpdateSmsSendStatus(int status,string memo,string id,DateTime sendtime)
{
SmsSend updateSmssend = new SmsSend
{
Sendstatus = status,
Memo = memo
};
int successcount = new SmsSendDAL().Update(updateSmssend, m => new { m.Sendstatus,m.Memo }, s => s.Id == id);
return successcount > 0 ? true : false;
}
}
}