package main

import (
	"runtime/debug"
	"time"

	"github.com/sirupsen/logrus"

	"lc/common/models"
	"lc/common/util"
)

var (
	StatYestodayStartMinute   = 30
	StatCurrentdayStartMinute = 10
)

// CalcLightingRate 临时使用
func CalcLightingRate( /*t*/ time.Time) {
	t, err1 := util.MlParseTime("2020-08-04 00:00:00")
	if err1 == nil {
		for !t.After(util.MlNow()) {
			models.CalcLightingRate(t)
			t = t.AddDate(0, 0, 1)
		}
	}
}

func GetNext15minite(t time.Time) time.Time {
	t = t.Add(-time.Duration(t.Second()) * time.Second)         //去掉秒钟部分
	t = t.Add(-time.Duration(t.Nanosecond()) * time.Nanosecond) //去掉秒钟部分
	if t.Minute()%15 == 0 {
		return t
	}
	m := t.Minute()
	next := 0
	t = t.Add(-time.Duration(t.Minute()) * time.Minute) //去掉分钟部分
	if m > 0 && m < 15 {
		next = 15
	} else if m > 15 && m < 30 {
		next = 30
	} else if m > 30 && m < 45 {
		next = 45
	} else if m > 45 && m < 60 {
		next = 60
	}
	t = t.Add(time.Duration(next) * time.Minute)
	return t
}

func StatData(args ...interface{}) interface{} {
	defer func() {
		if err := recover(); err != nil {
			logrus.Error("StatData发生异常:", err)
			logrus.Error("StatData发生异常,堆栈:", string(debug.Stack()))
			gopool.Add(StatData, 1)
		}
	}()
	LastHour := util.BeginningOfHour().Add(1 * time.Hour)
	LastDay := util.BeginningOfDay()
	NextCalcMinuteRate := GetNext15minite(util.MlNow())

	//小于0点半启动，则还需要统计昨日的
	if time.Now().Minute() < StatYestodayStartMinute && time.Now().Hour() == 0 {
		LastDay = LastDay.Add(-24 * time.Hour)
	}
	//小于整点过10分启动，则需要统计当日的
	if time.Now().Minute() < StatCurrentdayStartMinute {
		LastHour = LastHour.Add(-1 * time.Hour)
	}
	for {
		//每天0点半执行昨日统计
		if util.BeginningOfDay().After(LastDay) && time.Now().Minute() > StatYestodayStartMinute {
			if err := models.StatDay(LastDay); err != nil {
				logrus.Errorf("统计日[%s]数据错误:%s", LastDay.Format("2006-01-02 15:04:05"), err.Error())
			}
			//统计昨日能耗
			if err := models.StatDayEnergy(util.MlNow(), 1); err != nil {
				logrus.Errorf("统计日能耗[%s]数据错误:%s", LastDay.Format("2006-01-02 15:04:05"), err.Error())
			}
			//统计当月能耗
			if err := models.StatMonthEnergy(util.MlNow(), 0); err != nil {
				logrus.Errorf("统计日能耗[%s]数据错误:%s", LastDay.Format("2006-01-02 15:04:05"), err.Error())
			}
			//月初第一天，还需要计算上月的统计
			if util.MlNow().Day() == 1 {
				if err := models.StatMonthEnergy(util.MlNow(), 1); err != nil {
					logrus.Errorf("统计月能耗[%s]数据错误:%s", LastDay.Format("2006-01-02 15:04:05"), err.Error())
				}
			}
			LastDay = util.BeginningOfDay()
		}
		//每日7点后整点过10分执行当日统计
		if time.Now().After(LastHour) && time.Now().Minute() >= StatCurrentdayStartMinute && time.Now().Hour() >= 6 {
			if err := models.StatDay(util.BeginningOfDay()); err != nil {
				logrus.Errorf("统计日[%s]数据错误:%s", LastHour.Format("2006-01-02 15:04:05"), err.Error())
			}
			//统计当日能耗
			if err := models.StatDayEnergy(util.MlNow(), 0); err != nil {
				logrus.Errorf("统计日能耗[%s]数据错误:%s", LastDay.Format("2006-01-02 15:04:05"), err.Error())
			}
			//每日8点统计昨日亮灯时长
			if time.Now().Hour() == 8 {
				tt := util.New(LastHour).BeginningOfDay().Add(-24 * time.Hour)
				err := models.CalcLightingRate(tt)
				if err != nil {
					return nil
				}
			}
			LastHour = LastHour.Add(1 * time.Hour)
		}

		//每15分钟统计一次小时亮灯率
		if util.MlNow().After(NextCalcMinuteRate) {
			err := models.CalcLightingMiniteRate(NextCalcMinuteRate)
			if err != nil {
				return nil
			}
			NextCalcMinuteRate = NextCalcMinuteRate.Add(time.Duration(15) * time.Minute)
		}

		time.Sleep(5 * time.Second)
	}
	return 0
}
