123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139 |
- package crontab
- import (
- "kpt-pasture/model"
- "time"
- "gitee.com/xuyiping_admin/pkg/xerr"
- "gitee.com/xuyiping_admin/pkg/logger/zaplog"
- "go.uber.org/zap"
- )
- 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 {
- zaplog.Error("HealthWarning", zap.Any("error", err), zap.Any("processIds", processIds))
- }
- var (
- lastCowID int64
- lastHeatDate string
- lastScore int32
- healthWarningList []*model.NeckRingHealth
- )
- for _, habit := range newNeckActiveHabitList {
- // 计算 sumChew 和 chew3dago
- sumChew := habit.SumRumina + habit.SumIntake
- chew3dago := habit.BeforeThreeSumRumina + habit.BeforeThreeSumIntake
- // 判断是否满足 isWorse 条件
- 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 {
- newHealthWarning := model.NewNeckRingHealth(habit, sumChew, chew3dago)
- if count := e.FindHealthWarning(pastureId, newHealthWarning); count <= 0 {
- continue
- }
- healthWarningList = append(healthWarningList, newHealthWarning)
- }
- }
- if len(healthWarningList) > 0 {
- if err := e.DB.Create(&healthWarningList).Error; err != nil {
- zaplog.Error("HealthWarning", zap.Any("error", err), zap.Any("healthWarningList", healthWarningList))
- }
- }
- }
- func (e *Entry) NeckRingHealthWarning() error {
- pastureList := e.FindPastureList()
- if pastureList == nil || len(pastureList) == 0 {
- return nil
- }
- for _, pasture := range pastureList {
- 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 {
- continue
- }
- healthValue = int32(v.Value)
- }
- nowTime := time.Now()
- neckRingHealthList := make([]*model.NeckRingHealth, 0)
- if err = e.DB.Model(new(model.NeckRingHealth)).
- Select("MAX(id) AS id,neck_ring_number,cow_id,score,max_high,created_at,min_high,min_chew,min_intake,sum_chew,before_three_sum_chew").
- Where("pasture_id = ?", pastureId).
- Where("heat_date >= ?", nowTime.AddDate(0, 0, -1).Format(model.LayoutDate2)).
- Group("neck_ring_number").
- Find(&neckRingHealthList).Error; err != nil {
- return xerr.WithStack(err)
- }
- newNeckRingHealthWarningList := make([]*model.NeckRingHealthWarning, 0)
- for _, v := range neckRingHealthList {
- newScore := calculateNewScore(v)
- if newScore > healthValue {
- continue
- }
- cowInfo, err := e.GetCowById(pastureId, v.CowId)
- if err != nil {
- continue
- }
- if cowInfo == nil {
- continue
- }
- newNeckRingHealthWarning := model.NewNeckRingHealthWarning(pastureId, v, cowInfo, newScore)
- newNeckRingHealthWarningList = append(newNeckRingHealthWarningList, newNeckRingHealthWarning)
- }
- 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) FindHealthWarning(pastureId int64, data *model.NeckRingHealth) int64 {
- var count int64
- if err := e.DB.Model(new(model.NeckRingHealth)).
- Where("pasture_id = ?", pastureId).
- Where("cow_id = ?", data.CowId).
- Where("heat_date = ?", data.HeatDate).
- Where("score = ?", data.Score).
- Count(&count).Error; err != nil {
- zaplog.Error("FindHealthWarning", zap.Any("error", err))
- }
- return count
- }
- func calculateNewScore(data *model.NeckRingHealth) int32 {
- return data.Score
- }
|