|
@@ -3,6 +3,8 @@ package crontab
|
|
|
import (
|
|
|
"fmt"
|
|
|
"kpt-pasture/model"
|
|
|
+ "kpt-pasture/util"
|
|
|
+ "time"
|
|
|
|
|
|
pasturePb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/cow"
|
|
|
|
|
@@ -36,6 +38,7 @@ func (e *Entry) LactationCow(pastureList []*model.AppPastureList, milkKind pastu
|
|
|
Where("pasture_id = ?", pasture.Id).
|
|
|
Where("admission_status = ?", pasturePb.AdmissionStatus_Admission).
|
|
|
Where("milk_kind = ?", milkKind).
|
|
|
+ Where("sex = ?", pasturePb.Genders_Female).
|
|
|
Count(&count).Error; err != nil {
|
|
|
zaplog.Error("LactationCow", zap.Any("pasture_id", pasture.Id), zap.Any("err", err))
|
|
|
}
|
|
@@ -241,6 +244,7 @@ func (e *Entry) CowPregnantRate(pastureList []*model.AppPastureList, caseName st
|
|
|
Where("pasture_id = ?", pasture.Id).
|
|
|
Where("breed_status = ?", pasturePb.BreedStatus_Pregnant).
|
|
|
Where("admission_status = ?", pasturePb.AdmissionStatus_Admission).
|
|
|
+ Where("sex = ?", pasturePb.Genders_Female).
|
|
|
Find(&cowList).Error; err != nil {
|
|
|
zaplog.Error("YouthPregnantRate", zap.Any("pasture_id", pasture.Id), zap.Any("error", err))
|
|
|
}
|
|
@@ -309,6 +313,7 @@ func (e *Entry) PregnantCheckRate(pastureList []*model.AppPastureList, startAt,
|
|
|
|
|
|
}
|
|
|
|
|
|
+// ForbiddenCowNumber 禁配牛数
|
|
|
func (e *Entry) ForbiddenCowNumber(pastureList []*model.AppPastureList, startAt, endAt int64) map[int64]string {
|
|
|
res := make(map[int64]string)
|
|
|
for _, pasture := range pastureList {
|
|
@@ -324,3 +329,131 @@ func (e *Entry) ForbiddenCowNumber(pastureList []*model.AppPastureList, startAt,
|
|
|
}
|
|
|
return res
|
|
|
}
|
|
|
+
|
|
|
+// AvgRegistrationDays 平均配准天数
|
|
|
+func (e *Entry) AvgRegistrationDays(pastureList []*model.AppPastureList) map[int64]string {
|
|
|
+ res := make(map[int64]string)
|
|
|
+ for _, pasture := range pastureList {
|
|
|
+ cowList := make([]*model.Cow, 0)
|
|
|
+ if err := e.DB.Model(new(model.Cow)).
|
|
|
+ Where("pasture_id = ?", pasture.Id).
|
|
|
+ Where("admission_status = ?", pasturePb.AdmissionStatus_Admission).
|
|
|
+ Where("breed_status = ?", pasturePb.BreedStatus_Pregnant).
|
|
|
+ Where("sex = ?", pasturePb.Genders_Female).
|
|
|
+ Where("lact >= ?", 1).
|
|
|
+ Find(&cowList).Error; err != nil {
|
|
|
+ zaplog.Error("RegistrationDays", zap.Any("err", err))
|
|
|
+ }
|
|
|
+
|
|
|
+ regDays := int64(0)
|
|
|
+ count := int64(0)
|
|
|
+ for _, cow := range cowList {
|
|
|
+ if cow.LastMatingAt > 0 && cow.LastCalvingAt > 0 {
|
|
|
+ regDays += util.DaysBetween(cow.LastMatingAt, cow.LastCalvingAt)
|
|
|
+ count++
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if count > 0 {
|
|
|
+ res[pasture.Id] = fmt.Sprintf("%d", regDays/count)
|
|
|
+ } else {
|
|
|
+ res[pasture.Id] = "0"
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return res
|
|
|
+}
|
|
|
+
|
|
|
+// AvgPregnancyDays 平均怀孕天数
|
|
|
+func (e *Entry) AvgPregnancyDays(pastureList []*model.AppPastureList, startAt, endAt int64) map[int64]string {
|
|
|
+ res := make(map[int64]string)
|
|
|
+ for _, pasture := range pastureList {
|
|
|
+ eventCalvingList := make([]*model.EventCalving, 0)
|
|
|
+ if err := e.DB.Model(new(model.EventCalving)).
|
|
|
+ Where("pasture_id = ?", pasture.Id).
|
|
|
+ Where("status = ?", pasturePb.IsShow_Ok).
|
|
|
+ Where("reality_day BETWEEN ? AND ?", startAt, endAt).
|
|
|
+ Find(&eventCalvingList).Error; err != nil {
|
|
|
+ zaplog.Error("FindCalvingInterval", zap.Any("pasture_id", pasture.Id), zap.Any("err", err))
|
|
|
+ }
|
|
|
+
|
|
|
+ allPregnancyAge := int32(0)
|
|
|
+ count := int32(0)
|
|
|
+ for _, v := range eventCalvingList {
|
|
|
+ if v.PregnancyAge > 0 {
|
|
|
+ allPregnancyAge += v.PregnancyAge
|
|
|
+ count++
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if count > 0 {
|
|
|
+ res[pasture.Id] = fmt.Sprintf("%d", allPregnancyAge/count)
|
|
|
+ } else {
|
|
|
+ res[pasture.Id] = "0"
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return res
|
|
|
+}
|
|
|
+
|
|
|
+// AvgGestationalAge 平均受孕日龄
|
|
|
+func (e *Entry) AvgGestationalAge(pastureList []*model.AppPastureList) map[int64]string {
|
|
|
+ res := make(map[int64]string)
|
|
|
+ for _, pasture := range pastureList {
|
|
|
+ eventMatingList := make([]*model.EventMating, 0)
|
|
|
+ if err := e.DB.Model(new(model.EventMating)).
|
|
|
+ Where("pasture_id = ?", pasture.Id).
|
|
|
+ Where("status = ?", pasturePb.IsShow_Ok).
|
|
|
+ Where("mating_result = ?", pasturePb.MatingResult_Pregnant).
|
|
|
+ Where("lact = ?", 0).
|
|
|
+ Find(&eventMatingList).Error; err != nil {
|
|
|
+ zaplog.Error("FindGestationalAge", zap.Any("pasture_id", pasture.Id), zap.Any("err", err))
|
|
|
+ }
|
|
|
+
|
|
|
+ allDayAge := int32(0)
|
|
|
+ count := int32(0)
|
|
|
+ for _, v := range eventMatingList {
|
|
|
+ if v.DayAge > 0 && v.RealityDay > 0 {
|
|
|
+ allDayAge += v.DayAge
|
|
|
+ count++
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if count > 0 {
|
|
|
+ res[pasture.Id] = fmt.Sprintf("%d", allDayAge/count)
|
|
|
+ } else {
|
|
|
+ res[pasture.Id] = "0"
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return res
|
|
|
+}
|
|
|
+
|
|
|
+// MonthUnPregnancyRate 17-20月龄未孕比例
|
|
|
+func (e *Entry) MonthUnPregnancyRate(pastureList []*model.AppPastureList, month int32) map[int64]string {
|
|
|
+ res := make(map[int64]string)
|
|
|
+ for _, pasture := range pastureList {
|
|
|
+ cowList := make([]*model.Cow, 0)
|
|
|
+ if err := e.DB.Model(new(model.Cow)).
|
|
|
+ Where("pasture_id = ?", pasture.Id).
|
|
|
+ Where("admission_status = ?", pasturePb.AdmissionStatus_Admission).
|
|
|
+ Where("sex = ?", pasturePb.Genders_Female).
|
|
|
+ Find(&cowList).Error; err != nil {
|
|
|
+ zaplog.Error("AvgEmptyDays", zap.Any("pastureId", pasture.Id), zap.Any("err", err))
|
|
|
+ }
|
|
|
+
|
|
|
+ nowTime := time.Now().Local()
|
|
|
+ count := int32(0)
|
|
|
+ for _, cow := range cowList {
|
|
|
+ if cow.BirthAt <= 0 {
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ birthAt := time.Unix(cow.BirthAt, 0).Local()
|
|
|
+ monthYear := util.GetMonths(birthAt, nowTime)
|
|
|
+ if int32(monthYear) >= month {
|
|
|
+ count++
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if count > 0 {
|
|
|
+ res[pasture.Id] = fmt.Sprintf("%.2f", float64(count)/float64(len(cowList)))
|
|
|
+ } else {
|
|
|
+ res[pasture.Id] = "0"
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return res
|
|
|
+}
|