Support filtered and selected export on apply and disburse/process board pages. Co-authored-by: Cursor <cursoragent@cursor.com>
705 lines
23 KiB
Python
705 lines
23 KiB
Python
"""车辆维保业务逻辑"""
|
|
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,
|
|
PlVehicleMaintenanceApply,
|
|
PlVehicleMaintenanceProcess,
|
|
)
|
|
from app.schemas.pl.vehicle_maintenance import (
|
|
VehicleMaintenanceApplyCreate,
|
|
VehicleMaintenanceApplyRead,
|
|
VehicleMaintenanceApplyReminder,
|
|
VehicleMaintenanceApplyUpdate,
|
|
VehicleMaintenanceProcessBoardItem,
|
|
VehicleMaintenanceProcessCreate,
|
|
VehicleMaintenanceProcessRead,
|
|
VehicleMaintenanceProcessUpdate,
|
|
)
|
|
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,
|
|
PlVehicleMaintenanceApply,
|
|
"apply_no",
|
|
f"WB{date.today():%Y%m%d}",
|
|
seq_width=2,
|
|
)
|
|
|
|
|
|
def generate_process_no(db: Session) -> str:
|
|
return _generate_seq_no(
|
|
db,
|
|
PlVehicleMaintenanceProcess,
|
|
"process_no",
|
|
f"SXWC{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_user_id(
|
|
db: Session,
|
|
tenant_no: str,
|
|
user_id: Optional[str],
|
|
user: DbUserContext,
|
|
*,
|
|
field_label: str = "用户",
|
|
) -> str:
|
|
target = (user_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=f"{field_label}不存在或已停用")
|
|
return target
|
|
|
|
|
|
def _validate_apply_dates(
|
|
apply_date: date,
|
|
start_date: date,
|
|
expected_pickup_date: Optional[date],
|
|
) -> None:
|
|
if apply_date > start_date:
|
|
raise HTTPException(status_code=400, detail="申请时间不能晚于维保开始时间")
|
|
if expected_pickup_date and start_date > expected_pickup_date:
|
|
raise HTTPException(status_code=400, detail="维保开始时间不能晚于预计取车时间")
|
|
|
|
|
|
def _get_apply_row(
|
|
db: Session, tenant_no: str, apply_id: str, user: DbUserContext
|
|
) -> PlVehicleMaintenanceApply:
|
|
row = (
|
|
db.query(PlVehicleMaintenanceApply)
|
|
.filter(
|
|
PlVehicleMaintenanceApply.id == apply_id,
|
|
PlVehicleMaintenanceApply.tenant_no == tenant_no,
|
|
PlVehicleMaintenanceApply.del_flag == 0,
|
|
)
|
|
.first()
|
|
)
|
|
if not row:
|
|
raise HTTPException(status_code=404, detail="申请单不存在")
|
|
assert_pl_row_in_scope(db, row, PlVehicleMaintenanceApply, user, tenant_no)
|
|
return row
|
|
|
|
|
|
def _get_process_row(
|
|
db: Session, tenant_no: str, process_id: str, user: DbUserContext
|
|
) -> PlVehicleMaintenanceProcess:
|
|
row = (
|
|
db.query(PlVehicleMaintenanceProcess)
|
|
.filter(
|
|
PlVehicleMaintenanceProcess.id == process_id,
|
|
PlVehicleMaintenanceProcess.tenant_no == tenant_no,
|
|
PlVehicleMaintenanceProcess.del_flag == 0,
|
|
)
|
|
.first()
|
|
)
|
|
if not row:
|
|
raise HTTPException(status_code=404, detail="处理登记不存在")
|
|
assert_pl_row_in_scope(db, row, PlVehicleMaintenanceProcess, user, tenant_no)
|
|
return row
|
|
|
|
|
|
def _has_active_process(db: Session, tenant_no: str, apply_id: str) -> bool:
|
|
return (
|
|
db.query(PlVehicleMaintenanceProcess.id)
|
|
.filter(
|
|
PlVehicleMaintenanceProcess.apply_id == apply_id,
|
|
PlVehicleMaintenanceProcess.tenant_no == tenant_no,
|
|
PlVehicleMaintenanceProcess.del_flag == 0,
|
|
)
|
|
.first()
|
|
is not None
|
|
)
|
|
|
|
|
|
def apply_to_read(row: PlVehicleMaintenanceApply, db: Session) -> VehicleMaintenanceApplyRead:
|
|
return VehicleMaintenanceApplyRead(
|
|
id=row.id,
|
|
applyNo=row.apply_no,
|
|
licensePlate=row.license_plate,
|
|
applicantId=row.applicant_id,
|
|
applicantName=_resolve_user_name(db, row.applicant_id),
|
|
applyDate=row.apply_date,
|
|
maintenanceType=row.maintenance_type,
|
|
startDate=row.start_date,
|
|
expectedPickupDate=row.expected_pickup_date,
|
|
maintenanceContent=row.maintenance_content,
|
|
shopAddress=row.shop_address,
|
|
attachmentUrls=row.attachment_urls,
|
|
status=row.status,
|
|
remark=row.remark,
|
|
createTime=row.create_time,
|
|
updateTime=row.update_time,
|
|
)
|
|
|
|
|
|
def process_to_read(
|
|
row: PlVehicleMaintenanceProcess, db: Session, apply_no: Optional[str] = None
|
|
) -> VehicleMaintenanceProcessRead:
|
|
if apply_no is None and row.apply_id:
|
|
apply = (
|
|
db.query(PlVehicleMaintenanceApply.apply_no)
|
|
.filter(
|
|
PlVehicleMaintenanceApply.id == row.apply_id,
|
|
PlVehicleMaintenanceApply.del_flag == 0,
|
|
)
|
|
.first()
|
|
)
|
|
apply_no = apply.apply_no if apply else None
|
|
return VehicleMaintenanceProcessRead(
|
|
id=row.id,
|
|
processNo=row.process_no,
|
|
applyId=row.apply_id,
|
|
applyNo=apply_no,
|
|
licensePlate=row.license_plate,
|
|
amount=row.amount,
|
|
handlerId=row.handler_id,
|
|
handlerName=_resolve_user_name(db, row.handler_id),
|
|
processDate=row.process_date,
|
|
attachmentUrls=row.attachment_urls,
|
|
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_process(
|
|
db: Session,
|
|
tenant_no: str,
|
|
payload: VehicleMaintenanceProcessCreate | VehicleMaintenanceProcessUpdate,
|
|
user: DbUserContext,
|
|
*,
|
|
current_process_id: Optional[str] = None,
|
|
) -> tuple[Optional[PlVehicleMaintenanceApply], str]:
|
|
license_plate = (payload.license_plate or "").strip()
|
|
|
|
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(PlVehicleMaintenanceProcess)
|
|
.filter(
|
|
PlVehicleMaintenanceProcess.apply_id == apply.id,
|
|
PlVehicleMaintenanceProcess.tenant_no == tenant_no,
|
|
PlVehicleMaintenanceProcess.del_flag == 0,
|
|
)
|
|
.first()
|
|
)
|
|
if existing and existing.id != current_process_id:
|
|
raise HTTPException(status_code=400, detail="该申请单已被关联")
|
|
return apply, apply.license_plate
|
|
|
|
if not license_plate:
|
|
raise HTTPException(status_code=400, detail="请填写车牌号或关联申请单")
|
|
_assert_vehicle_exists(db, tenant_no, license_plate)
|
|
return None, license_plate
|
|
|
|
|
|
def list_applies(
|
|
db: Session,
|
|
tenant_no: str,
|
|
user: DbUserContext,
|
|
*,
|
|
q: Optional[str] = None,
|
|
maintenance_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[VehicleMaintenanceApplyRead], int]:
|
|
query = db.query(PlVehicleMaintenanceApply).filter(
|
|
PlVehicleMaintenanceApply.tenant_no == tenant_no,
|
|
PlVehicleMaintenanceApply.del_flag == 0,
|
|
)
|
|
if q:
|
|
query = query.filter(
|
|
or_(
|
|
PlVehicleMaintenanceApply.apply_no.contains(q),
|
|
PlVehicleMaintenanceApply.license_plate.contains(q),
|
|
)
|
|
)
|
|
if maintenance_type:
|
|
query = query.filter(
|
|
PlVehicleMaintenanceApply.maintenance_type.contains(maintenance_type)
|
|
)
|
|
if status:
|
|
query = query.filter(PlVehicleMaintenanceApply.status == status)
|
|
if apply_date_from:
|
|
query = query.filter(PlVehicleMaintenanceApply.apply_date >= apply_date_from)
|
|
if apply_date_to:
|
|
query = query.filter(PlVehicleMaintenanceApply.apply_date <= apply_date_to)
|
|
if applicant_id:
|
|
query = query.filter(PlVehicleMaintenanceApply.applicant_id == applicant_id)
|
|
if ids:
|
|
query = query.filter(PlVehicleMaintenanceApply.id.in_(ids))
|
|
|
|
query = apply_pl_scope(query, PlVehicleMaintenanceApply, 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(PlVehicleMaintenanceApply.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
|
|
) -> VehicleMaintenanceApplyRead:
|
|
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: VehicleMaintenanceApplyCreate,
|
|
) -> VehicleMaintenanceApplyRead:
|
|
license_plate = payload.license_plate.strip()
|
|
maintenance_type = payload.maintenance_type.strip()
|
|
if not maintenance_type:
|
|
raise HTTPException(status_code=400, detail="维保类型不能为空")
|
|
_assert_vehicle_exists(db, tenant_no, license_plate)
|
|
_validate_apply_dates(
|
|
payload.apply_date, payload.start_date, payload.expected_pickup_date
|
|
)
|
|
applicant_id = _resolve_user_id(
|
|
db, tenant_no, payload.applicant_id, user, field_label="申请人"
|
|
)
|
|
now = datetime.now()
|
|
row = PlVehicleMaintenanceApply(
|
|
id=generate_uuid(),
|
|
apply_no=generate_apply_no(db),
|
|
license_plate=license_plate,
|
|
applicant_id=applicant_id,
|
|
apply_date=payload.apply_date,
|
|
maintenance_type=maintenance_type,
|
|
start_date=payload.start_date,
|
|
expected_pickup_date=payload.expected_pickup_date,
|
|
maintenance_content=(payload.maintenance_content or "").strip() or None,
|
|
shop_address=(payload.shop_address or "").strip() or None,
|
|
attachment_urls=payload.attachment_urls,
|
|
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: VehicleMaintenanceApplyUpdate,
|
|
) -> VehicleMaintenanceApplyRead:
|
|
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()
|
|
maintenance_type = payload.maintenance_type.strip()
|
|
if not maintenance_type:
|
|
raise HTTPException(status_code=400, detail="维保类型不能为空")
|
|
_assert_vehicle_exists(db, tenant_no, license_plate)
|
|
_validate_apply_dates(
|
|
payload.apply_date, payload.start_date, payload.expected_pickup_date
|
|
)
|
|
row.license_plate = license_plate
|
|
row.applicant_id = _resolve_user_id(
|
|
db, tenant_no, payload.applicant_id, user, field_label="申请人"
|
|
)
|
|
row.apply_date = payload.apply_date
|
|
row.maintenance_type = maintenance_type
|
|
row.start_date = payload.start_date
|
|
row.expected_pickup_date = payload.expected_pickup_date
|
|
row.maintenance_content = (payload.maintenance_content or "").strip() or None
|
|
row.shop_address = (payload.shop_address or "").strip() or None
|
|
row.attachment_urls = payload.attachment_urls
|
|
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
|
|
) -> VehicleMaintenanceApplyRead:
|
|
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_process(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_reminders(
|
|
db: Session,
|
|
tenant_no: str,
|
|
user: DbUserContext,
|
|
license_plate: str,
|
|
*,
|
|
exclude_apply_id: Optional[str] = None,
|
|
) -> list[VehicleMaintenanceApplyReminder]:
|
|
plate = license_plate.strip()
|
|
if not plate:
|
|
return []
|
|
today = date.today()
|
|
query = db.query(PlVehicleMaintenanceApply).filter(
|
|
PlVehicleMaintenanceApply.tenant_no == tenant_no,
|
|
PlVehicleMaintenanceApply.del_flag == 0,
|
|
PlVehicleMaintenanceApply.status == "pending",
|
|
PlVehicleMaintenanceApply.license_plate == plate,
|
|
)
|
|
if exclude_apply_id:
|
|
query = query.filter(PlVehicleMaintenanceApply.id != exclude_apply_id)
|
|
query = apply_pl_scope(query, PlVehicleMaintenanceApply, user, db, tenant_no)
|
|
rows = query.order_by(PlVehicleMaintenanceApply.create_time.desc()).all()
|
|
result: list[VehicleMaintenanceApplyReminder] = []
|
|
for row in rows:
|
|
pickup = row.expected_pickup_date
|
|
if pickup is not None and today > pickup:
|
|
continue
|
|
result.append(
|
|
VehicleMaintenanceApplyReminder(
|
|
id=row.id,
|
|
applyNo=row.apply_no,
|
|
licensePlate=row.license_plate,
|
|
maintenanceType=row.maintenance_type,
|
|
applyDate=row.apply_date,
|
|
startDate=row.start_date,
|
|
expectedPickupDate=row.expected_pickup_date,
|
|
maintenanceContent=row.maintenance_content,
|
|
shopAddress=row.shop_address,
|
|
)
|
|
)
|
|
return result
|
|
|
|
|
|
def board_item_to_read(
|
|
apply_row: PlVehicleMaintenanceApply,
|
|
process_row: Optional[PlVehicleMaintenanceProcess],
|
|
db: Session,
|
|
) -> VehicleMaintenanceProcessBoardItem:
|
|
return VehicleMaintenanceProcessBoardItem(
|
|
applyId=apply_row.id,
|
|
applyNo=apply_row.apply_no,
|
|
licensePlate=apply_row.license_plate,
|
|
maintenanceType=apply_row.maintenance_type,
|
|
applyDate=apply_row.apply_date,
|
|
startDate=apply_row.start_date,
|
|
expectedPickupDate=apply_row.expected_pickup_date,
|
|
maintenanceContent=apply_row.maintenance_content,
|
|
shopAddress=apply_row.shop_address,
|
|
applicantName=_resolve_user_name(db, apply_row.applicant_id),
|
|
applyStatus=apply_row.status,
|
|
applyRemark=apply_row.remark,
|
|
processId=process_row.id if process_row else None,
|
|
processNo=process_row.process_no if process_row else None,
|
|
processDate=process_row.process_date if process_row else None,
|
|
amount=process_row.amount if process_row else None,
|
|
handlerName=_resolve_user_name(db, process_row.handler_id) if process_row else None,
|
|
processRemark=process_row.remark if process_row else None,
|
|
)
|
|
|
|
|
|
def list_process_board(
|
|
db: Session,
|
|
tenant_no: str,
|
|
user: DbUserContext,
|
|
*,
|
|
q: Optional[str] = None,
|
|
maintenance_type: Optional[str] = None,
|
|
apply_status: Optional[str] = None,
|
|
apply_date_from: Optional[date] = None,
|
|
apply_date_to: Optional[date] = None,
|
|
process_date_from: Optional[date] = None,
|
|
process_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[VehicleMaintenanceProcessBoardItem], int]:
|
|
query = (
|
|
db.query(PlVehicleMaintenanceApply, PlVehicleMaintenanceProcess)
|
|
.outerjoin(
|
|
PlVehicleMaintenanceProcess,
|
|
and_(
|
|
PlVehicleMaintenanceProcess.apply_id == PlVehicleMaintenanceApply.id,
|
|
PlVehicleMaintenanceProcess.tenant_no == tenant_no,
|
|
PlVehicleMaintenanceProcess.del_flag == 0,
|
|
),
|
|
)
|
|
.filter(
|
|
PlVehicleMaintenanceApply.tenant_no == tenant_no,
|
|
PlVehicleMaintenanceApply.del_flag == 0,
|
|
)
|
|
)
|
|
if q:
|
|
query = query.filter(
|
|
or_(
|
|
PlVehicleMaintenanceApply.apply_no.contains(q),
|
|
PlVehicleMaintenanceApply.license_plate.contains(q),
|
|
func.coalesce(PlVehicleMaintenanceProcess.process_no, "").contains(q),
|
|
)
|
|
)
|
|
if maintenance_type:
|
|
query = query.filter(
|
|
PlVehicleMaintenanceApply.maintenance_type.contains(maintenance_type)
|
|
)
|
|
if apply_status:
|
|
query = query.filter(PlVehicleMaintenanceApply.status == apply_status)
|
|
if apply_date_from:
|
|
query = query.filter(PlVehicleMaintenanceApply.apply_date >= apply_date_from)
|
|
if apply_date_to:
|
|
query = query.filter(PlVehicleMaintenanceApply.apply_date <= apply_date_to)
|
|
if process_date_from:
|
|
query = query.filter(
|
|
PlVehicleMaintenanceProcess.process_date >= process_date_from
|
|
)
|
|
if process_date_to:
|
|
query = query.filter(PlVehicleMaintenanceProcess.process_date <= process_date_to)
|
|
|
|
if ids:
|
|
query = query.filter(PlVehicleMaintenanceApply.id.in_(ids))
|
|
|
|
query = apply_pl_scope(query, PlVehicleMaintenanceApply, 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(PlVehicleMaintenanceApply.create_time.desc())
|
|
.offset((page - 1) * page_size)
|
|
.limit(page_size)
|
|
.all()
|
|
)
|
|
return [
|
|
board_item_to_read(apply_row, process_row, db)
|
|
for apply_row, process_row in rows
|
|
], total
|
|
|
|
|
|
def get_process(
|
|
db: Session, tenant_no: str, user: DbUserContext, process_id: str
|
|
) -> VehicleMaintenanceProcessRead:
|
|
row = _get_process_row(db, tenant_no, process_id, user)
|
|
return process_to_read(row, db)
|
|
|
|
|
|
def create_process(
|
|
db: Session,
|
|
tenant_no: str,
|
|
user: DbUserContext,
|
|
payload: VehicleMaintenanceProcessCreate,
|
|
) -> VehicleMaintenanceProcessRead:
|
|
apply_row, license_plate = _resolve_apply_for_process(db, tenant_no, payload, user)
|
|
handler_id = _resolve_user_id(
|
|
db, tenant_no, payload.handler_id, user, field_label="处理人"
|
|
)
|
|
now = datetime.now()
|
|
row = PlVehicleMaintenanceProcess(
|
|
id=generate_uuid(),
|
|
process_no=generate_process_no(db),
|
|
apply_id=apply_row.id if apply_row else None,
|
|
license_plate=license_plate,
|
|
amount=payload.amount,
|
|
handler_id=handler_id,
|
|
process_date=payload.process_date,
|
|
attachment_urls=payload.attachment_urls,
|
|
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 = "processed"
|
|
apply_row.update_by = user.id
|
|
apply_row.update_time = now
|
|
db.commit()
|
|
db.refresh(row)
|
|
return process_to_read(row, db)
|
|
|
|
|
|
def update_process(
|
|
db: Session,
|
|
tenant_no: str,
|
|
user: DbUserContext,
|
|
process_id: str,
|
|
payload: VehicleMaintenanceProcessUpdate,
|
|
) -> VehicleMaintenanceProcessRead:
|
|
row = _get_process_row(db, tenant_no, process_id, user)
|
|
old_apply_id = row.apply_id
|
|
apply_row, license_plate = _resolve_apply_for_process(
|
|
db, tenant_no, payload, user, current_process_id=process_id
|
|
)
|
|
handler_id = _resolve_user_id(
|
|
db, tenant_no, payload.handler_id, user, field_label="处理人"
|
|
)
|
|
now = datetime.now()
|
|
|
|
if old_apply_id and old_apply_id != (apply_row.id if apply_row else None):
|
|
old_apply = (
|
|
db.query(PlVehicleMaintenanceApply)
|
|
.filter(
|
|
PlVehicleMaintenanceApply.id == old_apply_id,
|
|
PlVehicleMaintenanceApply.tenant_no == tenant_no,
|
|
PlVehicleMaintenanceApply.del_flag == 0,
|
|
)
|
|
.first()
|
|
)
|
|
if old_apply:
|
|
old_apply.status = "pending"
|
|
old_apply.update_by = user.id
|
|
old_apply.update_time = now
|
|
|
|
row.apply_id = apply_row.id if apply_row else None
|
|
row.license_plate = license_plate
|
|
row.amount = payload.amount
|
|
row.handler_id = handler_id
|
|
row.process_date = payload.process_date
|
|
row.attachment_urls = payload.attachment_urls
|
|
row.remark = payload.remark
|
|
row.update_by = user.id
|
|
row.update_time = now
|
|
|
|
if apply_row:
|
|
apply_row.status = "processed"
|
|
apply_row.update_by = user.id
|
|
apply_row.update_time = now
|
|
|
|
db.commit()
|
|
db.refresh(row)
|
|
return process_to_read(row, db)
|
|
|
|
|
|
def delete_processes(
|
|
db: Session, tenant_no: str, user: DbUserContext, process_ids: list[str]
|
|
) -> None:
|
|
now = datetime.now()
|
|
for process_id in process_ids:
|
|
row = _get_process_row(db, tenant_no, process_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(PlVehicleMaintenanceApply)
|
|
.filter(
|
|
PlVehicleMaintenanceApply.id == apply_id,
|
|
PlVehicleMaintenanceApply.tenant_no == tenant_no,
|
|
PlVehicleMaintenanceApply.del_flag == 0,
|
|
)
|
|
.first()
|
|
)
|
|
if apply and apply.status == "processed":
|
|
apply.status = "pending"
|
|
apply.update_by = user.id
|
|
apply.update_time = now
|
|
db.commit()
|