Browse Source

crontab: neckRingError update

Yi 2 tuần trước cách đây
mục cha
commit
de915a3c0e

+ 11 - 0
model/neck_active_habit.go

@@ -310,3 +310,14 @@ type WeeklyActiveModel struct {
 	Nb       int32  `json:"nb"`
 	High     int32  `json:"high"`
 }
+
+// 		Select(`h.cow_id, h.ear_number,
+//		SUM(IF(TIMESTAMPDIFF(HOUR, UNIX_TIMESTAMP(h.active_time), h.created_at)>9, 1, 0)) AS nb,
+//		COUNT(1) AS nba, ROUND(AVG(h.voltage), 0) AS voltage`).
+
+type NeckRingErrorModel struct {
+	CowId   int64
+	Nb      int32
+	Nba     int32
+	Voltage int32
+}

+ 3 - 3
module/crontab/cow_neck_ring_error.go

@@ -265,7 +265,7 @@ func (e *Entry) NeckRingErrorOfReceivingLess(pastureId int64, neckRing *model.Ne
 
 // NeckRingErrorOfDataLatency 数据延迟
 func (e *Entry) NeckRingErrorOfDataLatency(pastureId int64, neckRing *model.NeckRing, habitMinId int64, dateTime string) pasturePb.NeckRingNumberError_Kind {
-	var count int64
+	res := make([]*model.NeckRingErrorModel, 0)
 	if err := e.DB.Model(new(model.NeckActiveHabit)).
 		Select(`h.cow_id, h.ear_number, SUM(IF(TIMESTAMPDIFF(HOUR, UNIX_TIMESTAMP(h.active_time), h.created_at)>9, 1, 0)) AS nb, COUNT(1) AS nba, ROUND(AVG(h.voltage), 0) AS voltage`).
 		Where("id >= ?", habitMinId).
@@ -273,10 +273,10 @@ func (e *Entry) NeckRingErrorOfDataLatency(pastureId int64, neckRing *model.Neck
 		Where("filter_high > ?", 200).
 		Where("heat_date >= ?", dateTime).
 		Having("nb/nba >= ?", 0.7).
-		Count(&count).Error; err != nil {
+		Find(&res).Error; err != nil {
 		zaplog.Error("NeckRingErrorOfDataLatency", zap.Any("err", err), zap.Any("pastureId", pastureId))
 	}
-	if count > 0 {
+	if len(res) > 0 {
 		return pasturePb.NeckRingNumberError_Data_Latency
 	}
 	return pasturePb.NeckRingNumberError_Invalid

+ 2 - 2
module/crontab/health_warning.go

@@ -122,8 +122,8 @@ func (e *Entry) NeckRingHealthWarning() error {
 	for _, pasture := range pastureList {
 
 		e.DB.Model(new(model.NeckRingHealthWarning)).
-			Delete(new(model.NeckRingHealthWarning)).
-			Where("pasture_id = ?", pasture.Id)
+			Where("pasture_id = ?", pasture.Id).
+			Delete(new(model.NeckRingHealthWarning))
 
 		if err := e.UpdateNeckRingHealth(pasture.Id); err != nil {
 			zaplog.Error("NeckRingHealthWarning", zap.Any("UpdateNeckRingHealth", err), zap.Any("pasture", pasture))