package crontab import ( "database/sql" "fmt" "math" "gorm.io/driver/mysql" "gorm.io/gorm" ) type HActiveHabit struct { ID uint IntPastureID uint IntCowID uint VarCowCode string IntCurFetal uint DIM uint Createtime sql.NullString Changeadjust uint Changefilter uint Filtercorrect float64 Ruminafilter int Filterhigh uint Heatdate sql.NullString } func main() { dsn := "username:password@tcp(127.0.0.1:3306)/dbname?charset=utf8mb4&parseTime=True&loc=Local" db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{}) if err != nil { panic("failed to connect database") } // 假设这些变量是从外部传入的 xbegactid, xendactid := uint(1), uint(100) xPastuId := uint(123) xthatdate := "2023-10-01" xactlevellow := float64(10) xadjust21 := float64(2) maxruminaadjust := int(10) rumtoheat := float64(0.5) // 假设值 var habits []HActiveHabit db.Where("id BETWEEN ? AND ? AND int_pasture_id = ? AND heatdate = ?", xbegactid, xendactid, xPastuId, xthatdate). Find(&habits) var results []struct { IntPastureID uint IntCowID uint VarCowCode string IntCurFetal uint DIM uint Createtime string CFT float64 High uint } cowMap := make(map[uint][]HActiveHabit) for _, habit := range habits { cowMap[habit.IntCowID] = append(cowMap[habit.IntCowID], habit) } for cowID, habits := range cowMap { var createtime string var cft float64 var high uint if len(habits) > 0 { sortedHabits := sortHabitsByChangefilterDesc(habits) createtime = sortedHabits[0].Createtime.String maxCft := float64(-math.MaxFloat64) for _, habit := range sortedHabits { adjustedValue := calculateAdjustedValue(habit, maxruminaadjust, rumtoheat) if adjustedValue > maxCft { maxCft = adjustedValue high = habit.Filterhigh } } cft = round(maxCft) } if cft >= (xactlevellow - xadjust21) { anyDimValid := false for _, habit := range habits { if habit.DIM >= 20 || habit.IntCurFetal == 0 { anyDimValid = true break } } if anyDimValid { results = append(results, struct { IntPastureID uint IntCowID uint VarCowCode string IntCurFetal uint DIM uint Createtime string CFT float64 High uint }{ IntPastureID: habits[0].IntPastureID, IntCowID: cowID, VarCowCode: habits[0].VarCowCode, IntCurFetal: habits[0].IntCurFetal, DIM: habits[0].DIM, Createtime: createtime, CFT: cft, High: high, }) } } } for _, result := range results { fmt.Printf("IntPastureID: %d, IntCowID: %d, VarCowCode: %s, IntCurFetal: %d, DIM: %d, Createtime: %s, CFT: %.2f, High: %d\n", result.IntPastureID, result.IntCowID, result.VarCowCode, result.IntCurFetal, result.DIM, result.Createtime, result.CFT, result.High) } } func sortHabitsByChangefilterDesc(habits []HActiveHabit) []HActiveHabit { sorted := make([]HActiveHabit, len(habits)) copy(sorted, habits) for i := range sorted { for j := i + 1; j < len(sorted); j++ { if sorted[i].Changefilter < sorted[j].Changefilter { sorted[i], sorted[j] = sorted[j], sorted[i] } } } return sorted } func calculateAdjustedValue(habit HActiveHabit, maxruminaadjust int, rumtoheat float64) float64 { baseValue := float64(0) if habit.Changeadjust >= 10 { baseValue = float64(habit.Changefilter-habit.Changeadjust+3) * habit.Filtercorrect / 100 } else { baseValue = float64(habit.Changefilter) * habit.Filtercorrect / 100 } ruminaAdjustment := float64(0) if habit.Ruminafilter > maxruminaadjust { ruminaAdjustment = 5 } else if habit.Ruminafilter > 0 { ruminaAdjustment = float64(habit.Ruminafilter) * 0.25 } else if habit.Ruminafilter < -maxruminaadjust { ruminaAdjustment = float64(-maxruminaadjust) * rumtoheat } else { ruminaAdjustment = float64(habit.Ruminafilter) * rumtoheat } return baseValue - ruminaAdjustment } func round(value float64) float64 { return math.Round(value*100) / 100 }