123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158 |
- 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
- }
|