|  | @@ -6,7 +6,6 @@ import (
 | 
	
		
			
				|  |  |  	"kpt-pasture/model"
 | 
	
		
			
				|  |  |  	"kpt-pasture/module/backend"
 | 
	
		
			
				|  |  |  	"kpt-pasture/util"
 | 
	
		
			
				|  |  | -	"sort"
 | 
	
		
			
				|  |  |  	"strings"
 | 
	
		
			
				|  |  |  	"time"
 | 
	
		
			
				|  |  |  
 | 
	
	
		
			
				|  | @@ -250,7 +249,7 @@ func (e *Entry) UpdateCowInfoByPasture(pastureId int64) {
 | 
	
		
			
				|  |  |  	for _, cow := range cowList {
 | 
	
		
			
				|  |  |  		// 周活动量
 | 
	
		
			
				|  |  |  		weeklyActive := e.UpdateCowWeeklyHigh(pastureId, cow)
 | 
	
		
			
				|  |  | -		zaplog.Info("crontab", zap.Any("weeklyActive", weeklyActive))
 | 
	
		
			
				|  |  | +		zaplog.Info("crontab", zap.Any("weeklyActive", weeklyActive), zap.Any("cow", cow))
 | 
	
		
			
				|  |  |  		cow.EventUpdate(weeklyActive)
 | 
	
		
			
				|  |  |  		if err := e.DB.Model(new(model.Cow)).
 | 
	
		
			
				|  |  |  			Select("day_age", "calving_age", "pregnancy_age", "admission_age", "abortion_age", "cow_type", "weekly_active", "lactation_age", "dry_milk_age", "mating_age").
 | 
	
	
		
			
				|  | @@ -641,63 +640,3 @@ func (e *Entry) InitEventData(cowList []*model.Cow, systemBasic *model.SystemBas
 | 
	
		
			
				|  |  |  	}
 | 
	
		
			
				|  |  |  	e.CreatedCalendar(systemBasic.PastureId, calendarType, startDay, endDay, int32(len(cowList)))
 | 
	
		
			
				|  |  |  }
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -// UpdateCowWeeklyHigh 更新牛只周活动量
 | 
	
		
			
				|  |  | -func (e *Entry) UpdateCowWeeklyHigh(pastureId int64, cow *model.Cow) int32 {
 | 
	
		
			
				|  |  | -	highData, err := e.GetSystemNeckRingConfigure(pastureId, model.High)
 | 
	
		
			
				|  |  | -	if err != nil || highData == nil || highData.Value <= 0 {
 | 
	
		
			
				|  |  | -		return 0
 | 
	
		
			
				|  |  | -	}
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -	minWeeklyActive, er := e.GetSystemNeckRingConfigure(pastureId, model.MinWeeklyActive)
 | 
	
		
			
				|  |  | -	if er != nil || minWeeklyActive == nil || minWeeklyActive.Value <= 0 {
 | 
	
		
			
				|  |  | -		return 0
 | 
	
		
			
				|  |  | -	}
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -	weeklyActiveModelList := make([]*model.WeeklyActiveModel, 0)
 | 
	
		
			
				|  |  | -	startTime := time.Now().Local().AddDate(0, 0, -7).Format(model.LayoutDate2)
 | 
	
		
			
				|  |  | -	endTime := time.Now().Local().AddDate(0, 0, -1).Format(model.LayoutDate2)
 | 
	
		
			
				|  |  | -	selectStr := fmt.Sprintf(`cow_id,heat_date,count(1) AS nb,IF(ROUND(AVG(high))>%d, ROUND(AVG(high)), %d) AS high`, minWeeklyActive.Value, minWeeklyActive.Value)
 | 
	
		
			
				|  |  | -	if err = e.DB.Model(new(model.NeckActiveHabit)).
 | 
	
		
			
				|  |  | -		Select(selectStr).
 | 
	
		
			
				|  |  | -		Where("pasture_id = ?", pastureId).
 | 
	
		
			
				|  |  | -		Where("cow_id = ?", cow.Id).
 | 
	
		
			
				|  |  | -		Where("heat_date BETWEEN ? AND ?", startTime, endTime).
 | 
	
		
			
				|  |  | -		Where("high > ?", highData.Value).
 | 
	
		
			
				|  |  | -		Having("nb > ?", 8).
 | 
	
		
			
				|  |  | -		Order("high").
 | 
	
		
			
				|  |  | -		Group("heat_date").
 | 
	
		
			
				|  |  | -		Find(&weeklyActiveModelList).Error; err != nil {
 | 
	
		
			
				|  |  | -		zaplog.Error("crontab", zap.Any("UpdateCowWeeklyHigh", err))
 | 
	
		
			
				|  |  | -		return 0
 | 
	
		
			
				|  |  | -	}
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -	if len(weeklyActiveModelList) < 3 {
 | 
	
		
			
				|  |  | -		return 0
 | 
	
		
			
				|  |  | -	}
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -	// 1. 先按High值排序
 | 
	
		
			
				|  |  | -	sort.Slice(weeklyActiveModelList, func(i, j int) bool {
 | 
	
		
			
				|  |  | -		return weeklyActiveModelList[i].High < weeklyActiveModelList[j].High
 | 
	
		
			
				|  |  | -	})
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -	countHigh := len(weeklyActiveModelList)
 | 
	
		
			
				|  |  | -	sumHigh := int32(0)
 | 
	
		
			
				|  |  | -	minHigh := weeklyActiveModelList[0].High
 | 
	
		
			
				|  |  | -	maxHigh := weeklyActiveModelList[len(weeklyActiveModelList)-1].High
 | 
	
		
			
				|  |  | -	for _, v := range weeklyActiveModelList {
 | 
	
		
			
				|  |  | -		sumHigh += v.High
 | 
	
		
			
				|  |  | -	}
 | 
	
		
			
				|  |  | -	diff := 0
 | 
	
		
			
				|  |  | -	x := int32(0)
 | 
	
		
			
				|  |  | -	if countHigh > 3 {
 | 
	
		
			
				|  |  | -		diff = 3
 | 
	
		
			
				|  |  | -		x = weeklyActiveModelList[1].High
 | 
	
		
			
				|  |  | -	} else if countHigh == 3 {
 | 
	
		
			
				|  |  | -		diff = 2
 | 
	
		
			
				|  |  | -	} else {
 | 
	
		
			
				|  |  | -		return 0
 | 
	
		
			
				|  |  | -	}
 | 
	
		
			
				|  |  | -	avgHigh := float64(sumHigh-minHigh-maxHigh-x) / float64(countHigh-diff)
 | 
	
		
			
				|  |  | -	return int32(avgHigh)
 | 
	
		
			
				|  |  | -}
 |