功能:新增托盘库存事务基础
This commit is contained in:
@@ -0,0 +1,101 @@
|
||||
from sqlalchemy import func, select
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from module_business.entity.do.master_do import BizPallet
|
||||
from module_business.entity.do.operation_do import BizPalletEvent, BizPalletInventory
|
||||
|
||||
|
||||
class InventoryDao:
|
||||
"""托盘库存锁、资产锁和只追加事件数据库操作层。"""
|
||||
|
||||
@classmethod
|
||||
async def get_inventory_for_update(
|
||||
cls,
|
||||
db: AsyncSession,
|
||||
pallet_id: int,
|
||||
) -> BizPalletInventory | None:
|
||||
result = await db.execute(
|
||||
select(BizPalletInventory).where(BizPalletInventory.pallet_id == pallet_id).with_for_update()
|
||||
)
|
||||
return result.scalar_one_or_none()
|
||||
|
||||
@classmethod
|
||||
async def get_inventories_for_update(
|
||||
cls,
|
||||
db: AsyncSession,
|
||||
pallet_ids: list[int],
|
||||
) -> list[BizPalletInventory]:
|
||||
ordered_ids = sorted(set(pallet_ids))
|
||||
if not ordered_ids:
|
||||
return []
|
||||
|
||||
result = await db.execute(
|
||||
select(BizPalletInventory)
|
||||
.where(BizPalletInventory.pallet_id.in_(ordered_ids))
|
||||
.order_by(BizPalletInventory.pallet_id)
|
||||
.with_for_update()
|
||||
)
|
||||
return sorted(result.scalars().all(), key=lambda inventory: inventory.pallet_id)
|
||||
|
||||
@classmethod
|
||||
async def get_pallet_by_qr_token(
|
||||
cls,
|
||||
db: AsyncSession,
|
||||
qr_token: str,
|
||||
) -> BizPallet | None:
|
||||
result = await db.execute(select(BizPallet).where(BizPallet.qr_token == qr_token))
|
||||
return result.scalar_one_or_none()
|
||||
|
||||
@classmethod
|
||||
async def get_pallets_by_ids(
|
||||
cls,
|
||||
db: AsyncSession,
|
||||
pallet_ids: list[int],
|
||||
) -> list[BizPallet]:
|
||||
"""在调用方已锁库存后,按托盘 ID 升序锁定资产行。"""
|
||||
ordered_ids = sorted(set(pallet_ids))
|
||||
if not ordered_ids:
|
||||
return []
|
||||
|
||||
result = await db.execute(
|
||||
select(BizPallet)
|
||||
.where(BizPallet.pallet_id.in_(ordered_ids))
|
||||
.order_by(BizPallet.pallet_id)
|
||||
.with_for_update()
|
||||
)
|
||||
return sorted(result.scalars().all(), key=lambda pallet: pallet.pallet_id)
|
||||
|
||||
@classmethod
|
||||
async def get_event_by_idempotency_key(
|
||||
cls,
|
||||
db: AsyncSession,
|
||||
pallet_id: int,
|
||||
idempotency_key: str,
|
||||
) -> BizPalletEvent | None:
|
||||
result = await db.execute(
|
||||
select(BizPalletEvent).where(
|
||||
BizPalletEvent.pallet_id == pallet_id,
|
||||
BizPalletEvent.idempotency_key == idempotency_key,
|
||||
)
|
||||
)
|
||||
return result.scalar_one_or_none()
|
||||
|
||||
@classmethod
|
||||
async def add_event(
|
||||
cls,
|
||||
db: AsyncSession,
|
||||
inventory: BizPalletInventory,
|
||||
event: BizPalletEvent,
|
||||
) -> BizPalletEvent:
|
||||
"""在调用方持有库存行锁时,为托盘追加下一序号事件。"""
|
||||
sequence_result = await db.execute(
|
||||
select(func.max(BizPalletEvent.event_seq)).where(BizPalletEvent.pallet_id == inventory.pallet_id)
|
||||
)
|
||||
last_sequence = sequence_result.scalar_one()
|
||||
event.pallet_id = inventory.pallet_id
|
||||
event.event_seq = (last_sequence or 0) + 1
|
||||
db.add(event)
|
||||
await db.flush()
|
||||
inventory.last_event_id = event.event_id
|
||||
await db.flush()
|
||||
return event
|
||||
@@ -0,0 +1,667 @@
|
||||
from datetime import datetime, time
|
||||
from typing import Any
|
||||
|
||||
from sqlalchemy import ColumnElement, Select, delete, select, update
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
from sqlalchemy.orm.attributes import InstrumentedAttribute
|
||||
|
||||
from common.vo import PageModel
|
||||
from module_business.entity.do.master_do import BizPallet
|
||||
from module_business.entity.do.operation_do import (
|
||||
BizFillingRecord,
|
||||
BizInboundItem,
|
||||
BizInboundOrder,
|
||||
BizReturnItem,
|
||||
BizReturnOrder,
|
||||
BizShipmentItem,
|
||||
BizShipmentOrder,
|
||||
BizUsageRecord,
|
||||
)
|
||||
from module_business.entity.vo.operation_vo import (
|
||||
FillingPageQueryModel,
|
||||
InboundPageQueryModel,
|
||||
ReturnPageQueryModel,
|
||||
ShipmentPageQueryModel,
|
||||
UsagePageQueryModel,
|
||||
)
|
||||
from utils.page_util import PageUtil
|
||||
|
||||
|
||||
class OperationDao:
|
||||
"""托盘入库、装填、发货、使用和退回单据数据库操作层。"""
|
||||
|
||||
@staticmethod
|
||||
def _time_conditions(
|
||||
column: InstrumentedAttribute,
|
||||
begin_time: str | None,
|
||||
end_time: str | None,
|
||||
) -> list[ColumnElement[bool]]:
|
||||
conditions = []
|
||||
if begin_time:
|
||||
begin_at = datetime.combine(
|
||||
datetime.strptime(begin_time, '%Y-%m-%d').date(),
|
||||
time.min,
|
||||
)
|
||||
conditions.append(column >= begin_at)
|
||||
if end_time:
|
||||
end_at = datetime.combine(
|
||||
datetime.strptime(end_time, '%Y-%m-%d').date(),
|
||||
time(23, 59, 59),
|
||||
)
|
||||
conditions.append(column <= end_at)
|
||||
return conditions
|
||||
|
||||
@staticmethod
|
||||
async def _paginate(
|
||||
db: AsyncSession,
|
||||
statement: Select,
|
||||
query_object: Any,
|
||||
is_page: bool,
|
||||
) -> PageModel | list[dict[str, Any]]:
|
||||
return await PageUtil.paginate(
|
||||
db,
|
||||
statement,
|
||||
query_object.page_num,
|
||||
query_object.page_size,
|
||||
is_page,
|
||||
)
|
||||
|
||||
@staticmethod
|
||||
async def _get_for_update(
|
||||
db: AsyncSession,
|
||||
model: type[Any],
|
||||
id_column: InstrumentedAttribute,
|
||||
record_id: int,
|
||||
*extra_conditions: ColumnElement[bool],
|
||||
) -> Any | None:
|
||||
result = await db.execute(select(model).where(id_column == record_id, *extra_conditions).with_for_update())
|
||||
return result.scalar_one_or_none()
|
||||
|
||||
@staticmethod
|
||||
async def _get_by_key(
|
||||
db: AsyncSession,
|
||||
model: type[Any],
|
||||
key_column: InstrumentedAttribute,
|
||||
idempotency_key: str,
|
||||
) -> Any | None:
|
||||
result = await db.execute(select(model).where(key_column == idempotency_key))
|
||||
return result.scalar_one_or_none()
|
||||
|
||||
@staticmethod
|
||||
async def _get_by_id(
|
||||
db: AsyncSession,
|
||||
model: type[Any],
|
||||
id_column: InstrumentedAttribute,
|
||||
record_id: int,
|
||||
) -> Any | None:
|
||||
result = await db.execute(select(model).where(id_column == record_id))
|
||||
return result.scalar_one_or_none()
|
||||
|
||||
@classmethod
|
||||
async def _get_order_detail(
|
||||
cls,
|
||||
db: AsyncSession,
|
||||
order_model: type[Any],
|
||||
order_id_column: InstrumentedAttribute,
|
||||
order_id: int,
|
||||
item_model: type[Any],
|
||||
item_order_id_column: InstrumentedAttribute,
|
||||
item_id_column: InstrumentedAttribute,
|
||||
) -> Any | None:
|
||||
order = await cls._get_by_id(db, order_model, order_id_column, order_id)
|
||||
if order is None:
|
||||
return None
|
||||
|
||||
item_result = await db.execute(
|
||||
select(item_model).where(item_order_id_column == order_id).order_by(item_id_column)
|
||||
)
|
||||
order.items = list(item_result.scalars().all())
|
||||
return order
|
||||
|
||||
@staticmethod
|
||||
async def _add(db: AsyncSession, record: Any) -> Any:
|
||||
db.add(record)
|
||||
await db.flush()
|
||||
return record
|
||||
|
||||
@staticmethod
|
||||
async def _update(
|
||||
db: AsyncSession,
|
||||
model: type[Any],
|
||||
id_column: InstrumentedAttribute,
|
||||
record: Any,
|
||||
) -> Any:
|
||||
primary_key = id_column.key
|
||||
payload = {
|
||||
column.key: vars(record)[column.key]
|
||||
for column in model.__table__.columns
|
||||
if column.key != primary_key and column.key in vars(record)
|
||||
}
|
||||
if payload:
|
||||
await db.execute(update(model).where(id_column == getattr(record, primary_key)).values(**payload))
|
||||
await db.flush()
|
||||
return record
|
||||
|
||||
@staticmethod
|
||||
async def _delete(
|
||||
db: AsyncSession,
|
||||
model: type[Any],
|
||||
id_column: InstrumentedAttribute,
|
||||
record_id: int,
|
||||
) -> None:
|
||||
await db.execute(delete(model).where(id_column == record_id))
|
||||
await db.flush()
|
||||
|
||||
@staticmethod
|
||||
async def _replace_items(
|
||||
db: AsyncSession,
|
||||
item_model: type[Any],
|
||||
order_id_column: InstrumentedAttribute,
|
||||
order_id: int,
|
||||
items: list[Any],
|
||||
) -> list[Any]:
|
||||
await db.execute(delete(item_model).where(order_id_column == order_id))
|
||||
for item in items:
|
||||
setattr(item, order_id_column.key, order_id)
|
||||
if items:
|
||||
db.add_all(items)
|
||||
await db.flush()
|
||||
return items
|
||||
|
||||
@staticmethod
|
||||
async def _add_items(db: AsyncSession, items: list[Any]) -> list[Any]:
|
||||
if items:
|
||||
db.add_all(items)
|
||||
await db.flush()
|
||||
return items
|
||||
|
||||
@classmethod
|
||||
async def get_inbound_for_update(
|
||||
cls,
|
||||
db: AsyncSession,
|
||||
inbound_id: int,
|
||||
) -> BizInboundOrder | None:
|
||||
return await cls._get_for_update(
|
||||
db,
|
||||
BizInboundOrder,
|
||||
BizInboundOrder.inbound_id,
|
||||
inbound_id,
|
||||
)
|
||||
|
||||
@classmethod
|
||||
async def get_shipment_for_update(
|
||||
cls,
|
||||
db: AsyncSession,
|
||||
shipment_id: int,
|
||||
) -> BizShipmentOrder | None:
|
||||
return await cls._get_for_update(
|
||||
db,
|
||||
BizShipmentOrder,
|
||||
BizShipmentOrder.shipment_id,
|
||||
shipment_id,
|
||||
)
|
||||
|
||||
@classmethod
|
||||
async def get_return_for_update(
|
||||
cls,
|
||||
db: AsyncSession,
|
||||
return_id: int,
|
||||
) -> BizReturnOrder | None:
|
||||
return await cls._get_for_update(
|
||||
db,
|
||||
BizReturnOrder,
|
||||
BizReturnOrder.return_id,
|
||||
return_id,
|
||||
)
|
||||
|
||||
@classmethod
|
||||
async def get_active_filling_for_update(
|
||||
cls,
|
||||
db: AsyncSession,
|
||||
filling_id: int,
|
||||
) -> BizFillingRecord | None:
|
||||
"""按已锁库存的 active_filling_id 锁定仍处于活动状态的装填周期。"""
|
||||
return await cls._get_for_update(
|
||||
db,
|
||||
BizFillingRecord,
|
||||
BizFillingRecord.filling_id,
|
||||
filling_id,
|
||||
BizFillingRecord.active_slot == 'ACTIVE',
|
||||
)
|
||||
|
||||
@classmethod
|
||||
async def get_inbound_by_confirm_idempotency_key(
|
||||
cls,
|
||||
db: AsyncSession,
|
||||
idempotency_key: str,
|
||||
) -> BizInboundOrder | None:
|
||||
return await cls._get_by_key(
|
||||
db,
|
||||
BizInboundOrder,
|
||||
BizInboundOrder.confirm_idempotency_key,
|
||||
idempotency_key,
|
||||
)
|
||||
|
||||
@classmethod
|
||||
async def get_shipment_by_confirm_idempotency_key(
|
||||
cls,
|
||||
db: AsyncSession,
|
||||
idempotency_key: str,
|
||||
) -> BizShipmentOrder | None:
|
||||
return await cls._get_by_key(
|
||||
db,
|
||||
BizShipmentOrder,
|
||||
BizShipmentOrder.confirm_idempotency_key,
|
||||
idempotency_key,
|
||||
)
|
||||
|
||||
@classmethod
|
||||
async def get_filling_by_idempotency_key(
|
||||
cls,
|
||||
db: AsyncSession,
|
||||
idempotency_key: str,
|
||||
) -> BizFillingRecord | None:
|
||||
return await cls._get_by_key(
|
||||
db,
|
||||
BizFillingRecord,
|
||||
BizFillingRecord.idempotency_key,
|
||||
idempotency_key,
|
||||
)
|
||||
|
||||
@classmethod
|
||||
async def get_usage_by_idempotency_key(
|
||||
cls,
|
||||
db: AsyncSession,
|
||||
idempotency_key: str,
|
||||
) -> BizUsageRecord | None:
|
||||
return await cls._get_by_key(
|
||||
db,
|
||||
BizUsageRecord,
|
||||
BizUsageRecord.idempotency_key,
|
||||
idempotency_key,
|
||||
)
|
||||
|
||||
@classmethod
|
||||
async def get_return_by_request_idempotency_key(
|
||||
cls,
|
||||
db: AsyncSession,
|
||||
idempotency_key: str,
|
||||
) -> BizReturnOrder | None:
|
||||
return await cls._get_by_key(
|
||||
db,
|
||||
BizReturnOrder,
|
||||
BizReturnOrder.request_idempotency_key,
|
||||
idempotency_key,
|
||||
)
|
||||
|
||||
@classmethod
|
||||
async def get_return_by_confirm_idempotency_key(
|
||||
cls,
|
||||
db: AsyncSession,
|
||||
idempotency_key: str,
|
||||
) -> BizReturnOrder | None:
|
||||
return await cls._get_by_key(
|
||||
db,
|
||||
BizReturnOrder,
|
||||
BizReturnOrder.confirm_idempotency_key,
|
||||
idempotency_key,
|
||||
)
|
||||
|
||||
@classmethod
|
||||
async def get_inbound_list(
|
||||
cls,
|
||||
db: AsyncSession,
|
||||
query_object: InboundPageQueryModel,
|
||||
is_page: bool = True,
|
||||
) -> PageModel | list[dict[str, Any]]:
|
||||
conditions = []
|
||||
if query_object.inbound_no:
|
||||
conditions.append(BizInboundOrder.inbound_no.like(f'%{query_object.inbound_no}%'))
|
||||
if query_object.warehouse_id:
|
||||
conditions.append(BizInboundOrder.warehouse_id == query_object.warehouse_id)
|
||||
if query_object.status:
|
||||
conditions.append(BizInboundOrder.status == query_object.status.value)
|
||||
conditions.extend(
|
||||
cls._time_conditions(
|
||||
BizInboundOrder.create_time,
|
||||
query_object.begin_time,
|
||||
query_object.end_time,
|
||||
)
|
||||
)
|
||||
statement = select(BizInboundOrder).where(*conditions).order_by(BizInboundOrder.inbound_id)
|
||||
return await cls._paginate(db, statement, query_object, is_page)
|
||||
|
||||
@classmethod
|
||||
async def get_inbound_by_id(
|
||||
cls,
|
||||
db: AsyncSession,
|
||||
inbound_id: int,
|
||||
) -> BizInboundOrder | None:
|
||||
return await cls._get_order_detail(
|
||||
db,
|
||||
BizInboundOrder,
|
||||
BizInboundOrder.inbound_id,
|
||||
inbound_id,
|
||||
BizInboundItem,
|
||||
BizInboundItem.inbound_id,
|
||||
BizInboundItem.inbound_item_id,
|
||||
)
|
||||
|
||||
@classmethod
|
||||
async def add_inbound(
|
||||
cls,
|
||||
db: AsyncSession,
|
||||
inbound: BizInboundOrder,
|
||||
) -> BizInboundOrder:
|
||||
return await cls._add(db, inbound)
|
||||
|
||||
@classmethod
|
||||
async def update_inbound(
|
||||
cls,
|
||||
db: AsyncSession,
|
||||
inbound: BizInboundOrder,
|
||||
) -> BizInboundOrder:
|
||||
return await cls._update(
|
||||
db,
|
||||
BizInboundOrder,
|
||||
BizInboundOrder.inbound_id,
|
||||
inbound,
|
||||
)
|
||||
|
||||
@classmethod
|
||||
async def delete_inbound(cls, db: AsyncSession, inbound_id: int) -> None:
|
||||
await cls._delete(db, BizInboundOrder, BizInboundOrder.inbound_id, inbound_id)
|
||||
|
||||
@classmethod
|
||||
async def replace_inbound_items(
|
||||
cls,
|
||||
db: AsyncSession,
|
||||
inbound_id: int,
|
||||
items: list[BizInboundItem],
|
||||
) -> list[BizInboundItem]:
|
||||
return await cls._replace_items(
|
||||
db,
|
||||
BizInboundItem,
|
||||
BizInboundItem.inbound_id,
|
||||
inbound_id,
|
||||
items,
|
||||
)
|
||||
|
||||
@classmethod
|
||||
async def get_filling_list(
|
||||
cls,
|
||||
db: AsyncSession,
|
||||
query_object: FillingPageQueryModel,
|
||||
is_page: bool = True,
|
||||
) -> PageModel | list[dict[str, Any]]:
|
||||
conditions = []
|
||||
if query_object.filling_no:
|
||||
conditions.append(BizFillingRecord.filling_no.like(f'%{query_object.filling_no}%'))
|
||||
if query_object.warehouse_id:
|
||||
conditions.append(BizFillingRecord.warehouse_id == query_object.warehouse_id)
|
||||
if query_object.strain_id:
|
||||
conditions.append(BizFillingRecord.strain_id == query_object.strain_id)
|
||||
if query_object.batch_no:
|
||||
conditions.append(BizFillingRecord.batch_no.like(f'%{query_object.batch_no}%'))
|
||||
if query_object.pallet_code:
|
||||
conditions.append(BizPallet.pallet_code.like(f'%{query_object.pallet_code}%'))
|
||||
conditions.extend(
|
||||
cls._time_conditions(
|
||||
BizFillingRecord.operated_at,
|
||||
query_object.begin_time,
|
||||
query_object.end_time,
|
||||
)
|
||||
)
|
||||
statement = select(BizFillingRecord)
|
||||
if query_object.pallet_code:
|
||||
statement = statement.join(
|
||||
BizPallet,
|
||||
BizPallet.pallet_id == BizFillingRecord.pallet_id,
|
||||
)
|
||||
statement = statement.where(*conditions).order_by(BizFillingRecord.filling_id)
|
||||
return await cls._paginate(db, statement, query_object, is_page)
|
||||
|
||||
@classmethod
|
||||
async def get_filling_by_id(
|
||||
cls,
|
||||
db: AsyncSession,
|
||||
filling_id: int,
|
||||
) -> BizFillingRecord | None:
|
||||
return await cls._get_by_id(
|
||||
db,
|
||||
BizFillingRecord,
|
||||
BizFillingRecord.filling_id,
|
||||
filling_id,
|
||||
)
|
||||
|
||||
@classmethod
|
||||
async def add_filling_record(
|
||||
cls,
|
||||
db: AsyncSession,
|
||||
filling: BizFillingRecord,
|
||||
) -> BizFillingRecord:
|
||||
return await cls._add(db, filling)
|
||||
|
||||
@classmethod
|
||||
async def update_filling_record(
|
||||
cls,
|
||||
db: AsyncSession,
|
||||
filling: BizFillingRecord,
|
||||
) -> BizFillingRecord:
|
||||
return await cls._update(
|
||||
db,
|
||||
BizFillingRecord,
|
||||
BizFillingRecord.filling_id,
|
||||
filling,
|
||||
)
|
||||
|
||||
@classmethod
|
||||
async def get_shipment_list(
|
||||
cls,
|
||||
db: AsyncSession,
|
||||
query_object: ShipmentPageQueryModel,
|
||||
is_page: bool = True,
|
||||
) -> PageModel | list[dict[str, Any]]:
|
||||
conditions = []
|
||||
if query_object.shipment_no:
|
||||
conditions.append(BizShipmentOrder.shipment_no.like(f'%{query_object.shipment_no}%'))
|
||||
if query_object.source_warehouse_id:
|
||||
conditions.append(BizShipmentOrder.source_warehouse_id == query_object.source_warehouse_id)
|
||||
if query_object.target_site_id:
|
||||
conditions.append(BizShipmentOrder.target_site_id == query_object.target_site_id)
|
||||
if query_object.status:
|
||||
conditions.append(BizShipmentOrder.status == query_object.status.value)
|
||||
conditions.extend(
|
||||
cls._time_conditions(
|
||||
BizShipmentOrder.create_time,
|
||||
query_object.begin_time,
|
||||
query_object.end_time,
|
||||
)
|
||||
)
|
||||
statement = select(BizShipmentOrder).where(*conditions).order_by(BizShipmentOrder.shipment_id)
|
||||
return await cls._paginate(db, statement, query_object, is_page)
|
||||
|
||||
@classmethod
|
||||
async def get_shipment_by_id(
|
||||
cls,
|
||||
db: AsyncSession,
|
||||
shipment_id: int,
|
||||
) -> BizShipmentOrder | None:
|
||||
return await cls._get_order_detail(
|
||||
db,
|
||||
BizShipmentOrder,
|
||||
BizShipmentOrder.shipment_id,
|
||||
shipment_id,
|
||||
BizShipmentItem,
|
||||
BizShipmentItem.shipment_id,
|
||||
BizShipmentItem.shipment_item_id,
|
||||
)
|
||||
|
||||
@classmethod
|
||||
async def add_shipment(
|
||||
cls,
|
||||
db: AsyncSession,
|
||||
shipment: BizShipmentOrder,
|
||||
) -> BizShipmentOrder:
|
||||
return await cls._add(db, shipment)
|
||||
|
||||
@classmethod
|
||||
async def update_shipment(
|
||||
cls,
|
||||
db: AsyncSession,
|
||||
shipment: BizShipmentOrder,
|
||||
) -> BizShipmentOrder:
|
||||
return await cls._update(
|
||||
db,
|
||||
BizShipmentOrder,
|
||||
BizShipmentOrder.shipment_id,
|
||||
shipment,
|
||||
)
|
||||
|
||||
@classmethod
|
||||
async def delete_shipment(cls, db: AsyncSession, shipment_id: int) -> None:
|
||||
await cls._delete(
|
||||
db,
|
||||
BizShipmentOrder,
|
||||
BizShipmentOrder.shipment_id,
|
||||
shipment_id,
|
||||
)
|
||||
|
||||
@classmethod
|
||||
async def replace_shipment_items(
|
||||
cls,
|
||||
db: AsyncSession,
|
||||
shipment_id: int,
|
||||
items: list[BizShipmentItem],
|
||||
) -> list[BizShipmentItem]:
|
||||
return await cls._replace_items(
|
||||
db,
|
||||
BizShipmentItem,
|
||||
BizShipmentItem.shipment_id,
|
||||
shipment_id,
|
||||
items,
|
||||
)
|
||||
|
||||
@classmethod
|
||||
async def get_usage_list(
|
||||
cls,
|
||||
db: AsyncSession,
|
||||
query_object: UsagePageQueryModel,
|
||||
is_page: bool = True,
|
||||
) -> PageModel | list[dict[str, Any]]:
|
||||
conditions = []
|
||||
if query_object.usage_no:
|
||||
conditions.append(BizUsageRecord.usage_no.like(f'%{query_object.usage_no}%'))
|
||||
if query_object.site_id:
|
||||
conditions.append(BizUsageRecord.site_id == query_object.site_id)
|
||||
if query_object.line_id:
|
||||
conditions.append(BizUsageRecord.line_id == query_object.line_id)
|
||||
if query_object.pallet_code:
|
||||
conditions.append(BizPallet.pallet_code.like(f'%{query_object.pallet_code}%'))
|
||||
conditions.extend(
|
||||
cls._time_conditions(
|
||||
BizUsageRecord.occurred_at,
|
||||
query_object.begin_time,
|
||||
query_object.end_time,
|
||||
)
|
||||
)
|
||||
statement = select(BizUsageRecord)
|
||||
if query_object.pallet_code:
|
||||
statement = statement.join(
|
||||
BizPallet,
|
||||
BizPallet.pallet_id == BizUsageRecord.pallet_id,
|
||||
)
|
||||
statement = statement.where(*conditions).order_by(BizUsageRecord.usage_id)
|
||||
return await cls._paginate(db, statement, query_object, is_page)
|
||||
|
||||
@classmethod
|
||||
async def get_usage_by_id(
|
||||
cls,
|
||||
db: AsyncSession,
|
||||
usage_id: int,
|
||||
) -> BizUsageRecord | None:
|
||||
return await cls._get_by_id(
|
||||
db,
|
||||
BizUsageRecord,
|
||||
BizUsageRecord.usage_id,
|
||||
usage_id,
|
||||
)
|
||||
|
||||
@classmethod
|
||||
async def add_usage_record(
|
||||
cls,
|
||||
db: AsyncSession,
|
||||
usage: BizUsageRecord,
|
||||
) -> BizUsageRecord:
|
||||
return await cls._add(db, usage)
|
||||
|
||||
@classmethod
|
||||
async def get_return_list(
|
||||
cls,
|
||||
db: AsyncSession,
|
||||
query_object: ReturnPageQueryModel,
|
||||
is_page: bool = True,
|
||||
) -> PageModel | list[dict[str, Any]]:
|
||||
conditions = []
|
||||
if query_object.return_no:
|
||||
conditions.append(BizReturnOrder.return_no.like(f'%{query_object.return_no}%'))
|
||||
if query_object.source_site_id:
|
||||
conditions.append(BizReturnOrder.source_site_id == query_object.source_site_id)
|
||||
if query_object.target_warehouse_id:
|
||||
conditions.append(BizReturnOrder.target_warehouse_id == query_object.target_warehouse_id)
|
||||
if query_object.status:
|
||||
conditions.append(BizReturnOrder.status == query_object.status.value)
|
||||
conditions.extend(
|
||||
cls._time_conditions(
|
||||
BizReturnOrder.requested_at,
|
||||
query_object.begin_time,
|
||||
query_object.end_time,
|
||||
)
|
||||
)
|
||||
statement = select(BizReturnOrder).where(*conditions).order_by(BizReturnOrder.return_id)
|
||||
return await cls._paginate(db, statement, query_object, is_page)
|
||||
|
||||
@classmethod
|
||||
async def get_return_by_id(
|
||||
cls,
|
||||
db: AsyncSession,
|
||||
return_id: int,
|
||||
) -> BizReturnOrder | None:
|
||||
return await cls._get_order_detail(
|
||||
db,
|
||||
BizReturnOrder,
|
||||
BizReturnOrder.return_id,
|
||||
return_id,
|
||||
BizReturnItem,
|
||||
BizReturnItem.return_id,
|
||||
BizReturnItem.return_item_id,
|
||||
)
|
||||
|
||||
@classmethod
|
||||
async def add_return_order(
|
||||
cls,
|
||||
db: AsyncSession,
|
||||
return_order: BizReturnOrder,
|
||||
) -> BizReturnOrder:
|
||||
return await cls._add(db, return_order)
|
||||
|
||||
@classmethod
|
||||
async def update_return_order(
|
||||
cls,
|
||||
db: AsyncSession,
|
||||
return_order: BizReturnOrder,
|
||||
) -> BizReturnOrder:
|
||||
return await cls._update(
|
||||
db,
|
||||
BizReturnOrder,
|
||||
BizReturnOrder.return_id,
|
||||
return_order,
|
||||
)
|
||||
|
||||
@classmethod
|
||||
async def add_return_items(
|
||||
cls,
|
||||
db: AsyncSession,
|
||||
items: list[BizReturnItem],
|
||||
) -> list[BizReturnItem]:
|
||||
return await cls._add_items(db, items)
|
||||
Reference in New Issue
Block a user