|
@@ -13,62 +13,32 @@ import (
|
|
|
|
|
|
// HealthWarning 健康预警 create_jbq_update_2024
|
|
|
func (e *Entry) HealthWarning(pastureId int64, processIds []int64) {
|
|
|
- newNeckActiveHabitList := make([]*model.NeckActiveHabit, 0)
|
|
|
- if err := e.DB.Model(new(model.NeckActiveHabit)).
|
|
|
- Where("pasture_id = ?", pastureId).
|
|
|
- Where("id IN (?)", processIds).
|
|
|
- Where("score BETWEEN ? AND ?", model.MinScore, model.MaxScore).
|
|
|
- Order("neck_ring_number,heat_date,frameid").
|
|
|
- Find(&newNeckActiveHabitList).Error; err != nil {
|
|
|
+ healthWarningList := make([]*model.NeckRingHealth, 0)
|
|
|
+ sqlQuery := `SELECT aa.pasture_id,aa.cow_id,aa.ear_number,aa.neck_ring_number,aa.lact,aa.calving_age,aa.heat_date,aa.frameid,aa.active_time,
|
|
|
+ aa.change_filter,aa.chew_filter,aa.sum_chew,aa.sum_inactive,aa.before_three_sum_chew,aa.min_high,aa.max_high,aa.min_chew,aa.score
|
|
|
+ FROM (
|
|
|
+ SELECT h.id,h.cow_id,h.pasture_id,h.ear_number,h.neck_ring_number,h.calving_age,h.heat_date,h.frameid,h.lact,
|
|
|
+ IF(h.change_filter>-99, h.change_filter, 0) AS change_filter,
|
|
|
+ IF(h.filter_chew>-99, h.filter_chew, 0) AS chew_filter,
|
|
|
+ (h.sum_rumina +h.sum_intake) AS sum_chew,
|
|
|
+ h.sum_inactive,
|
|
|
+ (h.before_three_sum_rumina +h.before_three_sum_intake) AS before_three_sum_chew,
|
|
|
+ IF(h.sum_min_high>-99, h.sum_min_high, 0) AS min_high,
|
|
|
+ IF(h.sum_max_high>-99, h.sum_max_high, 0) AS max_high,
|
|
|
+ IF(h.sum_min_chew>-99, h.sum_min_chew, 0) AS min_chew,
|
|
|
+ h.score,h.active_time,
|
|
|
+ IF(h.cow_id=@cow AND h.heat_date=@date AND h.score>=@score, 0, 1) is_worse, @cow:=h.cow_id, @date:=h.heat_date, @score:=h.score
|
|
|
+ FROM neck_active_habit h, (SELECT @cow:=0, @date:='2001-01-01', @score:=100) aaa
|
|
|
+ WHERE h.id IN ?
|
|
|
+ AND h.score BETWEEN ? AND ?
|
|
|
+ AND h.pasture_id = ?
|
|
|
+ ) aa WHERE aa.is_worse = ?
|
|
|
+ ORDER BY aa.id;`
|
|
|
+ if err := e.DB.Raw(sqlQuery, processIds, model.MinScore, model.MaxScore, pastureId, pasturePb.IsShow_Ok).
|
|
|
+ Find(&healthWarningList).Error; err != nil {
|
|
|
zaplog.Error("HealthWarning", zap.Any("error", err), zap.Any("processIds", processIds))
|
|
|
}
|
|
|
|
|
|
- var (
|
|
|
- lastCowID int64 = 0
|
|
|
- lastHeatDate string = "2001-01-01"
|
|
|
- lastScore int32 = 100
|
|
|
- healthWarningList []*model.NeckRingHealth
|
|
|
- )
|
|
|
-
|
|
|
- for _, habit := range newNeckActiveHabitList {
|
|
|
- if habit.ChangeFilter <= -99 {
|
|
|
- habit.ChangeFilter = 0
|
|
|
- }
|
|
|
-
|
|
|
- if habit.ChewFilter <= -99 {
|
|
|
- habit.ChewFilter = 0
|
|
|
- }
|
|
|
-
|
|
|
- if habit.SumMinHigh <= -99 {
|
|
|
- habit.SumMinHigh = 0
|
|
|
- }
|
|
|
-
|
|
|
- if habit.SumMaxHigh <= -99 {
|
|
|
- habit.SumMaxHigh = 0
|
|
|
- }
|
|
|
-
|
|
|
- if habit.SumMinChew <= -99 {
|
|
|
- habit.SumMinChew = 0
|
|
|
- }
|
|
|
-
|
|
|
- sumChew := habit.SumRumina + habit.SumIntake
|
|
|
- chew3dago := habit.BeforeThreeSumRumina + habit.BeforeThreeSumIntake
|
|
|
-
|
|
|
- isWorse := 1
|
|
|
- if habit.CowId == lastCowID && habit.HeatDate == lastHeatDate && habit.Score >= lastScore {
|
|
|
- isWorse = 0
|
|
|
- }
|
|
|
-
|
|
|
- lastCowID = habit.CowId
|
|
|
- lastHeatDate = habit.HeatDate
|
|
|
- lastScore = habit.Score
|
|
|
-
|
|
|
- if isWorse != 1 {
|
|
|
- continue
|
|
|
- }
|
|
|
- newHealthWarning := model.NewNeckRingHealth(habit, sumChew, chew3dago)
|
|
|
- healthWarningList = append(healthWarningList, newHealthWarning)
|
|
|
- }
|
|
|
if len(healthWarningList) > 0 {
|
|
|
e.updateNeckRingHealth(pastureId, healthWarningList)
|
|
|
}
|