fix: mgroup 增加need_enm字段

This commit is contained in:
caoqianming 2023-10-13 13:13:31 +08:00
parent ff58685cbc
commit f3b43ab0ce
4 changed files with 43 additions and 13 deletions

View File

@ -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:

View File

@ -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='是否进行能源监测'),
),
]

View File

@ -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:

View File

@ -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, [])
cal_enstat('month_st', sflogId, sflog.mgroup.id, None,
None, None, None, None, None, None, False, [])