feat: sflogexp合并stlog

This commit is contained in:
caoqianming 2023-08-08 14:41:50 +08:00
parent f4e3d43034
commit ccfeb9d901
8 changed files with 102 additions and 81 deletions

View File

@ -15,6 +15,7 @@ class SfLogFilter(filters.FilterSet):
} }
class SfLogExpFilter(filters.FilterSet): class SfLogExpFilter(filters.FilterSet):
is_st = filters.BooleanFilter(method='filter_is_st', label='是否停机')
class Meta: class Meta:
model = SfLogExp model = SfLogExp
fields = { fields = {
@ -22,4 +23,12 @@ class SfLogExpFilter(filters.FilterSet):
"sflog__mgroup__name": ["exact"], "sflog__mgroup__name": ["exact"],
"happen_time": ["day", "month", "year"], "happen_time": ["day", "month", "year"],
"sflog__end_time": ["day", "month", "year"], "sflog__end_time": ["day", "month", "year"],
"stlog": ["exact"],
"stlog__start_time": ["day", "month", "year"],
"stlog__end_time": ["day", "month", "year"]
} }
def filter_is_st(self, queryset, name, value):
if value:
return queryset.exclude(stlog=None)
return queryset.filter(stlog=None)

View File

@ -0,0 +1,68 @@
# Generated by Django 3.2.12 on 2023-08-08 06:13
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('wpm', '0009_sflogexp'),
]
operations = [
migrations.AddField(
model_name='sflogexp',
name='duration',
field=models.FloatField(blank=True, null=True, verbose_name='停机时长(h)'),
),
migrations.AddField(
model_name='sflogexp',
name='is_current_down',
field=models.BooleanField(default=False, verbose_name='是否本班停机'),
),
migrations.AddField(
model_name='sflogexp',
name='stlog',
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, to='wpm.stlog', verbose_name='关联停机记录'),
),
migrations.AddField(
model_name='sflogexp',
name='title',
field=models.CharField(default='停机', max_length=20, verbose_name='异常名称'),
preserve_default=False,
),
migrations.AlterField(
model_name='sflog',
name='stlogs',
field=models.ManyToManyField(through='wpm.SfLogExp', to='wpm.StLog', verbose_name='关联停机记录'),
),
migrations.AlterField(
model_name='sflogexp',
name='cate',
field=models.CharField(blank=True, max_length=10, null=True, verbose_name='原因类别'),
),
migrations.AlterField(
model_name='sflogexp',
name='handler',
field=models.CharField(default='', max_length=100, verbose_name='处理人'),
),
migrations.AlterField(
model_name='sflogexp',
name='happen_time',
field=models.DateTimeField(blank=True, null=True, verbose_name='发生时间'),
),
migrations.AlterField(
model_name='sflogexp',
name='measure',
field=models.TextField(default='', max_length=100, verbose_name='处置措施'),
),
migrations.AlterField(
model_name='sflogexp',
name='reason',
field=models.TextField(default='', max_length=100, verbose_name='事件原因'),
),
migrations.DeleteModel(
name='StSfLog',
),
]

View File

@ -25,7 +25,7 @@ class SfLog(CommonADModel):
start_time = models.DateTimeField('值班开始') start_time = models.DateTimeField('值班开始')
end_time = models.DateTimeField('值班结束') end_time = models.DateTimeField('值班结束')
note = models.TextField('其他备注', null=True, blank=True) note = models.TextField('其他备注', null=True, blank=True)
stlogs = models.ManyToManyField('wpm.stlog', verbose_name='关联停机记录', through='wpm.stsflog') stlogs = models.ManyToManyField('wpm.stlog', verbose_name='关联停机记录', through='wpm.sflogexp')
last_test_time = models.DateTimeField('最后质检时间', null=True, blank=True) last_test_time = models.DateTimeField('最后质检时间', null=True, blank=True)
total_hour_now = models.FloatField('总时长动', default=0) total_hour_now = models.FloatField('总时长动', default=0)
shut_hour = models.FloatField('停机时长', default=0) shut_hour = models.FloatField('停机时长', default=0)
@ -39,27 +39,17 @@ class SfLog(CommonADModel):
end_time_local = localtime(self.end_time) end_time_local = localtime(self.end_time)
return end_time_local.year, end_time_local.month, end_time_local.day return end_time_local.year, end_time_local.month, end_time_local.day
class StSfLog(BaseModel):
"""
停机-值班记录关联表
"""
stlog = models.ForeignKey(StLog, verbose_name='关联停机记录', on_delete=models.CASCADE)
sflog = models.ForeignKey(SfLog, verbose_name='关联值班记录', on_delete=models.CASCADE)
is_current_down = models.BooleanField('是否本班停机', default=False)
reason = models.TextField('停机原因', null=True, blank=True)
duration = models.FloatField('停机时长(h)', null=True, blank=True)
class Meta:
ordering = ['sflog__start_time']
class SfLogExp(CommonADModel): class SfLogExp(CommonADModel):
""" """
生产异常情况记录 生产异常情况记录
""" """
sflog = models.ForeignKey(SfLog, on_delete=models.CASCADE, verbose_name='关联值班记录') sflog = models.ForeignKey(SfLog, on_delete=models.CASCADE, verbose_name='关联值班记录')
happen_time = models.DateTimeField('发生时间') stlog = models.ForeignKey(StLog, verbose_name='关联停机记录', on_delete=models.CASCADE, null=True, blank=True)
cate = models.CharField('异常类别', max_length=10) title = models.CharField('异常名称', max_length=20)
reason = models.TextField('原因') happen_time = models.DateTimeField('发生时间', null=True, blank=True)
measure = models.TextField('处置措施') cate = models.CharField('原因类别', max_length=10, null=True, blank=True)
handler = models.CharField('处理人', max_length=100) reason = models.TextField('事件原因', default='', max_length=100)
measure = models.TextField('处置措施', default='', max_length=100)
handler = models.CharField('处理人', default='', max_length=100)
is_current_down = models.BooleanField('是否本班停机', default=False)
duration = models.FloatField('停机时长(h)', null=True, blank=True)

View File

@ -2,7 +2,7 @@ from apps.utils.constants import EXCLUDE_FIELDS
from apps.utils.serializers import CustomModelSerializer from apps.utils.serializers import CustomModelSerializer
from rest_framework import serializers from rest_framework import serializers
from apps.wpm.models import SfLog, StLog, StSfLog, SfLogExp from apps.wpm.models import SfLog, StLog, SfLogExp
from apps.system.models import Dictionary from apps.system.models import Dictionary
from apps.wpm.tasks import cal_enstat_when_pcoal_heat_change, cal_enstat_when_team_change from apps.wpm.tasks import cal_enstat_when_pcoal_heat_change, cal_enstat_when_team_change
@ -46,19 +46,12 @@ class SfLogSerializer(CustomModelSerializer):
# return super().to_internal_value(data) # return super().to_internal_value(data)
class StSfLogSerializer(CustomModelSerializer):
stlog_ = StLogSerializer(source='stlog', read_only=True)
sflog_ = SfLogSerializer(source='sflog', read_only=True)
class Meta:
model = StSfLog
fields = '__all__'
read_only_fields = EXCLUDE_FIELDS + ['stlog', 'sflog', 'is_current_down']
class SflogExpSerializer(CustomModelSerializer): class SflogExpSerializer(CustomModelSerializer):
mgroup = serializers.CharField(source='sflog.mgroup.id', read_only=True) mgroup = serializers.CharField(source='sflog.mgroup.id', read_only=True)
mgroup_name = serializers.CharField(source='sflog.mgroup.name', read_only=True) mgroup_name = serializers.CharField(source='sflog.mgroup.name', read_only=True)
happen_time = serializers.DateTimeField(required=True, label='发生时间')
cate = serializers.CharField(required=True, label='原因类别')
class Meta: class Meta:
model = SfLogExp model = SfLogExp
fields = '__all__' fields = '__all__'
read_only_fields = EXCLUDE_FIELDS read_only_fields = EXCLUDE_FIELDS + ['stlog', 'is_current_down']

View File

@ -1,4 +1,4 @@
from apps.wpm.models import SfLog, StLog, StSfLog from apps.wpm.models import SfLog, SfLogExp
from apps.mtm.models import Shift, Mgroup from apps.mtm.models import Shift, Mgroup
import datetime import datetime
from django.utils.timezone import localtime from django.utils.timezone import localtime

View File

@ -10,7 +10,7 @@ from apps.mtm.models import Mgroup
import datetime import datetime
from django.db.models import Sum from django.db.models import Sum
from apps.wpm.services import make_sflogs from apps.wpm.services import make_sflogs
from apps.wpm.models import SfLog, StLog, StSfLog from apps.wpm.models import SfLog, StLog, SfLogExp
from django.utils.timezone import localtime from django.utils.timezone import localtime
from django.db.models import F from django.db.models import F
@ -76,7 +76,7 @@ def cal_shut_hour(stlogId: str):
is_current_down = False is_current_down = False
if ind == 0: if ind == 0:
is_current_down = True is_current_down = True
stsf, _ = StSfLog.objects.get_or_create(stlog=stlog, sflog=sflog, defaults={'stlog': stlog, 'sflog': sflog, 'is_current_down': is_current_down}) sflogexp, _ = SfLogExp.objects.get_or_create(stlog=stlog, sflog=sflog, defaults={'stlog': stlog, 'sflog': sflog, 'is_current_down': is_current_down, 'title': '停机'})
# 计算duration # 计算duration
sf_end, sf_start = sflog.end_time, sflog.start_time sf_end, sf_start = sflog.end_time, sflog.start_time
duration_item = min(sf_end, st_end) - min(sf_start, st_start) duration_item = min(sf_end, st_end) - min(sf_start, st_start)
@ -84,10 +84,10 @@ def cal_shut_hour(stlogId: str):
duration_item = 0 duration_item = 0
else: else:
duration_item = duration_item.total_seconds()/3600 duration_item = duration_item.total_seconds()/3600
stsf.duration = duration_item sflogexp.duration = duration_item
stsf.save() sflogexp.save()
# 计算每班的总停机时间 # 计算每班的总停机时间
ret = StSfLog.objects.filter(sflog=sflog).aggregate(sum=Sum('duration')) ret = SfLogExp.objects.filter(sflog=sflog).exclude(stlog=None).aggregate(sum=Sum('duration'))
if ret.get('sum', 0): if ret.get('sum', 0):
sflog.shut_hour = ret['sum'] sflog.shut_hour = ret['sum']
sflog.save() sflog.save()

View File

@ -1,7 +1,7 @@
from django.urls import path, include from django.urls import path, include
from rest_framework.routers import DefaultRouter from rest_framework.routers import DefaultRouter
from apps.wpm.views import SfLogViewSet, StLogViewSet, StSfLogViewSet, SfLogExpViewSet from apps.wpm.views import SfLogViewSet, StLogViewSet, SfLogExpViewSet
API_BASE_URL = 'api/wpm/' API_BASE_URL = 'api/wpm/'
@ -10,7 +10,6 @@ HTML_BASE_URL = 'wpm/'
router = DefaultRouter() router = DefaultRouter()
router.register('sflog', SfLogViewSet, basename='sflog') router.register('sflog', SfLogViewSet, basename='sflog')
router.register('stlog', StLogViewSet, basename='stlog') router.register('stlog', StLogViewSet, basename='stlog')
router.register('stsflog', StSfLogViewSet, basename='stsflog')
router.register('sflogexp', SfLogExpViewSet, basename='sflogexp') router.register('sflogexp', SfLogExpViewSet, basename='sflogexp')
urlpatterns = [ urlpatterns = [

View File

@ -7,8 +7,8 @@ from rest_framework.exceptions import ParseError
from rest_framework.mixins import DestroyModelMixin, UpdateModelMixin, ListModelMixin from rest_framework.mixins import DestroyModelMixin, UpdateModelMixin, ListModelMixin
from apps.utils.viewsets import CustomGenericViewSet, CustomModelViewSet from apps.utils.viewsets import CustomGenericViewSet, CustomModelViewSet
from apps.wpm.models import SfLog, StLog, StSfLog, SfLogExp from apps.wpm.models import SfLog, StLog, SfLogExp
from apps.wpm.serializers import SfLogSerializer, StLogSerializer, StSfLogSerializer, SflogExpSerializer from apps.wpm.serializers import SfLogSerializer, StLogSerializer, SflogExpSerializer
from apps.wpm.filters import SfLogFilter, SfLogExpFilter from apps.wpm.filters import SfLogFilter, SfLogExpFilter
from apps.mtm.models import Material from apps.mtm.models import Material
@ -41,25 +41,6 @@ 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):
# 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.exclude(end_time=None).filter(start_time__lte=ins.start_time, end_time__gt=ins.end_time)|
# stls.exclude(end_time=None).filter(end_time__gte=ins.start_time, end_time__lt=ins.end_time))
# for i in stls_:
# StSfLog.objects.get_or_create(stlog=i, sflog=ins, defaults={'stlog': i, 'sflog': ins})
# stsflog = StSfLog.objects.filter(stlog=i).order_by('-sflog__start_time').first()
# if stsflog:
# stsflog.is_current_down = True
# stsflog.save()
# # 计算能耗
# from apps.enm.tasks import cal_sflog_en_val
# cal_sflog_en_val.delay(ins.id)
@action(methods=['get'], detail=True, perms_map={'get': '*'}) @action(methods=['get'], detail=True, perms_map={'get': '*'})
def init_test(self, request, pk=None): def init_test(self, request, pk=None):
""" """
@ -80,25 +61,6 @@ class SfLogViewSet(UpdateModelMixin, ListModelMixin, DestroyModelMixin, CustomGe
sr = QuaStatSerializer(instance=qs, many=True) sr = QuaStatSerializer(instance=qs, many=True)
return Response(sr.data) return Response(sr.data)
class StSfLogViewSet(ListModelMixin, UpdateModelMixin, CustomGenericViewSet):
"""
list:值班停机关系
值班停机关系
"""
perms_map = {'get': '*', 'put': 'sflog.update'}
queryset = StSfLog.objects.all()
serializer_class = StSfLogSerializer
select_related_fields = ['stlog', 'sflog']
filterset_fields = ['stlog', 'sflog']
search_fields = ['reason']
def filter_queryset(self, queryset):
params = self.request.query_params
if 'stlog' not in params or 'sflog' not in params:
raise ParseError('请指定所属停机或值班记录')
return super().filter_queryset(queryset)
class SfLogExpViewSet(CustomModelViewSet): class SfLogExpViewSet(CustomModelViewSet):
""" """
@ -108,5 +70,5 @@ class SfLogExpViewSet(CustomModelViewSet):
""" """
queryset = SfLogExp.objects.all() queryset = SfLogExp.objects.all()
serializer_class = SflogExpSerializer serializer_class = SflogExpSerializer
select_related_fields = ['sflog', 'sflog__mgroup'] select_related_fields = ['sflog', 'sflog__mgroup', 'stlog']
filterset_class = SfLogExpFilter filterset_class = SfLogExpFilter