From f3b43ab0ce80075f42b26c98517dc3b36aa4d23b Mon Sep 17 00:00:00 2001 From: caoqianming Date: Fri, 13 Oct 2023 13:13:31 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20mgroup=20=E5=A2=9E=E5=8A=A0need=5Fenm?= =?UTF-8?q?=E5=AD=97=E6=AE=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/enm/tasks.py | 2 +- apps/mtm/migrations/0015_mgroup_need_enm.py | 18 +++++++++++ apps/mtm/models.py | 1 + apps/wpm/tasks.py | 35 ++++++++++++++------- 4 files changed, 43 insertions(+), 13 deletions(-) create mode 100644 apps/mtm/migrations/0015_mgroup_need_enm.py diff --git a/apps/enm/tasks.py b/apps/enm/tasks.py index 2d18ef5e..b6ebe627 100644 --- a/apps/enm/tasks.py +++ b/apps/enm/tasks.py @@ -223,7 +223,7 @@ def cal_mpointstats(is_now=1, year=None, month=None, day=None, hour=None): cal_mpointstat_hour(item.id, year, month, day, hour) # 开始计算enstat - mgroups = Mgroup.objects.all().order_by('sort') + mgroups = Mgroup.objects.filter(need_enm=True).order_by('sort') # mgroups_group = [] year_s, month_s, day_s = 0, 0, 0 for mgroup in mgroups: diff --git a/apps/mtm/migrations/0015_mgroup_need_enm.py b/apps/mtm/migrations/0015_mgroup_need_enm.py new file mode 100644 index 00000000..6facfa7f --- /dev/null +++ b/apps/mtm/migrations/0015_mgroup_need_enm.py @@ -0,0 +1,18 @@ +# Generated by Django 3.2.12 on 2023-10-13 05:12 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('mtm', '0014_alter_process_options'), + ] + + operations = [ + migrations.AddField( + model_name='mgroup', + name='need_enm', + field=models.BooleanField(default=True, verbose_name='是否进行能源监测'), + ), + ] diff --git a/apps/mtm/models.py b/apps/mtm/models.py index 7d84ebdc..b307cc71 100644 --- a/apps/mtm/models.py +++ b/apps/mtm/models.py @@ -101,6 +101,7 @@ class Mgroup(CommonBModel): test_materials = models.JSONField( '检测材料', default=list, blank=True, help_text='material的ID列表') sort = models.PositiveSmallIntegerField('排序', default=1) + need_enm = models.BooleanField('是否进行能源监测', default=True) is_runing = models.BooleanField('是否正常运行', default=True) class Meta: diff --git a/apps/wpm/tasks.py b/apps/wpm/tasks.py index 4833fdb2..93426f40 100644 --- a/apps/wpm/tasks.py +++ b/apps/wpm/tasks.py @@ -14,8 +14,9 @@ from apps.wpm.models import SfLog, StLog, SfLogExp from django.utils import timezone from django.db.models import F + @shared_task(base=CustomTask) -def make_sflogs_simple(days, state_date:str, end_date:str): +def make_sflogs_simple(days, state_date: str, end_date: str): """ 根据班次规则生成今明两天的排班记录 """ @@ -25,9 +26,10 @@ def make_sflogs_simple(days, state_date:str, end_date:str): else: start_date = datetime.datetime.strptime(state_date, "%Y-%m-%d").date() end_date = datetime.datetime.strptime(end_date, "%Y-%m-%d").date() - mgroups = Mgroup.objects.filter(cate='section').all() + mgroups = Mgroup.objects.filter(cate='section', need_enm=True).all() for mgroup in mgroups: - make_sflogs(mgroup, start_date, end_date) + make_sflogs(mgroup, start_date, end_date) + @shared_task(base=CustomTask) def get_total_hour_now(sflogId: str): @@ -46,13 +48,15 @@ def get_total_hour_now(sflogId: str): sflog.save() return sflog.total_hour_now else: - SfLog.objects.filter(end_time__lte=now).exclude(total_hour_now=12).update(total_hour_now=12) + SfLog.objects.filter(end_time__lte=now).exclude( + total_hour_now=12).update(total_hour_now=12) sf_qs = SfLog.objects.filter(end_time__gt=now) for i in sf_qs: total_hour_now = (now-i.start_time).total_seconds()/3600 i.total_hour_now = total_hour_now if total_hour_now > 0 else 0 i.save() + @shared_task(base=CustomTask) def cal_shut_hour(stlogId: str): """ @@ -68,17 +72,19 @@ def cal_shut_hour(stlogId: str): st_start = stlog.start_time if st_start >= now: break - if stlog.end_time is None: # 说明停机还未结束,此时也需要计算duration + if stlog.end_time is None: # 说明停机还未结束,此时也需要计算duration st_end = now else: st_end = stlog.end_time sf_qs = SfLog.objects.filter(mgroup=stlog.mgroup) - sf_qs = (sf_qs.filter(start_time__gte=st_start, start_time__lt=st_end)|sf_qs.filter(end_time__gt=st_start, end_time__lte=st_end)|sf_qs.filter(start_time__lte=st_start, end_time__gte=st_end)).order_by('start_time').distinct() + sf_qs = (sf_qs.filter(start_time__gte=st_start, start_time__lt=st_end) | sf_qs.filter(end_time__gt=st_start, + end_time__lte=st_end) | sf_qs.filter(start_time__lte=st_start, end_time__gte=st_end)).order_by('start_time').distinct() for ind, sflog in enumerate(sf_qs): is_current_down = False if ind == 0: is_current_down = True - sflogexp, _ = SfLogExp.objects.get_or_create(stlog=stlog, sflog=sflog, defaults={'stlog': stlog, 'sflog': sflog, 'is_current_down': is_current_down, 'title': '停机'}) + sflogexp, _ = SfLogExp.objects.get_or_create(stlog=stlog, sflog=sflog, defaults={ + 'stlog': stlog, 'sflog': sflog, 'is_current_down': is_current_down, 'title': '停机'}) # 计算duration sf_end, sf_start = sflog.end_time, sflog.start_time duration_item_delta = min(sf_end, st_end) - max(sf_start, st_start) @@ -90,7 +96,8 @@ def cal_shut_hour(stlogId: str): sflogexp.duration = duration_item sflogexp.save() # 计算每班的总停机时间 - ret = SfLogExp.objects.filter(sflog=sflog).exclude(stlog=None).aggregate(sum=Sum('duration')) + ret = SfLogExp.objects.filter(sflog=sflog).exclude( + stlog=None).aggregate(sum=Sum('duration')) if ret.get('sum', 0): sflog.shut_hour = ret['sum'] sflog.save() @@ -98,7 +105,8 @@ def cal_shut_hour(stlogId: str): if sflog.end_time > now: get_total_hour_now(sflog.id) if stlogId: - cal_enstat('sflog', sflog.id, sflog.mgroup.id, None, None, None, None, None, None, None, cascade=True, cal_attrs=['run_hour']) + cal_enstat('sflog', sflog.id, sflog.mgroup.id, None, None, None, + None, None, None, None, cascade=True, cal_attrs=['run_hour']) @shared_task(base=CustomTask) @@ -110,10 +118,12 @@ def cal_enstat_when_pcoal_heat_change(sflogId): pcoal_heat = sflog.pcoal_heat year_s, month_s, day_s = sflog.get_ymd # 只会影响到回转窑及水泥磨数据 - enstats = EnStat.objects.filter(mgroup__name='回转窑', year_s=year_s, month_s=month_s, day_s=day_s, type__in = ['hour_s', 'sflog', 'day_s']) + enstats = EnStat.objects.filter(mgroup__name='回转窑', year_s=year_s, + month_s=month_s, day_s=day_s, type__in=['hour_s', 'sflog', 'day_s']) for enstat in enstats: cal_enstat_pcoal_change(enstat, pcoal_heat) - enstats_other = EnStat.objects.filter(mgroup__name='回转窑', year_s=year_s, month_s=month_s, type__in = ['month_st', 'month_s'])|EnStat.objects.filter(mgroup__name='回转窑', year_s=year_s, type__in = ['year_s']) + enstats_other = EnStat.objects.filter(mgroup__name='回转窑', year_s=year_s, month_s=month_s, type__in=[ + 'month_st', 'month_s']) | EnStat.objects.filter(mgroup__name='回转窑', year_s=year_s, type__in=['year_s']) for enstat in enstats_other: cal_enstat_pcoal_change(enstat, pcoal_heat) @@ -124,4 +134,5 @@ def cal_enstat_when_pcoal_heat_change(sflogId): def cal_enstat_when_team_change(sflogId): from apps.enm.tasks import cal_enstat sflog = SfLog.objects.get(id=sflogId) - cal_enstat('month_st', sflogId, sflog.mgroup.id, None, None, None, None, None, None, None, False, []) \ No newline at end of file + cal_enstat('month_st', sflogId, sflog.mgroup.id, None, + None, None, None, None, None, None, False, [])