health_warning.go 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. package crontab
  2. import (
  3. "kpt-pasture/model"
  4. "kpt-pasture/util"
  5. "math"
  6. "time"
  7. pasturePb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/cow"
  8. "gitee.com/xuyiping_admin/pkg/xerr"
  9. "gitee.com/xuyiping_admin/pkg/logger/zaplog"
  10. "go.uber.org/zap"
  11. )
  12. func (e *Entry) HealthWarning(pastureId int64, processIds []int64) {
  13. newNeckActiveHabitList := make([]*model.NeckActiveHabit, 0)
  14. if err := e.DB.Model(new(model.NeckActiveHabit)).
  15. Where("pasture_id = ?", pastureId).
  16. Where("id IN (?)", processIds).
  17. Where("score BETWEEN ? AND ?", model.MinScore, model.MaxScore).
  18. Order("neck_ring_number,heat_date,frameid").
  19. Find(&newNeckActiveHabitList).Error; err != nil {
  20. zaplog.Error("HealthWarning", zap.Any("error", err), zap.Any("processIds", processIds))
  21. }
  22. var (
  23. lastCowID int64 = 0
  24. lastHeatDate string = "2001-01-01"
  25. lastScore int32 = 100
  26. healthWarningList []*model.NeckRingHealth
  27. )
  28. for _, habit := range newNeckActiveHabitList {
  29. if habit.ChangeFilter <= -99 {
  30. habit.ChangeFilter = 0
  31. }
  32. if habit.ChewFilter <= -99 {
  33. habit.ChewFilter = 0
  34. }
  35. if habit.SumMinHigh <= -99 {
  36. habit.SumMinHigh = 0
  37. }
  38. if habit.SumMaxHigh <= -99 {
  39. habit.SumMaxHigh = 0
  40. }
  41. if habit.SumMinChew <= -99 {
  42. habit.SumMinChew = 0
  43. }
  44. sumChew := habit.SumRumina + habit.SumIntake
  45. chew3dago := habit.BeforeThreeSumRumina + habit.BeforeThreeSumIntake
  46. isWorse := 1
  47. if habit.CowId == lastCowID && habit.HeatDate == lastHeatDate && habit.Score >= lastScore {
  48. isWorse = 0
  49. }
  50. lastCowID = habit.CowId
  51. lastHeatDate = habit.HeatDate
  52. lastScore = habit.Score
  53. if isWorse == 1 {
  54. newHealthWarning := model.NewNeckRingHealth(habit, sumChew, chew3dago)
  55. healthWarningList = append(healthWarningList, newHealthWarning)
  56. }
  57. }
  58. if len(healthWarningList) > 0 {
  59. e.updateNeckRingHealth(pastureId, healthWarningList)
  60. }
  61. }
  62. func (e *Entry) updateNeckRingHealth(pastureId int64, healthWarningList []*model.NeckRingHealth) {
  63. zaplog.Info("HealthWarning", zap.Any("healthWarningList", healthWarningList))
  64. for _, v := range healthWarningList {
  65. startAt := util.TimeParseLocalUnix(v.HeatDate)
  66. endAt := util.TimeParseLocalEndUnix(v.HeatDate)
  67. isMove := e.isEventCowLog(pastureId, v.CowId, startAt, endAt, pasturePb.EventType_Transfer_Ben)
  68. if isMove {
  69. v.IsTransferGroup = pasturePb.IsShow_Ok
  70. }
  71. isDryMilk := e.isEventCowLog(pastureId, v.CowId, startAt, endAt, pasturePb.EventType_Dry_Milk)
  72. if isDryMilk {
  73. v.IsDryMilk = pasturePb.IsShow_Ok
  74. }
  75. isImmunization := e.isEventCowLog(pastureId, v.CowId, startAt, endAt, pasturePb.EventType_Immunication)
  76. if isImmunization {
  77. v.IsImmunization = pasturePb.IsShow_Ok
  78. }
  79. }
  80. if err := e.DB.Model(new(model.NeckRingHealth)).Create(&healthWarningList).Error; err != nil {
  81. zaplog.Error("HealthWarning", zap.Any("error", err), zap.Any("healthWarningList", healthWarningList))
  82. }
  83. }
  84. func (e *Entry) isEventCowLog(pastureId int64, CowId int64, startAt, endAt int64, eventType pasturePb.EventType_Kind) bool {
  85. var count int64
  86. eventCowLog := &model.EventCowLog{CowId: CowId}
  87. if err := e.DB.Table(eventCowLog.TableName()).
  88. Where("pasture_id = ?", pastureId).
  89. Where("cow_id = ?", CowId).
  90. Where("event_at BETWEEN ? AND ?", startAt, endAt).
  91. Where("event_type = ?", eventType).
  92. Count(&count).Error; err != nil {
  93. return false
  94. }
  95. return count > 0
  96. }
  97. func (e *Entry) NeckRingHealthWarning() error {
  98. pastureList := e.FindPastureList()
  99. if pastureList == nil || len(pastureList) == 0 {
  100. return nil
  101. }
  102. for _, pasture := range pastureList {
  103. e.DB.Model(new(model.NeckRingHealthWarning)).
  104. Delete(new(model.NeckRingHealthWarning)).
  105. Where("pasture_id = ?", pasture.Id)
  106. if err := e.UpdateNeckRingHealth(pasture.Id); err != nil {
  107. zaplog.Error("NeckRingHealthWarning", zap.Any("UpdateNeckRingHealth", err), zap.Any("pasture", pasture))
  108. }
  109. }
  110. return nil
  111. }
  112. func (e *Entry) UpdateNeckRingHealth(pastureId int64) error {
  113. neckRingConfigureList, err := e.FindSystemNeckRingConfigure(pastureId)
  114. if err != nil {
  115. return xerr.WithStack(err)
  116. }
  117. healthValue := int32(0)
  118. for _, v := range neckRingConfigureList {
  119. if v.Name != model.HealthWarning {
  120. continue
  121. }
  122. healthValue = int32(v.Value)
  123. }
  124. newNeckRingHealthWarningList, err := e.FindNewNeckRingHealthWarning(pastureId, healthValue)
  125. if err != nil {
  126. return xerr.WithStack(err)
  127. }
  128. if len(newNeckRingHealthWarningList) > 0 {
  129. if err = e.DB.Model(new(model.NeckRingHealthWarning)).
  130. Create(&newNeckRingHealthWarningList).Error; err != nil {
  131. zaplog.Error("UpdateNeckRingHealth",
  132. zap.Any("error", err),
  133. zap.Any("newNeckRingHealthWarningList", newNeckRingHealthWarningList))
  134. }
  135. }
  136. return nil
  137. }
  138. func calculateNewScore(data *model.NeckRingHealth) int32 {
  139. otherScore := int32(0)
  140. otherScore += calculateMilkFilterScore(data.FilterMilk, data.MaxHigh)
  141. if data.IsTransferGroup == pasturePb.IsShow_Ok {
  142. otherScore += 3
  143. }
  144. if data.IsDryMilk == pasturePb.IsShow_Ok {
  145. otherScore += 5
  146. }
  147. if data.IsImmunization == pasturePb.IsShow_Ok {
  148. otherScore += 12
  149. }
  150. return data.Score + otherScore
  151. }
  152. func calculateMilkFilterScore(milkFilter int32, maxHigh int32) int32 {
  153. // 处理NULL值,默认为0
  154. milkFilterValue := int32(0)
  155. if milkFilter != 0 {
  156. milkFilterValue = milkFilter
  157. }
  158. // 计算系数:如果maxHigh>50则为0.5,否则为1
  159. coefficient := 1.0
  160. if maxHigh > 50 {
  161. coefficient = 0.5
  162. }
  163. // 计算中间值:milkFilterValue * 0.3 * coefficient
  164. intermediateValue := float64(milkFilterValue) * 0.3 * coefficient
  165. // 四舍五入
  166. roundedValue := math.Round(intermediateValue)
  167. // 取最小值(roundedValue和0中的较小值)
  168. result := int32(math.Min(roundedValue, 0))
  169. return result
  170. }
  171. func (e *Entry) FindNewNeckRingHealthWarning(pastureId int64, healthValue int32) ([]*model.NeckRingHealthWarning, error) {
  172. nowTime := time.Now().Local()
  173. //endTime := nowTime.Format(model.LayoutDate2)
  174. startTime := nowTime.AddDate(0, 0, -1).Format(model.LayoutDate2)
  175. neckRingHealthList := make([]*model.NeckRingHealth, 0)
  176. if err := e.DB.Model(new(model.NeckRingHealth)).
  177. Select(`MAX(id) AS id,heat_date,neck_ring_number,cow_id,score,max_high,created_at,min_high,min_chew,
  178. min_intake,sum_chew,before_three_sum_chew`).
  179. Where("pasture_id = ?", pastureId).
  180. Where("heat_date >= ?", startTime).
  181. Group("neck_ring_number").
  182. Find(&neckRingHealthList).Error; err != nil {
  183. return nil, xerr.WithStack(err)
  184. }
  185. newNeckRingHealthWarningList := make([]*model.NeckRingHealthWarning, 0)
  186. for _, v := range neckRingHealthList {
  187. cowInfo, err := e.GetCowById(pastureId, v.CowId)
  188. if err != nil {
  189. continue
  190. }
  191. if cowInfo == nil {
  192. continue
  193. }
  194. newScore := calculateNewScore(v)
  195. if newScore > healthValue {
  196. continue
  197. }
  198. if e.HistoryNeckRingHealthWarning(pastureId, cowInfo.NeckRingNumber, v.HeatDate) {
  199. continue
  200. }
  201. newNeckRingHealthWarning := model.NewNeckRingHealthWarning(pastureId, v, cowInfo, newScore)
  202. newNeckRingHealthWarningList = append(newNeckRingHealthWarningList, newNeckRingHealthWarning)
  203. }
  204. return newNeckRingHealthWarningList, nil
  205. }
  206. func (e *Entry) HistoryNeckRingHealthWarning(pastureId int64, neckRingNumber string, heatDate string) bool {
  207. var count int64
  208. if err := e.DB.Model(new(model.NeckRingHealthWarning)).
  209. Where("pasture_id = ?", pastureId).
  210. Where("neck_ring_number = ?", neckRingNumber).
  211. Where("heat_date = ?", heatDate).
  212. Where("is_show = ?", pasturePb.IsShow_No).
  213. Count(&count).Error; err != nil {
  214. return false
  215. }
  216. return count > 0
  217. }