sql.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. package mqtt
  2. import (
  3. "errors"
  4. "kpt-pasture/model"
  5. "gitee.com/xuyiping_admin/pkg/logger/zaplog"
  6. "go.uber.org/zap"
  7. "gitee.com/xuyiping_admin/pkg/xerr"
  8. "gorm.io/gorm"
  9. pasturePb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/cow"
  10. )
  11. // NeckRingIsBind 脖环是否绑定
  12. func (e *Entry) NeckRingIsBind(number string) bool {
  13. var count int64 = 0
  14. if err := e.DB.Model(new(model.NeckRing)).Where("number = ?", number).
  15. Where("status != ?", pasturePb.NeckRingStatus_Unbind).
  16. Count(&count).Error; err != nil {
  17. return false
  18. }
  19. if count > 0 {
  20. return true
  21. }
  22. return false
  23. }
  24. func (e *Entry) GetCowInfoByNeckRingNumber(pastureId int64, neckRingNumber string) *model.Cow {
  25. res := &model.Cow{}
  26. if err := e.DB.Model(new(model.Cow)).
  27. Where("pasture_id = ?", pastureId).
  28. Where("neck_ring_number = ?", neckRingNumber).
  29. Where("admission_status = ?", pasturePb.AdmissionStatus_Admission).
  30. First(res).Error; err != nil {
  31. return nil
  32. }
  33. return res
  34. }
  35. func (e *Entry) GetSystemConfigure(name string) *model.SystemConfigure {
  36. res := &model.SystemConfigure{}
  37. if err := e.DB.Model(new(model.SystemConfigure)).
  38. Where("name = ?", name).
  39. Where("is_show = ?", pasturePb.IsShow_Ok).
  40. First(res).Error; err != nil {
  41. return nil
  42. }
  43. return res
  44. }
  45. func (e *Entry) IsExistNeckActiveHabit(neckRingNumber, heatDate string, frameId int32) (*model.NeckActiveHabit, int64) {
  46. count := int64(0)
  47. neckRingProcess := &model.NeckRingProcess{}
  48. if err := e.DB.Model(new(model.NeckRingProcess)).
  49. Where("neck_ring_number = ?", neckRingNumber).
  50. Where("active_date = ?", heatDate).
  51. Where("frameid = ?", frameId).
  52. Count(&count).First(neckRingProcess).Error; err != nil {
  53. return nil, 0
  54. }
  55. res := &model.NeckActiveHabit{}
  56. if neckRingProcess != nil && neckRingProcess.HabitId > 0 {
  57. if err := e.DB.Model(new(model.NeckActiveHabit)).
  58. Where("id = ?", neckRingProcess.HabitId).
  59. First(res).Error; err != nil {
  60. return nil, 0
  61. }
  62. }
  63. return res, count
  64. }
  65. // GetMinIdByHeatDate 获取最小的id
  66. func (e *Entry) GetMinIdByHeatDate(heatDate string, defaultId int64) (int64, error) {
  67. xMinId := struct {
  68. Id int64
  69. }{}
  70. if err := e.DB.Model(new(model.NeckActiveHabit)).
  71. Select("MIN(id) as id").
  72. Where("heat_date = ?", heatDate).
  73. First(&xMinId).Error; err != nil {
  74. if errors.Is(err, gorm.ErrRecordNotFound) {
  75. xMinId.Id = defaultId
  76. } else {
  77. return 0, xerr.WithStack(err)
  78. }
  79. }
  80. return xMinId.Id, nil
  81. }
  82. func (e *Entry) FindPastureList() []*model.AppPastureList {
  83. res := make([]*model.AppPastureList, 0)
  84. if err := e.DB.Model(new(model.AppPastureList)).
  85. Where("is_show = ?", pasturePb.IsShow_Ok).
  86. Find(&res).Error; err != nil {
  87. zaplog.Error("FindPastureList error", zap.Any("err", err))
  88. return res
  89. }
  90. return res
  91. }
  92. func (e *Entry) GetSystemConfigure2(pastureId int64, name string) (*model.SystemConfigure, error) {
  93. res := &model.SystemConfigure{}
  94. if err := e.DB.Model(new(model.SystemConfigure)).
  95. Where("name = ?", name).
  96. Where("pasture_id = ?", pastureId).
  97. Where("is_show = ?", pasturePb.IsShow_Ok).
  98. First(res).Error; err != nil {
  99. return nil, xerr.WithStack(err)
  100. }
  101. return res, nil
  102. }