othr
This commit is contained in:
@@ -1,13 +1,15 @@
|
||||
from sqlalchemy import select, update
|
||||
from sqlalchemy import select, update, insert
|
||||
from sqlalchemy.orm import selectinload
|
||||
|
||||
from models import BarcodeTemplate, BarcodeTemplateAttribute, BarcodeTemplateAttributeLink
|
||||
from models import BarcodeTemplate, BarcodeTemplateAttribute, barcode_template_attribute_link
|
||||
from schemas.barcode import (GetBarcodeTemplateByIdRequest,
|
||||
GetBarcodeTemplateByIdResponse,
|
||||
BarcodeTemplateCreateResponse,
|
||||
BarcodeTemplateCreateRequest, CreateBarcodeTemplateAttributeRequest,
|
||||
BarcodeTemplateUpdateResponse, BarcodeTemplateUpdateRequest,
|
||||
BarcodeTemplateAttributeSchema, GetAllBarcodeTemplateAttributesResponse,
|
||||
GetAllBarcodeTemplatesResponse)
|
||||
GetAllBarcodeTemplatesResponse, BarcodeTemplateDeleteRequest,
|
||||
BarcodeTemplateDeleteResponse)
|
||||
from services.base import BaseService
|
||||
|
||||
|
||||
@@ -23,7 +25,12 @@ class BarcodeService(BaseService):
|
||||
return query.scalar()
|
||||
|
||||
async def get_all_barcode_templates(self) -> GetAllBarcodeTemplatesResponse:
|
||||
stmt = select(BarcodeTemplate).order_by(BarcodeTemplate.id)
|
||||
stmt = (
|
||||
select(BarcodeTemplate)
|
||||
.options(
|
||||
selectinload(BarcodeTemplate.attributes)
|
||||
)
|
||||
.order_by(BarcodeTemplate.id))
|
||||
query = await self.session.execute(stmt)
|
||||
templates = query.scalars().all()
|
||||
return GetAllBarcodeTemplatesResponse(templates=templates)
|
||||
@@ -43,10 +50,12 @@ class BarcodeService(BaseService):
|
||||
raise ValueError('Шаблон с таким именем уже существует')
|
||||
|
||||
# create template then add attributes
|
||||
template = BarcodeTemplate(name=request.name, is_default=request.is_default)
|
||||
request_dict = request.dict()
|
||||
del request_dict['attribute_ids']
|
||||
template = BarcodeTemplate(**request_dict)
|
||||
self.session.add(template)
|
||||
await self.session.flush()
|
||||
|
||||
await self.session.refresh(template)
|
||||
# get all attributes from database
|
||||
stmt = select(BarcodeTemplateAttribute).filter(
|
||||
BarcodeTemplateAttribute.id.in_(request.attribute_ids))
|
||||
@@ -55,12 +64,7 @@ class BarcodeService(BaseService):
|
||||
|
||||
# add attributes to template
|
||||
for attribute in attributes:
|
||||
template_attribute_link = BarcodeTemplateAttributeLink(
|
||||
barcode_template_id=template.id,
|
||||
attribute_id=attribute.id
|
||||
)
|
||||
self.session.add(template_attribute_link)
|
||||
await self.session.flush()
|
||||
template.attributes.append(attribute)
|
||||
await self.session.commit()
|
||||
return BarcodeTemplateCreateResponse(message='Шаблон успешно создан',
|
||||
ok=True,
|
||||
@@ -97,24 +101,22 @@ class BarcodeService(BaseService):
|
||||
)
|
||||
|
||||
# difference deleted and new
|
||||
template_attributes = set([attribute.attribute_id for attribute in template.attributes])
|
||||
template_attributes = set([attribute.id for attribute in template.attributes])
|
||||
request_attributes = set(request.attribute_ids)
|
||||
new_attributes = request_attributes.difference(template_attributes)
|
||||
deleted_attributes = template_attributes.difference(request_attributes)
|
||||
new_attribute_ids = request_attributes.difference(template_attributes)
|
||||
deleted_attribute_ids = template_attributes.difference(request_attributes)
|
||||
|
||||
# delete attributes
|
||||
for attribute in template.attributes:
|
||||
if attribute.attribute_id not in deleted_attributes:
|
||||
if attribute.id not in deleted_attribute_ids:
|
||||
continue
|
||||
await self.session.delete(attribute)
|
||||
|
||||
# add new attributes
|
||||
for new_attribute in new_attributes:
|
||||
template_attribute_link = BarcodeTemplateAttributeLink(
|
||||
barcode_template_id=template.id,
|
||||
attribute_id=new_attribute
|
||||
)
|
||||
self.session.add(template_attribute_link)
|
||||
template.attributes.remove(attribute)
|
||||
for new_attribute_id in new_attribute_ids:
|
||||
stmt = insert(barcode_template_attribute_link).values({
|
||||
'barcode_template_id': template.id,
|
||||
'attribute_id': new_attribute_id
|
||||
})
|
||||
await self.session.execute(stmt)
|
||||
await self.session.flush()
|
||||
|
||||
await self.session.commit()
|
||||
@@ -125,6 +127,15 @@ class BarcodeService(BaseService):
|
||||
return BarcodeTemplateUpdateResponse(message=str(e),
|
||||
ok=False)
|
||||
|
||||
async def delete_template(self, request: BarcodeTemplateDeleteRequest) -> BarcodeTemplateDeleteResponse:
|
||||
try:
|
||||
template = await self.session.get(BarcodeTemplate, request.id)
|
||||
await self.session.delete(template)
|
||||
await self.session.commit()
|
||||
return BarcodeTemplateDeleteResponse(ok=True, message='Шаблон успешно удален')
|
||||
except Exception as e:
|
||||
return BarcodeTemplateDeleteResponse(ok=False, message=str(e))
|
||||
|
||||
# endregion
|
||||
|
||||
# region Template attributes
|
||||
|
||||
@@ -91,7 +91,17 @@ class ClientService(BaseService):
|
||||
client = await self.get_by_id(request.data.id)
|
||||
if not client:
|
||||
return ClientUpdateResponse(ok=False, message='Клиент не найден')
|
||||
await self.session.execute(update(Client).where(Client.id == client.id).values(name=request.data.name))
|
||||
request_dict = request.data.dict()
|
||||
del request_dict['id']
|
||||
del request_dict['details']
|
||||
del request_dict['barcode_template']
|
||||
request_dict['barcode_template_id'] = request.data.barcode_template.id
|
||||
stmt = (
|
||||
update(Client)
|
||||
.where(Client.id == client.id)
|
||||
.values(**request_dict)
|
||||
)
|
||||
await self.session.execute(stmt)
|
||||
await self.update_details(user, client, request.data.details)
|
||||
await self.session.commit()
|
||||
return ClientUpdateResponse(ok=True, message='Клиент обновлен')
|
||||
|
||||
@@ -21,7 +21,7 @@ class DealService(BaseService):
|
||||
async def _get_deal_by_id(self, deal_id) -> Union[Deal, None]:
|
||||
return await self.session.get(Deal, deal_id)
|
||||
|
||||
async def _get_rank_for_deal(self, deal_status: DealStatus) -> int:
|
||||
async def _get_rank_for_deal(self, deal_status: DealStatus) -> str:
|
||||
deal_query = await self.session.execute(
|
||||
select(Deal).where(Deal.current_status == deal_status).order_by(Deal.lexorank.desc()).limit(1))
|
||||
deal = deal_query.scalar_one_or_none()
|
||||
@@ -35,7 +35,7 @@ class DealService(BaseService):
|
||||
user: User,
|
||||
deadline: datetime.datetime = None,
|
||||
rank=None,
|
||||
comment: str = '') -> DealStatusHistory:
|
||||
comment: str = ''):
|
||||
if not deal.current_status == status:
|
||||
deadline = deadline
|
||||
status_change = DealStatusHistory(
|
||||
|
||||
Reference in New Issue
Block a user