"""采集协议单 — 协议级退费登记 CRUD(不改转账笔 pay_status)""" from __future__ import annotations from datetime import datetime from typing import Any, Dict, List, Optional from sqlalchemy.orm import Session from app.core.utils import generate_uuid, parse_datetime from app.crud.collection_protocol import urls_to_attachment_list from app.models.models import BizProtocolRefundRecord from app.services.protocol_refund_record import validate_refund_amount def _now() -> datetime: return datetime.now() def _record_to_dict( db: Session, r: BizProtocolRefundRecord, tenant_no: str ) -> Dict[str, Any]: return { "id": r.id, "collectionProtocolId": r.collection_protocol_id, "refundAmount": float(r.refund_amount) if r.refund_amount is not None else None, "refundTime": r.refund_time.strftime("%Y-%m-%d %H:%M:%S") if r.refund_time else "", "refundRemark": r.refund_remark or "", "refundAttachments": r.refund_attachments or "", "refundAttachmentFiles": urls_to_attachment_list( r.refund_attachments, db, tenant_no ), "workflowInstanceId": r.workflow_instance_id or "", "remark": r.remark or "", "createTime": r.create_time.strftime("%Y-%m-%d %H:%M:%S") if r.create_time else "", "updateTime": r.update_time.strftime("%Y-%m-%d %H:%M:%S") if r.update_time else "", } def list_refund_records( db: Session, *, collection_protocol_id: str, tenant_no: str ) -> List[Dict[str, Any]]: rows = ( db.query(BizProtocolRefundRecord) .filter( BizProtocolRefundRecord.collection_protocol_id == collection_protocol_id, BizProtocolRefundRecord.tenant_no == tenant_no, BizProtocolRefundRecord.del_flag == 0, ) .order_by(BizProtocolRefundRecord.create_time.desc()) .all() ) return [_record_to_dict(db, r, tenant_no) for r in rows] def create_refund_record( db: Session, *, collection_protocol_id: str, tenant_no: str, user_id: Optional[str], refund_amount: Any, refund_time: Any = None, refund_remark: Optional[str] = None, refund_attachments: Optional[str] = None, workflow_instance_id: Optional[str] = None, remark: Optional[str] = None, ) -> BizProtocolRefundRecord: amt = validate_refund_amount(refund_amount) now = _now() rt = None if refund_time not in (None, ""): rt = ( refund_time if isinstance(refund_time, datetime) else parse_datetime(str(refund_time)) ) obj = BizProtocolRefundRecord( id=generate_uuid(), collection_protocol_id=collection_protocol_id, refund_amount=amt, refund_time=rt or now, refund_remark=(refund_remark or "").strip() or None, refund_attachments=(refund_attachments or "").strip() or None, workflow_instance_id=(workflow_instance_id or "").strip() or None, tenant_no=tenant_no, order_num=0, remark=(remark or "").strip() or None, create_by=user_id, create_dept_id="", create_time=now, update_by=user_id, update_time=now, del_flag=0, ) db.add(obj) db.commit() db.refresh(obj) return obj def update_refund_record( db: Session, *, record_id: str, tenant_no: str, user_id: Optional[str], refund_amount: Any = None, refund_time: Any = None, refund_remark: Optional[str] = None, refund_attachments: Optional[str] = None, workflow_instance_id: Optional[str] = None, remark: Optional[str] = None, fields_set: Optional[set] = None, ) -> BizProtocolRefundRecord: obj = ( db.query(BizProtocolRefundRecord) .filter( BizProtocolRefundRecord.id == record_id, BizProtocolRefundRecord.tenant_no == tenant_no, BizProtocolRefundRecord.del_flag == 0, ) .first() ) if not obj: raise ValueError("退费登记不存在") fs = fields_set or { "refund_amount", "refund_time", "refund_remark", "refund_attachments", "workflow_instance_id", "remark", } if "refund_amount" in fs and refund_amount is not None: obj.refund_amount = validate_refund_amount(refund_amount) if "refund_time" in fs: if refund_time in (None, ""): obj.refund_time = None elif isinstance(refund_time, datetime): obj.refund_time = refund_time else: obj.refund_time = parse_datetime(str(refund_time)) if "refund_remark" in fs: obj.refund_remark = (refund_remark or "").strip() or None if "refund_attachments" in fs: obj.refund_attachments = (refund_attachments or "").strip() or None if "workflow_instance_id" in fs: obj.workflow_instance_id = (workflow_instance_id or "").strip() or None if "remark" in fs: obj.remark = (remark or "").strip() or None obj.update_by = user_id obj.update_time = _now() db.add(obj) db.commit() db.refresh(obj) return obj def soft_delete_refund_record( db: Session, *, record_id: str, tenant_no: str, user_id: Optional[str], ) -> None: obj = ( db.query(BizProtocolRefundRecord) .filter( BizProtocolRefundRecord.id == record_id, BizProtocolRefundRecord.tenant_no == tenant_no, BizProtocolRefundRecord.del_flag == 0, ) .first() ) if not obj: raise ValueError("退费登记不存在") obj.del_flag = 1 obj.update_by = user_id obj.update_time = _now() db.add(obj) db.commit()