2023-10-26 17:15:49 +08:00
|
|
|
|
package base
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
2023-11-07 21:05:21 +08:00
|
|
|
|
"context"
|
2023-12-05 23:03:51 +08:00
|
|
|
|
"fmt"
|
|
|
|
|
|
"mayfly-go/pkg/contextx"
|
|
|
|
|
|
"mayfly-go/pkg/global"
|
2023-11-07 21:05:21 +08:00
|
|
|
|
"mayfly-go/pkg/model"
|
2024-04-09 12:55:51 +08:00
|
|
|
|
"mayfly-go/pkg/utils/collx"
|
2023-11-07 21:05:21 +08:00
|
|
|
|
|
2023-10-26 17:15:49 +08:00
|
|
|
|
"gorm.io/gorm"
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
// 基础application接口
|
2023-11-07 21:05:21 +08:00
|
|
|
|
type App[T model.ModelI] interface {
|
2023-10-26 17:15:49 +08:00
|
|
|
|
|
|
|
|
|
|
// 新增一个实体
|
2023-11-07 21:05:21 +08:00
|
|
|
|
Insert(ctx context.Context, e T) error
|
2023-10-26 17:15:49 +08:00
|
|
|
|
|
2023-10-27 17:41:45 +08:00
|
|
|
|
// 使用指定gorm db执行,主要用于事务执行
|
2023-11-07 21:05:21 +08:00
|
|
|
|
InsertWithDb(ctx context.Context, db *gorm.DB, e T) error
|
2023-10-27 17:41:45 +08:00
|
|
|
|
|
2023-10-26 17:15:49 +08:00
|
|
|
|
// 批量新增实体
|
2023-11-07 21:05:21 +08:00
|
|
|
|
BatchInsert(ctx context.Context, models []T) error
|
2023-10-26 17:15:49 +08:00
|
|
|
|
|
2023-10-27 17:41:45 +08:00
|
|
|
|
// 使用指定gorm db执行,主要用于事务执行
|
2023-11-07 21:05:21 +08:00
|
|
|
|
BatchInsertWithDb(ctx context.Context, db *gorm.DB, es []T) error
|
2023-10-27 17:41:45 +08:00
|
|
|
|
|
2023-10-26 17:15:49 +08:00
|
|
|
|
// 根据实体id更新实体信息
|
2023-11-07 21:05:21 +08:00
|
|
|
|
UpdateById(ctx context.Context, e T) error
|
2023-10-26 17:15:49 +08:00
|
|
|
|
|
|
|
|
|
|
// 使用指定gorm db执行,主要用于事务执行
|
2023-11-07 21:05:21 +08:00
|
|
|
|
UpdateByIdWithDb(ctx context.Context, db *gorm.DB, e T) error
|
2023-10-26 17:15:49 +08:00
|
|
|
|
|
2024-04-09 12:55:51 +08:00
|
|
|
|
// UpdateByWheres 更新满足wheres条件的数据
|
|
|
|
|
|
// @param wheres key => "age > ?" value => 10等
|
|
|
|
|
|
UpdateByWheres(ctx context.Context, e T, wheres collx.M, columns ...string) error
|
|
|
|
|
|
|
|
|
|
|
|
// UpdateByWheresWithDb 使用指定gorm.Db更新满足wheres条件的数据
|
|
|
|
|
|
// @param wheres key => "age > ?" value => 10等
|
|
|
|
|
|
UpdateByWheresWithDb(ctx context.Context, db *gorm.DB, e T, wheres collx.M, columns ...string) error
|
|
|
|
|
|
|
2023-10-26 17:15:49 +08:00
|
|
|
|
// 根据实体主键删除实体
|
2023-11-07 21:05:21 +08:00
|
|
|
|
DeleteById(ctx context.Context, id uint64) error
|
2023-10-26 17:15:49 +08:00
|
|
|
|
|
|
|
|
|
|
// 使用指定gorm db执行,主要用于事务执行
|
2023-11-07 21:05:21 +08:00
|
|
|
|
DeleteByIdWithDb(ctx context.Context, db *gorm.DB, id uint64) error
|
2023-10-26 17:15:49 +08:00
|
|
|
|
|
2024-04-09 12:55:51 +08:00
|
|
|
|
// DeleteByWheres 根据wheres条件进行删除
|
|
|
|
|
|
// @param wheres key -> "age > ?" value -> 10等
|
|
|
|
|
|
DeleteByWheres(ctx context.Context, wheres collx.M) error
|
|
|
|
|
|
|
|
|
|
|
|
// DeleteByWheresWithDb 使用指定gorm.Db根据wheres条件进行删除
|
|
|
|
|
|
// @param wheres key -> "age > ?" value -> 10等
|
|
|
|
|
|
DeleteByWheresWithDb(ctx context.Context, db *gorm.DB, wheres collx.M) error
|
|
|
|
|
|
|
2023-10-26 17:15:49 +08:00
|
|
|
|
// 根据实体条件,更新参数udpateFields指定字段
|
2023-11-07 21:05:21 +08:00
|
|
|
|
Updates(ctx context.Context, cond any, udpateFields map[string]any) error
|
2023-10-26 17:15:49 +08:00
|
|
|
|
|
2024-01-07 21:46:25 +08:00
|
|
|
|
// 保存实体,实体IsCreate返回true则新增,否则更新
|
|
|
|
|
|
Save(ctx context.Context, e T) error
|
|
|
|
|
|
|
|
|
|
|
|
// 保存实体,实体IsCreate返回true则新增,否则更新。
|
|
|
|
|
|
// 使用指定gorm db执行,主要用于事务执行
|
|
|
|
|
|
SaveWithDb(ctx context.Context, db *gorm.DB, e T) error
|
|
|
|
|
|
|
2023-10-26 17:15:49 +08:00
|
|
|
|
// 根据实体条件删除实体
|
2023-11-07 21:05:21 +08:00
|
|
|
|
DeleteByCond(ctx context.Context, cond any) error
|
2023-10-26 17:15:49 +08:00
|
|
|
|
|
|
|
|
|
|
// 使用指定gorm db执行,主要用于事务执行
|
2023-11-07 21:05:21 +08:00
|
|
|
|
DeleteByCondWithDb(ctx context.Context, db *gorm.DB, cond any) error
|
2023-10-26 17:15:49 +08:00
|
|
|
|
|
|
|
|
|
|
// 根据实体id查询
|
|
|
|
|
|
GetById(e T, id uint64, cols ...string) (T, error)
|
|
|
|
|
|
|
|
|
|
|
|
GetByIdIn(list any, ids []uint64, orderBy ...string) error
|
|
|
|
|
|
|
|
|
|
|
|
// 根据实体条件查询实体信息
|
|
|
|
|
|
GetBy(condModel T, cols ...string) error
|
|
|
|
|
|
|
|
|
|
|
|
// 根据条件查询数据映射至listModels
|
|
|
|
|
|
ListByCond(cond any, listModels any, cols ...string) error
|
|
|
|
|
|
|
2024-04-12 17:07:28 +08:00
|
|
|
|
// 根据wheres条件进行过滤
|
|
|
|
|
|
// @param wheres key -> "age > ?" value -> 10等
|
|
|
|
|
|
ListByWheres(wheres collx.M, listModels any, cols ...string) error
|
|
|
|
|
|
|
2024-04-09 12:55:51 +08:00
|
|
|
|
// PageQuery 分页查询
|
|
|
|
|
|
PageQuery(cond any, pageParam *model.PageParam, toModels any) (*model.PageResult[any], error)
|
|
|
|
|
|
|
2023-10-26 17:15:49 +08:00
|
|
|
|
// 获取满足model中不为空的字段值条件的所有数据.
|
|
|
|
|
|
//
|
|
|
|
|
|
// @param list为数组类型 如 var users *[]User,可指定为非model结构体
|
|
|
|
|
|
// @param cond 条件
|
|
|
|
|
|
ListByCondOrder(cond any, list any, order ...string) error
|
|
|
|
|
|
|
|
|
|
|
|
// 根据指定条件统计model表的数量, cond为条件可以为map等
|
|
|
|
|
|
CountByCond(cond any) int64
|
2023-12-05 23:03:51 +08:00
|
|
|
|
|
|
|
|
|
|
// 执行事务操作
|
|
|
|
|
|
Tx(ctx context.Context, funcs ...func(context.Context) error) (err error)
|
2023-10-26 17:15:49 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 基础application接口实现
|
2023-11-07 21:05:21 +08:00
|
|
|
|
type AppImpl[T model.ModelI, R Repo[T]] struct {
|
2023-10-26 17:15:49 +08:00
|
|
|
|
Repo R // repo接口
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 获取repo
|
|
|
|
|
|
func (ai *AppImpl[T, R]) GetRepo() R {
|
|
|
|
|
|
return ai.Repo
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 新增一个实体 (单纯新增,不做其他业务逻辑处理)
|
2023-11-07 21:05:21 +08:00
|
|
|
|
func (ai *AppImpl[T, R]) Insert(ctx context.Context, e T) error {
|
|
|
|
|
|
return ai.GetRepo().Insert(ctx, e)
|
2023-10-26 17:15:49 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-10-27 17:41:45 +08:00
|
|
|
|
// 使用指定gorm db执行,主要用于事务执行
|
2023-11-07 21:05:21 +08:00
|
|
|
|
func (ai *AppImpl[T, R]) InsertWithDb(ctx context.Context, db *gorm.DB, e T) error {
|
|
|
|
|
|
return ai.GetRepo().InsertWithDb(ctx, db, e)
|
2023-10-27 17:41:45 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-10-26 17:15:49 +08:00
|
|
|
|
// 批量新增实体 (单纯新增,不做其他业务逻辑处理)
|
2023-11-07 21:05:21 +08:00
|
|
|
|
func (ai *AppImpl[T, R]) BatchInsert(ctx context.Context, es []T) error {
|
|
|
|
|
|
return ai.GetRepo().BatchInsert(ctx, es)
|
2023-10-26 17:15:49 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-10-27 17:41:45 +08:00
|
|
|
|
// 使用指定gorm db执行,主要用于事务执行
|
2024-01-11 11:35:51 +08:00
|
|
|
|
func (ai *AppImpl[T, R]) BatchInsertWithDb(ctx context.Context, db *gorm.DB, models []T) error {
|
|
|
|
|
|
return ai.GetRepo().BatchInsertWithDb(ctx, db, models)
|
2023-10-27 17:41:45 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-10-26 17:15:49 +08:00
|
|
|
|
// 根据实体id更新实体信息 (单纯更新,不做其他业务逻辑处理)
|
2023-11-07 21:05:21 +08:00
|
|
|
|
func (ai *AppImpl[T, R]) UpdateById(ctx context.Context, e T) error {
|
|
|
|
|
|
return ai.GetRepo().UpdateById(ctx, e)
|
2023-10-26 17:15:49 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 使用指定gorm db执行,主要用于事务执行
|
2023-11-07 21:05:21 +08:00
|
|
|
|
func (ai *AppImpl[T, R]) UpdateByIdWithDb(ctx context.Context, db *gorm.DB, e T) error {
|
|
|
|
|
|
return ai.GetRepo().UpdateByIdWithDb(ctx, db, e)
|
2023-10-26 17:15:49 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-04-09 12:55:51 +08:00
|
|
|
|
func (ai *AppImpl[T, R]) UpdateByWheres(ctx context.Context, e T, wheres collx.M, columns ...string) error {
|
|
|
|
|
|
return ai.GetRepo().UpdateByWheres(ctx, e, wheres, columns...)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (ai *AppImpl[T, R]) UpdateByWheresWithDb(ctx context.Context, db *gorm.DB, e T, wheres collx.M, columns ...string) error {
|
|
|
|
|
|
return ai.GetRepo().UpdateByWheresWithDb(ctx, db, e, wheres, columns...)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-10-26 17:15:49 +08:00
|
|
|
|
// 根据实体条件,更新参数udpateFields指定字段 (单纯更新,不做其他业务逻辑处理)
|
2023-11-07 21:05:21 +08:00
|
|
|
|
func (ai *AppImpl[T, R]) Updates(ctx context.Context, cond any, udpateFields map[string]any) error {
|
2023-10-26 17:15:49 +08:00
|
|
|
|
return ai.GetRepo().Updates(cond, udpateFields)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-01-07 21:46:25 +08:00
|
|
|
|
// 保存实体,实体IsCreate返回true则新增,否则更新
|
|
|
|
|
|
func (ai *AppImpl[T, R]) Save(ctx context.Context, e T) error {
|
|
|
|
|
|
return ai.GetRepo().Save(ctx, e)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 保存实体,实体IsCreate返回true则新增,否则更新。
|
|
|
|
|
|
// 使用指定gorm db执行,主要用于事务执行
|
|
|
|
|
|
func (ai *AppImpl[T, R]) SaveWithDb(ctx context.Context, db *gorm.DB, e T) error {
|
|
|
|
|
|
return ai.GetRepo().SaveWithDb(ctx, db, e)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-10-26 17:15:49 +08:00
|
|
|
|
// 根据实体主键删除实体 (单纯删除实体,不做其他业务逻辑处理)
|
2023-11-07 21:05:21 +08:00
|
|
|
|
func (ai *AppImpl[T, R]) DeleteById(ctx context.Context, id uint64) error {
|
|
|
|
|
|
return ai.GetRepo().DeleteById(ctx, id)
|
2023-10-26 17:15:49 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-11-07 21:05:21 +08:00
|
|
|
|
func (ai *AppImpl[T, R]) DeleteByIdWithDb(ctx context.Context, db *gorm.DB, id uint64) error {
|
|
|
|
|
|
return ai.GetRepo().DeleteByCondWithDb(ctx, db, id)
|
2023-10-26 17:15:49 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 根据指定条件删除实体 (单纯删除实体,不做其他业务逻辑处理)
|
2023-11-07 21:05:21 +08:00
|
|
|
|
func (ai *AppImpl[T, R]) DeleteByCond(ctx context.Context, cond any) error {
|
|
|
|
|
|
return ai.GetRepo().DeleteByCond(ctx, cond)
|
2023-10-26 17:15:49 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 使用指定gorm db执行,主要用于事务执行
|
2023-11-07 21:05:21 +08:00
|
|
|
|
func (ai *AppImpl[T, R]) DeleteByCondWithDb(ctx context.Context, db *gorm.DB, cond any) error {
|
|
|
|
|
|
return ai.GetRepo().DeleteByCondWithDb(ctx, db, cond)
|
2023-10-26 17:15:49 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-04-09 12:55:51 +08:00
|
|
|
|
func (ai *AppImpl[T, R]) DeleteByWheres(ctx context.Context, wheres collx.M) error {
|
|
|
|
|
|
return ai.GetRepo().DeleteByWheres(ctx, wheres)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (ai *AppImpl[T, R]) DeleteByWheresWithDb(ctx context.Context, db *gorm.DB, wheres collx.M) error {
|
|
|
|
|
|
return ai.GetRepo().DeleteByWheresWithDb(ctx, db, wheres)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-10-26 17:15:49 +08:00
|
|
|
|
// 根据实体id查询
|
|
|
|
|
|
func (ai *AppImpl[T, R]) GetById(e T, id uint64, cols ...string) (T, error) {
|
|
|
|
|
|
if err := ai.GetRepo().GetById(e, id, cols...); err != nil {
|
|
|
|
|
|
return e, err
|
|
|
|
|
|
}
|
|
|
|
|
|
return e, nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (ai *AppImpl[T, R]) GetByIdIn(list any, ids []uint64, orderBy ...string) error {
|
|
|
|
|
|
return ai.GetRepo().GetByIdIn(list, ids, orderBy...)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 根据实体条件查询实体信息
|
|
|
|
|
|
func (ai *AppImpl[T, R]) GetBy(condModel T, cols ...string) error {
|
|
|
|
|
|
return ai.GetRepo().GetBy(condModel, cols...)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 根据条件查询数据映射至listModels
|
|
|
|
|
|
func (ai *AppImpl[T, R]) ListByCond(cond any, listModels any, cols ...string) error {
|
|
|
|
|
|
return ai.GetRepo().ListByCond(cond, listModels, cols...)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-04-12 17:07:28 +08:00
|
|
|
|
func (ai *AppImpl[T, R]) ListByWheres(wheres collx.M, listModels any, cols ...string) error {
|
|
|
|
|
|
return ai.GetRepo().ListByWheres(wheres, listModels, cols...)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-04-09 12:55:51 +08:00
|
|
|
|
// PageQuery 分页查询
|
|
|
|
|
|
func (ai *AppImpl[T, R]) PageQuery(cond any, pageParam *model.PageParam, toModels any) (*model.PageResult[any], error) {
|
|
|
|
|
|
return ai.GetRepo().PageQuery(cond, pageParam, toModels)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-10-26 17:15:49 +08:00
|
|
|
|
// 获取满足model中不为空的字段值条件的所有数据.
|
|
|
|
|
|
//
|
|
|
|
|
|
// @param list为数组类型 如 var users *[]User,可指定为非model结构体
|
|
|
|
|
|
// @param cond 条件
|
|
|
|
|
|
func (ai *AppImpl[T, R]) ListByCondOrder(cond any, list any, order ...string) error {
|
|
|
|
|
|
return ai.GetRepo().ListByCondOrder(cond, list, order...)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 根据指定条件统计model表的数量, cond为条件可以为map等
|
|
|
|
|
|
func (ai *AppImpl[T, R]) CountByCond(cond any) int64 {
|
|
|
|
|
|
return ai.GetRepo().CountByCond(cond)
|
|
|
|
|
|
}
|
2023-12-05 23:03:51 +08:00
|
|
|
|
|
|
|
|
|
|
// 执行事务操作
|
|
|
|
|
|
func (ai *AppImpl[T, R]) Tx(ctx context.Context, funcs ...func(context.Context) error) (err error) {
|
|
|
|
|
|
tx := global.Db.Begin()
|
|
|
|
|
|
dbCtx := contextx.WithDb(ctx, tx)
|
|
|
|
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
|
|
// 移除当前已执行完成的的数据库事务实例
|
|
|
|
|
|
contextx.RmDb(ctx)
|
|
|
|
|
|
if r := recover(); r != nil {
|
|
|
|
|
|
tx.Rollback()
|
|
|
|
|
|
err = fmt.Errorf("%v", err)
|
|
|
|
|
|
}
|
|
|
|
|
|
}()
|
|
|
|
|
|
|
|
|
|
|
|
for _, f := range funcs {
|
|
|
|
|
|
err = f(dbCtx)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
tx.Rollback()
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
err = tx.Commit().Error
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|