Ver código fonte

crontab: neck_ring_merge update

ping 3 semanas atrás
pai
commit
06688e8ee0
3 arquivos alterados com 121 adições e 30 exclusões
  1. 0 27
      module/crontab/model.go
  2. 119 2
      module/crontab/neck_ring_merge.go
  3. 2 1
      module/crontab/sql.go

+ 0 - 27
module/crontab/model.go

@@ -1,20 +1,5 @@
 package crontab
 
-/*
-	type XToday struct {
-		XBegDate       string
-		XEndDate       string
-		LastMaxHabitId int64
-		CurrMaxHabitId int64
-		XMin2Id        int64
-		XMin7Id        int64
-		ActiveLowest   int64
-		RuminaLowest   int64
-		ActiveLow      int64
-		ActiveMiddle   int64
-		ActiveHigh     int64
-	}
-*/
 type WeekHabit struct {
 	NeckRingNumber    string
 	WeekHighHabit     int32
@@ -25,18 +10,6 @@ type WeekHabit struct {
 	WeekOtherHabit    int32
 }
 
-/*
-	type SumHabit struct {
-		NeckRingNumber string
-		SumRumina      int32
-		SumIntake      int32
-		SumInactive    int32
-		SumActive      int32
-		SumMaxHigh     int32
-		SumMinHigh     int32
-		SumMinChew     int32
-	}
-*/
 type ActivityVolume struct {
 	NeckRingNumber string
 	AvgFilter      float64

+ 119 - 2
module/crontab/neck_ring_merge.go

@@ -345,6 +345,7 @@ func (e *Entry) FirstFilterUpdate(pastureId int64, xToDay *XToday) (processIds [
 	return processIds, nil
 }
 
+// WeeklyUpdateActiveHabit  时间点周平均值计算
 func (e *Entry) WeeklyUpdateActiveHabit(pastureId int64, processIds []int64, xToDay *XToday) (err error) {
 	newNeckActiveHabitList := make([]*model.NeckActiveHabit, 0)
 	if err = e.DB.Model(new(model.NeckActiveHabit)).
@@ -356,6 +357,123 @@ func (e *Entry) WeeklyUpdateActiveHabit(pastureId int64, processIds []int64, xTo
 	for _, v := range newNeckActiveHabitList {
 		// 前七天的
 		weekHabitData := e.FindWeekHabitData(pastureId, v.NeckRingNumber, v.HeatDate, v.Frameid, xToDay)
+		// 更新过滤值
+		if err = e.DB.Model(new(model.NeckActiveHabit)).
+			Select("week_high_habit", "week_rumina_habit", "week_chew_habit", "week_intake_habit", "week_inactive_habit").
+			Where("id = ?", v.Id).
+			Updates(map[string]interface{}{
+				"week_high_habit":     weekHabitData.WeekHighHabit,
+				"week_rumina_habit":   weekHabitData.WeekRuminaHabit,
+				"week_chew_habit":     weekHabitData.WeekChewHabit,
+				"week_intake_habit":   weekHabitData.WeekIntakeHabit,
+				"week_inactive_habit": weekHabitData.WeekInactiveHabit,
+			}).Error; err != nil {
+			zaplog.Error("WeeklyUpdateActiveHabit",
+				zap.Error(err),
+				zap.Any("NeckActiveHabit", v),
+				zap.Any("pastureId", pastureId),
+			)
+		}
+	}
+
+	if err = e.SumUpdateActiveHabit(pastureId, newNeckActiveHabitList, xToDay); err != nil {
+		zaplog.Error("WeeklyUpdateActiveHabit",
+			zap.Any("SumUpdateActiveHabit", err),
+			zap.Any("newNeckActiveHabitList", newNeckActiveHabitList),
+			zap.Any("pastureId", pastureId),
+		)
+	}
+
+	if err = e.ActiveChange(pastureId, processIds, xToDay); err != nil {
+		zaplog.Error("WeeklyUpdateActiveHabit",
+			zap.Any("ActiveChange", err),
+			zap.Any("processIds", processIds),
+			zap.Any("xToDay", xToDay),
+			zap.Any("pastureId", pastureId),
+		)
+	}
+
+	return nil
+}
+
+// SumUpdateActiveHabit -- 累计24小时数值
+func (e *Entry) SumUpdateActiveHabit(pastureId int64, newNeckActiveHabitList []*model.NeckActiveHabit, xToDay *XToday) (err error) {
+	for _, v := range newNeckActiveHabitList {
+		sumHabitData := e.FindSumHabitData(pastureId, v.NeckRingNumber, v.HeatDate, v.Frameid, xToDay)
+		// 更新过滤值
+		if err = e.DB.Model(new(model.NeckActiveHabit)).
+			Select("sum_rumina", "sum_intake", "sum_inactive", "sum_active", "sum_max_high", "sum_min_high", "sum_min_chew").
+			Where("id = ?", v.Id).
+			Updates(map[string]interface{}{
+				"sum_rumina":   sumHabitData.SumRumina,
+				"sum_intake":   sumHabitData.SumIntake,
+				"sum_inactive": sumHabitData.SumInactive,
+				"sum_active":   sumHabitData.SumActive,
+				"sum_max_high": sumHabitData.SumMaxHigh,
+				"sum_min_high": sumHabitData.SumMinHigh,
+				"sum_min_chew": sumHabitData.SumMinChew,
+			}).Error; err != nil {
+			zaplog.Error("WeeklyUpdateActiveHabit",
+				zap.Any("err", err),
+				zap.Any("NeckActiveHabit", v),
+				zap.Any("pastureId", pastureId),
+			)
+		}
+	}
+	return err
+}
+
+// ActiveChange -- 变化百分比
+func (e *Entry) ActiveChange(pastureId int64, processIds []int64, xToDay *XToday) (err error) {
+	newNeckActiveHabitList := make([]*model.NeckActiveHabit, 0)
+	if err = e.DB.Model(new(model.NeckActiveHabit)).
+		Where("id IN (?)", processIds).
+		Where("week_high_habit > ?", 0).
+		Where(e.DB.Where("high >= ?", xToDay.High).Or("rumina >= ?", xToDay.Rumina)).
+		Find(&newNeckActiveHabitList).Error; err != nil {
+		return xerr.WithStack(err)
+	}
+	for _, v := range newNeckActiveHabitList {
+		highDiff := v.FilterHigh - v.WeekHighHabit
+		denominator := float64(v.WeekHigh)*0.6 + float64(v.WeekHighHabit)*0.2 + float64(xToDay.WeeklyActive)*0.2
+		if highDiff > 0 {
+			v.ChangeHigh = int32(math.Round((float64(highDiff) / denominator / float64(v.WeekHighHabit)) * 100))
+		} else {
+			v.ChangeHigh = int32(math.Round(float64(highDiff) / float64(v.WeekHighHabit) * 100))
+		}
+
+		if v.WeekRuminaHabit != 0 {
+			v.ChangeRumina = int32(math.Round(float64(v.FilterRumina-v.WeekRuminaHabit) / float64(v.WeekRuminaHabit) * 100))
+		}
+
+		if v.WeekChewHabit != 0 {
+			v.ChangeChew = int32(math.Round(float64(v.FilterChew-v.WeekChewHabit) / float64(v.WeekChewHabit) * 100))
+		}
+
+		// 更新过滤值
+		if err = e.DB.Model(new(model.NeckActiveHabit)).
+			Select("change_high", "change_rumina", "change_chew").
+			Where("id = ?", v.Id).
+			Updates(map[string]interface{}{
+				"change_high":   v.ChangeHigh,
+				"change_rumina": v.ChangeRumina,
+				"change_chew":   v.ChangeChew,
+			}).Error; err != nil {
+			zaplog.Error("WeeklyUpdateActiveHabit",
+				zap.Error(err),
+				zap.Any("NeckActiveHabit", v),
+				zap.Any("pastureId", pastureId),
+			)
+		}
+	}
+	return err
+}
+
+func (e *Entry) WeeklyUpdateActiveHabitOld(pastureId int64, newNeckActiveHabitList []*model.NeckActiveHabit, xToDay *XToday) (err error) {
+	for _, v := range newNeckActiveHabitList {
+		// 前七天的
+		weekHabitData := e.FindWeekHabitData(pastureId, v.NeckRingNumber, v.HeatDate, v.Frameid, xToDay)
+
 		highDiff := v.FilterHigh - weekHabitData.WeekHighHabit
 		denominator := float64(v.WeekHigh)*0.6 + float64(weekHabitData.WeekHighHabit)*0.2 + float64(xToDay.WeeklyActive)*0.2
 		if highDiff > 0 {
@@ -388,7 +506,7 @@ func (e *Entry) WeeklyUpdateActiveHabit(pastureId int64, processIds []int64, xTo
 				"week_rumina_habit":   weekHabitData.WeekRuminaHabit,
 				"week_chew_habit":     weekHabitData.WeekChewHabit,
 				"week_intake_habit":   weekHabitData.WeekIntakeHabit,
-				"week_inactive_habit": weekHabitData.WeekIntakeHabit,
+				"week_inactive_habit": weekHabitData.WeekInactiveHabit,
 				"sum_rumina":          sumHabitData.SumRumina,
 				"sum_intake":          sumHabitData.SumIntake,
 				"sum_inactive":        sumHabitData.SumInactive,
@@ -409,7 +527,6 @@ func (e *Entry) WeeklyUpdateActiveHabit(pastureId int64, processIds []int64, xTo
 	}
 	return err
 }
-
 func (e *Entry) Before3DaysNeckActiveHabit(pastureId int64, processIds []int64, xToDay *XToday) (err error) {
 	newNeckActiveHabitList := make([]*model.NeckActiveHabit, 0)
 	if err = e.DB.Model(new(model.NeckActiveHabit)).

+ 2 - 1
module/crontab/sql.go

@@ -209,7 +209,8 @@ func (e *Entry) FindWeekHabitData(pastureId int64, neckRingNumber, heatDate stri
 		Where("neck_ring_number = ? ", neckRingNumber).
 		Where("frameid = ?", frameid).
 		Where(e.DB.Where("high > ?", xToday.High).Or("rumina >= ?", xToday.Rumina)).
-		Group("neck_ring_number").First(weekHabitData).Error; err != nil {
+		Group("neck_ring_number").
+		First(weekHabitData).Error; err != nil {
 		zaplog.Error("WeeklyActiveAvgUpdate-1",
 			zap.Any("error", err),
 			zap.Any("neckRingNumber", neckRingNumber),