from apps.wpm.models import SfLog, StLog, StSfLog from apps.mtm.models import Shift, Mgroup import datetime from django.utils.timezone import localtime from django.db.models import Sum from django.core.cache import cache def make_sflogs(mgroup: Mgroup, start_date: datetime.date, end_date: datetime.date): for shift in Shift.objects.all(): start_time_o = shift.start_time_o end_time_o = shift.end_time_o current_date = start_date while current_date <= end_date: start_time = datetime.datetime.combine(current_date, start_time_o) end_time = datetime.datetime.combine(current_date, end_time_o) if start_time > end_time: start_time -= datetime.timedelta(days=1) SfLog.objects.get_or_create(mgroup=mgroup, shift=shift, start_time=start_time, defaults={ "mgroup": mgroup, "shift": shift, "start_time": start_time, "end_time": end_time, }) current_date = current_date + datetime.timedelta(days=1) def get_pcoal_val(year_s: int, month_s: int, day_s: int): """ 获取煤粉热值 只有回转窑需要录入煤粉热值 """ key = f'pgoal_val_{year_s}_{month_s}_{day_s}' pcoal_val = cache.get(key, None) if pcoal_val is not None: return pcoal_val else: try: qs = SfLog.objects.get(end_time__year=year_s, end_time__month=month_s, end_time__day=day_s, mgroup__name='回转窑', shift__name='夜班') if qs.pcoal_val is None: qs.pcoal_val = 0 qs.save() cache.set(f'pgoal_val_{year_s}_{month_s}_{day_s}', qs.pcoal_val) return qs.pcoal_val except: return 0