sql.go 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. package backend
  2. import (
  3. "context"
  4. "errors"
  5. "kpt-pasture/model"
  6. operationPb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/operation"
  7. pasturePb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/cow"
  8. "gitee.com/xuyiping_admin/pkg/xerr"
  9. "gorm.io/gorm"
  10. )
  11. // GetCurrentUserName 获取当前用户名
  12. func (s *StoreEntry) GetCurrentUserName(ctx context.Context) (string, error) {
  13. userNameInter := ctx.Value(CurrentUserName)
  14. if userNameInter == nil {
  15. return "", xerr.Customf("cannot userName")
  16. }
  17. if userName, ok := userNameInter.(string); ok {
  18. return userName, nil
  19. } else {
  20. return "", xerr.Customf("waring userName")
  21. }
  22. }
  23. func (s *StoreEntry) GetCurrentSystemUser(ctx context.Context) (*model.SystemUser, error) {
  24. // 解析token
  25. userName, err := s.GetCurrentUserName(ctx)
  26. if err != nil {
  27. return nil, xerr.WithStack(err)
  28. }
  29. // 根据用户token获取用户数据
  30. systemUser := &model.SystemUser{Name: userName}
  31. if err = s.DB.Where("name = ?", userName).
  32. Where("is_show = ? and is_delete = ?", pasturePb.IsShow_Ok, pasturePb.IsShow_Ok).
  33. First(systemUser).Error; err != nil {
  34. if errors.Is(err, gorm.ErrRecordNotFound) {
  35. return nil, xerr.Custom("当前登录用户数据不存在")
  36. }
  37. return nil, xerr.WithStack(err)
  38. }
  39. return systemUser, nil
  40. }
  41. func (s *StoreEntry) GetSystemUserById(ctx context.Context, userId int64) (*model.SystemUser, error) {
  42. systemUser := &model.SystemUser{
  43. Id: userId,
  44. }
  45. if err := s.DB.First(systemUser).Error; err != nil {
  46. if errors.Is(err, gorm.ErrRecordNotFound) {
  47. return nil, xerr.Customf("该系统用户数据不存在: %d", userId)
  48. }
  49. return nil, xerr.WithStack(err)
  50. }
  51. return systemUser, nil
  52. }
  53. func (s *StoreEntry) SystemDeptList(ctx context.Context) ([]*model.SystemDept, error) {
  54. deptList := make([]*model.SystemDept, 0)
  55. if err := s.DB.Where("is_delete = ?", pasturePb.IsShow_Ok).Find(&deptList).Error; err != nil {
  56. return nil, xerr.WithStack(err)
  57. }
  58. return deptList, nil
  59. }
  60. func (s *StoreEntry) GetPenById(ctx context.Context, penId int64) (*model.Pen, error) {
  61. penData := &model.Pen{
  62. Id: penId,
  63. }
  64. if err := s.DB.First(penData).Error; err != nil {
  65. if errors.Is(err, gorm.ErrRecordNotFound) {
  66. return nil, xerr.Customf("该栏舍数据不存在: %d", penId)
  67. }
  68. return nil, xerr.WithStack(err)
  69. }
  70. return penData, nil
  71. }
  72. func (s *StoreEntry) GetPenList(ctx context.Context) ([]*model.Pen, error) {
  73. penList := make([]*model.Pen, 0)
  74. if err := s.DB.Where("is_delete = ?", pasturePb.IsShow_Ok).Find(&penList).Error; err != nil {
  75. return nil, xerr.WithStack(err)
  76. }
  77. return penList, nil
  78. }
  79. func (s *StoreEntry) PenMap(ctx context.Context) map[int32]*model.Pen {
  80. penList, _ := s.GetPenList(ctx)
  81. penMap := make(map[int32]*model.Pen)
  82. for _, v := range penList {
  83. penMap[int32(v.Id)] = v
  84. }
  85. return penMap
  86. }
  87. func (s *StoreEntry) GetCowList(ctx context.Context) ([]*model.Cow, error) {
  88. cowList := make([]*model.Cow, 0)
  89. if err := s.DB.Where("is_remove = ?", pasturePb.IsShow_Ok).Find(&cowList).Error; err != nil {
  90. return nil, xerr.WithStack(err)
  91. }
  92. return cowList, nil
  93. }
  94. func (s *StoreEntry) GetCowInfoByCowId(ctx context.Context, cowId int64) (*model.Cow, error) {
  95. cowData := &model.Cow{Id: cowId}
  96. if err := s.DB.Where("is_remove = ?", pasturePb.IsShow_Ok).First(cowData).Error; err != nil {
  97. if errors.Is(err, gorm.ErrRecordNotFound) {
  98. return nil, xerr.Customf("该牛只数据不存在: %d", cowId)
  99. }
  100. return nil, xerr.WithStack(err)
  101. }
  102. return cowData, nil
  103. }
  104. func (s *StoreEntry) GetCowInfoByCowIds(ctx context.Context, cowIds []int64) ([]*model.Cow, error) {
  105. cowList := make([]*model.Cow, 0)
  106. if err := s.DB.Model(&model.Cow{}).
  107. Where("is_remove = ?", pasturePb.IsShow_Ok).
  108. Where("id IN ?", cowIds).
  109. Find(&cowList).Error; err != nil {
  110. return nil, xerr.WithStack(err)
  111. }
  112. return cowList, nil
  113. }
  114. func (s *StoreEntry) GetTransferReasonInfo(ctx context.Context, reasonId int64) (*model.ConfigTransferPenReason, error) {
  115. configTransferPenReasonData := &model.ConfigTransferPenReason{
  116. Id: reasonId,
  117. }
  118. if err := s.DB.First(configTransferPenReasonData).Error; err != nil {
  119. if errors.Is(err, gorm.ErrRecordNotFound) {
  120. return nil, xerr.Customf("该转群原因数据不存在: %d", reasonId)
  121. }
  122. return nil, xerr.WithStack(err)
  123. }
  124. return configTransferPenReasonData, nil
  125. }
  126. func (s *StoreEntry) GetCowWeightByLastSecond(ctx context.Context, cowId, lastWeightAt int64) (*model.EventWeight, error) {
  127. cowWeightData := &model.EventWeight{}
  128. if err := s.DB.Where("cow_id = ?", cowId).Where("weight_at < ?", lastWeightAt).First(cowWeightData).Error; err != nil {
  129. if errors.Is(err, gorm.ErrRecordNotFound) {
  130. return nil, xerr.Customf("该牛只体重数据不存在: %d", cowId)
  131. }
  132. return nil, xerr.WithStack(err)
  133. }
  134. return cowWeightData, nil
  135. }
  136. func (s *StoreEntry) GetDrugsById(ctx context.Context, id int64) (*model.Drugs, error) {
  137. drugs := &model.Drugs{Id: id}
  138. if err := s.DB.First(drugs).Error; err != nil {
  139. if errors.Is(err, gorm.ErrRecordNotFound) {
  140. return nil, xerr.Customf("该药品数据不存在: %d", id)
  141. }
  142. return nil, xerr.WithStack(err)
  143. }
  144. return drugs, nil
  145. }
  146. func (s *StoreEntry) DiseaseTypeList(ctx context.Context) ([]*model.ConfigDiseaseType, error) {
  147. diseaseTypeList := make([]*model.ConfigDiseaseType, 0)
  148. if err := s.DB.Where("is_show = ?", pasturePb.IsShow_Ok).Find(&diseaseTypeList).Error; err != nil {
  149. return nil, xerr.WithStack(err)
  150. }
  151. return diseaseTypeList, nil
  152. }
  153. func (s *StoreEntry) GetDiseaseById(ctx context.Context, id int32) (*model.Disease, error) {
  154. newDisease := &model.Disease{}
  155. if err := s.DB.Where("id = ?", id).First(newDisease).Error; err != nil {
  156. return nil, xerr.WithStack(err)
  157. }
  158. return newDisease, nil
  159. }
  160. func (s *StoreEntry) DiseaseListByIds(ctx context.Context, ids []int32) ([]*model.Disease, error) {
  161. diseaseList := make([]*model.Disease, 0)
  162. if err := s.DB.Where("id IN ?", ids).Find(&diseaseList).Error; err != nil {
  163. return nil, xerr.WithStack(err)
  164. }
  165. return diseaseList, nil
  166. }
  167. func (s *StoreEntry) GetImmunizationById(ctx context.Context, id int64) (*model.ImmunizationPlan, error) {
  168. immunizationPlan := &model.ImmunizationPlan{Id: id}
  169. if err := s.DB.First(immunizationPlan).Error; err != nil {
  170. if errors.Is(err, gorm.ErrRecordNotFound) {
  171. return nil, xerr.Customf("该免疫计划不存在: %d", id)
  172. }
  173. return nil, xerr.WithStack(err)
  174. }
  175. return immunizationPlan, nil
  176. }
  177. func (s *StoreEntry) GetWorkOrderSubByWorkOrderId(ctx context.Context, workOrderId int64) ([]*model.WorkOrderSub, error) {
  178. workOrderSubList := make([]*model.WorkOrderSub, 0)
  179. if err := s.DB.Where("work_order_id = ?", workOrderId).Find(&workOrderSubList).Error; err != nil {
  180. return nil, xerr.WithStack(err)
  181. }
  182. return workOrderSubList, nil
  183. }
  184. func (s *StoreEntry) GetSameTimeById(ctx context.Context, id int64) (*model.SameTime, error) {
  185. sameTime := &model.SameTime{Id: id}
  186. if err := s.DB.Where("is_show = ?", pasturePb.IsShow_Ok).First(sameTime).Error; err != nil {
  187. return nil, xerr.WithStack(err)
  188. }
  189. return sameTime, nil
  190. }
  191. func (s *StoreEntry) SearchSystemDeptListByIds(ctx context.Context, ids []int64) ([]*model.SystemDept, error) {
  192. systemDeptList := make([]*model.SystemDept, 0)
  193. if err := s.DB.Model(new(model.SystemDept)).
  194. Where("is_show = ?", operationPb.IsShow_OK).
  195. Find(&systemDeptList, ids).Error; err != nil {
  196. return nil, xerr.WithStack(err)
  197. }
  198. return systemDeptList, nil
  199. }
  200. func (s *StoreEntry) GetSystemDeptListById(ctx context.Context, id int64) (*model.SystemDept, error) {
  201. systemDept := &model.SystemDept{Id: id}
  202. if err := s.DB.Model(new(model.SystemDept)).First(systemDept).Error; err != nil {
  203. return nil, xerr.WithStack(err)
  204. }
  205. return systemDept, nil
  206. }