using FlexJobApi.Core;
using Furion.DatabaseAccessor;
using MediatR;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FlexJobApi.CommonServer.Application
{
///
/// 保存编号地址
///
///
public class CodeUrlCommandHandler(
IRepository rep
) :
IRequestHandler
{
private readonly IRepository rep = rep;
///
/// 保存编号地址
///
///
///
///
public async Task Handle(SaveCodeUrlCommand request, CancellationToken cancellationToken)
{
var entity = new CodeUrl
{
Scene = request.Scene,
Url = request.Url,
ExpiredTime = request.ExpiredTime
};
await SetCode(entity);
await rep.InsertAsync(entity);
return entity.Code;
}
private async Task SetCode(CodeUrl entity)
{
var now = DateTime.Now;
entity.Code = $"{StringUtils.GenerateRandomString(5)}";
var exist = await rep.AnyAsync(it =>
it.Scene == entity.Scene
&& it.Code == entity.Code
&& (it.ExpiredTime == null
|| it.ExpiredTime > now));
if (exist)
{
await SetCode(entity);
}
return entity.Code;
}
}
}