feat: enstat参数计算并新建enstat2表

This commit is contained in:
caoqianming 2023-07-28 17:36:44 +08:00
parent 6d5d2f84d4
commit 8e3802966e
4 changed files with 354 additions and 36 deletions

View File

@ -10,6 +10,9 @@ class MpointStatFilter(filters.FilterSet):
"mpoint__mgroup": ["exact"],
"mpoint__material": ["exact"],
"mpoint__mgroup__belong_dept": ["exact"],
"mpoint__ep_belong": ["exact"],
"mpoint__ep_monitored": ["exact"],
"mpoint__ep_monitored__power_kw": ["exact", "gte"],
"sflog": ["exact"],
"hour": ["exact"],
"day": ["exact"],

View File

@ -0,0 +1,115 @@
# Generated by Django 3.2.12 on 2023-07-28 09:05
from django.db import migrations, models
import django.db.models.deletion
import django.utils.timezone
class Migration(migrations.Migration):
dependencies = [
('mtm', '0006_auto_20230725_1357'),
('enm', '0012_auto_20230725_1112'),
]
operations = [
migrations.CreateModel(
name='EnStat2',
fields=[
('id', models.CharField(editable=False, help_text='主键ID', max_length=20, primary_key=True, serialize=False, verbose_name='主键ID')),
('create_time', models.DateTimeField(default=django.utils.timezone.now, help_text='创建时间', verbose_name='创建时间')),
('update_time', models.DateTimeField(auto_now=True, help_text='修改时间', verbose_name='修改时间')),
('is_deleted', models.BooleanField(default=False, help_text='删除标记', verbose_name='删除标记')),
('year_s', models.PositiveSmallIntegerField(blank=True, null=True, verbose_name='班年')),
('month_s', models.PositiveSmallIntegerField(blank=True, null=True, verbose_name='班月')),
('industry_total_val', models.FloatField(blank=True, help_text='万元', null=True, verbose_name='工业总产值')),
('industry_add_val', models.FloatField(blank=True, help_text='万元', null=True, verbose_name='工业增加值')),
('elec_consume', models.FloatField(blank=True, help_text='kw·h', null=True, verbose_name='总电耗')),
('elec_coal_consume', models.FloatField(blank=True, help_text='tce', null=True, verbose_name='电量折标煤')),
('pcoal_consume', models.FloatField(blank=True, help_text='t', null=True, verbose_name='煤粉消耗')),
('pcoal_val', models.FloatField(blank=True, null=True, verbose_name='煤粉热值')),
('pcoal_coal_consume', models.FloatField(blank=True, help_text='tce', null=True, verbose_name='煤粉折标煤')),
('en_consume', models.FloatField(blank=True, help_text='tce', null=True, verbose_name='能源消耗')),
('en_consume_unit', models.FloatField(blank=True, null=True, verbose_name='单位工业总产值能耗')),
('en_add_consume_unit', models.FloatField(blank=True, null=True, verbose_name='单位工业增加值能耗')),
],
options={
'abstract': False,
},
),
migrations.AddField(
model_name='enstat',
name='cair_consume',
field=models.FloatField(blank=True, help_text='m3', null=True, verbose_name='压缩空气'),
),
migrations.AddField(
model_name='enstat',
name='ccr_consume',
field=models.FloatField(blank=True, help_text='t', null=True, verbose_name='电石渣消耗'),
),
migrations.AddField(
model_name='enstat',
name='elec_coal_consume',
field=models.FloatField(blank=True, help_text='tce', null=True, verbose_name='电量折标煤'),
),
migrations.AddField(
model_name='enstat',
name='en_consume_unit',
field=models.FloatField(blank=True, help_text='tce/t', null=True, verbose_name='单位产品能耗'),
),
migrations.AddField(
model_name='enstat',
name='equip_elec_data',
field=models.JSONField(blank=True, default=list, verbose_name='设备电耗数据'),
),
migrations.AddField(
model_name='enstat',
name='kiln_end_heat',
field=models.FloatField(blank=True, help_text='tce', null=True, verbose_name='窑尾余热'),
),
migrations.AddField(
model_name='enstat',
name='out_steam',
field=models.FloatField(blank=True, help_text='t', null=True, verbose_name='外送蒸汽'),
),
migrations.AddField(
model_name='enstat',
name='out_steam_coal',
field=models.FloatField(blank=True, help_text='tce', null=True, verbose_name='外送蒸汽折标煤'),
),
migrations.AddField(
model_name='enstat',
name='pcoal_coal_consume',
field=models.FloatField(blank=True, help_text='tce', null=True, verbose_name='煤粉折标煤'),
),
migrations.AddField(
model_name='enstat',
name='pcoal_consume',
field=models.FloatField(blank=True, help_text='t', null=True, verbose_name='煤粉消耗'),
),
migrations.AddField(
model_name='enstat',
name='shut_hour',
field=models.FloatField(blank=True, help_text='h', null=True, verbose_name='停机时长'),
),
migrations.AddField(
model_name='enstat',
name='total_hour_now',
field=models.FloatField(blank=True, help_text='h', null=True, verbose_name='动态总时长'),
),
migrations.AddField(
model_name='enstat',
name='water_consume',
field=models.FloatField(blank=True, help_text='t', null=True, verbose_name='水消耗'),
),
migrations.AddField(
model_name='mpointstat',
name='team',
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, to='mtm.team', verbose_name='关联班组'),
),
migrations.AlterField(
model_name='mpointstat',
name='type',
field=models.CharField(default='hour', help_text='year/month/day/year_s/month_s/month_st/day_s/sflog/hour_s/hour', max_length=50, verbose_name='统计维度'),
),
]

View File

@ -34,7 +34,7 @@ class MpLog(BaseModel):
class MpointStat(CommonADModel):
"""测点统计表
"""
type = models.CharField('统计维度', max_length=50, default='hour', help_text='year/month/day/year_s/month_s/day_s/sflog/hour_s/hour')
type = models.CharField('统计维度', max_length=50, default='hour', help_text='year/month/day/year_s/month_s/month_st/day_s/sflog/hour_s/hour')
year = models.PositiveSmallIntegerField('', null=True, blank=True)
month = models.PositiveSmallIntegerField('', null=True, blank=True)
day = models.PositiveSmallIntegerField('', null=True, blank=True)
@ -46,6 +46,7 @@ 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)
@ -67,15 +68,45 @@ class EnStat(BaseModel):
day_s = models.PositiveSmallIntegerField('班日', null=True, blank=True)
total_production = models.FloatField('总产量', null=True, blank=True, help_text='t')
elec_consume = models.FloatField('总电耗', null=True, blank=True, help_text='kw·h')
elec_coal_consume = models.FloatField('电量折标煤', null=True, blank=True, help_text='tce')
pcoal_consume = models.FloatField('煤粉消耗', null=True, blank=True, help_text='t')
pcoal_coal_consume = models.FloatField('煤粉折标煤', null=True, blank=True, help_text='tce')
water_consume = models.FloatField('水消耗', null=True, blank=True, help_text='t')
cair_consume = models.FloatField('压缩空气', null=True, blank=True, help_text='m3')
out_steam = models.FloatField('外送蒸汽', null=True, blank=True, help_text='t')
out_steam_coal = models.FloatField('外送蒸汽折标煤', null=True, blank=True, help_text='tce')
ccr_consume = models.FloatField('电石渣消耗', null=True, blank=True, help_text='t')
kiln_end_heat = models.FloatField('窑尾余热', null=True, blank=True, help_text='tce')
imaterial_data = models.JSONField('成本物料数据', default=list, blank=True)
other_cost_data = models.JSONField('其他成本数据', default=list, blank=True)
qua_data = models.JSONField('质检数据', default=list, blank=True)
equip_elec_data = models.JSONField('设备电耗数据', default=list, blank=True)
production_cost_unit = models.FloatField('单位产品成本', null=True, blank=True, help_text='y/t')
elec_consume_unit = models.FloatField('单位产品分布电耗', null=True, blank=True, help_text='kw·h/t')
celec_consume_unit = models.FloatField('单位产品综合电耗', null=True, blank=True, help_text='kw·h/t')
coal_consume_unit = models.FloatField('单位产品标煤耗', null=True, blank=True, help_text='kgce/t')
en_consume_unit = models.FloatField('单位产品能耗', null=True, blank=True, help_text='tce/t')
cen_consume_unit = models.FloatField('单位产品综合能耗', null=True, blank=True, help_text='kgce/t')
production_hour = models.FloatField('台时产量', null=True, blank=True, help_text='t/h')
total_hour_now = models.FloatField('动态总时长', null=True, blank=True, help_text='h')
run_hour = models.FloatField('运转时长', null=True, blank=True, help_text='h')
shut_hour = models.FloatField('停机时长', null=True, blank=True, help_text='h')
run_rate = models.FloatField('运转率', null=True, blank=True, help_text='%')
class EnStat2(BaseModel):
"""
能源数据统计表2
"""
year_s = models.PositiveSmallIntegerField('班年', null=True, blank=True)
month_s = models.PositiveSmallIntegerField('班月', null=True, blank=True)
industry_total_val = models.FloatField('工业总产值', null=True, blank=True, help_text='万元')
industry_add_val = models.FloatField('工业增加值', null=True, blank=True, help_text='万元')
elec_consume = models.FloatField('总电耗', null=True, blank=True, help_text='kw·h')
elec_coal_consume = models.FloatField('电量折标煤', null=True, blank=True, help_text='tce')
pcoal_consume = models.FloatField('煤粉消耗', null=True, blank=True, help_text='t')
pcoal_val = models.FloatField('煤粉热值', null=True, blank=True)
pcoal_coal_consume = models.FloatField('煤粉折标煤', null=True, blank=True, help_text='tce')
en_consume = models.FloatField('能源消耗', null=True, blank=True, help_text='tce')
en_consume_unit = models.FloatField('单位工业总产值能耗', null=True, blank=True)
en_add_consume_unit = models.FloatField('单位工业增加值能耗', null=True, blank=True)

View File

@ -3,12 +3,12 @@ from __future__ import absolute_import, unicode_literals
from apps.utils.tasks import CustomTask
from celery import shared_task, group, chain
from apps.utils.sql import DbConnection
from server.settings import get_sysconfig
from server.settings import get_sysconfig, update_sysconfig
from django.core.cache import cache
from apps.enm.models import MpLog, Mpoint, MpointStat, EnStat
from apps.wpm.models import SfLog
from apps.wpm.models import SfLog, StLog
import datetime
from django.db.models import Sum
from django.db.models import Sum, Avg
from dateutil import tz
from django.conf import settings
from apps.wpm.services import make_sflogs
@ -18,6 +18,8 @@ from apps.fim.models import Fee
from django.core.cache import cache
from apps.enm.services import translate_eval_formula
import logging
from django.db.models import F
from apps.wpm.services import get_pcoal_val
myLogger = logging.getLogger('log')
def get_current_and_previous_time():
@ -30,14 +32,14 @@ def get_current_and_previous_time():
def get_tag_val():
config = get_sysconfig()
with DbConnection(config['enm']['db_host'], config['enm']['db_user'], config['enm']['db_password'], config['enm']['db_database']) as cursor:
last_tag_id = cache.get('last_tag_id')
if last_tag_id is None:
last_tag_id = config['enm'].get('last_tag_id', None)
if not last_tag_id:
mr = MpLog.objects.all().order_by('-tag_update', 'tag_id').first()
if mr is None:
last_tag_id = 0
else:
last_tag_id = mr.tag_id
cache.set('last_tag_id', last_tag_id)
update_sysconfig({'enm': {'last_tag_id': last_tag_id}})
cursor.execute("select id, val, tag_code, update_time from tag_value where id > %s order by id", (last_tag_id))
results = cursor.fetchall() # 获取数据后保存至本地
for row in results:
@ -46,7 +48,7 @@ def get_tag_val():
mr_one.mpoint, _ = Mpoint.objects.get_or_create(code=mr_one.tag_code, defaults={'name': mr_one.tag_code, 'code': mr_one.tag_code, 'unit': 'unknown'})
mr_one.save()
last_tag_id = mr_one.tag_id
cache.set('last_tag_id', last_tag_id)
update_sysconfig({'enm': {'last_tag_id': last_tag_id}})
@shared_task(base=CustomTask)
def cal_mpointstat_hour(mpointId: str, year: int, month: int, day: int, hour: int):
@ -176,6 +178,16 @@ 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)
@ -191,46 +203,58 @@ def cal_mpointstat_manual(mpointId: str, sflogId: str, mgroupId: str, year: int,
if next_cal: # 二次计算
if hour:
compute_enstat('hour_s', sflogId, mgroupId, year, month, day, hour, year_s, month_s, day_s)
compute_enstat('sflog', sflogId, mgroupId, year, month, day, hour, year_s, month_s, day_s)
compute_enstat('day_s', sflogId, mgroupId, year, month, day, hour, year_s, month_s, day_s)
compute_enstat('month_st', sflogId, mgroupId, year, month, day, hour, year_s, month_s, day_s)
compute_enstat('month_s', sflogId, mgroupId, year, month, day, hour, year_s, month_s, day_s)
compute_enstat('year_s', sflogId, mgroupId, year, month, day, hour, year_s, month_s, day_s)
else:
compute_enstat('sflog', sflogId, mgroupId, year, month, day, hour, year_s, month_s, day_s)
def compute_enstat(type, sflogId, mgroupId, year, month, day, hour, year_s, month_s, day_s):
types_list = ['hour_s', 'sflog', 'day_s', 'month_st', 'month_s', 'year_s']
def compute_enstat(type, sflogId, mgroupId, year, month, day, hour, year_s, month_s, day_s, cascade=True):
"""
计算能源数据统计
"""
if cascade:
if type in types_list:
start_index = types_list.index(type)
new_types_list = types_list[start_index:]
for type in new_types_list:
compute_enstat(type, sflogId, mgroupId, year, month, day, hour, year_s, month_s, day_s, False)
return
mgroup = Mgroup.objects.get(id=mgroupId)
sflog = SfLog.objects.get(id=sflogId)
team = sflog.team
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,
'total_production': 0, 'elec_consume': 0})
'year': year, 'month': month, 'day': day, 'hour': hour, 'sflog': sflog, 'team': team})
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, 'total_production': 0, 'elec_consume': 0})
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, 'total_production': 0, 'elec_consume': 0})
defaults={'type': 'day_s', 'mgroup': mgroup, 'year_s': year_s, 'month_s': month_s, 'day_s': day_s})
elif type == 'month_st':
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, 'total_production': 0, 'elec_consume': 0})
defaults={'type': 'month_st', 'mgroup': mgroup, 'year_s': year_s, 'month_s': month_s, 'team': team})
elif type == 'month_s':
enstat, _ = EnStat.objects.get_or_create(type="month_s", mgroup=mgroup, year_s=year_s, month_s=month_s,
defaults={'type': 'month_s', 'mgroup': mgroup, 'year_s': year_s, 'month_s': month_s, 'total_production': 0, 'elec_consume': 0})
defaults={'type': 'month_s', 'mgroup': mgroup, 'year_s': year_s, 'month_s': month_s})
elif type == 'year_s':
enstat, _ = EnStat.objects.get_or_create(type="year_s", mgroup=mgroup, year_s=year_s,
defaults={'type': 'year_s', 'mgroup': mgroup, 'year_s': year_s, 'total_production': 0, 'elec_consume': 0})
defaults={'type': 'year_s', 'mgroup': mgroup, 'year_s': year_s})
# 物料统计
# 消耗物料统计(包括电耗)
input_materials = []
has_product = False
input_materials = mgroup.input_materials
if mgroup.product:
input_materials.insert(0, mgroup.product.id)
cost_unit_total = 0
has_product = True
imaterial_cost_unit = 0
imaterial_data = []
for ind, mid in enumerate(input_materials):
material = Material.objects.get(id=mid)
@ -251,30 +275,175 @@ def compute_enstat(type, sflogId, mgroupId, year, month, day, hour, year_s, mont
amount_consume = mps.aggregate(sum=Sum('val'))['sum']
if amount_consume is None:
amount_consume = 0
if ind == 0: # 如果是产量
if ind == 0 and has_product: # 如果是产量
enstat.total_production = amount_consume
enstat.save()
else:
price_unit = get_price_unit(material, year_s, month_s)
if material.code in ['pcoal', 'cair', 'steam']:
price_unit = 0
else:
price_unit = get_price_unit(material, year_s, month_s)
cost = amount_consume * price_unit
try:
cost_unit = cost/ enstat.total_production
except:
cost_unit = 0
cost_unit_total = cost_unit_total + cost_unit
imaterial_cost_unit = imaterial_cost_unit + cost_unit
if material.code == 'elec':
enstat.elec_consume = amount_consume
enstat.save()
imaterial_item = {'material': mid, 'material_name': material.name, 'material_type': material.type, 'price_unit': price_unit, 'amount_consume': amount_consume, 'cost': cost, 'cost_unit': cost_unit}
enstat.elec_coal_consume = enstat.elec_consume*0.1229/1000
try:
enstat.elec_consume_unit = enstat.elec_consume/enstat.total_production
except:
pass
elif material.code == 'water':
enstat.water = amount_consume
elif material.code == 'pcoal':
enstat.pcoal_consume = amount_consume
elif material.code == 'cair':
enstat.cair_consume = amount_consume
elif material.code == 'steam':
enstat.out_steam = amount_consume
enstat.out_steam_coal = enstat.out_steam * 128.6 / 1000
elif material.code == 'ccr':
enstat.ccr_consume = amount_consume
try:
enstat.kiln_end_heat = enstat.total_production - enstat.ccr_consume
except:
pass
imaterial_item = {'material': mid, 'material_name': material.name, 'material_code': material.code, 'material_type': material.type, 'price_unit': price_unit, 'amount_consume': amount_consume, 'cost': cost, 'cost_unit': cost_unit}
imaterial_data.append(imaterial_item)
enstat.imaterial_data = imaterial_data
enstat.save()
enstat.imaterial_data = imaterial_data
# 其他成本数据
other_cost_data = []
for fee in Fee.objects.order_by('sort'):
other_cost_unit = 0
fee_qs = Fee.objects.order_by('sort')
for fee in fee_qs:
item = {'element': fee.element, 'cate': fee.cate, 'name': fee.name, 'id': fee.id}
item['cost_unit'] = get_cost_unit(mgroup, fee, year_s, month_s)
cost_unit_total = cost_unit_total + item['cost_unit']
other_cost_unit = other_cost_unit + item['cost_unit']
other_cost_data.append(item)
enstat.other_cost_data = other_cost_data
enstat.production_cost_unit = cost_unit_total
enstat.save()
enstat.production_cost_unit = imaterial_cost_unit + other_cost_unit
if enstat.mgroup.type == 'section':
# 算能耗
if enstat.mgroup.name != '回转窑':
try:
enstat.en_consume_unit = enstat.elec_coal_consume / enstat.total_production
except:
pass
# 计算一些其他数据
if type == 'month_sf': # 如果计算的是班月,把主要设备电耗数据拉过来
res = MpointStat.objects.filter(year_s=year_s, month_s=month_s, team=team, mgroup=mgroup, 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:
try:
item['consume_unit'] = item['consume'] / enstat.total_production
except:
pass
enstat.equip_elec_data = res
if enstat.mgroup.name == '回转窑': # 算单位产品(综合电耗/标煤耗/综合能耗)
# 综合电耗
if enstat.type in ['hour_s', 'day_s', 'year_s', 'month_s']:
pre_enstat = EnStat.objects.filter(type=enstat.type, year_s=enstat.year_s, month_s=enstat.month_s, day_s=enstat.day_s, hour=enstat.hour, mgroup__name='原料磨').first()
if pre_enstat:
try:
enstat.celec_consume_unit = enstat.elec_consume_unit + 1.45*pre_enstat.elec_consume_unit
except:
pass
# 算标煤耗
if type in ['hour_s', 'day_s']:
pcoal_val = get_pcoal_val(enstat.year_s, enstat.month_s, enstat.day_s)
if pcoal_val:
try:
enstat.pcoal_coal_consume = enstat.pcoal_consume * pcoal_val/7000
enstat.coal_consume_unit = enstat.pcoal_coal_consume /enstat.total_production
except:
pass
elif type == 'month_st':
try:
enstat.coal_consume_unit = EnStat.objects.filter(type='day_s', mgroup=enstat.mgroup, year_s=year_s, month_s=month_s, team=enstat.team).annotate(avg=Avg('coal_consume_unit'))['avg']
except:
pass
elif type == 'month_s':
try:
enstat.coal_consume_unit = EnStat.objects.filter(type='day_s', mgroup=enstat.mgroup, year_s=year_s, month_s=month_s).annotate(avg=Avg('coal_consume_unit'))['avg']
except:
pass
elif type == 'year_s':
try:
enstat.coal_consume_unit = EnStat.objects.filter(type='day_s', mgroup=enstat.mgroup, year_s=year_s).annotate(avg=Avg('coal_consume_unit'))['avg']
except:
pass
# 综合能耗
try:
enstat.cen_consume_unit = enstat.coal_consume_unit + 0.1229 * enstat.elec_consume_unit
except:
pass
if enstat.mgroup.name == '水泥磨' and enstat.type not in ['month_st', 'sflog']:
pre_enstat = EnStat.objects.filter(type=enstat.type, year_s=enstat.year_s, month_s=enstat.month_s, day_s=enstat.day_s, hour=enstat.hour, mgroup__name='回转窑').first()
if pre_enstat:
try:
enstat.cen_consume_unit = enstat.elec_consume_unit*0.1229 + 0.7*pre_enstat.cen_consume_unit
except:
pass
# 运转时长相关
if type != 'hour_s':
enstat.total_hour_now, enstat.shut_hour = get_total_hour_now_and_shut_hour(enstat)
enstat.run_hour = enstat.total_hour_now - enstat.shut_hour
enstat.run_rate = (enstat.run_hour / enstat.total_hour_now)*100
enstat.production_hour = enstat.total_production / enstat.total_hour_now
enstat.save()
def get_total_hour_now_and_shut_hour(enstat: EnStat):
from apps.wpm.models import SfLog
# if enstat.type == 'hour_s':
# # 找到停机记录,并划分到该小时
# end_time = datetime.datetime(enstat.year, enstat.month, enstat.day, enstat.hour)
# start_time = end_time - datetime.timedelta(hours=1)
# sts = StLog.objects.filter(mgroup=enstat.mgroup)
# sts = (sts.filter(start_time__gt=start_time, start_time__lt=end_time)|sts.filter(start_time__lt=start_time, end_time=None)|sts.filter(end_time__gt=start_time, end_time__lt=end_time)).distinct()
# shut_hour = 0
# for i in sts:
# if i.end_time is None:
# run_hour = 0
# if i.start_time > start_time:
# run_hour = (i.start_time - start_time).total_seconds/3600
# shut_hour = 1 - run_hour
# shut_hour = shut_hour +
# return 1, 0
if enstat.type == 'sflog':
sflog = enstat.sflog
return sflog.total_hour_now, sflog.shut_hour
elif enstat.type == 'day_s':
res = SfLog.objects.filter(end_time__year=enstat.year_s, end_time__month=enstat.month_s, end_time__day=enstat.day_s, mgroup=enstat.mgroup).annotate(
sum1 = Sum('total_hour_now'),
sum2 = Sum('shut_hour')
)
return res['sum1'] if res['sum1'] else 0, res['sum2'] if res['sum2'] else 0
elif enstat.type == 'month_st':
res = SfLog.objects.filter(end_time__year=enstat.year_s, end_time__month=enstat.month_s, mgroup=enstat.mgroup, team=enstat.team).annotate(
sum1 = Sum('total_hour_now'),
sum2 = Sum('shut_hour')
)
return res['sum1'] if res['sum1'] else 0, res['sum2'] if res['sum2'] else 0
elif enstat.type == 'month_s':
res = SfLog.objects.filter(end_time__year=enstat.year_s, end_time__month=enstat.month_s, mgroup=enstat.mgroup).annotate(
sum1 = Sum('total_hour_now'),
sum2 = Sum('shut_hour')
)
return res['sum1'] if res['sum1'] else 0, res['sum2'] if res['sum2'] else 0
elif enstat.type == 'year_s':
res = SfLog.objects.filter(end_time__year=enstat.year_s, mgroup=enstat.mgroup).annotate(
sum1 = Sum('total_hour_now'),
sum2 = Sum('shut_hour')
)
return res['sum1'] if res['sum1'] else 0, res['sum2'] if res['sum2'] else 0