using Aop.Api.Domain;
|
using Azure;
|
using FlexJobApi.Core;
|
using Furion.DatabaseAccessor;
|
using Furion.DistributedIDGenerator;
|
using Furion.FriendlyException;
|
using MediatR;
|
using Microsoft.EntityFrameworkCore;
|
using System;
|
using System.Collections.Generic;
|
using System.Linq;
|
using System.Text;
|
using System.Threading.Tasks;
|
|
namespace FlexJobApi.UserServer.Application
|
{
|
/// <summary>
|
/// 企业钱包命令处理器
|
/// </summary>
|
public class EnterpriseWalletCommandHandler(
|
IRepository<EnterpriseWallet> rep,
|
IRepository<Enterprise> repEnterprise,
|
AlipayUtils alipayUtils
|
) :
|
IRequestHandler<OpenEnterpriseWalletCommand, OpenEnterpriseWalletCommandResult>,
|
IRequestHandler<CloseEnterpriseWalletCommand, Guid>
|
{
|
private readonly IRepository<EnterpriseWallet> rep = rep;
|
private readonly IRepository<Enterprise> repEnterprise = repEnterprise;
|
private readonly AlipayUtils alipayUtils = alipayUtils;
|
|
/// <summary>
|
/// 开通企业钱包
|
/// </summary>
|
/// <param name="request"></param>
|
/// <param name="cancellationToken"></param>
|
/// <returns></returns>
|
public async Task<OpenEnterpriseWalletCommandResult> Handle(OpenEnterpriseWalletCommand request, CancellationToken cancellationToken)
|
{
|
var logier = JwtUtils.GetCurrentLogier();
|
var enterprise = await repEnterprise.AsQueryable().AsNoTracking()
|
.Where(it => it.Id == logier.EnterpriseId)
|
.FirstOrDefaultAsync();
|
if (enterprise == null) throw Oops.Oh(EnumErrorCodeType.s404, "企业");
|
if (!enterprise.IsReal) throw Oops.Oh(EnumErrorCodeType.s510, "请先实名");
|
var entity = await rep.AsQueryable()
|
.Where(it => it.EnterpriseId == logier.EnterpriseId && it.Access == request.Access)
|
.FirstOrDefaultAsync();
|
if (entity == null)
|
{
|
entity = new EnterpriseWallet();
|
entity.EnterpriseId = logier.EnterpriseId!.Value;
|
entity.Access = EnumEnterpriseWalletAccess.Alipay;
|
entity.PersonalProductCode = "FUND_SAFT_SIGN_WITHHOLDING_P";
|
entity.SignScene = "INDUSTRY|SATF_ACC";
|
entity.ThirdPartyType = "PARTNER";
|
entity.ProductCode = "FUND_SAFT_SIGN_WITHHOLDING";
|
entity.SignStatus = EnumEnterpriseWalletSignStatus.Apply;
|
await SetCode(entity);
|
await rep.InsertAsync(entity);
|
}
|
else
|
{
|
if (entity.SignStatus == EnumEnterpriseWalletSignStatus.Normal) throw Oops.Oh(EnumErrorCodeType.s510, "已签约");
|
entity.SignStatus = EnumEnterpriseWalletSignStatus.Apply;
|
await rep.UpdateAsync(entity);
|
}
|
var model = new AlipayUserAgreementPageSignModel();
|
model.ExternalLogonId = enterprise.Id.ToString();
|
model.PersonalProductCode = entity.PersonalProductCode;
|
model.SignScene = entity.SignScene;
|
model.ExternalAgreementNo = entity.Code;
|
model.ThirdPartyType = entity.ThirdPartyType;
|
model.ProductCode = entity.ProductCode;
|
model.AccessParams = new AccessParams
|
{
|
Channel = "QRCODE"
|
};
|
var response = alipayUtils.UserAgreementPageSign(model, $"/api/user/enterpriseWallet/getAlipayUserAgreementPageSignNotify");
|
if (response.IsError) throw Oops.Oh(EnumErrorCodeType.s510, response.SubMsg ?? response.Msg);
|
var result = new OpenEnterpriseWalletCommandResult();
|
result.SignUrl = response.PageRedirectionData;
|
return result;
|
}
|
|
/// <summary>
|
/// 关闭企业钱包
|
/// </summary>
|
/// <param name="request"></param>
|
/// <param name="cancellationToken"></param>
|
/// <returns></returns>
|
public async Task<Guid> Handle(CloseEnterpriseWalletCommand request, CancellationToken cancellationToken)
|
{
|
var logier = JwtUtils.GetCurrentLogier();
|
var entity = await rep.AsQueryable()
|
.Where(it => it.EnterpriseId == logier.EnterpriseId && it.Access == request.Access)
|
.FirstOrDefaultAsync();
|
if (entity == null) throw Oops.Oh(EnumErrorCodeType.s404, "企业钱包");
|
if (entity.SignStatus != EnumEnterpriseWalletSignStatus.Normal) throw Oops.Oh(EnumErrorCodeType.s510, "未签约");
|
var response = alipayUtils.UserAgreementUnsign(new AlipayUserAgreementUnsignModel
|
{
|
ExternalAgreementNo = entity.Code,
|
PersonalProductCode = entity.PersonalProductCode,
|
SignScene = entity.SignScene
|
});
|
if (response.IsError) throw Oops.Oh(EnumErrorCodeType.s510, response.SubMsg ?? response.Msg);
|
entity.SignStatus = EnumEnterpriseWalletSignStatus.Stop;
|
return entity.Id;
|
}
|
|
private async Task SetCode(EnterpriseWallet entity)
|
{
|
entity.Code = $"{DateTime.Now:yyyyMMddHHmmss}{new Random(IDGen.NextID().GetHashCode()).Next(1000, 9999)}";
|
var exist = await rep.AsQueryable().AsNoTracking()
|
.AnyAsync(it => it.Code == entity.Code);
|
if (exist)
|
{
|
await SetCode(entity);
|
}
|
}
|
}
|
}
|