YD_Event.Admin/YD_Event.Application/Merchant/MerchantCertificationService.cs
2026-03-30 11:23:24 +08:00

220 lines
8.7 KiB
C#

using Microsoft.Extensions.Caching.Memory;
using YD_Event.Application.Auth;
using YD_Event.Application.Merchant.Dtos;
using YD_Event.Core.Entities;
using YD_Event.Core.Enum;
using YD_Event.Core.Infrastructure.Aliyun.Sms;
namespace YD_Event.Application.Merchant;
/// <summary>
/// 单位认证管理
/// </summary>
public class MerchantCertificationService : BaseService<MerchantCertification>, ITransient
{
private readonly ISqlSugarRepository<MerchantCertification> _repository;
private readonly ISqlSugarRepository<YD_Event.Core.Entities.Merchant> _merchantRepository;
private readonly AuthManager _authManager;
private readonly IIdGenerator _idGenerator;
private readonly IMemoryCache _memoryCache;
private readonly AliyunSmsService _smsService;
public MerchantCertificationService(
ISqlSugarRepository<MerchantCertification> repository,
ISqlSugarRepository<YD_Event.Core.Entities.Merchant> merchantRepository,
AuthManager authManager,
IIdGenerator idGenerator,
IMemoryCache memoryCache,
AliyunSmsService smsService) : base(repository)
{
_repository = repository;
_merchantRepository = merchantRepository;
_authManager = authManager;
_idGenerator = idGenerator;
_memoryCache = memoryCache;
_smsService = smsService;
}
/// <summary>
/// 发送验证码
/// </summary>
[DisplayName("发送验证码")]
[HttpPost("sendVerificationCode")]
[AllowAnonymous]
public async Task SendVerificationCode([FromQuery] string phone)
{
if (string.IsNullOrWhiteSpace(phone) || !System.Text.RegularExpressions.Regex.IsMatch(phone, @"^1[3456789]\d{9}$"))
throw Oops.Bah("手机号格式不正确");
var code = new Random().Next(100000, 999999).ToString();
_memoryCache.Set($"verification_code_{phone}", code, TimeSpan.FromMinutes(5));
await _smsService.SendVerificationCodeAsync(phone, code);
}
/// <summary>
/// 提交单位认证
/// </summary>
[DisplayName("提交单位认证")]
[HttpPost("submitCertification")]
public async Task SubmitCertification(SubmitCertificationInput input)
{
if (!_memoryCache.TryGetValue($"verification_code_{input.ContactPhone}", out string? cachedCode) || cachedCode != input.VerificationCode)
{
throw Oops.Bah("验证码错误或已过期");
}
var userId = _authManager.UserId;
var certification = await _repository.GetFirstAsync(c => c.UserId == userId && !c.DeleteMark);
if (certification == null)
{
certification = new MerchantCertification
{
Id = _idGenerator.NextId(),
UserId = userId,
MerchantName = input.MerchantName,
CreditCode = input.CreditCode,
BusinessLicenseUrl = input.BusinessLicenseUrl,
Address = input.Address,
ContactPhone = input.ContactPhone,
AuditStatus = AuditStatus.PendingAudit,
Status = AvailabilityStatus.Enable,
DeleteMark = false,
CreatedUserId = userId,
CreatedTime = DateTime.Now
};
await _repository.InsertAsync(certification);
}
else
{
certification.MerchantName = input.MerchantName;
certification.CreditCode = input.CreditCode;
certification.BusinessLicenseUrl = input.BusinessLicenseUrl;
certification.Address = input.Address;
certification.ContactPhone = input.ContactPhone;
certification.AuditStatus = AuditStatus.PendingAudit;
certification.UpdatedUserId = userId;
certification.UpdatedTime = DateTime.Now;
await _repository.UpdateAsync(certification);
}
_memoryCache.Remove($"verification_code_{input.ContactPhone}");
}
/// <summary>
/// 获取我的认证信息
/// </summary>
[DisplayName("获取我的认证信息")]
[HttpGet("myCertification")]
public async Task<MerchantCertificationOutput?> GetMyCertification()
{
var userId = _authManager.UserId;
var certification = await _repository.GetFirstAsync(c => c.UserId == userId && !c.DeleteMark);
return certification?.Adapt<MerchantCertificationOutput>();
}
/// <summary>
/// 单位认证分页查询
/// </summary>
[DisplayName("单位认证分页查询")]
[HttpGet("page")]
public async Task<PageResult<MerchantCertificationOutput>> Page([FromQuery] MerchantCertificationQueryInput input)
{
var pageResult = await _repository.AsQueryable()
.WhereIF(!string.IsNullOrWhiteSpace(input.MerchantName), x => x.MerchantName.Contains(input.MerchantName))
.WhereIF(!string.IsNullOrWhiteSpace(input.ContactPhone), x => x.ContactPhone.Contains(input.ContactPhone))
.WhereIF(input.AuditStatus.HasValue, x => x.AuditStatus == input.AuditStatus)
.WhereIF(input.StartTime.HasValue, x => x.CreatedTime >= input.StartTime)
.WhereIF(input.EndTime.HasValue, x => x.CreatedTime <= input.EndTime)
.OrderByDescending(x => x.CreatedTime)
.Select(x => new MerchantCertificationOutput
{
Id = x.Id,
UserId = x.UserId,
MerchantName = x.MerchantName,
CreditCode = x.CreditCode,
BusinessLicenseUrl = x.BusinessLicenseUrl,
ContactPhone = x.ContactPhone,
Address = x.Address,
AuditStatus = x.AuditStatus,
AuditRemark = x.AuditRemark,
CreatedTime = x.CreatedTime,
UpdatedTime = x.UpdatedTime
})
.ToPagedListAsync(input);
return pageResult;
}
/// <summary>
/// 获取单位认证详情
/// </summary>
[DisplayName("获取单位认证详情")]
[HttpGet("detail")]
public async Task<MerchantCertificationOutput?> Detail([FromQuery] long id)
{
var certification = await _repository.GetByIdAsync(id);
return certification?.Adapt<MerchantCertificationOutput>();
}
/// <summary>
/// 审核单位认证
/// </summary>
[DisplayName("审核单位认证")]
[HttpPost("audit")]
[UnitOfWork]
public async Task Audit([FromBody] AuditCertificationInput input)
{
if (input.Ids == null || !input.Ids.Any()) return;
var certifications = await _repository.GetListAsync(c => input.Ids.Contains(c.Id) && !c.DeleteMark);
var userId = _authManager.UserId;
foreach (var certification in certifications)
{
certification.AuditStatus = input.Status;
certification.AuditRemark = input.Remark;
certification.UpdatedUserId = userId;
certification.UpdatedTime = DateTime.Now;
if (input.Status == AuditStatus.Pass)
{
var merchant = await _merchantRepository.GetFirstAsync(m => m.CreatedUserId == certification.UserId && !m.DeleteMark);
if (merchant == null)
{
merchant = new YD_Event.Core.Entities.Merchant
{
Id = _idGenerator.NextId(),
MerchantName = certification.MerchantName,
CreditCode = certification.CreditCode,
BusinessLicenseUrl = certification.BusinessLicenseUrl,
Address = certification.Address,
ContactPhone = certification.ContactPhone,
BankName = "",
AccountNumber = "",
AuditStatus = AuditStatus.Pass,
Status = AvailabilityStatus.Enable,
DeleteMark = false,
CreatedUserId = certification.UserId,
CreatedTime = DateTime.Now
};
await _merchantRepository.InsertAsync(merchant);
}
else
{
merchant.MerchantName = certification.MerchantName;
merchant.CreditCode = certification.CreditCode;
merchant.BusinessLicenseUrl = certification.BusinessLicenseUrl;
merchant.Address = certification.Address;
merchant.ContactPhone = certification.ContactPhone;
merchant.UpdatedUserId = userId;
merchant.UpdatedTime = DateTime.Now;
await _merchantRepository.UpdateAsync(merchant);
}
}
}
await _repository.UpdateRangeAsync(certifications);
}
}