package crontab import ( "kpt-pasture/model" "time" pasturePb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/cow" "gitee.com/xuyiping_admin/pkg/logger/zaplog" "gitee.com/xuyiping_admin/pkg/xerr" "go.uber.org/zap" ) func (e *Entry) NeckRingHealthWarning() error { pastureList := e.FindPastureList() if pastureList == nil || len(pastureList) == 0 { return nil } for _, pasture := range pastureList { e.DB.Model(new(model.NeckRingHealthWarning)). 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), ) } } return nil } func (e *Entry) UpdateNeckRingHealth(pastureId int64) error { neckRingConfigureList, err := e.FindSystemNeckRingConfigure(pastureId) if err != nil { return xerr.WithStack(err) } healthValue := int32(0) for _, v := range neckRingConfigureList { if v.Name == model.HealthWarning { healthValue = int32(v.Value) break } } newNeckRingHealthWarningList, err := e.FindNewNeckRingHealthWarning(pastureId, healthValue) if err != nil { return xerr.WithStack(err) } if len(newNeckRingHealthWarningList) > 0 { if err = e.DB.Model(new(model.NeckRingHealthWarning)). Create(&newNeckRingHealthWarningList).Error; err != nil { zaplog.Error("UpdateNeckRingHealth", zap.Any("error", err), zap.Any("newNeckRingHealthWarningList", newNeckRingHealthWarningList), ) } } return nil } func (e *Entry) FindNewNeckRingHealthWarning(pastureId int64, healthValue int32) ([]*model.NeckRingHealthWarning, error) { nowTime := time.Now().Local() startTime := nowTime.AddDate(0, 0, -1).Format(model.LayoutDate2) 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) } neckRingHealthList := make([]*model.NeckRingHealth, 0) if len(maxIds) > 0 { if err := e.DB.Model(new(model.NeckRingHealth)). Where("pasture_id = ?", pastureId). Where("heat_date >= ?", startTime). Where("id IN (?)", maxIds). Group("cow_id"). Find(&neckRingHealthList).Error; err != nil { return nil, xerr.WithStack(err) } } newNeckRingHealthWarningList := make([]*model.NeckRingHealthWarning, 0) for _, v := range neckRingHealthList { if v.HeatDate == "" { continue } cowInfo, err := e.GetCowById(pastureId, v.CowId) if err != nil || cowInfo == nil { continue } newScore := calculateNewScore(v) zaplog.Info("calculateNewScore", zap.Any("newScore", newScore), zap.Any("v", v), zap.Any("healthValue", healthValue), zap.Any("cowInfo", cowInfo), ) if newScore > healthValue { continue } if e.HistoryNeckRingHealthWarning(pastureId, cowInfo.NeckRingNumber, v.HeatDate) { continue } if e.FindNeckRingError(pastureId, cowInfo.NeckRingNumber) { continue } newNeckRingHealthWarning := model.NewNeckRingHealthWarning(pastureId, v, cowInfo, newScore) zaplog.Info("newNeckRingHealthWarning", zap.Any("newNeckRingHealthWarning", newNeckRingHealthWarning), zap.Any("pastureId", pastureId), zap.Any("neckRingHealth", v), zap.Any("cowInfo", cowInfo), zap.Any("newScore", newScore), ) newNeckRingHealthWarningList = append(newNeckRingHealthWarningList, newNeckRingHealthWarning) } return newNeckRingHealthWarningList, nil } func (e *Entry) HistoryNeckRingHealthWarning(pastureId int64, neckRingNumber string, heatDate string) bool { var count int64 if err := e.DB.Model(new(model.NeckRingHealthWarning)). Where("pasture_id = ?", pastureId). Where("neck_ring_number = ?", neckRingNumber). Where("heat_date = ?", heatDate). Where("is_show = ?", pasturePb.IsShow_Ok). Count(&count).Error; err != nil { return false } return count > 0 } func (e *Entry) FindNeckRingError(pastureId int64, neckRingNumber string) bool { var count int64 if err := e.DB.Model(new(model.NeckRing)). Where("pasture_id = ?", pastureId). Where("neck_ring_number = ?", neckRingNumber). Where("status = ?", pasturePb.IsShow_No). Where("error_kind = ?", pasturePb.NeckRingNumberError_Suspected_Fall_Off). Count(&count).Error; err != nil { return false } return count > 0 }