sql.go 3.6 KB

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