search.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. package dto
  2. import (
  3. "git.cestong.com.cn/cecf/hx-admin-server/common/global"
  4. "github.com/go-admin-team/go-admin-core/tools/search"
  5. "gorm.io/gorm"
  6. )
  7. type GeneralDelDto struct {
  8. Id int `uri:"id" json:"id" validate:"required"`
  9. Ids []int `json:"ids"`
  10. }
  11. func (g GeneralDelDto) GetIds() []int {
  12. ids := make([]int, 0)
  13. if g.Id != 0 {
  14. ids = append(ids, g.Id)
  15. }
  16. if len(g.Ids) > 0 {
  17. for _, id := range g.Ids {
  18. if id > 0 {
  19. ids = append(ids, id)
  20. }
  21. }
  22. } else {
  23. if g.Id > 0 {
  24. ids = append(ids, g.Id)
  25. }
  26. }
  27. if len(ids) <= 0 {
  28. //方式全部删除
  29. ids = append(ids, 0)
  30. }
  31. return ids
  32. }
  33. type GeneralGetDto struct {
  34. Id int `uri:"id" json:"id" validate:"required"`
  35. }
  36. func MakeCondition(q interface{}) func(db *gorm.DB) *gorm.DB {
  37. return func(db *gorm.DB) *gorm.DB {
  38. condition := &search.GormCondition{
  39. GormPublic: search.GormPublic{},
  40. Join: make([]*search.GormJoin, 0),
  41. }
  42. search.ResolveSearchQuery(global.Driver, q, condition)
  43. for _, join := range condition.Join {
  44. if join == nil {
  45. continue
  46. }
  47. db = db.Joins(join.JoinOn)
  48. for k, v := range join.Where {
  49. db = db.Where(k, v...)
  50. }
  51. for k, v := range join.Or {
  52. db = db.Or(k, v...)
  53. }
  54. for _, o := range join.Order {
  55. db = db.Order(o)
  56. }
  57. }
  58. for k, v := range condition.Where {
  59. db = db.Where(k, v...)
  60. }
  61. for k, v := range condition.Or {
  62. db = db.Or(k, v...)
  63. }
  64. for _, o := range condition.Order {
  65. db = db.Order(o)
  66. }
  67. return db
  68. }
  69. }
  70. func Paginate(pageSize, pageIndex int) func(db *gorm.DB) *gorm.DB {
  71. return func(db *gorm.DB) *gorm.DB {
  72. offset := (pageIndex - 1) * pageSize
  73. if offset < 0 {
  74. offset = 0
  75. }
  76. return db.Offset(offset).Limit(pageSize)
  77. }
  78. }