heyuntao
2023-10-23 45e3e2f4b3a55c3949c8a5bc5e6928f0cc2cacb1
api/SmsSendTool/SendMain.cs
@@ -1,4 +1,5 @@
using CommonUtil;
using CommonUtil.Web;
using Operater.DAL;
using Operater.DbModel;
using SqlSugar;
@@ -20,7 +21,7 @@
                if (smssend == null)
                {
                    Console.WriteLine($"任务ID:{id},任务不存在");
                    UpdateSmsSendStatus(6, "任务不存在", id);
                   // UpdateSmsSendStatus(6, "任务不存在", id);
                    return true;
                }
                //判断任务状态是否是审核通过
@@ -33,7 +34,7 @@
                if (smssend.Isshowsend == 1)
                {
                    Console.WriteLine($"任务ID:{id},任务已被删除");
                    UpdateSmsSendStatus(6, "任务已被删除", id);
                  //  UpdateSmsSendStatus(6, "任务已被删除", id);
                    return true;
                }
                //判断定时时间是否大于当前时间
@@ -44,7 +45,7 @@
                }
                //修改发送状态为发送中
                UpdateSmsSendStatus(4, "", id);
                UpdateSmsSendStatus(4, "", id, DateTime.Now);
                //发送短信
                Send(smssend);
@@ -79,13 +80,15 @@
            if (!MobileUtils.IsPhone(smsSend.Mobile))
            {
                Console.WriteLine($"任务ID:{smsSend.Id},手机号不正确");
                UpdateSmsSendStatus(7, "手机号不正确", 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);
                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);//短信计费
@@ -98,7 +101,8 @@
            if (memberinfo == null)
            {
                LogUtil.Error($"用户不存在,id:{smsSend.Id}");
                UpdateSmsSendStatus(7, "用户不存在", smsSend.Id);
                UpdateSmsSendStatus(7, "用户不存在", smsSend.Id, DateTime.Now);
                SendQwGroup.SendMessage($"用户不存在:{smsSend.Id}");
                return;
            }
            else
@@ -106,9 +110,18 @@
                if(memberinfo.Smscount< thisSmsCount)
                {
                    LogUtil.Error($"余额不足,id:{smsSend.Id}");
                    UpdateSmsSendStatus(7, "余额不足", 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\":\"\"}";
@@ -123,7 +136,7 @@
                if (scount == 0)
                {
                    LogUtil.Error($",id:{smsSend.Id}");
                    UpdateSmsSendStatus(7, "扣费失败", smsSend.Id);
                    UpdateSmsSendStatus(7, "扣费失败", smsSend.Id, DateTime.Now);
                    return;
                }
@@ -131,7 +144,7 @@
                //插入发送成功记录
                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<SmsSendhistory>().And(m => m.Mainid == smsSend.Id).ToExpression();
                var record_where_expression = Expressionable.Create<SmsSendhistory>().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)
                {
@@ -175,6 +188,18 @@
            else
            {
                Console.WriteLine($"手机号:{smsSend.Mobile},发送失败");
                //查看是否存在发送记录
                var record_where_expression = Expressionable.Create<SmsSendhistory>().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(),
@@ -191,16 +216,17 @@
                    IsRead = 0,
                    ReaLsuccedsmscount = realSmsCount
                };
                bool issuccess = new SmsSendDAL().Insert(smsSend);
                bool issuccess = new SmsSendhistoryDAL().Insert(sendhistory);
                //修改短信任务状态
                UpdateSmsSendStatus(7, "发送失败", smsSend.Id);
                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)
        public bool UpdateSmsSendStatus(int status,string memo,string id,DateTime sendtime)
        {
            SmsSend updateSmssend = new SmsSend
            {