فهرست منبع

crontab: healthWarning update

Yi 6 روز پیش
والد
کامیت
cb665982bc
1فایلهای تغییر یافته به همراه9 افزوده شده و 15 حذف شده
  1. 9 15
      module/crontab/health_waning.go

+ 9 - 15
module/crontab/health_waning.go

@@ -1,7 +1,6 @@
 package crontab
 
 import (
-	"fmt"
 	"kpt-pasture/model"
 	"time"
 
@@ -40,10 +39,10 @@ func (e *Entry) UpdateNeckRingHealth(pastureId int64) error {
 	}
 	healthValue := int32(0)
 	for _, v := range neckRingConfigureList {
-		if v.Name != model.HealthWarning {
-			continue
+		if v.Name == model.HealthWarning {
+			healthValue = int32(v.Value)
+			break
 		}
-		healthValue = int32(v.Value)
 	}
 
 	newNeckRingHealthWarningList, err := e.FindNewNeckRingHealthWarning(pastureId, healthValue)
@@ -66,17 +65,12 @@ func (e *Entry) UpdateNeckRingHealth(pastureId int64) error {
 func (e *Entry) FindNewNeckRingHealthWarning(pastureId int64, healthValue int32) ([]*model.NeckRingHealthWarning, error) {
 	nowTime := time.Now().Local()
 	startTime := nowTime.AddDate(0, 0, -1).Format(model.LayoutDate2)
-
-	type MaxId struct {
-		Id int64
-	}
-
-	maxIds := make([]*MaxId, 0)
-	sqlQuery := fmt.Sprintf(`SELECT MAX(d.id) AS id FROM neck_ring_health d 
-		WHERE d.heat_date>= %s AND d.pasture_id = %d AND NOT EXISTS 
-		(SELECT 1 FROM neck_active_habit h WHERE h.cow_id=d.cow_id AND h.heat_date=%s AND d.heat_date=%s)
-		GROUP BY d.cow_id`, startTime, pastureId, nowTime.Format(model.LayoutDate2), startTime)
-	if err := e.DB.Raw(sqlQuery).
+	maxIds := make([]int64, 0)
+	sqlQuery := `SELECT MAX(d.id) AS id FROM neck_ring_health d 
+		WHERE d.heat_date>= ? AND d.pasture_id = ? AND NOT EXISTS 
+		(SELECT 1 FROM neck_active_habit h WHERE h.cow_id=d.cow_id AND h.heat_date= ? AND d.heat_date= ?)
+		GROUP BY d.cow_id`
+	if err := e.DB.Raw(sqlQuery, startTime, pastureId, nowTime.Format(model.LayoutDate2), startTime).
 		Scan(&maxIds).Error; err != nil {
 		zaplog.Error("FindNewNeckRingHealthWarning", zap.Any("error", err), zap.Any("sqlQuery", sqlQuery))
 		return nil, xerr.WithStack(err)