feat: cal_enstat_when_team_change

This commit is contained in:
caoqianming 2023-08-03 10:31:38 +08:00
parent 8ab973749d
commit a609b1db94
5 changed files with 40 additions and 22 deletions

View File

@ -0,0 +1,17 @@
# Generated by Django 3.2.12 on 2023-08-03 02:23
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('enm', '0017_rename_pcoal_val_enstat_pcoal_heat'),
]
operations = [
migrations.RemoveField(
model_name='mpointstat',
name='team',
),
]

View File

@ -46,7 +46,6 @@ class MpointStat(CommonADModel):
hour = models.PositiveSmallIntegerField('', null=True, blank=True)
sflog = models.ForeignKey(SfLog, verbose_name='关联值班记录', on_delete=models.CASCADE, null=True, blank=True)
mgroup = models.ForeignKey(Mgroup, verbose_name='关联测点集', on_delete=models.CASCADE, null=True, blank=True)
team = models.ForeignKey(Team, verbose_name='关联班组', on_delete=models.CASCADE, null=True, blank=True)
mpoint = models.ForeignKey(Mpoint, verbose_name='关联测点', on_delete=models.CASCADE)
val = models.FloatField('统计值', default=0)

View File

@ -193,16 +193,6 @@ def cal_mpointstat_manual(mpointId: str, sflogId: str, mgroupId: str, year: int,
ms_day_s.val = sum_dict_day_s['sum']
ms_day_s.save()
if mpoint.ep_monitored:
if mpoint.material and mpoint.material.code == 'elec' and mpoint.ep_monitored.power_kw >= 100: # 统计班月数据使用的
sflog = SfLog.objects.get(id=sflogId)
team = sflog.team
sum_dict_day_st = MpointStat.objects.filter(type='sflog', mpoint=mpoint, year_s=year_s, month_s=month_s, sflog__team=team).aggregate(sum=Sum('val'))
params_day_s = {'type':'month_st', 'mpoint': mpoint, 'year_s': year_s, 'month_s': month_s, 'mgroup': mgroup, 'team': team}
ms_day_s, _ = MpointStat.objects.get_or_create(**params_day_s, defaults=params_day_s)
ms_day_s.val = sum_dict_day_st['sum']
ms_day_s.save()
sum_dict_month_s = MpointStat.objects.filter(type='day_s', mpoint=mpoint, year_s=year_s, month_s=month_s, mgroup=mgroup).aggregate(sum=Sum('val'))
params_month_s = {'type':'month_s', 'mpoint': mpoint, 'year_s': year_s, 'month_s': month_s, 'mgroup': mgroup}
ms_month_s, _ = MpointStat.objects.get_or_create(**params_month_s, defaults=params_month_s)
@ -251,19 +241,21 @@ def cal_enstat(type, sflogId, mgroupId, year, month, day, hour, year_s, month_s,
dt = datetime.datetime(year=year, month=month, day=day, hour=hour, tzinfo=mytz)
sflog = SfLog.objects.get(start_time__lt=dt, end_time__gte=dt, mgroup=mgroup)
team = sflog.team
if team is None and type == 'month_st':
return
if sflog:
year_s, month_s, day_s = sflog.get_ymd
if type == 'hour_s':
enstat, _ = EnStat.objects.get_or_create(type="hour_s", mgroup=mgroup, year=year, month=month, day=day, hour=hour,
defaults={'type': 'hour_s', 'mgroup': mgroup, 'year_s': year_s, 'month_s': month_s, 'day_s': day_s,
'year': year, 'month': month, 'day': day, 'hour': hour, 'sflog': sflog, 'team': team})
'year': year, 'month': month, 'day': day, 'hour': hour, 'sflog': sflog})
elif type == 'sflog':
enstat, _ = EnStat.objects.get_or_create(type="sflog", sflog=sflog,
defaults={'type': 'sflog', 'sflog': sflog, 'mgroup': mgroup, 'year_s': year_s, 'month_s': month_s, 'day_s': day_s})
elif type == 'day_s':
enstat, _ = EnStat.objects.get_or_create(type="day_s", mgroup=mgroup, year_s=year_s, month_s=month_s, day_s=day_s,
defaults={'type': 'day_s', 'mgroup': mgroup, 'year_s': year_s, 'month_s': month_s, 'day_s': day_s})
elif type == 'month_st':
elif type == 'month_st' and team:
enstat, _ = EnStat.objects.get_or_create(type="month_st", mgroup=mgroup, team=team, year_s=year_s, month_s=month_s,
defaults={'type': 'month_st', 'mgroup': mgroup, 'year_s': year_s, 'month_s': month_s, 'team': team})
elif type == 'month_s':
@ -360,8 +352,8 @@ def cal_enstat(type, sflogId, mgroupId, year, month, day, hour, year_s, month_s,
pass
enstat.save()
# 计算一些其他数据
if type == 'month_sf' and 'material' in cal_attrs: # 如果计算的是班月,把主要设备电耗数据拉过来
res = MpointStat.objects.filter(year_s=year_s, month_s=month_s, team=team, mgroup=mgroup, mpoint__ep_monitored__power_kw__gte=100).annotate(
if type == 'month_st' and 'material' in cal_attrs: # 如果计算的是班月,把主要设备电耗数据拉过来
res = MpointStat.objects.filter(type='sflog', year_s=year_s, month_s=month_s, sflog__team=enstat.team, mpoint__ep_monitored__power_kw__gte=100).annotate(
equipment=F('mpoint__ep_monitored__id', equipment_name=F('mpoint__ep_monitored__name')), consume=F('val')).values('equipment', 'equipment_name', 'consume')
res = list(res)
for item in res:
@ -387,11 +379,11 @@ def cal_enstat(type, sflogId, mgroupId, year, month, day, hour, year_s, month_s,
enstat.pcoal_heat = get_pcoal_heat(enstat.year_s, enstat.month_s, enstat.day_s)
enstat.pcoal_coal_consume = enstat.pcoal_consume * enstat.pcoal_heat/7000
elif type == 'month_st':
enstat.pcoal_coal_consume = EnStat.objects.filter(type='day_s', mgroup=enstat.mgroup, year_s=year_s, month_s=month_s, team=enstat.team).aggregate(sum=Sum('pcoal_coal_consume'))['sum']
enstat.pcoal_coal_consume = EnStat.objects.filter(type='sflog', mgroup=enstat.mgroup, year_s=year_s, month_s=month_s, sflog__team=enstat.team).aggregate(sum=Sum('pcoal_coal_consume'))['sum']
elif type == 'month_s':
enstat.pcoal_coal_consume = EnStat.objects.filter(type='day_s', mgroup=enstat.mgroup, year_s=year_s, month_s=month_s).aggregate(sum=Sum('pcoal_coal_consume'))['sum']
enstat.pcoal_coal_consume = EnStat.objects.filter(type='sflog', mgroup=enstat.mgroup, year_s=year_s, month_s=month_s).aggregate(sum=Sum('pcoal_coal_consume'))['sum']
elif type == 'year_s':
enstat.pcoal_coal_consume = EnStat.objects.filter(type='day_s', mgroup=enstat.mgroup, year_s=year_s).aggregate(sum=Sum('pcoal_coal_consume'))['sum']
enstat.pcoal_coal_consume = EnStat.objects.filter(type='sflog', mgroup=enstat.mgroup, year_s=year_s).aggregate(sum=Sum('pcoal_coal_consume'))['sum']
# 算单位产品标煤耗
try:
@ -520,11 +512,11 @@ def cal_enstat_pcoal_change(enstat, new_pcoal_heat):
enstat.pcoal_heat = new_pcoal_heat
enstat.pcoal_coal_consume = enstat.pcoal_consume * enstat.pcoal_heat/7000
elif type == 'month_st':
enstat.pcoal_coal_consume = EnStat.objects.filter(type='day_s', mgroup=enstat.mgroup, year_s=enstat.year_s, month_s=enstat.month_s, team=enstat.team).aggregate(sum=Sum('pcoal_coal_consume'))['sum']
enstat.pcoal_coal_consume = EnStat.objects.filter(type='sflog', mgroup=enstat.mgroup, year_s=enstat.year_s, month_s=enstat.month_s, sflog__team=enstat.team).aggregate(sum=Sum('pcoal_coal_consume'))['sum']
elif type == 'month_s':
enstat.pcoal_coal_consume = EnStat.objects.filter(type='day_s', mgroup=enstat.mgroup, year_s=enstat.year_s, month_s=enstat.month_s).aggregate(sum=Sum('pcoal_coal_consume'))['sum']
enstat.pcoal_coal_consume = EnStat.objects.filter(type='sflog', mgroup=enstat.mgroup, year_s=enstat.year_s, month_s=enstat.month_s).aggregate(sum=Sum('pcoal_coal_consume'))['sum']
elif type == 'year_s':
enstat.pcoal_coal_consume = EnStat.objects.filter(type='day_s', mgroup=enstat.mgroup, year_s=enstat.year_s).aggregate(sum=Sum('pcoal_coal_consume'))['sum']
enstat.pcoal_coal_consume = EnStat.objects.filter(type='sflog', mgroup=enstat.mgroup, year_s=enstat.year_s).aggregate(sum=Sum('pcoal_coal_consume'))['sum']
# 算单位产品标煤耗
try:

View File

@ -4,7 +4,7 @@ from rest_framework import serializers
from apps.wpm.models import SfLog, StLog, StSfLog
from apps.system.models import Dictionary
from apps.wpm.tasks import cal_enstat_when_pcoal_heat_change
from apps.wpm.tasks import cal_enstat_when_pcoal_heat_change, cal_enstat_when_team_change
class StLogSerializer(CustomModelSerializer):
mgroup_name = serializers.CharField(source='mgroup.name', read_only=True)
@ -29,10 +29,14 @@ class SfLogSerializer(CustomModelSerializer):
def update(self, instance, validated_data):
old_pcoal_heat = instance.pcoal_heat
old_team = instance.team
instance = super().update(instance, validated_data)
new_pcoal_heat = instance.pcoal_heat
new_team = instance.team
if new_pcoal_heat != old_pcoal_heat:
cal_enstat_when_pcoal_heat_change.delay(instance.id)
if new_team != old_team:
cal_enstat_when_team_change.delay(instance.id)
return instance
# def to_internal_value(self, data):
# if hasattr(self.Meta, 'update_fields') and self.context['request'].method in ['PUT', 'PATCH']:

View File

@ -116,3 +116,9 @@ def cal_enstat_when_pcoal_heat_change(sflogId):
cal_enstat2(year_s, month_s)
@shared_task(base=CustomTask)
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, [])