|
@@ -47,7 +47,6 @@ func (e *Entry) EntryUpdateActiveHabit(pastureId int64) (err error) {
|
|
zaplog.Error("NeckRingCalculate", zap.Any("pastureId", pastureId), zap.Any("FirstFilterUpdate", err), zap.Any("xToday", xToday))
|
|
zaplog.Error("NeckRingCalculate", zap.Any("pastureId", pastureId), zap.Any("FirstFilterUpdate", err), zap.Any("xToday", xToday))
|
|
}
|
|
}
|
|
|
|
|
|
- zaplog.Info("NeckRingCalculate", zap.Any("pastureId", pastureId), zap.Any("xToday", xToday), zap.Any("processIds", processIds))
|
|
|
|
if len(processIds) <= 0 {
|
|
if len(processIds) <= 0 {
|
|
return nil
|
|
return nil
|
|
}
|
|
}
|
|
@@ -67,7 +66,7 @@ func (e *Entry) EntryUpdateActiveHabit(pastureId int64) (err error) {
|
|
e.UpdateFilterCorrect(pastureId, processIds)
|
|
e.UpdateFilterCorrect(pastureId, processIds)
|
|
|
|
|
|
// 插入群体校正表
|
|
// 插入群体校正表
|
|
- e.UpdateChangeAdJust(pastureId, xToday)
|
|
|
|
|
|
+ e.UpdateChangeAdJust(pastureId, processIds)
|
|
|
|
|
|
// 更新 Cft
|
|
// 更新 Cft
|
|
e.UpdateCft(pastureId, processIds)
|
|
e.UpdateCft(pastureId, processIds)
|
|
@@ -171,7 +170,7 @@ func (e *Entry) FirstFilterUpdate(pastureId int64, xToDay *XToday) (processIds [
|
|
processIds = append(processIds, v.Id)
|
|
processIds = append(processIds, v.Id)
|
|
// 更新过滤值
|
|
// 更新过滤值
|
|
if err = e.DB.Model(new(model.NeckActiveHabit)).
|
|
if err = e.DB.Model(new(model.NeckActiveHabit)).
|
|
- Select("filter_high", "filter_rumina", "filter_chew", "cow_id", "lact", "calving_age", "ear_number", "week_high").
|
|
|
|
|
|
+ Select("filter_high", "filter_rumina", "filter_chew", "cow_id", "lact", "calving_age", "ear_number", "pen_id", "week_high").
|
|
Where("id = ?", v.Id).
|
|
Where("id = ?", v.Id).
|
|
Updates(map[string]interface{}{
|
|
Updates(map[string]interface{}{
|
|
"filter_high": firstFilterData.FilterHigh,
|
|
"filter_high": firstFilterData.FilterHigh,
|
|
@@ -181,6 +180,7 @@ func (e *Entry) FirstFilterUpdate(pastureId int64, xToDay *XToday) (processIds [
|
|
"lact": cowInfo.Lact,
|
|
"lact": cowInfo.Lact,
|
|
"calving_age": cowInfo.CalvingAge,
|
|
"calving_age": cowInfo.CalvingAge,
|
|
"ear_number": cowInfo.EarNumber,
|
|
"ear_number": cowInfo.EarNumber,
|
|
|
|
+ "pen_id": cowInfo.PenId,
|
|
"week_high": cowWeeklyActive,
|
|
"week_high": cowWeeklyActive,
|
|
}).Error; err != nil {
|
|
}).Error; err != nil {
|
|
zaplog.Error("FirstFilterUpdate",
|
|
zaplog.Error("FirstFilterUpdate",
|
|
@@ -330,12 +330,10 @@ func (e *Entry) FilterCorrectAndScoreUpdate(pastureId int64, processIds []int64,
|
|
}
|
|
}
|
|
|
|
|
|
if activityVolume != nil && activityVolume.NeckRingNumber != "" {
|
|
if activityVolume != nil && activityVolume.NeckRingNumber != "" {
|
|
- //filterCorrect := model.DefaultFilterCorrect - int(math.Floor(activityVolume.AvgFilter/3+float64(activityVolume.StdFilter)/2))
|
|
|
|
filterCorrect := model.DefaultFilterCorrect - int(math.Round(activityVolume.AvgFilter/3+float64(int(math.Round(activityVolume.StdFilter))/2)))
|
|
filterCorrect := model.DefaultFilterCorrect - int(math.Round(activityVolume.AvgFilter/3+float64(int(math.Round(activityVolume.StdFilter))/2)))
|
|
// 活动量校正系数
|
|
// 活动量校正系数
|
|
if err := e.DB.Model(new(model.NeckActiveHabit)).
|
|
if err := e.DB.Model(new(model.NeckActiveHabit)).
|
|
Where("id = ?", v.Id).
|
|
Where("id = ?", v.Id).
|
|
- //Where("neck_ring_number = ?", v.NeckRingNumber).
|
|
|
|
Update("filter_correct", filterCorrect).Error; err != nil {
|
|
Update("filter_correct", filterCorrect).Error; err != nil {
|
|
zaplog.Error("ActivityVolumeChanges-2", zap.Any("error", err), zap.Any("xToday", xToday))
|
|
zaplog.Error("ActivityVolumeChanges-2", zap.Any("error", err), zap.Any("xToday", xToday))
|
|
continue
|
|
continue
|
|
@@ -373,34 +371,94 @@ func (e *Entry) UpdateFilterCorrect(pastureId int64, processIds []int64) {
|
|
}
|
|
}
|
|
|
|
|
|
// UpdateChangeAdJust 更新群体校正数据
|
|
// UpdateChangeAdJust 更新群体校正数据
|
|
-func (e *Entry) UpdateChangeAdJust(pastureId int64, xToday *XToday) {
|
|
|
|
- res := make([]*model.NeckRingBarChange, 0)
|
|
|
|
|
|
+func (e *Entry) UpdateChangeAdJust(pastureId int64, processIds []int64) {
|
|
|
|
+ neckRingPenChangeList := make([]*model.NeckRingPenChange, 0)
|
|
yesterday := time.Now().Local().AddDate(0, 0, -1).Format(model.LayoutDate2)
|
|
yesterday := time.Now().Local().AddDate(0, 0, -1).Format(model.LayoutDate2)
|
|
- if err := e.DB.Table(fmt.Sprintf("%s as h", new(model.NeckActiveHabit).TableName())).
|
|
|
|
- Select(`h.neck_ring_number,h.heat_date, h.frameid, c.pen_id, c.pen_name, COUNT(*) as nb,
|
|
|
|
- ROUND(AVG(h.change_high)) as change_high, ROUND(AVG(h.change_filter)) as change_filter`).
|
|
|
|
- Joins("JOIN cow as c ON h.cow_id = c.id").
|
|
|
|
- Where("h.pasture_id = ?", pastureId).
|
|
|
|
- Where("h.heat_date >= ?", yesterday).
|
|
|
|
- Where("h.cow_id > ?", 0).
|
|
|
|
- Where("c.pen_id > ?", 0).
|
|
|
|
- Group("h.heat_date, h.frameid, c.pen_id").
|
|
|
|
- Order("h.heat_date, h.frameid, c.pen_id").
|
|
|
|
- Find(&res).Error; err != nil {
|
|
|
|
- zaplog.Error("UpdateChangeAdJust", zap.Any("error", err), zap.Any("xToday", xToday))
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- for _, v := range res {
|
|
|
|
- if math.Abs(float64(v.ChangeFilter)) < 10 {
|
|
|
|
- continue
|
|
|
|
|
|
+ if err := e.DB.Model(new(model.NeckActiveHabit)).
|
|
|
|
+ Select(`heat_date,frameid,pen_id,COUNT(*) AS cow_count,ROUND(AVG(change_high)) AS change_high,ROUND(AVG(change_filter)) AS change_filter`).
|
|
|
|
+ Where("pasture_id = ?", pastureId).
|
|
|
|
+ Where("heat_date >= ?", yesterday).
|
|
|
|
+ Where("cow_id > ?", 0).
|
|
|
|
+ Where("pen_id > ?", 0).
|
|
|
|
+ Group("heat_date,frameid,pen_id").
|
|
|
|
+ Order("heat_date,frameid,pen_id").
|
|
|
|
+ Find(&neckRingPenChangeList).Error; err != nil {
|
|
|
|
+ zaplog.Error("UpdateChangeAdJust", zap.Any("error", err), zap.Any("pastureId", pastureId))
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ for _, v := range neckRingPenChangeList {
|
|
|
|
+ var count int64
|
|
|
|
+ if err := e.DB.Model(new(model.NeckRingPenChange)).
|
|
|
|
+ Where("pasture_id = ?", pastureId).
|
|
|
|
+ Where("heat_date = ?", v.HeatDate).
|
|
|
|
+ Where("frameid = ?", v.Frameid).
|
|
|
|
+ Where("pen_id = ?", v.PenId).
|
|
|
|
+ Count(&count).Error; err != nil {
|
|
|
|
+ zaplog.Error("UpdateChangeAdJust", zap.Any("error", err), zap.Any("v", v), zap.Any("pastureId", pastureId))
|
|
|
|
+ }
|
|
|
|
+ // 有就更新,没有就新增
|
|
|
|
+ if count > 0 {
|
|
|
|
+ if err := e.DB.Model(new(model.NeckRingPenChange)).
|
|
|
|
+ Where("pasture_id = ?", pastureId).
|
|
|
|
+ Where("heat_date = ?", v.HeatDate).
|
|
|
|
+ Where("frameid = ?", v.Frameid).
|
|
|
|
+ Where("pen_id = ?", v.PenId).
|
|
|
|
+ Updates(map[string]interface{}{
|
|
|
|
+ "cow_count": v.CowCount,
|
|
|
|
+ "change_high": v.ChangeHigh,
|
|
|
|
+ "change_filter": v.ChangeFilter,
|
|
|
|
+ }).Error; err != nil {
|
|
|
|
+ zaplog.Error("UpdateChangeAdJust", zap.Any("error", err), zap.Any("v", v), zap.Any("pastureId", pastureId))
|
|
|
|
+ }
|
|
|
|
+ } else {
|
|
|
|
+ neckRingPenChange := model.NewNeckRingPenChange(pastureId, v.HeatDate, v.CowCount, v.Frameid, v.PenId, v.ChangeHigh, v.ChangeFilter)
|
|
|
|
+ if err := e.DB.Model(new(model.NeckRingPenChange)).
|
|
|
|
+ Create(neckRingPenChange).Error; err != nil {
|
|
|
|
+ zaplog.Error("UpdateChangeAdJust",
|
|
|
|
+ zap.Any("error", err),
|
|
|
|
+ zap.Any("neckRingPenChange", neckRingPenChange),
|
|
|
|
+ zap.Any("pastureId", pastureId),
|
|
|
|
+ )
|
|
|
|
+ }
|
|
}
|
|
}
|
|
- if err := e.DB.Model(new(model.NeckActiveHabit)).
|
|
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ neckActiveHabitList := make([]*model.NeckActiveHabit, 0)
|
|
|
|
+ if err := e.DB.Model(new(model.NeckActiveHabit)).
|
|
|
|
+ Where("id IN (?)", processIds).
|
|
|
|
+ Where("pasture_id = ?", pastureId).
|
|
|
|
+ Find(&neckActiveHabitList).Error; err != nil {
|
|
|
|
+ zaplog.Error("UpdateChangeAdJust", zap.Any("error", err))
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if len(neckActiveHabitList) <= 0 {
|
|
|
|
+ return
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ for _, v := range neckActiveHabitList {
|
|
|
|
+ neckRingPenChange := &model.NeckRingPenChange{}
|
|
|
|
+ if err := e.DB.Model(new(model.NeckRingPenChange)).
|
|
Where("pasture_id = ?", pastureId).
|
|
Where("pasture_id = ?", pastureId).
|
|
- Where("neck_ring_number = ?", v.NeckRingNumber).
|
|
|
|
Where("heat_date = ?", v.HeatDate).
|
|
Where("heat_date = ?", v.HeatDate).
|
|
- Where("frameid = ?", v.FrameId).
|
|
|
|
- Update("change_adjust", v.ChangeFilter).Error; err != nil {
|
|
|
|
- zaplog.Error("UpdateChangeAdJust-1", zap.Any("error", err), zap.Any("xToday", xToday))
|
|
|
|
|
|
+ Where("frameid = ?", v.Frameid).
|
|
|
|
+ Where("pen_id = ?", v.PenId).
|
|
|
|
+ First(&neckRingPenChange).Error; err != nil {
|
|
|
|
+ zaplog.Error("UpdateChangeAdJust", zap.Any("error", err), zap.Any("v", v), zap.Any("pastureId", pastureId))
|
|
|
|
+ continue
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if neckRingPenChange == nil || neckRingPenChange.Id <= 0 {
|
|
|
|
+ continue
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if neckRingPenChange.ChangeFilter < 10 {
|
|
|
|
+ continue
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if err := e.DB.Model(new(model.NeckActiveHabit)).
|
|
|
|
+ Where("id = ?", v.Id).
|
|
|
|
+ Update("change_adjust", neckRingPenChange.ChangeFilter).Error; err != nil {
|
|
|
|
+ zaplog.Error("UpdateChangeAdJust", zap.Any("error", err), zap.Any("v", v), zap.Any("neckRingPenChange", neckRingPenChange))
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|