from apps.enm.models import Mpoint, MpointStat, EnStat, MpLog import re import traceback from apps.mtm.services import get_mgroup_goals from django.db.models import Q import datetime from django.utils.timezone import now def translate_eval_formula(exp_str: str, year: int, month: int, day: int, hour: int): """ 传入 """ pattern = r"\${(.*?)}" matches = re.findall(pattern, exp_str) for match in matches: mpst = MpointStat.objects.filter(Q(mpoint__id=match)|Q(mpoint__name=match)|Q(mpoint__code=match), type='hour', year=year, month=month, day=day, hour=hour).first() if mpst: exp_str = exp_str.replace(f"${{{match}}}", str(mpst.val)) rval = eval(exp_str) return rval def get_day_s(year: int, month: int, day: int, hour: int, hour_split: int = 21): """ 根据给定的小时数, 计算出班天 """ if hour <= hour_split: return year, month, day else: now = datetime.datetime(year, month, day, hour) now2 = now + datetime.timedelta(days=1) return now2.year, now2.month, now2.day # 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 def shutdown_or_startup(mplog: MpLog): from apps.wpm.models import StLog from apps.wpm.tasks import cal_shut_hour mpoint = mplog.mpoint mgroup = mpoint.mgroup if mplog.tag_val == 1 and mgroup.is_running is False: # 从停到开 stlog = StLog.objects.filter(mgroup=mgroup).order_by('start_time').last() stlog.end_time = now() stlog.save() mgroup.is_runing = True mgroup.save() cal_shut_hour(stlog.id) # 触发停机时间分配 elif mplog.tag_val == 0 and mgroup.is_runing is True: # 从开到停 StLog.objects.create(mgroup=mgroup, start_time=now) mgroup.is_runing = False mgroup.save()