sunpengfei
2025-09-30 a6e5a2e630a4a1dcfd9def94c20bde6bec89f96f
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
using Baidu.Aip.BodyAnalysis;
using ApiTools.Core;
using Furion.DatabaseAccessor;
using Furion.HttpRemote;
using MediatR;
using Microsoft.AspNetCore.Http;
using Microsoft.EntityFrameworkCore;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace ApiTools.CommonServer.Application
{
    /// <summary>
    /// 诚立业短信平台回传通知
    /// </summary>
    public class SmsChengLiYeNotifyCommandHandler(
            IHttpContextAccessor httpContextAccessor,
            IRepository<SmsSetting> repSmsSetting,
            IRepository<SmsLog> repSmsLog,
            ApiTools.Core.SmsUtils smsUtils
        ) :
        IRequestHandler<SmsChengLiYeNotifyCommand, bool>
    {
        private readonly IHttpContextAccessor httpContextAccessor = httpContextAccessor;
        private readonly IRepository<SmsSetting> repSmsSetting = repSmsSetting;
        private readonly IRepository<SmsLog> repSmsLog = repSmsLog;
        private readonly ApiTools.Core.SmsUtils smsUtils = smsUtils;
 
        /// <summary>
        /// 诚立业短信平台回传通知
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<bool> Handle(SmsChengLiYeNotifyCommand request, CancellationToken cancellationToken)
        {
            if (request.MsgReports.IsNotNull())
            {
                var templateCodes = await EnumUtils.GetModel<EnumSmsTemplateCode>();
                var msgIds = request.MsgReports.DistinctSelect(it => it.Msgid.ToString());
                var entities = await repSmsLog.AsQueryable()
                    .Where(it =>
                        it.Access == EnumSmsAccess.ChengLiYe
                        && msgIds.Contains(it.RequestId)
                        && it.Status == EnumSmsStatus.InProcess)
                    .ToListAsync();
                var channelIds = entities.DistinctSelect(it => it.ChannelId.HasValue, it => it.ChannelId.Value);
                var settings = await repSmsSetting.AsQueryable().AsNoTracking()
                    .Include(it => it.Accesses)
                    .Where(it => it.ChannelId.HasValue && channelIds.Contains(it.ChannelId.Value))
                    .ToListAsync();
                foreach (var entity in entities)
                {
                    var report = request.MsgReports.FirstOrDefault(it => it.Msgid.ToString() == entity.RequestId);
                    if (report != null)
                    {
                        entity.Status = report.Status == "DELIVRD"
                            ? EnumSmsStatus.Success
                            : EnumSmsStatus.Fail;
                        entity.Code = report.Status;
                        entity.Message = report.StatusDes;
                        await repSmsLog.UpdateNowAsync(entity);
 
                        if (entity.Status == EnumSmsStatus.Fail)
                        {
                            var templateCode = templateCodes.Items.FirstOrDefault(it => it.Name == entity.TemplateCode)?.Enum;
                            if (templateCode.HasValue)
                            {
                                var templateParam = entity.TemplateParam.IsNotNull()
                                    ? JObject.Parse(entity.TemplateParam)
                                    : null;
                                var setting = settings.FirstOrDefault(it => it.ChannelId == entity.ChannelId);
                                await smsUtils.Send(setting, entity, new SendSmsModel
                                {
                                    PhoneNumber = entity.PhoneNumber,
                                    TemplateCode = templateCode.Value,
                                    Expiry = entity.Expiry,
                                }, templateParam, cancellationToken);
                            }
                        }
                    }
                }
            }
            return true;
        }
    }
}