|
@@ -13,14 +13,16 @@ import (
|
|
|
)
|
|
|
|
|
|
const (
|
|
|
- DefaultLimit = 10000
|
|
|
- DefaultChangeFilter = 0
|
|
|
- MinChangeFilter = -99
|
|
|
- MinChangeHigh = -99
|
|
|
+ DefaultLimit = 10000
|
|
|
+ MinChangeFilter = -99
|
|
|
+ MinRuminaFilter = -99
|
|
|
+ MinChewFilter = -99
|
|
|
+ MinChangeHigh = -99
|
|
|
+ DefaultNb = 30
|
|
|
)
|
|
|
|
|
|
-
|
|
|
-func (e *Entry) NeckRingMergeData() error {
|
|
|
+
|
|
|
+func (e *Entry) NeckRingOriginalMergeData() error {
|
|
|
|
|
|
if ok := e.IsExistCrontabLog(NeckRingOriginal); ok {
|
|
|
return nil
|
|
@@ -115,68 +117,64 @@ func (e *Entry) NeckRingMergeData() error {
|
|
|
}
|
|
|
|
|
|
func (e *Entry) ActiveHabit() error {
|
|
|
-
|
|
|
- lastNeckActiveHabit := &model.NeckActiveHabit{}
|
|
|
+ lastMaxHabitId := e.GetSystemConfigure(model.MaxHabit).Value
|
|
|
+ currentMaxHabit := &model.NeckActiveHabit{}
|
|
|
if err := e.DB.Model(new(model.NeckActiveHabit)).
|
|
|
- Where("is_show = ?", pasturePb.IsShow_No).
|
|
|
- Order("id").First(lastNeckActiveHabit).Error; err != nil {
|
|
|
+ Order("id desc").First(currentMaxHabit).Error; err != nil {
|
|
|
return xerr.WithStack(err)
|
|
|
}
|
|
|
|
|
|
- xToday := &XToday{}
|
|
|
- if err := e.DB.Model(new(model.NeckActiveHabit)).
|
|
|
- Select(`MIN(TO_DAYS(h.heat_date)*1000 + h.frameid) as xTodaySBegFrameid, MIN(h.heat_date) as xBegDate,
|
|
|
- MAX(TO_DAYS(h.heat_date)*1000 + h.frameid) as xTodaySMaxFrameid, MAX(h.heat_date) as xEndDate, MAX(h.id) as xEndUpdateActId`).
|
|
|
- Where("id >= ?", lastNeckActiveHabit.Id).First(xToday).Error; err != nil {
|
|
|
- return xerr.WithStack(err)
|
|
|
+
|
|
|
+ if currentMaxHabit.Id < int64(lastMaxHabitId) {
|
|
|
+ return nil
|
|
|
}
|
|
|
|
|
|
- minHeatDate := struct {
|
|
|
- HeatDate string
|
|
|
- }{}
|
|
|
-
|
|
|
+
|
|
|
if err := e.DB.Model(new(model.NeckActiveHabit)).
|
|
|
- Select("MIN(heat_date) as heat_date").
|
|
|
- Where("id >= ?", lastNeckActiveHabit.Id).
|
|
|
- First(&minHeatDate).Error; err != nil {
|
|
|
+ Where("is_max_time = ?", pasturePb.IsShow_Ok).
|
|
|
+ Update("is_max_time", pasturePb.IsShow_No).Error; err != nil {
|
|
|
return xerr.WithStack(err)
|
|
|
}
|
|
|
- minHeatDateParse, _ := time.Parse(model.LayoutDate2, minHeatDate.HeatDate)
|
|
|
- xMin2Id := struct {
|
|
|
- Id int64
|
|
|
- }{}
|
|
|
+
|
|
|
+
|
|
|
+ xToday := &XToday{}
|
|
|
if err := e.DB.Model(new(model.NeckActiveHabit)).
|
|
|
- Select("IFNULL(MIN(id), ?) as id", lastNeckActiveHabit.Id).
|
|
|
- Where("heat_date = ?", minHeatDateParse.AddDate(0, 0, -1).Format(model.LayoutDate2)).
|
|
|
- First(&xMin2Id).Error; err != nil {
|
|
|
+ Select(`MIN(h.heat_date) as x_beg_date, MAX(h.heat_date) as x_end_date`).
|
|
|
+ Where("id BETWEEN ? AND ?", lastMaxHabitId, currentMaxHabit).
|
|
|
+ First(xToday).Error; err != nil {
|
|
|
return xerr.WithStack(err)
|
|
|
}
|
|
|
|
|
|
- xMin7Id := struct {
|
|
|
- Id int64
|
|
|
- }{}
|
|
|
- XBegDateTime, _ := time.Parse(model.LayoutDate2, xToday.XBegDate)
|
|
|
- if err := e.DB.Model(new(model.NeckActiveHabit)).
|
|
|
- Select("MIN(id) as id").
|
|
|
- Where("heat_date >= ?", XBegDateTime.AddDate(0, 0, -7).Format(model.LayoutDate2)).
|
|
|
- First(&xMin7Id).Error; err != nil {
|
|
|
+ xToday.LastMaxHabitId = int64(lastMaxHabitId)
|
|
|
+ xToday.CurrMaxHabitId = currentMaxHabit.Id
|
|
|
+
|
|
|
+ minHeatDateParse, err := time.Parse(model.LayoutDate2, xToday.XBegDate)
|
|
|
+ if err != nil {
|
|
|
return xerr.WithStack(err)
|
|
|
}
|
|
|
-
|
|
|
- if err := e.DB.Model(new(model.NeckActiveHabit)).
|
|
|
- Where("is_max_time = ?", pasturePb.IsShow_Ok).
|
|
|
- Update("is_max_time", pasturePb.IsShow_No).Error; err != nil {
|
|
|
+ xBefore2Day := minHeatDateParse.AddDate(0, 0, -1).Format(model.LayoutDate2)
|
|
|
+ xBefore7Day := minHeatDateParse.AddDate(0, 0, -7).Format(model.LayoutDate2)
|
|
|
+ xMin2Id, err := e.GetMinIdByHeatDate(xBefore2Day, xToday.LastMaxHabitId)
|
|
|
+ if err != nil {
|
|
|
return xerr.WithStack(err)
|
|
|
}
|
|
|
- xBegDate, _ := time.Parse(model.LayoutDate2, xToday.XBegDate)
|
|
|
- before7xBegDate := xBegDate.AddDate(0, 0, 7).Format(model.LayoutDate2)
|
|
|
+
|
|
|
+ xMin7Id, err := e.GetMinIdByHeatDate(xBefore7Day, xToday.LastMaxHabitId)
|
|
|
+ if err != nil {
|
|
|
+ return xerr.WithStack(err)
|
|
|
+ }
|
|
|
+ xToday.XMin2Id = xMin2Id
|
|
|
+ xToday.XMin7Id = xMin7Id
|
|
|
+
|
|
|
+
|
|
|
sqlQuery := e.DB.Model(new(model.NeckActiveHabit)).
|
|
|
Select("MAX(id) as id").
|
|
|
- Where("id BETWEEN ? AND ?", xMin2Id.Id, lastNeckActiveHabit.Id).
|
|
|
+ Where("id BETWEEN ? AND ?", xToday.XMin2Id, xToday.LastMaxHabitId).
|
|
|
Where("change_filter > ?", MinChangeFilter).
|
|
|
- Where("heat_date >", before7xBegDate).Group("cow_id")
|
|
|
+ Where("heat_date >", xBefore7Day).
|
|
|
+ Group("cow_id")
|
|
|
|
|
|
- if err := e.DB.Model(new(model.NeckActiveHabit)).
|
|
|
+ if err = e.DB.Model(new(model.NeckActiveHabit)).
|
|
|
Joins("JOIN (?) bb ON neck_active_habit.id = bb.id", sqlQuery).
|
|
|
Update("is_max_time", pasturePb.IsShow_Ok).Error; err != nil {
|
|
|
return xerr.WithStack(err)
|
|
@@ -184,24 +182,34 @@ func (e *Entry) ActiveHabit() error {
|
|
|
|
|
|
activeLowest := e.GetSystemConfigure(model.ActiveLowest)
|
|
|
ruminaLowest := e.GetSystemConfigure(model.RuminaLowest)
|
|
|
+ xToday.ActiveLowest = int64(activeLowest.Value)
|
|
|
+ xToday.RuminaLowest = int64(ruminaLowest.Value)
|
|
|
|
|
|
|
|
|
- if err := e.FilterUpdate(activeLowest.Value, ruminaLowest.Value); err != nil {
|
|
|
+ if err = e.FilterUpdate(xToday); err != nil {
|
|
|
return xerr.WithStack(err)
|
|
|
}
|
|
|
|
|
|
- if err := e.WeeklyActiveAvgUpdate(xMin2Id.Id, activeLowest.Value, ruminaLowest.Value, xToday, lastNeckActiveHabit); err != nil {
|
|
|
+ if err = e.WeeklyActiveAvgUpdate(xToday); err != nil {
|
|
|
+ return xerr.WithStack(err)
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ if err = e.DB.Model(new(model.SystemConfigure)).
|
|
|
+ Where("name = ?", model.MaxHabit).
|
|
|
+ Update("value = ?", xToday.CurrMaxHabitId+1).
|
|
|
+ Error; err != nil {
|
|
|
return xerr.WithStack(err)
|
|
|
}
|
|
|
return nil
|
|
|
}
|
|
|
|
|
|
|
|
|
-func (e *Entry) FilterUpdate(activeLowest, ruminaLowest int32) error {
|
|
|
+func (e *Entry) FilterUpdate(xToDay *XToday) error {
|
|
|
newNeckActiveHabitList := make([]*model.NeckActiveHabit, 0)
|
|
|
if err := e.DB.Model(new(model.NeckActiveHabit)).
|
|
|
- Where(e.DB.Where("change_filter = ?", DefaultChangeFilter).Or("is_max_time = ?", pasturePb.IsShow_Ok)).
|
|
|
- Where(e.DB.Where("high >= ?", activeLowest).Or("rumina >= ?", ruminaLowest)).
|
|
|
+ Where(e.DB.Where("change_filter = ?", model.DefaultChangeFilter).Or("is_max_time = ?", pasturePb.IsShow_Ok)).
|
|
|
+ Where(e.DB.Where("high >= ?", xToDay.ActiveLowest).Or("rumina >= ?", xToDay.RuminaLowest)).
|
|
|
Order("cow_id,id").
|
|
|
Find(&newNeckActiveHabitList).Error; err != nil {
|
|
|
return xerr.WithStack(err)
|
|
@@ -245,56 +253,46 @@ func (e *Entry) FilterUpdate(activeLowest, ruminaLowest int32) error {
|
|
|
return nil
|
|
|
}
|
|
|
|
|
|
-func (e *Entry) WeeklyActiveAvgUpdate(xMin2Id int64, activeLowest, ruminaLowest int32, xToday *XToday, lastNeckActiveHabit *model.NeckActiveHabit) error {
|
|
|
- before7DaysMinId := struct {
|
|
|
- Id int64 `json:"id"`
|
|
|
- }{
|
|
|
- Id: 0,
|
|
|
- }
|
|
|
- before7DayDate, _ := time.Parse(model.LayoutDate2, xToday.XBegDate)
|
|
|
- before7DayDateStr := before7DayDate.AddDate(0, 0, -7).Format(model.LayoutDate2)
|
|
|
- before1DayDateStr := before7DayDate.AddDate(0, 0, -1).Format(model.LayoutDate2)
|
|
|
-
|
|
|
- if err := e.DB.Model(new(model.NeckActiveHabit)).
|
|
|
- Select("MIN(id) as id").
|
|
|
- Where("heat_date >= ?", before7DayDateStr).
|
|
|
- First(&before7DaysMinId).Error; err != nil {
|
|
|
+func (e *Entry) WeeklyActiveAvgUpdate(xToday *XToday) error {
|
|
|
+ beginDayDate, err := time.Parse(model.LayoutDate2, xToday.XBegDate)
|
|
|
+ if err != nil {
|
|
|
return xerr.WithStack(err)
|
|
|
}
|
|
|
+ before7DayDate := beginDayDate.AddDate(0, 0, -7).Format(model.LayoutDate2)
|
|
|
+ before1DayDate := beginDayDate.AddDate(0, 0, -1).Format(model.LayoutDate2)
|
|
|
|
|
|
weeklyActive := e.GetSystemConfigure(model.WeeklyActive)
|
|
|
xframeId := int64(0)
|
|
|
maxXframeId := int64(11)
|
|
|
- currDate, _ := time.Parse(model.LayoutDate2, xToday.XBegDate)
|
|
|
- XEndDateTime, _ := time.Parse(model.LayoutDate2, xToday.XEndDate)
|
|
|
- for currDate.Format(model.LayoutDate2) < XEndDateTime.Format(model.LayoutDate2) || (currDate == XEndDateTime && xframeId <= maxXframeId) {
|
|
|
-
|
|
|
+ xStartDate, _ := time.Parse(model.LayoutDate2, xToday.XBegDate)
|
|
|
+ xEndDate, _ := time.Parse(model.LayoutDate2, xToday.XEndDate)
|
|
|
+ for xStartDate.Format(model.LayoutDate2) < xEndDate.Format(model.LayoutDate2) || (xStartDate == xEndDate && xframeId <= maxXframeId) {
|
|
|
|
|
|
- weeklyHabitList := make([]*WeeklyHabit, 0)
|
|
|
- if err := e.DB.Model(new(model.NeckActiveHabit)).
|
|
|
+ AvgHabitList := make([]*AvgHabit, 0)
|
|
|
+ if err = e.DB.Model(new(model.NeckActiveHabit)).
|
|
|
Select("cow_id").
|
|
|
- Select("IF(COUNT(1)>=3, ROUND((SUM(filter_high) -MIN(filter_high) -MAX(filter_high))/ABS(COUNT(1) -2),0), -1) as week_avg_high_habit").
|
|
|
- Select("IF(COUNT(1)>=3, ROUND((SUM(filter_rumina) -MIN(filter_rumina) -MAX(filter_rumina))/ABS(COUNT(1) -2),0), -1) as week_avg_rumina_habit").
|
|
|
- Select("IF(COUNT(1)>=3, ROUND((SUM(filter_chew) -MIN(filter_chew) -MAX(filter_chew))/ABS(COUNT(1) -2),0), -1) as week_avg_chew_habit").
|
|
|
- Select("ROUND(AVG(intake),0) as week_avg_intake_habit").
|
|
|
- Select("ROUND(AVG(inactive),0) as week_avg_inactive_habit").
|
|
|
- Where("id BETWEEN ? AND ?", xMin2Id, xToday.xEndUpdateActId).
|
|
|
- Where("heat_date BETWEEN ? AND ?", before7DayDateStr, before1DayDateStr).
|
|
|
+ Select("IF(COUNT(1)>=3, ROUND((SUM(filter_high) -MIN(filter_high) -MAX(filter_high))/ABS(COUNT(1) -2),0), -1) as avg_high_habit").
|
|
|
+ Select("IF(COUNT(1)>=3, ROUND((SUM(filter_rumina) -MIN(filter_rumina) -MAX(filter_rumina))/ABS(COUNT(1) -2),0), -1) as avg_rumina_habit").
|
|
|
+ Select("IF(COUNT(1)>=3, ROUND((SUM(filter_chew) -MIN(filter_chew) -MAX(filter_chew))/ABS(COUNT(1) -2),0), -1) as avg_chew_habit").
|
|
|
+ Select("ROUND(AVG(intake),0) as avg_intake_habit").
|
|
|
+ Select("ROUND(AVG(inactive),0) as avg_inactive_habit").
|
|
|
+ Where("id BETWEEN ? AND ?", xToday.XMin7Id, xToday.CurrMaxHabitId).
|
|
|
+ Where("heat_date BETWEEN ? AND ?", before7DayDate, before1DayDate).
|
|
|
Where("frameid = ?", xframeId).
|
|
|
- Where("change_filter = ?", DefaultChangeFilter).
|
|
|
- Where(e.DB.Where("high > ?", activeLowest).Or("rumina > ?", ruminaLowest)).
|
|
|
+ Where("change_filter = ?", model.DefaultChangeFilter).
|
|
|
+ Where(e.DB.Where("high > ?", xToday.ActiveLowest).Or("rumina > ?", xToday.RuminaLowest)).
|
|
|
Group("cow_id").
|
|
|
- Find(&weeklyHabitList).Error; err != nil {
|
|
|
+ Find(&AvgHabitList).Error; err != nil {
|
|
|
return xerr.WithStack(err)
|
|
|
}
|
|
|
- for _, v := range weeklyHabitList {
|
|
|
+ for _, v := range AvgHabitList {
|
|
|
if err := e.DB.Model(new(model.NeckActiveHabit)).
|
|
|
- Select("week_avg_high_habit", "week_avg_rumina_habit", "week_avg_chew_habit", "week_avg_intake_habit", "week_avg_inactive_habit").
|
|
|
+ Select("week_avg_high_habit", "avg_rumina_habit", "avg_chew_habit", "avg_intake_habit", "avg_inactive_habit").
|
|
|
Where("cow_id = ?", v.CowId).
|
|
|
- Where("id BETWEEN ? AND ?", lastNeckActiveHabit.Id, xToday.xEndUpdateActId).
|
|
|
+ Where("id BETWEEN ? AND ?", xToday.LastMaxHabitId, xToday.CurrMaxHabitId).
|
|
|
Where("frameid = ?", xframeId).
|
|
|
- Where("change_filter = ?", DefaultChangeFilter).
|
|
|
- Where("heat_date = ?", currDate).
|
|
|
+ Where("change_filter = ?", model.DefaultChangeFilter).
|
|
|
+ Where("heat_date = ?", xStartDate).
|
|
|
Updates(v).Error; err != nil {
|
|
|
return xerr.WithStack(err)
|
|
|
}
|
|
@@ -302,7 +300,7 @@ func (e *Entry) WeeklyActiveAvgUpdate(xMin2Id int64, activeLowest, ruminaLowest
|
|
|
|
|
|
|
|
|
sumHabitList := make([]*SumHabit, 0)
|
|
|
- if err := e.DB.Model(new(model.NeckActiveHabit)).
|
|
|
+ if err = e.DB.Model(new(model.NeckActiveHabit)).
|
|
|
Select("cow_id").
|
|
|
Select("IF(COUNT(1)>6, ROUND(AVG( h2.filter_rumina)*12,0), 0) as sum_rumina").
|
|
|
Select("IF(COUNT(1)>6, ROUND(AVG( h2.intake)*12,0), 0) as sum_intake").
|
|
@@ -310,50 +308,55 @@ func (e *Entry) WeeklyActiveAvgUpdate(xMin2Id int64, activeLowest, ruminaLowest
|
|
|
Select("IF(COUNT(1)>6, ROUND(AVG( h2.active)*12,0), 0) as sum_active").
|
|
|
Select("MAX(h2.change_filter) as sum_max_high").
|
|
|
Select("MIN(IF(change_filter > ?, change_filter, 0)) as sum_min_high", MinChangeFilter).
|
|
|
- Select("MIN( CASE WHEN filter_chew > ? THEN filter_chew WHEN filter_rumina >= ? THEN filter_rumina ELSE 0 END) as sum_min_chew", MinChangeFilter, MinChangeFilter).
|
|
|
- Where("id BETWEEN ? AND ?", before7DaysMinId.Id, xToday.xEndUpdateActId).
|
|
|
- Where("heat_date BETWEEN ? AND ?", currDate.AddDate(0, 0, -1).Format(model.LayoutDate2), currDate.Format(model.LayoutDate2)).
|
|
|
- Where("created_at BETWEEN ? AND ?", currDate.Add(-23*time.Hour), currDate.Unix()).
|
|
|
- Where(e.DB.Where("high > ?", activeLowest).Or("rumina >= ?", ruminaLowest)).
|
|
|
+ Select("MIN( "+
|
|
|
+ "CASE WHEN filter_chew > ? "+
|
|
|
+ "THEN filter_chew "+
|
|
|
+ "WHEN filter_rumina >= ? "+
|
|
|
+ "THEN filter_rumina "+
|
|
|
+ "ELSE 0 END) as sum_min_chew", MinChangeFilter, MinRuminaFilter).
|
|
|
+ Where("id BETWEEN ? AND ?", xToday.XMin2Id, xToday.CurrMaxHabitId).
|
|
|
+ Where("heat_date BETWEEN ? AND ?", xStartDate.AddDate(0, 0, -1).Format(model.LayoutDate2), xStartDate.Format(model.LayoutDate2)).
|
|
|
+ Where("created_at BETWEEN ? AND ?", xStartDate.Add(-23*time.Hour).Unix(), xStartDate.Unix()).
|
|
|
+ Where(e.DB.Where("high > ?", xToday.ActiveLowest).Or("rumina >= ?", xToday.RuminaLowest)).
|
|
|
Group("cow_id").
|
|
|
Find(&sumHabitList).Error; err != nil {
|
|
|
return xerr.WithStack(err)
|
|
|
}
|
|
|
|
|
|
for _, v := range sumHabitList {
|
|
|
- if err := e.DB.Model(new(model.NeckActiveHabit)).
|
|
|
+ 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("cow_id = ?", v.CowId).
|
|
|
- Where("id BETWEEN ? AND ?", lastNeckActiveHabit.Id, xToday.xEndUpdateActId).
|
|
|
- Where("heat_date = ?", currDate.Format(model.LayoutDate2)).
|
|
|
+ Where("id BETWEEN ? AND ?", xToday.LastMaxHabitId, xToday.CurrMaxHabitId).
|
|
|
+ Where("heat_date = ?", xStartDate.Format(model.LayoutDate2)).
|
|
|
Where("frameid = ?", xframeId).
|
|
|
- Where("change_filter = ?", DefaultChangeFilter).
|
|
|
+ Where("change_filter = ?", model.DefaultChangeFilter).
|
|
|
Updates(v).Error; err != nil {
|
|
|
return xerr.WithStack(err)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
changeHabitList := make([]*model.NeckActiveHabit, 0)
|
|
|
- if err := e.DB.Model(new(model.NeckActiveHabit)).
|
|
|
- Where("id BETWEEN ? AND ?", lastNeckActiveHabit.Id, xToday.xEndUpdateActId).
|
|
|
- Where("heat_date = ?", currDate.Format(model.LayoutDate2)).
|
|
|
+ if err = e.DB.Model(new(model.NeckActiveHabit)).
|
|
|
+ Where("id BETWEEN ? AND ?", xToday.LastMaxHabitId, xToday.CurrMaxHabitId).
|
|
|
+ Where("heat_date = ?", xStartDate.Format(model.LayoutDate2)).
|
|
|
Where("frameid = ?", xframeId).
|
|
|
- Where("change_filter = ?", DefaultChangeFilter).
|
|
|
+ Where("change_filter = ?", model.DefaultChangeFilter).
|
|
|
Where("week_avg_high_habit > ?", 0).
|
|
|
- Where(e.DB.Where("high > ?", activeLowest).Or("rumina >= ?", ruminaLowest)).
|
|
|
+ Where(e.DB.Where("high > ?", xToday.ActiveLowest).Or("rumina >= ?", xToday.RuminaLowest)).
|
|
|
Find(&changeHabitList).Error; err != nil {
|
|
|
return xerr.WithStack(err)
|
|
|
}
|
|
|
|
|
|
for _, v := range changeHabitList {
|
|
|
- if v.FilterHigh-v.WeekAvgHighHabit > 0 {
|
|
|
- v.ChangeHigh = (v.FilterHigh - v.WeekAvgHighHabit) / int32(float64(v.WeekHigh)*0.6+float64(v.WeekAvgHighHabit)*0.2+float64(weeklyActive.Value)*0.2)
|
|
|
+ if v.FilterHigh-v.AvgHighHabit > 0 {
|
|
|
+ v.ChangeHigh = (v.FilterHigh - v.AvgHighHabit) / int32(float64(v.WeekHigh)*0.6+float64(v.AvgHighHabit)*0.2+float64(weeklyActive.Value)*0.2)
|
|
|
} else {
|
|
|
- v.ChangeHigh = v.FilterHigh - v.WeekAvgHighHabit/v.WeekAvgHighHabit*100
|
|
|
+ v.ChangeHigh = v.FilterHigh - v.AvgHighHabit/v.AvgHighHabit*100
|
|
|
}
|
|
|
|
|
|
- v.ChangeRumina = v.RuminaFilter - v.WeekAvgRuminaHabit/v.WeekAvgHighHabit*100
|
|
|
- v.ChangeChew = v.FilterChew - v.WeekAvgChewHabit/v.WeekAvgHighHabit*100
|
|
|
+ v.ChangeRumina = v.RuminaFilter - v.AvgRuminaHabit/v.AvgHighHabit*100
|
|
|
+ v.ChangeChew = v.FilterChew - v.AvgChewHabit/v.AvgHighHabit*100
|
|
|
if err := e.DB.Model(new(model.NeckActiveHabit)).
|
|
|
Select("change_high", "change_rumina", "change_chew").
|
|
|
Where("id = ?", v.Id).
|
|
@@ -364,7 +367,7 @@ func (e *Entry) WeeklyActiveAvgUpdate(xMin2Id int64, activeLowest, ruminaLowest
|
|
|
|
|
|
if xframeId == maxXframeId {
|
|
|
xframeId = 0
|
|
|
- currDate = currDate.AddDate(0, 0, 1)
|
|
|
+ xStartDate = xStartDate.AddDate(0, 0, 1)
|
|
|
} else {
|
|
|
xframeId++
|
|
|
}
|
|
@@ -374,15 +377,15 @@ func (e *Entry) WeeklyActiveAvgUpdate(xMin2Id int64, activeLowest, ruminaLowest
|
|
|
}
|
|
|
|
|
|
|
|
|
-func (e *Entry) UpdateChangeFilter(xMin2Id int64, xToday *XToday) error {
|
|
|
+func (e *Entry) UpdateChangeFilter(xToday *XToday) error {
|
|
|
xRuminaDisc := e.GetSystemConfigure(model.XRuminaDisc)
|
|
|
xChangeDiscount := e.GetSystemConfigure(model.XChangeDiscount)
|
|
|
newChangeFilterList := make([]*ChangeFilterData, 0)
|
|
|
if err := e.DB.Model(new(model.NeckActiveHabit)).
|
|
|
Select("id,cow_id,change_high,change_filter,rumina_filter,change_rumina,chew_filter,change_chew").
|
|
|
Select("IF(lact=0,0.8,1) as xlc_dis_count").
|
|
|
- Where("id BETWEEN ? AND ?", xMin2Id, xToday.xEndUpdateActId).
|
|
|
- Where(e.DB.Where("change_filter = ?", DefaultChangeFilter).Or("is_max_time = ?", pasturePb.IsShow_Ok)).
|
|
|
+ Where("id BETWEEN ? AND ?", xToday.XMin2Id, xToday.CurrMaxHabitId).
|
|
|
+ Where(e.DB.Where("change_filter = ?", model.DefaultChangeFilter).Or("is_max_time = ?", pasturePb.IsShow_Ok)).
|
|
|
Where("change_high > ?", MinChangeHigh).
|
|
|
Order("cow_id,heat_date,frameid").
|
|
|
Find(&newChangeFilterList).Error; err != nil {
|
|
@@ -442,7 +445,9 @@ func (e *Entry) UpdateChangeFilter(xMin2Id int64, xToday *XToday) error {
|
|
|
}
|
|
|
if err := e.DB.Model(new(model.NeckActiveHabit)).
|
|
|
Select("change_filter", "rumina_filter", "chew_filter").
|
|
|
- Where("id = ?", v.Id).Where("cow_id = ?", v.CowId).
|
|
|
+ Where("id = ?", v.Id).
|
|
|
+ Where("cow_id = ?", v.CowId).
|
|
|
+ Where("change_filter = ?", model.DefaultChangeFilter).
|
|
|
Updates(v).Error; err != nil {
|
|
|
return xerr.WithStack(err)
|
|
|
}
|
|
@@ -453,89 +458,104 @@ func (e *Entry) UpdateChangeFilter(xMin2Id int64, xToday *XToday) error {
|
|
|
}
|
|
|
|
|
|
|
|
|
-func (e *Entry) ActivityVolumeChanges(xMin7Id int64, activeLowest, ruminaLowest int32, xToday *XToday, lastNeckActiveHabitId int64) error {
|
|
|
+func (e *Entry) ActivityVolumeChanges(xToday *XToday) error {
|
|
|
currDate, _ := time.Parse(model.LayoutDate2, xToday.XBegDate)
|
|
|
XEndDateTime, _ := time.Parse(model.LayoutDate2, xToday.XEndDate)
|
|
|
xframeId := int64(0)
|
|
|
maxXframeId := int64(11)
|
|
|
dayTimes := int64(1)
|
|
|
for currDate.Format(model.LayoutDate2) < XEndDateTime.Format(model.LayoutDate2) || (currDate == XEndDateTime && xframeId <= maxXframeId) {
|
|
|
+ activityVolumeList := make([]*ActivityVolume, 0)
|
|
|
+ activeTime := fmt.Sprintf("%s %02d:00:00", currDate.Format(model.LayoutDate2), xframeId*2)
|
|
|
+ activeTimeParse, err := time.Parse(model.LayoutTime, activeTime)
|
|
|
+ if err != nil {
|
|
|
+ return xerr.WithStack(err)
|
|
|
+ }
|
|
|
if dayTimes == 1 {
|
|
|
- ActivityVolumeList := make([]*ActivityVolume, 0)
|
|
|
- if err := e.DB.Model(new(model.NeckActiveHabit)).
|
|
|
+ if err = e.DB.Model(new(model.NeckActiveHabit)).
|
|
|
Select("cow_id").
|
|
|
- Where("id BETWEEN ? AND ?", xMin7Id, xToday.xEndUpdateActId).
|
|
|
+ Select("").
|
|
|
+ Where("id BETWEEN ? AND ?", xToday.XMin7Id, xToday.CurrMaxHabitId).
|
|
|
Where("heat_date BETWEEN ? AND ?", currDate.AddDate(0, 0, -7).Format(model.LayoutDate2), currDate.AddDate(0, 0, -1).Format(model.LayoutDate2)).
|
|
|
Where("frameid = ?", xframeId).
|
|
|
- Where(e.DB.Where("high > ?", activeLowest).Or("rumina >= ?", ruminaLowest)).
|
|
|
- Where("created_at <= ?", currDate.Add(-12*time.Hour).Unix()).
|
|
|
+ Where(e.DB.Where("high > ?", xToday.ActiveLowest).Or("rumina >= ?", xToday.RuminaLowest)).
|
|
|
+ Where("active_time <= ?", activeTimeParse.Add(-12*time.Hour)).
|
|
|
+ Where("change_filter > ?", MinChangeFilter).
|
|
|
+ Having("nb > ?", DefaultNb).
|
|
|
Group("cow_id").
|
|
|
- Having("nb > ?", 30).
|
|
|
- Find(&ActivityVolumeList).Error; err != nil {
|
|
|
+ Find(&activityVolumeList).Error; err != nil {
|
|
|
return xerr.WithStack(err)
|
|
|
}
|
|
|
- for _, v := range ActivityVolumeList {
|
|
|
- filterCorrect := 100 - math.Floor(float64(v.AvgFilter)/3+float64(v.StdFilter)/2)
|
|
|
- if err := e.DB.Model(new(model.NeckActiveHabit)).
|
|
|
- Where("cow_id = ?", v.CowId).
|
|
|
- Where("id BETWEEN ? AND ?", lastNeckActiveHabitId, xToday.xEndUpdateActId).
|
|
|
- Where("frameid = ?", xframeId).
|
|
|
- Where("head_date = ?", currDate.Format(model.LayoutDate2)).
|
|
|
- Update("filter_correct", filterCorrect).Error; err != nil {
|
|
|
- return xerr.WithStack(err)
|
|
|
- }
|
|
|
+ }
|
|
|
+
|
|
|
+ for _, v := range activityVolumeList {
|
|
|
+ filterCorrect := model.DefaultFilterCorrect - int(math.Floor(float64(v.AvgFilter)/3+float64(v.StdFilter)/2))
|
|
|
+ if err = e.DB.Model(new(model.NeckActiveHabit)).
|
|
|
+ Where("cow_id = ?", v.CowId).
|
|
|
+ Where("id BETWEEN ? AND ?", xToday.LastMaxHabitId, xToday.CurrMaxHabitId).
|
|
|
+ Where("frameid = ?", xframeId).
|
|
|
+ Where("head_date = ?", currDate.Format(model.LayoutDate2)).
|
|
|
+ Update("filter_correct", filterCorrect).Error; err != nil {
|
|
|
+ return xerr.WithStack(err)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- n := 0
|
|
|
+
|
|
|
if n <= 10 {
|
|
|
|
|
|
|
|
|
|
|
|
n += 2
|
|
|
+ }*/
|
|
|
+
|
|
|
+ if err = e.DB.Model(new(model.NeckActiveHabit)).
|
|
|
+ Where("id BETWEEN ? AND ?", xToday.LastMaxHabitId, xToday.CurrMaxHabitId).
|
|
|
+ Where("heat_date = ?", currDate.Format(model.LayoutDate2)).
|
|
|
+ Where("frameid = ?", xframeId).
|
|
|
+ Where("change_filter = ?", model.DefaultChangeFilter).
|
|
|
+ Update("change_filter", MinChangeFilter).Error; err != nil {
|
|
|
+ return xerr.WithStack(err)
|
|
|
}
|
|
|
|
|
|
- if err := e.DB.Model(new(model.NeckActiveHabit)).
|
|
|
- Where("id BETWEEN ? AND ?", lastNeckActiveHabitId, xToday.xEndUpdateActId).
|
|
|
+ if err = e.DB.Model(new(model.NeckActiveHabit)).
|
|
|
+ Where("id BETWEEN ? AND ?", xToday.LastMaxHabitId, xToday.CurrMaxHabitId).
|
|
|
Where("heat_date = ?", currDate.Format(model.LayoutDate2)).
|
|
|
Where("frameid = ?", xframeId).
|
|
|
- Where("change_filter = ?", 0).
|
|
|
- Update("change_filter", MinChangeHigh).Error; err != nil {
|
|
|
+ Where("rumina_filter = ?", model.DefaultRuminaFilter).
|
|
|
+ Update("rumina_filter", MinRuminaFilter).Error; err != nil {
|
|
|
return xerr.WithStack(err)
|
|
|
}
|
|
|
|
|
|
- if err := e.DB.Model(new(model.NeckActiveHabit)).
|
|
|
- Where("id BETWEEN ? AND ?", lastNeckActiveHabitId, xToday.xEndUpdateActId).
|
|
|
+ if err = e.DB.Model(new(model.NeckActiveHabit)).
|
|
|
+ Where("id BETWEEN ? AND ?", xToday.LastMaxHabitId, xToday.CurrMaxHabitId).
|
|
|
Where("heat_date = ?", currDate.Format(model.LayoutDate2)).
|
|
|
Where("frameid = ?", xframeId).
|
|
|
- Where("rumina_filter = ?", 0).
|
|
|
- Update("rumina_filter", MinChangeHigh).Error; err != nil {
|
|
|
+ Where("chew_filter = ?", model.DefaultChewFilter).
|
|
|
+ Update("chew_filter", MinChewFilter).Error; err != nil {
|
|
|
return xerr.WithStack(err)
|
|
|
}
|
|
|
|
|
|
- if err := e.DB.Model(new(model.NeckActiveHabit)).
|
|
|
- Where("id BETWEEN ? AND ?", lastNeckActiveHabitId, xToday.xEndUpdateActId).
|
|
|
+ if err = e.DB.Model(new(model.NeckActiveHabit)).
|
|
|
+ Where("id BETWEEN ? AND ?", xToday.LastMaxHabitId, xToday.CurrMaxHabitId).
|
|
|
Where("heat_date = ?", currDate.Format(model.LayoutDate2)).
|
|
|
Where("frameid = ?", xframeId).
|
|
|
- Where("filter_correct < ?", 100).
|
|
|
+ Where("filter_correct < ?", model.DefaultFilterCorrect).
|
|
|
Where("change_filter < ?", 0).
|
|
|
- Update("filter_correct", 100).Error; err != nil {
|
|
|
+ Update("filter_correct", model.DefaultFilterCorrect).Error; err != nil {
|
|
|
return xerr.WithStack(err)
|
|
|
}
|
|
|
|
|
|
|
|
|
newNeckActiveHabitList := make([]*model.NeckActiveHabit, 0)
|
|
|
- if err := e.DB.Model(new(model.NeckActiveHabit)).
|
|
|
- Where("id BETWEEN ? AND ?", lastNeckActiveHabitId, xToday.xEndUpdateActId).
|
|
|
+ if err = e.DB.Model(new(model.NeckActiveHabit)).
|
|
|
+ Where("id BETWEEN ? AND ?", xToday.LastMaxHabitId, xToday.CurrMaxHabitId).
|
|
|
Where("heat_date = ?", currDate.Format(model.LayoutDate2)).
|
|
|
Where("frameid = ?", xframeId).
|
|
|
Where("score = ?", 0).
|
|
|
Find(&newNeckActiveHabitList).Error; err != nil {
|
|
|
return xerr.WithStack(err)
|
|
|
}
|
|
|
-
|
|
|
-
|
|
|
- }*/
|
|
|
+
|
|
|
}
|
|
|
|
|
|
return nil
|