unity.go 3.8 KB

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