# Create your tasks here
from __future__ import absolute_import, unicode_literals
from apps.utils.tasks import CustomTask
from celery import shared_task
from apps.mtm.models import Mgroup
from datetime import datetime, timedelta
from django.db.models import Sum
from apps.wpm.services import make_sflogs
from apps.wpm.models import SfLog, StLog, SfLogExp
from django.utils import timezone
from django.db.models import F
from apps.wpm.services import get_pcoal_heat
from django.core.cache import cache
import time


@shared_task(base=CustomTask)
def make_sflogs_simple(days, state_date: str, end_date: str):
    """
    根据班次规则生成今明两天的排班记录
    """
    if days:
        start_date = datetime.today()
        end_date = start_date + timedelta(days=days)
    else:
        start_date = datetime.strptime(state_date, "%Y-%m-%d").date()
        end_date = datetime.strptime(end_date, "%Y-%m-%d").date()
    mgroups = Mgroup.objects.exclude(shift_rule='').exclude(shift_rule=None).all()
    for mgroup in mgroups:
        make_sflogs(mgroup, start_date, end_date)


@shared_task(base=CustomTask)
def get_total_sec_now(sflogId: str='', now: datetime=None):
    """
    获取当前总时长, 当传入的是一个sflog时, 返回其total_sec_now
    否则更新所有total_sec_now
    """
    if now is None:
        now = timezone.now()
    if sflogId:
        sflog = SfLog.objects.get(id=sflogId)
        if sflog.end_time <= now:
            sflog.total_sec_now = sflog.total_sec
        else:
            total_sec_now = (now-sflog.start_time).total_seconds()
            sflog.total_sec_now = total_sec_now if total_sec_now > 0 else 0
        sflog.save()
        return sflog.total_sec_now
    else:
        SfLog.objects.filter(end_time__lte=now).exclude(
            total_sec_now=F('total_sec')).update(total_sec_now=F('total_sec'))
        sf_qs = SfLog.objects.filter(end_time__gt=now)
        for i in sf_qs:
            total_sec_now = (now-i.start_time).total_seconds()
            i.total_sec_now = total_sec_now if total_sec_now > 0 else 0
            i.save()


@shared_task(base=CustomTask)
def cal_exp_duration_sec(stlogId: str='', all=False, now: datetime=None):
    """
    计算异常记录对应的每班持续时间(只针对工段)
    """
    # from apps.enm.tasks import cal_enstat  # 如果是停机需要进行统计停机时长
    if stlogId:
        stlogs = StLog.objects.filter(id=stlogId)
    elif all:
        stlogs = StLog.objects.all()
    else:  # 不传就默认更新未结束的
        stlogs = StLog.objects.filter(end_time=None)|StLog.objects.filter(duration_sec=None)
    if now is None:
        now = timezone.now()
    for stlog in stlogs:
        if stlog.duration_sec is None and stlog.end_time:
            duration_sec = (stlog.end_time-stlog.start_time).total_seconds()
            if duration_sec <= 10:
                stlog.delete()
            else:
                stlog.duration_sec = duration_sec
                stlog.save()
        is_shutdown_stlog = True
        if stlog.is_shutdown is False:
            is_shutdown_stlog = False
        st_start = stlog.start_time
        if st_start >= now:
            break
        if stlog.end_time is None:  # 说明异常还未结束,此时也需要计算duration
            st_end = now
        else:
            st_end = stlog.end_time
        sf_qs = SfLog.objects.filter(mgroup=stlog.mgroup)
        sf_qs = (sf_qs.filter(start_time__gte=st_start, start_time__lt=st_end) | sf_qs.filter(end_time__gt=st_start,
                 end_time__lte=st_end) | sf_qs.filter(start_time__lte=st_start, end_time__gte=st_end)).order_by('start_time').distinct()
        SfLogExp.objects.filter(stlog=stlog).exclude(sflog__in=sf_qs).delete()
        for ind, sflog in enumerate(sf_qs):
            if ind == 0:
                stlog.sflog = sflog
                stlog.save()
            sflogexp, _ = SfLogExp.objects.get_or_create(stlog=stlog, sflog=sflog, defaults={
                                                         'stlog': stlog, 'sflog': sflog})
            # 计算duration
            sf_end, sf_start = sflog.end_time, sflog.start_time
            duration_item_delta = min(sf_end, st_end) - max(sf_start, st_start)
            total_seconds = duration_item_delta.total_seconds()
            sflogexp.duration_sec = total_seconds if total_seconds > 0 else 0
            sflogexp.save()
            if is_shutdown_stlog:
                # 计算每班的总停机时间
                ret = SfLogExp.objects.filter(
                    sflog=sflog, stlog__is_shutdown=True).aggregate(sum=Sum('duration_sec'))
                if ret.get('sum', 0):
                    get_total_sec_now(sflog.id)
                    sflog.shut_sec = ret['sum']
                    sflog.save()
                    # 更新sflog总时长

                    # if stlogId:
                    #     cal_enstat('sflog', sflog.id, sflog.mgroup.id, None, None, None,
                    #                None, None, None, None, cascade=True, cal_attrs=['run_hour'])

@shared_task(base=CustomTask)
def cal_enstat_when_pcoal_heat_change(year_s, month_s, day_s):
    from apps.enm.models import EnStat
    from apps.enm.tasks import cal_enstat_pcoal_change, cal_enstat2

    pcoal_heat = get_pcoal_heat(year_s, month_s, day_s)
    # 只会影响到回转窑及水泥磨数据
    enstats = EnStat.objects.filter(mgroup__name='回转窑', year_s=year_s,
                                    month_s=month_s, day_s=day_s, type__in=['hour_s', 'sflog', 'day_s'])
    for enstat in enstats:
        cal_enstat_pcoal_change(enstat, pcoal_heat)
    enstats_other = EnStat.objects.filter(mgroup__name='回转窑', year_s=year_s, month_s=month_s, type__in=[
                                          'month_st', 'month_s']) | EnStat.objects.filter(mgroup__name='回转窑', year_s=year_s, type__in=['year_s'])
    for enstat in enstats_other:
        cal_enstat_pcoal_change(enstat, pcoal_heat)

    cal_enstat2('day_s', year_s, month_s, day_s)


@shared_task(base=CustomTask)
def cal_enstat_when_team_change(sflogId):
    from apps.enm.tasks import cal_enstat
    sflog = SfLog.objects.get(id=sflogId)
    cal_enstat('month_st', sflogId, sflog.mgroup.id, None,
               None, None, None, None, None, None, False, [])
