sql.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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) PenMap(ctx context.Context) map[int32]*model.Pen {
  79. penList, _ := s.GetPenList(ctx)
  80. penMap := make(map[int32]*model.Pen)
  81. for _, v := range penList {
  82. penMap[int32(v.Id)] = v
  83. }
  84. return penMap
  85. }
  86. func (s *StoreEntry) GetCowList(ctx context.Context) ([]*model.Cow, error) {
  87. cowList := make([]*model.Cow, 0)
  88. if err := s.DB.Where("is_remove = ?", pasturePb.IsShow_Ok).Find(&cowList).Error; err != nil {
  89. return nil, xerr.WithStack(err)
  90. }
  91. return cowList, nil
  92. }
  93. func (s *StoreEntry) GetCowInfoByCowId(ctx context.Context, cowId int64) (*model.Cow, error) {
  94. cowData := &model.Cow{Id: cowId}
  95. if err := s.DB.Where("is_remove = ?", pasturePb.IsShow_Ok).First(cowData).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 cowData, nil
  102. }
  103. func (s *StoreEntry) GetCowInfoByCowIds(ctx context.Context, cowIds []int64) ([]*model.Cow, error) {
  104. cowList := make([]*model.Cow, 0)
  105. if err := s.DB.Model(&model.Cow{}).Where("is_remove = ?", pasturePb.IsShow_Ok).Find(&cowList).Error; err != nil {
  106. return nil, xerr.WithStack(err)
  107. }
  108. return cowList, nil
  109. }
  110. func (s *StoreEntry) GetTransferReasonInfo(ctx context.Context, reasonId int64) (*model.ConfigTransferPenReason, error) {
  111. configTransferPenReasonData := &model.ConfigTransferPenReason{
  112. Id: reasonId,
  113. }
  114. if err := s.DB.First(configTransferPenReasonData).Error; err != nil {
  115. if errors.Is(err, gorm.ErrRecordNotFound) {
  116. return nil, xerr.Customf("该转群原因数据不存在: %d", reasonId)
  117. }
  118. return nil, xerr.WithStack(err)
  119. }
  120. return configTransferPenReasonData, nil
  121. }
  122. func (s *StoreEntry) GetCowWeightByLastSecond(ctx context.Context, cowId, lastWeightAt int64) (*model.EventWeight, error) {
  123. cowWeightData := &model.EventWeight{}
  124. if err := s.DB.Where("cow_id = ?", cowId).Where("weight_at < ?", lastWeightAt).First(cowWeightData).Error; err != nil {
  125. if errors.Is(err, gorm.ErrRecordNotFound) {
  126. return nil, xerr.Customf("该牛只体重数据不存在: %d", cowId)
  127. }
  128. return nil, xerr.WithStack(err)
  129. }
  130. return cowWeightData, nil
  131. }
  132. func (s *StoreEntry) DiseaseTypeList(ctx context.Context) ([]*model.ConfigDiseaseType, error) {
  133. diseaseTypeList := make([]*model.ConfigDiseaseType, 0)
  134. if err := s.DB.Where("is_show = ?", pasturePb.IsShow_Ok).Find(&diseaseTypeList).Error; err != nil {
  135. return nil, xerr.WithStack(err)
  136. }
  137. return diseaseTypeList, nil
  138. }
  139. func (s *StoreEntry) GetDrugsById(ctx context.Context, id int64) (*model.Drugs, error) {
  140. drugs := &model.Drugs{Id: id}
  141. if err := s.DB.First(drugs).Error; err != nil {
  142. if errors.Is(err, gorm.ErrRecordNotFound) {
  143. return nil, xerr.Customf("该药品数据不存在: %d", id)
  144. }
  145. return nil, xerr.WithStack(err)
  146. }
  147. return drugs, nil
  148. }
  149. func (s *StoreEntry) DiseaseListByIds(ctx context.Context, ids []int32) ([]*model.Disease, error) {
  150. diseaseList := make([]*model.Disease, 0)
  151. if err := s.DB.Where("id IN ?", ids).Find(&diseaseList).Error; err != nil {
  152. return nil, xerr.WithStack(err)
  153. }
  154. return diseaseList, nil
  155. }
  156. func (s *StoreEntry) GetImmunizationById(ctx context.Context, id int64) (*model.ImmunizationPlan, error) {
  157. immunizationPlan := &model.ImmunizationPlan{Id: id}
  158. if err := s.DB.First(immunizationPlan).Error; err != nil {
  159. if errors.Is(err, gorm.ErrRecordNotFound) {
  160. return nil, xerr.Customf("该免疫计划不存在: %d", id)
  161. }
  162. return nil, xerr.WithStack(err)
  163. }
  164. return immunizationPlan, nil
  165. }
  166. func (s *StoreEntry) GetWorkOrderSubByWorkOrderId(ctx context.Context, workOrderId int64) ([]*model.WorkOrderSub, error) {
  167. workOrderSubList := make([]*model.WorkOrderSub, 0)
  168. if err := s.DB.Where("work_order_id = ?", workOrderId).Find(&workOrderSubList).Error; err != nil {
  169. return nil, xerr.WithStack(err)
  170. }
  171. return workOrderSubList, nil
  172. }
  173. func (s *StoreEntry) GetSameTimeById(ctx context.Context, id int64) (*model.SameTime, error) {
  174. sameTime := &model.SameTime{Id: id}
  175. if err := s.DB.Where("is_show = ?", pasturePb.IsShow_Ok).First(sameTime).Error; err != nil {
  176. if errors.Is(err, gorm.ErrRecordNotFound) {
  177. }
  178. }
  179. return sameTime, nil
  180. }