Просмотр исходного кода

crontab: neckActiveHabit update changAdjust

Yi 3 дней назад
Родитель
Сommit
a63c068984

+ 1 - 0
model/neck_active_habit.go

@@ -30,6 +30,7 @@ type NeckActiveHabit struct {
 	EarNumber            string                `json:"earNumber"`
 	Lact                 int32                 `json:"lact"`
 	CalvingAge           int32                 `json:"calvingAge"`
+	PenId                int32                 `json:"penId"`
 	ActiveTime           string                `json:"activeTime"`
 	Frameid              int32                 `json:"frameid"`
 	HeatDate             string                `json:"heatDate"`

+ 0 - 33
model/neck_ring_bar_change.go

@@ -1,33 +0,0 @@
-package model
-
-type NeckRingBarChange struct {
-	Id             int64  `json:"id"`
-	PastureId      int64  `json:"pastureId"`
-	NeckRingNumber string `json:"neckRingNumber"`
-	HeatDate       string `json:"heatDate"`
-	FrameId        int32  `json:"frameId"`
-	PenId          int32  `json:"penId"`
-	PenName        string `json:"penName"`
-	Nb             int32  `json:"nb"`
-	ChangeHigh     int32  `json:"changeHigh"`
-	ChangeFilter   int32  `json:"changeFilter"`
-	CreatedAt      int64  `json:"createdAt"`
-	UpdatedAt      int64  `json:"updatedAt"`
-}
-
-func (n *NeckRingBarChange) TableName() string {
-	return "neck_ring_bar_change"
-}
-
-func NewNeckRingBarChange(neckRingNumber, heatDate string, frameId, nb, changeHigh, changeFilter int32, pen *Pen) *NeckRingBarChange {
-	return &NeckRingBarChange{
-		PastureId:    pen.PastureId,
-		HeatDate:     heatDate,
-		FrameId:      frameId,
-		PenId:        pen.Id,
-		PenName:      pen.Name,
-		ChangeHigh:   changeHigh,
-		ChangeFilter: changeFilter,
-		Nb:           nb,
-	}
-}

+ 30 - 0
model/neck_ring_pen_change.go

@@ -0,0 +1,30 @@
+package model
+
+type NeckRingPenChange struct {
+	Id           int64  `json:"id"`
+	PastureId    int64  `json:"pastureId"`
+	HeatDate     string `json:"heatDate"`
+	Frameid      int32  `json:"frameid"`
+	PenId        int32  `json:"penId"`
+	CowCount     int32  `json:"cowCount"`
+	ChangeHigh   int32  `json:"changeHigh"`
+	ChangeFilter int32  `json:"changeFilter"`
+	CreatedAt    int64  `json:"createdAt"`
+	UpdatedAt    int64  `json:"updatedAt"`
+}
+
+func (n *NeckRingPenChange) TableName() string {
+	return "neck_ring_pen_change"
+}
+
+func NewNeckRingPenChange(pastureId int64, heatDate string, cowCount, frameId, penId, changeHigh, changeFilter int32) *NeckRingPenChange {
+	return &NeckRingPenChange{
+		PastureId:    pastureId,
+		HeatDate:     heatDate,
+		Frameid:      frameId,
+		PenId:        penId,
+		CowCount:     cowCount,
+		ChangeHigh:   changeHigh,
+		ChangeFilter: changeFilter,
+	}
+}

+ 87 - 28
module/crontab/neck_ring_calculate.go

@@ -47,7 +47,6 @@ func (e *Entry) EntryUpdateActiveHabit(pastureId int64) (err error) {
 		zaplog.Error("NeckRingCalculate", zap.Any("pastureId", pastureId), zap.Any("FirstFilterUpdate", err), zap.Any("xToday", xToday))
 	}
 
-	zaplog.Info("NeckRingCalculate", zap.Any("pastureId", pastureId), zap.Any("xToday", xToday), zap.Any("processIds", processIds))
 	if len(processIds) <= 0 {
 		return nil
 	}
@@ -67,7 +66,7 @@ func (e *Entry) EntryUpdateActiveHabit(pastureId int64) (err error) {
 	e.UpdateFilterCorrect(pastureId, processIds)
 
 	// 插入群体校正表
-	e.UpdateChangeAdJust(pastureId, xToday)
+	e.UpdateChangeAdJust(pastureId, processIds)
 
 	// 更新 Cft
 	e.UpdateCft(pastureId, processIds)
@@ -171,7 +170,7 @@ func (e *Entry) FirstFilterUpdate(pastureId int64, xToDay *XToday) (processIds [
 		processIds = append(processIds, v.Id)
 		// 更新过滤值
 		if err = e.DB.Model(new(model.NeckActiveHabit)).
-			Select("filter_high", "filter_rumina", "filter_chew", "cow_id", "lact", "calving_age", "ear_number", "week_high").
+			Select("filter_high", "filter_rumina", "filter_chew", "cow_id", "lact", "calving_age", "ear_number", "pen_id", "week_high").
 			Where("id = ?", v.Id).
 			Updates(map[string]interface{}{
 				"filter_high":   firstFilterData.FilterHigh,
@@ -181,6 +180,7 @@ func (e *Entry) FirstFilterUpdate(pastureId int64, xToDay *XToday) (processIds [
 				"lact":          cowInfo.Lact,
 				"calving_age":   cowInfo.CalvingAge,
 				"ear_number":    cowInfo.EarNumber,
+				"pen_id":        cowInfo.PenId,
 				"week_high":     cowWeeklyActive,
 			}).Error; err != nil {
 			zaplog.Error("FirstFilterUpdate",
@@ -330,7 +330,6 @@ func (e *Entry) FilterCorrectAndScoreUpdate(pastureId int64, processIds []int64,
 		}
 
 		if activityVolume != nil && activityVolume.NeckRingNumber != "" {
-			//filterCorrect := model.DefaultFilterCorrect - int(math.Floor(activityVolume.AvgFilter/3+float64(activityVolume.StdFilter)/2))
 			filterCorrect := model.DefaultFilterCorrect - int(math.Round(activityVolume.AvgFilter/3+float64(int(math.Round(activityVolume.StdFilter))/2)))
 			// 活动量校正系数
 			if err := e.DB.Model(new(model.NeckActiveHabit)).
@@ -373,34 +372,94 @@ func (e *Entry) UpdateFilterCorrect(pastureId int64, processIds []int64) {
 }
 
 // UpdateChangeAdJust 更新群体校正数据
-func (e *Entry) UpdateChangeAdJust(pastureId int64, xToday *XToday) {
-	res := make([]*model.NeckRingBarChange, 0)
+func (e *Entry) UpdateChangeAdJust(pastureId int64, processIds []int64) {
+	neckRingPenChangeList := make([]*model.NeckRingPenChange, 0)
 	yesterday := time.Now().Local().AddDate(0, 0, -1).Format(model.LayoutDate2)
-	if err := e.DB.Table(fmt.Sprintf("%s as h", new(model.NeckActiveHabit).TableName())).
-		Select(`h.neck_ring_number,h.heat_date, h.frameid, c.pen_id, c.pen_name, COUNT(*) as nb,
-		ROUND(AVG(h.change_high)) as change_high, ROUND(AVG(h.change_filter)) as change_filter`).
-		Joins("JOIN cow as c ON h.cow_id = c.id").
-		Where("h.pasture_id = ?", pastureId).
-		Where("h.heat_date >= ?", yesterday).
-		Where("h.cow_id > ?", 0).
-		Where("c.pen_id > ?", 0).
-		Group("h.heat_date, h.frameid, c.pen_id").
-		Order("h.heat_date, h.frameid, c.pen_id").
-		Find(&res).Error; err != nil {
-		zaplog.Error("UpdateChangeAdJust", zap.Any("error", err), zap.Any("xToday", xToday))
-	}
-
-	for _, v := range res {
-		if math.Abs(float64(v.ChangeFilter)) < 10 {
-			continue
+	if err := e.DB.Model(new(model.NeckActiveHabit)).
+		Select(`heat_date,frameid,pen_id,COUNT(*) AS cow_count,ROUND(AVG(change_high)) AS change_high,ROUND(AVG(change_filter)) AS change_filter`).
+		Where("pasture_id = ?", pastureId).
+		Where("heat_date >= ?", yesterday).
+		Where("cow_id > ?", 0).
+		Where("pen_id > ?", 0).
+		Group("heat_date,frameid,pen_id").
+		Order("heat_date,frameid,pen_id").
+		Find(&neckRingPenChangeList).Error; err != nil {
+		zaplog.Error("UpdateChangeAdJust", zap.Any("error", err), zap.Any("pastureId", pastureId))
+	}
+
+	for _, v := range neckRingPenChangeList {
+		var count int64
+		if err := e.DB.Model(new(model.NeckRingPenChange)).
+			Where("pasture_id = ?", pastureId).
+			Where("heat_date = ?", v.HeatDate).
+			Where("frameid = ?", v.Frameid).
+			Where("pen_id = ?", v.PenId).
+			Count(&count).Error; err != nil {
+			zaplog.Error("UpdateChangeAdJust", zap.Any("error", err), zap.Any("v", v), zap.Any("pastureId", pastureId))
+		}
+		// 有就更新,没有就新增
+		if count > 0 {
+			if err := e.DB.Model(new(model.NeckRingPenChange)).
+				Where("pasture_id = ?", pastureId).
+				Where("heat_date = ?", v.HeatDate).
+				Where("frameid = ?", v.Frameid).
+				Where("pen_id = ?", v.PenId).
+				Updates(map[string]interface{}{
+					"cow_count":     v.CowCount,
+					"change_high":   v.ChangeHigh,
+					"change_filter": v.ChangeFilter,
+				}).Error; err != nil {
+				zaplog.Error("UpdateChangeAdJust", zap.Any("error", err), zap.Any("v", v), zap.Any("pastureId", pastureId))
+			}
+		} else {
+			neckRingPenChange := model.NewNeckRingPenChange(pastureId, v.HeatDate, v.CowCount, v.Frameid, v.PenId, v.ChangeHigh, v.ChangeFilter)
+			if err := e.DB.Model(new(model.NeckRingPenChange)).
+				Create(neckRingPenChange).Error; err != nil {
+				zaplog.Error("UpdateChangeAdJust",
+					zap.Any("error", err),
+					zap.Any("neckRingPenChange", neckRingPenChange),
+					zap.Any("pastureId", pastureId),
+				)
+			}
 		}
-		if err := e.DB.Model(new(model.NeckActiveHabit)).
+	}
+
+	neckActiveHabitList := make([]*model.NeckActiveHabit, 0)
+	if err := e.DB.Model(new(model.NeckActiveHabit)).
+		Where("id IN (?)", processIds).
+		Where("pasture_id = ?", pastureId).
+		Find(&neckActiveHabitList).Error; err != nil {
+		zaplog.Error("UpdateChangeAdJust", zap.Any("error", err))
+	}
+
+	if len(neckActiveHabitList) <= 0 {
+		return
+	}
+
+	for _, v := range neckActiveHabitList {
+		neckRingPenChange := &model.NeckRingPenChange{}
+		if err := e.DB.Model(new(model.NeckRingPenChange)).
 			Where("pasture_id = ?", pastureId).
-			Where("neck_ring_number = ?", v.NeckRingNumber).
 			Where("heat_date = ?", v.HeatDate).
-			Where("frameid = ?", v.FrameId).
-			Update("change_adjust", v.ChangeFilter).Error; err != nil {
-			zaplog.Error("UpdateChangeAdJust-1", zap.Any("error", err), zap.Any("xToday", xToday))
+			Where("frameid = ?", v.Frameid).
+			Where("pen_id = ?", v.PenId).
+			First(&neckRingPenChange).Error; err != nil {
+			zaplog.Error("UpdateChangeAdJust", zap.Any("error", err), zap.Any("v", v), zap.Any("pastureId", pastureId))
+			continue
+		}
+
+		if neckRingPenChange == nil || neckRingPenChange.Id <= 0 {
+			continue
+		}
+
+		if neckRingPenChange.ChangeFilter < 10 {
+			continue
+		}
+
+		if err := e.DB.Model(new(model.NeckActiveHabit)).
+			Where("id = ?", v.Id).
+			Update("change_adjust", neckRingPenChange.ChangeFilter).Error; err != nil {
+			zaplog.Error("UpdateChangeAdJust", zap.Any("error", err), zap.Any("v", v), zap.Any("neckRingPenChange", neckRingPenChange))
 		}
 	}
 }