Support filtered and selected export on apply and disburse/process board pages. Co-authored-by: Cursor <cursoragent@cursor.com>
693 lines
23 KiB
Python
693 lines
23 KiB
Python
"""车辆 ETC/加油充值业务逻辑"""
|
|
from __future__ import annotations
|
|
|
|
from datetime import date, datetime
|
|
from typing import Optional
|
|
|
|
from fastapi import HTTPException
|
|
from sqlalchemy import and_, func, or_
|
|
from sqlalchemy.orm import Session
|
|
|
|
from app.core.db_auth import DbUserContext
|
|
from app.core.utils import generate_uuid
|
|
from app.models.models import SysUser
|
|
from app.models.pl_models import (
|
|
PlVehicle,
|
|
PlVehicleRechargeApply,
|
|
PlVehicleRechargeRecord,
|
|
)
|
|
from app.schemas.pl.vehicle_recharge import (
|
|
VehicleRechargeApplyCreate,
|
|
VehicleRechargeApplyRead,
|
|
VehicleRechargeApplyUpdate,
|
|
VehicleRechargeDisburseBoardItem,
|
|
VehicleRechargeRecordCreate,
|
|
VehicleRechargeRecordRead,
|
|
VehicleRechargeRecordUpdate,
|
|
)
|
|
from app.services.pl_data_scope import apply_pl_scope, assert_pl_row_in_scope
|
|
|
|
|
|
def _generate_seq_no(
|
|
db: Session, model, column_attr: str, prefix: str, *, seq_width: int = 6
|
|
) -> str:
|
|
column = getattr(model, column_attr)
|
|
last = (
|
|
db.query(model)
|
|
.filter(column.like(f"{prefix}%"))
|
|
.order_by(column.desc())
|
|
.first()
|
|
)
|
|
seq = int(getattr(last, column_attr)[-seq_width:]) + 1 if last else 1
|
|
return f"{prefix}{seq:0{seq_width}d}"
|
|
|
|
|
|
def generate_apply_no(db: Session) -> str:
|
|
return _generate_seq_no(
|
|
db,
|
|
PlVehicleRechargeApply,
|
|
"apply_no",
|
|
f"ETC{date.today():%Y%m%d}",
|
|
seq_width=2,
|
|
)
|
|
|
|
|
|
def generate_recharge_order_no(db: Session) -> str:
|
|
return _generate_seq_no(
|
|
db,
|
|
PlVehicleRechargeRecord,
|
|
"recharge_order_no",
|
|
f"SXCL{date.today():%Y%m%d}",
|
|
)
|
|
|
|
|
|
def _resolve_user_name(db: Session, user_id: Optional[str]) -> Optional[str]:
|
|
if not user_id:
|
|
return None
|
|
row = (
|
|
db.query(SysUser)
|
|
.filter(SysUser.id == user_id, SysUser.del_flag == 0)
|
|
.first()
|
|
)
|
|
return row.nick_name or row.user_no if row else None
|
|
|
|
|
|
def _assert_vehicle_exists(db: Session, tenant_no: str, license_plate: str) -> None:
|
|
exists = (
|
|
db.query(PlVehicle.id)
|
|
.filter(
|
|
PlVehicle.tenant_no == tenant_no,
|
|
PlVehicle.license_plate == license_plate,
|
|
PlVehicle.del_flag == 0,
|
|
)
|
|
.first()
|
|
)
|
|
if not exists:
|
|
raise HTTPException(status_code=400, detail="车牌号不存在")
|
|
|
|
|
|
def _resolve_applicant_id(
|
|
db: Session,
|
|
tenant_no: str,
|
|
applicant_id: Optional[str],
|
|
user: DbUserContext,
|
|
) -> str:
|
|
target = (applicant_id or "").strip() or user.id
|
|
row = (
|
|
db.query(SysUser.id)
|
|
.filter(
|
|
SysUser.id == target,
|
|
SysUser.tenant_no == tenant_no,
|
|
SysUser.del_flag == 0,
|
|
SysUser.status == "1",
|
|
)
|
|
.first()
|
|
)
|
|
if not row:
|
|
raise HTTPException(status_code=400, detail="申请人不存在或已停用")
|
|
return target
|
|
|
|
|
|
def _get_apply_row(
|
|
db: Session, tenant_no: str, apply_id: str, user: DbUserContext
|
|
) -> PlVehicleRechargeApply:
|
|
row = (
|
|
db.query(PlVehicleRechargeApply)
|
|
.filter(
|
|
PlVehicleRechargeApply.id == apply_id,
|
|
PlVehicleRechargeApply.tenant_no == tenant_no,
|
|
PlVehicleRechargeApply.del_flag == 0,
|
|
)
|
|
.first()
|
|
)
|
|
if not row:
|
|
raise HTTPException(status_code=404, detail="申请单不存在")
|
|
assert_pl_row_in_scope(db, row, PlVehicleRechargeApply, user, tenant_no)
|
|
return row
|
|
|
|
|
|
def _get_record_row(
|
|
db: Session, tenant_no: str, record_id: str, user: DbUserContext
|
|
) -> PlVehicleRechargeRecord:
|
|
row = (
|
|
db.query(PlVehicleRechargeRecord)
|
|
.filter(
|
|
PlVehicleRechargeRecord.id == record_id,
|
|
PlVehicleRechargeRecord.tenant_no == tenant_no,
|
|
PlVehicleRechargeRecord.del_flag == 0,
|
|
)
|
|
.first()
|
|
)
|
|
if not row:
|
|
raise HTTPException(status_code=404, detail="发放登记不存在")
|
|
assert_pl_row_in_scope(db, row, PlVehicleRechargeRecord, user, tenant_no)
|
|
return row
|
|
|
|
|
|
def _has_active_record(db: Session, tenant_no: str, apply_id: str) -> bool:
|
|
return (
|
|
db.query(PlVehicleRechargeRecord.id)
|
|
.filter(
|
|
PlVehicleRechargeRecord.apply_id == apply_id,
|
|
PlVehicleRechargeRecord.tenant_no == tenant_no,
|
|
PlVehicleRechargeRecord.del_flag == 0,
|
|
)
|
|
.first()
|
|
is not None
|
|
)
|
|
|
|
|
|
def apply_to_read(row: PlVehicleRechargeApply, db: Session) -> VehicleRechargeApplyRead:
|
|
return VehicleRechargeApplyRead(
|
|
id=row.id,
|
|
applyNo=row.apply_no,
|
|
licensePlate=row.license_plate,
|
|
rechargeType=row.recharge_type,
|
|
applyDate=row.apply_date,
|
|
odometerKm=row.odometer_km,
|
|
attachmentUrls=row.attachment_urls,
|
|
applicantId=row.applicant_id,
|
|
applicantName=_resolve_user_name(db, row.applicant_id),
|
|
status=row.status,
|
|
remark=row.remark,
|
|
createTime=row.create_time,
|
|
updateTime=row.update_time,
|
|
)
|
|
|
|
|
|
def record_to_read(
|
|
row: PlVehicleRechargeRecord, db: Session, apply_no: Optional[str] = None
|
|
) -> VehicleRechargeRecordRead:
|
|
if apply_no is None and row.apply_id:
|
|
apply = (
|
|
db.query(PlVehicleRechargeApply.apply_no)
|
|
.filter(
|
|
PlVehicleRechargeApply.id == row.apply_id,
|
|
PlVehicleRechargeApply.del_flag == 0,
|
|
)
|
|
.first()
|
|
)
|
|
apply_no = apply.apply_no if apply else None
|
|
return VehicleRechargeRecordRead(
|
|
id=row.id,
|
|
rechargeOrderNo=row.recharge_order_no,
|
|
rechargeDate=row.recharge_date,
|
|
rechargeType=row.recharge_type,
|
|
cardNo=row.card_no,
|
|
gasStation=row.gas_station,
|
|
amount=row.amount,
|
|
attachmentUrls=row.attachment_urls,
|
|
applyId=row.apply_id,
|
|
applyNo=apply_no,
|
|
licensePlate=row.license_plate,
|
|
remark=row.remark,
|
|
createBy=row.create_by,
|
|
createByName=_resolve_user_name(db, row.create_by),
|
|
createTime=row.create_time,
|
|
updateTime=row.update_time,
|
|
)
|
|
|
|
|
|
def _resolve_apply_for_record(
|
|
db: Session,
|
|
tenant_no: str,
|
|
payload: VehicleRechargeRecordCreate | VehicleRechargeRecordUpdate,
|
|
user: DbUserContext,
|
|
*,
|
|
current_record_id: Optional[str] = None,
|
|
) -> tuple[Optional[PlVehicleRechargeApply], str, str]:
|
|
license_plate = (payload.license_plate or "").strip()
|
|
recharge_type = payload.recharge_type
|
|
|
|
if payload.apply_id:
|
|
apply = _get_apply_row(db, tenant_no, payload.apply_id, user)
|
|
if apply.status != "pending":
|
|
raise HTTPException(status_code=400, detail="仅待发放申请可关联")
|
|
existing = (
|
|
db.query(PlVehicleRechargeRecord)
|
|
.filter(
|
|
PlVehicleRechargeRecord.apply_id == apply.id,
|
|
PlVehicleRechargeRecord.tenant_no == tenant_no,
|
|
PlVehicleRechargeRecord.del_flag == 0,
|
|
)
|
|
.first()
|
|
)
|
|
if existing and existing.id != current_record_id:
|
|
raise HTTPException(status_code=400, detail="该申请单已被关联")
|
|
if apply.recharge_type != recharge_type:
|
|
raise HTTPException(status_code=400, detail="充值类型与申请单不一致")
|
|
return apply, apply.license_plate, apply.recharge_type
|
|
|
|
if not license_plate:
|
|
raise HTTPException(status_code=400, detail="请填写车牌号或关联申请单")
|
|
_assert_vehicle_exists(db, tenant_no, license_plate)
|
|
return None, license_plate, recharge_type
|
|
|
|
|
|
def list_applies(
|
|
db: Session,
|
|
tenant_no: str,
|
|
user: DbUserContext,
|
|
*,
|
|
q: Optional[str] = None,
|
|
recharge_type: Optional[str] = None,
|
|
status: Optional[str] = None,
|
|
apply_date_from: Optional[date] = None,
|
|
apply_date_to: Optional[date] = None,
|
|
applicant_id: Optional[str] = None,
|
|
ids: Optional[list[str]] = None,
|
|
page: int = 1,
|
|
page_size: int = 20,
|
|
max_page_size: Optional[int] = None,
|
|
) -> tuple[list[VehicleRechargeApplyRead], int]:
|
|
query = db.query(PlVehicleRechargeApply).filter(
|
|
PlVehicleRechargeApply.tenant_no == tenant_no,
|
|
PlVehicleRechargeApply.del_flag == 0,
|
|
)
|
|
if q:
|
|
query = query.filter(
|
|
or_(
|
|
PlVehicleRechargeApply.apply_no.contains(q),
|
|
PlVehicleRechargeApply.license_plate.contains(q),
|
|
)
|
|
)
|
|
if recharge_type:
|
|
query = query.filter(PlVehicleRechargeApply.recharge_type == recharge_type)
|
|
if status:
|
|
query = query.filter(PlVehicleRechargeApply.status == status)
|
|
if apply_date_from:
|
|
query = query.filter(PlVehicleRechargeApply.apply_date >= apply_date_from)
|
|
if apply_date_to:
|
|
query = query.filter(PlVehicleRechargeApply.apply_date <= apply_date_to)
|
|
if applicant_id:
|
|
query = query.filter(PlVehicleRechargeApply.applicant_id == applicant_id)
|
|
|
|
if ids:
|
|
query = query.filter(PlVehicleRechargeApply.id.in_(ids))
|
|
|
|
query = apply_pl_scope(query, PlVehicleRechargeApply, user, db, tenant_no)
|
|
total = query.count()
|
|
page = max(1, page)
|
|
cap = int(max_page_size) if max_page_size else 500
|
|
page_size = min(max(1, page_size), cap)
|
|
rows = (
|
|
query.order_by(PlVehicleRechargeApply.create_time.desc())
|
|
.offset((page - 1) * page_size)
|
|
.limit(page_size)
|
|
.all()
|
|
)
|
|
return [apply_to_read(row, db) for row in rows], total
|
|
|
|
|
|
def get_apply(
|
|
db: Session, tenant_no: str, user: DbUserContext, apply_id: str
|
|
) -> VehicleRechargeApplyRead:
|
|
row = _get_apply_row(db, tenant_no, apply_id, user)
|
|
return apply_to_read(row, db)
|
|
|
|
|
|
def create_apply(
|
|
db: Session,
|
|
tenant_no: str,
|
|
user: DbUserContext,
|
|
payload: VehicleRechargeApplyCreate,
|
|
) -> VehicleRechargeApplyRead:
|
|
license_plate = payload.license_plate.strip()
|
|
_assert_vehicle_exists(db, tenant_no, license_plate)
|
|
applicant_id = _resolve_applicant_id(db, tenant_no, payload.applicant_id, user)
|
|
now = datetime.now()
|
|
row = PlVehicleRechargeApply(
|
|
id=generate_uuid(),
|
|
apply_no=generate_apply_no(db),
|
|
license_plate=license_plate,
|
|
recharge_type=payload.recharge_type,
|
|
apply_date=payload.apply_date,
|
|
odometer_km=payload.odometer_km,
|
|
attachment_urls=payload.attachment_urls,
|
|
applicant_id=applicant_id,
|
|
status="pending",
|
|
tenant_no=tenant_no,
|
|
remark=payload.remark,
|
|
create_by=user.id,
|
|
create_dept_id=user.dept_id or "",
|
|
create_time=now,
|
|
update_by=user.id,
|
|
update_time=now,
|
|
del_flag=0,
|
|
)
|
|
db.add(row)
|
|
db.commit()
|
|
db.refresh(row)
|
|
return apply_to_read(row, db)
|
|
|
|
|
|
def update_apply(
|
|
db: Session,
|
|
tenant_no: str,
|
|
user: DbUserContext,
|
|
apply_id: str,
|
|
payload: VehicleRechargeApplyUpdate,
|
|
) -> VehicleRechargeApplyRead:
|
|
row = _get_apply_row(db, tenant_no, apply_id, user)
|
|
if row.status != "pending":
|
|
raise HTTPException(status_code=400, detail="仅待发放申请可编辑")
|
|
license_plate = payload.license_plate.strip()
|
|
_assert_vehicle_exists(db, tenant_no, license_plate)
|
|
row.license_plate = license_plate
|
|
row.recharge_type = payload.recharge_type
|
|
row.apply_date = payload.apply_date
|
|
row.odometer_km = payload.odometer_km
|
|
row.attachment_urls = payload.attachment_urls
|
|
row.applicant_id = _resolve_applicant_id(db, tenant_no, payload.applicant_id, user)
|
|
row.remark = payload.remark
|
|
row.update_by = user.id
|
|
row.update_time = datetime.now()
|
|
db.commit()
|
|
db.refresh(row)
|
|
return apply_to_read(row, db)
|
|
|
|
|
|
def cancel_apply(
|
|
db: Session, tenant_no: str, user: DbUserContext, apply_id: str
|
|
) -> VehicleRechargeApplyRead:
|
|
row = _get_apply_row(db, tenant_no, apply_id, user)
|
|
if row.status != "pending":
|
|
raise HTTPException(status_code=400, detail="仅待发放申请可作废")
|
|
row.status = "cancelled"
|
|
row.update_by = user.id
|
|
row.update_time = datetime.now()
|
|
db.commit()
|
|
db.refresh(row)
|
|
return apply_to_read(row, db)
|
|
|
|
|
|
def delete_applies(
|
|
db: Session, tenant_no: str, user: DbUserContext, apply_ids: list[str]
|
|
) -> None:
|
|
for apply_id in apply_ids:
|
|
row = _get_apply_row(db, tenant_no, apply_id, user)
|
|
if row.status != "pending":
|
|
raise HTTPException(status_code=400, detail="仅待发放申请可删除")
|
|
if _has_active_record(db, tenant_no, apply_id):
|
|
raise HTTPException(status_code=400, detail="已关联发放记录的申请不可删除")
|
|
row.del_flag = 1
|
|
row.update_by = user.id
|
|
row.update_time = datetime.now()
|
|
db.commit()
|
|
|
|
|
|
def list_pending_apply_options(
|
|
db: Session, tenant_no: str, user: DbUserContext
|
|
) -> list[dict]:
|
|
query = db.query(PlVehicleRechargeApply).filter(
|
|
PlVehicleRechargeApply.tenant_no == tenant_no,
|
|
PlVehicleRechargeApply.del_flag == 0,
|
|
PlVehicleRechargeApply.status == "pending",
|
|
)
|
|
query = apply_pl_scope(query, PlVehicleRechargeApply, user, db, tenant_no)
|
|
rows = query.order_by(PlVehicleRechargeApply.create_time.desc()).all()
|
|
return [
|
|
{
|
|
"id": row.id,
|
|
"applyNo": row.apply_no,
|
|
"licensePlate": row.license_plate,
|
|
"rechargeType": row.recharge_type,
|
|
}
|
|
for row in rows
|
|
]
|
|
|
|
|
|
def list_records(
|
|
db: Session,
|
|
tenant_no: str,
|
|
user: DbUserContext,
|
|
*,
|
|
q: Optional[str] = None,
|
|
recharge_type: Optional[str] = None,
|
|
recharge_date_from: Optional[date] = None,
|
|
recharge_date_to: Optional[date] = None,
|
|
gas_station: Optional[str] = None,
|
|
apply_no: Optional[str] = None,
|
|
page: int = 1,
|
|
page_size: int = 20,
|
|
) -> tuple[list[VehicleRechargeRecordRead], int]:
|
|
query = db.query(PlVehicleRechargeRecord).filter(
|
|
PlVehicleRechargeRecord.tenant_no == tenant_no,
|
|
PlVehicleRechargeRecord.del_flag == 0,
|
|
)
|
|
if q:
|
|
query = query.filter(
|
|
or_(
|
|
PlVehicleRechargeRecord.recharge_order_no.contains(q),
|
|
func.coalesce(PlVehicleRechargeRecord.license_plate, "").contains(q),
|
|
func.coalesce(PlVehicleRechargeRecord.card_no, "").contains(q),
|
|
)
|
|
)
|
|
if recharge_type:
|
|
query = query.filter(PlVehicleRechargeRecord.recharge_type == recharge_type)
|
|
if recharge_date_from:
|
|
query = query.filter(PlVehicleRechargeRecord.recharge_date >= recharge_date_from)
|
|
if recharge_date_to:
|
|
query = query.filter(PlVehicleRechargeRecord.recharge_date <= recharge_date_to)
|
|
if gas_station:
|
|
query = query.filter(
|
|
func.coalesce(PlVehicleRechargeRecord.gas_station, "").contains(gas_station)
|
|
)
|
|
if apply_no:
|
|
query = query.join(
|
|
PlVehicleRechargeApply,
|
|
PlVehicleRechargeApply.id == PlVehicleRechargeRecord.apply_id,
|
|
).filter(PlVehicleRechargeApply.apply_no.contains(apply_no))
|
|
|
|
query = apply_pl_scope(query, PlVehicleRechargeRecord, user, db, tenant_no)
|
|
total = query.count()
|
|
rows = (
|
|
query.order_by(PlVehicleRechargeRecord.create_time.desc())
|
|
.offset((page - 1) * page_size)
|
|
.limit(page_size)
|
|
.all()
|
|
)
|
|
return [record_to_read(row, db) for row in rows], total
|
|
|
|
|
|
def board_item_to_read(
|
|
apply_row: PlVehicleRechargeApply,
|
|
record_row: Optional[PlVehicleRechargeRecord],
|
|
db: Session,
|
|
) -> VehicleRechargeDisburseBoardItem:
|
|
return VehicleRechargeDisburseBoardItem(
|
|
applyId=apply_row.id,
|
|
applyNo=apply_row.apply_no,
|
|
licensePlate=apply_row.license_plate,
|
|
rechargeType=apply_row.recharge_type,
|
|
applyDate=apply_row.apply_date,
|
|
odometerKm=apply_row.odometer_km,
|
|
applicantName=_resolve_user_name(db, apply_row.applicant_id),
|
|
applyStatus=apply_row.status,
|
|
applyRemark=apply_row.remark,
|
|
recordId=record_row.id if record_row else None,
|
|
rechargeOrderNo=record_row.recharge_order_no if record_row else None,
|
|
rechargeDate=record_row.recharge_date if record_row else None,
|
|
cardNo=record_row.card_no if record_row else None,
|
|
gasStation=record_row.gas_station if record_row else None,
|
|
amount=record_row.amount if record_row else None,
|
|
recordCreateByName=_resolve_user_name(db, record_row.create_by) if record_row else None,
|
|
recordRemark=record_row.remark if record_row else None,
|
|
)
|
|
|
|
|
|
def list_disburse_board(
|
|
db: Session,
|
|
tenant_no: str,
|
|
user: DbUserContext,
|
|
*,
|
|
q: Optional[str] = None,
|
|
recharge_type: Optional[str] = None,
|
|
apply_status: Optional[str] = None,
|
|
apply_date_from: Optional[date] = None,
|
|
apply_date_to: Optional[date] = None,
|
|
recharge_date_from: Optional[date] = None,
|
|
recharge_date_to: Optional[date] = None,
|
|
ids: Optional[list[str]] = None,
|
|
page: int = 1,
|
|
page_size: int = 20,
|
|
max_page_size: Optional[int] = None,
|
|
) -> tuple[list[VehicleRechargeDisburseBoardItem], int]:
|
|
query = (
|
|
db.query(PlVehicleRechargeApply, PlVehicleRechargeRecord)
|
|
.outerjoin(
|
|
PlVehicleRechargeRecord,
|
|
and_(
|
|
PlVehicleRechargeRecord.apply_id == PlVehicleRechargeApply.id,
|
|
PlVehicleRechargeRecord.tenant_no == tenant_no,
|
|
PlVehicleRechargeRecord.del_flag == 0,
|
|
),
|
|
)
|
|
.filter(
|
|
PlVehicleRechargeApply.tenant_no == tenant_no,
|
|
PlVehicleRechargeApply.del_flag == 0,
|
|
)
|
|
)
|
|
if q:
|
|
query = query.filter(
|
|
or_(
|
|
PlVehicleRechargeApply.apply_no.contains(q),
|
|
PlVehicleRechargeApply.license_plate.contains(q),
|
|
func.coalesce(PlVehicleRechargeRecord.recharge_order_no, "").contains(q),
|
|
func.coalesce(PlVehicleRechargeRecord.card_no, "").contains(q),
|
|
)
|
|
)
|
|
if recharge_type:
|
|
query = query.filter(PlVehicleRechargeApply.recharge_type == recharge_type)
|
|
if apply_status:
|
|
query = query.filter(PlVehicleRechargeApply.status == apply_status)
|
|
if apply_date_from:
|
|
query = query.filter(PlVehicleRechargeApply.apply_date >= apply_date_from)
|
|
if apply_date_to:
|
|
query = query.filter(PlVehicleRechargeApply.apply_date <= apply_date_to)
|
|
if recharge_date_from:
|
|
query = query.filter(PlVehicleRechargeRecord.recharge_date >= recharge_date_from)
|
|
if recharge_date_to:
|
|
query = query.filter(PlVehicleRechargeRecord.recharge_date <= recharge_date_to)
|
|
|
|
if ids:
|
|
query = query.filter(PlVehicleRechargeApply.id.in_(ids))
|
|
|
|
query = apply_pl_scope(query, PlVehicleRechargeApply, user, db, tenant_no)
|
|
total = query.count()
|
|
page = max(1, page)
|
|
cap = int(max_page_size) if max_page_size else 500
|
|
page_size = min(max(1, page_size), cap)
|
|
rows = (
|
|
query.order_by(PlVehicleRechargeApply.create_time.desc())
|
|
.offset((page - 1) * page_size)
|
|
.limit(page_size)
|
|
.all()
|
|
)
|
|
return [board_item_to_read(apply_row, record_row, db) for apply_row, record_row in rows], total
|
|
|
|
|
|
def get_record(
|
|
db: Session, tenant_no: str, user: DbUserContext, record_id: str
|
|
) -> VehicleRechargeRecordRead:
|
|
row = _get_record_row(db, tenant_no, record_id, user)
|
|
return record_to_read(row, db)
|
|
|
|
|
|
def create_record(
|
|
db: Session,
|
|
tenant_no: str,
|
|
user: DbUserContext,
|
|
payload: VehicleRechargeRecordCreate,
|
|
) -> VehicleRechargeRecordRead:
|
|
apply_row, license_plate, recharge_type = _resolve_apply_for_record(
|
|
db, tenant_no, payload, user
|
|
)
|
|
now = datetime.now()
|
|
row = PlVehicleRechargeRecord(
|
|
id=generate_uuid(),
|
|
recharge_order_no=generate_recharge_order_no(db),
|
|
recharge_date=payload.recharge_date,
|
|
recharge_type=recharge_type,
|
|
card_no=(payload.card_no or "").strip() or None,
|
|
gas_station=(payload.gas_station or "").strip() or None,
|
|
amount=payload.amount,
|
|
attachment_urls=payload.attachment_urls,
|
|
apply_id=apply_row.id if apply_row else None,
|
|
license_plate=license_plate,
|
|
tenant_no=tenant_no,
|
|
remark=payload.remark,
|
|
create_by=user.id,
|
|
create_dept_id=user.dept_id or "",
|
|
create_time=now,
|
|
update_by=user.id,
|
|
update_time=now,
|
|
del_flag=0,
|
|
)
|
|
db.add(row)
|
|
if apply_row:
|
|
apply_row.status = "disbursed"
|
|
apply_row.update_by = user.id
|
|
apply_row.update_time = now
|
|
db.commit()
|
|
db.refresh(row)
|
|
return record_to_read(row, db)
|
|
|
|
|
|
def update_record(
|
|
db: Session,
|
|
tenant_no: str,
|
|
user: DbUserContext,
|
|
record_id: str,
|
|
payload: VehicleRechargeRecordUpdate,
|
|
) -> VehicleRechargeRecordRead:
|
|
row = _get_record_row(db, tenant_no, record_id, user)
|
|
old_apply_id = row.apply_id
|
|
apply_row, license_plate, recharge_type = _resolve_apply_for_record(
|
|
db, tenant_no, payload, user, current_record_id=record_id
|
|
)
|
|
now = datetime.now()
|
|
|
|
if old_apply_id and old_apply_id != (apply_row.id if apply_row else None):
|
|
old_apply = (
|
|
db.query(PlVehicleRechargeApply)
|
|
.filter(
|
|
PlVehicleRechargeApply.id == old_apply_id,
|
|
PlVehicleRechargeApply.tenant_no == tenant_no,
|
|
PlVehicleRechargeApply.del_flag == 0,
|
|
)
|
|
.first()
|
|
)
|
|
if old_apply:
|
|
old_apply.status = "pending"
|
|
old_apply.update_by = user.id
|
|
old_apply.update_time = now
|
|
|
|
row.recharge_date = payload.recharge_date
|
|
row.recharge_type = recharge_type
|
|
row.card_no = (payload.card_no or "").strip() or None
|
|
row.gas_station = (payload.gas_station or "").strip() or None
|
|
row.amount = payload.amount
|
|
row.attachment_urls = payload.attachment_urls
|
|
row.apply_id = apply_row.id if apply_row else None
|
|
row.license_plate = license_plate
|
|
row.remark = payload.remark
|
|
row.update_by = user.id
|
|
row.update_time = now
|
|
|
|
if apply_row:
|
|
apply_row.status = "disbursed"
|
|
apply_row.update_by = user.id
|
|
apply_row.update_time = now
|
|
|
|
db.commit()
|
|
db.refresh(row)
|
|
return record_to_read(row, db)
|
|
|
|
|
|
def delete_records(
|
|
db: Session, tenant_no: str, user: DbUserContext, record_ids: list[str]
|
|
) -> None:
|
|
now = datetime.now()
|
|
for record_id in record_ids:
|
|
row = _get_record_row(db, tenant_no, record_id, user)
|
|
apply_id = row.apply_id
|
|
row.del_flag = 1
|
|
row.update_by = user.id
|
|
row.update_time = now
|
|
if apply_id:
|
|
apply = (
|
|
db.query(PlVehicleRechargeApply)
|
|
.filter(
|
|
PlVehicleRechargeApply.id == apply_id,
|
|
PlVehicleRechargeApply.tenant_no == tenant_no,
|
|
PlVehicleRechargeApply.del_flag == 0,
|
|
)
|
|
.first()
|
|
)
|
|
if apply and apply.status == "disbursed":
|
|
apply.status = "pending"
|
|
apply.update_by = user.id
|
|
apply.update_time = now
|
|
db.commit()
|