From 35f32a19ef9d513da4c85893ecec837c2691b8cb Mon Sep 17 00:00:00 2001 From: root Date: Wed, 12 May 2021 09:18:39 +0800 Subject: [PATCH] add mgr src --- src/components/HelloWorld.vue | 68 ++++++++++++++++++++++++++++++----- 1 file changed, 60 insertions(+), 8 deletions(-) diff --git a/src/components/HelloWorld.vue b/src/components/HelloWorld.vue index 22c6c20..af0654b 100644 --- a/src/components/HelloWorld.vue +++ b/src/components/HelloWorld.vue @@ -147,9 +147,69 @@ export default { const properties = v.properties; const tableName = toCamelCase(v.title); const tableNameLower = lowerCase(tableName); + var primary = ``; + var primaryLower = lowerCase(primary) + Object.keys(properties).forEach(function (key) { + const o = properties[key]; + const skey = toCamelCase(key); + if (Object.prototype.hasOwnProperty.call(o, "$comment")) { + primary = skey; + } + }); var enumUpdate = ``; var enumdefine = ``; var paramsheader = ` + var ${tableName}Set = wire.NewSet(wire.Struct(new(${tableName}Mgr), "*")) + + type ${tableName}Mgr struct { + DB *gorm.DB + Cache cache.Cache + } + + // GetTableName get sql table name.获取数据库名字 + func (obj *${tableName}Mgr) TableName() string { + return "${v.title}" + } + + func (obj *${tableName}Mgr) PreTableName(s string) string { + b := strings.Builder{} + b.WriteString(obj.TableName()) + b.WriteString(".") + b.WriteString(s) + return b.String() + } + + // Delete By ID + func (obj *${tableName}Mgr) Delete(ctx context.Context, ${primaryLower} int64) error { + err := obj.DB.WithContext(ctx).Delete(&${tableName}{}, ${primaryLower}).Error + return err + } + + // CacheKey${primary} CacheKey generate by ids + func (obj *${tableName}Mgr) CacheKey${primary}(${primaryLower} int64) string { + return strings.Join([]string{obj.TableName(), "${primaryLower}", utils.AsString(${primaryLower})}, "_") + } + + // Updates 更新 + func (obj *${tableName}Mgr) Updates(ctx context.Context, input *${tableName}, q *${tableNameLower}Q) error { + Q := q.Query(ctx) + key := "" + if k, _ := Q.InstanceGet("cache_key"); k != nil { + key = utils.AsString(k) + } + return obj.Cache.Exec(ctx, key, func(ctx context.Context) error { + return Q.Updates(*input).Error + }) + } + + // Create 创建 + func (obj *${tableName}Mgr) Create(ctx context.Context, input *${tableName}) (*${tableName}, error) { + if err := obj.DB.WithContext(ctx).Create(input).Error; err != nil { + return nil, err + } + return input, nil + } + type ${tableNameLower}Q struct { Mgr *${tableName}Mgr opts []GormOptionFunc @@ -233,14 +293,6 @@ export default { var filtertail = `}} return opt}`; - var primary = ``; - Object.keys(properties).forEach(function (key) { - const o = properties[key]; - const skey = toCamelCase(key); - if (Object.prototype.hasOwnProperty.call(o, "$comment")) { - primary = skey; - } - }); Object.keys(properties).forEach(function (key) { const o = properties[key];