|
@@ -68,8 +68,8 @@ func (e *Entry) NeckRingOriginalMergeData() error {
|
|
|
return nil
|
|
|
}
|
|
|
|
|
|
- zaplog.Info("NeckRingOriginalMergeData start", zap.Any("neckRingListLen", len(neckRingList)))
|
|
|
-
|
|
|
+ // 批量插入和更新
|
|
|
+ var newNeckRingProcessList []*model.NeckRingProcess
|
|
|
for _, neckActiveHabit := range neckActiveHabitList {
|
|
|
//更新脖环牛只相关信息 新数据直接插入
|
|
|
historyNeckActiveHabit, ct := e.IsExistNeckActiveHabit(neckActiveHabit.NeckRingNumber, neckActiveHabit.HeatDate, neckActiveHabit.Frameid)
|
|
@@ -78,9 +78,7 @@ func (e *Entry) NeckRingOriginalMergeData() error {
|
|
|
zaplog.Info("NeckRingOriginalMergeData-1", zap.Any("err", err), zap.Any("neckActiveHabit", neckActiveHabit))
|
|
|
}
|
|
|
newNeckRingProcess := model.NewNeckRingProcess(neckActiveHabit)
|
|
|
- if err = e.DB.Create(newNeckRingProcess).Error; err != nil {
|
|
|
- zaplog.Info("NeckRingOriginalMergeData-2", zap.Any("err", err), zap.Any("neckActiveHabit", neckActiveHabit))
|
|
|
- }
|
|
|
+ newNeckRingProcessList = append(newNeckRingProcessList, newNeckRingProcess)
|
|
|
if err = e.UpdateNeckRingOriginalIsShow(neckActiveHabit); err != nil {
|
|
|
zaplog.Error("NeckRingOriginalMergeData-3", zap.Any("err", err), zap.Any("neckActiveHabit", neckActiveHabit))
|
|
|
}
|
|
@@ -104,6 +102,11 @@ func (e *Entry) NeckRingOriginalMergeData() error {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ if len(newNeckRingProcessList) > 0 {
|
|
|
+ if err = e.DB.Create(newNeckRingProcessList).Error; err != nil {
|
|
|
+ zaplog.Error("newNeckRingProcessList", zap.Any("err", err))
|
|
|
+ }
|
|
|
+ }
|
|
|
return nil
|
|
|
}
|
|
|
|
|
@@ -234,10 +237,7 @@ func (e *Entry) EntryUpdateActiveHabit(pastureId int64) (err error) {
|
|
|
if err = e.FirstFilterUpdate(pastureId, xToday); err != nil {
|
|
|
zaplog.Error("EntryUpdateActiveHabit", zap.Any("FirstFilterUpdate", err), zap.Any("xToday", xToday))
|
|
|
}
|
|
|
- // 更新周平均值
|
|
|
- if err = e.WeeklyActiveAvgUpdate(pastureId, xToday); err != nil {
|
|
|
- zaplog.Error("EntryUpdateActiveHabit", zap.Any("WeeklyActiveAvgUpdate", err), zap.Any("xToday", xToday))
|
|
|
- }
|
|
|
+
|
|
|
// 二次更新滤波
|
|
|
if err = e.SecondUpdateChangeFilter(pastureId, xToday); err != nil {
|
|
|
zaplog.Error("EntryUpdateActiveHabit", zap.Any("SecondUpdateChangeFilter", err), zap.Any("xToday", xToday))
|
|
@@ -299,14 +299,14 @@ func (e *Entry) CurrentMaxXFrameId(neckRingNumber, activeDate string) (frameid i
|
|
|
}
|
|
|
|
|
|
// FirstFilterUpdate 首次更新活动滤波
|
|
|
-func (e *Entry) FirstFilterUpdate(pastureId int64, xToDay *XToday) error {
|
|
|
+func (e *Entry) FirstFilterUpdate(pastureId int64, xToDay *XToday) (err error) {
|
|
|
newNeckActiveHabitList := make([]*model.NeckActiveHabit, 0)
|
|
|
- if err := e.DB.Model(new(model.NeckActiveHabit)).
|
|
|
- Where("id <= ?", xToDay.CurrMaxHabitId).
|
|
|
+ if err = e.DB.Model(new(model.NeckActiveHabit)).
|
|
|
+ Where("id BETWEEN ? AND ?", xToDay.LastMaxHabitId, xToDay.CurrMaxHabitId).
|
|
|
Where("pasture_id = ?", pastureId).
|
|
|
Where("is_show = ?", pasturePb.IsShow_No).
|
|
|
Where("change_filter = ?", model.InitChangeFilter).
|
|
|
- Where(e.DB.Where("high >= ?", xToDay.High).Or("rumina >= ?", xToDay.Rumina)). // 活动量过低牛只不参与计算
|
|
|
+ Where(e.DB.Where("high >= ?", xToDay.High).Or("rumina >= ?", xToDay.Rumina)).
|
|
|
Order("neck_ring_number,heat_date,frameid").
|
|
|
Limit(int(defaultLimit)).Find(&newNeckActiveHabitList).Error; err != nil {
|
|
|
return xerr.WithStack(err)
|
|
@@ -319,7 +319,6 @@ func (e *Entry) FirstFilterUpdate(pastureId int64, xToDay *XToday) error {
|
|
|
continue
|
|
|
}
|
|
|
|
|
|
- firstFilterData := &FirstFilterData{}
|
|
|
frameId := v.Frameid
|
|
|
heatDate := v.HeatDate
|
|
|
if v.Frameid == 0 {
|
|
@@ -327,20 +326,8 @@ func (e *Entry) FirstFilterUpdate(pastureId int64, xToDay *XToday) error {
|
|
|
heatDateParse, _ := time.Parse(model.LayoutDate2, heatDate)
|
|
|
heatDate = heatDateParse.AddDate(0, 0, -1).Format(model.LayoutDate2)
|
|
|
}
|
|
|
- if err := e.DB.Model(new(model.NeckActiveHabit)).
|
|
|
- Select("neck_ring_number", "filter_high", "filter_rumina", "filter_chew").
|
|
|
- Where("neck_ring_number = ?", v.NeckRingNumber).
|
|
|
- Where("heat_date = ?", heatDate).
|
|
|
- Where("frameid = ?", frameId).
|
|
|
- First(&firstFilterData).Error; err != nil {
|
|
|
- zaplog.Error("FirstFilterUpdate",
|
|
|
- zap.Any("err", err),
|
|
|
- zap.Any("NeckRingNumber", v.NeckRingNumber),
|
|
|
- zap.Any("heatDate", heatDate),
|
|
|
- zap.Any("frameId", frameId),
|
|
|
- )
|
|
|
- }
|
|
|
|
|
|
+ firstFilterData := e.FindFirstFilter(pastureId, v.NeckRingNumber, heatDate, frameId)
|
|
|
if v.FilterHigh > 0 {
|
|
|
firstFilterData.FilterHigh = v.FilterHigh
|
|
|
} else {
|
|
@@ -370,89 +357,10 @@ func (e *Entry) FirstFilterUpdate(pastureId int64, xToDay *XToday) error {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- // 更新过滤值
|
|
|
- if err := e.DB.Model(new(model.NeckActiveHabit)).
|
|
|
- Select("filter_high", "filter_rumina", "filter_chew", "cow_id", "lact", "calving_age").
|
|
|
- Where("id = ?", v.Id).
|
|
|
- Updates(map[string]interface{}{
|
|
|
- "filter_high": firstFilterData.FilterHigh,
|
|
|
- "filter_rumina": firstFilterData.FilterRumina,
|
|
|
- "filter_chew": firstFilterData.FilterChew,
|
|
|
- "cow_id": cowInfo.Id,
|
|
|
- "lact": cowInfo.Lact,
|
|
|
- "calving_age": cowInfo.CalvingAge,
|
|
|
- }).Error; err != nil {
|
|
|
- return xerr.WithStack(err)
|
|
|
- }
|
|
|
- }
|
|
|
- zaplog.Info("EntryUpdateActiveHabit-FilterUpdate-Success")
|
|
|
- return nil
|
|
|
-}
|
|
|
-
|
|
|
-// WeeklyActiveAvgUpdate 更新周平均值
|
|
|
-func (e *Entry) WeeklyActiveAvgUpdate(pastureId int64, xToday *XToday) (err error) {
|
|
|
- neckActiveHabitList := make([]*model.NeckActiveHabit, 0)
|
|
|
- if err = e.DB.Model(new(model.NeckActiveHabit)).
|
|
|
- Where("id <= ?", xToday.CurrMaxHabitId).
|
|
|
- Where("pasture_id = ?", pastureId).
|
|
|
- Where("change_filter = ?", model.InitChangeFilter).
|
|
|
- Where("is_show = ?", pasturePb.IsShow_No).
|
|
|
- Where(e.DB.Where("high > ?", xToday.High).Or("rumina >= ?", xToday.Rumina)).
|
|
|
- Order("neck_ring_number,heat_date,frameid").
|
|
|
- Limit(int(defaultLimit)).Find(&neckActiveHabitList).Error; err != nil {
|
|
|
- return xerr.WithStack(err)
|
|
|
- }
|
|
|
-
|
|
|
- for _, v := range neckActiveHabitList {
|
|
|
- beginDayDate, _ := time.Parse(model.LayoutDate2, v.HeatDate)
|
|
|
- before7DayDate := beginDayDate.AddDate(0, 0, -7).Format(model.LayoutDate2)
|
|
|
- before1DayDate := beginDayDate.AddDate(0, 0, -1).Format(model.LayoutDate2)
|
|
|
-
|
|
|
- activeTime := fmt.Sprintf("%s %02d:00:00", v.HeatDate, v.Frameid*2+1)
|
|
|
- activeStartTimeParse, _ := time.Parse(model.LayoutTime, activeTime)
|
|
|
- activeStartTime := activeStartTimeParse.Add(-23 * time.Hour).Format(model.LayoutTime)
|
|
|
-
|
|
|
- weekHabitData := &WeekHabit{}
|
|
|
- if err = e.DB.Model(new(model.NeckActiveHabit)).
|
|
|
- Select(
|
|
|
- "neck_ring_number",
|
|
|
- "IF(COUNT(1)>=3, ROUND((SUM(filter_high) -MIN(filter_high) -MAX(filter_high))/ABS(COUNT(1) -2),0), -1) as week_high_habit",
|
|
|
- "IF(COUNT(1)>=3, ROUND((SUM(filter_rumina) -MIN(filter_rumina) -MAX(filter_rumina))/ABS(COUNT(1) -2),0), -1) as week_rumina_habit",
|
|
|
- "IF(COUNT(1)>=3, ROUND((SUM(filter_chew) -MIN(filter_chew) -MAX(filter_chew))/ABS(COUNT(1) -2),0), -1) as week_chew_habit",
|
|
|
- "ROUND(AVG(intake),0) as week_intake_habit",
|
|
|
- "ROUND(AVG(inactive),0) as week_inactive_habit",
|
|
|
- ).Where("pasture_id = ?", pastureId).
|
|
|
- Where("heat_date BETWEEN ? AND ?", before7DayDate, before1DayDate).
|
|
|
- Where("neck_ring_number = ? ", v.NeckRingNumber).
|
|
|
- Where("frameid = ?", v.Frameid).
|
|
|
- Where(e.DB.Where("high > ?", xToday.High).Or("rumina >= ?", xToday.Rumina)).
|
|
|
- Group("neck_ring_number").First(weekHabitData).Error; err != nil {
|
|
|
- zaplog.Error("WeeklyActiveAvgUpdate-1", zap.Any("error", err), zap.Any("xToday", xToday))
|
|
|
- }
|
|
|
- // 累计24小时数值
|
|
|
- sumHabitData := &SumHabit{}
|
|
|
- if err = e.DB.Model(new(model.NeckActiveHabit)).
|
|
|
- Select(
|
|
|
- "neck_ring_number",
|
|
|
- "IF(COUNT(1)>6, ROUND(AVG(filter_rumina)*12,0), 0) as sum_rumina",
|
|
|
- "IF(COUNT(1)>6, ROUND(AVG(intake)*12,0), 0) as sum_intake",
|
|
|
- "IF(COUNT(1)>6, ROUND(AVG(inactive)*12,0), 0) as sum_inactive",
|
|
|
- "IF(COUNT(1)>6, ROUND(AVG(active)*12,0), 0) as sum_active",
|
|
|
- "MAX(change_filter) as sum_max_high",
|
|
|
- fmt.Sprintf("MIN(IF(change_filter > %d, change_filter, %d)) as sum_min_high", model.DefaultChangeFilter, model.InitChangeFilter),
|
|
|
- fmt.Sprintf("MIN( CASE WHEN filter_chew > %d THEN filter_chew WHEN filter_rumina >= %d THEN filter_rumina ELSE 0 END) as sum_min_chew", model.DefaultChangeFilter, model.DefaultRuminaFilter),
|
|
|
- ).
|
|
|
- Where("pasture_id = ?", pastureId).
|
|
|
- Where("heat_date BETWEEN ? AND ?", before1DayDate, beginDayDate).
|
|
|
- Where("active_time BETWEEN ? AND ?", activeStartTime, activeTime).
|
|
|
- Where(e.DB.Where("high > ?", xToday.High).Or("rumina >= ?", xToday.Rumina)).
|
|
|
- Where("neck_ring_number = ? AND frameid = ?", v.NeckRingNumber, v.Frameid).
|
|
|
- Group("neck_ring_number").First(sumHabitData).Error; err != nil {
|
|
|
- zaplog.Error("WeeklyActiveAvgUpdate-2", zap.Any("error", err), zap.Any("xToday", xToday))
|
|
|
- }
|
|
|
-
|
|
|
+ // 前七天的
|
|
|
+ weekHabitData := e.FindWeekHabitData(pastureId, v.NeckRingNumber, v.HeatDate, v.Frameid, xToDay)
|
|
|
highDiff := v.FilterHigh - weekHabitData.WeekHighHabit
|
|
|
- denominator := float64(v.WeekHigh)*0.6 + float64(weekHabitData.WeekHighHabit)*0.2 + float64(xToday.WeeklyActive)*0.2
|
|
|
+ denominator := float64(v.WeekHigh)*0.6 + float64(weekHabitData.WeekHighHabit)*0.2 + float64(xToDay.WeeklyActive)*0.2
|
|
|
if highDiff > 0 {
|
|
|
v.ChangeHigh = int32(math.Round((float64(highDiff) / denominator) * 100))
|
|
|
} else {
|
|
@@ -469,26 +377,25 @@ func (e *Entry) WeeklyActiveAvgUpdate(pastureId int64, xToday *XToday) (err erro
|
|
|
} else {
|
|
|
v.ChangeChew = 0
|
|
|
}
|
|
|
- // 三天前的反刍和采食
|
|
|
- before3DaysNeckActiveHabit := &model.NeckActiveHabit{}
|
|
|
- before3DayDate := beginDayDate.AddDate(0, 0, -3).Format(model.LayoutDate2)
|
|
|
- if err = e.DB.Model(new(model.NeckActiveHabit)).
|
|
|
- Select("sum_rumina", "sum_intake").
|
|
|
- Where("pasture_id = ?", pastureId).
|
|
|
- Where("neck_ring_number = ?", v.NeckRingNumber).
|
|
|
- Where("heat_date = ?", before3DayDate).
|
|
|
- Where("frameid = ? ", v.Frameid).
|
|
|
- First(before3DaysNeckActiveHabit).Error; err != nil {
|
|
|
- zaplog.Error("WeeklyActiveAvgUpdate-3", zap.Any("error", err), zap.Any("xToday", xToday))
|
|
|
- }
|
|
|
|
|
|
+ sumHabitData := e.FindSumHabitData(pastureId, v.NeckRingNumber, v.HeatDate, v.Frameid, xToDay)
|
|
|
+ before3DaysNeckActiveHabit := e.FindBefore3DaysNeckActiveHabit(pastureId, v.NeckRingNumber, v.HeatDate, v.Frameid)
|
|
|
+
|
|
|
+ // 更新过滤值
|
|
|
if err = e.DB.Model(new(model.NeckActiveHabit)).
|
|
|
Select(
|
|
|
+ "filter_high", "filter_rumina", "filter_chew", "cow_id", "lact", "calving_age",
|
|
|
"week_high_habit", "week_rumina_habit", "week_chew_habit", "week_intake_habit", "week_inactive_habit",
|
|
|
"sum_rumina", "sum_intake", "sum_inactive", "sum_active", "sum_max_high", "sum_min_high", "sum_min_chew",
|
|
|
"change_high", "change_rumina", "change_chew", "before_three_sum_rumina", "before_three_sum_intake",
|
|
|
).Where("id = ?", v.Id).
|
|
|
Updates(map[string]interface{}{
|
|
|
+ "filter_high": firstFilterData.FilterHigh,
|
|
|
+ "filter_rumina": firstFilterData.FilterRumina,
|
|
|
+ "filter_chew": firstFilterData.FilterChew,
|
|
|
+ "cow_id": cowInfo.Id,
|
|
|
+ "lact": cowInfo.Lact,
|
|
|
+ "calving_age": cowInfo.CalvingAge,
|
|
|
"week_high_habit": weekHabitData.WeekHighHabit,
|
|
|
"week_rumina_habit": weekHabitData.WeekRuminaHabit,
|
|
|
"week_chew_habit": weekHabitData.WeekChewHabit,
|
|
@@ -507,9 +414,10 @@ func (e *Entry) WeeklyActiveAvgUpdate(pastureId int64, xToday *XToday) (err erro
|
|
|
"before_three_sum_rumina": before3DaysNeckActiveHabit.SumRumina,
|
|
|
"before_three_sum_intake": before3DaysNeckActiveHabit.SumIntake,
|
|
|
}).Error; err != nil {
|
|
|
- zaplog.Error("WeeklyActiveAvgUpdate-6", zap.Any("error", err), zap.Any("xToday", xToday))
|
|
|
+ return xerr.WithStack(err)
|
|
|
}
|
|
|
}
|
|
|
+ zaplog.Info("EntryUpdateActiveHabit-FilterUpdate-Success")
|
|
|
return nil
|
|
|
}
|
|
|
|
|
@@ -612,7 +520,8 @@ func (e *Entry) FilterCorrectAndScoreUpdate(pastureId int64, xToday *XToday) err
|
|
|
activityVolumeList := make([]*ActivityVolume, 0)
|
|
|
activityVolumeMap := make(map[string]*ActivityVolume)
|
|
|
if err := e.DB.Model(new(model.NeckActiveHabit)).
|
|
|
- Select("neck_ring_number", "AVG(IF(change_filter>=60, 60, change_filter)) as avg_filter", "ROUND(STD(IF(change_filter>=60, 60, change_filter))) as std_filter", "COUNT(1) as nb").
|
|
|
+ Select("neck_ring_number", "AVG(IF(change_filter>=60, 60, change_filter)) as avg_filter",
|
|
|
+ "ROUND(STD(IF(change_filter>=60, 60, change_filter))) as std_filter", "COUNT(1) as nb").
|
|
|
Where("heat_date BETWEEN ? AND ?", before7DayDate, before1DayDate).
|
|
|
Where("pasture_id = ?", pastureId).
|
|
|
Where(e.DB.Where("high > ?", xToday.High).Or("rumina >= ?", xToday.Rumina)).
|
|
@@ -643,7 +552,7 @@ func (e *Entry) FilterCorrectAndScoreUpdate(pastureId int64, xToday *XToday) err
|
|
|
|
|
|
for _, v := range neckActiveHabitList {
|
|
|
if filterCorrectMap, ok := activityVolumeMap[v.NeckRingNumber]; ok {
|
|
|
- filterCorrect := model.DefaultFilterCorrect - int(math.Floor(float64(filterCorrectMap.AvgFilter)/3+float64(filterCorrectMap.StdFilter)/2))
|
|
|
+ filterCorrect := model.DefaultFilterCorrect - int(math.Floor(filterCorrectMap.AvgFilter/3+float64(filterCorrectMap.StdFilter)/2))
|
|
|
// 活动量校正系数
|
|
|
if err := e.DB.Model(new(model.NeckActiveHabit)).
|
|
|
Where("id = ?", v.Id).
|