search.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. package dto
  2. import (
  3. "cold-delivery/common/global"
  4. "fmt"
  5. "gogs.baozhida.cn/zoie/OAuth-core/tools/search"
  6. "gorm.io/gorm"
  7. "strings"
  8. )
  9. type GeneralDelDto struct {
  10. Id int `uri:"id" json:"id" validate:"required"`
  11. Ids []int `json:"ids"`
  12. }
  13. func (g GeneralDelDto) GetIds() []int {
  14. ids := make([]int, 0)
  15. if g.Id != 0 {
  16. ids = append(ids, g.Id)
  17. }
  18. if len(g.Ids) > 0 {
  19. for _, id := range g.Ids {
  20. if id > 0 {
  21. ids = append(ids, id)
  22. }
  23. }
  24. } else {
  25. if g.Id > 0 {
  26. ids = append(ids, g.Id)
  27. }
  28. }
  29. if len(ids) <= 0 {
  30. //方式全部删除
  31. ids = append(ids, 0)
  32. }
  33. return ids
  34. }
  35. type GeneralGetDto struct {
  36. Id int `uri:"id" json:"id" validate:"required"`
  37. }
  38. func MakeCondition(q interface{}) func(db *gorm.DB) *gorm.DB {
  39. return func(db *gorm.DB) *gorm.DB {
  40. condition := &search.GormCondition{
  41. GormPublic: search.GormPublic{},
  42. Join: make([]*search.GormJoin, 0),
  43. }
  44. search.ResolveSearchQuery(global.Driver, q, condition)
  45. for _, join := range condition.Join {
  46. if join == nil {
  47. continue
  48. }
  49. db = db.Joins(join.JoinOn)
  50. for k, v := range join.Where {
  51. db = db.Where(k, v...)
  52. }
  53. for k, v := range join.Or {
  54. db = db.Or(k, v...)
  55. }
  56. for _, o := range join.Order {
  57. db = db.Order(o)
  58. }
  59. }
  60. for k, v := range condition.Where {
  61. db = db.Where(k, v...)
  62. }
  63. for k, v := range condition.Or {
  64. db = db.Or(k, v...)
  65. }
  66. var orContains string
  67. for k, v := range condition.OrContains {
  68. orContains += fmt.Sprintf(" OR %v '%v'", k, v[0])
  69. }
  70. if len(orContains) > 0 {
  71. db = db.Where(strings.TrimLeft(orContains, " OR"))
  72. }
  73. for _, o := range condition.Order {
  74. db = db.Order(o)
  75. }
  76. return db
  77. }
  78. }
  79. //func MakeCondition(q interface{}) func(db *gorm.DB) *gorm.DB {
  80. // return func(db *gorm.DB) *gorm.DB {
  81. // // 检查是否是WaybillGetPageReq类型
  82. // req, ok := q.(*dto2.WaybillGetPageReq)
  83. // if !ok {
  84. // // 如果不是WaybillGetPageReq类型,则继续正常的处理逻辑
  85. // return makeNormalCondition(q)(db)
  86. // }
  87. //
  88. // // 特别处理Status字段
  89. // if req.Status == 1 {
  90. // // 添加一个额外的条件来匹配status=7
  91. // db = db.Or("status = ?", model.WaybillStatusoverrule)
  92. // }
  93. //
  94. // // 继续使用search.ResolveSearchQuery来处理其他条件
  95. // condition := &search.GormCondition{
  96. // GormPublic: search.GormPublic{},
  97. // Join: make([]*search.GormJoin, 0),
  98. // }
  99. // search.ResolveSearchQuery(global.Driver, q, condition)
  100. //
  101. // for _, join := range condition.Join {
  102. // if join == nil {
  103. // continue
  104. // }
  105. // db = db.Joins(join.JoinOn)
  106. // for k, v := range join.Where {
  107. // db = db.Where(k, v...)
  108. // }
  109. // for k, v := range join.Or {
  110. // db = db.Or(k, v...)
  111. // }
  112. // for _, o := range join.Order {
  113. // db = db.Order(o)
  114. // }
  115. // }
  116. // for k, v := range condition.Where {
  117. // db = db.Where(k, v...)
  118. // }
  119. // for k, v := range condition.Or {
  120. // db = db.Or(k, v...)
  121. // }
  122. // var orContains string
  123. // for k, v := range condition.OrContains {
  124. // orContains += fmt.Sprintf(" OR %v '%v'", k, v[0])
  125. // }
  126. // if len(orContains) > 0 {
  127. // db = db.Where(strings.TrimLeft(orContains, " OR"))
  128. // }
  129. // for _, o := range condition.Order {
  130. // db = db.Order(o)
  131. // }
  132. // return db
  133. // }
  134. //}
  135. //
  136. //// 辅助函数:用于处理非WaybillGetPageReq类型的正常情况
  137. //func makeNormalCondition(q interface{}) func(db *gorm.DB) *gorm.DB {
  138. // return func(db *gorm.DB) *gorm.DB {
  139. // condition := &search.GormCondition{
  140. // GormPublic: search.GormPublic{},
  141. // Join: make([]*search.GormJoin, 0),
  142. // }
  143. // search.ResolveSearchQuery(global.Driver, q, condition)
  144. //
  145. // for _, join := range condition.Join {
  146. // if join == nil {
  147. // continue
  148. // }
  149. // db = db.Joins(join.JoinOn)
  150. // for k, v := range join.Where {
  151. // db = db.Where(k, v...)
  152. // }
  153. // for k, v := range join.Or {
  154. // db = db.Or(k, v...)
  155. // }
  156. // for _, o := range join.Order {
  157. // db = db.Order(o)
  158. // }
  159. // }
  160. // for k, v := range condition.Where {
  161. // db = db.Where(k, v...)
  162. // }
  163. // for k, v := range condition.Or {
  164. // db = db.Or(k, v...)
  165. // }
  166. // var orContains string
  167. // for k, v := range condition.OrContains {
  168. // orContains += fmt.Sprintf(" OR %v '%v'", k, v[0])
  169. // }
  170. // if len(orContains) > 0 {
  171. // db = db.Where(strings.TrimLeft(orContains, " OR"))
  172. // }
  173. // for _, o := range condition.Order {
  174. // db = db.Order(o)
  175. // }
  176. // return db
  177. // }
  178. //}
  179. func Paginate(pageSize, pageIndex int) func(db *gorm.DB) *gorm.DB {
  180. return func(db *gorm.DB) *gorm.DB {
  181. offset := (pageIndex - 1) * pageSize
  182. if offset < 0 {
  183. offset = 0
  184. }
  185. return db.Offset(offset).Limit(pageSize)
  186. }
  187. }