refactor(crud): 修改关联查询参数类型为字符串

- 将ClearFieldPage、ClearFieldList、ClearFieldOne等方法中的with参数从bool改为string
- 更新Preload逻辑从固定"*"改为动态传入的字符串参数
- 修改FindPri、First、All、Paginate等方法的with参数类型变更
- 更新IDE缓存文件中的模型和服务映射关系
- 添加News和User相关实体的缓存配置
- 更新数据库表结构映射配置
This commit is contained in:
2026-03-04 15:10:05 +08:00
parent 8bacccb894
commit 5581041da4
2 changed files with 156 additions and 22 deletions
+21 -21
View File
@@ -212,14 +212,14 @@ func (c Crud[R]) ClearField(req any, delField []string, subField ...map[string]a
}
// ClearFieldPage -------------------------- 原ClearFieldPage对应实现:清理参数+分页查询 --------------------------
func (c Crud[R]) ClearFieldPage(ctx ctx, req any, delField []string, where any, page *Paginate, order any, with bool) (items []*R, total int64, err error) {
func (c Crud[R]) ClearFieldPage(ctx ctx, req any, delField []string, where any, page *Paginate, order any, with string) (items []*R, total int64, err error) {
// 1. 清理请求参数
filterMap := c.ClearField(req, delField)
// 2. 初始化GORM查询
db := c.Dao.Ctx(ctx)
if with {
db = db.Preload("*") // GORM 关联查询全部,对应GF的WithAll()
if with != "" {
db = db.Preload(with) // GORM 关联查询全部,对应GF的WithAll()
}
// 3. 构建查询条件
@@ -250,12 +250,12 @@ func (c Crud[R]) ClearFieldPage(ctx ctx, req any, delField []string, where any,
}
// ClearFieldList -------------------------- 原ClearFieldList对应实现:清理参数+列表查询(不分页) --------------------------
func (c Crud[R]) ClearFieldList(ctx ctx, req any, delField []string, where any, order any, with bool) (items []*R, err error) {
func (c Crud[R]) ClearFieldList(ctx ctx, req any, delField []string, where any, order any, with string) (items []*R, err error) {
filterMap := c.ClearField(req, delField)
db := c.Dao.Ctx(ctx).Model(new(R))
if with {
db = db.Preload("*")
if with != "" {
db = db.Preload(with)
}
if where != nil {
db = db.Where(where)
@@ -269,13 +269,13 @@ func (c Crud[R]) ClearFieldList(ctx ctx, req any, delField []string, where any,
}
// ClearFieldOne -------------------------- 原ClearFieldOne对应实现:清理参数+单条查询 --------------------------
func (c Crud[R]) ClearFieldOne(ctx ctx, req any, delField []string, where any, order any, with bool) (item *R, err error) {
func (c Crud[R]) ClearFieldOne(ctx ctx, req any, delField []string, where any, order any, with string) (item *R, err error) {
item = new(R)
filterMap := c.ClearField(req, delField)
db := c.Dao.Ctx(ctx).Model(item)
if with {
db = db.Preload("*")
if with != "" {
db = db.Preload(with)
}
if where != nil {
db = db.Where(where)
@@ -371,7 +371,7 @@ func (c Crud[R]) ArrayField(ctx ctx, where any, field any) []interface{} {
}
// FindPri -------------------------- 原FindPri对应实现:按主键查询单条记录 --------------------------
func (c Crud[R]) FindPri(ctx ctx, primaryKey any, with bool) (model *R) {
func (c Crud[R]) FindPri(ctx ctx, primaryKey any, with string) (model *R) {
model = new(R)
db := c.Dao.Ctx(ctx).Model(model)
pk := c.Dao.PrimaryKey()
@@ -379,8 +379,8 @@ func (c Crud[R]) FindPri(ctx ctx, primaryKey any, with bool) (model *R) {
if pk == "" {
panic("主键字段未配置")
}
if with {
db = db.Preload("*")
if with != "" {
db = db.Preload(with)
}
// 按主键查询
@@ -392,12 +392,12 @@ func (c Crud[R]) FindPri(ctx ctx, primaryKey any, with bool) (model *R) {
}
// -------------------------- 原First对应实现:按条件查询第一条记录 --------------------------
func (c Crud[R]) First(ctx ctx, where any, order any, with bool) (model *R) {
func (c Crud[R]) First(ctx ctx, where any, order any, with string) (model *R) {
model = new(R)
db := c.Dao.Ctx(ctx).Model(model)
if with {
db = db.Preload("*")
if with != "" {
db = db.Preload(with)
}
if where != nil {
db = db.Where(where)
@@ -424,11 +424,11 @@ func (c Crud[R]) Exists(ctx ctx, where any) (exists bool) {
}
// -------------------------- 原All对应实现:查询所有符合条件的记录 --------------------------
func (c Crud[R]) All(ctx ctx, where any, order any, with bool) (items []*R) {
func (c Crud[R]) All(ctx ctx, where any, order any, with string) (items []*R) {
db := c.Dao.Ctx(ctx).Model(new(R))
if with {
db = db.Preload("*")
if with != "" {
db = db.Preload(with)
}
if where != nil {
db = db.Where(where)
@@ -487,7 +487,7 @@ func (c Crud[R]) UpdatePri(ctx ctx, primaryKey any, data any) (count int64) {
}
// -------------------------- 原Paginate对应实现:分页查询 --------------------------
func (c Crud[R]) Paginate(ctx context.Context, where any, p Paginate, with bool, order any) (items []*R, total int64) {
func (c Crud[R]) Paginate(ctx context.Context, where any, p Paginate, with string, order any) (items []*R, total int64) {
db := c.Dao.Ctx(ctx).Model(new(R))
// 1. 构建查询条件
@@ -501,8 +501,8 @@ func (c Crud[R]) Paginate(ctx context.Context, where any, p Paginate, with bool,
}
// 3. 关联查询
if with {
db = db.Preload("*")
if with != "" {
db = db.Preload(with)
}
// 4. 排序