using Aop.Api.Domain;
|
using Azure;
|
using FlexJobApi.Core;
|
using Furion.DatabaseAccessor;
|
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
|
{
|
/// <summary>
|
/// 支付宝资金二级商户KYB代进件
|
/// </summary>
|
public class AlipayFundExpandindirectCommandHandler(
|
IOptions<AlipayOptions> options,
|
AlipayUtils alipayUtils,
|
IHttpRemoteService httpRemoteService,
|
IRepository<AlipayFundExpandindirectOrder> rep
|
) :
|
IRequestHandler<AlipayFundExpandindirectCreateCommand, Guid>,
|
IRequestHandler<AlipayFundExpandindirectImageUploadCommand, string>,
|
IRequestHandler<AlipayFundExpandindirectOrderCancelCommand, Guid>,
|
IRequestHandler<AlipayFundExpandindirectCreateNotifyCommand, Guid>
|
{
|
private readonly IOptions<AlipayOptions> options = options;
|
private readonly AlipayUtils alipayUtils = alipayUtils;
|
private readonly IHttpRemoteService httpRemoteService = httpRemoteService;
|
private readonly IRepository<AlipayFundExpandindirectOrder> rep = rep;
|
|
/// <summary>
|
/// 支付宝资金二级商户KYB代进件
|
/// </summary>
|
/// <param name="request"></param>
|
/// <param name="cancellationToken"></param>
|
/// <returns></returns>
|
[UnitOfWork(false)]
|
public async Task<Guid> Handle(AlipayFundExpandindirectCreateCommand request, CancellationToken cancellationToken)
|
{
|
AlipayFundExpandindirectOrder? entity;
|
if (request.Id.HasValue)
|
{
|
entity = await rep.AsQueryable()
|
.Where(it => it.Id == request.Id)
|
.FirstOrDefaultAsync(cancellationToken);
|
if (entity == null) throw Oops.Oh(EnumErrorCodeType.s404, "代进件单");
|
}
|
else
|
{
|
entity = new AlipayFundExpandindirectOrder();
|
}
|
entity.Sites = request.SitesInfo.ToJson();
|
request.Adapt(entity);
|
if (request.Id.HasValue)
|
{
|
await rep.UpdateNowAsync(entity);
|
}
|
else
|
{
|
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, $"{options.Value.NotifyUrl}/api/user/enterpriseWallet/alipayFundExpandindirectCreateNotify");
|
if (result.IsError)
|
{
|
entity.RiskReviewRemark = result.Msg;
|
}
|
entity.TaskFinishTime = result.TaskFinishTime;
|
entity.OrderId = result.OrderId;
|
entity.Status = result.Status;
|
await rep.UpdateNowAsync(entity);
|
if (result.IsError)
|
{
|
throw Oops.Oh(EnumErrorCodeType.s510, result.Msg);
|
}
|
return entity.Id;
|
}
|
|
/// <summary>
|
/// 支付宝资金二级商户KYB代进件文件上传接口
|
/// </summary>
|
/// <param name="request"></param>
|
/// <param name="cancellationToken"></param>
|
/// <returns></returns>
|
public async Task<string> Handle(AlipayFundExpandindirectImageUploadCommand request, CancellationToken cancellationToken)
|
{
|
using (var memoryStream = new MemoryStream())
|
{
|
await request.File.CopyToAsync(memoryStream);
|
var bytes = memoryStream.ToArray();
|
var result = alipayUtils.FundExpandindirectImageUpload(new Aop.Api.Request.AlipayFundExpandindirectImageUploadRequest
|
{
|
ProductCode = request.ProductCode,
|
BizScene = request.BizScene,
|
ImageType = Path.GetExtension(request.File.FileName),
|
ImageContent = new Aop.Api.Util.FileItem(request.File.FileName, bytes)
|
});
|
if (result.IsError) throw Oops.Oh(EnumErrorCodeType.s510, result.Msg);
|
return result.ImageId;
|
}
|
}
|
|
/// <summary>
|
/// 支付宝资金二级商户KYB代进件单取消接口
|
/// </summary>
|
/// <param name="request"></param>
|
/// <param name="cancellationToken"></param>
|
/// <returns></returns>
|
/// <exception cref="NotImplementedException"></exception>
|
public async Task<Guid> Handle(AlipayFundExpandindirectOrderCancelCommand request, CancellationToken cancellationToken)
|
{
|
var entity = await rep.AsQueryable()
|
.Where(it => it.Id == request.Id)
|
.FirstOrDefaultAsync();
|
if (entity == null) throw Oops.Oh(EnumErrorCodeType.s404, "代进件单");
|
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);
|
result.Status = result.Status;
|
await rep.UpdateAsync(entity);
|
return entity.Id;
|
}
|
|
/// <summary>
|
/// 支付宝资金二级商户KYB代进件状态通知
|
/// </summary>
|
/// <param name="request"></param>
|
/// <param name="cancellationToken"></param>
|
/// <returns></returns>
|
public Task<Guid> Handle(AlipayFundExpandindirectCreateNotifyCommand request, CancellationToken cancellationToken)
|
{
|
return Task.FromResult(Guid.Empty);
|
}
|
}
|
}
|