sql.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. package backend
  2. import (
  3. "context"
  4. "errors"
  5. "kpt-pasture/model"
  6. pasturePb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/cow"
  7. "gitee.com/xuyiping_admin/pkg/xerr"
  8. "gorm.io/gorm"
  9. )
  10. // GetCurrentUserName 获取当前用户名
  11. func (s *StoreEntry) GetCurrentUserName(ctx context.Context) (string, error) {
  12. userNameInter := ctx.Value(CurrentUserName)
  13. if userNameInter == nil {
  14. return "", xerr.Customf("cannot userName")
  15. }
  16. if userName, ok := userNameInter.(string); ok {
  17. return userName, nil
  18. } else {
  19. return "", xerr.Customf("waring userName")
  20. }
  21. }
  22. func (s *StoreEntry) GetCurrentSystemUser(ctx context.Context) (*model.SystemUser, error) {
  23. // 解析token
  24. userName, err := s.GetCurrentUserName(ctx)
  25. if err != nil {
  26. return nil, xerr.WithStack(err)
  27. }
  28. // 根据用户token获取用户数据
  29. systemUser := &model.SystemUser{Name: userName}
  30. if err = s.DB.Where("name = ?", userName).
  31. Where("is_show = ? and is_delete = ?", pasturePb.IsShow_Ok, pasturePb.IsShow_Ok).
  32. First(systemUser).Error; err != nil {
  33. if errors.Is(err, gorm.ErrRecordNotFound) {
  34. return nil, xerr.Custom("当前登录用户数据不存在")
  35. }
  36. return nil, xerr.WithStack(err)
  37. }
  38. return systemUser, nil
  39. }
  40. func (s *StoreEntry) GetSystemUserInfo(ctx context.Context, userId int64) (*model.SystemUser, error) {
  41. systemUser := &model.SystemUser{
  42. Id: userId,
  43. }
  44. if err := s.DB.First(systemUser).Error; err != nil {
  45. if errors.Is(err, gorm.ErrRecordNotFound) {
  46. return nil, xerr.Customf("该系统用户数据不存在: %d", userId)
  47. }
  48. return nil, xerr.WithStack(err)
  49. }
  50. return systemUser, nil
  51. }
  52. func (s *StoreEntry) GetPenInfo(ctx context.Context, penId int64) (*model.Pen, error) {
  53. penData := &model.Pen{
  54. Id: penId,
  55. }
  56. if err := s.DB.First(penData).Error; err != nil {
  57. if errors.Is(err, gorm.ErrRecordNotFound) {
  58. return nil, xerr.Customf("该栏舍数据不存在: %d", penId)
  59. }
  60. return nil, xerr.WithStack(err)
  61. }
  62. return penData, nil
  63. }
  64. func (s *StoreEntry) GetPenList(ctx context.Context) ([]*model.Pen, error) {
  65. penList := make([]*model.Pen, 0)
  66. if err := s.DB.Where("is_delete = ?", pasturePb.IsShow_Ok).Find(&penList).Error; err != nil {
  67. return nil, xerr.WithStack(err)
  68. }
  69. return penList, nil
  70. }
  71. func (s *StoreEntry) GetCowInfo(ctx context.Context, cowId int64) (*model.Cow, error) {
  72. cowData := &model.Cow{Id: cowId}
  73. if err := s.DB.First(cowData).Error; err != nil {
  74. if errors.Is(err, gorm.ErrRecordNotFound) {
  75. return nil, xerr.Customf("该牛只数据不存在: %d", cowId)
  76. }
  77. return nil, xerr.WithStack(err)
  78. }
  79. return cowData, nil
  80. }
  81. func (s *StoreEntry) GetTransferReasonInfo(ctx context.Context, reasonId int64) (*model.ConfigTransferPenReason, error) {
  82. configTransferPenReasonData := &model.ConfigTransferPenReason{
  83. Id: reasonId,
  84. }
  85. if err := s.DB.First(configTransferPenReasonData).Error; err != nil {
  86. if errors.Is(err, gorm.ErrRecordNotFound) {
  87. return nil, xerr.Customf("该转群原因数据不存在: %d", reasonId)
  88. }
  89. return nil, xerr.WithStack(err)
  90. }
  91. return configTransferPenReasonData, nil
  92. }
  93. func (s *StoreEntry) GetCowWeightByLastSecond(ctx context.Context, cowId, lastWeightAt int64) (*model.EventWeight, error) {
  94. cowWeightData := &model.EventWeight{}
  95. if err := s.DB.Where("cow_id = ?", cowId).Where("weight_at < ?", lastWeightAt).First(cowWeightData).Error; err != nil {
  96. if errors.Is(err, gorm.ErrRecordNotFound) {
  97. return nil, xerr.Customf("该牛只体重数据不存在: %d", cowId)
  98. }
  99. return nil, xerr.WithStack(err)
  100. }
  101. return cowWeightData, nil
  102. }
  103. func (s *StoreEntry) DiseaseTypeList(ctx context.Context) ([]*model.ConfigDiseaseType, error) {
  104. diseaseTypeList := make([]*model.ConfigDiseaseType, 0)
  105. if err := s.DB.Where("is_show = ?", pasturePb.IsShow_Ok).Find(&diseaseTypeList).Error; err != nil {
  106. return nil, xerr.WithStack(err)
  107. }
  108. return diseaseTypeList, nil
  109. }
  110. func (s *StoreEntry) DrugsById(ctx context.Context, id int64) (*model.Drugs, error) {
  111. drugs := &model.Drugs{Id: id}
  112. if err := s.DB.First(drugs).Error; err != nil {
  113. if errors.Is(err, gorm.ErrRecordNotFound) {
  114. return nil, xerr.Customf("该药品数据不存在: %d", id)
  115. }
  116. return nil, xerr.WithStack(err)
  117. }
  118. return drugs, nil
  119. }
  120. func (s *StoreEntry) DiseaseListByIds(ctx context.Context, ids []int32) ([]*model.Disease, error) {
  121. diseaseList := make([]*model.Disease, 0)
  122. if err := s.DB.Where("id IN ?", ids).Find(&diseaseList).Error; err != nil {
  123. return nil, xerr.WithStack(err)
  124. }
  125. return diseaseList, nil
  126. }
  127. func (s *StoreEntry) ImmunizationById(ctx context.Context, id int64) (*model.ImmunizationPlan, error) {
  128. immunizationPlan := &model.ImmunizationPlan{Id: id}
  129. if err := s.DB.First(immunizationPlan).Error; err != nil {
  130. if errors.Is(err, gorm.ErrRecordNotFound) {
  131. return nil, xerr.Customf("该免疫计划不存在: %d", id)
  132. }
  133. return nil, xerr.WithStack(err)
  134. }
  135. return immunizationPlan, nil
  136. }