Browse Source

crontba: healthWarning update

Yi 3 weeks ago
parent
commit
b8ecb7dc85

+ 1 - 0
model/neck_ring_health_warning.go

@@ -48,6 +48,7 @@ func NewNeckRingHealthWarning(pastureId int64, neckRingHealth *NeckRingHealth, c
 		BeforeThreeSumChew: neckRingHealth.BeforeThreeSumChew,
 		Score:              newScore,
 		Level:              level,
+		IsShow:             pasturePb.IsShow_Ok,
 	}
 }
 

+ 5 - 3
module/backend/prescription.go

@@ -425,13 +425,15 @@ func (s *StoreEntry) ImmunizationSetList(ctx context.Context, req *pasturePb.Imm
 	immunizationList := make([]*model.ImmunizationPlan, 0)
 	var count int64 = 0
 	pref := s.DB.Model(new(model.ImmunizationPlan)).
-		Where("pasture_id = ?", userModel.AppPasture.Id).
-		Where("is_show = ?", pasturePb.IsShow_Ok)
+		Where("pasture_id = ?", userModel.AppPasture.Id)
 	if req.Name != "" {
 		pref.Where("name like ?", fmt.Sprintf("%s%s%s", "%", req.Name, "%"))
 	}
 
-	if err = pref.Order("id desc").Count(&count).Limit(int(pagination.PageSize)).Offset(int(pagination.PageOffset)).
+	if err = pref.Order("is_show asc,id desc").
+		Count(&count).
+		Limit(int(pagination.PageSize)).
+		Offset(int(pagination.PageOffset)).
 		Find(&immunizationList).Error; err != nil {
 		return nil, xerr.WithStack(err)
 	}

+ 14 - 7
module/crontab/cow_cron.go

@@ -140,11 +140,11 @@ func (e *Entry) UpdateCowInfoByPasture(pastureId int64) {
 	dateTime := time.Now().Local().Format(model.LayoutMonth)
 	for _, cow := range cowList {
 		// 周活动量
-		weeklyActive := e.UpdateCowWeeklyHigh(cow)
+		weeklyActive := e.UpdateCowWeeklyHigh(pastureId, cow)
+		zaplog.Info("crontab", zap.Any("weeklyActive", weeklyActive))
 		cow.EventUpdate(weeklyActive)
 		if err := e.DB.Model(new(model.Cow)).
-			Select("day_age", "calving_age", "pregnancy_age", "admission_age",
-				"abortion_age", "cow_type", "weekly_active", "lactation_age", "dry_milk_age", "mating_age").
+			Select("day_age", "calving_age", "pregnancy_age", "admission_age", "abortion_age", "cow_type", "weekly_active", "lactation_age", "dry_milk_age", "mating_age").
 			Where("id = ?", cow.Id).
 			Updates(cow).Error; err != nil {
 			zaplog.Error("Crontab", zap.Any("UpdateCowDayAge", err))
@@ -451,10 +451,16 @@ func (e *Entry) SystemBasicCrontab() error {
 
 func (e *Entry) DeleteOldOriginal() error {
 	if err := e.DB.Model(new(model.NeckRingOriginal)).
-		Where("active_date <= ?", time.Now().Local().AddDate(0, 0, -30).Format(model.LayoutDate2)).
+		Where("active_date <= ?", time.Now().Local().AddDate(0, -1, 0).Format(model.LayoutDate2)).
 		Delete(&model.NeckRingOriginal{}).Error; err != nil {
 		zaplog.Error("crontab", zap.Any("DeleteOldOriginal", err))
 	}
+
+	if err := e.DB.Model(new(model.NeckActiveHabit)).
+		Where("heat_date <= ?", time.Now().Local().AddDate(0, -1, 0).Format(model.LayoutDate2)).
+		Delete(&model.NeckActiveHabit{}).Error; err != nil {
+		zaplog.Error("crontab", zap.Any("DeleteNeckActiveHabit", err))
+	}
 	return nil
 }
 
@@ -527,13 +533,13 @@ func (e *Entry) InitEventData(cowList []*model.Cow, systemBasic *model.SystemBas
 }
 
 // UpdateCowWeeklyHigh 更新牛只周活动量
-func (e *Entry) UpdateCowWeeklyHigh(cow *model.Cow) int32 {
-	highData, err := e.GetSystemNeckRingConfigure(cow.PastureId, model.High)
+func (e *Entry) UpdateCowWeeklyHigh(pastureId int64, cow *model.Cow) int32 {
+	highData, err := e.GetSystemNeckRingConfigure(pastureId, model.High)
 	if err != nil || highData == nil || highData.Value <= 0 {
 		return 0
 	}
 
-	minWeeklyActive, er := e.GetSystemNeckRingConfigure(cow.PastureId, model.MinWeeklyActive)
+	minWeeklyActive, er := e.GetSystemNeckRingConfigure(pastureId, model.MinWeeklyActive)
 	if er != nil || minWeeklyActive == nil || minWeeklyActive.Value <= 0 {
 		return 0
 	}
@@ -544,6 +550,7 @@ func (e *Entry) UpdateCowWeeklyHigh(cow *model.Cow) int32 {
 	selectStr := fmt.Sprintf(`cow_id,heat_date,count(1) AS nb,IF(ROUND(AVG(high))>%d, ROUND(AVG(high)), %d) AS high`, minWeeklyActive.Value, minWeeklyActive.Value)
 	if err = e.DB.Model(new(model.NeckActiveHabit)).
 		Select(selectStr).
+		Where("pasture_id = ?", pastureId).
 		Where("cow_id = ?", cow.Id).
 		Where("heat_date BETWEEN ? AND ?", startTime, endTime).
 		Where("high > ?", highData.Value).

+ 3 - 0
module/crontab/cow_indicators_base.go

@@ -30,6 +30,7 @@ func (e *Entry) FindPastureAllCow(pastureList []*model.AppPastureList) map[int64
 	return res
 }
 
+// FindPastureAdultCow 成母牛头数
 func (e *Entry) FindPastureAdultCow(pastureList []*model.AppPastureList) map[int64]string {
 	res := make(map[int64]string)
 	for _, pasture := range pastureList {
@@ -104,6 +105,7 @@ func (e *Entry) FindOutputNumber(pastureList []*model.AppPastureList, startTime,
 	return res
 }
 
+// FindInputNumber 入栏量(购入)
 func (e *Entry) FindInputNumber(pastureList []*model.AppPastureList, startTime, endTime int64) map[int64]string {
 	res := make(map[int64]string)
 	for _, pasture := range pastureList {
@@ -120,6 +122,7 @@ func (e *Entry) FindInputNumber(pastureList []*model.AppPastureList, startTime,
 	return res
 }
 
+// FindSalesVolume 销售额
 func (e *Entry) FindSalesVolume(pastureList []*model.AppPastureList, startTime, endTime int64) map[int64]string {
 	res := make(map[int64]string)
 	for _, pasture := range pastureList {

+ 2 - 0
module/crontab/cow_indicators_breed.go

@@ -10,6 +10,7 @@ import (
 	"go.uber.org/zap"
 )
 
+// FindCalvingNumber 产犊事件总数
 func (e *Entry) FindCalvingNumber(pastureList []*model.AppPastureList, startTime, endTime int64) map[int64]string {
 	res := make(map[int64]string)
 	for _, pasture := range pastureList {
@@ -26,6 +27,7 @@ func (e *Entry) FindCalvingNumber(pastureList []*model.AppPastureList, startTime
 	return res
 }
 
+// LactationCow 泌乳牛头数 干奶牛头数 后备牛头数
 func (e *Entry) LactationCow(pastureList []*model.AppPastureList, milkKind pasturePb.CowMilk_Kind) map[int64]string {
 	res := make(map[int64]string)
 	for _, pasture := range pastureList {

+ 5 - 0
module/crontab/cow_indicators_health.go

@@ -9,6 +9,7 @@ import (
 	"go.uber.org/zap"
 )
 
+// FindAdultAbortionRate 成母牛流产率(全)
 func (e *Entry) FindAdultAbortionRate(pastureList []*model.AppPastureList, cowType string, startTime, endTime int64) map[int64]string {
 	res := make(map[int64]string)
 	for _, pasture := range pastureList {
@@ -31,6 +32,7 @@ func (e *Entry) FindAdultAbortionRate(pastureList []*model.AppPastureList, cowTy
 	return res
 }
 
+// FindDepartureNumber 出栏量(销售)
 func (e *Entry) FindDepartureNumber(pastureList []*model.AppPastureList, saleKind pasturePb.SalesType_Kind, startTime, endTime int64) map[int64]string {
 	res := make(map[int64]string)
 	for _, pasture := range pastureList {
@@ -47,6 +49,7 @@ func (e *Entry) FindDepartureNumber(pastureList []*model.AppPastureList, saleKin
 	return res
 }
 
+// FindDiseaseNumber 发病头数
 func (e *Entry) FindDiseaseNumber(pastureList []*model.AppPastureList, startTime, endTime int64) map[int64]string {
 	res := make(map[int64]string)
 	for _, pasture := range pastureList {
@@ -63,6 +66,7 @@ func (e *Entry) FindDiseaseNumber(pastureList []*model.AppPastureList, startTime
 	return res
 }
 
+// FindCureNumber 治愈头数
 func (e *Entry) FindCureNumber(pastureList []*model.AppPastureList, startTime, endTime int64) map[int64]string {
 	res := make(map[int64]string)
 	for _, pasture := range pastureList {
@@ -79,6 +83,7 @@ func (e *Entry) FindCureNumber(pastureList []*model.AppPastureList, startTime, e
 	return res
 }
 
+// FindDeathNumber 全群牛死亡数
 func (e *Entry) FindDeathNumber(pastureList []*model.AppPastureList, startTime, endTime int64, isCalf bool) map[int64]string {
 	res := make(map[int64]string)
 	for _, pasture := range pastureList {

+ 1 - 0
module/crontab/cow_neck_ring_error.go

@@ -10,6 +10,7 @@ import (
 	"go.uber.org/zap"
 )
 
+// CowNeckRingErrorEnter 异常脖环监控 create_jbq_tj
 func (e *Entry) CowNeckRingErrorEnter() (err error) {
 	pastureList := e.FindPastureList()
 	if pastureList == nil || len(pastureList) == 0 {

+ 5 - 0
module/crontab/health_warning.go

@@ -221,13 +221,18 @@ func (e *Entry) FindNewNeckRingHealthWarning(pastureId int64, healthValue int32)
 
 	newNeckRingHealthWarningList := make([]*model.NeckRingHealthWarning, 0)
 	for _, v := range neckRingHealthList {
+		if v.HeatDate == "" || v.HeatDate != nowTime.Format(model.LayoutDate2) {
+			continue
+		}
 		cowInfo, err := e.GetCowById(pastureId, v.CowId)
 		if err != nil {
 			continue
 		}
+
 		if cowInfo == nil {
 			continue
 		}
+
 		newScore := calculateNewScore(v)
 		if newScore > healthValue {
 			continue

+ 0 - 5
module/crontab/neck_ring_merge.go

@@ -31,12 +31,7 @@ var (
 // NeckRingOriginalMerge 把脖环数据合并成2个小时的
 func (e *Entry) NeckRingOriginalMerge() (err error) {
 	if ok := e.IsExistCrontabLog(NeckRingOriginal); !ok {
-		newTime := time.Now().Local()
 		e.CreateCrontabLog(NeckRingOriginal)
-		// 原始数据删除15天前的
-		e.DB.Model(new(model.NeckRingOriginal)).
-			Where("created_at < ?", newTime.AddDate(0, -1, 0).Unix()).
-			Delete(new(model.NeckRingOriginal))
 	}
 
 	pastureList := e.FindPastureList()