sql.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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) GetSystemUserById(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) SystemDeptList(ctx context.Context) ([]*model.SystemDept, error) {
  53. deptList := make([]*model.SystemDept, 0)
  54. if err := s.DB.Where("is_delete = ?", pasturePb.IsShow_Ok).Find(&deptList).Error; err != nil {
  55. return nil, xerr.WithStack(err)
  56. }
  57. return deptList, nil
  58. }
  59. func (s *StoreEntry) GetPenById(ctx context.Context, penId int64) (*model.Pen, error) {
  60. penData := &model.Pen{
  61. Id: penId,
  62. }
  63. if err := s.DB.First(penData).Error; err != nil {
  64. if errors.Is(err, gorm.ErrRecordNotFound) {
  65. return nil, xerr.Customf("该栏舍数据不存在: %d", penId)
  66. }
  67. return nil, xerr.WithStack(err)
  68. }
  69. return penData, nil
  70. }
  71. func (s *StoreEntry) GetPenList(ctx context.Context) ([]*model.Pen, error) {
  72. penList := make([]*model.Pen, 0)
  73. if err := s.DB.Where("is_delete = ?", pasturePb.IsShow_Ok).Find(&penList).Error; err != nil {
  74. return nil, xerr.WithStack(err)
  75. }
  76. return penList, nil
  77. }
  78. func (s *StoreEntry) GetCowList(ctx context.Context) ([]*model.Cow, error) {
  79. cowList := make([]*model.Cow, 0)
  80. if err := s.DB.Where("is_remove = ?", pasturePb.IsShow_Ok).Find(&cowList).Error; err != nil {
  81. return nil, xerr.WithStack(err)
  82. }
  83. return cowList, nil
  84. }
  85. func (s *StoreEntry) GetCowInfoByCowId(ctx context.Context, cowId int64) (*model.Cow, error) {
  86. cowData := &model.Cow{Id: cowId}
  87. if err := s.DB.Where("is_remove = ?", pasturePb.IsShow_Ok).First(cowData).Error; err != nil {
  88. if errors.Is(err, gorm.ErrRecordNotFound) {
  89. return nil, xerr.Customf("该牛只数据不存在: %d", cowId)
  90. }
  91. return nil, xerr.WithStack(err)
  92. }
  93. return cowData, nil
  94. }
  95. func (s *StoreEntry) GetCowInfoByCowIds(ctx context.Context, cowIds []int64) ([]*model.Cow, error) {
  96. cowList := make([]*model.Cow, 0)
  97. if err := s.DB.Model(&model.Cow{}).Where("is_remove = ?", pasturePb.IsShow_Ok).Find(&cowList).Error; err != nil {
  98. return nil, xerr.WithStack(err)
  99. }
  100. return cowList, nil
  101. }
  102. func (s *StoreEntry) GetTransferReasonInfo(ctx context.Context, reasonId int64) (*model.ConfigTransferPenReason, error) {
  103. configTransferPenReasonData := &model.ConfigTransferPenReason{
  104. Id: reasonId,
  105. }
  106. if err := s.DB.First(configTransferPenReasonData).Error; err != nil {
  107. if errors.Is(err, gorm.ErrRecordNotFound) {
  108. return nil, xerr.Customf("该转群原因数据不存在: %d", reasonId)
  109. }
  110. return nil, xerr.WithStack(err)
  111. }
  112. return configTransferPenReasonData, nil
  113. }
  114. func (s *StoreEntry) GetCowWeightByLastSecond(ctx context.Context, cowId, lastWeightAt int64) (*model.EventWeight, error) {
  115. cowWeightData := &model.EventWeight{}
  116. if err := s.DB.Where("cow_id = ?", cowId).Where("weight_at < ?", lastWeightAt).First(cowWeightData).Error; err != nil {
  117. if errors.Is(err, gorm.ErrRecordNotFound) {
  118. return nil, xerr.Customf("该牛只体重数据不存在: %d", cowId)
  119. }
  120. return nil, xerr.WithStack(err)
  121. }
  122. return cowWeightData, nil
  123. }
  124. func (s *StoreEntry) DiseaseTypeList(ctx context.Context) ([]*model.ConfigDiseaseType, error) {
  125. diseaseTypeList := make([]*model.ConfigDiseaseType, 0)
  126. if err := s.DB.Where("is_show = ?", pasturePb.IsShow_Ok).Find(&diseaseTypeList).Error; err != nil {
  127. return nil, xerr.WithStack(err)
  128. }
  129. return diseaseTypeList, nil
  130. }
  131. func (s *StoreEntry) GetDrugsById(ctx context.Context, id int64) (*model.Drugs, error) {
  132. drugs := &model.Drugs{Id: id}
  133. if err := s.DB.First(drugs).Error; err != nil {
  134. if errors.Is(err, gorm.ErrRecordNotFound) {
  135. return nil, xerr.Customf("该药品数据不存在: %d", id)
  136. }
  137. return nil, xerr.WithStack(err)
  138. }
  139. return drugs, nil
  140. }
  141. func (s *StoreEntry) DiseaseListByIds(ctx context.Context, ids []int32) ([]*model.Disease, error) {
  142. diseaseList := make([]*model.Disease, 0)
  143. if err := s.DB.Where("id IN ?", ids).Find(&diseaseList).Error; err != nil {
  144. return nil, xerr.WithStack(err)
  145. }
  146. return diseaseList, nil
  147. }
  148. func (s *StoreEntry) GetImmunizationById(ctx context.Context, id int64) (*model.ImmunizationPlan, error) {
  149. immunizationPlan := &model.ImmunizationPlan{Id: id}
  150. if err := s.DB.First(immunizationPlan).Error; err != nil {
  151. if errors.Is(err, gorm.ErrRecordNotFound) {
  152. return nil, xerr.Customf("该免疫计划不存在: %d", id)
  153. }
  154. return nil, xerr.WithStack(err)
  155. }
  156. return immunizationPlan, nil
  157. }
  158. func (s *StoreEntry) GetWorkOrderSubByWorkOrderId(ctx context.Context, workOrderId int64) ([]*model.WorkOrderSub, error) {
  159. workOrderSubList := make([]*model.WorkOrderSub, 0)
  160. if err := s.DB.Where("work_order_id = ?", workOrderId).Find(&workOrderSubList).Error; err != nil {
  161. return nil, xerr.WithStack(err)
  162. }
  163. return workOrderSubList, nil
  164. }
  165. func (s *StoreEntry) GetSameTimeById(ctx context.Context, id int64) (*model.SameTime, error) {
  166. sameTime := &model.SameTime{Id: id}
  167. if err := s.DB.Where("is_show = ?", pasturePb.IsShow_Ok).First(sameTime).Error; err != nil {
  168. if errors.Is(err, gorm.ErrRecordNotFound) {
  169. }
  170. }
  171. return sameTime, nil
  172. }