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
{
///
/// 保存短信配置
///
public class SaveSmsSettingCommandHandler(
IRepository rep
) :
IRequestHandler
{
private readonly IRepository rep = rep;
///
/// 保存短信配置
///
///
///
///
public async Task 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;
}
}
}