perf: 优化了数据库查询
This commit is contained in:
parent
9ebf15b04f
commit
8f90df0f32
|
@ -1,5 +1,6 @@
|
|||
from django.db import models
|
||||
from apps.system.models import CommonAModel, Dictionary, CommonBModel, CommonADModel, File, BaseModel
|
||||
from django.db.models import Subquery, OuterRef
|
||||
|
||||
|
||||
class Process(CommonBModel):
|
||||
|
@ -147,7 +148,10 @@ class Route(CommonAModel):
|
|||
|
||||
@staticmethod
|
||||
def get_routes(material: Material, autotask: bool = False):
|
||||
"""
|
||||
返回工艺路线带车间
|
||||
"""
|
||||
kwargs = {'material': material}
|
||||
if autotask:
|
||||
kwargs['is_autotask'] = True
|
||||
return Route.objects.filter(**kwargs).order_by('sort', 'process_sort' 'create_time')
|
||||
return Route.objects.annotate(mgroups=Subquery(Mgroup.objects.filter(process=OuterRef('pk')))).filter(**kwargs).order_by('sort', 'process_sort' 'create_time')
|
||||
|
|
|
@ -15,9 +15,9 @@ class PmService:
|
|||
校验orderitems并返回整合后的字典以productId为key, [product, count, end_date, orderitems] 为value
|
||||
"""
|
||||
rdict = {}
|
||||
if orderitems.exclude(mtask=None).exists():
|
||||
raise ParseError('存在订单项已排任务!')
|
||||
for item in orderitems:
|
||||
if item.mtask:
|
||||
raise ParseError('订单项已排任务!')
|
||||
productId = item.material.id
|
||||
if productId not in rdict:
|
||||
rdict[productId] = [item.material, item.count,
|
||||
|
@ -38,7 +38,7 @@ class PmService:
|
|||
orderitems = OrderItem.objects.filter(pk__in=orderitemIds)
|
||||
rdict = cls.check_orderitems(orderitems)
|
||||
start_date_str = start_date.strftime('%Y%m%d')
|
||||
for k, v in enumerate(rdict):
|
||||
for k, v in rdict.items():
|
||||
product, count, end_date_cal, orderitemId_list = v
|
||||
if end_date is None:
|
||||
end_date = end_date_cal
|
||||
|
@ -53,13 +53,13 @@ class PmService:
|
|||
for ind, val in enumerate(rqs):
|
||||
if val.is_autotask:
|
||||
# 找到工段
|
||||
mgroups = Mgroup.objects.filter(process=val.process)
|
||||
mgroups_count = mgroups.count()
|
||||
mgroups = val.mgroups
|
||||
mgroups_count = len(mgroups)
|
||||
if mgroups_count == 1:
|
||||
mgroup = mgroups.first()
|
||||
elif mgroups_count == 0:
|
||||
raise ParseError(f'{val.name}-工段不存在!')
|
||||
else:
|
||||
else: # 后面可能会指定车间
|
||||
raise ParseError(f'{val.name}-工段存在多个!')
|
||||
# 找到存在的半成品
|
||||
halfgood, _ = Material.objects.get_or_create(type=Material.MA_TYPE_HALFGOOD, parent=product, mgroup=mgroup,
|
||||
|
|
Loading…
Reference in New Issue