feat: 增加enstat 统计数据,优化task执行

This commit is contained in:
caoqianming 2023-08-02 10:22:29 +08:00
parent ab581f2851
commit 5402f2181c
5 changed files with 556 additions and 185 deletions

View File

@ -0,0 +1,57 @@
# Generated by Django 3.2.12 on 2023-07-31 05:26
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('enm', '0013_auto_20230728_1705'),
]
operations = [
migrations.RemoveField(
model_name='enstat2',
name='pcoal_val',
),
migrations.AddField(
model_name='enstat2',
name='bag_cement_price',
field=models.FloatField(blank=True, null=True, verbose_name='袋装水泥价格'),
),
migrations.AddField(
model_name='enstat2',
name='bag_cement_val',
field=models.FloatField(blank=True, null=True, verbose_name='袋装水泥发运量'),
),
migrations.AddField(
model_name='enstat2',
name='bulk_cement_price',
field=models.FloatField(blank=True, null=True, verbose_name='散装水泥价格'),
),
migrations.AddField(
model_name='enstat2',
name='bulk_cement_val',
field=models.FloatField(blank=True, null=True, verbose_name='散装水泥发运量'),
),
migrations.AddField(
model_name='enstat2',
name='cement_cost_unit',
field=models.FloatField(blank=True, null=True, verbose_name='水泥单位成本'),
),
migrations.AddField(
model_name='enstat2',
name='cement_val',
field=models.FloatField(blank=True, null=True, verbose_name='水泥产量'),
),
migrations.AddField(
model_name='enstat2',
name='clinker_price',
field=models.FloatField(blank=True, null=True, verbose_name='散装熟料价格'),
),
migrations.AddField(
model_name='enstat2',
name='clinker_val',
field=models.FloatField(blank=True, null=True, verbose_name='散装熟料发运量'),
),
]

View File

@ -0,0 +1,226 @@
# Generated by Django 3.2.12 on 2023-08-01 07:38
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('enm', '0014_auto_20230731_1326'),
]
operations = [
migrations.AddField(
model_name='enstat',
name='pcoal_val',
field=models.FloatField(default=0, verbose_name='煤粉热值'),
),
migrations.AlterField(
model_name='enstat',
name='cair_consume',
field=models.FloatField(default=0, help_text='m3', verbose_name='压缩空气'),
),
migrations.AlterField(
model_name='enstat',
name='ccr_consume',
field=models.FloatField(default=0, help_text='t', verbose_name='电石渣消耗'),
),
migrations.AlterField(
model_name='enstat',
name='celec_consume_unit',
field=models.FloatField(default=0, help_text='kw·h/t', verbose_name='单位产品综合电耗'),
),
migrations.AlterField(
model_name='enstat',
name='cen_consume_unit',
field=models.FloatField(default=0, help_text='kgce/t', verbose_name='单位产品综合能耗'),
),
migrations.AlterField(
model_name='enstat',
name='coal_consume_unit',
field=models.FloatField(default=0, help_text='kgce/t', verbose_name='单位产品标煤耗'),
),
migrations.AlterField(
model_name='enstat',
name='elec_coal_consume',
field=models.FloatField(default=0, help_text='tce', verbose_name='电量折标煤'),
),
migrations.AlterField(
model_name='enstat',
name='elec_consume',
field=models.FloatField(default=0, help_text='kw·h', verbose_name='总电耗'),
),
migrations.AlterField(
model_name='enstat',
name='elec_consume_unit',
field=models.FloatField(default=0, help_text='kw·h/t', verbose_name='单位产品分布电耗'),
),
migrations.AlterField(
model_name='enstat',
name='en_consume_unit',
field=models.FloatField(default=0, help_text='tce/t', verbose_name='单位产品能耗'),
),
migrations.AlterField(
model_name='enstat',
name='kiln_end_heat',
field=models.FloatField(default=0, help_text='tce', verbose_name='窑尾余热'),
),
migrations.AlterField(
model_name='enstat',
name='out_steam',
field=models.FloatField(default=0, help_text='t', verbose_name='外送蒸汽'),
),
migrations.AlterField(
model_name='enstat',
name='out_steam_coal',
field=models.FloatField(default=0, help_text='tce', verbose_name='外送蒸汽折标煤'),
),
migrations.AlterField(
model_name='enstat',
name='pcoal_coal_consume',
field=models.FloatField(default=0, help_text='tce', verbose_name='煤粉折标煤'),
),
migrations.AlterField(
model_name='enstat',
name='pcoal_consume',
field=models.FloatField(default=0, help_text='t', verbose_name='煤粉消耗'),
),
migrations.AlterField(
model_name='enstat',
name='production_cost_unit',
field=models.FloatField(default=0, help_text='y/t', verbose_name='单位产品成本'),
),
migrations.AlterField(
model_name='enstat',
name='production_hour',
field=models.FloatField(default=0, help_text='t/h', verbose_name='台时产量'),
),
migrations.AlterField(
model_name='enstat',
name='run_hour',
field=models.FloatField(default=0, help_text='h', verbose_name='运转时长'),
),
migrations.AlterField(
model_name='enstat',
name='shut_hour',
field=models.FloatField(default=0, help_text='h', verbose_name='停机时长'),
),
migrations.AlterField(
model_name='enstat',
name='total_hour_now',
field=models.FloatField(default=0, help_text='h', verbose_name='动态总时长'),
),
migrations.AlterField(
model_name='enstat',
name='total_production',
field=models.FloatField(default=0, help_text='t', verbose_name='总产量'),
),
migrations.AlterField(
model_name='enstat',
name='water_consume',
field=models.FloatField(default=0, help_text='t', verbose_name='水消耗'),
),
migrations.AlterField(
model_name='enstat',
name='year',
field=models.PositiveSmallIntegerField(default=2023, verbose_name=''),
preserve_default=False,
),
migrations.AlterField(
model_name='enstat2',
name='bag_cement_price',
field=models.FloatField(default=0, verbose_name='袋装水泥价格'),
),
migrations.AlterField(
model_name='enstat2',
name='bag_cement_val',
field=models.FloatField(default=0, verbose_name='袋装水泥发运量'),
),
migrations.AlterField(
model_name='enstat2',
name='bulk_cement_price',
field=models.FloatField(default=0, verbose_name='散装水泥价格'),
),
migrations.AlterField(
model_name='enstat2',
name='bulk_cement_val',
field=models.FloatField(default=0, verbose_name='散装水泥发运量'),
),
migrations.AlterField(
model_name='enstat2',
name='cement_cost_unit',
field=models.FloatField(default=0, verbose_name='水泥单位成本'),
),
migrations.AlterField(
model_name='enstat2',
name='cement_val',
field=models.FloatField(default=0, verbose_name='水泥产量'),
),
migrations.AlterField(
model_name='enstat2',
name='clinker_price',
field=models.FloatField(default=0, verbose_name='散装熟料价格'),
),
migrations.AlterField(
model_name='enstat2',
name='clinker_val',
field=models.FloatField(default=0, verbose_name='散装熟料发运量'),
),
migrations.AlterField(
model_name='enstat2',
name='elec_coal_consume',
field=models.FloatField(default=0, help_text='tce', verbose_name='电量折标煤'),
),
migrations.AlterField(
model_name='enstat2',
name='elec_consume',
field=models.FloatField(default=0, help_text='kw·h', verbose_name='总电耗'),
),
migrations.AlterField(
model_name='enstat2',
name='en_add_consume_unit',
field=models.FloatField(default=0, verbose_name='单位工业增加值能耗'),
),
migrations.AlterField(
model_name='enstat2',
name='en_consume',
field=models.FloatField(default=0, help_text='tce', verbose_name='能源消耗'),
),
migrations.AlterField(
model_name='enstat2',
name='en_consume_unit',
field=models.FloatField(default=0, verbose_name='单位工业总产值能耗'),
),
migrations.AlterField(
model_name='enstat2',
name='industry_add_val',
field=models.FloatField(default=0, help_text='万元', verbose_name='工业增加值'),
),
migrations.AlterField(
model_name='enstat2',
name='industry_total_val',
field=models.FloatField(default=0, help_text='万元', verbose_name='工业总产值'),
),
migrations.AlterField(
model_name='enstat2',
name='month_s',
field=models.PositiveSmallIntegerField(default=1, verbose_name='班月'),
preserve_default=False,
),
migrations.AlterField(
model_name='enstat2',
name='pcoal_coal_consume',
field=models.FloatField(default=0, help_text='tce', verbose_name='煤粉折标煤'),
),
migrations.AlterField(
model_name='enstat2',
name='pcoal_consume',
field=models.FloatField(default=0, help_text='t', verbose_name='煤粉消耗'),
),
migrations.AlterField(
model_name='enstat2',
name='year_s',
field=models.PositiveSmallIntegerField(default=2023, verbose_name='班年'),
preserve_default=False,
),
]

View File

@ -0,0 +1,23 @@
# Generated by Django 3.2.12 on 2023-08-01 07:49
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('enm', '0015_auto_20230801_1538'),
]
operations = [
migrations.AlterField(
model_name='enstat',
name='run_rate',
field=models.FloatField(default=0, help_text='%', verbose_name='运转率'),
),
migrations.AlterField(
model_name='enstat',
name='year',
field=models.PositiveSmallIntegerField(blank=True, null=True, verbose_name=''),
),
]

View File

@ -1,10 +1,10 @@
from django.db import models
from apps.utils.models import BaseModel, CommonBModel, CommonADModel
from apps.utils.models import BaseModel, CommonBModel, CommonADModel, CommonBDModel
from apps.wpm.models import SfLog
from apps.mtm.models import Material, Mgroup, Team
class Mpoint(CommonBModel):
class Mpoint(CommonBDModel):
"""测点
"""
name = models.CharField('测点名称', max_length=50)
@ -66,47 +66,55 @@ class EnStat(BaseModel):
year_s = models.PositiveSmallIntegerField('班年', null=True, blank=True)
month_s = models.PositiveSmallIntegerField('班月', null=True, blank=True)
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')
total_production = models.FloatField('总产量', default=0, help_text='t')
elec_consume = models.FloatField('总电耗', default=0, help_text='kw·h')
elec_coal_consume = models.FloatField('电量折标煤', default=0, help_text='tce')
pcoal_val = models.FloatField('煤粉热值', default=0)
pcoal_consume = models.FloatField('煤粉消耗', default=0, help_text='t')
pcoal_coal_consume = models.FloatField('煤粉折标煤', default=0, help_text='tce')
water_consume = models.FloatField('水消耗', default=0, help_text='t')
cair_consume = models.FloatField('压缩空气', default=0, help_text='m3')
out_steam = models.FloatField('外送蒸汽', default=0, help_text='t')
out_steam_coal = models.FloatField('外送蒸汽折标煤', default=0, help_text='tce')
ccr_consume = models.FloatField('电石渣消耗', default=0, help_text='t')
kiln_end_heat = models.FloatField('窑尾余热', default=0, 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='%')
production_cost_unit = models.FloatField('单位产品成本', default=0, help_text='y/t')
elec_consume_unit = models.FloatField('单位产品分布电耗', default=0, help_text='kw·h/t')
celec_consume_unit = models.FloatField('单位产品综合电耗', default=0, help_text='kw·h/t')
coal_consume_unit = models.FloatField('单位产品标煤耗', default=0, help_text='kgce/t')
en_consume_unit = models.FloatField('单位产品能耗', default=0, help_text='tce/t')
cen_consume_unit = models.FloatField('单位产品综合能耗', default=0, help_text='kgce/t')
production_hour = models.FloatField('台时产量', default=0, help_text='t/h')
total_hour_now = models.FloatField('动态总时长', default=0, help_text='h')
run_hour = models.FloatField('运转时长', default=0, help_text='h')
shut_hour = models.FloatField('停机时长', default=0, help_text='h')
run_rate = models.FloatField('运转率', default=0, 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)
year_s = models.PositiveSmallIntegerField('班年')
month_s = models.PositiveSmallIntegerField('班月')
industry_total_val = models.FloatField('工业总产值', default=0, help_text='万元')
industry_add_val = models.FloatField('工业增加值', default=0, help_text='万元')
elec_consume = models.FloatField('总电耗', default=0, help_text='kw·h')
elec_coal_consume = models.FloatField('电量折标煤', default=0, help_text='tce')
pcoal_consume = models.FloatField('煤粉消耗', default=0, help_text='t')
pcoal_coal_consume = models.FloatField('煤粉折标煤', default=0, help_text='tce')
bulk_cement_val =models.FloatField('散装水泥发运量', default=0)
bulk_cement_price = models.FloatField('散装水泥价格', default=0)
bag_cement_val = models.FloatField('袋装水泥发运量', default=0)
bag_cement_price = models.FloatField('袋装水泥价格', default=0)
clinker_val = models.FloatField('散装熟料发运量', default=0)
clinker_price = models.FloatField('散装熟料价格', default=0)
cement_val = models.FloatField('水泥产量', default=0)
cement_cost_unit = models.FloatField('水泥单位成本', default=0)
en_consume = models.FloatField('能源消耗', default=0, help_text='tce')
en_consume_unit = models.FloatField('单位工业总产值能耗', default=0)
en_add_consume_unit = models.FloatField('单位工业增加值能耗', default=0)

View File

@ -5,7 +5,7 @@ from celery import shared_task, group, chain
from apps.utils.sql import DbConnection
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.enm.models import MpLog, Mpoint, MpointStat, EnStat, EnStat2
from apps.wpm.models import SfLog, StLog
import datetime
from django.db.models import Sum, Avg
@ -20,6 +20,7 @@ from apps.enm.services import translate_eval_formula
import logging
from django.db.models import F
from apps.wpm.services import get_pcoal_val
import traceback
myLogger = logging.getLogger('log')
def get_current_and_previous_time():
@ -106,10 +107,10 @@ def cal_mpointstat_hour(mpointId: str, year: int, month: int, day: int, hour: in
mgroup = Mgroup.objects.get(id=allocate['mgroup'])
ratio = allocate['ratio']
# 查找并绑定值班记录
sflog = SfLog.objects.filter(start_time__lte=dt, end_time__gt=dt, mgroup=mgroup).first()
sflog = SfLog.objects.filter(start_time__lt=dt, end_time__gte=dt, mgroup=mgroup).first()
if sflog is None: # 需要创建值班记录
make_sflogs(mgroup=mgroup, start_date=(dt-datetime.timedelta(days=1)).date(), end_date=dt.date())
sflog = SfLog.objects.filter(start_time__lte=dt, end_time__gt=dt, mgroup=mgroup).first()
sflog = SfLog.objects.filter(start_time__lt=dt, end_time__gte=dt, mgroup=mgroup).first()
year_s, month_s, day_s = sflog.get_ymd
params_hour_s = {'type': 'hour_s', 'mpoint': mpoint, 'sflog': sflog, 'mgroup': mgroup, 'year': year, 'month': month, 'day': day, 'year_s': year_s, 'month_s': month_s, 'day_s': day_s, 'hour': hour}
@ -125,13 +126,13 @@ def cal_mpointstat_hour(mpointId: str, year: int, month: int, day: int, hour: in
ms_sflog_s.val = sum_dict_sflog_s['sum']
ms_sflog_s.save()
next_cal_dict = [mpoint.material.id, sflog.id, year, month, day, hour, year_s, month_s, day_s]
if next_cal_dict == cache.get('enm_cal_dict', None):
next_cal = 0
else:
next_cal = 1
cache.set('enm_cal_dict', next_cal_dict, 60)
cal_mpointstat_manual(mpoint.id, sflog.id, mgroup.id, year, month, day, hour, year_s, month_s, day_s, next_cal)
# next_cal_dict = [mpoint.material.id, sflog.id, year, month, day, hour, year_s, month_s, day_s]
# if next_cal_dict == cache.get('enm_cal_dict', None):
# next_cal = 0
# else:
# next_cal = 1
# cache.set('enm_cal_dict', next_cal_dict, 60)
cal_mpointstat_manual(mpoint.id, sflog.id, mgroup.id, year, month, day, hour, year_s, month_s, day_s, 0)
@ -149,20 +150,23 @@ def cal_mpointstats(is_now=1, year=None, month=None, day=None, hour=None):
else:
year, month, day, hour = pre.year, pre.month, pre.day, pre.hour
mgroups = Mgroup.objects.exclude(product=None).order_by('sort') # 必须要进行排序, 因为有的产量是经过计算而得的
# 先统计自动采集的产量值
caled_mpointids = []
for mgroup in mgroups:
product = mgroup.product
mpoints = Mpoint.objects.filter(material=product, is_auto=True)
for ind, item in enumerate(mpoints):
caled_mpointids.append(item.id)
cal_mpointstat_hour(item.id, year, month, day, hour)
# 先统计不带公式的测点
mpoints_without_formula = Mpoint.objects.filter(is_auto=True, formula='')
for item in mpoints_without_formula:
cal_mpointstat_hour(item.id, year, month, day, hour)
# 统计其他测点
mpoints = Mpoint.objects.filter(is_auto=True).exclude(id__in=caled_mpointids).order_by('material', 'mgroup')
for i in mpoints:
cal_mpointstat_hour(i.id, year, month, day, hour)
# 再统计其他测点
mpoints_other = Mpoint.objects.filter(is_auto=True).exclude(formula='')
for item in mpoints_other:
cal_mpointstat_hour(item.id, year, month, day, hour)
# 开始计算enstat
mgroups = Mgroup.objects.all().order_by('sort')
for mgroup in mgroups:
cal_enstat('hour_s', None, mgroup.id, year, month, day, hour, None, None, None, True, [])
# 最后计算enstat2
cal_enstat2(year_s=year, month_s=month)
@shared_task(base=CustomTask)
@ -202,15 +206,15 @@ 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)
cal_enstat('hour_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)
cal_enstat('sflog', 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):
def cal_enstat(type, sflogId, mgroupId, year, month, day, hour, year_s, month_s, day_s, cascade=True, cal_attrs=[]):
"""
计算能源数据统计
"""
@ -219,11 +223,21 @@ def compute_enstat(type, sflogId, mgroupId, year, month, day, hour, year_s, mont
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)
cal_enstat(type, sflogId, mgroupId, year, month, day, hour, year_s, month_s, day_s, False, cal_attrs)
return
if not cal_attrs:
this_cal_attrs = ['production', 'coal', 'run_hour']
else:
this_cal_attrs = cal_attrs[:]
mgroup = Mgroup.objects.get(id=mgroupId)
sflog = SfLog.objects.get(id=sflogId)
if sflogId:
sflog = SfLog.objects.get(id=sflogId)
elif year and month and day and hour:
mytz = tz.gettz(settings.TIME_ZONE)
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 sflog:
year_s, month_s, day_s = sflog.get_ymd
@ -246,102 +260,100 @@ def compute_enstat(type, sflogId, mgroupId, year, month, day, hour, year_s, mont
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})
# 消耗物料统计(包括电耗)
input_materials = []
has_product = False
input_materials = mgroup.input_materials
if mgroup.product:
input_materials.insert(0, mgroup.product.id)
has_product = True
imaterial_cost_unit = 0
imaterial_data = []
for ind, mid in enumerate(input_materials):
material = Material.objects.get(id=mid)
if type == 'hour_s':
mps = MpointStat.objects.filter(type='hour_s', mgroup=mgroup, year_s=year_s, month_s=month_s, day_s=day_s, hour=hour, mpoint__material=material)
elif type == 'sflog':
mps = MpointStat.objects.filter(type='sflog', sflog=sflog, mpoint__material=material)
elif type == 'day_s':
mps = MpointStat.objects.filter(type='day_s', mgroup=mgroup, year_s=year_s, month_s=month_s, day_s=day_s, mpoint__material=material)
elif type == 'month_st':
mps = MpointStat.objects.filter(type='sflog', mgroup=mgroup, sflog__team=team, year_s=year_s, month_s=month_s, mpoint__material=material)
elif type == 'month_s':
mps = MpointStat.objects.filter(type='month_s', mgroup=mgroup, year_s=year_s, month_s=month_s, mpoint__material=material)
elif type == 'year_s':
mps = MpointStat.objects.filter(type='year_s', mgroup=mgroup, year_s=year_s, mpoint__material=material)
if mps.filter(mpoint__is_all=True).exists():
mps = mps.filter(mpoint__is_all=True)
amount_consume = mps.aggregate(sum=Sum('val'))['sum']
if amount_consume is None:
amount_consume = 0
if ind == 0 and has_product: # 如果是产量
enstat.total_production = amount_consume
else:
if material.code in ['pcoal', 'cair', 'steam']:
price_unit = 0
if 'production' in this_cal_attrs:
# 消耗物料统计(包括电耗)
input_materials = []
has_product = False
input_materials = mgroup.input_materials
if mgroup.product:
input_materials.insert(0, mgroup.product.id)
has_product = True
imaterial_cost_unit = 0
imaterial_data = []
for ind, mid in enumerate(input_materials):
material = Material.objects.get(id=mid)
if type == 'hour_s':
mps = MpointStat.objects.filter(type='hour_s', mgroup=mgroup, year_s=year_s, month_s=month_s, day_s=day_s, hour=hour, mpoint__material=material)
elif type == 'sflog':
mps = MpointStat.objects.filter(type='sflog', sflog=sflog, mpoint__material=material)
elif type == 'day_s':
mps = MpointStat.objects.filter(type='day_s', mgroup=mgroup, year_s=year_s, month_s=month_s, day_s=day_s, mpoint__material=material)
elif type == 'month_st':
mps = MpointStat.objects.filter(type='sflog', mgroup=mgroup, sflog__team=team, year_s=year_s, month_s=month_s, mpoint__material=material)
elif type == 'month_s':
mps = MpointStat.objects.filter(type='month_s', mgroup=mgroup, year_s=year_s, month_s=month_s, mpoint__material=material)
elif type == 'year_s':
mps = MpointStat.objects.filter(type='year_s', mgroup=mgroup, year_s=year_s, mpoint__material=material)
if mps.filter(mpoint__is_all=True).exists():
mps = mps.filter(mpoint__is_all=True)
amount_consume = mps.aggregate(sum=Sum('val'))['sum']
if amount_consume is None:
amount_consume = 0
if ind == 0 and has_product: # 如果是产量
enstat.total_production = amount_consume
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
imaterial_cost_unit = imaterial_cost_unit + cost_unit
if material.code == 'elec':
enstat.elec_consume = amount_consume
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
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 Exception as e:
cost_unit = 0
imaterial_cost_unit = imaterial_cost_unit + cost_unit
if material.code == 'elec':
enstat.elec_consume = amount_consume
enstat.elec_coal_consume = enstat.elec_consume*0.1229/1000
try:
enstat.elec_consume_unit = enstat.elec_consume/enstat.total_production
except Exception as e:
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
enstat.kiln_end_heat = enstat.total_production - enstat.ccr_consume
except:
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
# 其他成本数据
other_cost_data = []
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)
other_cost_unit = other_cost_unit + item['cost_unit']
other_cost_data.append(item)
enstat.production_cost_unit = imaterial_cost_unit + other_cost_unit
if enstat.mgroup.cate == 'section':
if 'production' in this_cal_attrs:
# 算能耗
if enstat.mgroup.name != '回转窑':
try:
enstat.en_consume_unit = enstat.elec_coal_consume / enstat.total_production
except Exception as e:
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
# 其他成本数据
other_cost_data = []
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)
other_cost_unit = other_cost_unit + item['cost_unit']
other_cost_data.append(item)
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': # 如果计算的是班月,把主要设备电耗数据拉过来
if type == 'month_sf' and 'production' 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(
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:
except Exception as e:
pass
enstat.equip_elec_data = res
@ -353,48 +365,45 @@ def compute_enstat(type, sflogId, mgroupId, year, month, day, hour, year_s, mont
if pre_enstat:
try:
enstat.celec_consume_unit = enstat.elec_consume_unit + 1.45*pre_enstat.elec_consume_unit
except:
except Exception as e:
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':
# 算总煤耗
if 'coal' in this_cal_attrs:
if type in ['hour_s', 'sflog', 'day_s']:
enstat.pcoal_val = get_pcoal_val(enstat.year_s, enstat.month_s, enstat.day_s)
enstat.pcoal_coal_consume = enstat.pcoal_consume * enstat.pcoal_val/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']
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']
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']
# 算单位产品标煤耗
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']
enstat.coal_consume_unit = enstat.pcoal_coal_consume /enstat.total_production
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']:
# # 同步更新水泥磨的综合能耗,这步有可能不成功,因为水泥磨是后算的
# if type not in ['month_st', 'sflog']:
# next_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 next_enstat:
# next_enstat.cen_consume_unit = next_enstat.elec_consume_unit*0.1229 + 0.7*enstat.cen_consume_unit
# next_enstat.save()
if enstat.mgroup.name == '水泥磨' and enstat.type not in ['month_st', 'sflog'] and 'coal' in this_cal_attrs:
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
# 综合能耗
enstat.cen_consume_unit = enstat.elec_consume_unit*0.1229 + 0.7*pre_enstat.cen_consume_unit
# 运转时长相关
if type != 'hour_s':
if type != 'hour_s' and 'run_hour' in this_cal_attrs:
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
@ -424,26 +433,74 @@ def get_total_hour_now_and_shut_hour(enstat: EnStat):
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(
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).aggregate(
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(
res = SfLog.objects.filter(end_time__year=enstat.year_s, end_time__month=enstat.month_s, mgroup=enstat.mgroup, team=enstat.team).aggregate(
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(
res = SfLog.objects.filter(end_time__year=enstat.year_s, end_time__month=enstat.month_s, mgroup=enstat.mgroup).aggregate(
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(
res = SfLog.objects.filter(end_time__year=enstat.year_s, mgroup=enstat.mgroup).aggregate(
sum1 = Sum('total_hour_now'),
sum2 = Sum('shut_hour')
)
return res['sum1'] if res['sum1'] else 0, res['sum2'] if res['sum2'] else 0
return res['sum1'] if res['sum1'] else 0, res['sum2'] if res['sum2'] else 0
def cal_enstat2(year_s: int, month_s: int):
enstat2, _ = EnStat2.objects.get_or_create(year_s=year_s, month_s=month_s, defaults={'year_s': year_s, 'month_s': month_s})
material_cement = Material.objects.get(code='cement')
material_clinker = Material.objects.get(code='clinker')
material_bulk_cement = Material.objects.get(code='bulk_cement')
material_bag_cement = Material.objects.get(code='bag_cement')
enstat2.bulk_cement_price = get_price_unit(material_bulk_cement, year_s, month_s)
enstat2.clinker_price = get_price_unit(material_clinker, year_s, month_s)
enstat2.bag_cement_price = get_price_unit(material_bag_cement, year_s, month_s)
enstat2.bulk_cement_val = MpointStat.objects.filter(type='month_s', mpoint__material=material_bulk_cement, year_s=year_s, month_s=month_s).aggregate(sum=Sum('val'))['sum']
if enstat2.bulk_cement_val is None:
enstat2.bulk_cement_val = 0
enstat2.bag_cement_val = MpointStat.objects.filter(type='month_s', mpoint__material=material_bag_cement, year_s=year_s, month_s=month_s).aggregate(sum=Sum('val'))['sum']
if enstat2.bag_cement_val is None:
enstat2.bag_cement_val = 0
enstat2.clinker_val = MpointStat.objects.filter(type='month_s', mpoint__material=material_cement, year_s=year_s, month_s=month_s).aggregate(sum=Sum('val'))['sum']
if enstat2.clinker_val is None:
enstat2.clinker_val = 0
enstat2.industry_total_val = (enstat2.bulk_cement_val*enstat2.bulk_cement_price+enstat2.bag_cement_val*enstat2.bag_cement_price+enstat2.clinker_val*enstat2.clinker_price)/10000
res = EnStat.objects.filter(mgroup__product__code='cement', type='month_s', year_s=year_s, month_s=month_s).aggregate(sum=Sum('total_production'), avg=Avg('production_cost_unit'))
enstat2.cement_val = res['sum'] if res['sum'] else 0
enstat2.cement_cost_unit = res['avg'] if res['avg'] else 0
enstat2.industry_add_val = enstat2.industry_total_val - enstat2.cement_val * enstat2.cement_cost_unit /10000
# 全厂电量
enstat_qs = EnStat.objects.filter(type='month_s', year_s=year_s, month_s=month_s)
res_elec_pcoal = enstat_qs.aggregate(sum1=Sum('elec_consume'), sum2=Sum('elec_coal_consume'), sum3=Sum('pcoal_consume'), sum4=Sum('pcoal_coal_consume'))
enstat2.elec_consume = res_elec_pcoal['sum1'] if res_elec_pcoal['sum1'] else 0
enstat2.elec_coal_consume = enstat2.elec_consume*0.1229/1000
enstat2.pcoal_consume = res_elec_pcoal['sum3'] if res_elec_pcoal['sum3'] else 0
enstat2.pcoal_coal_consume = res_elec_pcoal['sum4'] if res_elec_pcoal['sum4'] else 0
enstat2.en_consume = enstat2.pcoal_coal_consume + enstat2.elec_coal_consume
try:
enstat2.en_consume_unit = enstat2.en_consume/enstat2.industry_total_val
except ZeroDivisionError:
pass
try:
enstat2.en_add_consume_unit = enstat2.en_consume/enstat2.industry_add_val
except ZeroDivisionError:
pass
enstat2.save()