From 77499b50cc999313e295c9539e8fe659761177df Mon Sep 17 00:00:00 2001 From: zhangwenjian Date: Sat, 29 Aug 2020 16:32:59 +0800 Subject: [PATCH] update:update gen code temp --- apis/sysjob/sysjob.go | 4 +- apis/system/dict/dictdata.go | 2 +- go.mod | 2 +- go.sum | 2 + template/api.go.template | 19 ++----- template/model.go.template | 107 +++++++++++++++++------------------ 6 files changed, 63 insertions(+), 73 deletions(-) diff --git a/apis/sysjob/sysjob.go b/apis/sysjob/sysjob.go index 3ef5567b..18e495da 100644 --- a/apis/sysjob/sysjob.go +++ b/apis/sysjob/sysjob.go @@ -20,10 +20,10 @@ func GetSysJobList(c *gin.Context) { var pageIndex = 1 if size := c.Request.FormValue("pageSize"); size != "" { - pageSize = tools.StrToInt(err, size) + pageSize,err = tools.StringToInt(size) } if index := c.Request.FormValue("pageIndex"); index != "" { - pageIndex = tools.StrToInt(err, index) + pageIndex,err = tools.StringToInt(index) } var v dto.SysJobSearch diff --git a/apis/system/dict/dictdata.go b/apis/system/dict/dictdata.go index 37127b10..b9d2552b 100644 --- a/apis/system/dict/dictdata.go +++ b/apis/system/dict/dictdata.go @@ -105,7 +105,7 @@ func GetDictDataByDictType(c *gin.Context) { // @Security Bearer func InsertDictData(c *gin.Context) { var data models.DictData - err := c.BindWith(&data, binding.JSON) + err := c.ShouldBindJSON(&data) data.CreateBy = tools.GetUserIdStr(c) tools.HasError(err, "", 500) result, err := data.Create() diff --git a/go.mod b/go.mod index 5ba0e9f3..ebe51d77 100644 --- a/go.mod +++ b/go.mod @@ -15,8 +15,8 @@ require ( github.com/gogf/gf v1.13.4 github.com/google/uuid v1.1.1 github.com/gorilla/websocket v1.4.2 - github.com/matchstalk/redisqueue v1.0.3 github.com/matchstalk/go-admin-core v1.0.2 + github.com/matchstalk/redisqueue v1.0.3 github.com/mojocn/base64Captcha v1.3.1 github.com/mssola/user_agent v0.5.2 github.com/pkg/errors v0.9.1 diff --git a/go.sum b/go.sum index 102b8daf..29a7cf7d 100644 --- a/go.sum +++ b/go.sum @@ -328,6 +328,7 @@ github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHX github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU= github.com/mattn/go-runewidth v0.0.9 h1:Lm995f3rfxdpd6TSmuVCHVb/QhupuXlYr8sCI/QdE+0= github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI= +github.com/mattn/go-sqlite3 v1.14.0 h1:mLyGNKR8+Vv9CAU7PphKa2hkEqxxhn8i32J6FPj1/QA= github.com/mattn/go-sqlite3 v1.14.0/go.mod h1:JIl7NbARA7phWnGvh0LKTyg7S9BA+6gx71ShQilpsus= github.com/mattn/goveralls v0.0.5 h1:spfq8AyZ0cCk57Za6/juJ5btQxeE1FaEGMdfcI+XO48= github.com/mattn/goveralls v0.0.5/go.mod h1:Xg2LHi51faXLyKXwsndxiW6uxEEQT9+3sjGzzwU4xy0= @@ -676,6 +677,7 @@ gorm.io/driver/mysql v0.3.2/go.mod h1:jC/HyPAZJ0xqfGlOqYybd8oNPMtGrkNgvwjWuKDT7f gorm.io/driver/postgres v0.2.6/go.mod h1:AsPyuhKFOplSmQwOPsycVKbe0dRxF8v18KZ7p9i8dIs= gorm.io/driver/postgres v0.2.9 h1:oMY4f4tLEUVg3eapqHFzVEplWKy1Gj1cGBEe9wJ1yWM= gorm.io/driver/postgres v0.2.9/go.mod h1:YATpayFLKALuiVXVRNbDfoEJwECHSMYzl1SmXJ2+CIg= +gorm.io/driver/sqlite v1.0.9 h1:rqOPFRPY3U5fL5AZSDVa9Np+XpkUbxLVmvpG+i1VHjg= gorm.io/driver/sqlite v1.0.9/go.mod h1:xkm8/CEmA3yc4zRd0pdCqm43BjO8Hm6avfTpxWb/7c4= gorm.io/driver/sqlserver v0.2.4 h1:AGofGL/TfzTZotzIHlaLISfxEKJpzj0ATbtXJW+ga1A= gorm.io/driver/sqlserver v0.2.4/go.mod h1:TcPfkdce5b8qlCMgyUeUdm7HQa1ZzWUuxzI+odcueLA= diff --git a/template/api.go.template b/template/api.go.template index d9b2faac..766a0e77 100644 --- a/template/api.go.template +++ b/template/api.go.template @@ -17,25 +17,18 @@ func Get{{.ClassName}}List(c *gin.Context) { var pageIndex = 1 if size := c.Request.FormValue("pageSize"); size != "" { - pageSize = tools.StrToInt(err, size) + pageSize, err = tools.StringToInt(size) } if index := c.Request.FormValue("pageIndex"); index != "" { - pageIndex = tools.StrToInt(err, index) + pageIndex, err = tools.StringToInt(index) } - {{ range .Columns -}} - {{$z := .IsQuery}} - {{- if ($z) -}} - {{ if eq (.GoType) "int" -}} - data.{{.GoField}}, _ = tools.StringToInt(c.Request.FormValue("{{.JsonField}}")) - {{ else -}} - data.{{.GoField}} = c.Request.FormValue("{{.JsonField}}") - {{ end }} - {{- end -}} - {{end}} + var v dto.{{.ClassName}}Search + err = c.Bind(&v) + tools.HasError(err, "数据解析失败", 422) data.DataScope = tools.GetUserIdStr(c) - result, count, err := data.GetPage(pageSize, pageIndex) + result, count, err := data.GetPage(pageSize, pageIndex, v) tools.HasError(err, "", -1) app.PageOK(c, result, count, pageIndex, pageSize, "") diff --git a/template/model.go.template b/template/model.go.template index 532e0c6e..a7bf67c4 100644 --- a/template/model.go.template +++ b/template/model.go.template @@ -1,96 +1,91 @@ package models import ( -orm "go-admin/global" -"go-admin/tools" -{{$ii := 1}} -{{ range .Columns }} - {{- if eq .GoType "time.Time" -}}{{- if eq $ii 1 -}} "time" {{$ii = 2}}{{- end}}{{- end}} -{{- end }} + orm "go-admin/global" + "go-admin/tools" + {{ range .Columns }} + {{- if eq .GoField "CreatedAt" -}} + {{- else if eq .GoField "UpdatedAt" -}} + {{- else if eq .GoField "DeletedAt" -}} + {{- else if eq .GoType "time.Time" -}}"time"{{- end}} + {{- end }} ) type {{.ClassName}} struct { -{{ range .Columns -}} + {{ range .Columns -}} {{$x := .Pk}} {{- if ($x) }} - {{.GoField}} {{.GoType}} `json:"{{.JsonField}}" gorm:"type:{{.ColumnType}};primary_key"` // {{.ColumnComment}} + {{.GoField}} {{.GoType}} `json:"{{.JsonField}}" gorm:"type:{{.ColumnType}};primary_key"` // {{.ColumnComment}} {{- else if eq .GoField "CreatedAt" -}} {{- else if eq .GoField "UpdatedAt" -}} {{- else if eq .GoField "DeletedAt" -}} {{- else }} - {{.GoField}} {{.GoType}} `json:"{{.JsonField}}" gorm:"type:{{.ColumnType}};"` // {{.ColumnComment}}{{end -}} -{{- end }} -DataScope string `json:"dataScope" gorm:"-"` -Params string `json:"params" gorm:"-"` -BaseModel + {{.GoField}} {{.GoType}} `json:"{{.JsonField}}" gorm:"type:{{.ColumnType}};"` // {{.ColumnComment}}{{end -}} + {{- end }} + DataScope string `json:"dataScope" gorm:"-"` + Params string `json:"params" gorm:"-"` + BaseModel } func ({{.ClassName}}) TableName() string { -return "{{.TBName}}" + return "{{.TBName}}" } // 创建{{.ClassName}} func (e *{{.ClassName}}) Create() ({{.ClassName}}, error) { -var doc {{.ClassName}} -result := orm.Eloquent.Table(e.TableName()).Create(&e) -if result.Error != nil { -err := result.Error -return doc, err -} -doc = *e -return doc, nil + var doc {{.ClassName}} + result := orm.Eloquent.Table(e.TableName()).Create(&e) + if result.Error != nil { + err := result.Error + return doc, err + } + doc = *e + return doc, nil } // 获取{{.ClassName}} func (e *{{.ClassName}}) Get() ({{.ClassName}}, error) { -var doc {{.ClassName}} -table := orm.Eloquent.Table(e.TableName()) -{{ range .Columns }} + var doc {{.ClassName}} + table := orm.Eloquent.Table(e.TableName()) + {{ range .Columns }} {{$x := .Pk}} {{- if ($x) }} - if e.{{.GoField}} != {{if eq .GoType "string" -}} "" {{ else if eq .GoType "int" -}} 0 {{- end}} { + if e.{{.GoField}} != {{if eq .GoType "string" -}} "" {{ else if eq .GoType "int" -}} 0 {{- end}} { table = table.Where("{{.ColumnName}}{{if eq .QueryType "EQ"}} = {{else if eq .QueryType "NE"}} != {{else if eq .QueryType "GT"}} > {{else if eq .QueryType "GTE"}} >= {{else if eq .QueryType "LT"}} < {{else if eq .QueryType "LTE"}} <= {{else if eq .QueryType "LIKE"}} like {{end}}?", {{ if eq .QueryType "LIKE"}}"%"+e.{{.GoField}}+"%"{{else}}e.{{.GoField}}{{end}}) - } + } {{- else if .IsQuery }} - if e.{{.GoField}} != {{if eq .GoType "string" -}} "" {{ else if eq .GoType "int" -}} 0 {{- end}} { + if e.{{.GoField}} != {{if eq .GoType "string" -}} "" {{ else if eq .GoType "int" -}} 0 {{- end}} { table = table.Where("{{.ColumnName}}{{if eq .QueryType "EQ"}} = {{else if eq .QueryType "NE"}} != {{else if eq .QueryType "GT"}} > {{else if eq .QueryType "GTE"}} >= {{else if eq .QueryType "LT"}} < {{else if eq .QueryType "LTE"}} <= {{else if eq .QueryType "LIKE"}} like {{end}}?", {{ if eq .QueryType "LIKE"}}"%"+e.{{.GoField}}+"%"{{else}}e.{{.GoField}}{{end}}) - } + } {{ end -}} -{{- end }} + {{- end }} -if err := table.First(&doc).Error; err != nil { -return doc, err -} -return doc, nil + if err := table.First(&doc).Error; err != nil { + return doc, err + } + return doc, nil } // 获取{{.ClassName}}带分页 -func (e *{{.ClassName}}) GetPage(pageSize int, pageIndex int) ([]{{.ClassName}}, int, error) { -var doc []{{.ClassName}} +func (e *{{.ClassName}}) GetPage(pageSize int, pageIndex int, v interface{}) ([]{{.ClassName}}, int, error) { + var doc []{{.ClassName}} -table := orm.Eloquent.Table(e.TableName()) -{{ range .Columns }} - {{- if .IsQuery }} - if e.{{.GoField}} != {{if eq .GoType "string" -}} "" {{ else if eq .GoType "int" -}} 0 {{- end}} { - table = table.Where("{{.ColumnName}}{{if eq .QueryType "EQ"}} = {{else if eq .QueryType "NE"}} != {{else if eq .QueryType "GT"}} > {{else if eq .QueryType "GTE"}} >= {{else if eq .QueryType "LT"}} < {{else if eq .QueryType "LTE"}} <= {{else if eq .QueryType "LIKE"}} like {{end}}?", {{ if eq .QueryType "LIKE"}}"%"+e.{{.GoField}}+"%"{{else}}e.{{.GoField}}{{end}}) - } - {{ end -}} -{{- end }} + table := orm.Eloquent.Table(e.TableName()).Scopes(tools.MakeCondition(v)) -// 数据权限控制(如果不需要数据权限请将此处去掉) -dataPermission := new(DataPermission) -dataPermission.UserId, _ = tools.StringToInt(e.DataScope) -table,err := dataPermission.GetDataScope(e.TableName(), table) -if err != nil { -return nil, 0, err -} -var count int + // 数据权限控制(如果不需要数据权限请将此处去掉) + dataPermission := new(DataPermission) + dataPermission.UserId, _ = tools.StringToInt(e.DataScope) + table,err := dataPermission.GetDataScope(e.TableName(), table) + if err != nil { + return nil, 0, err + } + var count int -if err := table.Offset((pageIndex - 1) * pageSize).Limit(pageSize).Find(&doc).Offset(-1).Limit(-1).Count(&count).Error; err != nil { -return nil, 0, err -} -return doc, count, nil + if err := table.Scopes(tools.Paginate(pageSize, pageIndex)).Find(&doc).Offset(-1).Limit(-1).Count(&count).Error; err != nil { + return nil, 0, err + } + return doc, count, nil } // 更新{{.ClassName}}