feat: sflog init_test接口

This commit is contained in:
caoqianming 2023-07-05 13:44:07 +08:00
parent 0f1bb7fd72
commit 93881852d0
1 changed files with 44 additions and 17 deletions

View File

@ -10,6 +10,8 @@ from apps.utils.viewsets import CustomGenericViewSet, CustomModelViewSet
from apps.wpm.models import SfLog, StLog, StSfLog from apps.wpm.models import SfLog, StLog, StSfLog
from apps.wpm.serializers import SfLogSerializer, StLogSerializer, StSfLogSerializer from apps.wpm.serializers import SfLogSerializer, StLogSerializer, StSfLogSerializer
from apps.wpm.filters import SfLogFilter from apps.wpm.filters import SfLogFilter
from apps.mtm.models import Material
from django.utils.timezone import localtime
# Create your views here. # Create your views here.
class StLogViewSet(ListModelMixin, CustomGenericViewSet): class StLogViewSet(ListModelMixin, CustomGenericViewSet):
@ -40,23 +42,48 @@ class SfLogViewSet(UpdateModelMixin, ListModelMixin, DestroyModelMixin, CustomGe
search_fields = ['note'] search_fields = ['note']
ordering = ['-start_time'] ordering = ['-start_time']
@transaction.atomic # 此处需要进行修改
def perform_create(self, serializer): # @transaction.atomic
ins = serializer.save() # def perform_create(self, serializer):
# 查看并比对停机记录 # ins = serializer.save()
stls = StLog.objects.filter(mroup=ins.mgroup) # # 查看并比对停机记录
stls_ = (stls.filter(start_time__gte=ins.start_time, start_time__lt=ins.end_time)| # stls = StLog.objects.filter(mroup=ins.mgroup)
stls.exclude(end_time=None).filter(start_time__lte=ins.start_time, end_time__gt=ins.end_time)| # stls_ = (stls.filter(start_time__gte=ins.start_time, start_time__lt=ins.end_time)|
stls.exclude(end_time=None).filter(end_time__gte=ins.start_time, end_time__lt=ins.end_time)) # stls.exclude(end_time=None).filter(start_time__lte=ins.start_time, end_time__gt=ins.end_time)|
for i in stls_: # stls.exclude(end_time=None).filter(end_time__gte=ins.start_time, end_time__lt=ins.end_time))
StSfLog.objects.get_or_create(stlog=i, sflog=ins, defaults={'stlog': i, 'sflog': ins}) # for i in stls_:
stsflog = StSfLog.objects.filter(stlog=i).order_by('-sflog__start_time').first() # StSfLog.objects.get_or_create(stlog=i, sflog=ins, defaults={'stlog': i, 'sflog': ins})
if stsflog: # stsflog = StSfLog.objects.filter(stlog=i).order_by('-sflog__start_time').first()
stsflog.is_current_down = True # if stsflog:
stsflog.save() # stsflog.is_current_down = True
# 计算能耗 # stsflog.save()
from apps.enm.tasks import cal_sflog_en_val # # 计算能耗
cal_sflog_en_val.delay(ins.id) # from apps.enm.tasks import cal_sflog_en_val
# cal_sflog_en_val.delay(ins.id)
@action(methods=['get'], detail=True, perms_map={'get': '*'})
def init_test(self, request, pk=None):
"""
初始化检测录入
初始化检测录入
"""
from apps.qm.models import QuaStat, TestItem
from apps.qm.serializers import QuaStatSerializer
obj = self.get_object()
test_materials = obj.mgroup.test_materials
for mid in test_materials:
material = Material.objects.get(id=mid)
testitems = material.testitems
for tid in testitems:
testitem = TestItem.objects.get(id=tid)
end_time_local = localtime(obj.end_time)
params = {'type': 'sflog', 'material': material, 'testitem': testitem}
params_default = params.update({'year_s': end_time_local.year, 'month': end_time_local.month, 'day': end_time_local.day})
QuaStat.objects.get_or_create(**params, defaults=params_default)
qs = QuaStat.objects.filter(type='sflog', sflog=obj)
sr = QuaStatSerializer(instance=qs, many=True)
return Response(sr.data)
class StSfLogViewSet(ListModelMixin, UpdateModelMixin, CustomGenericViewSet): class StSfLogViewSet(ListModelMixin, UpdateModelMixin, CustomGenericViewSet):
""" """