From 5c45bc53e2c18e45130c21048df4af4896455c6d Mon Sep 17 00:00:00 2001
From: sunpengfei <i@angelzzz.com>
Date: 星期五, 12 九月 2025 15:58:48 +0800
Subject: [PATCH] feat:开发

---
 FlexJobApi.UserServer.Application/EnterpriseWallets/Queries/EnterpriseWalletQueryHandler.cs |  146 +++++++++++++++++++++++++++++++++++++-----------
 1 files changed, 113 insertions(+), 33 deletions(-)

diff --git a/FlexJobApi.UserServer.Application/EnterpriseWallets/Queries/EnterpriseWalletQueryHandler.cs b/FlexJobApi.UserServer.Application/EnterpriseWallets/Queries/EnterpriseWalletQueryHandler.cs
index 2202f1f..b9dd551 100644
--- a/FlexJobApi.UserServer.Application/EnterpriseWallets/Queries/EnterpriseWalletQueryHandler.cs
+++ b/FlexJobApi.UserServer.Application/EnterpriseWallets/Queries/EnterpriseWalletQueryHandler.cs
@@ -2,6 +2,7 @@
 using Azure;
 using FlexJobApi.Core;
 using Furion.DatabaseAccessor;
+using Furion.DistributedIDGenerator;
 using Furion.FriendlyException;
 using Mapster;
 using MediatR;
@@ -57,11 +58,24 @@
                 .FirstOrDefaultAsync();
             if (entity == null)
             {
+                entity = new EnterpriseWallet();
+                entity.EnterpriseId = request.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.Wait;
+                await SetCode(entity);
+                await rep.InsertAsync(entity);
+
                 return new GetEnterpriseWalletQueryResult
                 {
+                    Id = entity.Id,
+                    Code = entity.Code,
                     Access = request.Access,
                     SignStatus = EnumEnterpriseWalletSignStatus.Wait,
-                    ExpandindirectOrderStatus = EnumEnterpriseWalletExpandindirectOrderStatus.Wait
+                    ExpandindirectOrderStatus = EnumEnterpriseWalletExpandindirectOrderStatus.Wait,
                 };
             }
             var update = false;
@@ -97,6 +111,12 @@
                         entity.CreditAuthMode = response.CreditAuthMode;
                         update = true;
                     }
+                    else
+                    {
+                        entity.ErrorCode = response.Code;
+                        entity.FailReason = response.SubMsg ?? response.Msg;
+                        update = true;
+                    }
                 }
 
                 if (entity.SignStatus == EnumEnterpriseWalletSignStatus.Normal && entity.AccountBookStatus != EnumEnterpriseWalletAccountBookStatus.Normal)
@@ -112,32 +132,28 @@
                             cert_no = enterprise.SocietyCreditCode
                         }.ToJson()
                     });
-                    if (response.IsError) throw Oops.Oh(EnumErrorCodeType.s510, response.SubMsg ?? response.Msg);
-                    entity.AccountBookId = response.AccountBookId;
-                    entity.BankAccName = response.ExtCardInfo.BankAccName;
-                    entity.CardBank = response.ExtCardInfo.CardBank;
-                    entity.CardBranch = response.ExtCardInfo.CardBranch;
-                    entity.CardDeposit = response.ExtCardInfo.CardDeposit;
-                    entity.CardLocation = response.ExtCardInfo.CardLocation;
-                    entity.CardNo = response.ExtCardInfo.CardNo;
-                    entity.AccountBookStatus = response.ExtCardInfo.Status == "A"
-                        ? EnumEnterpriseWalletAccountBookStatus.Normal
-                        : EnumEnterpriseWalletAccountBookStatus.Exception;
-                    update = true;
+                    if (!response.IsError)
+                    {
+                        entity.AccountBookId = response.AccountBookId;
+                        entity.BankAccName = response.ExtCardInfo.BankAccName;
+                        entity.CardBank = response.ExtCardInfo.CardBank;
+                        entity.CardBranch = response.ExtCardInfo.CardBranch;
+                        entity.CardDeposit = response.ExtCardInfo.CardDeposit;
+                        entity.CardLocation = response.ExtCardInfo.CardLocation;
+                        entity.CardNo = response.ExtCardInfo.CardNo;
+                        entity.AccountBookStatus = response.ExtCardInfo.Status == "A"
+                            ? EnumEnterpriseWalletAccountBookStatus.Normal
+                            : EnumEnterpriseWalletAccountBookStatus.Exception;
+                        update = true;
+                    }
+                    else
+                    {
+                        entity.ErrorCode = response.Code;
+                        entity.FailReason = response.SubMsg ?? response.Msg;
+                        update = true;
+                    }
                 }
 
-                if (entity.AccountBookStatus == EnumEnterpriseWalletAccountBookStatus.Normal)
-                {
-                    var response = alipayUtils.FundAccountbookQuery(new AlipayFundAccountbookQueryModel
-                    {
-                        AccountBookId = entity.AccountBookId,
-                        SceneCode = "SATF_FUND_BOOK",
-                        MerchantUserId = entity.Code,
-                    });
-                    if (response.IsError) throw Oops.Oh(EnumErrorCodeType.s510, response.SubMsg ?? response.Msg);
-                    entity.Balance = response.AvailableAmount.ToDecimal() ?? 0;
-                    update = true;
-                }
             }
 
             if (update)
@@ -147,19 +163,79 @@
 
             var model = entity.Adapt<GetEnterpriseWalletQueryResult>();
 
-            var expandindirectOrder = await repEnterpriseWalletExpandindirectOrder.AsQueryable().AsNoTracking()
+            var expandindirectOrder = await repEnterpriseWalletExpandindirectOrder.AsQueryable()
+                .Include(it => it.Files)
                 .OrderByDescending(it => it.CreatedTime)
                 .Where(it => it.WalletId == model.Id)
-                .Select(it => new
-                {
-                    it.OrderStatus
-                })
                 .FirstOrDefaultAsync();
-            model.ExpandindirectOrderStatus = expandindirectOrder == null
-                ? EnumEnterpriseWalletExpandindirectOrderStatus.Wait
-                : expandindirectOrder.OrderStatus;
+            if (expandindirectOrder != null)
+            {
+                if (expandindirectOrder.OrderStatus == EnumEnterpriseWalletExpandindirectOrderStatus.PROCESSING)
+                {
+                    var response = alipayUtils.FundExpandindirectOrderQuery(new Aop.Api.Domain.AlipayFundExpandindirectOrderQueryModel
+                    {
+                        BizScene = expandindirectOrder.BizScene,
+                        OrderId = expandindirectOrder.OrderId,
+                        OutBizNo = expandindirectOrder.OutBizNo,
+                        ProductCode = expandindirectOrder.ProductCode,
+                    });
+                    if (!response.IsError)
+                    {
+                        expandindirectOrder.Status = response.Status;
+                        expandindirectOrder.OrderStatus = expandindirectOrder.Status.ToEnum<EnumEnterpriseWalletExpandindirectOrderStatus>("鏈瘑鍒殑鐘舵��")!.Value;
+                        expandindirectOrder.TaskFinishTime = response.TaskFinishTime.ToDateTime();
+                        expandindirectOrder.RiskReviewRemark = response.RiskReviewRemark;
+                    }
+                    else
+                    {
+                        expandindirectOrder.RiskReviewRemark = response.SubMsg ?? response.Msg;
+                    }
+                    await repEnterpriseWalletExpandindirectOrder.UpdateNowAsync(expandindirectOrder);
+                }
+
+                model.Name = expandindirectOrder.Name;
+                model.Identity = expandindirectOrder.Identity;
+                model.Scene = expandindirectOrder.Scene;
+                model.SceneDirections = expandindirectOrder.SceneDirections;
+                model.TaskFinishTime = expandindirectOrder.TaskFinishTime;
+                model.SitesInfo = expandindirectOrder.Sites?.JsonTo<GetEnterpriseWalletExpandindirectOrderQueryResultSites>();
+                model.Files = expandindirectOrder.Files?.Adapt<List<GetEnterpriseWalletExpandindirectOrderQueryResultFile>>() ?? new List<GetEnterpriseWalletExpandindirectOrderQueryResultFile>();
+                model.ExpandindirectOrderStatus = expandindirectOrder.OrderStatus;
+            }
+            else
+            {
+                model.ExpandindirectOrderStatus = EnumEnterpriseWalletExpandindirectOrderStatus.Wait;
+            }
+
+            if (entity.AccountBookStatus == EnumEnterpriseWalletAccountBookStatus.Normal)
+            {
+                var response = alipayUtils.FundAccountbookQuery(new AlipayFundAccountbookQueryModel
+                {
+                    AccountBookId = entity.AccountBookId,
+                    SceneCode = "SATF_FUND_BOOK",
+                    MerchantUserId = entity.Code,
+                    ExtInfo = new
+                    {
+                        agreement_no = entity.AgreementNo
+                    }.ToJson()
+                });
+                if (response.IsError) throw Oops.Oh(EnumErrorCodeType.s510, response.SubMsg ?? response.Msg);
+                entity.Balance = response.AvailableAmount.ToDecimal() ?? 0;
+                await rep.UpdateNowAsync(entity);
+            }
 
             return model;
+        }
+
+        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);
+            }
         }
 
         /// <summary>
@@ -225,6 +301,10 @@
                 AccountBookId = entity.AccountBookId,
                 SceneCode = "SATF_FUND_BOOK",
                 MerchantUserId = entity.Code,
+                ExtInfo = new
+                {
+                    agreement_no = entity.AgreementNo
+                }.ToJson()
             });
             if (response.IsError) throw Oops.Oh(EnumErrorCodeType.s510, response.SubMsg ?? response.Msg);
             entity.Balance = response.AvailableAmount.ToDecimal() ?? 0;

--
Gitblit v1.9.1