using Aop.Api.Domain; using Azure; using FlexJobApi.Core; using Furion.DatabaseAccessor; using Furion.DistributedIDGenerator; using Furion.FriendlyException; using Furion.HttpRemote; using Mapster; using MediatR; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Options; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace FlexJobApi.UserServer.Application { /// /// 支付宝资金二级商户KYB代进件 /// public class EnterpriseWalletExpandindirectCommandHandler( AlipayUtils alipayUtils, IHttpRemoteService httpRemoteService, IRepository rep, IRepository repEnterpriseWallet ) : IRequestHandler, IRequestHandler, IRequestHandler { private readonly AlipayUtils alipayUtils = alipayUtils; private readonly IHttpRemoteService httpRemoteService = httpRemoteService; private readonly IRepository rep = rep; private readonly IRepository repEnterpriseWallet = repEnterpriseWallet; /// /// 支付宝资金二级商户KYB代进件 /// /// /// /// [UnitOfWork(false)] public async Task Handle(EnterpriseWalletExpandindirectCreateCommand request, CancellationToken cancellationToken) { var logier = JwtUtils.GetCurrentLogier(); if (logier.Type == EnumUserType.Enterprise) { request.EnterpriseId = logier.EnterpriseId; } if (request.EnterpriseId == null) throw Oops.Oh(EnumErrorCodeType.s400, "请填写企业Id"); var wallet = await repEnterpriseWallet.AsQueryable().AsNoTracking() .Where(it => it.EnterpriseId == request.EnterpriseId) .FirstOrDefaultAsync(); if (wallet == null) { throw Oops.Oh(EnumErrorCodeType.s404, "企业钱包"); } var source = await rep.AsQueryable() .OrderByDescending(it => it.CreatedTime) .Where(it => it.WalletId == wallet.Id && it.OrderId != null && it.OrderId != "") .FirstOrDefaultAsync(); var entity = new EnterpriseWalletExpandindirectOrder(); entity.WalletId = wallet.Id; if (source != null) { if (source.OrderStatus == EnumEnterpriseWalletExpandindirectOrderStatus.PROCESSING) throw Oops.Oh(EnumErrorCodeType.s510, "正在审核中,请勿重复提交"); if (source.OrderStatus == EnumEnterpriseWalletExpandindirectOrderStatus.VALID) throw Oops.Oh(EnumErrorCodeType.s510, "已生效"); entity.BizType = "MODIFY"; entity.OriginalOrderId = source.OrderId; } else { entity.BizType = "CREATE"; } entity.ProductCode = "MERCHANT_TRANSFER_EXPAND_INDIRECT"; entity.BizScene = "ENTRUST_SATF_TRANSFER"; entity.IdentityType = "ALIPAY_LOGON_ID"; entity.Scene = EnumEnterpriseWalletExpandindirectOrderScene.YONGJIN_BAOCHOU; entity.SceneCode = entity.Scene.ToString(); entity.Files = request.Files .Select(it => new EnterpriseWalletExpandindirectOrderFile { Type = it.Type, File = it.File }) .ToList(); entity.SceneImage = await GetImages(entity, EnumEnterpriseWalletExpandindirectOrderFileType.Scene); entity.SceneQualificationImage = await GetImages(entity, EnumEnterpriseWalletExpandindirectOrderFileType.SceneQualification); entity.Sites = request.SitesInfo.ToJson(); request.Adapt(entity); await SetCode(entity); await rep.InsertNowAsync(entity); var model = new AlipayFundExpandindirectCreateModel(); model.ProductCode = entity.ProductCode; model.BizScene = entity.BizScene; model.OutBizNo = entity.OutBizNo; model.BizType = entity.BizType; model.OriginalOrderId = entity.OriginalOrderId; model.SceneCode = entity.SceneCode; model.SceneDirections = entity.SceneDirections; model.SceneImage = entity.SceneImage; model.SceneQualificationImage = entity.SceneQualificationImage; model.Sites = entity.Sites; var secondaryPartnerInfo = new SecondaryPartnerInfo(); secondaryPartnerInfo.Identity = entity.Identity; secondaryPartnerInfo.Name = entity.Name; secondaryPartnerInfo.IdentityType = entity.IdentityType; model.SecondaryPartnerInfo = secondaryPartnerInfo; var result = alipayUtils.FundExpandindirectCreate(model, $"/api/user/enterpriseWallet/alipayFundExpandindirectCreateNotify"); if (result.IsError) { entity.RiskReviewRemark = result.Msg; } entity.TaskFinishTime = result.TaskFinishTime.ToDateTime(); entity.OrderId = result.OrderId; entity.Status = result.Status; entity.OrderStatus = entity.Status.ToEnum("未识别的状态")!.Value; await rep.UpdateNowAsync(entity); if (result.IsError) { throw Oops.Oh(EnumErrorCodeType.s510, result.Msg); } return entity.Id; } private async Task GetImages(EnterpriseWalletExpandindirectOrder entity, EnumEnterpriseWalletExpandindirectOrderFileType type) { var imageIds = new List(); foreach (var file in entity.Files) { if (file.Type == type) { var url = AliyunOSSUtils.GetUrl(file.File); var buffer = await httpRemoteService.GetAsByteArrayAsync(url); var response = alipayUtils.FundExpandindirectImageUpload(new Aop.Api.Request.AlipayFundExpandindirectImageUploadRequest { ProductCode = "MERCHANT_TRANSFER_EXPAND_INDIRECT", BizScene = "ENTRUST_SATF_TRANSFER", ImageType = file.File.Substring(file.File.LastIndexOf('.') + 1).ToLower(), ImageContent = new Aop.Api.Util.FileItem(file.File.Substring(file.File.LastIndexOf('/') + 1), buffer) }); if (response.IsError) throw Oops.Oh(EnumErrorCodeType.s510, response.SubMsg ?? response.Msg); imageIds.Add(response.ImageId); } } return imageIds.SplitJoin(","); } private async Task SetCode(EnterpriseWalletExpandindirectOrder entity) { entity.OutBizNo = $"{DateTime.Now:yyyyMMddHHmmss}{new Random(IDGen.NextID().GetHashCode()).Next(1000, 9999)}"; var exist = await rep.AsQueryable().AsNoTracking() .AnyAsync(it => it.OutBizNo == entity.OutBizNo); if (exist) { await SetCode(entity); } } /// /// 支付宝资金二级商户KYB代进件单取消接口 /// /// /// /// /// public async Task Handle(EnterpriseWalletExpandindirectOrderCancelCommand request, CancellationToken cancellationToken) { var logier = JwtUtils.GetCurrentLogier(); if (logier.Type == EnumUserType.Enterprise) { request.EnterpriseId = logier.EnterpriseId; } if (request.EnterpriseId == null) throw Oops.Oh(EnumErrorCodeType.s400, "请填写企业Id"); var entity = await rep.AsQueryable() .OrderByDescending(it => it.CreatedTime) .Where(it => it.Wallet.EnterpriseId == request.EnterpriseId) .FirstOrDefaultAsync(); if (entity == null) throw Oops.Oh(EnumErrorCodeType.s404, "代进件单"); if (entity.OrderStatus != EnumEnterpriseWalletExpandindirectOrderStatus.VALID) throw Oops.Oh(EnumErrorCodeType.s510, "未生效"); var result = alipayUtils.FundExpandindirectOrderCancel(new AlipayFundExpandindirectOrderCancelModel { BizScene = entity.BizScene, OrderId = entity.OrderId, OutBizNo = entity.OutBizNo, ProductCode = entity.ProductCode, }); if (result.IsError) throw Oops.Oh(EnumErrorCodeType.s510, result.Msg); entity.Status = result.Status; entity.OrderStatus = result.Status.ToEnum("未识别的状态")!.Value; await rep.UpdateAsync(entity); return entity.Id; } /// /// 支付宝资金二级商户KYB代进件状态通知 /// /// /// /// public Task Handle(AlipayFundExpandindirectCreateNotifyCommand request, CancellationToken cancellationToken) { return Task.FromResult(Guid.Empty); } } }