|
@@ -4,7 +4,6 @@ import (
|
|
|
"fmt"
|
|
|
"kpt-pasture/model"
|
|
|
"math"
|
|
|
- "sort"
|
|
|
"time"
|
|
|
|
|
|
pasturePb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/cow"
|
|
@@ -38,49 +37,17 @@ func (e *Entry) NeckRingCalculate() error {
|
|
|
|
|
|
func (e *Entry) EntryUpdateActiveHabit(pastureId int64) (err error) {
|
|
|
// 获取这段执行数据内最大日期和最小日期
|
|
|
- xToday := &XToday{}
|
|
|
- systemConfigureList, err := e.GetSystemNeckRingConfigure(pastureId)
|
|
|
+ xToday, err := e.XToday(pastureId)
|
|
|
if err != nil {
|
|
|
return xerr.WithStack(err)
|
|
|
}
|
|
|
- for _, v := range systemConfigureList {
|
|
|
- switch v.Name {
|
|
|
- case model.MaxHabit:
|
|
|
- xToday.LastMaxHabitId = v.Value
|
|
|
- case model.High:
|
|
|
- xToday.High = int32(v.Value)
|
|
|
- case model.Rumina:
|
|
|
- xToday.Rumina = int32(v.Value)
|
|
|
- case model.XRuminaDisc:
|
|
|
- xToday.XRuminaDisc = int32(v.Value)
|
|
|
- case model.XChangeDiscount:
|
|
|
- xToday.XChangeDiscount = int32(v.Value)
|
|
|
- case model.WeeklyActive:
|
|
|
- xToday.WeeklyActive = int32(v.Value)
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- currMaxHabit := &model.NeckActiveHabit{}
|
|
|
- if err = e.DB.Model(new(model.NeckActiveHabit)).
|
|
|
- Where("id > ?", xToday.LastMaxHabitId).
|
|
|
- Where("pasture_id = ?", pastureId).
|
|
|
- Where("is_show = ?", pasturePb.IsShow_No).
|
|
|
- Order("id desc").
|
|
|
- First(currMaxHabit).Error; err != nil {
|
|
|
- return xerr.WithStack(err)
|
|
|
- }
|
|
|
-
|
|
|
- if currMaxHabit.Id <= 0 || currMaxHabit.Id <= xToday.LastMaxHabitId {
|
|
|
- return nil
|
|
|
- }
|
|
|
-
|
|
|
- xToday.CurrMaxHabitId = currMaxHabit.Id
|
|
|
var processIds []int64
|
|
|
// 更新活动滤波
|
|
|
processIds, err = e.FirstFilterUpdate(pastureId, xToday)
|
|
|
if err != nil {
|
|
|
zaplog.Error("NeckRingCalculate", zap.Any("FirstFilterUpdate", err), zap.Any("xToday", xToday))
|
|
|
}
|
|
|
+
|
|
|
zaplog.Info("NeckRingCalculate", zap.Any("xToday", xToday), zap.Any("processIds", processIds))
|
|
|
if len(processIds) <= 0 {
|
|
|
return nil
|
|
@@ -90,9 +57,6 @@ func (e *Entry) EntryUpdateActiveHabit(pastureId int64) (err error) {
|
|
|
zaplog.Error("NeckRingCalculate", zap.Any("WeeklyUpdateActiveHabit", err), zap.Any("xToday", xToday))
|
|
|
}
|
|
|
|
|
|
- if err = e.Before3DaysNeckActiveHabit(pastureId, processIds, xToday); err != nil {
|
|
|
- zaplog.Error("NeckRingCalculate", zap.Any("Before3DaysNeckActiveHabit", err), zap.Any("xToday", xToday))
|
|
|
- }
|
|
|
// 二次更新滤波
|
|
|
if err = e.SecondUpdateChangeFilter(pastureId, xToday); err != nil {
|
|
|
zaplog.Error("NeckRingCalculate", zap.Any("SecondUpdateChangeFilter", err), zap.Any("xToday", xToday))
|
|
@@ -140,17 +104,6 @@ func (e *Entry) EntryUpdateActiveHabit(pastureId int64) (err error) {
|
|
|
zaplog.Error("UpdateChangeAdJust-2", zap.Any("error", err), zap.Any("xToday", xToday))
|
|
|
}
|
|
|
|
|
|
- sort.Slice(processIds, func(i, j int) bool {
|
|
|
- return processIds[i] > processIds[j]
|
|
|
- })
|
|
|
- // 更新日志
|
|
|
- if err = e.DB.Model(new(model.NeckRingConfigure)).
|
|
|
- Where("name = ?", model.MaxHabit).
|
|
|
- Where("pasture_id = ?", pastureId).
|
|
|
- Update("value", processIds[len(processIds)-1]).Error; err != nil {
|
|
|
- zaplog.Error("NeckRingCalculate", zap.Any("MaxHabit", err), zap.Any("xToday", xToday))
|
|
|
- }
|
|
|
-
|
|
|
// 健康预警
|
|
|
if err = e.HealthWarning(pastureId, processIds); err != nil {
|
|
|
zaplog.Error("EntryUpdateActiveHabit", zap.Any("HealthWarning", err))
|
|
@@ -162,11 +115,12 @@ func (e *Entry) EntryUpdateActiveHabit(pastureId int64) (err error) {
|
|
|
func (e *Entry) FirstFilterUpdate(pastureId int64, xToDay *XToday) (processIds []int64, err error) {
|
|
|
newNeckActiveHabitList := make([]*model.NeckActiveHabit, 0)
|
|
|
if err = e.DB.Model(new(model.NeckActiveHabit)).
|
|
|
- Where("id > ?", xToDay.LastMaxHabitId).
|
|
|
+ Where("heat_date >= ?", "2025-03-01").
|
|
|
Where("pasture_id = ?", pastureId).
|
|
|
Where("is_show = ?", pasturePb.IsShow_No).
|
|
|
Where("record_count = ?", model.DefaultRecordCount).
|
|
|
Where(e.DB.Where("high >= ?", xToDay.High).Or("rumina >= ?", xToDay.Rumina)).
|
|
|
+ Where("cow_id > ?", 0).
|
|
|
Order("heat_date,neck_ring_number,frameid").
|
|
|
Limit(int(defaultLimit)).
|
|
|
Find(&newNeckActiveHabitList).Error; err != nil {
|
|
@@ -178,10 +132,11 @@ func (e *Entry) FirstFilterUpdate(pastureId int64, xToDay *XToday) (processIds [
|
|
|
// 过滤牛只未绑定的脖环的数据
|
|
|
cowInfo := e.GetCowInfoByNeckRingNumber(v.PastureId, v.NeckRingNumber)
|
|
|
if cowInfo == nil || cowInfo.Id <= 0 {
|
|
|
+ v.UpdateIsShowOk()
|
|
|
if err = e.DB.Model(new(model.NeckActiveHabit)).
|
|
|
+ Select("is_show").
|
|
|
Where("id = ?", v.Id).
|
|
|
- Where("pasture_id = ?", v.PastureId).
|
|
|
- Update("is_show", pasturePb.IsShow_Ok).Error; err != nil {
|
|
|
+ Updates(v).Error; err != nil {
|
|
|
zaplog.Error("EntryUpdateActiveHabit", zap.Any("error", err))
|
|
|
}
|
|
|
continue
|
|
@@ -197,7 +152,7 @@ func (e *Entry) FirstFilterUpdate(pastureId int64, xToDay *XToday) (processIds [
|
|
|
frameId -= 1
|
|
|
}
|
|
|
|
|
|
- firstFilterData := e.FindFirstFilter(pastureId, v.NeckRingNumber, heatDate, frameId)
|
|
|
+ firstFilterData := e.FindFilterData(pastureId, v.NeckRingNumber, heatDate, frameId)
|
|
|
if v.FilterHigh > 0 {
|
|
|
firstFilterData.FilterHigh = v.FilterHigh
|
|
|
} else {
|
|
@@ -253,223 +208,12 @@ func (e *Entry) FirstFilterUpdate(pastureId int64, xToDay *XToday) (processIds [
|
|
|
return processIds, nil
|
|
|
}
|
|
|
|
|
|
-// WeeklyUpdateActiveHabit 时间点周平均值计算
|
|
|
-func (e *Entry) WeeklyUpdateActiveHabit(pastureId int64, processIds []int64, xToDay *XToday) (err error) {
|
|
|
- newNeckActiveHabitList := make([]*model.NeckActiveHabit, 0)
|
|
|
- if err = e.DB.Model(new(model.NeckActiveHabit)).
|
|
|
- Where("id IN (?)", processIds).
|
|
|
- Order("heat_date,neck_ring_number,frameid").
|
|
|
- Find(&newNeckActiveHabitList).Error; err != nil {
|
|
|
- return xerr.WithStack(err)
|
|
|
- }
|
|
|
- for _, v := range newNeckActiveHabitList {
|
|
|
- // 前七天的
|
|
|
- weekHabitData := e.FindWeekHabitData(pastureId, v.NeckRingNumber, v.HeatDate, v.Frameid, xToDay)
|
|
|
- // 更新过滤值
|
|
|
- if err = e.DB.Model(new(model.NeckActiveHabit)).
|
|
|
- Select("week_high_habit", "week_rumina_habit", "week_chew_habit", "week_intake_habit", "week_inactive_habit").
|
|
|
- Where("id = ?", v.Id).
|
|
|
- Updates(map[string]interface{}{
|
|
|
- "week_high_habit": weekHabitData.WeekHighHabit,
|
|
|
- "week_rumina_habit": weekHabitData.WeekRuminaHabit,
|
|
|
- "week_chew_habit": weekHabitData.WeekChewHabit,
|
|
|
- "week_intake_habit": weekHabitData.WeekIntakeHabit,
|
|
|
- "week_inactive_habit": weekHabitData.WeekInactiveHabit,
|
|
|
- }).Error; err != nil {
|
|
|
- zaplog.Error("WeeklyUpdateActiveHabit",
|
|
|
- zap.Error(err),
|
|
|
- zap.Any("NeckActiveHabit", v),
|
|
|
- zap.Any("pastureId", pastureId),
|
|
|
- )
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- if err = e.SumUpdateActiveHabit(pastureId, newNeckActiveHabitList, xToDay); err != nil {
|
|
|
- zaplog.Error("WeeklyUpdateActiveHabit",
|
|
|
- zap.Any("SumUpdateActiveHabit", err),
|
|
|
- zap.Any("newNeckActiveHabitList", newNeckActiveHabitList),
|
|
|
- zap.Any("pastureId", pastureId),
|
|
|
- )
|
|
|
- }
|
|
|
-
|
|
|
- if err = e.ActiveChange(pastureId, processIds, xToDay); err != nil {
|
|
|
- zaplog.Error("WeeklyUpdateActiveHabit",
|
|
|
- zap.Any("ActiveChange", err),
|
|
|
- zap.Any("processIds", processIds),
|
|
|
- zap.Any("xToDay", xToDay),
|
|
|
- zap.Any("pastureId", pastureId),
|
|
|
- )
|
|
|
- }
|
|
|
-
|
|
|
- return nil
|
|
|
-}
|
|
|
-
|
|
|
-// SumUpdateActiveHabit -- 累计24小时数值
|
|
|
-func (e *Entry) SumUpdateActiveHabit(pastureId int64, newNeckActiveHabitList []*model.NeckActiveHabit, xToDay *XToday) (err error) {
|
|
|
- for _, v := range newNeckActiveHabitList {
|
|
|
- sumHabitData := e.FindSumHabitData(pastureId, v.NeckRingNumber, v.HeatDate, v.Frameid, xToDay)
|
|
|
- // 更新过滤值
|
|
|
- if err = e.DB.Model(new(model.NeckActiveHabit)).
|
|
|
- Select("sum_rumina", "sum_intake", "sum_inactive", "sum_active", "sum_max_high", "sum_min_high", "sum_min_chew").
|
|
|
- Where("id = ?", v.Id).
|
|
|
- Updates(map[string]interface{}{
|
|
|
- "sum_rumina": sumHabitData.SumRumina,
|
|
|
- "sum_intake": sumHabitData.SumIntake,
|
|
|
- "sum_inactive": sumHabitData.SumInactive,
|
|
|
- "sum_active": sumHabitData.SumActive,
|
|
|
- "sum_max_high": sumHabitData.SumMaxHigh,
|
|
|
- "sum_min_high": sumHabitData.SumMinHigh,
|
|
|
- "sum_min_chew": sumHabitData.SumMinChew,
|
|
|
- }).Error; err != nil {
|
|
|
- zaplog.Error("WeeklyUpdateActiveHabit",
|
|
|
- zap.Any("err", err),
|
|
|
- zap.Any("NeckActiveHabit", v),
|
|
|
- zap.Any("pastureId", pastureId),
|
|
|
- )
|
|
|
- }
|
|
|
- }
|
|
|
- return err
|
|
|
-}
|
|
|
-
|
|
|
-// ActiveChange -- 变化百分比
|
|
|
-func (e *Entry) ActiveChange(pastureId int64, processIds []int64, xToDay *XToday) (err error) {
|
|
|
- newNeckActiveHabitList := make([]*model.NeckActiveHabit, 0)
|
|
|
- if err = e.DB.Model(new(model.NeckActiveHabit)).
|
|
|
- Where("id IN (?)", processIds).
|
|
|
- Where("week_high_habit > ?", 0).
|
|
|
- Where(e.DB.Where("high >= ?", xToDay.High).Or("rumina >= ?", xToDay.Rumina)).
|
|
|
- Find(&newNeckActiveHabitList).Error; err != nil {
|
|
|
- return xerr.WithStack(err)
|
|
|
- }
|
|
|
- for _, v := range newNeckActiveHabitList {
|
|
|
- highDiff := v.FilterHigh - v.WeekHighHabit
|
|
|
- denominator := float64(v.WeekHigh)*0.6 + float64(v.WeekHighHabit)*0.2 + float64(xToDay.WeeklyActive)*0.2
|
|
|
- if highDiff > 0 {
|
|
|
- v.ChangeHigh = int32(math.Round((float64(highDiff) / denominator / float64(v.WeekHighHabit)) * 100))
|
|
|
- } else {
|
|
|
- v.ChangeHigh = int32(math.Round(float64(highDiff) / float64(v.WeekHighHabit) * 100))
|
|
|
- }
|
|
|
-
|
|
|
- if v.WeekRuminaHabit != 0 {
|
|
|
- v.ChangeRumina = int32(math.Round(float64(v.FilterRumina-v.WeekRuminaHabit) / float64(v.WeekRuminaHabit) * 100))
|
|
|
- }
|
|
|
-
|
|
|
- if v.WeekChewHabit != 0 {
|
|
|
- v.ChangeChew = int32(math.Round(float64(v.FilterChew-v.WeekChewHabit) / float64(v.WeekChewHabit) * 100))
|
|
|
- }
|
|
|
-
|
|
|
- // 更新过滤值
|
|
|
- if err = e.DB.Model(new(model.NeckActiveHabit)).
|
|
|
- Select("change_high", "change_rumina", "change_chew").
|
|
|
- Where("id = ?", v.Id).
|
|
|
- Updates(map[string]interface{}{
|
|
|
- "change_high": v.ChangeHigh,
|
|
|
- "change_rumina": v.ChangeRumina,
|
|
|
- "change_chew": v.ChangeChew,
|
|
|
- }).Error; err != nil {
|
|
|
- zaplog.Error("WeeklyUpdateActiveHabit",
|
|
|
- zap.Error(err),
|
|
|
- zap.Any("NeckActiveHabit", v),
|
|
|
- zap.Any("pastureId", pastureId),
|
|
|
- )
|
|
|
- }
|
|
|
- }
|
|
|
- return err
|
|
|
-}
|
|
|
-
|
|
|
-func (e *Entry) WeeklyUpdateActiveHabitOld(pastureId int64, newNeckActiveHabitList []*model.NeckActiveHabit, xToDay *XToday) (err error) {
|
|
|
- for _, v := range newNeckActiveHabitList {
|
|
|
- // 前七天的
|
|
|
- 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
|
|
|
- if highDiff > 0 {
|
|
|
- v.ChangeHigh = int32(math.Round((float64(highDiff) / denominator) * 100))
|
|
|
- } else {
|
|
|
- v.ChangeHigh = int32(math.Round(float64(highDiff) / denominator * 100))
|
|
|
- }
|
|
|
-
|
|
|
- if weekHabitData.WeekRuminaHabit != 0 {
|
|
|
- v.ChangeRumina = int32(math.Round(float64(v.FilterRumina-weekHabitData.WeekRuminaHabit) / float64(weekHabitData.WeekRuminaHabit) * 100))
|
|
|
- } else {
|
|
|
- v.ChangeRumina = 0
|
|
|
- }
|
|
|
- if weekHabitData.WeekChewHabit != 0 {
|
|
|
- v.ChangeChew = int32(math.Round(float64(v.FilterChew-weekHabitData.WeekChewHabit) / float64(weekHabitData.WeekChewHabit) * 100))
|
|
|
- } else {
|
|
|
- v.ChangeChew = 0
|
|
|
- }
|
|
|
-
|
|
|
- sumHabitData := e.FindSumHabitData(pastureId, v.NeckRingNumber, v.HeatDate, v.Frameid, xToDay)
|
|
|
- // 更新过滤值
|
|
|
- if err = e.DB.Model(new(model.NeckActiveHabit)).
|
|
|
- Select(
|
|
|
- "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{}{
|
|
|
- "week_high_habit": weekHabitData.WeekHighHabit,
|
|
|
- "week_rumina_habit": weekHabitData.WeekRuminaHabit,
|
|
|
- "week_chew_habit": weekHabitData.WeekChewHabit,
|
|
|
- "week_intake_habit": weekHabitData.WeekIntakeHabit,
|
|
|
- "week_inactive_habit": weekHabitData.WeekInactiveHabit,
|
|
|
- "sum_rumina": sumHabitData.SumRumina,
|
|
|
- "sum_intake": sumHabitData.SumIntake,
|
|
|
- "sum_inactive": sumHabitData.SumInactive,
|
|
|
- "sum_active": sumHabitData.SumActive,
|
|
|
- "sum_max_high": sumHabitData.SumMaxHigh,
|
|
|
- "sum_min_high": sumHabitData.SumMinHigh,
|
|
|
- "sum_min_chew": sumHabitData.SumMinChew,
|
|
|
- "change_high": v.ChangeHigh,
|
|
|
- "change_rumina": v.ChangeRumina,
|
|
|
- "change_chew": v.ChangeChew,
|
|
|
- }).Error; err != nil {
|
|
|
- zaplog.Error("WeeklyUpdateActiveHabit",
|
|
|
- zap.Error(err),
|
|
|
- zap.Any("NeckActiveHabit", v),
|
|
|
- zap.Any("pastureId", pastureId),
|
|
|
- )
|
|
|
- }
|
|
|
- }
|
|
|
- return err
|
|
|
-}
|
|
|
-func (e *Entry) Before3DaysNeckActiveHabit(pastureId int64, processIds []int64, xToDay *XToday) (err error) {
|
|
|
- newNeckActiveHabitList := make([]*model.NeckActiveHabit, 0)
|
|
|
- if err = e.DB.Model(new(model.NeckActiveHabit)).
|
|
|
- Where("id IN (?)", processIds).
|
|
|
- Order("heat_date,neck_ring_number,frameid").
|
|
|
- Find(&newNeckActiveHabitList).Error; err != nil {
|
|
|
- return xerr.WithStack(err)
|
|
|
- }
|
|
|
- for _, v := range newNeckActiveHabitList {
|
|
|
- before3DaysNeckActiveHabit := e.FindBefore3DaysNeckActiveHabit(pastureId, v.NeckRingNumber, v.HeatDate, v.Frameid)
|
|
|
- // 更新过滤值
|
|
|
- if err = e.DB.Model(new(model.NeckActiveHabit)).
|
|
|
- Select("before_three_sum_rumina", "before_three_sum_intake").
|
|
|
- Where("id = ?", v.Id).
|
|
|
- Updates(map[string]interface{}{
|
|
|
- "before_three_sum_rumina": before3DaysNeckActiveHabit.SumRumina,
|
|
|
- "before_three_sum_intake": before3DaysNeckActiveHabit.SumIntake,
|
|
|
- }).Error; err != nil {
|
|
|
- zaplog.Error("Before3DaysNeckActiveHabit",
|
|
|
- zap.Error(err),
|
|
|
- zap.Any("NeckActiveHabit", v),
|
|
|
- zap.Any("pastureId", pastureId),
|
|
|
- )
|
|
|
- }
|
|
|
- }
|
|
|
- return nil
|
|
|
-}
|
|
|
-
|
|
|
// SecondUpdateChangeFilter 第二次更新变化趋势滤波
|
|
|
func (e *Entry) SecondUpdateChangeFilter(pastureId int64, xToday *XToday) (err error) {
|
|
|
newChangeFilterList := make([]*ChangeFilterData, 0)
|
|
|
if err = e.DB.Model(new(model.NeckActiveHabit)).
|
|
|
Select("id", "neck_ring_number", "change_high", "change_filter", "rumina_filter", "change_rumina",
|
|
|
"chew_filter", "change_chew", "heat_date", "frameid", "IF(lact = 0, 0.8, 1) as xlc_dis_count").
|
|
|
- Where("id > ?", xToday.LastMaxHabitId).
|
|
|
Where("heat_date >= ?", time.Now().AddDate(0, 0, -2).Format(model.LayoutDate2)).
|
|
|
Where("pasture_id = ?", pastureId).
|
|
|
Where("change_filter = ?", model.InitChangeFilter).
|
|
@@ -481,7 +225,6 @@ func (e *Entry) SecondUpdateChangeFilter(pastureId int64, xToday *XToday) (err e
|
|
|
}
|
|
|
|
|
|
for _, v := range newChangeFilterList {
|
|
|
- secondFilterData := &SecondFilterData{}
|
|
|
frameId := v.FrameId
|
|
|
heatDate := v.HeatDate
|
|
|
if v.FrameId == 0 {
|
|
@@ -491,57 +234,51 @@ func (e *Entry) SecondUpdateChangeFilter(pastureId int64, xToday *XToday) (err e
|
|
|
} else {
|
|
|
frameId -= 1
|
|
|
}
|
|
|
- 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(&secondFilterData).Error; err != nil {
|
|
|
- zaplog.Error("EntryUpdateActiveHabit", zap.Any("FirstFilterUpdate", err))
|
|
|
- }
|
|
|
|
|
|
+ secondFilterData := e.FindFilterData(pastureId, v.NeckRingNumber, heatDate, frameId)
|
|
|
if v.ChangeFilter > MinChangeFilter {
|
|
|
- secondFilterData.ChangeFilter = float64(v.ChangeFilter)
|
|
|
+ secondFilterData.ChangeFilter = v.ChangeFilter
|
|
|
} else {
|
|
|
if v.NeckRingNumber == secondFilterData.NeckRingNumber {
|
|
|
- secondFilterData.ChangeFilter = secondFilterData.ChangeFilter*(1-(float64(xToday.XChangeDiscount)/10)*v.XlcDisCount) +
|
|
|
- math.Min(float64(v.ChangeHigh), secondFilterData.ChangeFilter+135)*(float64(xToday.XChangeDiscount)/10)*v.XlcDisCount
|
|
|
+ changeFilter := float64(secondFilterData.ChangeFilter)*(1-(float64(xToday.XChangeDiscount)/10)*v.XlcDisCount) +
|
|
|
+ math.Min(float64(v.ChangeHigh), float64(secondFilterData.ChangeFilter)+135)*(float64(xToday.XChangeDiscount)/10)*v.XlcDisCount
|
|
|
+ secondFilterData.ChangeFilter = int32(changeFilter)
|
|
|
} else {
|
|
|
secondFilterData.ChangeFilter = 0
|
|
|
}
|
|
|
}
|
|
|
|
|
|
if v.RuminaFilter > MinRuminaFilter {
|
|
|
- secondFilterData.RuminaFilter = float64(v.ChangeFilter)
|
|
|
+ secondFilterData.RuminaFilter = v.ChangeFilter
|
|
|
} else {
|
|
|
if v.NeckRingNumber == secondFilterData.NeckRingNumber {
|
|
|
discount := float64(xToday.XRuminaDisc) / 10 * v.XlcDisCount
|
|
|
if math.Abs(float64(v.ChangeRumina)) > 60 {
|
|
|
discount *= 0.5
|
|
|
}
|
|
|
- secondFilterData.RuminaFilter = secondFilterData.RuminaFilter*(1-discount) + float64(v.ChangeRumina)*discount
|
|
|
+ secondFilterData.RuminaFilter = int32(float64(secondFilterData.RuminaFilter)*(1-discount) + float64(v.ChangeRumina)*discount)
|
|
|
} else {
|
|
|
secondFilterData.RuminaFilter = 0
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- secondFilterData.RuminaFilter = math.Min(50, secondFilterData.RuminaFilter)
|
|
|
+ secondFilterData.RuminaFilter = int32(math.Min(50, float64(secondFilterData.RuminaFilter)))
|
|
|
|
|
|
if v.ChewFilter > MinChewFilter {
|
|
|
- secondFilterData.ChewFilter = float64(v.ChangeChew)
|
|
|
+ secondFilterData.ChewFilter = v.ChangeChew
|
|
|
} else {
|
|
|
if v.NeckRingNumber == secondFilterData.NeckRingNumber {
|
|
|
discount := float64(xToday.XRuminaDisc) / 10
|
|
|
if math.Abs(float64(v.ChangeChew)) > 60 {
|
|
|
discount *= 0.5
|
|
|
}
|
|
|
- secondFilterData.ChewFilter = secondFilterData.ChewFilter*(1-discount) + float64(v.ChangeChew)*discount
|
|
|
+ secondFilterData.ChewFilter = int32(float64(secondFilterData.ChewFilter)*(1-discount) + float64(v.ChangeChew)*discount)
|
|
|
} else {
|
|
|
secondFilterData.ChewFilter = 0
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- secondFilterData.ChewFilter = math.Min(50, secondFilterData.ChewFilter)
|
|
|
+ secondFilterData.ChewFilter = int32(math.Min(50, float64(secondFilterData.ChewFilter)))
|
|
|
if err = e.DB.Model(new(model.NeckActiveHabit)).
|
|
|
Select("change_filter", "rumina_filter", "chew_filter").
|
|
|
Where("id = ?", v.Id).
|
|
@@ -658,3 +395,176 @@ func (e *Entry) UpdateChangeAdJust(pastureId int64, xToday *XToday) error {
|
|
|
|
|
|
return nil
|
|
|
}
|
|
|
+
|
|
|
+func (e *Entry) XToday(pastureId int64) (*XToday, error) {
|
|
|
+ xToday := &XToday{}
|
|
|
+ systemConfigureList, err := e.GetSystemNeckRingConfigure(pastureId)
|
|
|
+ if err != nil {
|
|
|
+ return nil, xerr.WithStack(err)
|
|
|
+ }
|
|
|
+ for _, v := range systemConfigureList {
|
|
|
+ switch v.Name {
|
|
|
+ case model.MaxHabit:
|
|
|
+ xToday.LastMaxHabitId = v.Value
|
|
|
+ case model.High:
|
|
|
+ xToday.High = int32(v.Value)
|
|
|
+ case model.Rumina:
|
|
|
+ xToday.Rumina = int32(v.Value)
|
|
|
+ case model.XRuminaDisc:
|
|
|
+ xToday.XRuminaDisc = int32(v.Value)
|
|
|
+ case model.XChangeDiscount:
|
|
|
+ xToday.XChangeDiscount = int32(v.Value)
|
|
|
+ case model.WeeklyActive:
|
|
|
+ xToday.WeeklyActive = int32(v.Value)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return xToday, nil
|
|
|
+}
|
|
|
+
|
|
|
+// WeeklyUpdateActiveHabit 时间点周平均值计算
|
|
|
+func (e *Entry) WeeklyUpdateActiveHabit(pastureId int64, processIds []int64, xToDay *XToday) (err error) {
|
|
|
+ newNeckActiveHabitList := make([]*model.NeckActiveHabit, 0)
|
|
|
+ if err = e.DB.Model(new(model.NeckActiveHabit)).
|
|
|
+ Where("id IN (?)", processIds).
|
|
|
+ Order("heat_date,neck_ring_number,frameid").
|
|
|
+ Find(&newNeckActiveHabitList).Error; err != nil {
|
|
|
+ return xerr.WithStack(err)
|
|
|
+ }
|
|
|
+
|
|
|
+ if len(newNeckActiveHabitList) <= 0 {
|
|
|
+ return nil
|
|
|
+ }
|
|
|
+
|
|
|
+ e.HabitUpdateActiveHabit(pastureId, newNeckActiveHabitList, xToDay)
|
|
|
+ e.SumUpdateActiveHabit(pastureId, newNeckActiveHabitList, xToDay)
|
|
|
+ e.ActiveChange(pastureId, processIds, xToDay)
|
|
|
+ e.Before3DaysNeckActiveHabit(pastureId, processIds)
|
|
|
+ return nil
|
|
|
+}
|
|
|
+
|
|
|
+func (e *Entry) HabitUpdateActiveHabit(pastureId int64, newNeckActiveHabitList []*model.NeckActiveHabit, xToDay *XToday) {
|
|
|
+ for _, v := range newNeckActiveHabitList {
|
|
|
+ // 前七天的
|
|
|
+ weekHabitData := e.FindWeekHabitData(pastureId, v.NeckRingNumber, v.HeatDate, v.Frameid, xToDay)
|
|
|
+ // 更新过滤值
|
|
|
+ if err := e.DB.Model(new(model.NeckActiveHabit)).
|
|
|
+ Select("high_habit", "rumina_habit", "chew_habit", "intake_habit", "inactive_habit").
|
|
|
+ Where("id = ?", v.Id).
|
|
|
+ Updates(map[string]interface{}{
|
|
|
+ "high_habit": weekHabitData.HighHabit,
|
|
|
+ "rumina_habit": weekHabitData.RuminaHabit,
|
|
|
+ "chew_habit": weekHabitData.ChewHabit,
|
|
|
+ "intake_habit": weekHabitData.IntakeHabit,
|
|
|
+ "inactive_habit": weekHabitData.InactiveHabit,
|
|
|
+ }).Error; err != nil {
|
|
|
+ zaplog.Error("WeeklyUpdateActiveHabit",
|
|
|
+ zap.Error(err),
|
|
|
+ zap.Any("NeckActiveHabit", v),
|
|
|
+ zap.Any("pastureId", pastureId),
|
|
|
+ )
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+// SumUpdateActiveHabit -- 累计24小时数值
|
|
|
+func (e *Entry) SumUpdateActiveHabit(pastureId int64, newNeckActiveHabitList []*model.NeckActiveHabit, xToDay *XToday) {
|
|
|
+ for _, v := range newNeckActiveHabitList {
|
|
|
+ sumHabitData := e.FindSumHabitData(pastureId, v.NeckRingNumber, v.HeatDate, v.Frameid, xToDay)
|
|
|
+ // 更新过滤值
|
|
|
+ if err := e.DB.Model(new(model.NeckActiveHabit)).
|
|
|
+ Select("sum_rumina", "sum_intake", "sum_inactive", "sum_active", "sum_max_high", "sum_min_high", "sum_min_chew").
|
|
|
+ Where("id = ?", v.Id).
|
|
|
+ Updates(map[string]interface{}{
|
|
|
+ "sum_rumina": sumHabitData.SumRumina,
|
|
|
+ "sum_intake": sumHabitData.SumIntake,
|
|
|
+ "sum_inactive": sumHabitData.SumInactive,
|
|
|
+ "sum_active": sumHabitData.SumActive,
|
|
|
+ "sum_max_high": sumHabitData.SumMaxHigh,
|
|
|
+ "sum_min_high": sumHabitData.SumMinHigh,
|
|
|
+ "sum_min_chew": sumHabitData.SumMinChew,
|
|
|
+ }).Error; err != nil {
|
|
|
+ zaplog.Error("WeeklyUpdateActiveHabit",
|
|
|
+ zap.Any("err", err),
|
|
|
+ zap.Any("NeckActiveHabit", v),
|
|
|
+ zap.Any("pastureId", pastureId),
|
|
|
+ )
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+// ActiveChange -- 变化百分比
|
|
|
+func (e *Entry) ActiveChange(pastureId int64, processIds []int64, xToDay *XToday) {
|
|
|
+ newNeckActiveHabitList := make([]*model.NeckActiveHabit, 0)
|
|
|
+ if err := e.DB.Model(new(model.NeckActiveHabit)).
|
|
|
+ Where("id IN (?)", processIds).
|
|
|
+ Where("high_habit > ?", 0).
|
|
|
+ Where(e.DB.Where("high >= ?", xToDay.High).Or("rumina >= ?", xToDay.Rumina)).
|
|
|
+ Find(&newNeckActiveHabitList).Error; err != nil {
|
|
|
+ zaplog.Error("ActiveChange", zap.Any("error", err), zap.Any("processIds", processIds))
|
|
|
+ }
|
|
|
+
|
|
|
+ for _, v := range newNeckActiveHabitList {
|
|
|
+ highDiff := v.FilterHigh - v.HighHabit
|
|
|
+ denominator := float64(v.WeekHigh)*0.6 + float64(v.HighHabit)*0.2 + float64(xToDay.WeeklyActive)*0.2
|
|
|
+ if highDiff > 0 {
|
|
|
+ v.ChangeHigh = int32(math.Round((float64(highDiff) / denominator / float64(v.HighHabit)) * 100))
|
|
|
+ } else {
|
|
|
+ v.ChangeHigh = int32(math.Round(float64(highDiff) / float64(v.HighHabit) * 100))
|
|
|
+ }
|
|
|
+
|
|
|
+ if v.RuminaHabit != 0 {
|
|
|
+ v.ChangeRumina = int32(math.Round(float64(v.FilterRumina-v.RuminaHabit) / float64(v.RuminaHabit) * 100))
|
|
|
+ }
|
|
|
+
|
|
|
+ if v.ChewHabit != 0 {
|
|
|
+ v.ChangeChew = int32(math.Round(float64(v.FilterChew-v.ChewHabit) / float64(v.ChewHabit) * 100))
|
|
|
+ }
|
|
|
+
|
|
|
+ // 更新过滤值
|
|
|
+ if err := e.DB.Model(new(model.NeckActiveHabit)).
|
|
|
+ Select("change_high", "change_rumina", "change_chew").
|
|
|
+ Where("id = ?", v.Id).
|
|
|
+ Updates(map[string]interface{}{
|
|
|
+ "change_high": v.ChangeHigh,
|
|
|
+ "change_rumina": v.ChangeRumina,
|
|
|
+ "change_chew": v.ChangeChew,
|
|
|
+ }).Error; err != nil {
|
|
|
+ zaplog.Error("WeeklyUpdateActiveHabit",
|
|
|
+ zap.Error(err),
|
|
|
+ zap.Any("NeckActiveHabit", v),
|
|
|
+ zap.Any("pastureId", pastureId),
|
|
|
+ )
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+func (e *Entry) Before3DaysNeckActiveHabit(pastureId int64, processIds []int64) {
|
|
|
+ newNeckActiveHabitList := make([]*model.NeckActiveHabit, 0)
|
|
|
+ if err := e.DB.Model(new(model.NeckActiveHabit)).
|
|
|
+ Where("id IN (?)", processIds).
|
|
|
+ Order("heat_date,neck_ring_number,frameid").
|
|
|
+ Find(&newNeckActiveHabitList).Error; err != nil {
|
|
|
+ zaplog.Error("Before3DaysNeckActiveHabit", zap.Any("error", err), zap.Any("processIds", processIds))
|
|
|
+ }
|
|
|
+
|
|
|
+ for _, v := range newNeckActiveHabitList {
|
|
|
+ before3DaysNeckActiveHabit := e.FindBefore3DaysNeckActiveHabit(pastureId, v.NeckRingNumber, v.HeatDate, v.Frameid)
|
|
|
+ if before3DaysNeckActiveHabit.SumRumina == 0 && before3DaysNeckActiveHabit.SumIntake == 0 {
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ // 更新过滤值
|
|
|
+ if err := e.DB.Model(new(model.NeckActiveHabit)).
|
|
|
+ Select("before_three_sum_rumina", "before_three_sum_intake").
|
|
|
+ Where("id = ?", v.Id).
|
|
|
+ Updates(map[string]interface{}{
|
|
|
+ "before_three_sum_rumina": before3DaysNeckActiveHabit.SumRumina,
|
|
|
+ "before_three_sum_intake": before3DaysNeckActiveHabit.SumIntake,
|
|
|
+ }).Error; err != nil {
|
|
|
+ zaplog.Error("Before3DaysNeckActiveHabit",
|
|
|
+ zap.Error(err),
|
|
|
+ zap.Any("NeckActiveHabit", v),
|
|
|
+ zap.Any("pastureId", pastureId),
|
|
|
+ )
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|