a_dataStore.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. package dao
  2. import (
  3. "Cold_Logistic/internal/pkg/common/constant"
  4. "Cold_Logistic/internal/pkg/common/global"
  5. "context"
  6. "gorm.io/gorm"
  7. "gorm.io/gorm/clause"
  8. "Cold_Logistic/internal/server/infra/models"
  9. )
  10. type DataStore struct {
  11. db *gorm.DB
  12. }
  13. type dbOption func(*gorm.DB) *gorm.DB
  14. func NewDataStore(db *gorm.DB) *DataStore {
  15. return &DataStore{db: db}
  16. }
  17. type contextTxKey struct{}
  18. func (ds *DataStore) InTx(ctx context.Context, fn func(ctx context.Context) error) error {
  19. return ds.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
  20. ctx = context.WithValue(ctx, contextTxKey{}, tx)
  21. return fn(ctx)
  22. })
  23. }
  24. func (ds *DataStore) GetDB(ctx context.Context) *gorm.DB {
  25. tx, ok := ctx.Value(contextTxKey{}).(*gorm.DB)
  26. if ok {
  27. return tx
  28. }
  29. return ds.db
  30. }
  31. func (ds *DataStore) withByID(id int) dbOption {
  32. return func(db *gorm.DB) *gorm.DB {
  33. return db.Where("id = ?", id)
  34. }
  35. }
  36. func (ds *DataStore) withByColumn(column string, val interface{}) dbOption {
  37. return func(db *gorm.DB) *gorm.DB {
  38. return db.Where(column+" = ?", val)
  39. }
  40. }
  41. func (ds *DataStore) withByColumnInVal(column string, val interface{}) dbOption {
  42. return func(db *gorm.DB) *gorm.DB {
  43. return db.Where(column+" in (?)", val)
  44. }
  45. }
  46. func (ds *DataStore) withBySelects(filed ...string) dbOption {
  47. return func(db *gorm.DB) *gorm.DB {
  48. return db.Select(filed)
  49. }
  50. }
  51. func (ds *DataStore) withByNotDeleted() dbOption {
  52. return func(db *gorm.DB) *gorm.DB {
  53. return db.Where("deleted = ?", models.DeleteNo)
  54. }
  55. }
  56. func (ds *DataStore) withForUpdate() dbOption {
  57. return func(db *gorm.DB) *gorm.DB {
  58. return db.Clauses(clause.Locking{Strength: "UPDATE"})
  59. }
  60. }
  61. func (ds *DataStore) optionDB(ctx context.Context, opts ...dbOption) *gorm.DB {
  62. db := ds.GetDB(ctx).WithContext(ctx)
  63. // db := ds.db.WithContext(ctx)
  64. for _, opt := range opts {
  65. db = opt(db)
  66. }
  67. return db
  68. }
  69. func (ds *DataStore) withAccountDataPermis(ctx context.Context, column string) dbOption {
  70. tokenInfo := global.GetTokenInfoFromContext(ctx)
  71. if tokenInfo.AccountType == constant.AccountApplet {
  72. return func(db *gorm.DB) *gorm.DB {
  73. return db.Where(column+" = ?", tokenInfo.AccountId)
  74. }
  75. }
  76. // 默认
  77. return func(db *gorm.DB) *gorm.DB {
  78. return db
  79. }
  80. }
  81. // 数据权限
  82. func (ds *DataStore) withCompanyDataPermis(ctx context.Context, column string) dbOption {
  83. tokenInfo := global.GetTokenInfoFromContext(ctx)
  84. if tokenInfo.AccountType == constant.AccountPlatform {
  85. return func(db *gorm.DB) *gorm.DB {
  86. return db.Where(column+" = ?", tokenInfo.UsePid)
  87. }
  88. }
  89. // 默认
  90. return func(db *gorm.DB) *gorm.DB {
  91. return db
  92. }
  93. }