245 lines
8.4 KiB
C#
245 lines
8.4 KiB
C#
using Microsoft.Extensions.Caching.Memory;
|
||
using YD_Event.Application.Auth;
|
||
using YD_Event.Application.Merchant.Dtos;
|
||
using YD_Event.Core.Infrastructure.Aliyun.Sms;
|
||
using YD_Event.Core.Entities;
|
||
using YD_Event.Core.Enum;
|
||
|
||
namespace YD_Event.Application.Merchant;
|
||
|
||
/// <summary>
|
||
/// 商户管理
|
||
/// </summary>
|
||
public class MerchantService : BaseService<YD_Event.Core.Entities.Merchant>, ITransient
|
||
{
|
||
private readonly ISqlSugarRepository<YD_Event.Core.Entities.Merchant> _repository;
|
||
private readonly ISqlSugarRepository<UserCoupon> _userCouponRepository;
|
||
private readonly AuthManager _authManager;
|
||
private readonly IIdGenerator _idGenerator;
|
||
private readonly IMemoryCache _memoryCache;
|
||
private readonly AliyunSmsService _smsService;
|
||
|
||
public MerchantService(ISqlSugarRepository<YD_Event.Core.Entities.Merchant> repository,
|
||
ISqlSugarRepository<UserCoupon> userCouponRepository,
|
||
AuthManager authManager,
|
||
IIdGenerator idGenerator,
|
||
IMemoryCache memoryCache,
|
||
AliyunSmsService smsService) : base(repository)
|
||
{
|
||
_repository = repository;
|
||
_userCouponRepository = userCouponRepository;
|
||
_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("手机号格式不正确");
|
||
|
||
// 生成6位验证码
|
||
var code = new Random().Next(100000, 999999).ToString();
|
||
|
||
// 存入缓存,有效期5分钟
|
||
_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 merchant = await _repository.GetFirstAsync(m => m.CreatedUserId == userId);
|
||
|
||
if (merchant == null)
|
||
{
|
||
// 新增商户
|
||
merchant = new YD_Event.Core.Entities.Merchant
|
||
{
|
||
Id = _idGenerator.NextId(),
|
||
MerchantName = input.MerchantName,
|
||
CreditCode = input.CreditCode,
|
||
BusinessLicenseUrl = input.BusinessLicenseUrl,
|
||
Address = input.Address,
|
||
ContactPhone = input.ContactPhone,
|
||
BankName = "", // 默认为空,后续完善
|
||
AccountNumber = "", // 默认为空,后续完善
|
||
AuditStatus = AuditStatus.PendingAudit, // 待审核
|
||
Status = AvailabilityStatus.Enable,
|
||
CreatedUserId = userId,
|
||
CreatedTime = DateTime.Now
|
||
};
|
||
await _repository.InsertAsync(merchant);
|
||
}
|
||
else
|
||
{
|
||
// 更新商户信息
|
||
merchant.MerchantName = input.MerchantName;
|
||
merchant.CreditCode = input.CreditCode;
|
||
merchant.BusinessLicenseUrl = input.BusinessLicenseUrl;
|
||
merchant.Address = input.Address;
|
||
merchant.ContactPhone = input.ContactPhone;
|
||
merchant.AuditStatus = AuditStatus.PendingAudit; // 重新提交后变为待审核
|
||
merchant.UpdatedUserId = userId;
|
||
merchant.UpdatedTime = DateTime.Now;
|
||
|
||
await _repository.UpdateAsync(merchant);
|
||
}
|
||
|
||
// 清除验证码
|
||
_memoryCache.Remove($"verification_code_{input.ContactPhone}");
|
||
}
|
||
|
||
/// <summary>
|
||
/// 获取我的认证信息
|
||
/// </summary>
|
||
[DisplayName("获取我的认证信息")]
|
||
[HttpGet("myCertification")]
|
||
public async Task<YD_Event.Core.Entities.Merchant> GetMyCertification()
|
||
{
|
||
var userId = _authManager.UserId;
|
||
var merchant = await _repository.GetFirstAsync(m => m.CreatedUserId == userId);
|
||
return merchant;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 商户分页查询
|
||
/// </summary>
|
||
[DisplayName("商户分页查询")]
|
||
[HttpGet("page")]
|
||
public async Task<PageResult<YD_Event.Core.Entities.Merchant>> Page([FromQuery] MerchantQueryInput input)
|
||
{
|
||
return 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)
|
||
.ToPagedListAsync(input);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 添加商户
|
||
/// </summary>
|
||
[DisplayName("添加商户")]
|
||
[HttpPost("add")]
|
||
public async Task Add(AddMerchantInput input)
|
||
{
|
||
var entity = input.Adapt<YD_Event.Core.Entities.Merchant>();
|
||
entity.Id = _idGenerator.NextId();
|
||
await _repository.InsertAsync(entity);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 更新商户
|
||
/// </summary>
|
||
[DisplayName("更新商户")]
|
||
[HttpPut("edit")]
|
||
public async Task Update(UpdateMerchantInput input)
|
||
{
|
||
var entity = await _repository.GetByIdAsync(input.Id);
|
||
if (entity == null) throw Oops.Bah("商户不存在");
|
||
|
||
input.Adapt(entity);
|
||
await _repository.UpdateAsync(entity);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 删除商户
|
||
/// </summary>
|
||
[DisplayName("删除商户")]
|
||
[HttpPost("delete")]
|
||
public async Task Delete([FromBody] List<long> ids)
|
||
{
|
||
await _repository.DeleteAsync(x => ids.Contains(x.Id));
|
||
}
|
||
|
||
/// <summary>
|
||
/// 获取商户详情
|
||
/// </summary>
|
||
[DisplayName("获取商户详情")]
|
||
[HttpGet("detail")]
|
||
public async Task<YD_Event.Core.Entities.Merchant> Detail([FromQuery] long id)
|
||
{
|
||
var entity = await _repository.GetByIdAsync(id);
|
||
if (entity == null) throw Oops.Bah("商户不存在");
|
||
return entity;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 审核商户
|
||
/// </summary>
|
||
[HttpPost("audit")]
|
||
[UnitOfWork]
|
||
public async Task Audit([FromBody] MerchantAuditInput input)
|
||
{
|
||
if (input.Ids == null || !input.Ids.Any()) return;
|
||
|
||
await _repository.UpdateAsync(u => new YD_Event.Core.Entities.Merchant
|
||
{
|
||
AuditStatus = input.Status,
|
||
AuditRemark = input.Remark,
|
||
UpdatedTime = DateTime.Now,
|
||
UpdatedUserId = _authManager.UserId
|
||
}, u => input.Ids.Contains(u.Id));
|
||
}
|
||
|
||
/// <summary>
|
||
/// 发放优惠券
|
||
/// </summary>
|
||
[HttpPost("issueCoupon")]
|
||
[UnitOfWork]
|
||
public async Task IssueCoupon([FromBody] IssueCouponInput input)
|
||
{
|
||
if (input.MerchantIds == null || !input.MerchantIds.Any()) return;
|
||
if (input.Count <= 0) throw Oops.Bah("发放数量必须大于0");
|
||
|
||
var merchants = await _repository.GetListAsync(m => input.MerchantIds.Contains(m.Id));
|
||
if (!merchants.Any()) return;
|
||
|
||
var coupons = new List<UserCoupon>();
|
||
var now = DateTime.Now;
|
||
|
||
foreach (var merchant in merchants)
|
||
{
|
||
for (int i = 0; i < input.Count; i++)
|
||
{
|
||
coupons.Add(new UserCoupon
|
||
{
|
||
Id = _idGenerator.NextId(),
|
||
UserId = merchant.CreatedUserId, // Issue to the creator of the merchant
|
||
CouponId = input.CouponId,
|
||
Status = 0, // Unused
|
||
ClaimTime = now,
|
||
CreatedTime = now,
|
||
CreatedUserId = _authManager.UserId
|
||
});
|
||
}
|
||
}
|
||
|
||
await _userCouponRepository.InsertRangeAsync(coupons);
|
||
}
|
||
}
|