feat: 只更新必要的字段
This commit is contained in:
parent
ebe6ab9f3b
commit
50a34a44bb
|
@ -1,5 +1,7 @@
|
|||
from apps.enm.models import Mpoint, MpointStat
|
||||
from apps.enm.models import Mpoint, MpointStat, EnStat
|
||||
import re
|
||||
import traceback
|
||||
from apps.mtm.services import get_mgroup_goals
|
||||
|
||||
|
||||
def translate_eval_formula(exp_str: str, year: int, month: int, day: int, hour: int):
|
||||
|
@ -16,3 +18,48 @@ def translate_eval_formula(exp_str: str, year: int, month: int, day: int, hour:
|
|||
|
||||
rval = eval(exp_str)
|
||||
return rval
|
||||
|
||||
|
||||
# cal_rule = {
|
||||
# "电石渣": {
|
||||
# "total_production": 0.4,
|
||||
# "elec_consume_unit": 0.4,
|
||||
# "production_cost_unit": 0.2
|
||||
# },
|
||||
# "原料磨":{
|
||||
# "production_hour": 0.3,
|
||||
# "elec_consume_unit": 0.3,
|
||||
# "production_cost_unit": 0.1,
|
||||
# "辅料_细度":0.05,
|
||||
# "辅料_水分":0.04,
|
||||
# "干混生料_CaO":0.04
|
||||
# }
|
||||
# }
|
||||
# def cal_team_score(data):
|
||||
# """
|
||||
# 计算月度绩效
|
||||
# """
|
||||
# qua_rate = {}
|
||||
# month_s = data['month_s']
|
||||
# for item in data['qua_data']:
|
||||
# qua_rate[f'{item["material_name"]}_{item["testitem_name"]}'] = item["rate_pass"]
|
||||
|
||||
# goal_dict = get_mgroup_goals(data['mgroup'], data['year_s'], False)
|
||||
# goal_data = {}
|
||||
# try:
|
||||
# rule = cal_rule[data['mgroup_name']]
|
||||
# score = 0
|
||||
# for key in rule:
|
||||
# new_key = f'{key}_{month_s}'
|
||||
# goal_data[new_key] = goal_dict[new_key]
|
||||
# if '-' in key:
|
||||
# score = score + qua_rate.get(key, 0)/goal_data[new_key]*rule[key]
|
||||
# else:
|
||||
# score = score + data.get(key)/goal_data[new_key]*rule[key]
|
||||
# print(score)
|
||||
# # enstat.goal_data = goal_data
|
||||
# # enstat.score =score
|
||||
# # enstat.save(update_fields=['goal_data', 'score'])
|
||||
# except:
|
||||
# print(traceback.format_exc())
|
||||
# return goal_data, score
|
|
@ -354,13 +354,13 @@ def cal_enstat(type, sflogId, mgroupId, year, month, day, hour, year_s, month_s,
|
|||
enstat.save()
|
||||
# 计算一些其他数据
|
||||
if type == 'month_st' and 'material' in this_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).values(
|
||||
res = MpointStat.objects.filter(type='month_s', year_s=year_s, month_s=month_s, sflog__team=enstat.team, mpoint__ep_monitored__power_kw__gte=100).values(
|
||||
equipment=F('mpoint__ep_monitored__id'), equipment_name=F('mpoint__ep_monitored__name'), consume=F('val'))
|
||||
res = list(res)
|
||||
for item in res:
|
||||
try:
|
||||
item['consume_unit'] = item['consume'] / enstat.total_production
|
||||
except Exception as e:
|
||||
except ZeroDivisionError:
|
||||
pass
|
||||
enstat.equip_elec_data = res
|
||||
enstat.save()
|
||||
|
@ -536,10 +536,11 @@ def cal_enstat_pcoal_change(enstat, new_pcoal_heat):
|
|||
# 综合能耗
|
||||
enstat.cen_consume_unit = enstat.coal_consume_unit + 0.1229 * enstat.elec_consume_unit
|
||||
|
||||
enstat.save(update_fields=['pcoal_heat', 'pcoal_coal_consume', 'coal_consume_unit', 'cen_consume_unit'])
|
||||
# 同步更新水泥磨的综合能耗,这步有可能不成功,因为水泥磨是后算的, 但是当pcoal_change时这个就有用了
|
||||
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()
|
||||
enstat.save()
|
||||
next_enstat.save(update_fields=['cen_consume_unit'])
|
||||
|
|
@ -1,12 +1,12 @@
|
|||
from django.shortcuts import render
|
||||
from apps.enm.models import Mpoint, MpLog, MpointStat, EnStat
|
||||
from apps.enm.models import Mpoint, MpLog, MpointStat, EnStat, EnStat2
|
||||
from apps.utils.viewsets import CustomModelViewSet, CustomGenericViewSet
|
||||
from rest_framework.mixins import ListModelMixin
|
||||
from apps.utils.mixins import BulkCreateModelMixin, BulkDestroyModelMixin
|
||||
from apps.enm.serializers import (MpointSerializer, MpLogSerializer, MpointStatSerializer, EnStatSerializer)
|
||||
from apps.enm.serializers import (MpointSerializer, MpLogSerializer, MpointStatSerializer, EnStatSerializer, EnStat2Serializer)
|
||||
from apps.enm.filters import MpointStatFilter, EnStatFilter
|
||||
from apps.enm.tasks import cal_mpointstat_manual
|
||||
|
||||
from rest_framework.response import Response
|
||||
|
||||
class MpointViewSet(CustomModelViewSet):
|
||||
"""
|
||||
|
@ -67,3 +67,15 @@ class EnStatViewSet(ListModelMixin, CustomGenericViewSet):
|
|||
serializer_class = EnStatSerializer
|
||||
select_related_fields = ['mgroup', 'team', 'mgroup__belong_dept']
|
||||
filterset_class = EnStatFilter
|
||||
|
||||
|
||||
class EnStat2ViewSet(ListModelMixin, CustomGenericViewSet):
|
||||
"""
|
||||
list:全厂统计记录
|
||||
|
||||
全厂统计记录
|
||||
"""
|
||||
perms_map = {'get': '*'}
|
||||
queryset = EnStat2.objects.all()
|
||||
serializer_class = EnStat2Serializer
|
||||
filterset_fields = ['year_s', 'month_s']
|
|
@ -39,7 +39,7 @@ 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()
|
||||
enstat.save(update_fields=['imaterial_data', 'production_cost_unit'])
|
||||
if material.code in ['bulk_cement', 'bag_cement', 'clinker']: # 需要更新enstat2
|
||||
from apps.enm.tasks import cal_enstat2
|
||||
cal_enstat2(priceset.year, priceset.month)
|
||||
|
@ -69,4 +69,4 @@ def cal_enstat_when_feeset_change(feesetId):
|
|||
# 更新一些数据
|
||||
enstat.other_cost_data = other_cost_data
|
||||
enstat.production_cost_unit = enstat.production_cost_unit - old_cost_unit + new_cost_unit
|
||||
enstat.save()
|
||||
enstat.save(update_fields=['other_cost_data', 'production_cost_unit'])
|
||||
|
|
|
@ -88,7 +88,7 @@ def cal_quastat_sflog(sflogId: str):
|
|||
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})
|
||||
enstat.qua_data = list(qs1_v)
|
||||
enstat.save()
|
||||
enstat.save(update_fields=['qua_data'])
|
||||
|
||||
# 日统计
|
||||
sql_q2 = f"""SELECT
|
||||
|
@ -118,7 +118,7 @@ ORDER BY mgroup.sort, mtma.sort, qmt.sort
|
|||
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})
|
||||
enstat.qua_data = res2
|
||||
enstat.save()
|
||||
enstat.save(update_fields=['qua_data'])
|
||||
|
||||
if team:
|
||||
# 班月统计
|
||||
|
@ -149,7 +149,7 @@ ORDER BY mgroup.sort, mtma.sort, qmt.sort
|
|||
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})
|
||||
enstat.qua_data = res3
|
||||
enstat.save()
|
||||
enstat.save(update_fields=['qua_data'])
|
||||
|
||||
# 月统计
|
||||
sql_q4 = f"""SELECT
|
||||
|
@ -179,7 +179,7 @@ ORDER BY mgroup.sort, mtma.sort, qmt.sort
|
|||
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})
|
||||
enstat.qua_data = res4
|
||||
enstat.save()
|
||||
enstat.save(update_fields=['qua_data'])
|
||||
|
||||
# 年统计
|
||||
sql_q5 = f"""SELECT
|
||||
|
@ -208,5 +208,5 @@ ORDER BY mgroup.sort, mtma.sort, qmt.sort
|
|||
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})
|
||||
enstat.qua_data = res5
|
||||
enstat.save()
|
||||
enstat.save(update_fields=['qua_data'])
|
||||
|
Loading…
Reference in New Issue