4
from typing import Any, Optional, List
5
from starlette.requests import Request
6
from sqlalchemy.ext.asyncio import AsyncSession
8
from app.inventory.order.enums.order_enum import OrderStatus
9
from app.inventory.order.models.order_models import Order
10
from app.inventory.order.schemas.order_schemas import OrderCreateScheme, OrderUpdateScheme, OrderFilter
11
from core.helpers.broker.tkq import list_brocker
12
from core.permissions import permit
13
from core.service.base import BaseService, UpdateSchemaType, ModelType, FilterSchemaType, CreateSchemaType
17
async def print_foo(foo: str = None) -> None:
18
env = list_brocker.state.data['env'].get_env()
19
adapter = env['order'].adapter
20
service = env['order'].service
21
some_order = await service.list({'lsn__gt': 0})
22
order = await adapter.get(some_order[0].id)
23
await asyncio.sleep(3)
27
class OrderService(BaseService[Order, OrderCreateScheme, OrderUpdateScheme, OrderFilter]):
28
def __init__(self, request: Request):
29
super(OrderService, self).__init__(request, Order, OrderCreateScheme, OrderUpdateScheme)
32
async def update(self, id: Any, obj: UpdateSchemaType) -> Optional[ModelType]:
33
return await super(OrderService, self).update(id, obj)
36
async def list(self, _filter: FilterSchemaType, size: int = 100):
37
return await super(OrderService, self).list(_filter, size)
39
@permit('order_create')
40
async def create(self, obj: CreateSchemaType) -> ModelType:
42
Метод создания ордера, в нем особой проверки не нужно, тк в теории ордер может быть создан как угодно
44
obj.number = datetime.datetime.now(datetime.UTC).strftime('%y%m%d%H%m%S')
45
obj.created_by = self.user.user_id
46
obj.edited_by = self.user.user_id
47
await print_foo.kiq('sd')
48
return await super(OrderService, self).create(obj)
50
@permit('order_delete')
51
async def delete(self, id: Any) -> None:
52
return await super(OrderService, self).delete(id)
54
@permit('order_move_counstructor')
55
async def move_counstructor(self, order_id: uuid.UUID, moves: list) -> None:
56
return await super(OrderService, self).delete(id)
58
@permit('assign_order')
59
async def assign_order(self, order_id: uuid.UUID, user_id: uuid.UUID = None) -> ModelType:
60
order_entity = await self.get(order_id)
62
order_entity.user_ids.append(user_id)
64
order_entity.user_ids.append(self.user.user_id)
65
await self.session.commit()
66
await self.session.refresh(order_entity)
67
await order_entity.notify('update')
70
@permit('order_start')
71
async def order_start(self, ids: List[uuid.UUID], user_id: uuid.UUID = None) -> List[ModelType]:
72
""" Стартует ордера, конфермит все мувы и ставит статус Confirmed"""
75
order_entity = await self.get(order_id)
76
for move in order_entity.move_list_rel:
77
await self.env['move'].service._confirm(move=move)
79
order_entity.user_ids.append(user_id)
81
order_entity.user_ids.append(self.user.user_id)
82
order_entity.status = OrderStatus.CONFIRMED
83
await self.session.commit()
84
await self.session.refresh(order_entity)
85
await order_entity.notify('update')
86
for move in order_entity.move_list_rel:
87
await self.session.refresh(move)
88
res.append(order_entity)