sunpengfei
2025-11-28 312ebed2d86858e4fb57ec09679244e9b806b57f
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
using ApiTools.Core;
using Furion.DatabaseAccessor;
using Mapster;
using MediatR;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace ApiTools.CommonServer.Application
{
    /// <summary>
    /// 保存短信配置
    /// </summary>
    public class SaveSmsSettingCommandHandler(
            IRepository<SmsSetting> rep
        ) :
        IRequestHandler<SaveSmsSettingCommand, Guid>
    {
        private readonly IRepository<SmsSetting> rep = rep;
 
        /// <summary>
        /// 保存短信配置
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<Guid> Handle(SaveSmsSettingCommand request, CancellationToken cancellationToken)
        {
            var logier = JwtUtils.GetCurrentLogier();
            var entity = await rep.AsQueryable()
                .Include(it => it.Accesses)
                .Where(it => it.ChannelId == logier.ChannelId)
                .FirstOrDefaultAsync();
            var add = false;
            if (entity == null)
            {
                entity = new SmsSetting();
                entity.ChannelId = logier.ChannelId;
                add = true;
            }
            request.Adapt(entity);
            if (add)
            {
                await rep.InsertAsync(entity);
            }
            else
            {
                await rep.UpdateAsync(entity);
            }
            return entity.Id;
        }
    }
}