Ver código fonte

Merge branch 'master' of http://110.188.24.182:10023/Fengwo/hotline

xf 1 ano atrás
pai
commit
0c3d6511e3

+ 4 - 0
src/Hotline.Api/Controllers/CommonPController.cs

@@ -95,6 +95,8 @@ namespace Hotline.Api.Controllers
             var model = await _systemAreaDomainService.GetArea(dto.Id,HttpContext.RequestAborted);
             if (model is null)
                 throw UserFriendlyException.SameMessage("未知数据,请刷新页面");
+            if (!model.IsCanModify)
+                throw UserFriendlyException.SameMessage("系统数据,不能修改");
 
             model = _mapper.Map<SystemArea>(dto);
             await _systemAreaDomainService.ModifyArea(model, HttpContext.RequestAborted);
@@ -112,6 +114,8 @@ namespace Hotline.Api.Controllers
             var model = await _systemAreaDomainService.GetArea(id, HttpContext.RequestAborted);
             if (model is null)
                 throw UserFriendlyException.SameMessage("未知数据,请刷新页面");
+            if (!model.IsCanModify)
+                throw UserFriendlyException.SameMessage("系统数据不能删除");
 
             await _systemAreaDomainService.DelArea(id, HttpContext.RequestAborted);
         }

+ 18 - 55
src/Hotline.Api/Controllers/PushMessageController.cs

@@ -1,22 +1,18 @@
-using Hotline.KnowledgeBase.Notifies;
-using Hotline.KnowledgeBase;
-using Hotline.Push;
+using Hotline.Push.FWMessage;
 using Hotline.Repository.SqlSugar.Extensions;
 using Hotline.Share.Dtos;
 using Hotline.Share.Dtos.Push;
+using Hotline.Share.Dtos.Push.FWMessage;
 using MapsterMapper;
 using Microsoft.AspNetCore.Authorization;
 using Microsoft.AspNetCore.Mvc;
-using MediatR;
-using Hotline.Push.Notifies;
-using Hotline.Share.Enums.Push;
-using NetTaste;
 using XF.Domain.Filters;
 
 namespace Hotline.Api.Controllers
 {
     public class PushMessageController : BaseController
     {
+        #region 注入
         private readonly IMessageRepository _messageRepository;
         private readonly IMapper _mapper;
         private readonly IPushDomainService _pushDomainService;
@@ -27,14 +23,15 @@ namespace Hotline.Api.Controllers
         /// <param name="messageRepository"></param>
         /// <param name="mapper"></param>
         /// <param name="pushDomainService"></param>
-        /// <param name="mediator"></param>
         public PushMessageController(IMessageRepository messageRepository, IMapper mapper, IPushDomainService pushDomainService)
         {
             _messageRepository = messageRepository;
             _mapper = mapper;
             _pushDomainService = pushDomainService;
-        }
+        } 
+        #endregion
 
+        #region 新增
         /// <summary>
         /// 新增
         /// </summary>
@@ -45,7 +42,9 @@ namespace Hotline.Api.Controllers
         {
             await _pushDomainService.PushAsync(dto, HttpContext.RequestAborted);
         }
+        #endregion
 
+        #region 查询短信剩余数量
         /// <summary>
         /// 查询短信剩余数量
         /// </summary>
@@ -55,41 +54,18 @@ namespace Hotline.Api.Controllers
         {
             return await _pushDomainService.GetAccountNum();
         }
+        #endregion
 
         #region 短信发送状态回调接口
         /// <summary>
         /// 短信发送状态回调接口
         /// </summary>
-        /// <param name="account">短信回传账号</param>
-        /// <param name="pswd">短信回传密码</param>
-        /// <param name="msgid">短信中心短信待发送ID,同短信发送接口返回值ID</param>
-        /// <param name="sendtime">短信发送时间</param>
-        /// <param name="msgcount">发送短信使用的数量</param>
-        /// <param name="state">发送状态:0:未发送  1:发送中  2:发送失败  3:发送成功</param>
-        /// <param name="errormsg">错误消息</param>
-        /// <param name="sfid">短信中心短信已发送ID</param>
-        /// <param name="telnumall">手机号码</param>
-        /// <param name="sign">短信签名</param>
+        /// <param name="receiveMessageDto"></param>
         /// <returns></returns>
         [HttpPost("receiveobtain")]
         [AllowAnonymous]
-        public async Task<OpenResponse> ReceiveObtain(string account, string pswd, string msgid, string sendtime, int msgcount, int state,
-            string errormsg, int sfid, string telnumall, string sign)
+        public async Task<OpenResponse> ReceiveObtain([FromForm] ReceiveMessageDto receiveMessageDto)
         {
-            ReceiveMessageDto receiveMessageDto = new()
-            {
-                account = account,
-                pswd = pswd,
-                msgid = msgid,
-                sendtime = sendtime,
-                msgcount = msgcount,
-                state = state,
-                errormsg = errormsg,
-                sfid = sfid,
-                telnumall = telnumall,
-                sign = sign
-            };
-
             var result = await _pushDomainService.ReceiveObtain(receiveMessageDto);
 
             return OpenResponse.Ok(result);
@@ -100,35 +76,19 @@ namespace Hotline.Api.Controllers
         /// <summary>
         /// 短信接收
         /// </summary>
-        /// <param name="account">短信回传账号</param>
-        /// <param name="pswd">短信回传密码</param>
-        /// <param name="msg">回复短信内容</param>
-        /// <param name="mobile">回复手机号码</param>
-        /// <param name="destcode">短信接收平台号码</param>
-        /// <param name="motime">回复时间</param>
-        /// <param name="fsfid">短信平台回复短信ID</param>
+        /// <param name="receiveMessageDto"></param>
         /// <returns></returns>
         [HttpPost("receivesms")]
         [AllowAnonymous]
-        public async Task<ActionResult<string>> ReceiveSms(string account, string pswd, string msg, string mobile, string destcode, string motime, string fsfid)
+        public async Task<OpenResponse> ReceiveSms([FromForm] ReceiveMessageDto receiveMessageDto)
         {
-            //组装参数
-            ReceiveMessageDto receiveMessageDto = new()
-            {
-                account = account,
-                pswd = pswd,
-                msg = msg,
-                mobile = mobile,
-                destcode = destcode,
-                motime = motime,
-                fsfid = fsfid
-            };
             var result = await _pushDomainService.ReceiveSms(receiveMessageDto);
 
-            return Ok(result);
+            return OpenResponse.Ok(result);
         }
         #endregion
 
+        #region 查询短信
         /// <summary>
         /// 查询短信
         /// </summary>
@@ -153,5 +113,8 @@ namespace Hotline.Api.Controllers
             return new PagedDto<MessageDataDto>(total, _mapper.Map<IReadOnlyList<MessageDataDto>>(items));
 
         }
+
+        #endregion
+
     }
 }

+ 2 - 1
src/Hotline.Api/Controllers/SettingController.cs

@@ -145,6 +145,7 @@ namespace Hotline.Api.Controllers
         /// <summary>
         /// 获取月份的日期设置
         /// </summary>
+        /// <param name="year"></param>
         /// <param name="month"></param>
         /// <returns></returns>
         [Permission(EPermission.DaySetings)]
@@ -158,7 +159,7 @@ namespace Hotline.Api.Controllers
         /// 设置日期
         /// </summary>
         /// <param name="request"></param>
-        /// <returns></returns>
+        /// <returns></returns>`
         [Permission(EPermission.SetDaySetting)]
         [HttpPost("setdaysettings")]
         public async Task SetDaySettings([FromBody]SetDaySettingsRequest request)

+ 3 - 2
src/Hotline.Application/Mappers/MapperConfigs.cs

@@ -5,11 +5,12 @@ using Hotline.FlowEngine.Workflows;
 using Hotline.Identity.Roles;
 using Hotline.KnowledgeBase;
 using Hotline.Orders;
+using Hotline.Push.FWMessage;
 using Hotline.Share.Dtos.CallCenter;
 using Hotline.Share.Dtos.FlowEngine;
 using Hotline.Share.Dtos.Knowledge;
 using Hotline.Share.Dtos.Order;
-using Hotline.Share.Dtos.Push;
+using Hotline.Share.Dtos.Push.FWMessage;
 using Hotline.Share.Dtos.Roles;
 using Hotline.Share.Dtos.Users;
 using Hotline.Users;
@@ -214,7 +215,7 @@ namespace Hotline.Application.Mappers
 
             #endregion
 
-            config.ForType<Hotline.Push.Message, MessageDataDto>()
+            config.ForType<Message, MessageDataDto>()
                .Map(d => d.SendUser, x => x.User.Name)
                .Map(d => d.SendOrganize, x => x.SystemOrganize.OrgName)
                .Map(d => d.OrderId, x => x.Order.Id)

+ 2 - 2
src/Hotline.Repository.SqlSugar/Push/MessageRepository.cs → src/Hotline.Repository.SqlSugar/Push/FWMessage/MessageRepository.cs

@@ -1,10 +1,10 @@
 using Hotline.KnowledgeBase;
-using Hotline.Push;
+using Hotline.Push.FWMessage;
 using Hotline.Repository.SqlSugar.DataPermissions;
 using SqlSugar;
 using XF.Domain.Dependency;
 
-namespace Hotline.Repository.SqlSugar.Push
+namespace Hotline.Repository.SqlSugar.Push.FWMessage
 {
     public class MessageRepository : BaseRepository<Message>, IMessageRepository, IScopeDependency
     {

+ 16 - 1
src/Hotline.Share/Dtos/Push/MessageDataDto.cs → src/Hotline.Share/Dtos/Push/FWMessage/MessageDataDto.cs

@@ -1,6 +1,6 @@
 using Hotline.Share.Enums.Push;
 
-namespace Hotline.Share.Dtos.Push
+namespace Hotline.Share.Dtos.Push.FWMessage
 {
     public record MessageDataDto
     {
@@ -63,5 +63,20 @@ namespace Hotline.Share.Dtos.Push
         /// 推送状态
         /// </summary>
         public EPushStatus Status { get; set; }
+
+        /// <summary>
+        /// 发送失败原因
+        /// </summary>
+        public string Reason { get; set; }
+
+        /// <summary>
+        /// 发送短信使用的数量
+        /// </summary>
+        public int? MsgCount { get; set; }
+
+        /// <summary>
+        /// 短信发送状态
+        /// </summary>
+        public ESendState SendState { get; set; }
     }
 }

+ 1 - 1
src/Hotline.Share/Dtos/Push/ReceiveMessageDto.cs → src/Hotline.Share/Dtos/Push/FWMessage/ReceiveMessageDto.cs

@@ -1,4 +1,4 @@
-namespace Hotline.Share.Dtos.Push
+namespace Hotline.Share.Dtos.Push.FWMessage
 {
     /// <summary>
     /// 丰窝短信接收值

+ 1 - 1
src/Hotline.Share/Dtos/Push/SendSmsModelDto.cs → src/Hotline.Share/Dtos/Push/FWMessage/SendSmsModelDto.cs

@@ -1,6 +1,6 @@
 using System.Runtime.Serialization;
 
-namespace Hotline.Share.Dtos.Push
+namespace Hotline.Share.Dtos.Push.FWMessage
 {
     public class SendSmsModelDto
     {

+ 1 - 1
src/Hotline.Share/Dtos/Push/SmsAccountInfo.cs → src/Hotline.Share/Dtos/Push/FWMessage/SmsAccountInfo.cs

@@ -1,4 +1,4 @@
-namespace Hotline.Share.Dtos.Push
+namespace Hotline.Share.Dtos.Push.FWMessage
 {
     public class SmsAccountInfo
     {

+ 0 - 6
src/Hotline.Share/Dtos/Push/PushMessageNotifyDto.cs

@@ -1,10 +1,4 @@
 using Hotline.Share.Enums.Push;
-using System;
-using System.Collections.Generic;
-using System.Data.SqlTypes;
-using System.Linq;
-using System.Text;
-using System.Threading.Tasks;
 
 namespace Hotline.Share.Dtos.Push
 {

+ 1 - 1
src/Hotline/Push/IMessageRepository.cs → src/Hotline/Push/FWMessage/IMessageRepository.cs

@@ -1,6 +1,6 @@
 using XF.Domain.Repository;
 
-namespace Hotline.Push
+namespace Hotline.Push.FWMessage
 {
     public interface IMessageRepository : IRepository<Message>
     {

+ 2 - 1
src/Hotline/Push/IPushDomainService.cs → src/Hotline/Push/FWMessage/IPushDomainService.cs

@@ -1,6 +1,7 @@
 using Hotline.Share.Dtos.Push;
+using Hotline.Share.Dtos.Push.FWMessage;
 
-namespace Hotline.Push
+namespace Hotline.Push.FWMessage
 {
     public interface IPushDomainService
     {

+ 14 - 2
src/Hotline/Push/Message.cs → src/Hotline/Push/FWMessage/Message.cs

@@ -5,7 +5,7 @@ using Hotline.Users;
 using SqlSugar;
 using XF.Domain.Repository;
 
-namespace Hotline.Push
+namespace Hotline.Push.FWMessage
 {
     /// <summary>
     /// 推送消息
@@ -24,6 +24,12 @@ namespace Hotline.Push
         [SugarColumn(ColumnDescription = "外部业务唯一编号", ColumnDataType = "varchar(50)")]
         public string ExternalId { get; set; }
 
+        /// <summary>
+        /// 业务系统短信ID
+        /// </summary>
+        [SugarColumn(ColumnDescription = "业务系统短信ID")]
+        public int WaitSendId { get; set; } = 0;
+
         /// <summary>
         /// 推送平台
         /// </summary>
@@ -99,7 +105,7 @@ namespace Hotline.Push
         /// <summary>
         /// 发送时间
         /// </summary>
-        [SugarColumn(ColumnDescription = "发送时间",IsNullable =true)]
+        [SugarColumn(ColumnDescription = "发送时间", IsNullable = true)]
         public DateTime? SendTime { get; set; }
 
         /// <summary>
@@ -120,6 +126,12 @@ namespace Hotline.Push
         [SugarColumn(ColumnDescription = "短信回复时间", IsNullable = true)]
         public DateTime? SmsReplyTime { get; set; }
 
+        /// <summary>
+        /// 发送失败原因等
+        /// </summary>
+        [SugarColumn(ColumnDescription = "发送失败原因等", ColumnDataType = "varchar(200)", IsNullable = true)]
+        public string? Reason { get; set; }
+
         /// <summary>
         /// 发送人
         /// </summary>

+ 100 - 39
src/Hotline/Push/PushDomainService.cs → src/Hotline/Push/FWMessage/PushDomainService.cs

@@ -1,5 +1,8 @@
-using Hotline.Push.Notifies;
+using Hotline.Identity.Accounts;
+using Hotline.Orders;
+using Hotline.Push.Notifies;
 using Hotline.Share.Dtos.Push;
+using Hotline.Share.Dtos.Push.FWMessage;
 using Hotline.Share.Enums.Push;
 using MapsterMapper;
 using MediatR;
@@ -10,48 +13,52 @@ using Microsoft.Extensions.Logging;
 using System.Net;
 using System.Text.RegularExpressions;
 using System.Xml;
+using XF.Domain.Cache;
 using XF.Domain.Dependency;
 using XF.Domain.Exceptions;
+using static System.Runtime.InteropServices.JavaScript.JSType;
 
-namespace Hotline.Push;
+namespace Hotline.Push.FWMessage;
 
 /// <summary>
 /// 
 /// </summary>
 public class PushDomainService : IPushDomainService, IScopeDependency
 {
-
+    #region 注入
     private readonly IMessageRepository _messageRepository;
     private readonly IMapper _mapper;
     private readonly IHttpClientFactory _httpClientFactory;
-    private readonly ILogger<PushDomainService> _logger;
     private readonly IConfiguration _config;
     private readonly SmsAccountInfo accountInfo = null;
     private readonly IMediator _mediator;
+    private readonly ITypedCache<CacheWaitSendId> _cacheWaitSendId;
 
     /// <summary>
     /// 
     /// </summary>
     /// <param name="messageRepository"></param>
     /// <param name="httpClientFactory"></param>
-    /// <param name="logger"></param>
     /// <param name="mapper"></param>
     /// <param name="config"></param>
     /// <param name="mediator"></param>
-    public PushDomainService(IMessageRepository messageRepository, IHttpClientFactory httpClientFactory, ILogger<PushDomainService> logger
-        , IMapper mapper, IConfiguration config, IMediator mediator)
+    /// <param name="cacheWaitSendId"></param>
+    public PushDomainService(IMessageRepository messageRepository, IHttpClientFactory httpClientFactory
+        , IMapper mapper, IConfiguration config, IMediator mediator, ITypedCache<CacheWaitSendId> cacheWaitSendId)
     {
         _messageRepository = messageRepository;
         _httpClientFactory = httpClientFactory;
-        _logger = logger;
         _mapper = mapper;
         _config = config;
         accountInfo = _config.GetSection("SmsAccountInfo").Get<SmsAccountInfo>();
         _mediator = mediator;
+        _cacheWaitSendId = cacheWaitSendId;
     }
+    #endregion
 
+    #region 短信发送
     /// <summary>
-    /// 业务处理
+    /// 短信发送
     /// </summary>
     /// <param name="messageDto"></param>
     /// <param name="cancellation"></param>
@@ -63,31 +70,47 @@ public class PushDomainService : IPushDomainService, IScopeDependency
             throw UserFriendlyException.SameMessage("消息不能为空!");
         }
 
-        #region 替换模板内容
-        //如果模板为空,参数为空则直接用短信内容
-        if (!string.IsNullOrEmpty(messageDto.Template) && messageDto.Params.Count > 0)
+        Message message = new();
+        int WaitSendId = 0;
+        var sendData = await _messageRepository.GetAsync(p => p.ExternalId == messageDto.ExternalId && p.PushBusiness == messageDto.PushBusiness && p.TelNumber == messageDto.TelNumber);
+        if (sendData != null)//处理重复数据
         {
-            string Template = messageDto.Template;
-            //正则查询模板中需要替换的内容
-            Regex regex = new(@"\{[a-zA-Z0-9]{1,}\}");
-            var matches = regex.Matches(Template);
-            if (matches != null && matches.Count != messageDto.Params.Count)
-                //参数与需要替换的字符数不匹配
-                //throw UserFriendlyException.SameMessage("模板需要参数与实际传递参数个数不匹配!");
-                messageDto.Remark = "模板需要参数与实际传递参数个数不匹配!";
+            WaitSendId = sendData.WaitSendId;
+            messageDto = _mapper.Map<MessageDto>(sendData);
+            message = sendData;
+        }
+        else
+        {
+
+            #region 替换模板内容
+            string reason = null;
+            //如果模板为空,参数为空则直接用短信内容
+            if (!string.IsNullOrEmpty(messageDto.Template) && messageDto.Params.Count > 0)
+            {
+                string Template = messageDto.Template;
+                //正则查询模板中需要替换的内容
+                Regex regex = new(@"\{[a-zA-Z0-9]{1,}\}");
+                var matches = regex.Matches(Template);
+                if (matches != null && matches.Count != messageDto.Params.Count)
+                    //参数与需要替换的字符数不匹配
+                    //throw UserFriendlyException.SameMessage("模板需要参数与实际传递参数个数不匹配!");
+                    reason = "模板需要参数与实际传递参数个数不匹配!";
 
-            //根据正则查询出来的匹配项替换内容
-            for (int i = 0; i < matches.Count; i++)
-                Template = Template.Replace(matches[i].ToString(), messageDto.Params[i]);
+                //根据正则查询出来的匹配项替换内容
+                for (int i = 0; i < matches.Count; i++)
+                    Template = Template.Replace(matches[i].ToString(), messageDto.Params[i]);
 
-            messageDto.Content = Template;
+                messageDto.Content = Template;
+            }
+            #endregion
+            message = _mapper.Map<Message>(messageDto);
+            WaitSendId = GenerateWaitSendId();
+            message.WaitSendId = WaitSendId;
+            message.Reason = reason;
         }
-        #endregion
-
-        var message = _mapper.Map<Message>(messageDto);
 
         //调用短信发送业务
-        var strResult = await SmsSend(messageDto);
+        var strResult = await SmsSend(messageDto, WaitSendId);
 
         //处理返回值
         if (true == strResult.Contains("ok,"))  // ok,35797257
@@ -97,17 +120,23 @@ public class PushDomainService : IPushDomainService, IScopeDependency
             {
                 // 短信服务中心返回 IFSFWID,用于短信发送状态接收时匹配
                 message.SmsWaitSendingId = strArray[1];
-                message.SendState = Share.Enums.Push.ESendState.Sending;
+                message.SendState = ESendState.Sending;
+                message.Status = EPushStatus.Success;
             }
         }
         else
         {
-            message.Remark = strResult;
+            message.Reason = strResult;
+            message.Status = EPushStatus.Failed;
         }
-        //写入本地数据库
-        await _messageRepository.AddAsync(message);
+        if (sendData != null)
+            await _messageRepository.UpdateAsync(message);//跟新本地数据库
+        else
+            await _messageRepository.AddAsync(message);//写入本地数据库
     }
 
+    #endregion
+
     #region 查询账号短信剩余量
     /// <summary>
     /// 查询账号短信剩余量
@@ -141,6 +170,7 @@ public class PushDomainService : IPushDomainService, IScopeDependency
     }
     #endregion
 
+    #region 短信发送状态回调接口
     /// <summary>
     /// 短信发送状态回调接口
     /// </summary>
@@ -161,7 +191,6 @@ public class PushDomainService : IPushDomainService, IScopeDependency
             strResult = "error,参数错误";
             return strResult;
         }
-
         //修改数据
         var data = await _messageRepository.GetAsync(p => p.SmsWaitSendingId == receiveMessageDto.msgid);
         if (data != null)
@@ -170,17 +199,19 @@ public class PushDomainService : IPushDomainService, IScopeDependency
             data.MsgCount = receiveMessageDto.msgcount;
             data.SendState = (ESendState)receiveMessageDto.state;
             data.SmsSendingCompletedId = receiveMessageDto.sfid + "";
-            data.Remark = receiveMessageDto.errormsg;
+            data.Reason = receiveMessageDto.errormsg;
             await _messageRepository.UpdateAsync(data);
             // 成功返回值必须是ok
             strResult = "ok";
         }
         else
             strResult = "error,调用失败";
-
         return strResult;
     }
 
+    #endregion
+
+    #region 短信接收
     /// <summary>
     /// 短信接收
     /// </summary>
@@ -222,6 +253,8 @@ public class PushDomainService : IPushDomainService, IScopeDependency
 
     }
 
+    #endregion
+
     #region 私有方法
 
     #region 短信发送
@@ -229,15 +262,15 @@ public class PushDomainService : IPushDomainService, IScopeDependency
     /// 短信发送
     /// </summary>
     /// <returns></returns>
-    private async Task<string> SmsSend(MessageDto messageDto)
+    private async Task<string> SmsSend(MessageDto messageDto, int WaitSendId)
     {
-        string strResult = "";
+        string strResult;
         try
         {
             SendSmsModelDto tSms = new()
             {
                 // 业务系统短信ID
-                nWaitID = 1,
+                nWaitID = WaitSendId,
                 // 短信类型1:默认2:营销
                 strType = "1",
                 // 业务系统工单ID
@@ -266,7 +299,8 @@ public class PushDomainService : IPushDomainService, IScopeDependency
                 strAccountPwd = accountInfo.AccountPwd
             };
             // 序列化
-            string strSendJson = Newtonsoft.Json.JsonConvert.SerializeObject(tSms);
+            string strSendJson = Newtonsoft.Json.JsonConvert.SerializeObject(tSms);//System.Text.Json.JsonSerializer.Serialize(tSms);//
+
             // 参数加密
             string strKey = await FwEncrypt(strSendJson);
             // 短信发送参数
@@ -348,5 +382,32 @@ public class PushDomainService : IPushDomainService, IScopeDependency
     }
     #endregion
 
+    #region 生成待发短信ID缓存
+    /// <summary>
+    /// 生成待发短信ID缓存
+    /// </summary>
+    /// <returns></returns>
+    private int GenerateWaitSendId()
+    {
+        var cacheKey = "PushMessageWaitSendId";
+        var cacheWaitSend = _cacheWaitSendId.GetOrSet(cacheKey, f =>
+        {
+            var waitSendId = _messageRepository.Queryable(true)
+                .MaxAsync(p => p.WaitSendId)
+                .GetAwaiter()
+                .GetResult();
+            return new CacheWaitSendId { WaitSendId = waitSendId };
+        }, TimeSpan.FromDays(1));
+        cacheWaitSend!.WaitSendId += 1;
+
+        _cacheWaitSendId.Set(cacheKey, cacheWaitSend);
+        return cacheWaitSend.WaitSendId;
+    }
+
+    public class CacheWaitSendId
+    {
+        public int WaitSendId { get; set; }
+    }
+    #endregion
     #endregion
 }