account.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. package dao
  2. import (
  3. "Cold_Logistic/internal/pkg/common/global"
  4. "Cold_Logistic/internal/server/infra/models"
  5. "context"
  6. "gogs.baozhida.cn/Cold_Logistic_libs/pkg/contrib/errors"
  7. "time"
  8. )
  9. // AccountStore 小程序账号表
  10. type AccountStore interface {
  11. DbBaseStore
  12. BatchSave(ctx context.Context, values []*models.Account, omit ...string) error
  13. GetMapByIds(ctx context.Context, ids []int, omit ...string) (map[int]models.Account, error)
  14. FindByOpenid(ctx context.Context, openid string, accType int) (models.Account, error)
  15. FindByUuid(ctx context.Context, uuid string) (models.Account, error)
  16. FindByUuids(ctx context.Context, uuid []string) (map[string]models.Account, error)
  17. FindTokenKeyById(ctx context.Context, id int) (string, error)
  18. }
  19. var _ AccountStore = &account{}
  20. type account struct {
  21. dbBase
  22. }
  23. func newAccount(ds *DataStore) *account {
  24. return &account{dbBase: dbBase{
  25. store: ds,
  26. baseEntity: &models.Account{},
  27. }}
  28. }
  29. func (ds *DataStore) Account() AccountStore {
  30. return newAccount(ds)
  31. }
  32. func (a *account) BatchSave(ctx context.Context, values []*models.Account, omit ...string) error {
  33. if len(values) == 0 {
  34. return nil
  35. }
  36. db := a.store.optionDB(ctx)
  37. tokenInfo := global.GetTokenInfoFromContext(ctx)
  38. for i := range values {
  39. if values[i].Id == 0 {
  40. values[i].CreatedBy = tokenInfo.AccountId
  41. values[i].CreatedTime.Time = time.Now()
  42. values[i].Deleted = models.DeleteNo
  43. } else {
  44. values[i].UpdatedBy = tokenInfo.AccountId
  45. values[i].UpdatedTime.Time = time.Now()
  46. }
  47. }
  48. return db.Table(a.baseEntity.TableName()).Omit(omit...).Save(&values).Error
  49. }
  50. func (a *account) GetMapByIds(ctx context.Context, ids []int, omit ...string) (map[int]models.Account, error) {
  51. res := make(map[int]models.Account, len(ids))
  52. if len(ids) == 0 {
  53. return res, nil
  54. }
  55. var list []models.Account
  56. db := a.store.optionDB(ctx)
  57. err := db.Table(a.baseEntity.TableName()).
  58. Where("deleted = ?", models.DeleteNo).
  59. Where("id IN (?)", ids).
  60. Omit(omit...).
  61. Find(&list).Error
  62. if err != nil {
  63. return res, err
  64. }
  65. for i := range list {
  66. res[list[i].Id] = list[i]
  67. }
  68. return res, nil
  69. }
  70. func (a *account) FindByOpenid(ctx context.Context, openid string, accType int) (models.Account, error) {
  71. db := a.store.optionDB(ctx)
  72. ent := models.Account{}
  73. err := db.Model(a.baseEntity).Where("deleted = ?", models.DeleteNo).
  74. Where("openid = ? AND account_type = ?", openid, accType).
  75. Find(&ent).Error
  76. if err != nil {
  77. return ent, errors.WithStackOnce(err)
  78. }
  79. return ent, nil
  80. }
  81. func (a *account) FindByUuid(ctx context.Context, uuid string) (models.Account, error) {
  82. db := a.store.optionDB(ctx)
  83. ent := models.Account{}
  84. err := db.Model(a.baseEntity).Where("deleted = ?", models.DeleteNo).
  85. Where("uuid = ?", uuid).
  86. Find(&ent).Limit(1).Error
  87. if err != nil {
  88. return ent, errors.WithStackOnce(err)
  89. }
  90. return ent, nil
  91. }
  92. func (a *account) FindByUuids(ctx context.Context, uuid []string) (map[string]models.Account, error) {
  93. db := a.store.optionDB(ctx)
  94. ent := make([]models.Account, 0, len(uuid))
  95. err := db.Model(a.baseEntity).Where("deleted = ?", models.DeleteNo).
  96. Where("uuid IN(?)", uuid).
  97. Find(&ent).Error
  98. if err != nil {
  99. return nil, errors.WithStackOnce(err)
  100. }
  101. ret := make(map[string]models.Account, len(ent))
  102. for _, v := range ent {
  103. ret[v.Uuid] = v
  104. }
  105. return ret, nil
  106. }
  107. func (a *account) FindTokenKeyById(ctx context.Context, id int) (string, error) {
  108. db := a.store.optionDB(ctx)
  109. var key string
  110. err := db.Model(a.baseEntity).Where("deleted = ?", models.DeleteNo).
  111. Where("id = ?", id).
  112. Pluck("tokey", &key).Error
  113. return key, errors.WithStackOnce(err)
  114. }