123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120 |
- package unity
- import (
- "Panel_development/global"
- "errors"
- )
- type PageParams struct {
- Page int `json:"page" form:"page"`
- Size int `json:"size" form:"size"`
- Desc string `json:"desc" form:"desc"`
- }
- type QueryPageParams struct {
- Page int `json:"page" form:"page"`
- Size int `json:"size" form:"size"`
- Query string `json:"query" form:"query"`
- 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 = global.DBLink.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 = global.DBLink.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 QueryPageParams, model T, queryCond interface{}) (result []T, total int64, err error) {
- var count int64
- if queryCond != nil {
- if err = global.DBLink.Model(model).Where(queryCond, params.Query).Count(&count).Error; err != nil {
- return nil, 0, err
- }
- // 计算查询的偏移量,并设置每次查询的记录数量
- offset := (params.Page - 1) * params.Size
- // 执行分页查询,包括偏移量设置、限制查询数量、排序及应用额外查询条件
- if err = global.DBLink.Offset(offset).Limit(params.Size).Order(params.Desc).Where(queryCond, params.Query).Find(&result).Error; err != nil {
- return nil, 0, err
- }
- } else {
- if err = global.DBLink.Model(model).Count(&count).Error; err != nil {
- return nil, 0, err
- }
- // 计算查询的偏移量,并设置每次查询的记录数量
- offset := (params.Page - 1) * params.Size
- // 执行分页查询,包括偏移量设置、限制查询数量、排序及应用额外查询条件
- if err = global.DBLink.Offset(offset).Limit(params.Size).Order(params.Desc).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 := global.DBLink.Create(dbModel).Error; err != nil {
- return err
- }
- return nil
- }
- // GetById 根据id查询
- func GetById[T any](id int, model T) (T, error) {
- tx := global.DBLink.Where("id = ?", id).First(&model)
- if tx.RowsAffected <= 0 {
- return model, errors.New("查询失败,当前id不存在")
- } else {
- return model, nil
- }
- }
- // UpdateById 根据id进行修改
- func UpdateById[T any](id int, model T) (T, error) {
- tx := global.DBLink.Where("id = ?", id).Updates(&model)
- if tx.RowsAffected > 0 {
- return model, nil
- } else {
- return model, errors.New("修改失败当前id不存在")
- }
- }
- // Add 添加
- func Add[T any](model T) (T, error) {
- tx := global.DBLink.Create(&model)
- if tx.RowsAffected > 0 {
- return model, nil
- } else {
- return model, errors.New("添加失败")
- }
- }
- // DeleteById 根据id删除
- func DeleteById[T any](id int, model T) (T, error) {
- tx := global.DBLink.Where("id = ?", id).Delete(&model)
- if tx.RowsAffected > 0 {
- return model, nil
- } else {
- return model, errors.New("删除失败当前id不存在")
- }
- }
- // QueryAll 获取所有信息
- func QueryAll(table string) ([]any, error) {
- var result []any
- tx := global.DBLink.Table(table).Find(&result)
- if tx.Error != nil {
- return result, errors.New("查询失败")
- }
- return result, nil
- }
|