diff --git a/apps/develop/views.py b/apps/develop/views.py index 8fc0ba87..7239b227 100755 --- a/apps/develop/views.py +++ b/apps/develop/views.py @@ -177,6 +177,18 @@ class CorrectViewSet(CustomGenericViewSet): item.save() return Response() + @action(methods=['post'], detail=False, serializer_class=Serializer) + def enstat(self, request, pk=None): + """矫正能源统计数据 + + 矫正能源统计数据 + """ + from apps.enm.models import EnStat + for item in EnStat.objects.exclude(sflog=None): + item.shift = item.sflog.shift + item.save() + return Response() + class TestViewSet(CustomGenericViewSet): authentication_classes = () permission_classes = () diff --git a/apps/enm/services.py b/apps/enm/services.py index e69de29b..803e0ccc 100644 --- a/apps/enm/services.py +++ b/apps/enm/services.py @@ -0,0 +1,18 @@ +from apps.enm.models import Mpoint, MpointStat +import re + + +def translate_eval_formula(exp_str: str, year: int, month: int, day: int, hour: int): + """ + 传入 + """ + pattern = r'\${(\d+)}' + matches = re.findall(pattern, exp_str) + + for match in matches: + mpst = MpointStat.objects.filter(type='hour', mpoint__id=match, year=year, month=month, day=day).first() + if mpst: + exp_str = exp_str.replace(f"${{{match}}}", mpst.val) + + rval = eval(exp_str) + return rval \ No newline at end of file diff --git a/apps/enm/tasks.py b/apps/enm/tasks.py index d4b8bd56..334b300d 100644 --- a/apps/enm/tasks.py +++ b/apps/enm/tasks.py @@ -16,6 +16,10 @@ from apps.wpm.services import make_sflogs from apps.mtm.models import Mgroup, Material from apps.fim.services import get_cost_unit, get_price_unit from apps.fim.models import Fee +from django.core.cache import cache +from apps.enm.services import translate_eval_formula +import logging +myLogger = logging.getLogger('log') def get_current_and_previous_time(): now = datetime.datetime.now() @@ -46,7 +50,7 @@ def get_tag_val(): cache.set('last_tag_id', last_tag_id) @shared_task(base=CustomTask) -def cal_mpointstat_hour(mpointId: str, year: int, month: int, day: int, hour: int, next_cal=0): +def cal_mpointstat_hour(mpointId: str, year: int, month: int, day: int, hour: int): """ 计算某一测点, 某一时间点某一小时的统计值 """ @@ -56,15 +60,23 @@ def cal_mpointstat_hour(mpointId: str, year: int, month: int, day: int, hour: in if mpoint.material: # 如果计量的是物料 params = {'mpoint': mpoint, 'type': 'hour'} params['year'], params['month'], params['day'], params['hour'] = year, month, day, hour - mrs = MpLog.objects.filter( - mpoint=mpoint, - tag_update__year=params['year'], - tag_update__month=params['month'], - tag_update__day=params['day'], - tag_update__hour= params['hour']).order_by('tag_update') val = 0 - if mrs.exists(): - val = mrs.last().tag_val - mrs.first().tag_val + if mpoint.formula: + formular = mpoint.formular + try: + val = translate_eval_formula(formular, year, month, day, hour) + except: + myLogger.error('公式执行错误:{}-{}'.format(mpoint.id, formular), exc_info=True) + raise + else: + mrs = MpLog.objects.filter( + mpoint=mpoint, + tag_update__year=params['year'], + tag_update__month=params['month'], + tag_update__day=params['day'], + tag_update__hour= params['hour']).order_by('tag_update') + if mrs.exists(): + val = mrs.last().tag_val - mrs.first().tag_val ms, _ = MpointStat.objects.get_or_create(**params, defaults=params) ms.val = val ms.save() @@ -113,7 +125,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() - cal_mpointstat_manual(mpoint.id, sflog.id, mgroup.id, year_s, month_s, day_s, year, month, day, hour, 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, next_cal) @@ -137,22 +155,18 @@ def cal_mpointstats(is_now=1, year=None, month=None, day=None, hour=None): for mgroup in mgroups: product = mgroup.product mpoints = Mpoint.objects.filter(material=product, is_auto=True) - len_mps = len(mpoints) for ind, item in enumerate(mpoints): caled_mpointids.append(item.id) - next_cal = 0 - if ind == len_mps - 1: # 运行到最后一次再去进行二次计算, 因为产量要作为分母使用 - next_cal = 1 - cal_mpointstat_hour(item.id, year, month, day, hour, next_cal) + cal_mpointstat_hour(item.id, year, month, day, hour) # 统计其他测点 - mpoints = Mpoint.objects.filter(is_auto=True).exclude(id__in=caled_mpointids).order_by('material') + 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, 1) + cal_mpointstat_hour(i.id, year, month, day, hour) @shared_task(base=CustomTask) -def cal_mpointstat_manual(mpointId: str, sflogId: str, mgroupId: str, year_s: int, month_s: int, day_s: int, year=None, month=None, day=None, hour=None, next_cal=0): +def cal_mpointstat_manual(mpointId: str, sflogId: str, mgroupId: str, year: int, month: int, day: int, hour: int, year_s: int, month_s: int, day_s: int, next_cal=0): """ 手动录入的测点数据进行往上统计,一级一级往上 """ @@ -178,13 +192,15 @@ def cal_mpointstat_manual(mpointId: str, sflogId: str, mgroupId: str, year_s: in if next_cal: # 二次计算 if hour: - compute_enstat('hour_s', sflogId, mgroupId, year_s, month_s, day_s, year, month, day, hour) - else: - compute_enstat('sflog', sflogId, mgroupId, year_s, month_s, day_s, year, month, day, 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_sf', 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) - -def compute_enstat(type, sflogId, mgroupId, year_s, month_s, day_s, year, month, day, hour): +def compute_enstat(type, sflogId, mgroupId, year, month, day, hour, year_s, month_s, day_s): """ 计算能源数据统计 """ @@ -209,7 +225,7 @@ def compute_enstat(type, sflogId, mgroupId, year_s, month_s, day_s, year, month, 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}) elif type == 'year_s': - enstat, _ = EnStat.objects.get_or_create(type="year_s", mgroup=mgroup, year_s=year_s, month_s=month_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}) # 物料统计 @@ -251,8 +267,8 @@ def compute_enstat(type, sflogId, mgroupId, year_s, month_s, day_s, year, month, if material.code == 'elec': enstat.elec_consume = amount_consume enstat.save() - print(amount_consume) - imaterial_data.append({'material': mid, 'material_name': material.name, 'material_type': material.type, 'price_unit': price_unit, 'amount_consume': amount_consume, 'cost': cost, 'cost_unit': cost_unit}) + 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} + imaterial_data.append(imaterial_item) enstat.imaterial_data = imaterial_data enstat.save() other_cost_data = [] @@ -263,15 +279,4 @@ def compute_enstat(type, sflogId, mgroupId, year_s, month_s, day_s, year, month, other_cost_data.append(item) enstat.other_cost_data = other_cost_data enstat.production_cost_unit = cost_unit_total - enstat.save() - - if type == 'hour_s': - compute_enstat('sflog', sflogId, mgroupId, year, month, day, hour, year_s, month_s, day_s) - elif type == 'sflog': - compute_enstat('day_s', sflogId, mgroupId, year, month, day, hour, year_s, month_s, day_s) - elif type == 'day_s': - compute_enstat('month_sf', sflogId, mgroupId, year, month, day, hour, year_s, month_s, day_s) - elif type == 'month_sf': - compute_enstat('month_s', sflogId, mgroupId, year, month, day, hour, year_s, month_s, day_s) - elif type == 'month_s': - compute_enstat('year_s', sflogId, mgroupId, year, month, day, hour, year_s, month_s, day_s) \ No newline at end of file + enstat.save() \ No newline at end of file diff --git a/apps/enm/views.py b/apps/enm/views.py index d18c36f0..97b18ff2 100644 --- a/apps/enm/views.py +++ b/apps/enm/views.py @@ -48,12 +48,12 @@ class MpointStatViewSet(BulkCreateModelMixin, BulkDestroyModelMixin, ListModelMi def perform_create(self, serializer): ins = serializer.save() - cal_mpointstat_manual.delay(ins.mpoint.id, ins.sflog.id, ins.mgroup.id, ins.year_s, ins.month_s, ins.day_s, nex_cal=1) + cal_mpointstat_manual.delay(ins.mpoint.id, ins.sflog.id, ins.mgroup.id, None, None, None, None, ins.year_s, ins.month_s, ins.day_s, nex_cal=1) def perform_destroy(self, instance): mpoint, sflog, mgroup, year_s, month_s, day_s = instance.mpoint, instance.sflog, instance.mgroup, instance.year_s, instance.month_s, instance.day_s instance.delete() - cal_mpointstat_manual.delay(mpoint.id, sflog.id, mgroup.id, year_s, month_s, day_s) + cal_mpointstat_manual.delay(mpoint.id, sflog.id, mgroup.id, None, None, None, None, year_s, month_s, day_s) class EnStatViewSet(ListModelMixin, CustomGenericViewSet):