Parcourir la source

fix(account): sync-all 映射 JointAccountBillDetail 字段 bill_no/biz_out_no/amount

alphah il y a 2 semaines
Parent
commit
7ef27afe43
1 fichiers modifiés avec 42 ajouts et 55 suppressions
  1. 42 55
      backend/app/plugin/module_payment/account/service.py

+ 42 - 55
backend/app/plugin/module_payment/account/service.py

@@ -966,30 +966,27 @@ class AccountService:
 
                     for c in bill_list:
                         total_records += 1
-                        pay_no = getattr(c, 'pay_no', None)
-                        consume_type = getattr(c, 'consume_type', None)
-                        if not pay_no:
+                        bill_no = getattr(c, 'bill_no', None)
+                        biz_out_no = getattr(c, 'biz_out_no', None)  # 外部业务号 = out_biz_no(转账时存在)
+                        if not bill_no:
                             continue
 
                         # — 1) 落库 pay_bill(有则更新,无则新增) —
                         bill_data = {
-                            "pay_no": pay_no,
+                            "pay_no": bill_no,
                             "enterprise_id": getattr(c, 'enterprise_id', eid),
-                            "account_id": getattr(c, 'account_id', ''),
-                            "employee_id": getattr(c, 'employee_id', ''),
-                            "consume_type": consume_type or '',
-                            "consume_amount": Decimal(str(getattr(c, 'consume_amount', 0))),
-                            "gmt_biz_create": _parse_dt(getattr(c, 'gmt_biz_create', None)),
-                            "gmt_recieve_pay": _parse_dt(getattr(c, 'gmt_recieve_pay', None)),
-                            "peer_pay_amount": Decimal(str(getattr(c, 'peer_pay_amount', 0))) if getattr(c, 'peer_pay_amount', None) else None,
-                            "notify_reason": getattr(c, 'notify_reason', 'SYNC'),
-                            "notify_msg": getattr(c, 'notify_msg', '全量同步'),
-                            "related_pay_no": getattr(c, 'related_pay_no', None),
+                            "account_id": "",
+                            "employee_id": "",
+                            "consume_type": "TRANSFER" if biz_out_no else "CONSUME",
+                            "consume_amount": Decimal(str(getattr(c, 'amount', 0))),
+                            "gmt_biz_create": _parse_dt(getattr(c, 'biz_date', None)),
+                            "notify_reason": "SYNC",
+                            "notify_msg": getattr(c, 'title', '全量同步'),
                             "status": "PROCESSED",
                         }
 
                         # 检查 pay_bill 是否已存在
-                        bill_stmt = select(PayBillModel).where(PayBillModel.pay_no == pay_no)
+                        bill_stmt = select(PayBillModel).where(PayBillModel.pay_no == bill_no)
                         bill_exist = (await auth.db.execute(bill_stmt)).scalar_one_or_none()
                         if bill_exist:
                             for k, v in bill_data.items():
@@ -1000,50 +997,40 @@ class AccountService:
                             await auth.db.execute(ins)
                         bill_upserted += 1
 
-                        # — 2) 如果是转账,同步 pay_transfer —
-                        if consume_type == "TRANSFER":
-                            out_biz_no = getattr(c, 'out_biz_no', None)
-                            if not out_biz_no:
-                                # 尝试从 ext_infos 取
-                                ext = getattr(c, 'ext_infos', None)
-                                if isinstance(ext, dict):
-                                    out_biz_no = ext.get('out_biz_no')
-
-                            if out_biz_no:
-                                # 查 local
-                                tf_stmt = select(TransferModel).where(TransferModel.out_biz_no == out_biz_no)
-                                tf_exist = (await auth.db.execute(tf_stmt)).scalar_one_or_none()
-                                if not tf_exist:
-                                    # 新增转账记录
-                                    ins_tf = sa_insert(TransferModel).values(
-                                        out_biz_no=out_biz_no,
-                                        enterprise_id=eid,
-                                        amount=Decimal(str(getattr(c, 'consume_amount', 0))),
-                                        status=TransferStatusEnum.DEALING.value,
-                                    )
-                                    await auth.db.execute(ins_tf)
-                                    transfer_upserted += 1
-                                    details.append({"pay_no": pay_no, "out_biz_no": out_biz_no, "type": "TRANSFER", "action": "insert"})
-
-                                    # 新记录尝试查转账详情
+                        # — 2) 如果有 biz_out_no,说明是转账,同步 pay_transfer —
+                        if biz_out_no:
+                            # 查 local
+                            tf_stmt = select(TransferModel).where(TransferModel.out_biz_no == biz_out_no)
+                            tf_exist = (await auth.db.execute(tf_stmt)).scalar_one_or_none()
+                            if not tf_exist:
+                                # 新增转账记录
+                                ins_tf = sa_insert(TransferModel).values(
+                                    out_biz_no=biz_out_no,
+                                    enterprise_id=eid,
+                                    amount=Decimal(str(getattr(c, 'amount', 0))),
+                                    status=TransferStatusEnum.DEALING.value,
+                                )
+                                await auth.db.execute(ins_tf)
+                                transfer_upserted += 1
+                                details.append({"pay_no": bill_no, "out_biz_no": biz_out_no, "type": "TRANSFER", "action": "insert"})
+
+                                # 新记录尝试查转账详情
+                                try:
+                                    await cls._sync_transfer_detail(auth, biz_out_no, eid)
+                                    transfer_detail_synced += 1
+                                except Exception as e:
+                                    log.warning(f"查询转账详情失败: out_biz_no={biz_out_no}, err={e}")
+                            else:
+                                # 已有记录,尝试更新状态
+                                if tf_exist.status == TransferStatusEnum.DEALING.value:
                                     try:
-                                        await cls._sync_transfer_detail(auth, out_biz_no, eid)
+                                        await cls._sync_transfer_detail(auth, biz_out_no, eid)
                                         transfer_detail_synced += 1
                                     except Exception as e:
-                                        log.warning(f"查询转账详情失败: out_biz_no={out_biz_no}, err={e}")
-                                else:
-                                    # 已有记录,尝试更新状态
-                                    if tf_exist.status == TransferStatusEnum.DEALING.value:
-                                        try:
-                                            await cls._sync_transfer_detail(auth, out_biz_no, eid)
-                                            transfer_detail_synced += 1
-                                        except Exception as e:
-                                            log.warning(f"查询转账详情失败: out_biz_no={out_biz_no}, err={e}")
-                                    details.append({"pay_no": pay_no, "out_biz_no": out_biz_no, "type": "TRANSFER", "action": "exists"})
-                            else:
-                                details.append({"pay_no": pay_no, "type": "TRANSFER", "action": "skipped", "reason": "no_out_biz_no"})
+                                        log.warning(f"查询转账详情失败: out_biz_no={biz_out_no}, err={e}")
+                                details.append({"pay_no": bill_no, "out_biz_no": biz_out_no, "type": "TRANSFER", "action": "exists"})
                         else:
-                            details.append({"pay_no": pay_no, "type": consume_type, "action": "bill_only"})
+                            details.append({"pay_no": bill_no, "type": "CONSUME", "action": "bill_only"})
 
                 except Exception as e:
                     errors += 1