From eee3526c6dfdc0196a1ad9a7d0a6c85d8f4c897c Mon Sep 17 00:00:00 2001 From: cobb Date: Wed, 13 Apr 2022 09:51:52 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BD=BF=E7=94=A8BaseModelFunc=E6=A8=A1?= =?UTF-8?q?=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/components/HelloWorld.vue | 54 +++++++++-------------------------- 1 file changed, 13 insertions(+), 41 deletions(-) diff --git a/src/components/HelloWorld.vue b/src/components/HelloWorld.vue index ce4a210..3c36185 100644 --- a/src/components/HelloWorld.vue +++ b/src/components/HelloWorld.vue @@ -448,14 +448,17 @@ export default { } type ${tableNameLower}Q struct { + BaseModelFunc Repo *${tableName}Repo opts []GormOptionFunc } func (obj *${tableName}Repo) Q() *${tableNameLower}Q { - return &${tableNameLower}Q{ - Repo: obj, - } + q := &${tableNameLower}Q{ + Repo: obj, + } + q.BaseModelFunc = obj.PreTableName + return q } // List 查询列表 @@ -498,39 +501,10 @@ export default { }) } - func (obj *${tableNameLower}Q) Select(strings ...string) *${tableNameLower}Q { - fn := func(db *gorm.DB) *gorm.DB { - if len(strings) > 0 { - var ss []string - for _, s := range strings { - ss = append(ss, obj.Repo.PreTableName(s)) - } - db = db.Select(ss) - } - return db - } - obj.opts = append(obj.opts, fn) - return obj - } - - func (obj *${tableNameLower}Q) Pagination(para *${tableName}ReqParams) *${tableNameLower}Q { - fn := func(db *gorm.DB) *gorm.DB { - if para.PageNum > 0 && para.PageSize > 0 { - if para.PageSize > 100 { - // 不允许大于100量 - db = db.Limit(100) - } else { - db = db.Limit(para.PageSize).Offset((para.PageNum - 1) * para.PageSize) - } - } else if !para.Export { - // 非导出,也是限制 100 - db = db.Limit(100) - } - return db - } - obj.opts = append(obj.opts, fn) - return obj - } + func (p *${tableName}ReqParams) PageNumF() int { return p.PageNum } + func (p *${tableName}ReqParams) PageSizeF() int { return p.PageSize } + func (p *${tableName}ReqParams) FieldsF() []string { return p.Fields } + func (p *${tableName}ReqParams) ExportF() bool { return p.Export } type ${tableName}ReqParams struct { Query *${tableName}Params \`json:"query,omitempty"\` @@ -547,12 +521,12 @@ export default { var filterHeader = ` func (opt *${tableNameLower}Q) Filter(para *${tableName}ReqParams) *${tableNameLower}Q { if para != nil { - opt.Select(para.Fields...) if para.Export { para.PageNum = 1 para.PageSize = 10000 } - opt.Pagination(para) + opt.opts = append(opt.opts, opt.Select(para.Fields...)) + opt.opts = append(opt.opts, opt.Pagination(para)) if para.Query != nil { `; var filtercenter = ``; @@ -584,9 +558,7 @@ export default { ${tableKey}${value} ${tableKey} = ${v} \n `; var f = ` - func (g ${tableKey}) ${value}() bool { - return g == ${tableKey}${value} - } \n + func (g ${tableKey}) ${value}() bool { return g == ${tableKey}${value} } \n func (obj *${tableName}Repo) ${value}(ctx context.Context, id int64) error { return obj.Updates(ctx, &${tableName}{${skey}: ${tableKey}${value}}, obj.Q().${primary}(id)) } \n