sql.go 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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) GetCowInfo(ctx context.Context, cowId int64) (*model.Cow, error) {
  65. cowData := &model.Cow{Id: cowId}
  66. if err := s.DB.First(cowData).Error; err != nil {
  67. if errors.Is(err, gorm.ErrRecordNotFound) {
  68. return nil, xerr.Customf("该牛只数据不存在: %d", cowId)
  69. }
  70. return nil, xerr.WithStack(err)
  71. }
  72. return cowData, nil
  73. }
  74. func (s *StoreEntry) GetTransferReasonInfo(ctx context.Context, reasonId int64) (*model.ConfigTransferPenReason, error) {
  75. configTransferPenReasonData := &model.ConfigTransferPenReason{
  76. Id: reasonId,
  77. }
  78. if err := s.DB.First(configTransferPenReasonData).Error; err != nil {
  79. if errors.Is(err, gorm.ErrRecordNotFound) {
  80. return nil, xerr.Customf("该转群原因数据不存在: %d", reasonId)
  81. }
  82. return nil, xerr.WithStack(err)
  83. }
  84. return configTransferPenReasonData, nil
  85. }