Browse Source

crontab: update

Yi 2 weeks ago
parent
commit
ea52b37996

+ 20 - 16
model/neck_active_habit.go

@@ -43,12 +43,12 @@ type NeckActiveHabit struct {
 	FilterRumina         int32                 `json:"filterRumina"`
 	FilterChew           int32                 `json:"filterChew"`
 	WeekHigh             int32                 `json:"weekHigh"`
-	WeekHighHabit        int32                 `json:"weekHighHabit"`
-	WeekRuminaHabit      int32                 `json:"weekRuminaHabit"`
-	WeekIntakeHabit      int32                 `json:"weekIntakeHabit"`
-	WeekChewHabit        int32                 `json:"weekChewHabit"`
-	WeekInactiveHabit    int32                 `json:"weekInactiveHabit"`
-	WeekOtherHabit       int32                 `json:"weekOtherHabit"`
+	HighHabit            int32                 `json:"highHabit"`
+	RuminaHabit          int32                 `json:"ruminaHabit"`
+	IntakeHabit          int32                 `json:"intakeHabit"`
+	ChewHabit            int32                 `json:"chewHabit"`
+	InactiveHabit        int32                 `json:"inactiveHabit"`
+	OtherHabit           int32                 `json:"otherHabit"`
 	ChangeHigh           int32                 `json:"changeHigh"`
 	ChangeRumina         int32                 `json:"changeRumina"`
 	ChangeChew           int32                 `json:"changeChew"`
@@ -79,6 +79,20 @@ func (n *NeckActiveHabit) TableName() string {
 	return "neck_active_habit"
 }
 
+func (n *NeckActiveHabit) SumAvg() {
+	n.Rumina = n.Rumina / n.RecordCount * n.RecordCount
+	n.Inactive = n.Inactive / n.RecordCount * n.RecordCount
+	n.Active = n.Active / n.RecordCount * n.RecordCount
+	n.Intake = n.Intake / n.RecordCount * n.RecordCount
+	n.Other = n.Other / n.RecordCount * n.RecordCount
+	n.Gasp = n.Gasp / n.RecordCount * n.RecordCount
+	n.High = n.High / n.RecordCount * n.RecordCount
+}
+
+func (n *NeckActiveHabit) UpdateIsShowOk() {
+	n.IsShow = pasturePb.IsShow_Ok
+}
+
 func NewNeckActiveHabit(data *NeckRingOriginalMerge) *NeckActiveHabit {
 	return &NeckActiveHabit{
 		PastureId:       data.PastureId,
@@ -199,16 +213,6 @@ func (n NeckActiveHabitSlice) ToPB(curveName string) *CowBehaviorCurveData {
 	return res
 }
 
-func (n *NeckActiveHabit) SumAvg() {
-	n.Rumina = n.Rumina / n.RecordCount * n.RecordCount
-	n.Inactive = n.Inactive / n.RecordCount * n.RecordCount
-	n.Active = n.Active / n.RecordCount * n.RecordCount
-	n.Intake = n.Intake / n.RecordCount * n.RecordCount
-	n.Other = n.Other / n.RecordCount * n.RecordCount
-	n.Gasp = n.Gasp / n.RecordCount * n.RecordCount
-	n.High = n.High / n.RecordCount * n.RecordCount
-}
-
 type MaxHabitIdModel struct {
 	Id int64 `json:"id"`
 }

+ 11 - 8
module/crontab/model.go

@@ -1,13 +1,13 @@
 package crontab
 
 type WeekHabit struct {
-	NeckRingNumber    string
-	WeekHighHabit     int32
-	WeekRuminaHabit   int32
-	WeekChewHabit     int32
-	WeekInactiveHabit int32
-	WeekIntakeHabit   int32
-	WeekOtherHabit    int32
+	NeckRingNumber string
+	HighHabit      int32
+	RuminaHabit    int32
+	ChewHabit      int32
+	InactiveHabit  int32
+	IntakeHabit    int32
+	OtherHabit     int32
 }
 
 type ActivityVolume struct {
@@ -56,11 +56,14 @@ type XToday struct {
 	WeeklyActive    int32
 }
 
-type FirstFilterData struct {
+type FilterData struct {
 	NeckRingNumber string
 	FilterHigh     int32
 	FilterRumina   int32
 	FilterChew     int32
+	ChangeFilter   int32
+	RuminaFilter   int32
+	ChewFilter     int32
 }
 
 type SumHabit struct {

+ 192 - 282
module/crontab/neck_ring_calculate.go

@@ -4,7 +4,6 @@ import (
 	"fmt"
 	"kpt-pasture/model"
 	"math"
-	"sort"
 	"time"
 
 	pasturePb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/cow"
@@ -38,49 +37,17 @@ func (e *Entry) NeckRingCalculate() error {
 
 func (e *Entry) EntryUpdateActiveHabit(pastureId int64) (err error) {
 	// 获取这段执行数据内最大日期和最小日期
-	xToday := &XToday{}
-	systemConfigureList, err := e.GetSystemNeckRingConfigure(pastureId)
+	xToday, err := e.XToday(pastureId)
 	if err != nil {
 		return xerr.WithStack(err)
 	}
-	for _, v := range systemConfigureList {
-		switch v.Name {
-		case model.MaxHabit:
-			xToday.LastMaxHabitId = v.Value
-		case model.High:
-			xToday.High = int32(v.Value)
-		case model.Rumina:
-			xToday.Rumina = int32(v.Value)
-		case model.XRuminaDisc:
-			xToday.XRuminaDisc = int32(v.Value)
-		case model.XChangeDiscount:
-			xToday.XChangeDiscount = int32(v.Value)
-		case model.WeeklyActive:
-			xToday.WeeklyActive = int32(v.Value)
-		}
-	}
-
-	currMaxHabit := &model.NeckActiveHabit{}
-	if err = e.DB.Model(new(model.NeckActiveHabit)).
-		Where("id > ?", xToday.LastMaxHabitId).
-		Where("pasture_id = ?", pastureId).
-		Where("is_show = ?", pasturePb.IsShow_No).
-		Order("id desc").
-		First(currMaxHabit).Error; err != nil {
-		return xerr.WithStack(err)
-	}
-
-	if currMaxHabit.Id <= 0 || currMaxHabit.Id <= xToday.LastMaxHabitId {
-		return nil
-	}
-
-	xToday.CurrMaxHabitId = currMaxHabit.Id
 	var processIds []int64
 	// 更新活动滤波
 	processIds, err = e.FirstFilterUpdate(pastureId, xToday)
 	if err != nil {
 		zaplog.Error("NeckRingCalculate", zap.Any("FirstFilterUpdate", err), zap.Any("xToday", xToday))
 	}
+
 	zaplog.Info("NeckRingCalculate", zap.Any("xToday", xToday), zap.Any("processIds", processIds))
 	if len(processIds) <= 0 {
 		return nil
@@ -90,9 +57,6 @@ func (e *Entry) EntryUpdateActiveHabit(pastureId int64) (err error) {
 		zaplog.Error("NeckRingCalculate", zap.Any("WeeklyUpdateActiveHabit", err), zap.Any("xToday", xToday))
 	}
 
-	if err = e.Before3DaysNeckActiveHabit(pastureId, processIds, xToday); err != nil {
-		zaplog.Error("NeckRingCalculate", zap.Any("Before3DaysNeckActiveHabit", err), zap.Any("xToday", xToday))
-	}
 	// 二次更新滤波
 	if err = e.SecondUpdateChangeFilter(pastureId, xToday); err != nil {
 		zaplog.Error("NeckRingCalculate", zap.Any("SecondUpdateChangeFilter", err), zap.Any("xToday", xToday))
@@ -140,17 +104,6 @@ func (e *Entry) EntryUpdateActiveHabit(pastureId int64) (err error) {
 		zaplog.Error("UpdateChangeAdJust-2", zap.Any("error", err), zap.Any("xToday", xToday))
 	}
 
-	sort.Slice(processIds, func(i, j int) bool {
-		return processIds[i] > processIds[j]
-	})
-	// 更新日志
-	if err = e.DB.Model(new(model.NeckRingConfigure)).
-		Where("name = ?", model.MaxHabit).
-		Where("pasture_id = ?", pastureId).
-		Update("value", processIds[len(processIds)-1]).Error; err != nil {
-		zaplog.Error("NeckRingCalculate", zap.Any("MaxHabit", err), zap.Any("xToday", xToday))
-	}
-
 	// 健康预警
 	if err = e.HealthWarning(pastureId, processIds); err != nil {
 		zaplog.Error("EntryUpdateActiveHabit", zap.Any("HealthWarning", err))
@@ -162,11 +115,12 @@ func (e *Entry) EntryUpdateActiveHabit(pastureId int64) (err error) {
 func (e *Entry) FirstFilterUpdate(pastureId int64, xToDay *XToday) (processIds []int64, err error) {
 	newNeckActiveHabitList := make([]*model.NeckActiveHabit, 0)
 	if err = e.DB.Model(new(model.NeckActiveHabit)).
-		Where("id > ?", xToDay.LastMaxHabitId).
+		Where("heat_date >= ?", "2025-03-01").
 		Where("pasture_id = ?", pastureId).
 		Where("is_show = ?", pasturePb.IsShow_No).
 		Where("record_count = ?", model.DefaultRecordCount).
 		Where(e.DB.Where("high >= ?", xToDay.High).Or("rumina >= ?", xToDay.Rumina)).
+		Where("cow_id > ?", 0).
 		Order("heat_date,neck_ring_number,frameid").
 		Limit(int(defaultLimit)).
 		Find(&newNeckActiveHabitList).Error; err != nil {
@@ -178,10 +132,11 @@ func (e *Entry) FirstFilterUpdate(pastureId int64, xToDay *XToday) (processIds [
 		// 过滤牛只未绑定的脖环的数据
 		cowInfo := e.GetCowInfoByNeckRingNumber(v.PastureId, v.NeckRingNumber)
 		if cowInfo == nil || cowInfo.Id <= 0 {
+			v.UpdateIsShowOk()
 			if err = e.DB.Model(new(model.NeckActiveHabit)).
+				Select("is_show").
 				Where("id = ?", v.Id).
-				Where("pasture_id = ?", v.PastureId).
-				Update("is_show", pasturePb.IsShow_Ok).Error; err != nil {
+				Updates(v).Error; err != nil {
 				zaplog.Error("EntryUpdateActiveHabit", zap.Any("error", err))
 			}
 			continue
@@ -197,7 +152,7 @@ func (e *Entry) FirstFilterUpdate(pastureId int64, xToDay *XToday) (processIds [
 			frameId -= 1
 		}
 
-		firstFilterData := e.FindFirstFilter(pastureId, v.NeckRingNumber, heatDate, frameId)
+		firstFilterData := e.FindFilterData(pastureId, v.NeckRingNumber, heatDate, frameId)
 		if v.FilterHigh > 0 {
 			firstFilterData.FilterHigh = v.FilterHigh
 		} else {
@@ -253,223 +208,12 @@ 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)).
-		Where("id IN (?)", processIds).
-		Order("heat_date,neck_ring_number,frameid").
-		Find(&newNeckActiveHabitList).Error; err != nil {
-		return xerr.WithStack(err)
-	}
-	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 {
-			v.ChangeHigh = int32(math.Round((float64(highDiff) / denominator) * 100))
-		} else {
-			v.ChangeHigh = int32(math.Round(float64(highDiff) / denominator * 100))
-		}
-
-		if weekHabitData.WeekRuminaHabit != 0 {
-			v.ChangeRumina = int32(math.Round(float64(v.FilterRumina-weekHabitData.WeekRuminaHabit) / float64(weekHabitData.WeekRuminaHabit) * 100))
-		} else {
-			v.ChangeRumina = 0
-		}
-		if weekHabitData.WeekChewHabit != 0 {
-			v.ChangeChew = int32(math.Round(float64(v.FilterChew-weekHabitData.WeekChewHabit) / float64(weekHabitData.WeekChewHabit) * 100))
-		} else {
-			v.ChangeChew = 0
-		}
-
-		sumHabitData := e.FindSumHabitData(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",
-				"sum_rumina", "sum_intake", "sum_inactive", "sum_active", "sum_max_high", "sum_min_high", "sum_min_chew",
-				"change_high", "change_rumina", "change_chew", "before_three_sum_rumina", "before_three_sum_intake",
-			).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,
-				"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,
-				"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) Before3DaysNeckActiveHabit(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).
-		Order("heat_date,neck_ring_number,frameid").
-		Find(&newNeckActiveHabitList).Error; err != nil {
-		return xerr.WithStack(err)
-	}
-	for _, v := range newNeckActiveHabitList {
-		before3DaysNeckActiveHabit := e.FindBefore3DaysNeckActiveHabit(pastureId, v.NeckRingNumber, v.HeatDate, v.Frameid)
-		// 更新过滤值
-		if err = e.DB.Model(new(model.NeckActiveHabit)).
-			Select("before_three_sum_rumina", "before_three_sum_intake").
-			Where("id = ?", v.Id).
-			Updates(map[string]interface{}{
-				"before_three_sum_rumina": before3DaysNeckActiveHabit.SumRumina,
-				"before_three_sum_intake": before3DaysNeckActiveHabit.SumIntake,
-			}).Error; err != nil {
-			zaplog.Error("Before3DaysNeckActiveHabit",
-				zap.Error(err),
-				zap.Any("NeckActiveHabit", v),
-				zap.Any("pastureId", pastureId),
-			)
-		}
-	}
-	return nil
-}
-
 // SecondUpdateChangeFilter 第二次更新变化趋势滤波
 func (e *Entry) SecondUpdateChangeFilter(pastureId int64, xToday *XToday) (err error) {
 	newChangeFilterList := make([]*ChangeFilterData, 0)
 	if err = e.DB.Model(new(model.NeckActiveHabit)).
 		Select("id", "neck_ring_number", "change_high", "change_filter", "rumina_filter", "change_rumina",
 			"chew_filter", "change_chew", "heat_date", "frameid", "IF(lact = 0, 0.8, 1) as xlc_dis_count").
-		Where("id > ?", xToday.LastMaxHabitId).
 		Where("heat_date >= ?", time.Now().AddDate(0, 0, -2).Format(model.LayoutDate2)).
 		Where("pasture_id = ?", pastureId).
 		Where("change_filter = ?", model.InitChangeFilter).
@@ -481,7 +225,6 @@ func (e *Entry) SecondUpdateChangeFilter(pastureId int64, xToday *XToday) (err e
 	}
 
 	for _, v := range newChangeFilterList {
-		secondFilterData := &SecondFilterData{}
 		frameId := v.FrameId
 		heatDate := v.HeatDate
 		if v.FrameId == 0 {
@@ -491,57 +234,51 @@ func (e *Entry) SecondUpdateChangeFilter(pastureId int64, xToday *XToday) (err e
 		} else {
 			frameId -= 1
 		}
-		if err = e.DB.Model(new(model.NeckActiveHabit)).
-			Select("neck_ring_number", "filter_high", "filter_rumina", "filter_chew").
-			Where("neck_ring_number = ?", v.NeckRingNumber).
-			Where("heat_date = ?", heatDate).
-			Where("frameid = ?", frameId).
-			First(&secondFilterData).Error; err != nil {
-			zaplog.Error("EntryUpdateActiveHabit", zap.Any("FirstFilterUpdate", err))
-		}
 
+		secondFilterData := e.FindFilterData(pastureId, v.NeckRingNumber, heatDate, frameId)
 		if v.ChangeFilter > MinChangeFilter {
-			secondFilterData.ChangeFilter = float64(v.ChangeFilter)
+			secondFilterData.ChangeFilter = v.ChangeFilter
 		} else {
 			if v.NeckRingNumber == secondFilterData.NeckRingNumber {
-				secondFilterData.ChangeFilter = secondFilterData.ChangeFilter*(1-(float64(xToday.XChangeDiscount)/10)*v.XlcDisCount) +
-					math.Min(float64(v.ChangeHigh), secondFilterData.ChangeFilter+135)*(float64(xToday.XChangeDiscount)/10)*v.XlcDisCount
+				changeFilter := float64(secondFilterData.ChangeFilter)*(1-(float64(xToday.XChangeDiscount)/10)*v.XlcDisCount) +
+					math.Min(float64(v.ChangeHigh), float64(secondFilterData.ChangeFilter)+135)*(float64(xToday.XChangeDiscount)/10)*v.XlcDisCount
+				secondFilterData.ChangeFilter = int32(changeFilter)
 			} else {
 				secondFilterData.ChangeFilter = 0
 			}
 		}
 
 		if v.RuminaFilter > MinRuminaFilter {
-			secondFilterData.RuminaFilter = float64(v.ChangeFilter)
+			secondFilterData.RuminaFilter = v.ChangeFilter
 		} else {
 			if v.NeckRingNumber == secondFilterData.NeckRingNumber {
 				discount := float64(xToday.XRuminaDisc) / 10 * v.XlcDisCount
 				if math.Abs(float64(v.ChangeRumina)) > 60 {
 					discount *= 0.5
 				}
-				secondFilterData.RuminaFilter = secondFilterData.RuminaFilter*(1-discount) + float64(v.ChangeRumina)*discount
+				secondFilterData.RuminaFilter = int32(float64(secondFilterData.RuminaFilter)*(1-discount) + float64(v.ChangeRumina)*discount)
 			} else {
 				secondFilterData.RuminaFilter = 0
 			}
 		}
 
-		secondFilterData.RuminaFilter = math.Min(50, secondFilterData.RuminaFilter)
+		secondFilterData.RuminaFilter = int32(math.Min(50, float64(secondFilterData.RuminaFilter)))
 
 		if v.ChewFilter > MinChewFilter {
-			secondFilterData.ChewFilter = float64(v.ChangeChew)
+			secondFilterData.ChewFilter = v.ChangeChew
 		} else {
 			if v.NeckRingNumber == secondFilterData.NeckRingNumber {
 				discount := float64(xToday.XRuminaDisc) / 10
 				if math.Abs(float64(v.ChangeChew)) > 60 {
 					discount *= 0.5
 				}
-				secondFilterData.ChewFilter = secondFilterData.ChewFilter*(1-discount) + float64(v.ChangeChew)*discount
+				secondFilterData.ChewFilter = int32(float64(secondFilterData.ChewFilter)*(1-discount) + float64(v.ChangeChew)*discount)
 			} else {
 				secondFilterData.ChewFilter = 0
 			}
 		}
 
-		secondFilterData.ChewFilter = math.Min(50, secondFilterData.ChewFilter)
+		secondFilterData.ChewFilter = int32(math.Min(50, float64(secondFilterData.ChewFilter)))
 		if err = e.DB.Model(new(model.NeckActiveHabit)).
 			Select("change_filter", "rumina_filter", "chew_filter").
 			Where("id = ?", v.Id).
@@ -658,3 +395,176 @@ func (e *Entry) UpdateChangeAdJust(pastureId int64, xToday *XToday) error {
 
 	return nil
 }
+
+func (e *Entry) XToday(pastureId int64) (*XToday, error) {
+	xToday := &XToday{}
+	systemConfigureList, err := e.GetSystemNeckRingConfigure(pastureId)
+	if err != nil {
+		return nil, xerr.WithStack(err)
+	}
+	for _, v := range systemConfigureList {
+		switch v.Name {
+		case model.MaxHabit:
+			xToday.LastMaxHabitId = v.Value
+		case model.High:
+			xToday.High = int32(v.Value)
+		case model.Rumina:
+			xToday.Rumina = int32(v.Value)
+		case model.XRuminaDisc:
+			xToday.XRuminaDisc = int32(v.Value)
+		case model.XChangeDiscount:
+			xToday.XChangeDiscount = int32(v.Value)
+		case model.WeeklyActive:
+			xToday.WeeklyActive = int32(v.Value)
+		}
+	}
+	return xToday, 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)).
+		Where("id IN (?)", processIds).
+		Order("heat_date,neck_ring_number,frameid").
+		Find(&newNeckActiveHabitList).Error; err != nil {
+		return xerr.WithStack(err)
+	}
+
+	if len(newNeckActiveHabitList) <= 0 {
+		return nil
+	}
+
+	e.HabitUpdateActiveHabit(pastureId, newNeckActiveHabitList, xToDay)
+	e.SumUpdateActiveHabit(pastureId, newNeckActiveHabitList, xToDay)
+	e.ActiveChange(pastureId, processIds, xToDay)
+	e.Before3DaysNeckActiveHabit(pastureId, processIds)
+	return nil
+}
+
+func (e *Entry) HabitUpdateActiveHabit(pastureId int64, newNeckActiveHabitList []*model.NeckActiveHabit, xToDay *XToday) {
+	for _, v := range newNeckActiveHabitList {
+		// 前七天的
+		weekHabitData := e.FindWeekHabitData(pastureId, v.NeckRingNumber, v.HeatDate, v.Frameid, xToDay)
+		// 更新过滤值
+		if err := e.DB.Model(new(model.NeckActiveHabit)).
+			Select("high_habit", "rumina_habit", "chew_habit", "intake_habit", "inactive_habit").
+			Where("id = ?", v.Id).
+			Updates(map[string]interface{}{
+				"high_habit":     weekHabitData.HighHabit,
+				"rumina_habit":   weekHabitData.RuminaHabit,
+				"chew_habit":     weekHabitData.ChewHabit,
+				"intake_habit":   weekHabitData.IntakeHabit,
+				"inactive_habit": weekHabitData.InactiveHabit,
+			}).Error; err != nil {
+			zaplog.Error("WeeklyUpdateActiveHabit",
+				zap.Error(err),
+				zap.Any("NeckActiveHabit", v),
+				zap.Any("pastureId", pastureId),
+			)
+		}
+	}
+}
+
+// SumUpdateActiveHabit -- 累计24小时数值
+func (e *Entry) SumUpdateActiveHabit(pastureId int64, newNeckActiveHabitList []*model.NeckActiveHabit, xToDay *XToday) {
+	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),
+			)
+		}
+	}
+}
+
+// ActiveChange -- 变化百分比
+func (e *Entry) ActiveChange(pastureId int64, processIds []int64, xToDay *XToday) {
+	newNeckActiveHabitList := make([]*model.NeckActiveHabit, 0)
+	if err := e.DB.Model(new(model.NeckActiveHabit)).
+		Where("id IN (?)", processIds).
+		Where("high_habit > ?", 0).
+		Where(e.DB.Where("high >= ?", xToDay.High).Or("rumina >= ?", xToDay.Rumina)).
+		Find(&newNeckActiveHabitList).Error; err != nil {
+		zaplog.Error("ActiveChange", zap.Any("error", err), zap.Any("processIds", processIds))
+	}
+
+	for _, v := range newNeckActiveHabitList {
+		highDiff := v.FilterHigh - v.HighHabit
+		denominator := float64(v.WeekHigh)*0.6 + float64(v.HighHabit)*0.2 + float64(xToDay.WeeklyActive)*0.2
+		if highDiff > 0 {
+			v.ChangeHigh = int32(math.Round((float64(highDiff) / denominator / float64(v.HighHabit)) * 100))
+		} else {
+			v.ChangeHigh = int32(math.Round(float64(highDiff) / float64(v.HighHabit) * 100))
+		}
+
+		if v.RuminaHabit != 0 {
+			v.ChangeRumina = int32(math.Round(float64(v.FilterRumina-v.RuminaHabit) / float64(v.RuminaHabit) * 100))
+		}
+
+		if v.ChewHabit != 0 {
+			v.ChangeChew = int32(math.Round(float64(v.FilterChew-v.ChewHabit) / float64(v.ChewHabit) * 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),
+			)
+		}
+	}
+}
+
+func (e *Entry) Before3DaysNeckActiveHabit(pastureId int64, processIds []int64) {
+	newNeckActiveHabitList := make([]*model.NeckActiveHabit, 0)
+	if err := e.DB.Model(new(model.NeckActiveHabit)).
+		Where("id IN (?)", processIds).
+		Order("heat_date,neck_ring_number,frameid").
+		Find(&newNeckActiveHabitList).Error; err != nil {
+		zaplog.Error("Before3DaysNeckActiveHabit", zap.Any("error", err), zap.Any("processIds", processIds))
+	}
+
+	for _, v := range newNeckActiveHabitList {
+		before3DaysNeckActiveHabit := e.FindBefore3DaysNeckActiveHabit(pastureId, v.NeckRingNumber, v.HeatDate, v.Frameid)
+		if before3DaysNeckActiveHabit.SumRumina == 0 && before3DaysNeckActiveHabit.SumIntake == 0 {
+			continue
+		}
+		// 更新过滤值
+		if err := e.DB.Model(new(model.NeckActiveHabit)).
+			Select("before_three_sum_rumina", "before_three_sum_intake").
+			Where("id = ?", v.Id).
+			Updates(map[string]interface{}{
+				"before_three_sum_rumina": before3DaysNeckActiveHabit.SumRumina,
+				"before_three_sum_intake": before3DaysNeckActiveHabit.SumIntake,
+			}).Error; err != nil {
+			zaplog.Error("Before3DaysNeckActiveHabit",
+				zap.Error(err),
+				zap.Any("NeckActiveHabit", v),
+				zap.Any("pastureId", pastureId),
+			)
+		}
+	}
+}

+ 2 - 1
module/crontab/neck_ring_merge.go

@@ -67,7 +67,8 @@ func (e *Entry) OriginalMergeData(pastureId int64) error {
 	if err := e.DB.Model(new(model.NeckRingOriginal)).
 		Where("is_show = ?", pasturePb.IsShow_No).
 		Where("pasture_id = ?", pastureId).
-		Limit(int(limit)).Find(&neckRingList).Error; err != nil {
+		Limit(int(limit)).
+		Find(&neckRingList).Error; err != nil {
 		return xerr.WithStack(err)
 	}
 	if len(neckRingList) <= 0 {

+ 9 - 8
module/crontab/sql.go

@@ -172,10 +172,10 @@ func (e *Entry) GetMinIdByHeatDate(heatDate string, defaultId int64) (int64, err
 	return xMinId.Id, nil
 }
 
-func (e *Entry) FindFirstFilter(pastureId int64, neckRingNumber, heatDate string, frameId int32) *FirstFilterData {
-	firstFilterData := &FirstFilterData{}
+func (e *Entry) FindFilterData(pastureId int64, neckRingNumber, heatDate string, frameId int32) *FilterData {
+	firstFilterData := &FilterData{}
 	if err := e.DB.Model(new(model.NeckActiveHabit)).
-		Select("neck_ring_number", "filter_high", "filter_rumina", "filter_chew").
+		Select("neck_ring_number", "filter_high", "filter_rumina", "filter_chew", "change_filter", "rumina_filter", "chew_filter").
 		Where("neck_ring_number = ?", neckRingNumber).
 		Where("heat_date = ?", heatDate).
 		Where("frameid = ?", frameId).
@@ -199,15 +199,16 @@ func (e *Entry) FindWeekHabitData(pastureId int64, neckRingNumber, heatDate stri
 	if err := e.DB.Model(new(model.NeckActiveHabit)).
 		Select(
 			"neck_ring_number",
-			"IF(COUNT(1)>=3, ROUND((SUM(filter_high) -MIN(filter_high) -MAX(filter_high))/ABS(COUNT(1) -2),0), -1) as week_high_habit",
-			"IF(COUNT(1)>=3, ROUND((SUM(filter_rumina) -MIN(filter_rumina) -MAX(filter_rumina))/ABS(COUNT(1) -2),0), -1) as week_rumina_habit",
-			"IF(COUNT(1)>=3, ROUND((SUM(filter_chew) -MIN(filter_chew) -MAX(filter_chew))/ABS(COUNT(1) -2),0), -1) as week_chew_habit",
-			"ROUND(AVG(intake),0) as week_intake_habit",
-			"ROUND(AVG(inactive),0) as week_inactive_habit",
+			"IF(COUNT(1)>=3, ROUND((SUM(filter_high) -MIN(filter_high) -MAX(filter_high))/ABS(COUNT(1) -2),0), -1) as high_habit",
+			"IF(COUNT(1)>=3, ROUND((SUM(filter_rumina) -MIN(filter_rumina) -MAX(filter_rumina))/ABS(COUNT(1) -2),0), -1) as rumina_habit",
+			"IF(COUNT(1)>=3, ROUND((SUM(filter_chew) -MIN(filter_chew) -MAX(filter_chew))/ABS(COUNT(1) -2),0), -1) as chew_habit",
+			"ROUND(AVG(intake),0) as intake_habit",
+			"ROUND(AVG(inactive),0) as inactive_habit",
 		).Where("pasture_id = ?", pastureId).
 		Where("heat_date BETWEEN ? AND ?", before7DayDate, before1DayDate).
 		Where("neck_ring_number = ? ", neckRingNumber).
 		Where("frameid = ?", frameid).
+		Where("cow_id > ?", 0).
 		Where(e.DB.Where("high > ?", xToday.High).Or("rumina >= ?", xToday.Rumina)).
 		Group("neck_ring_number").
 		First(weekHabitData).Error; err != nil {