base.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. package base
  2. import (
  3. "context"
  4. "fmt"
  5. "time"
  6. "gorm.io/gorm"
  7. )
  8. var globalIsRelated bool = true // 全局预加载
  9. type BaseMgr struct {
  10. *gorm.DB
  11. Ctx context.Context
  12. cancel context.CancelFunc
  13. timeout time.Duration
  14. isRelated bool
  15. }
  16. func (obj *BaseMgr) SetTimeOut(timeout time.Duration) {
  17. obj.Ctx, obj.cancel = context.WithTimeout(context.Background(), timeout)
  18. obj.timeout = timeout
  19. }
  20. func (obj *BaseMgr) SetCtx(c context.Context) {
  21. if c != nil {
  22. obj.Ctx = c
  23. }
  24. }
  25. func (obj *BaseMgr) GetCtx() context.Context {
  26. return obj.Ctx
  27. }
  28. func (obj *BaseMgr) Cancel(c context.Context) {
  29. obj.cancel()
  30. }
  31. func (obj *BaseMgr) GetDB() *gorm.DB {
  32. return obj.DB
  33. }
  34. func (obj *BaseMgr) UpdateDB(db *gorm.DB) {
  35. obj.DB = db
  36. }
  37. func (obj *BaseMgr) GetIsRelated() bool {
  38. return obj.isRelated
  39. }
  40. func (obj *BaseMgr) SetIsRelated(b bool) {
  41. obj.isRelated = b
  42. }
  43. func (obj *BaseMgr) New() {
  44. obj.DB = obj.NewDB()
  45. }
  46. func (obj *BaseMgr) NewDB() *gorm.DB {
  47. return obj.DB.Session(&gorm.Session{NewDB: true, Context: obj.Ctx})
  48. }
  49. type Options struct {
  50. Query map[string]interface{}
  51. }
  52. type Option interface {
  53. Apply(*Options)
  54. }
  55. type OptionFunc func(*Options)
  56. func (f OptionFunc) apply(o *Options) {
  57. f(o)
  58. }
  59. func OpenRelated() {
  60. globalIsRelated = true
  61. }
  62. func CloseRelated() {
  63. globalIsRelated = true
  64. }
  65. type Condition struct {
  66. list []*conditionInfo
  67. }
  68. func (c *Condition) AndWithCondition(condition bool, column string, cases string, value interface{}) *Condition {
  69. if condition {
  70. c.list = append(c.list, &conditionInfo{
  71. andor: "and",
  72. column: column, // 列名
  73. case_: cases, // 条件(and,or,in,>=,<=)
  74. value: value,
  75. })
  76. }
  77. return c
  78. }
  79. func (c *Condition) And(column string, cases string, value interface{}) *Condition {
  80. return c.AndWithCondition(true, column, cases, value)
  81. }
  82. func (c *Condition) OrWithCondition(condition bool, column string, cases string, value interface{}) *Condition {
  83. if condition {
  84. c.list = append(c.list, &conditionInfo{
  85. andor: "or",
  86. column: column, // 列名
  87. case_: cases, // 条件(and,or,in,>=,<=)
  88. value: value,
  89. })
  90. }
  91. return c
  92. }
  93. func (c *Condition) Or(column string, cases string, value interface{}) *Condition {
  94. return c.OrWithCondition(true, column, cases, value)
  95. }
  96. func (c *Condition) Get() (where string, out []interface{}) {
  97. firstAnd := -1
  98. for i := 0; i < len(c.list); i++ { // 查找第一个and
  99. if c.list[i].andor == "and" {
  100. where = fmt.Sprintf("`%v` %v ?", c.list[i].column, c.list[i].case_)
  101. out = append(out, c.list[i].value)
  102. firstAnd = i
  103. break
  104. }
  105. }
  106. if firstAnd < 0 && len(c.list) > 0 { // 补刀
  107. where = fmt.Sprintf("`%v` %v ?", c.list[0].column, c.list[0].case_)
  108. out = append(out, c.list[0].value)
  109. firstAnd = 0
  110. }
  111. for i := 0; i < len(c.list); i++ { // 添加剩余的
  112. if firstAnd != i {
  113. where += fmt.Sprintf(" %v `%v` %v ?", c.list[i].andor, c.list[i].column, c.list[i].case_)
  114. out = append(out, c.list[i].value)
  115. }
  116. }
  117. return
  118. }
  119. type conditionInfo struct {
  120. andor string
  121. column string // 列名
  122. case_ string // 条件(in,>=,<=)
  123. value interface{}
  124. }