using FlexJobApi.Core;
using Furion.DatabaseAccessor;
using Furion.FriendlyException;
using Mapster;
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
{
///
/// 企业钱包查询处理器
///
public class EnterpriseWalletQueryHandler(
IRepository rep,
AlipayUtils alipayUtils
) :
IRequestHandler
{
private readonly IRepository rep = rep;
private readonly AlipayUtils alipayUtils = alipayUtils;
///
/// 查询企业钱包详情
///
///
///
///
public async Task Handle(GetEnterpriseWalletQuery 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.Apply)
{
var response = alipayUtils.UserAgreementQuery(new Aop.Api.Domain.AlipayUserAgreementQueryModel
{
PersonalProductCode = entity.PersonalProductCode,
SignScene = entity.SignScene,
ExternalAgreementNo = entity.Code,
ThirdPartyType = entity.ThirdPartyType,
});
if (response.IsError) throw Oops.Oh(EnumErrorCodeType.s510, response.SubMsg ?? response.Msg);
entity.AgreementNo = response.AgreementNo;
entity.SignTime = response.SignTime.ToDateTime();
entity.ValidTime = response.ValidTime.ToDateTime();
entity.InvalidTime = response.InvalidTime.ToDateTime();
entity.SignStatus = response.Status == "TEMP"
? EnumEnterpriseWalletSignStatus.Apply
: response.Status == "NORMAL"
? EnumEnterpriseWalletSignStatus.Normal
: response.Status == "STOP"
? EnumEnterpriseWalletSignStatus.Stop
: throw Oops.Oh(EnumErrorCodeType.s510, "状态异常");
entity.PricipalType = response.PricipalType;
entity.AlipayLogonId = response.AlipayLogonId;
entity.PrincipalId = response.PrincipalId;
entity.PrincipalOpenId = response.PrincipalOpenId;
entity.ZmOpenId = response.ZmOpenId;
entity.CreditAuthMode = response.CreditAuthMode;
await rep.UpdateAsync(entity);
}
var model = entity.Adapt();
return model;
}
}
}