zhaojs
2023-09-27 74098f1401afe40f961d1d167bb18dd0a71c4d59
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
using CommonUtil;
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);
 
                //发送短信
                Send(smssend);
            }
            catch (Exception ex)
            {
                LogUtil.Error(ex);
                return false;
 
            }
            return true;
        }
 
        /// <summary>
        /// 短信发送
        /// </summary>
        /// <param name="smsSend"></param>
        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);
                return;
            }
            if (smscontent.Contains("?????"))
            {
                LogUtil.Error($"发送异常,id:{smsSend.Id}");
                UpdateSmsSendStatus(7, "短信异常", 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<SmsMember>().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);
                return;
            }
            else
            {
                if(memberinfo.Smscount< thisSmsCount)
                {
                    LogUtil.Error($"余额不足,id:{smsSend.Id}");
                    UpdateSmsSendStatus(7, "余额不足", smsSend.Id);
                    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);
                    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<SmsSendhistory>().And(m => m.Mainid == smsSend.Id).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},发送失败");
                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 SmsSendDAL().Insert(smsSend);
                //修改短信任务状态
                UpdateSmsSendStatus(7, "发送失败", smsSend.Id);
                LogUtil.Error($"短信异常:{smsSend.Id}", "公共号短信异常");
            }
           
        }
 
 
        public bool UpdateSmsSendStatus(int status,string memo,string id)
        {
            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;
        }
    }
}