123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121 |
- package unity
- import (
- "cc-officialweb/models"
- "cc-officialweb/utils"
- "errors"
- )
- type PageParams struct {
- Page int `json:"page" form:"page"`
- Size int `json:"size" form:"size"`
- Desc string `json:"desc" form:"desc"`
- }
- // Paginate 使用给定的DB连接执行分页查询
- func Paginate[T any](params PageParams, model T) (result []T, total int64, current int, err error) {
- var count int64
- if err = utils.DB.Model(model).Count(&count).Error; err != nil {
- return nil, 0, 0, err
- }
- // 计算总页数
- // totalPages := int(math.Ceil(float64(count) / float64(params.Size)))
- // 计算偏移量并设置分页大小
- offset := (params.Page - 1) * params.Size
- // 执行实际分页查询
- if err = utils.DB.Offset(offset).Limit(params.Size).Order(params.Desc).Find(&result).Error; err != nil {
- return nil, 0, 0, err
- }
- return result, count, params.Page, nil
- }
- // 针对resource不查询服务和产品得图片以免混淆
- func PaginateResource(params PageParams, resour models.Resource) (result []models.Resource, total int64, current int, err error) {
- var count int64
- if err = utils.DB.Model(resour).Where("type = ?", "banner").Or("type = ?", "honor").Or("type = ?", "example").Count(&count).Error; err != nil {
- return nil, 0, 0, err
- }
- // 计算总页数
- // totalPages := int(math.Ceil(float64(count) / float64(params.Size)))
- // 计算偏移量并设置分页大小
- offset := (params.Page - 1) * params.Size
- // 执行实际分页查询
- 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 {
- return nil, 0, 0, err
- }
- return result, count, params.Page, nil
- }
- func PaginateWithCondition[T any](params PageParams, model T, queryCond interface{}, args ...interface{}) (result []T, total int64, err error) {
- var count int64
- // 根据模型和额外查询条件查询数据库中符合条件的数据总量
- //if err = global.DBLink.Model(model).Where(queryCond, args...).Count(&count).Error; err != nil {
- // return nil, 0, err
- //}
- if queryCond != nil {
- if err = utils.DB.Model(result).Where(queryCond, args...).Count(&count).Error; err != nil {
- return nil, 0, err
- }
- } else {
- if err = utils.DB.Model(result).Count(&count).Error; err != nil {
- return nil, 0, err
- }
- }
- // 计算查询的偏移量,并设置每次查询的记录数量
- offset := (params.Page - 1) * params.Size
- // 执行分页查询,包括偏移量设置、限制查询数量、排序及应用额外查询条件
- if err = utils.DB.Offset(offset).Limit(params.Size).Order(params.Desc).Where(queryCond, args...).Find(&result).Error; err != nil {
- return nil, 0, err
- }
- return result, count, nil
- }
- func AddGenericItem[T any](item T, mapper func(T) interface{}) error {
- dbModel := mapper(item)
- if err := utils.DB.Create(dbModel).Error; err != nil {
- return err
- }
- return nil
- }
- // GetById 根据id查询
- func GetById[T any](id int, model T) (T, error) {
- tx := utils.DB.Where("id = ?", id).First(&model)
- if tx.Error != nil {
- return model, errors.New("查询失败")
- } else {
- return model, nil
- }
- }
- // UpdateById 根据id进行修改
- func UpdateById[T any](id uint, model T) (T, error) {
- tx := utils.DB.Where("id = ?", id).Updates(&model)
- if tx.RowsAffected > 0 {
- return model, nil
- } else {
- return model, errors.New("修改失败当前id不存在")
- }
- }
- func Add[T any](model T) (T, error) {
- tx := utils.DB.Create(&model)
- if tx.RowsAffected > 0 {
- return model, nil
- } else {
- return model, errors.New("添加失败当前id不存在")
- }
- }
- // DeleteById 根据id删除
- func DeleteById[T any](id int, model T) (T, error) {
- tx := utils.DB.Where("id = ?", id).Delete(&model)
- if tx.RowsAffected > 0 {
- return model, nil
- } else {
- return model, errors.New("删除失败当前id不存在")
- }
- }
|