diff --git a/src/components/HelloWorld.vue b/src/components/HelloWorld.vue index cbb45df..7e5a2c9 100644 --- a/src/components/HelloWorld.vue +++ b/src/components/HelloWorld.vue @@ -159,19 +159,19 @@ export default { var enumUpdate = ``; var enumdefine = ``; var paramsheader = ` - var ${tableName}Set = wire.NewSet(wire.Struct(new(${tableName}Mgr), "*")) + var ${tableName}Set = wire.NewSet(wire.Struct(new(${tableName}Repo), "*")) - type ${tableName}Mgr struct { + type ${tableName}Repo struct { DB *gorm.DB Cache cache.Cache } // GetTableName get sql table name.获取数据库名字 - func (obj *${tableName}Mgr) TableName() string { + func (obj *${tableName}Repo) TableName() string { return "${v.title}" } - func (obj *${tableName}Mgr) PreTableName(s string) string { + func (obj *${tableName}Repo) PreTableName(s string) string { b := strings.Builder{} b.WriteString(obj.TableName()) b.WriteString(".") @@ -180,18 +180,18 @@ export default { } // Delete By ID - func (obj *${tableName}Mgr) Delete(ctx context.Context, ${primaryLower} int64) error { + func (obj *${tableName}Repo) 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 { + func (obj *${tableName}Repo) 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 { + func (obj *${tableName}Repo) Updates(ctx context.Context, input *${tableName}, q *${tableNameLower}Q) error { Q := q.Query(ctx) key := "" if k, _ := Q.InstanceGet("cache_key"); k != nil { @@ -203,7 +203,7 @@ export default { } // Create 创建 - func (obj *${tableName}Mgr) Create(ctx context.Context, input *${tableName}) (*${tableName}, error) { + func (obj *${tableName}Repo) Create(ctx context.Context, input *${tableName}) (*${tableName}, error) { if err := obj.DB.WithContext(ctx).Create(input).Error; err != nil { return nil, err } @@ -211,13 +211,13 @@ export default { } type ${tableNameLower}Q struct { - Mgr *${tableName}Mgr + Repo *${tableName}Repo opts []GormOptionFunc } - func (obj *${tableName}Mgr) Q() *${tableNameLower}Q { + func (obj *${tableName}Repo) Q() *${tableNameLower}Q { return &${tableNameLower}Q{ - Mgr: obj, + Repo: obj, } } @@ -237,7 +237,7 @@ export default { cache_key = utils.AsString(k) Q = Q.Select("*") } - return obj.Mgr.Cache.Query(ctx, cache_key, value, func(ctx context.Context) error { + return obj.Repo.Cache.Query(ctx, cache_key, value, func(ctx context.Context) error { err := Q.First(value).Error if errors.Is(err, gorm.ErrRecordNotFound) { return errors.ErrIdCanNotFound @@ -247,7 +247,7 @@ export default { } func (obj *${tableNameLower}Q) Query(ctx context.Context) *gorm.DB { - db := obj.Mgr.DB.WithContext(ctx).Model(&${tableName}{}) + db := obj.Repo.DB.WithContext(ctx).Model(&${tableName}{}) for _, f := range obj.opts { db = f(db) } @@ -259,7 +259,7 @@ export default { if len(strings) > 0 { var ss []string for _, s := range strings { - ss = append(ss, obj.Mgr.PreTableName(s)) + ss = append(ss, obj.Repo.PreTableName(s)) } db = db.Select(ss) } @@ -337,7 +337,7 @@ export default { func (g ${skey}) ${value}() bool { return g == ${skey}${value} } \n - func (obj *${tableName}Mgr) ${value}(ctx context.Context, id int64) error { + func (obj *${tableName}Repo) ${value}(ctx context.Context, id int64) error { return obj.Updates(ctx, &${tableName}{${skey}: ${skey}${value}}, obj.Q().${primary}(id)) } \n `; @@ -382,7 +382,7 @@ export default { optCommon += ` func (obj *${tableNameLower}Q) ${skey}In(${skey}s ...${type}) *${tableNameLower}Q { fn := func(db *gorm.DB) *gorm.DB { - db = db.Where(obj.Mgr.PreTableName("${key} in (?)"), ${skey}s) + db = db.Where(obj.Repo.PreTableName("${key} in (?)"), ${skey}s) return db } obj.opts = append(obj.opts, fn) @@ -410,7 +410,7 @@ export default { optCommon += ` func (obj *${tableNameLower}Q) ${skey}(${skey} ${type}) *${tableNameLower}Q { fn := func(db *gorm.DB) *gorm.DB { - db = db.Where(obj.Mgr.PreTableName("${key} = ?"), ${skey}) + db = db.Where(obj.Repo.PreTableName("${key} = ?"), ${skey}) return db } obj.opts = append(obj.opts, fn) @@ -428,7 +428,7 @@ export default { optCommon += ` func (obj *${tableNameLower}Q) ${skey}Like(${skey} ${type}) *${tableNameLower}Q { fn := func(db *gorm.DB) *gorm.DB { - db = db.Where(obj.Mgr.PreTableName("${key} like ?"), "%"+${skey}+"%") + db = db.Where(obj.Repo.PreTableName("${key} like ?"), "%"+${skey}+"%") return db } obj.opts = append(obj.opts, fn)