feat: 只更新必要的字段

This commit is contained in:
caoqianming 2023-08-04 14:29:23 +08:00
parent ebe6ab9f3b
commit 50a34a44bb
5 changed files with 77 additions and 17 deletions

View File

@ -1,5 +1,7 @@
from apps.enm.models import Mpoint, MpointStat from apps.enm.models import Mpoint, MpointStat, EnStat
import re 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): def translate_eval_formula(exp_str: str, year: int, month: int, day: int, hour: int):
@ -15,4 +17,49 @@ def translate_eval_formula(exp_str: str, year: int, month: int, day: int, hour:
exp_str = exp_str.replace(f"${{{match}}}", mpst.val) exp_str = exp_str.replace(f"${{{match}}}", mpst.val)
rval = eval(exp_str) rval = eval(exp_str)
return rval 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

View File

@ -354,13 +354,13 @@ def cal_enstat(type, sflogId, mgroupId, year, month, day, hour, year_s, month_s,
enstat.save() enstat.save()
# 计算一些其他数据 # 计算一些其他数据
if type == 'month_st' and 'material' in this_cal_attrs: # 如果计算的是班月,把主要设备电耗数据拉过来 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')) equipment=F('mpoint__ep_monitored__id'), equipment_name=F('mpoint__ep_monitored__name'), consume=F('val'))
res = list(res) res = list(res)
for item in res: for item in res:
try: try:
item['consume_unit'] = item['consume'] / enstat.total_production item['consume_unit'] = item['consume'] / enstat.total_production
except Exception as e: except ZeroDivisionError:
pass pass
enstat.equip_elec_data = res enstat.equip_elec_data = res
enstat.save() 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.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时这个就有用了 # 同步更新水泥磨的综合能耗,这步有可能不成功,因为水泥磨是后算的, 但是当pcoal_change时这个就有用了
if 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() 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: if next_enstat:
next_enstat.cen_consume_unit = next_enstat.elec_consume_unit*0.1229 + 0.7*enstat.cen_consume_unit next_enstat.cen_consume_unit = next_enstat.elec_consume_unit*0.1229 + 0.7*enstat.cen_consume_unit
next_enstat.save() next_enstat.save(update_fields=['cen_consume_unit'])
enstat.save()

View File

@ -1,12 +1,12 @@
from django.shortcuts import render 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 apps.utils.viewsets import CustomModelViewSet, CustomGenericViewSet
from rest_framework.mixins import ListModelMixin from rest_framework.mixins import ListModelMixin
from apps.utils.mixins import BulkCreateModelMixin, BulkDestroyModelMixin 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.filters import MpointStatFilter, EnStatFilter
from apps.enm.tasks import cal_mpointstat_manual from apps.enm.tasks import cal_mpointstat_manual
from rest_framework.response import Response
class MpointViewSet(CustomModelViewSet): class MpointViewSet(CustomModelViewSet):
""" """
@ -66,4 +66,16 @@ class EnStatViewSet(ListModelMixin, CustomGenericViewSet):
queryset = EnStat.objects.all() queryset = EnStat.objects.all()
serializer_class = EnStatSerializer serializer_class = EnStatSerializer
select_related_fields = ['mgroup', 'team', 'mgroup__belong_dept'] select_related_fields = ['mgroup', 'team', 'mgroup__belong_dept']
filterset_class = EnStatFilter filterset_class = EnStatFilter
class EnStat2ViewSet(ListModelMixin, CustomGenericViewSet):
"""
list:全厂统计记录
全厂统计记录
"""
perms_map = {'get': '*'}
queryset = EnStat2.objects.all()
serializer_class = EnStat2Serializer
filterset_fields = ['year_s', 'month_s']

View File

@ -39,7 +39,7 @@ def cal_enstat_when_priceset_change(pricesetId):
# 更新一些数据 # 更新一些数据
enstat.imaterial_data = imaterial_data enstat.imaterial_data = imaterial_data
enstat.production_cost_unit = enstat.production_cost_unit - old_cost_unit + new_cost_unit 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 if material.code in ['bulk_cement', 'bag_cement', 'clinker']: # 需要更新enstat2
from apps.enm.tasks import cal_enstat2 from apps.enm.tasks import cal_enstat2
cal_enstat2(priceset.year, priceset.month) 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.other_cost_data = other_cost_data
enstat.production_cost_unit = enstat.production_cost_unit - old_cost_unit + new_cost_unit 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'])

View File

@ -88,7 +88,7 @@ def cal_quastat_sflog(sflogId: str):
enstat, _ = EnStat.objects.get_or_create(type="sflog", sflog=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}) 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.qua_data = list(qs1_v)
enstat.save() enstat.save(update_fields=['qua_data'])
# 日统计 # 日统计
sql_q2 = f"""SELECT 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, 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}) defaults={'type': 'day_s', 'mgroup': mgroup, 'year_s': year_s, 'month_s': month_s, 'day_s': day_s})
enstat.qua_data = res2 enstat.qua_data = res2
enstat.save() enstat.save(update_fields=['qua_data'])
if team: 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, 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}) defaults={'type': 'month_st', 'mgroup': mgroup, 'year_s': year_s, 'month_s': month_s, 'team': team})
enstat.qua_data = res3 enstat.qua_data = res3
enstat.save() enstat.save(update_fields=['qua_data'])
# 月统计 # 月统计
sql_q4 = f"""SELECT 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, 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}) defaults={'type': 'month_s', 'mgroup': mgroup, 'year_s': year_s, 'month_s': month_s})
enstat.qua_data = res4 enstat.qua_data = res4
enstat.save() enstat.save(update_fields=['qua_data'])
# 年统计 # 年统计
sql_q5 = f"""SELECT 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, 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}) defaults={'type': 'year_s', 'mgroup': mgroup, 'year_s': year_s})
enstat.qua_data = res5 enstat.qua_data = res5
enstat.save() enstat.save(update_fields=['qua_data'])