feat: enstat2增加day_s统计及相应的tasks改动

This commit is contained in:
caoqianming 2023-08-23 08:33:45 +08:00
parent 11ced96400
commit 4cd2c0f85b
5 changed files with 76 additions and 18 deletions

View File

@ -0,0 +1,23 @@
# Generated by Django 3.2.12 on 2023-08-22 09:58
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('enm', '0019_auto_20230818_1721'),
]
operations = [
migrations.AddField(
model_name='enstat2',
name='day_s',
field=models.PositiveSmallIntegerField(blank=True, null=True, verbose_name='班日'),
),
migrations.AddField(
model_name='enstat2',
name='type',
field=models.CharField(default='month_s', help_text='month_s/day_s', max_length=50, verbose_name='统计维度'),
),
]

View File

@ -98,8 +98,10 @@ class EnStat2(BaseModel):
"""
能源数据统计表2
"""
type = models.CharField('统计维度', max_length=50, default='month_s', help_text='month_s/day_s')
year_s = models.PositiveSmallIntegerField('班年')
month_s = models.PositiveSmallIntegerField('班月')
day_s = models.PositiveSmallIntegerField('班日', null=True, blank=True)
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')

View File

@ -166,17 +166,20 @@ def cal_mpointstats(is_now=1, year=None, month=None, day=None, hour=None):
# 开始计算enstat
mgroups = Mgroup.objects.all().order_by('sort')
mgroups_group = []
# mgroups_group = []
year_s, month_s, day_s = 0, 0, 0
for mgroup in mgroups:
# mgroups_group.append(cal_enstat.s('hour_s', None, mgroup.id, year, month, day, hour, None, None, None, True, ['material', 'run_hour']))
cal_enstat('hour_s', None, mgroup.id, year, month, day, hour, None, None, None, True)
year_s, month_s, day_s = cal_enstat('hour_s', None, mgroup.id, year, month, day, hour, None, None, None, True)
# mgroups_t = mgroups.filter(name__in=['回转窑', '水泥磨'])
# mgroups_t_group = []
# for mgroup in mgroups_t:
# mgroups_t_group.append(cal_enstat.s('hour_s', None, mgroup.id, year, month, day, hour, None, None, None, True, ['pcoal']))
# 最后计算enstat2
cal_enstat2(year_s=year, month_s=month)
cal_enstat2(type="day_s", year_s=year_s, month_s=month_s, day_s=day_s)
# task_chain = chain(group(mpoints_without_formula_group), group(mpoints_other_group), group(mgroups_group), group(mgroups_t_group), group([cal_enstat2.s(year_s=year, month_s=month)]))
# task_chain.delay()
@ -225,8 +228,8 @@ def cal_enstat(type, sflogId, mgroupId, year, month, day, hour, year_s, month_s,
start_index = types_list.index(type)
new_types_list = types_list[start_index:]
for type in new_types_list:
cal_enstat(type, sflogId, mgroupId, year, month, day, hour, year_s, month_s, day_s, False)
return
year_s, month_s, day_s = cal_enstat(type, sflogId, mgroupId, year, month, day, hour, year_s, month_s, day_s, False)
return year_s, month_s, day_s
if not cal_attrs:
this_cal_attrs = ['material', 'pcoal', 'run_hour']
else:
@ -414,6 +417,7 @@ def cal_enstat(type, sflogId, mgroupId, year, month, day, hour, year_s, month_s,
pass
enstat.save()
enstat.save()
return year_s, month_s, day_s # 返回这个值主要为了全厂数据计算而用
def get_total_hour_now_and_shut_hour(enstat: EnStat):
@ -464,8 +468,20 @@ def get_total_hour_now_and_shut_hour(enstat: EnStat):
return res['sum1'] if res['sum1'] else 0, res['sum2'] if res['sum2'] else 0
@shared_task(base=CustomTask)
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})
def cal_enstat2(type: str, year_s: int, month_s: int, day_s: int, cascade=True):
if type == 'day_s':
cal_enstat2('day_s', year_s, month_s, day_s)
if cascade:
cal_enstat2('month_s', year_s, month_s, day_s)
elif type == 'month_s':
cal_enstat2('month_s', year_s, month_s, day_s)
else:
return
if type == 'month_s':
enstat2, _ = EnStat2.objects.get_or_create(type="month_s", year_s=year_s, month_s=month_s, defaults={'year_s': year_s, 'month_s': month_s, 'type': 'month_s'})
elif type == 'day_s':
enstat2, _ = EnStat2.objects.get_or_create(type="day_s", year_s=year_s, month_s=month_s, day_s=day_s, defaults={'year_s': year_s, 'month_s': month_s, 'day_s': day_s, 'type': 'day_s'})
enstat2 = EnStat2.objects.select_for_update().get(id=enstat2.id) # 加锁
material_cement = Material.objects.get(code='cement')
material_clinker = Material.objects.get(code='clinker')
material_bulk_cement = Material.objects.get(code='bulk_cement')
@ -474,27 +490,41 @@ def cal_enstat2(year_s: int, month_s: int):
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 type == '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']
elif type == 'day_s':
enstat2.bulk_cement_val = MpointStat.objects.filter(type='day_s', mpoint__material=material_bulk_cement, year_s=year_s, month_s=month_s, day_s=day_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 type == 'month_s':
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']
elif type == 'day_s':
enstat2.bag_cement_val = MpointStat.objects.filter(type='day_s', mpoint__material=material_bag_cement, year_s=year_s, month_s=month_s, day_s=day_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 type == 'month_s':
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']
elif type == 'day_s':
enstat2.clinker_val = MpointStat.objects.filter(type='day_s', mpoint__material=material_cement, year_s=year_s, month_s=month_s, day_s=day_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'))
if type == 'month_s':
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'))
elif type == 'day_s':
res = EnStat.objects.filter(mgroup__product__code='cement', type='day_s', year_s=year_s, month_s=month_s, day_s=day_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)
if type == 'month_s':
enstat_qs = EnStat.objects.filter(type='month_s', year_s=year_s, month_s=month_s)
elif type == 'day_s':
enstat_qs = EnStat.objects.filter(type='day_s', year_s=year_s, month_s=month_s, day_s=day_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'),
sum5=Sum('water_consume'), sum6=Sum('cair_consume'))
enstat2.elec_consume = res_elec_pcoal['sum1'] if res_elec_pcoal['sum1'] else 0
@ -544,5 +574,4 @@ def cal_enstat_pcoal_change(enstat, new_pcoal_heat):
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(update_fields=['cen_consume_unit'])
next_enstat.save(update_fields=['cen_consume_unit'])

View File

@ -41,9 +41,13 @@ def cal_enstat_when_priceset_change(pricesetId):
enstat.imaterial_data = imaterial_data
enstat.production_cost_unit = enstat.production_cost_unit - old_cost_unit + new_cost_unit
enstat.save(update_fields=['imaterial_data', 'production_cost_unit'])
if material.code in ['bulk_cement', 'bag_cement', 'clinker']: # 需要更新enstat2
if material.code in ['bulk_cement', 'bag_cement', 'clinker']: # 需要更新enstat2 袋装水泥/散装水泥/熟料价格变化
from apps.enm.tasks import cal_enstat2
cal_enstat2(priceset.year, priceset.month)
from apps.enm.models import EnStat2
enstat2s = EnStat2.objects.filter(type='day_s', year_s=priceset.year, month_s=priceset.month)
for enstat2 in enstat2s:
cal_enstat2('day_s', enstat2.year_s, enstat2.month_s, enstat2.day_s, False)
cal_enstat2('month_s', priceset.year, priceset.month, None)
@shared_task(base=CustomTask)

View File

@ -114,7 +114,7 @@ def cal_enstat_when_pcoal_heat_change(sflogId):
for enstat in enstats_other:
cal_enstat_pcoal_change(enstat, pcoal_heat)
cal_enstat2(year_s, month_s)
cal_enstat2('day_s', year_s, month_s, day_s)
@shared_task(base=CustomTask)