unity.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. package unity
  2. import (
  3. "Panel_development/global"
  4. "errors"
  5. )
  6. type PageParams struct {
  7. Page int `json:"page" form:"page"`
  8. Size int `json:"size" form:"size"`
  9. Desc string `json:"desc" form:"desc"`
  10. }
  11. type QueryPageParams struct {
  12. Page int `json:"page" form:"page"`
  13. Size int `json:"size" form:"size"`
  14. Query string `json:"query" form:"query"`
  15. Desc string `json:"desc" form:"desc"`
  16. }
  17. // Paginate 使用给定的DB连接执行分页查询
  18. func Paginate[T any](params PageParams, model T) (result []T, total int64, current int, err error) {
  19. var count int64
  20. if err = global.DBLink.Model(model).Count(&count).Error; err != nil {
  21. return nil, 0, 0, err
  22. }
  23. // 计算总页数
  24. // totalPages := int(math.Ceil(float64(count) / float64(params.Size)))
  25. // 计算偏移量并设置分页大小
  26. offset := (params.Page - 1) * params.Size
  27. // 执行实际分页查询
  28. if err = global.DBLink.Offset(offset).Limit(params.Size).Order(params.Desc).Find(&result).Error; err != nil {
  29. return nil, 0, 0, err
  30. }
  31. return result, count, params.Page, nil
  32. }
  33. func PaginateWithCondition[T any](params QueryPageParams, model T, queryCond interface{}) (result []T, total int64, err error) {
  34. var count int64
  35. if queryCond != nil {
  36. if err = global.DBLink.Model(model).Where(queryCond, params.Query).Count(&count).Error; err != nil {
  37. return nil, 0, err
  38. }
  39. // 计算查询的偏移量,并设置每次查询的记录数量
  40. offset := (params.Page - 1) * params.Size
  41. // 执行分页查询,包括偏移量设置、限制查询数量、排序及应用额外查询条件
  42. if err = global.DBLink.Offset(offset).Limit(params.Size).Order(params.Desc).Where(queryCond, params.Query).Find(&result).Error; err != nil {
  43. return nil, 0, err
  44. }
  45. } else {
  46. if err = global.DBLink.Model(model).Count(&count).Error; err != nil {
  47. return nil, 0, err
  48. }
  49. // 计算查询的偏移量,并设置每次查询的记录数量
  50. offset := (params.Page - 1) * params.Size
  51. // 执行分页查询,包括偏移量设置、限制查询数量、排序及应用额外查询条件
  52. if err = global.DBLink.Offset(offset).Limit(params.Size).Order(params.Desc).Find(&result).Error; err != nil {
  53. return nil, 0, err
  54. }
  55. }
  56. return result, count, nil
  57. }
  58. func AddGenericItem[T any](item T, mapper func(T) interface{}) error {
  59. dbModel := mapper(item)
  60. if err := global.DBLink.Create(dbModel).Error; err != nil {
  61. return err
  62. }
  63. return nil
  64. }
  65. // GetById 根据id查询
  66. func GetById[T any](id int, model T) (T, error) {
  67. tx := global.DBLink.Where("id = ?", id).First(&model)
  68. if tx.RowsAffected <= 0 {
  69. return model, errors.New("查询失败,当前id不存在")
  70. } else {
  71. return model, nil
  72. }
  73. }
  74. // UpdateById 根据id进行修改
  75. func UpdateById[T any](id int, model T) (T, error) {
  76. tx := global.DBLink.Where("id = ?", id).Updates(&model)
  77. if tx.RowsAffected > 0 {
  78. return model, nil
  79. } else {
  80. return model, errors.New("修改失败当前id不存在")
  81. }
  82. }
  83. // Add 添加
  84. func Add[T any](model T) (T, error) {
  85. tx := global.DBLink.Create(&model)
  86. if tx.RowsAffected > 0 {
  87. return model, nil
  88. } else {
  89. return model, errors.New("添加失败")
  90. }
  91. }
  92. // DeleteById 根据id删除
  93. func DeleteById[T any](id int, model T) (T, error) {
  94. tx := global.DBLink.Where("id = ?", id).Delete(&model)
  95. if tx.RowsAffected > 0 {
  96. return model, nil
  97. } else {
  98. return model, errors.New("删除失败当前id不存在")
  99. }
  100. }
  101. // QueryAll 获取所有信息
  102. func QueryAll(table string) ([]any, error) {
  103. var result []any
  104. tx := global.DBLink.Table(table).Find(&result)
  105. if tx.Error != nil {
  106. return result, errors.New("查询失败")
  107. }
  108. return result, nil
  109. }