using EasyCaching.Core; 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; /// /// 单位认证管理 /// public class MerchantCertificationService : BaseService, ITransient { private readonly ISqlSugarRepository _repository; private readonly ISqlSugarRepository _merchantRepository; private readonly AuthManager _authManager; private readonly IIdGenerator _idGenerator; private readonly IEasyCachingProvider _cachingProvider; private readonly AliyunSmsService _smsService; public MerchantCertificationService( ISqlSugarRepository repository, ISqlSugarRepository merchantRepository, AuthManager authManager, IIdGenerator idGenerator, IEasyCachingProvider cachingProvider, AliyunSmsService smsService) : base(repository) { _repository = repository; _merchantRepository = merchantRepository; _authManager = authManager; _idGenerator = idGenerator; _cachingProvider = cachingProvider; _smsService = smsService; } /// /// 提交单位认证 /// [DisplayName("提交单位认证")] [HttpPost("submitCertification")] public async Task SubmitCertification(SubmitCertificationInput input) { var cacheKey = $"sms:code:{input.ContactPhone}"; var cachedCodeResult = _cachingProvider.Get(cacheKey); string? cachedCode = cachedCodeResult.Value; if (!cachedCodeResult.HasValue || 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); } _cachingProvider.Remove(cacheKey); } /// /// 获取我的认证信息 /// [DisplayName("获取我的认证信息")] [HttpGet("myCertification")] public async Task GetMyCertification() { var userId = _authManager.UserId; var certification = await _repository.GetFirstAsync(c => c.UserId == userId && !c.DeleteMark); return certification?.Adapt(); } /// /// 单位认证分页查询 /// [DisplayName("单位认证分页查询")] [HttpGet("page")] public async Task> 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; } /// /// 获取单位认证详情 /// [DisplayName("获取单位认证详情")] [HttpGet("detail")] public async Task Detail([FromQuery] long id) { var certification = await _repository.GetByIdAsync(id); return certification?.Adapt(); } /// /// 审核单位认证 /// [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); } }