From 598d5549c8294a070363797c0ca67da88c34462e Mon Sep 17 00:00:00 2001 From: Siyi Guo Date: Fri, 3 Feb 2023 10:31:05 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=A8=A1=E6=9D=BF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- template/v4/dto.go.template | 182 +++++++++--------- template/v4/no_actions/apis.go.template | 117 +++++------ .../no_actions/router_check_role.go.template | 4 +- template/v4/no_actions/service.go.template | 50 ++--- 4 files changed, 177 insertions(+), 176 deletions(-) diff --git a/template/v4/dto.go.template b/template/v4/dto.go.template index 507f0c1..0d7cd41 100644 --- a/template/v4/dto.go.template +++ b/template/v4/dto.go.template @@ -2,19 +2,19 @@ package dto import ( - {{- $bb := false -}} - {{- range .Columns -}} - {{$z := .IsQuery}} - {{- if ($z) }} - {{if eq .GoType "time.Time"}}{{- $bb = true -}}{{- end -}} - {{- end -}} - {{- end -}} - {{- range .Columns -}} - {{if eq .GoType "time.Time"}}{{- $bb = true -}}{{- end -}} - {{- end -}} - {{- if eq $bb true -}} - "time" - {{- end }} + {{- $bb := false -}} + {{- range .Columns -}} + {{$z := .IsQuery}} + {{- if ($z) }} + {{if eq .GoType "time.Time"}}{{- $bb = true -}}{{- end -}} + {{- end -}} + {{- end -}} + {{- range .Columns -}} + {{if eq .GoType "time.Time"}}{{- $bb = true -}}{{- end -}} + {{- end -}} + {{- if eq $bb true -}} + "time" + {{- end }} "go-admin/app/{{.PackageName}}/models" "go-admin/common/dto" @@ -22,22 +22,22 @@ import ( ) type {{.ClassName}}GetPageReq struct { - dto.Pagination `search:"-"` - {{- $tablename := .TBName -}} - {{- range .Columns -}} - {{$z := .IsQuery}} - {{- if ($z) }} - {{.GoField}} {{.GoType}} `form:"{{.JsonField}}" search:"type:{{if eq .QueryType "EQ"}}exact{{ else if eq .QueryType "NE"}}iexact{{ else if eq .QueryType "LIKE"}}contains{{ else if eq .QueryType "GT"}}gt{{ else if eq .QueryType "GTE"}}gte{{ else if eq .QueryType "LT"}}lt{{ else if eq .QueryType "LTE"}}lte{{- end }};column:{{.ColumnName}};table:{{$tablename}}" comment:"{{.ColumnComment}}"` - {{- end }} - {{- end }} - {{.ClassName}}Order + dto.Pagination `search:"-"` + {{- $tablename := .TBName -}} + {{- range .Columns -}} + {{$z := .IsQuery}} + {{- if ($z) }} + {{.GoField}} {{.GoType}} `form:"{{.JsonField}}" search:"type:{{if eq .QueryType "EQ"}}exact{{ else if eq .QueryType "NE"}}iexact{{ else if eq .QueryType "LIKE"}}contains{{ else if eq .QueryType "GT"}}gt{{ else if eq .QueryType "GTE"}}gte{{ else if eq .QueryType "LT"}}lt{{ else if eq .QueryType "LTE"}}lte{{- end }};column:{{.ColumnName}};table:{{$tablename}}" comment:"{{.ColumnComment}}"` + {{- end }} + {{- end }} + {{.ClassName}}Order } type {{.ClassName}}Order struct { - {{ $tablename := .TBName }} - {{- range .Columns -}} - {{.GoField}} string `form:"{{.JsonField}}Order" search:"type:order;column:{{.ColumnName}};table:{{$tablename}}"` - {{ end }} + {{ $tablename := .TBName }} + {{- range .Columns -}} + {{.GoField}} string `form:"{{.JsonField}}Order" search:"type:order;column:{{.ColumnName}};table:{{$tablename}}"` + {{ end }} } func (m *{{.ClassName}}GetPageReq) GetNeedSearch() interface{} { @@ -45,39 +45,39 @@ func (m *{{.ClassName}}GetPageReq) GetNeedSearch() interface{} { } type {{.ClassName}}InsertReq struct { - {{- range .Columns -}} - {{$x := .Pk}} - {{- if ($x) }} - {{.GoField}} {{.GoType}} `json:"-" comment:"{{.ColumnComment}}"` // {{.ColumnComment}} - {{- else if eq .GoField "CreatedAt" -}} - {{- else if eq .GoField "UpdatedAt" -}} - {{- else if eq .GoField "DeletedAt" -}} - {{- else if eq .GoField "CreateBy" -}} - {{- else if eq .GoField "UpdateBy" -}} - {{- else }} - {{.GoField}} {{.GoType}} `json:"{{.JsonField}}" comment:"{{.ColumnComment}}"` - {{- end -}} - {{- end }} - common.ControlBy + {{- range .Columns -}} + {{$x := .Pk}} + {{- if ($x) }} + {{.GoField}} {{.GoType}} `json:"-" comment:"{{.ColumnComment}}"` // {{.ColumnComment}} + {{- else if eq .GoField "CreatedAt" -}} + {{- else if eq .GoField "UpdatedAt" -}} + {{- else if eq .GoField "DeletedAt" -}} + {{- else if eq .GoField "CreateBy" -}} + {{- else if eq .GoField "UpdateBy" -}} + {{- else }} + {{.GoField}} {{.GoType}} `json:"{{.JsonField}}" comment:"{{.ColumnComment}}"` + {{- end -}} + {{- end }} + common.ControlBy } func (s *{{.ClassName}}InsertReq) Generate(model *models.{{.ClassName}}) { - {{- range .Columns -}} - {{$x := .Pk}} - {{- if ($x) }} - if s.{{.GoField}} == 0 { - model.Model = common.Model{ {{.GoField}}: s.{{.GoField}} } - } - {{- else if eq .GoField "CreatedAt" -}} - {{- else if eq .GoField "UpdatedAt" -}} - {{- else if eq .GoField "DeletedAt" -}} - {{- else if eq .GoField "CreateBy"}} - model.{{.GoField}} = s.{{.GoField}} // 添加这而,需要记录是被谁创建的 - {{- else if eq .GoField "UpdateBy" -}} - {{- else }} - model.{{.GoField}} = s.{{.GoField}} - {{- end -}} - {{- end }} + {{- range .Columns -}} + {{$x := .Pk}} + {{- if ($x) }} + if s.{{.GoField}} == 0 { + model.Model = common.Model{ {{.GoField}}: s.{{.GoField}} } + } + {{- else if eq .GoField "CreatedAt" -}} + {{- else if eq .GoField "UpdatedAt" -}} + {{- else if eq .GoField "DeletedAt" -}} + {{- else if eq .GoField "CreateBy"}} + model.{{.GoField}} = s.{{.GoField}} // 添加这而,需要记录是被谁创建的 + {{- else if eq .GoField "UpdateBy" -}} + {{- else }} + model.{{.GoField}} = s.{{.GoField}} + {{- end -}} + {{- end }} } func (s *{{.ClassName}}InsertReq) GetId() interface{} { @@ -85,39 +85,39 @@ func (s *{{.ClassName}}InsertReq) GetId() interface{} { } type {{.ClassName}}UpdateReq struct { - {{- range .Columns -}} - {{$x := .Pk}} - {{- if ($x) }} - {{.GoField}} {{.GoType}} `uri:"{{.JsonField}}" comment:"{{.ColumnComment}}"` // {{.ColumnComment}} - {{- else if eq .GoField "CreatedAt" -}} - {{- else if eq .GoField "UpdatedAt" -}} - {{- else if eq .GoField "DeletedAt" -}} - {{- else if eq .GoField "CreateBy" -}} - {{- else if eq .GoField "UpdateBy" -}} - {{- else }} - {{.GoField}} {{.GoType}} `json:"{{.JsonField}}" comment:"{{.ColumnComment}}"` - {{- end -}} - {{- end }} - common.ControlBy + {{- range .Columns -}} + {{$x := .Pk}} + {{- if ($x) }} + {{.GoField}} {{.GoType}} `uri:"{{.JsonField}}" comment:"{{.ColumnComment}}"` // {{.ColumnComment}} + {{- else if eq .GoField "CreatedAt" -}} + {{- else if eq .GoField "UpdatedAt" -}} + {{- else if eq .GoField "DeletedAt" -}} + {{- else if eq .GoField "CreateBy" -}} + {{- else if eq .GoField "UpdateBy" -}} + {{- else }} + {{.GoField}} {{.GoType}} `json:"{{.JsonField}}" comment:"{{.ColumnComment}}"` + {{- end -}} + {{- end }} + common.ControlBy } func (s *{{.ClassName}}UpdateReq) Generate(model *models.{{.ClassName}}) { - {{- range .Columns -}} - {{$x := .Pk}} - {{- if ($x) }} - if s.{{.GoField}} == 0 { - model.Model = common.Model{ {{.GoField}}: s.{{.GoField}} } - } - {{- else if eq .GoField "CreatedAt" -}} - {{- else if eq .GoField "UpdatedAt" -}} - {{- else if eq .GoField "DeletedAt" -}} - {{- else if eq .GoField "CreateBy" -}} - {{- else if eq .GoField "UpdateBy"}} - model.{{.GoField}} = s.{{.GoField}} // 添加这而,需要记录是被谁更新的 - {{- else }} - model.{{.GoField}} = s.{{.GoField}} - {{- end -}} - {{- end }} + {{- range .Columns -}} + {{$x := .Pk}} + {{- if ($x) }} + if s.{{.GoField}} == 0 { + model.Model = common.Model{ {{.GoField}}: s.{{.GoField}} } + } + {{- else if eq .GoField "CreatedAt" -}} + {{- else if eq .GoField "UpdatedAt" -}} + {{- else if eq .GoField "DeletedAt" -}} + {{- else if eq .GoField "CreateBy" -}} + {{- else if eq .GoField "UpdateBy"}} + model.{{.GoField}} = s.{{.GoField}} // 添加这而,需要记录是被谁更新的 + {{- else }} + model.{{.GoField}} = s.{{.GoField}} + {{- end -}} + {{- end }} } func (s *{{.ClassName}}UpdateReq) GetId() interface{} { @@ -127,11 +127,11 @@ func (s *{{.ClassName}}UpdateReq) GetId() interface{} { // {{.ClassName}}GetReq 功能获取请求参数 type {{.ClassName}}GetReq struct { {{- range .Columns -}} - {{$x := .Pk}} - {{- if ($x) }} - {{.GoField}} {{.GoType}} `uri:"{{.JsonField}}"` - {{- end }} - {{- end }} + {{$x := .Pk}} + {{- if ($x) }} + {{.GoField}} {{.GoType}} `uri:"{{.JsonField}}"` + {{- end }} + {{- end }} } func (s *{{.ClassName}}GetReq) GetId() interface{} { return s.{{.PkGoField}} diff --git a/template/v4/no_actions/apis.go.template b/template/v4/no_actions/apis.go.template index 7878776..ff73091 100644 --- a/template/v4/no_actions/apis.go.template +++ b/template/v4/no_actions/apis.go.template @@ -1,9 +1,10 @@ package apis import ( - "fmt" + "fmt" "github.com/gin-gonic/gin" + "github.com/gin-gonic/gin/binding" "github.com/go-admin-team/go-admin-core/sdk/api" "github.com/go-admin-team/go-admin-core/sdk/pkg/jwtauth/user" _ "github.com/go-admin-team/go-admin-core/sdk/pkg/response" @@ -35,18 +36,18 @@ type {{.ClassName}} struct { // @Router /api/v1/{{.ModuleName}} [get] // @Security Bearer func (e {{.ClassName}}) GetPage(c *gin.Context) { - req := dto.{{.ClassName}}GetPageReq{} - s := service.{{.ClassName}}{} - err := e.MakeContext(c). - MakeOrm(). - Bind(&req). - MakeService(&s.Service). - Errors - if err != nil { - e.Logger.Error(err) - e.Error(500, err, err.Error()) - return - } + req := dto.{{.ClassName}}GetPageReq{} + s := service.{{.ClassName}}{} + err := e.MakeContext(c). + MakeOrm(). + Bind(&req, binding.Form). + MakeService(&s.Service). + Errors + if err != nil { + e.Logger.Error(err) + e.Error(500, err, err.Error()) + return + } p := actions.GetPermissionFromContext(c) list := make([]models.{{.ClassName}}, 0) @@ -55,7 +56,7 @@ func (e {{.ClassName}}) GetPage(c *gin.Context) { err = s.GetPage(&req, p, &list, &count) if err != nil { e.Error(500, err, fmt.Sprintf("获取{{.TableComment}}失败,\r\n失败信息 %s", err.Error())) - return + return } e.PageOK(list, int(count), req.GetPageIndex(), req.GetPageSize(), "查询成功") @@ -72,7 +73,7 @@ func (e {{.ClassName}}) GetPage(c *gin.Context) { func (e {{.ClassName}}) Get(c *gin.Context) { req := dto.{{.ClassName}}GetReq{} s := service.{{.ClassName}}{} - err := e.MakeContext(c). + err := e.MakeContext(c). MakeOrm(). Bind(&req). MakeService(&s.Service). @@ -88,10 +89,10 @@ func (e {{.ClassName}}) Get(c *gin.Context) { err = s.Get(&req, p, &object) if err != nil { e.Error(500, err, fmt.Sprintf("获取{{.TableComment}}失败,\r\n失败信息 %s", err.Error())) - return + return } - e.OK( object, "查询成功") + e.OK(object, "查询成功") } // Insert 创建{{.TableComment}} @@ -105,25 +106,25 @@ func (e {{.ClassName}}) Get(c *gin.Context) { // @Router /api/v1/{{.ModuleName}} [post] // @Security Bearer func (e {{.ClassName}}) Insert(c *gin.Context) { - req := dto.{{.ClassName}}InsertReq{} - s := service.{{.ClassName}}{} - err := e.MakeContext(c). - MakeOrm(). - Bind(&req). - MakeService(&s.Service). - Errors - if err != nil { - e.Logger.Error(err) - e.Error(500, err, err.Error()) - return - } + req := dto.{{.ClassName}}InsertReq{} + s := service.{{.ClassName}}{} + err := e.MakeContext(c). + MakeOrm(). + Bind(&req). + MakeService(&s.Service). + Errors + if err != nil { + e.Logger.Error(err) + e.Error(500, err, err.Error()) + return + } // 设置创建人 req.SetCreateBy(user.GetUserId(c)) err = s.Insert(&req) if err != nil { e.Error(500, err, fmt.Sprintf("创建{{.TableComment}}失败,\r\n失败信息 %s", err.Error())) - return + return } e.OK(req.GetId(), "创建成功") @@ -141,27 +142,27 @@ func (e {{.ClassName}}) Insert(c *gin.Context) { // @Router /api/v1/{{.ModuleName}}/{id} [put] // @Security Bearer func (e {{.ClassName}}) Update(c *gin.Context) { - req := dto.{{.ClassName}}UpdateReq{} - s := service.{{.ClassName}}{} - err := e.MakeContext(c). - MakeOrm(). - Bind(&req). - MakeService(&s.Service). - Errors - if err != nil { - e.Logger.Error(err) - e.Error(500, err, err.Error()) - return - } + req := dto.{{.ClassName}}UpdateReq{} + s := service.{{.ClassName}}{} + err := e.MakeContext(c). + MakeOrm(). + Bind(&req). + MakeService(&s.Service). + Errors + if err != nil { + e.Logger.Error(err) + e.Error(500, err, err.Error()) + return + } req.SetUpdateBy(user.GetUserId(c)) p := actions.GetPermissionFromContext(c) err = s.Update(&req, p) if err != nil { e.Error(500, err, fmt.Sprintf("修改{{.TableComment}}失败,\r\n失败信息 %s", err.Error())) - return + return } - e.OK( req.GetId(), "修改成功") + e.OK(req.GetId(), "修改成功") } // Delete 删除{{.TableComment}} @@ -173,18 +174,18 @@ func (e {{.ClassName}}) Update(c *gin.Context) { // @Router /api/v1/{{.ModuleName}} [delete] // @Security Bearer func (e {{.ClassName}}) Delete(c *gin.Context) { - s := service.{{.ClassName}}{} - req := dto.{{.ClassName}}DeleteReq{} - err := e.MakeContext(c). - MakeOrm(). - Bind(&req). - MakeService(&s.Service). - Errors - if err != nil { - e.Logger.Error(err) - e.Error(500, err, err.Error()) - return - } + s := service.{{.ClassName}}{} + req := dto.{{.ClassName}}DeleteReq{} + err := e.MakeContext(c). + MakeOrm(). + Bind(&req). + MakeService(&s.Service). + Errors + if err != nil { + e.Logger.Error(err) + e.Error(500, err, err.Error()) + return + } // req.SetUpdateBy(user.GetUserId(c)) p := actions.GetPermissionFromContext(c) @@ -192,7 +193,7 @@ func (e {{.ClassName}}) Delete(c *gin.Context) { err = s.Remove(&req, p) if err != nil { e.Error(500, err, fmt.Sprintf("删除{{.TableComment}}失败,\r\n失败信息 %s", err.Error())) - return + return } - e.OK( req.GetId(), "删除成功") + e.OK(req.GetId(), "删除成功") } diff --git a/template/v4/no_actions/router_check_role.go.template b/template/v4/no_actions/router_check_role.go.template index f8a3c3a..6cbee35 100644 --- a/template/v4/no_actions/router_check_role.go.template +++ b/template/v4/no_actions/router_check_role.go.template @@ -5,8 +5,8 @@ import ( jwt "github.com/go-admin-team/go-admin-core/sdk/pkg/jwtauth" "go-admin/app/{{.PackageName}}/apis" - "go-admin/common/middleware" "go-admin/common/actions" + "go-admin/common/middleware" ) func init() { @@ -24,4 +24,4 @@ func register{{.ClassName}}Router(v1 *gin.RouterGroup, authMiddleware *jwt.GinJW r.PUT("/:id", actions.PermissionAction(), api.Update) r.DELETE("", api.Delete) } -} \ No newline at end of file +} diff --git a/template/v4/no_actions/service.go.template b/template/v4/no_actions/service.go.template index 84e8886..63aa26d 100644 --- a/template/v4/no_actions/service.go.template +++ b/template/v4/no_actions/service.go.template @@ -3,7 +3,7 @@ package service import ( "errors" - "github.com/go-admin-team/go-admin-core/sdk/service" + "github.com/go-admin-team/go-admin-core/sdk/service" "gorm.io/gorm" "go-admin/app/{{.PackageName}}/models" @@ -59,9 +59,9 @@ func (e *{{.ClassName}}) Get(d *dto.{{.ClassName}}GetReq, p *actions.DataPermiss // Insert 创建{{.ClassName}}对象 func (e *{{.ClassName}}) Insert(c *dto.{{.ClassName}}InsertReq) error { - var err error - var data models.{{.ClassName}} - c.Generate(&data) + var err error + var data models.{{.ClassName}} + c.Generate(&data) err = e.Orm.Create(&data).Error if err != nil { e.Log.Errorf("{{.ClassName}}Service Insert error:%s \r\n", err) @@ -72,22 +72,22 @@ func (e *{{.ClassName}}) Insert(c *dto.{{.ClassName}}InsertReq) error { // Update 修改{{.ClassName}}对象 func (e *{{.ClassName}}) Update(c *dto.{{.ClassName}}UpdateReq, p *actions.DataPermission) error { - var err error - var data = models.{{.ClassName}}{} - e.Orm.Scopes( - actions.Permission(data.TableName(), p), - ).First(&data, c.GetId()) - c.Generate(&data) + var err error + var data = models.{{.ClassName}}{} + e.Orm.Scopes( + actions.Permission(data.TableName(), p), + ).First(&data, c.GetId()) + c.Generate(&data) - db := e.Orm.Save(&data) - if err = db.Error; err != nil { - e.Log.Errorf("{{.ClassName}}Service Save error:%s \r\n", err) - return err - } - if db.RowsAffected == 0 { - return errors.New("无权更新该数据") - } - return nil + db := e.Orm.Save(&data) + if err = db.Error; err != nil { + e.Log.Errorf("{{.ClassName}}Service Save error:%s \r\n", err) + return err + } + if db.RowsAffected == 0 { + return errors.New("无权更新该数据") + } + return nil } // Remove 删除{{.ClassName}} @@ -99,11 +99,11 @@ func (e *{{.ClassName}}) Remove(d *dto.{{.ClassName}}DeleteReq, p *actions.DataP actions.Permission(data.TableName(), p), ).Delete(&data, d.GetId()) if err := db.Error; err != nil { - e.Log.Errorf("Service Remove{{.ClassName}} error:%s \r\n", err) - return err - } - if db.RowsAffected == 0 { - return errors.New("无权删除该数据") - } + e.Log.Errorf("Service Remove{{.ClassName}} error:%s \r\n", err) + return err + } + if db.RowsAffected == 0 { + return errors.New("无权删除该数据") + } return nil }