@ -316,6 +316,7 @@ export default {
$ { tableName } Service * service . $ { tableName } Service
}
/ / C r e a t e $ { t a b l e N a m e } 创 建 $ { t a b l e N a m e }
func ( l * $ { tableName } Api ) Create$ { tableName } ( c * gin . Context ) {
req : = & model . $ { tableName } { }
if err : = ginplus . ParseJSON ( c , req ) ; err != nil {
@ -330,6 +331,7 @@ export default {
ginplus . ResOK ( c )
}
/ / G e t $ { t a b l e N a m e } O n e 获 取 单 个 $ { t a b l e N a m e }
func ( l * $ { tableName } Api ) Get$ { tableName } One ( c * gin . Context ) {
req : = & schema . IDResult { }
if err : = ginplus . ParseQuery ( c , req ) ; err != nil {
@ -344,6 +346,7 @@ export default {
ginplus . ResSuccess ( c , resp )
}
/ / L i s t $ { t a b l e N a m e } 拉 取 $ { t a b l e N a m e } 列 表
func ( l * $ { tableName } Api ) List$ { tableName } ( c * gin . Context ) {
req : = & model . $ { tableName } ReqParams { }
if err : = ginplus . ParseQuery ( c , req ) ; err != nil {
@ -356,15 +359,19 @@ export default {
return
}
if req . Export {
f , _ : = excelize . Excelize ( resp ,
f , err : = excelize . Excelize ( resp ,
excelize . ColumnFilter ( "create_time" , excelize . FilterTimestamp ) ,
)
if err != nil {
return ginplus . ResError ( c , err )
}
ginplus . ResExcel ( c , f , "导出信息列表" )
return
}
ginplus . ResPage ( c , resp )
}
/ / D e l e t e $ { t a b l e N a m e } 删 除 $ { t a b l e N a m e }
func ( l * $ { tableName } Api ) Delete$ { tableName } ( c * gin . Context ) {
var req schema . IDResult
if err : = ginplus . ParseQuery ( c , & req ) ; err != nil {
@ -410,6 +417,7 @@ export default {
$ { tableName } * model . $ { tableName } Repo
}
/ / G e t $ { t a b l e N a m e } L i s t 获 取 $ { t a b l e N a m e } 列 表
func ( a * $ { tableName } Service ) Get$ { tableName } List ( ctx context . Context , req * model . $ { tableName } ReqParams ) ( * schema . PaginationResult , error ) {
var l [ ] * model . $ { tableName }
cnt , err : = a . $ { tableName } . Q ( ) . Filter ( req ) . List ( ctx , & l )
@ -426,6 +434,7 @@ export default {
return pagination , nil
}
/ / G e t $ { t a b l e N a m e } 获 取 单 个 $ { t a b l e N a m e }
func ( a * $ { tableName } Service ) Get$ { tableName } One ( ctx context . Context , $ { primaryLower } int64 ) ( interface { } , error ) {
var o model . $ { tableName }
err : = a . $ { tableName } . Q ( ) . $ { primary } ( $ { primaryLower } ) . One ( ctx , & o )
@ -435,6 +444,7 @@ export default {
return o , nil
}
/ / D e l $ { t a b l e N a m e } 删 除 $ { t a b l e N a m e }
func ( a * $ { tableName } Service ) Del$ { tableName } ( ctx context . Context , $ { primaryLower } int64 ) error {
var o model . $ { tableName }
err : = a . $ { tableName } . Q ( ) . $ { primary } ( $ { primaryLower } ) . One ( ctx , & o )
@ -444,6 +454,7 @@ export default {
return a . $ { tableName } . Delete ( ctx , o . $ { primary } )
}
/ / C r e a t e $ { t a b l e N a m e } 创 建 $ { t a b l e N a m e }
func ( a * $ { tableName } Service ) Create$ { tableName } ( ctx context . Context , r * model . $ { tableName } ) error {
if r . $ { primary } == 0 {
_ , err : = a . $ { tableName } . Create ( ctx , r )
@ -502,44 +513,69 @@ export default {
}
/ / G e t T a b l e N a m e g e t s q l t a b l e n a m e . 获 取 数 据 库 名 字
func ( obj * $ { tableName } Repo ) TableName ( ) string {
func ( rep o * $ { tableName } Repo ) TableName ( ) string {
return "${v.title}"
}
func ( obj * $ { tableName } Repo ) PreTableName ( s string ) string {
/ / P r e T a b l e N a m e 获 取 表 名 前 缀
func ( repo * $ { tableName } Repo ) PreTableName ( s string ) string {
b : = strings . Builder { }
b . WriteString ( obj . TableName ( ) )
b . WriteString ( rep o. TableName ( ) )
b . WriteString ( "." )
b . WriteString ( s )
return b . String ( )
}
/ / D e l e t e B y I D
func ( obj * $ { tableName } Repo ) Delete ( ctx context . Context , $ { primaryLower } int64 ) error {
err : = obj . DB . WithContext ( ctx ) . Delete ( & $ { tableName } { } , $ { primaryLower } ) . Error
func ( rep o * $ { tableName } Repo ) Delete ( ctx context . Context , $ { primaryLower } int64 ) error {
err : = rep o. DB . WithContext ( ctx ) . Delete ( & $ { tableName } { } , $ { primaryLower } ) . Error
return err
}
/ / C a c h e K e y $ { p r i m a r y } C a c h e K e y g e n e r a t e b y i d s
func ( obj * $ { tableName } Repo ) CacheKey$ { primary } ( $ { primaryLower } int64 ) string {
return strings . Join ( [ ] string { obj . TableName ( ) , "${primaryLower}" , utils . AsString ( $ { primaryLower } ) } , "_" )
func ( rep o * $ { tableName } Repo ) CacheKey$ { primary } ( $ { primaryLower } int64 ) string {
return strings . Join ( [ ] string { rep o. TableName ( ) , "${primaryLower}" , utils . AsString ( $ { primaryLower } ) } , "_" )
}
/ / U p d a t e s 更 新
func ( obj * $ { tableName } Repo ) Updates ( ctx context . Context , input * $ { tableName } , q * $ { tableNameLower } Q ) error {
func ( rep o * $ { 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 {
key = utils . AsString ( k )
}
return obj . Cache . Exec ( ctx , key , func ( ctx context . Context ) error {
return rep o. Cache . Exec ( ctx , key , func ( ctx context . Context ) error {
return Q . Updates ( * input ) . Error
} )
}
/ / S a v e 更 新 , 相 比 U p d a t e s 会 置 空 没 有 的 字 段
func ( repo * $ { tableName } Repo ) Save ( 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 repo . Cache . Exec ( ctx , key , func ( ctx context . Context ) error {
return Q . Save ( * input ) . Error
} )
}
/ / u p d a t e C o l u m n 更 新
func ( repo * $ { tableName } Repo ) updateColumn ( ctx context . Context , column string , value interface { } , q * $ { tableNameLower } Q ) error {
Q : = q . Query ( ctx )
key : = ""
if k , _ : = Q . InstanceGet ( "cache_key" ) ; k != nil {
key = utils . AsString ( k )
}
return repo . Cache . Exec ( ctx , key , func ( ctx context . Context ) error {
return Q . UpdateColumn ( column , value ) . Error
} )
}
/ / C r e a t e 创 建
func ( obj * $ { tableName } Repo ) Create ( ctx context . Context , input * $ { tableName } ) ( * $ { tableName } , error ) {
if err : = obj . DB . WithContext ( ctx ) . Create ( input ) . Error ; err != nil {
func ( rep o * $ { tableName } Repo ) Create ( ctx context . Context , input * $ { tableName } ) ( * $ { tableName } , error ) {
if err : = rep o. DB . WithContext ( ctx ) . Create ( input ) . Error ; err != nil {
return nil , err
}
return input , nil
@ -551,31 +587,31 @@ export default {
opts [ ] GormOptionFunc
}
func ( obj * $ { tableName } Repo ) Q ( ) * $ { tableNameLower } Q {
func ( rep o * $ { tableName } Repo ) Q ( ) * $ { tableNameLower } Q {
q : = & $ { tableNameLower } Q {
Repo : obj ,
Repo : rep o,
}
q . BaseModelFunc = obj . PreTableName
q . BaseModelFunc = rep o. PreTableName
return q
}
/ / L i s t 查 询 列 表
func ( obj * $ { tableNameLower } Q ) List ( ctx context . Context , value interface { } ) ( int64 , error ) {
func ( q * $ { tableNameLower } Q ) List ( ctx context . Context , value interface { } ) ( int64 , error ) {
var cnt int64
err : = obj . Query ( ctx ) . Order ( "${primaryLower} desc" ) . Find ( value ) . Offset ( - 1 ) . Count ( & cnt ) . Error
err : = q . Query ( ctx ) . Order ( "${primaryLower} desc" ) . Find ( value ) . Offset ( - 1 ) . Count ( & cnt ) . Error
return cnt , err
}
/ / O n e 查 询 单 个
func ( obj * $ { tableNameLower } Q ) One ( ctx context . Context , value interface { } ) error {
Q : = obj . Query ( ctx )
func ( q * $ { tableNameLower } Q ) One ( ctx context . Context , value interface { } ) error {
Q : = q . Query ( ctx )
cache_key : = ""
/ / 缓 存 只 适 用 于 单 行 全 列 的 情 况
if k , _ : = Q . InstanceGet ( "cache_key" ) ; len ( obj . opts ) == 1 && k != nil {
if k , _ : = Q . InstanceGet ( "cache_key" ) ; len ( q . opts ) == 1 && k != nil {
cache_key = utils . AsString ( k )
Q = Q . Select ( "*" )
}
return obj . Repo . Cache . Query ( ctx , cache_key , value , func ( ctx context . Context ) error {
return q . 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
@ -584,17 +620,18 @@ export default {
} )
}
func ( obj * $ { tableNameLower } Q ) Query ( ctx context . Context ) * gorm . DB {
db : = obj . Repo . DB . WithContext ( ctx ) . Model ( & $ { tableName } { } )
for _ , f : = range obj . opts {
/ / Q u e r y 内 部 查 询 命 令
func ( q * $ { tableNameLower } Q ) Query ( ctx context . Context ) * gorm . DB {
db : = q . Repo . DB . WithContext ( ctx ) . Model ( & $ { tableName } { } )
for _ , f : = range q . opts {
db = f ( db )
}
return db
}
func ( obj * $ { tableNameLower } Q ) where ( key string , value interface { } ) {
obj . opts = append ( obj . opts , func ( db * gorm . DB ) * gorm . DB {
db = db . Where ( obj . Repo . PreTableName ( key ) , value )
func ( q * $ { tableNameLower } Q ) where ( key string , value interface { } ) {
q . opts = append ( q . opts , func ( db * gorm . DB ) * gorm . DB {
db = db . Where ( q . Repo . PreTableName ( key ) , value )
return db
} )
}
@ -605,19 +642,18 @@ export default {
func ( p * $ { tableName } ReqParams ) GetExport ( ) bool { return p . Export }
type $ { tableName } ReqParams struct {
Query * $ { tableName } Params \ ` json:"query,omitempty" form:"inner_query" \`
Export bool \ ` json:"export,omitempty" form:"export" \`
For Query string \ ` json:"export ,omitempty" form:"query" \`
Query string \ ` json:"query ,omitempty" form:"query" \`
Fields [ ] string \ ` json:"fields,omitempty" form:"fields" \`
PageNum int \ ` json:"pageNum,omitempty" form:"pageNum" \`
PageSize int \ ` json:"pageSize,omitempty" form:"pageSize" \`
}
type $ { tableName } Params struct { \ n ` ;
` ;
var center = ` ` ;
var paramstail = ` } ` ;
var optCommon = ` ` ;
var filterHeader = `
/ / F i l t e r 自 动 组 合 查 询
func ( opt * $ { tableNameLower } Q ) Filter ( para * $ { tableName } ReqParams ) * $ { tableNameLower } Q {
if para != nil {
if para . Export {
@ -626,10 +662,9 @@ export default {
}
opt . opts = append ( opt . opts , opt . Select ( para . Fields ... ) )
opt . opts = append ( opt . opts , opt . Pagination ( para ) )
if para . Query != nil {
` ;
var filtercenter = ` ` ;
var filtertail = ` }}
var filtertail = ` }
return opt } ` ;
Object . keys ( properties ) . forEach ( function ( key ) {
@ -659,8 +694,8 @@ export default {
` ;
var f = `
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 ) )
func ( rep o * $ { tableName } Repo ) $ { value } ( ctx context . Context , id int64 ) error {
return repo . updateColumn ( ctx , "${key}" , $ { tableKey } $ { value } , rep o. Q ( ) . $ { primary } ( id ) )
} \ n
` ;
enumcenter += s ;
@ -681,7 +716,7 @@ export default {
}
` ;
optCommon += `
func ( obj * $ { tableNameLower } Q ) $ { skey } Interval ( interval [ ] interface { } ) * $ { tableNameLower } Q {
func ( q * $ { tableNameLower } Q ) $ { skey } Interval ( interval [ ] interface { } ) * $ { tableNameLower } Q {
fn : = func ( db * gorm . DB ) * gorm . DB {
db = db . Clauses (
TimeStampDate {
@ -690,8 +725,8 @@ export default {
} )
return db
}
obj . opts = append ( obj . opts , fn )
return obj
q . opts = append ( q . opts , fn )
return q
}
\ n
` ;
@ -705,14 +740,14 @@ export default {
center += ` ${ skey } In [] ${ type } \` json:" ${ caseKey } |in,omitempty" form:" ${ caseKey } |in" \` \ n ` ;
}
filtercenter += `
if len ( para . Query . $ { skey } In ) > 0 {
opt . $ { skey } In ( para . Query . $ { skey } In ... )
if len ( para . $ { skey } In ) > 0 {
opt . $ { skey } In ( para . $ { skey } In ... )
}
` ;
optCommon += `
func ( obj * $ { tableNameLower } Q ) $ { skey } In ( $ { skey } s ... $ { type } ) * $ { tableNameLower } Q {
obj . where ( "${key} in (?)" , $ { skey } s )
return obj
func ( q * $ { tableNameLower } Q ) $ { skey } In ( $ { skey } s ... $ { type } ) * $ { tableNameLower } Q {
q . where ( "${key} in (?)" , $ { skey } s )
return q
}
\ n
` ;
@ -726,22 +761,22 @@ export default {
}
if ( type == "string" ) {
filtercenter += `
if para . Query . $ { skey } != "" {
opt . $ { skey } ( para . Query . $ { skey } )
if para . $ { skey } != "" {
opt . $ { skey } ( para . $ { skey } )
}
` ;
} else if ( key !== "id" ) {
/ / i d 过 滤 掉 , 不 能 生 成 , L i s t 里 面 可 以 查 询 I D 会 有 风 险
filtercenter += `
if para . Query . $ { skey } != 0 {
opt . $ { skey } ( para . Query . $ { skey } )
if para . $ { skey } != 0 {
opt . $ { skey } ( para . $ { skey } )
}
` ;
}
optCommon += `
func ( obj * $ { tableNameLower } Q ) $ { skey } ( $ { skey } $ { type } ) * $ { tableNameLower } Q {
obj . where ( "${key} = ?" , $ { skey } )
return obj
func ( q * $ { tableNameLower } Q ) $ { skey } ( $ { skey } $ { type } ) * $ { tableNameLower } Q {
q . where ( "${key} = ?" , $ { skey } )
return q
}
\ n
` ;
@ -752,14 +787,14 @@ export default {
center += ` ${ skey } Like ${ type } \` json:" ${ caseKey } |like,omitempty" form:" ${ caseKey } |like" \` \ n ` ;
}
filtercenter += `
if para . Query . $ { skey } Like != "" {
opt . $ { skey } Like ( para . Query . $ { skey } Like )
if para . $ { skey } Like != "" {
opt . $ { skey } Like ( para . $ { skey } Like )
}
` ;
optCommon += `
func ( obj * $ { tableNameLower } Q ) $ { skey } Like ( $ { skey } $ { type } ) * $ { tableNameLower } Q {
obj . where ( "${key} like ?" , "%" + $ { skey } + "%" )
return obj
func ( q * $ { tableNameLower } Q ) $ { skey } Like ( $ { skey } $ { type } ) * $ { tableNameLower } Q {
q . where ( "${key} like ?" , "%" + $ { skey } + "%" )
return q
}
\ n
` ;