sql.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. package crontab
  2. import (
  3. "kpt-pasture/model"
  4. "go.uber.org/zap"
  5. "gitee.com/xuyiping_admin/pkg/logger/zaplog"
  6. pasturePb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/cow"
  7. "gitee.com/xuyiping_admin/pkg/xerr"
  8. )
  9. func (e *Entry) FindPastureList() []*model.AppPastureList {
  10. res := make([]*model.AppPastureList, 0)
  11. if err := e.DB.Model(new(model.AppPastureList)).
  12. Where("is_show = ?", pasturePb.IsShow_Ok).
  13. Find(&res).Error; err != nil {
  14. zaplog.Error("FindPastureList error", zap.Any("err", err))
  15. return res
  16. }
  17. return res
  18. }
  19. func (e *Entry) GetCowById(cowId int64) (*model.Cow, error) {
  20. cowInfo := &model.Cow{}
  21. if err := e.DB.Model(new(model.Cow)).
  22. Where("id = ?", cowId).
  23. Where("admission_status = ?", pasturePb.AdmissionStatus_Admission).
  24. First(cowInfo).Error; err != nil {
  25. return nil, err
  26. }
  27. return cowInfo, nil
  28. }
  29. func (e *Entry) GetPenMapList() (map[int32]*model.Pen, error) {
  30. penList := make([]*model.Pen, 0)
  31. if err := e.DB.Where("is_delete = ?", pasturePb.IsShow_Ok).Find(&penList).Error; err != nil {
  32. return nil, xerr.WithStack(err)
  33. }
  34. penMap := make(map[int32]*model.Pen)
  35. for _, v := range penList {
  36. penMap[v.Id] = v
  37. }
  38. return penMap, nil
  39. }
  40. func (e *Entry) GetSystemConfigure(pastureId int64, name string) *model.SystemConfigure {
  41. res := &model.SystemConfigure{}
  42. if err := e.DB.Model(new(model.SystemConfigure)).
  43. Where("name = ?", name).
  44. Where("pasture_id = ?", pastureId).
  45. Where("is_show = ?", pasturePb.IsShow_Ok).
  46. First(res).Error; err != nil {
  47. return nil
  48. }
  49. return res
  50. }
  51. // GetBeforeThreeDaysCowEstrus 获取值得时间之前三天内最大发情记录
  52. func (e *Entry) GetBeforeThreeDaysCowEstrus(cowId int64, activeTime string) *model.EventEstrus {
  53. eventEstrus := &model.EventEstrus{}
  54. if err := e.DB.Model(new(model.EventEstrus)).
  55. Select("MAX(max_high) as max_high,cow_id,MAX(day_high) as day_high").
  56. Select("MAX(IF(result=1,3,result)) AS result").
  57. Where("cow_id = ?", cowId).
  58. Where("active_date >= ?", activeTime).
  59. First(eventEstrus).Error; err != nil {
  60. return eventEstrus
  61. }
  62. return eventEstrus
  63. }
  64. // GetTwoEstrus 判断最近50天内是否存在发情记录(发情等级>=2),如果18~25天@xadjust21,如果36~50天@xadjust42
  65. func (e *Entry) GetTwoEstrus(pastureId, cowId int64, startActiveTime, endActiveTime string) *CowEstrus {
  66. newCowEstrus := &CowEstrus{}
  67. if err := e.DB.Model(new(model.EventEstrus)).
  68. Select("cow_id,MAX(active_date) as active_date").
  69. Where("cow_id = ?", cowId).
  70. Where("pasture_id = ?", pastureId).
  71. Where("active_date BETWEEN ? AND ?", startActiveTime, endActiveTime).
  72. Where("level >= ?", pasturePb.EstrusLevel_Middle).
  73. First(newCowEstrus).Error; err != nil {
  74. return newCowEstrus
  75. }
  76. return newCowEstrus
  77. }
  78. func (e *Entry) FindCowInfoByNeckRingNumber(neckRingNumber string) *model.Cow {
  79. res := &model.Cow{}
  80. if err := e.DB.Model(new(model.Cow)).
  81. Where("neck_ring_number = ?", neckRingNumber).
  82. Where("admission_status = ?", pasturePb.AdmissionStatus_Admission).
  83. First(res).Error; err != nil {
  84. return nil
  85. }
  86. return res
  87. }
  88. func (e *Entry) IsExistEventEstrus(pastureId, cowId int64) *model.EventEstrus {
  89. res := &model.EventEstrus{}
  90. if err := e.DB.Model(new(model.EventEstrus)).
  91. Where("cow_id = ?", cowId).
  92. Where("pasture_id = ?", pastureId).
  93. Where("expose_estrus_type = ?", pasturePb.ExposeEstrusType_Neck_Ring).
  94. Where("is_show = ?", pasturePb.IsShow_Ok).
  95. Order("id DESC").
  96. First(res).Error; err != nil {
  97. return nil
  98. }
  99. return res
  100. }