|
@@ -853,17 +853,10 @@ class AccountService:
|
|
|
全量同步转账状态
|
|
全量同步转账状态
|
|
|
自动查询所有卡在 DEALING 的转账,调用支付宝核实状态
|
|
自动查询所有卡在 DEALING 的转账,调用支付宝核实状态
|
|
|
"""
|
|
"""
|
|
|
- from app.plugin.module_payment.account.crud import TransferCRUD
|
|
|
|
|
- from app.plugin.module_payment.enterprise.service import EnterpriseService
|
|
|
|
|
- from sqlalchemy import update as sa_update
|
|
|
|
|
|
|
+ from sqlalchemy import select, update as sa_update
|
|
|
from app.plugin.module_payment.account.model import TransferModel
|
|
from app.plugin.module_payment.account.model import TransferModel
|
|
|
from app.plugin.module_payment.account.enums import TransferStatusEnum
|
|
from app.plugin.module_payment.account.enums import TransferStatusEnum
|
|
|
|
|
|
|
|
- # 获取所有企业
|
|
|
|
|
- enterprises = await EnterpriseService.all_service(auth=auth)
|
|
|
|
|
- if not enterprises:
|
|
|
|
|
- return {"total": 0, "synced": 0, "failed": 0, "details": []}
|
|
|
|
|
-
|
|
|
|
|
try:
|
|
try:
|
|
|
from alipay.aop.api.request.AlipayCommerceEcTransOrderQueryRequest import (
|
|
from alipay.aop.api.request.AlipayCommerceEcTransOrderQueryRequest import (
|
|
|
AlipayCommerceEcTransOrderQueryRequest,
|
|
AlipayCommerceEcTransOrderQueryRequest,
|
|
@@ -878,79 +871,78 @@ class AccountService:
|
|
|
except ImportError:
|
|
except ImportError:
|
|
|
raise CustomException(msg="支付宝SDK未正确安装")
|
|
raise CustomException(msg="支付宝SDK未正确安装")
|
|
|
|
|
|
|
|
- crud = TransferCRUD(auth)
|
|
|
|
|
client = AlipayClient.get_client()
|
|
client = AlipayClient.get_client()
|
|
|
|
|
|
|
|
|
|
+ # 直接查库,绕过 CRUD 权限过滤,获取所有 DEALING 转账
|
|
|
|
|
+ stmt = select(TransferModel).where(
|
|
|
|
|
+ TransferModel.status == TransferStatusEnum.DEALING.value,
|
|
|
|
|
+ TransferModel.out_biz_no.isnot(None),
|
|
|
|
|
+ )
|
|
|
|
|
+ stmt = stmt.order_by(TransferModel.id.asc())
|
|
|
|
|
+ result = await auth.db.execute(stmt)
|
|
|
|
|
+ dealing_transfers = result.scalars().all()
|
|
|
|
|
+
|
|
|
synced = 0
|
|
synced = 0
|
|
|
failed = 0
|
|
failed = 0
|
|
|
- total = 0
|
|
|
|
|
|
|
+ total = len(dealing_transfers)
|
|
|
details = []
|
|
details = []
|
|
|
|
|
|
|
|
- for enterprise in enterprises:
|
|
|
|
|
- eid = enterprise.enterprise_id
|
|
|
|
|
- dealing_transfers = await crud.list(search={
|
|
|
|
|
- "status": TransferStatusEnum.DEALING.value,
|
|
|
|
|
- "enterprise_id": eid,
|
|
|
|
|
- })
|
|
|
|
|
- if not dealing_transfers:
|
|
|
|
|
|
|
+ for transfer in dealing_transfers:
|
|
|
|
|
+ out_biz_no = transfer.out_biz_no
|
|
|
|
|
+ eid = transfer.enterprise_id
|
|
|
|
|
+ if not out_biz_no or not eid:
|
|
|
continue
|
|
continue
|
|
|
|
|
|
|
|
- for transfer in dealing_transfers:
|
|
|
|
|
- total += 1
|
|
|
|
|
- out_biz_no = transfer.out_biz_no
|
|
|
|
|
- if not out_biz_no:
|
|
|
|
|
|
|
+ try:
|
|
|
|
|
+ query_model = AlipayCommerceEcTransOrderQueryModel()
|
|
|
|
|
+ query_model.out_biz_no = out_biz_no
|
|
|
|
|
+ query_model.enterprise_id = eid
|
|
|
|
|
+
|
|
|
|
|
+ request = AlipayCommerceEcTransOrderQueryRequest()
|
|
|
|
|
+ request.biz_model = query_model
|
|
|
|
|
+
|
|
|
|
|
+ response = client.execute(request)
|
|
|
|
|
+ if not response:
|
|
|
|
|
+ failed += 1
|
|
|
|
|
+ details.append({"out_biz_no": out_biz_no, "status": "DEALING", "error": "无响应"})
|
|
|
continue
|
|
continue
|
|
|
|
|
|
|
|
- try:
|
|
|
|
|
- query_model = AlipayCommerceEcTransOrderQueryModel()
|
|
|
|
|
- query_model.out_biz_no = out_biz_no
|
|
|
|
|
- query_model.enterprise_id = eid
|
|
|
|
|
-
|
|
|
|
|
- request = AlipayCommerceEcTransOrderQueryRequest()
|
|
|
|
|
- request.biz_model = query_model
|
|
|
|
|
-
|
|
|
|
|
- response = client.execute(request)
|
|
|
|
|
- if not response:
|
|
|
|
|
- failed += 1
|
|
|
|
|
- details.append({"out_biz_no": out_biz_no, "status": "DEALING", "error": "无响应"})
|
|
|
|
|
- continue
|
|
|
|
|
-
|
|
|
|
|
- query_result = AlipayCommerceEcTransOrderQueryResponse()
|
|
|
|
|
- query_result.parse_response_content(response)
|
|
|
|
|
-
|
|
|
|
|
- if not query_result.is_success():
|
|
|
|
|
- failed += 1
|
|
|
|
|
- err_msg = query_result.sub_msg or query_result.msg
|
|
|
|
|
- details.append({"out_biz_no": out_biz_no, "status": "DEALING", "error": err_msg})
|
|
|
|
|
- log.warning(f"全量同步 - 查询失败: out_biz_no={out_biz_no}, error={err_msg}")
|
|
|
|
|
- continue
|
|
|
|
|
-
|
|
|
|
|
- alipay_status = getattr(query_result, 'status', None)
|
|
|
|
|
- if alipay_status and alipay_status != TransferStatusEnum.DEALING.value:
|
|
|
|
|
- update_data = {"status": alipay_status}
|
|
|
|
|
- order_no = getattr(query_result, 'order_no', None)
|
|
|
|
|
- fund_order_id = getattr(query_result, 'pay_fund_order_id', None)
|
|
|
|
|
- if order_no:
|
|
|
|
|
- update_data["order_no"] = order_no
|
|
|
|
|
- if fund_order_id:
|
|
|
|
|
- update_data["fund_order_id"] = fund_order_id
|
|
|
|
|
-
|
|
|
|
|
- upd = sa_update(TransferModel).where(
|
|
|
|
|
- TransferModel.out_biz_no == out_biz_no
|
|
|
|
|
- ).values(**update_data)
|
|
|
|
|
- await auth.db.execute(upd)
|
|
|
|
|
- synced += 1
|
|
|
|
|
- details.append({"out_biz_no": out_biz_no, "status": alipay_status, "synced": True})
|
|
|
|
|
- log.info(f"全量同步 - 已更新: out_biz_no={out_biz_no}, old=DEALING, new={alipay_status}")
|
|
|
|
|
- else:
|
|
|
|
|
- details.append({"out_biz_no": out_biz_no, "status": "DEALING", "error": "支付宝侧仍为处理中"})
|
|
|
|
|
- except Exception as e:
|
|
|
|
|
|
|
+ query_result = AlipayCommerceEcTransOrderQueryResponse()
|
|
|
|
|
+ query_result.parse_response_content(response)
|
|
|
|
|
+
|
|
|
|
|
+ if not query_result.is_success():
|
|
|
failed += 1
|
|
failed += 1
|
|
|
- details.append({"out_biz_no": out_biz_no, "status": "DEALING", "error": str(e)})
|
|
|
|
|
- log.warning(f"全量同步 - 异常: out_biz_no={out_biz_no}, error={e}")
|
|
|
|
|
|
|
+ err_msg = query_result.sub_msg or query_result.msg
|
|
|
|
|
+ details.append({"out_biz_no": out_biz_no, "status": "DEALING", "error": err_msg})
|
|
|
|
|
+ log.warning(f"全量同步 - 查询失败: out_biz_no={out_biz_no}, error={err_msg}")
|
|
|
|
|
+ continue
|
|
|
|
|
|
|
|
- if synced > 0 or failed > 0:
|
|
|
|
|
- await auth.db.flush()
|
|
|
|
|
|
|
+ alipay_status = getattr(query_result, 'status', None)
|
|
|
|
|
+ if alipay_status and alipay_status != TransferStatusEnum.DEALING.value:
|
|
|
|
|
+ update_data = {"status": alipay_status}
|
|
|
|
|
+ order_no = getattr(query_result, 'order_no', None)
|
|
|
|
|
+ fund_order_id = getattr(query_result, 'pay_fund_order_id', None)
|
|
|
|
|
+ if order_no:
|
|
|
|
|
+ update_data["order_no"] = order_no
|
|
|
|
|
+ if fund_order_id:
|
|
|
|
|
+ update_data["fund_order_id"] = fund_order_id
|
|
|
|
|
+
|
|
|
|
|
+ upd = sa_update(TransferModel).where(
|
|
|
|
|
+ TransferModel.out_biz_no == out_biz_no
|
|
|
|
|
+ ).values(**update_data)
|
|
|
|
|
+ await auth.db.execute(upd)
|
|
|
|
|
+ synced += 1
|
|
|
|
|
+ details.append({"out_biz_no": out_biz_no, "status": alipay_status, "synced": True})
|
|
|
|
|
+ log.info(f"全量同步 - 已更新: out_biz_no={out_biz_no}, old=DEALING, new={alipay_status}")
|
|
|
|
|
+ else:
|
|
|
|
|
+ details.append({"out_biz_no": out_biz_no, "status": "DEALING", "error": "支付宝侧仍为处理中"})
|
|
|
|
|
+ except Exception as e:
|
|
|
|
|
+ failed += 1
|
|
|
|
|
+ details.append({"out_biz_no": out_biz_no, "status": "DEALING", "error": str(e)})
|
|
|
|
|
+ log.warning(f"全量同步 - 异常: out_biz_no={out_biz_no}, error={e}")
|
|
|
|
|
+
|
|
|
|
|
+ if synced > 0 or failed > 0:
|
|
|
|
|
+ await auth.db.flush()
|
|
|
|
|
|
|
|
return {
|
|
return {
|
|
|
"total": total,
|
|
"total": total,
|