|
@@ -20,7 +20,7 @@ func (e *Entry) NeckRingCalculate() error {
|
|
|
return nil
|
|
|
}
|
|
|
for _, pasture := range pastureList {
|
|
|
- if err := e.EntryUpdateActiveHabit(pasture.Id); err != nil {
|
|
|
+ if err := e.EntryUpdateActiveHabit(pasture); err != nil {
|
|
|
zaplog.Error("NeckRingCalculate", zap.Any("err", err), zap.Any("pasture", pasture))
|
|
|
}
|
|
|
zaplog.Info(fmt.Sprintf("NeckRingCalculate Success %d", pasture.Id))
|
|
@@ -28,7 +28,8 @@ func (e *Entry) NeckRingCalculate() error {
|
|
|
return nil
|
|
|
}
|
|
|
|
|
|
-func (e *Entry) EntryUpdateActiveHabit(pastureId int64) (err error) {
|
|
|
+func (e *Entry) EntryUpdateActiveHabit(appPasture *model.AppPastureList) (err error) {
|
|
|
+ pastureId := appPasture.Id
|
|
|
// 获取这段执行数据内最大日期和最小日期
|
|
|
xToday, err := e.XToday(pastureId)
|
|
|
if err != nil {
|
|
@@ -47,7 +48,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.Info("NeckRingCalculate", zap.Any("pastureId", pastureId), zap.Any("xToday", xToday), zap.Any("processIds", processIds))
|
|
|
if len(processIds) <= 0 {
|
|
|
return nil
|
|
|
}
|
|
@@ -58,7 +58,7 @@ func (e *Entry) EntryUpdateActiveHabit(pastureId int64) (err error) {
|
|
|
e.SecondUpdateChangeFilter(pastureId, processIds, xToday)
|
|
|
|
|
|
// 活动量校正系数和健康评分
|
|
|
- e.FilterCorrectAndScoreUpdate(pastureId, processIds, xToday)
|
|
|
+ e.FilterCorrectAndScoreUpdate(appPasture, processIds, xToday)
|
|
|
|
|
|
// 更新 ChangeFilter
|
|
|
e.UpdateChangeFilter(pastureId, processIds)
|
|
@@ -67,7 +67,7 @@ func (e *Entry) EntryUpdateActiveHabit(pastureId int64) (err error) {
|
|
|
e.UpdateFilterCorrect(pastureId, processIds)
|
|
|
|
|
|
// 插入群体校正表
|
|
|
- e.UpdateChangeAdJust(pastureId, xToday)
|
|
|
+ e.UpdateChangeAdJust(pastureId, processIds)
|
|
|
|
|
|
// 更新 Cft
|
|
|
e.UpdateCft(pastureId, processIds)
|
|
@@ -171,7 +171,7 @@ func (e *Entry) FirstFilterUpdate(pastureId int64, xToDay *XToday) (processIds [
|
|
|
processIds = append(processIds, v.Id)
|
|
|
// 更新过滤值
|
|
|
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).
|
|
|
Updates(map[string]interface{}{
|
|
|
"filter_high": firstFilterData.FilterHigh,
|
|
@@ -181,6 +181,7 @@ func (e *Entry) FirstFilterUpdate(pastureId int64, xToDay *XToday) (processIds [
|
|
|
"lact": cowInfo.Lact,
|
|
|
"calving_age": cowInfo.CalvingAge,
|
|
|
"ear_number": cowInfo.EarNumber,
|
|
|
+ "pen_id": cowInfo.PenId,
|
|
|
"week_high": cowWeeklyActive,
|
|
|
}).Error; err != nil {
|
|
|
zaplog.Error("FirstFilterUpdate",
|
|
@@ -265,18 +266,6 @@ func (e *Entry) SecondUpdateChangeFilter(pastureId int64, processIds []int64, xT
|
|
|
chewFilter = 50
|
|
|
}
|
|
|
|
|
|
- zaplog.Info("SecondUpdateChangeFilter",
|
|
|
- zap.Any("NeckActiveHabit", v),
|
|
|
- zap.Any("discount", discount),
|
|
|
- zap.Any("xChangeDiscount", xChangeDiscount),
|
|
|
- zap.Any("xRuminaDisc", xRuminaDisc),
|
|
|
- zap.Any("chewFilterDiscount", chewFilterDiscount),
|
|
|
- zap.Any("secondFilterData", secondFilterData),
|
|
|
- zap.Any("changeFilter", changeFilter),
|
|
|
- zap.Any("ruminaFilter", ruminaFilter),
|
|
|
- zap.Any("chewFilter", chewFilter),
|
|
|
- )
|
|
|
-
|
|
|
if err := e.DB.Model(new(model.NeckActiveHabit)).
|
|
|
Select("change_filter", "rumina_filter", "chew_filter").
|
|
|
Where("id = ?", v.Id).
|
|
@@ -291,7 +280,7 @@ func (e *Entry) SecondUpdateChangeFilter(pastureId int64, processIds []int64, xT
|
|
|
}
|
|
|
|
|
|
// FilterCorrectAndScoreUpdate 计算活动量变化趋势校正值(活跃度校正)和健康评分
|
|
|
-func (e *Entry) FilterCorrectAndScoreUpdate(pastureId int64, processIds []int64, xToday *XToday) {
|
|
|
+func (e *Entry) FilterCorrectAndScoreUpdate(appPasture *model.AppPastureList, processIds []int64, xToday *XToday) {
|
|
|
beginDayDate := time.Now().Local()
|
|
|
before7DayDate := beginDayDate.AddDate(0, 0, -7).Format(model.LayoutDate2)
|
|
|
before1DayDate := beginDayDate.AddDate(0, 0, -1).Format(model.LayoutDate2)
|
|
@@ -299,14 +288,14 @@ func (e *Entry) FilterCorrectAndScoreUpdate(pastureId int64, processIds []int64,
|
|
|
neckActiveHabitList := make([]*model.NeckActiveHabit, 0)
|
|
|
if err := e.DB.Model(new(model.NeckActiveHabit)).
|
|
|
Where("id IN (?)", processIds).
|
|
|
- Where("pasture_id = ?", pastureId).
|
|
|
+ Where("pasture_id = ?", appPasture.Id).
|
|
|
Find(&neckActiveHabitList).Error; err != nil {
|
|
|
zaplog.Error("ActivityVolumeChanges-1", zap.Any("error", err), zap.Any("xToday", xToday))
|
|
|
return
|
|
|
}
|
|
|
|
|
|
for _, v := range neckActiveHabitList {
|
|
|
- cowScore := calculateScore(v)
|
|
|
+ cowScore := CalculateScore(appPasture, v)
|
|
|
if err := e.DB.Model(new(model.NeckActiveHabit)).
|
|
|
Where("id = ?", v.Id).
|
|
|
Update("score", cowScore).Error; err != nil {
|
|
@@ -318,7 +307,7 @@ func (e *Entry) FilterCorrectAndScoreUpdate(pastureId int64, processIds []int64,
|
|
|
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("pasture_id = ?", appPasture.Id).
|
|
|
Where(e.DB.Where("high > ?", 12).Or("rumina >= ?", xToday.Rumina)).
|
|
|
Where("active_time <= ?", beginDayDate.Add(-12*time.Hour).Format(model.LayoutTime)).
|
|
|
Where("change_filter > ?", MinChangeFilter).
|
|
@@ -330,12 +319,10 @@ func (e *Entry) FilterCorrectAndScoreUpdate(pastureId int64, processIds []int64,
|
|
|
}
|
|
|
|
|
|
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)))
|
|
|
// 活动量校正系数
|
|
|
if err := e.DB.Model(new(model.NeckActiveHabit)).
|
|
|
Where("id = ?", v.Id).
|
|
|
- //Where("neck_ring_number = ?", v.NeckRingNumber).
|
|
|
Update("filter_correct", filterCorrect).Error; err != nil {
|
|
|
zaplog.Error("ActivityVolumeChanges-2", zap.Any("error", err), zap.Any("xToday", xToday))
|
|
|
continue
|
|
@@ -373,38 +360,99 @@ func (e *Entry) UpdateFilterCorrect(pastureId int64, processIds []int64) {
|
|
|
}
|
|
|
|
|
|
// 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)
|
|
|
- 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("neck_ring_number = ?", v.NeckRingNumber).
|
|
|
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))
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+// UpdateCft 更新群体校正修正
|
|
|
func (e *Entry) UpdateCft(pastureId int64, processIds []int64) {
|
|
|
neckActiveHabitList := make([]*model.NeckActiveHabit, 0)
|
|
|
if err := e.DB.Model(new(model.NeckActiveHabit)).
|