Pārlūkot izejas kodu

fix: 批次作废回退相关额度记录为待发放(PENDING)状态

alphah 2 nedēļas atpakaļ
vecāks
revīzija
02819d1715

+ 42 - 0
backend/app/plugin/module_payment/expense/quota/service.py

@@ -631,6 +631,48 @@ class QuotaService:
         except Exception as e:
             log.warning(f"更新批次本地状态失败(不影响作废): {e}")
 
+        # 作废批次后,将涉及员工的额度记录回退为待发放状态
+        try:
+            from app.plugin.module_payment.expense.quota.model import QuotaModel
+            from app.plugin.module_payment.expense.quota.enums import QuotaStatusEnum
+            from sqlalchemy import select, update as sa_update
+
+            records_resp = await cls.issue_batch_records_query_service(
+                auth,
+                IssueBatchRecordsQuerySchema(
+                    enterprise_id=data.enterprise_id,
+                    institution_id=data.institution_id,
+                    issue_batch_id=data.issue_batch_id,
+                    page_size=100,
+                    page_num=1,
+                ),
+            )
+            if records_resp.issue_record_info_list:
+                reset_count = 0
+                for record in records_resp.issue_record_info_list:
+                    if record.issue_status != 1:
+                        continue
+                    owner_id = record.owner_id
+                    quota_id = record.quota_id
+                    if not owner_id:
+                        continue
+                    upd = sa_update(QuotaModel).where(
+                        QuotaModel.employee_id == owner_id,
+                        QuotaModel.institution_id == data.institution_id,
+                    ).values(
+                        total_amount=0,
+                        available_amount=0,
+                        status=QuotaStatusEnum.QUOTA_PENDING.value,
+                        quota_id=None,
+                    )
+                    await auth.db.execute(upd)
+                    reset_count += 1
+                if reset_count:
+                    await auth.db.flush()
+                    log.info(f"批次作废 - 已回退 {reset_count} 条额度记录为待发放状态")
+        except Exception as e:
+            log.warning(f"回退额度记录失败(不影响作废): {e}")
+
         return IssueBatchCancelOutSchema(
             result=getattr(result, 'result', False),
         )