pen_behavior_day.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. package crontab
  2. import (
  3. "kpt-pasture/model"
  4. "time"
  5. "gitee.com/xuyiping_admin/pkg/logger/zaplog"
  6. "gitee.com/xuyiping_admin/pkg/xerr"
  7. "go.uber.org/zap"
  8. )
  9. const (
  10. MaxDayHigh = 500
  11. MaxRumina = 200
  12. MaxCowCount = 10
  13. MaxNB = 8
  14. Days = 2
  15. )
  16. func (e *Entry) UpdatePenBehaviorDaily() error {
  17. pastureList := e.FindPastureList()
  18. if pastureList == nil || len(pastureList) == 0 {
  19. return nil
  20. }
  21. for _, pasture := range pastureList {
  22. e.BehaviorDaily(pasture.Id, Days)
  23. }
  24. return nil
  25. }
  26. // BehaviorDaily 更新牛只每日行为数据
  27. func (e *Entry) BehaviorDaily(pastureId int64, days int32) {
  28. for i := days; i >= 1; i-- {
  29. targetDate := time.Now().AddDate(0, 0, -int(i)).Format(model.LayoutDate2)
  30. // 1. 更新milk_daily表
  31. if err := e.updateMilkDaily(pastureId, targetDate); err != nil {
  32. zaplog.Error("UpdateBehaviorDaily", zap.Any("pastureId", pastureId), zap.Any("targetDate", targetDate), zap.Any("err", err))
  33. continue
  34. }
  35. // 2. 删除PenBehaviorDay表中的旧数据
  36. if err := e.DB.Where("pasture_id = ? AND heat_date = ?", pastureId, targetDate).
  37. Delete(new(model.PenBehaviorDay)).Error; err != nil {
  38. zaplog.Error("UpdateBehaviorDaily", zap.Any("delete error", err))
  39. continue
  40. }
  41. // 3. 插入新的PenBehaviorDay数据
  42. if err := e.insertBarBehaviorDay(pastureId, targetDate); err != nil {
  43. zaplog.Error("UpdateBehaviorDaily", zap.Any("insert error", err))
  44. continue
  45. }
  46. }
  47. }
  48. // updateMilkDaily 更新milk_daily表中的行为数据
  49. func (e *Entry) updateMilkDaily(pastureId int64, targetDate string) error {
  50. milkDailyModelList := make([]*model.MilkDailyModel, 0)
  51. if err := e.DB.Model(new(model.NeckActiveHabit)).
  52. Select(`cow_id,heat_date, ROUND(AVG(filter_high), 0) as day_high, ROUND(AVG(rumina)*12, 0) as day_rumina, ROUND(AVG(intake)*12, 0) as day_intake,
  53. ROUND(AVG(inactive)*12, 0) as day_inactive, ROUND(AVG(active)*12, 0) as day_active, COUNT(1) as nb`).
  54. Where("pasture_id = ? AND heat_date = ?", pastureId, targetDate).
  55. Where("cow_id > ?", 0).
  56. Group("cow_id").
  57. Having("nb >= ?", MaxNB).
  58. Find(&milkDailyModelList).Error; err != nil {
  59. return xerr.WithStack(err)
  60. }
  61. for _, v := range milkDailyModelList {
  62. if err := e.DB.Model(new(model.MilkDaily)).
  63. Where("pasture_id = ?", pastureId).
  64. Where("cow_id = ? AND heat_date = ?", v.CowId, targetDate).
  65. Updates(map[string]interface{}{
  66. "day_high": v.DayHigh,
  67. "day_rumina": v.DayRumina,
  68. "day_intake": v.DayIntake,
  69. "day_inactive": v.DayInactive,
  70. "day_active": v.DayActive,
  71. }).Error; err != nil {
  72. continue
  73. }
  74. }
  75. return nil
  76. }
  77. // insertBarBehaviorDay 插入栏舍行为数据
  78. func (e *Entry) insertBarBehaviorDay(pastureId int64, targetDate string) error {
  79. penBehaviorList := make([]*model.PenBehaviorDay, 0)
  80. if err := e.DB.Model(new(model.MilkDaily)).
  81. Select(`heat_date, pasture_id, pen_id, COUNT(1) as cow_count, ROUND(AVG(d.day_yield), 1) as day_milk,
  82. ROUND(AVG(d.week_avg), 1) as week_milk,ROUND(AVG(d.day_high), 0) as day_high,
  83. ROUND(AVG(d.day_rumina), 0) as day_rumina,ROUND(AVG(d.day_intake), 0) as day_intake,
  84. ROUND(AVG(d.day_inactive), 0) as day_inactive,ROUND(AVG(d.day_gasp), 0) as day_gasp,
  85. ROUND(AVG(d.day_active), 0) as day_active,STD(d.day_rumina) as rumina_std`).
  86. Where("pasture_id = ?", pastureId).
  87. Where("heat_date = ?", targetDate).
  88. Where("day_high > ?", MaxDayHigh).
  89. Where("day_rumina > ?", MaxRumina).
  90. Group("pen_id").
  91. Having("cow_count >= ?", MaxCowCount).
  92. Order("pen_id").
  93. Find(&penBehaviorList).Error; err != nil {
  94. return xerr.WithStack(err)
  95. }
  96. if len(penBehaviorList) > 0 {
  97. if err := e.DB.Create(&penBehaviorList).Error; err != nil {
  98. return xerr.WithStack(err)
  99. }
  100. }
  101. return nil
  102. }