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