Browse Source

使用BaseModelFunc模式

main
cobb 4 years ago
parent
commit
eee3526c6d
  1. 50
      src/components/HelloWorld.vue

50
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{
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

Loading…
Cancel
Save