diff --git a/apis/actions/create.go b/apis/actions/create.go new file mode 100644 index 00000000..79212acb --- /dev/null +++ b/apis/actions/create.go @@ -0,0 +1,39 @@ +package actions + +import ( + "errors" + + "github.com/gin-gonic/gin" + "gorm.io/gorm" + + "go-admin/tools" + "go-admin/tools/app" + "go-admin/tools/model" +) + +// CreateAction 通用新增动作 +func CreateAction(m model.ActiveRecord) gin.HandlerFunc { + return func(c *gin.Context) { + object := m.Generate() + var err error + idb, exist := c.Get("db") + if !exist { + err = errors.New("db connect not exist") + tools.HasError(err, "", 500) + } + switch idb.(type) { + case *gorm.DB: + //新增操作 + db := idb.(*gorm.DB) + err = c.Bind(object) + tools.HasError(err, "参数验证失败", 422) + err = db.WithContext(c).Create(object).Error + tools.HasError(err, "创建失败", 500) + default: + err = errors.New("db connect not exist") + tools.HasError(err, "", 500) + } + app.OK(c, object.GetId(), "创建成功") + c.Next() + } +} diff --git a/apis/actions/delete.go b/apis/actions/delete.go new file mode 100644 index 00000000..522aa4c0 --- /dev/null +++ b/apis/actions/delete.go @@ -0,0 +1,42 @@ +package actions + +import ( + "errors" + + "github.com/gin-gonic/gin" + "gorm.io/gorm" + + "go-admin/tools" + "go-admin/tools/app" + "go-admin/tools/model" +) + +// DeleteAction 通用删除动作 +func DeleteAction(m model.ActiveRecord) gin.HandlerFunc { + return func(c *gin.Context) { + object := m.Generate() + var err error + idb, exist := c.Get("db") + if !exist { + err = errors.New("db connect not exist") + tools.HasError(err, "", 500) + } + switch idb.(type) { + case *gorm.DB: + //新增操作 + db := idb.(*gorm.DB) + var generalDelDto tools.GeneralDelDto + err = c.Bind(&generalDelDto) + tools.HasError(err, "参数验证失败", 422) + err = c.BindUri(&generalDelDto) + tools.HasError(err, "参数验证失败", 422) + err = db.WithContext(c).Where(generalDelDto.GetIds()).Delete(object).Error + tools.HasError(err, "更新失败", 500) + default: + err = errors.New("db connect not exist") + tools.HasError(err, "", 500) + } + app.OK(c, object.GetId(), "更新成功") + c.Next() + } +} diff --git a/apis/actions/index.go b/apis/actions/index.go new file mode 100644 index 00000000..5d070d04 --- /dev/null +++ b/apis/actions/index.go @@ -0,0 +1,51 @@ +package actions + +import ( + "errors" + "go-admin/dto" + + "github.com/gin-gonic/gin" + "gorm.io/gorm" + + "go-admin/tools" + "go-admin/tools/app" + "go-admin/tools/model" +) + +// IndexAction 通用查询动作 +func IndexAction(m model.ActiveRecord, d dto.Dtor) gin.HandlerFunc { + return func(c *gin.Context) { + object := m.Generate() + req := d.Generate() + var err error + idb, exist := c.Get("db") + if !exist { + err = errors.New("db connect not exist") + tools.HasError(err, "", 500) + } + list := object.GenerateList() + var count int64 + switch idb.(type) { + case *gorm.DB: + //新增操作 + db := idb.(*gorm.DB) + err = c.Bind(req) + tools.HasError(err, "参数验证失败", 422) + err = req.Validate() + tools.HasError(err, "参数验证失败", 422) + err = db.WithContext(c).Model(object). + Scopes(tools.MakeCondition(req.GetNeedSearch())). + Scopes(tools.Paginate(req.GetPageSize(), req.GetPageIndex())). + Find(list).Limit(-1).Offset(-1). + Count(&count).Error + if !errors.Is(err, gorm.ErrRecordNotFound) { + tools.HasError(err, "查询失败", 500) + } + default: + err = errors.New("db connect not exist") + tools.HasError(err, "", 500) + } + app.PageOK(c, list, int(count), req.GetPageIndex(), req.GetPageSize(), "查询成功") + c.Next() + } +} diff --git a/apis/actions/update.go b/apis/actions/update.go new file mode 100644 index 00000000..0f5d44d1 --- /dev/null +++ b/apis/actions/update.go @@ -0,0 +1,39 @@ +package actions + +import ( + "errors" + + "github.com/gin-gonic/gin" + "gorm.io/gorm" + + "go-admin/tools" + "go-admin/tools/app" + "go-admin/tools/model" +) + +// UpdateAction 通用更新动作 +func UpdateAction(m model.ActiveRecord) gin.HandlerFunc { + return func(c *gin.Context) { + object := m.Generate() + var err error + idb, exist := c.Get("db") + if !exist { + err = errors.New("db connect not exist") + tools.HasError(err, "", 500) + } + switch idb.(type) { + case *gorm.DB: + //新增操作 + db := idb.(*gorm.DB) + err = c.Bind(object) + tools.HasError(err, "参数验证失败", 422) + err = db.WithContext(c).Updates(object).Error + tools.HasError(err, "更新失败", 500) + default: + err = errors.New("db connect not exist") + tools.HasError(err, "", 500) + } + app.OK(c, object.GetId(), "更新成功") + c.Next() + } +} diff --git a/apis/actions/view.go b/apis/actions/view.go new file mode 100644 index 00000000..5d98b510 --- /dev/null +++ b/apis/actions/view.go @@ -0,0 +1,40 @@ +package actions + +import ( + "errors" + + "github.com/gin-gonic/gin" + "gorm.io/gorm" + + "go-admin/tools" + "go-admin/tools/app" + "go-admin/tools/model" +) + +// ViewAction 通用详情动作 +func ViewAction(m model.ActiveRecord) gin.HandlerFunc { + return func(c *gin.Context) { + object := m.Generate() + var err error + idb, exist := c.Get("db") + if !exist { + err = errors.New("db connect not exist") + tools.HasError(err, "", 500) + } + switch idb.(type) { + case *gorm.DB: + //新增操作 + db := idb.(*gorm.DB) + var generalGetDto tools.GeneralGetDto + err = c.BindUri(&generalGetDto) + tools.HasError(err, "参数验证失败", 422) + err = db.WithContext(c).Where(generalGetDto.Id).First(object).Error + tools.HasError(err, "查看失败", 500) + default: + err = errors.New("db connect not exist") + tools.HasError(err, "", 500) + } + app.OK(c, object, "查看成功") + c.Next() + } +} diff --git a/dto/pagination.go b/dto/pagination.go new file mode 100644 index 00000000..37c2bc01 --- /dev/null +++ b/dto/pagination.go @@ -0,0 +1,6 @@ +package dto + +type Pagination struct { + PageIndex int `form:"pageIndex"` + PageSize int `form:"pageSize"` +} diff --git a/dto/sysjob.go b/dto/sysjob.go index 149ed54a..d7414755 100644 --- a/dto/sysjob.go +++ b/dto/sysjob.go @@ -1,6 +1,7 @@ package dto type SysJobSearch struct { + Pagination `search:"-"` JobId int `form:"jobId" search:"type:exact;column:job_id;table:sys_job"` JobName string `form:"jobName" search:"type:icontains;column:job_name;table:sys_job"` JobGroup string `form:"jobGroup" search:"type:exact;column:job_group;table:sys_job"` @@ -8,3 +9,24 @@ type SysJobSearch struct { InvokeTarget string `form:"invokeTarget" search:"type:exact;column:invoke_target;table:sys_job"` Status int `form:"status" search:"type:exact;column:status;table:sys_job"` } + +func (m *SysJobSearch) GetNeedSearch() interface{} { + return *m +} + +func (m *SysJobSearch) Validate() error { + return nil +} + +func (m *SysJobSearch) Generate() Dtor { + o := *m + return &o +} + +func (m *SysJobSearch) GetPageIndex() int { + return m.PageIndex +} + +func (m *SysJobSearch) GetPageSize() int { + return m.PageSize +} diff --git a/dto/type.go b/dto/type.go new file mode 100644 index 00000000..ba6fa638 --- /dev/null +++ b/dto/type.go @@ -0,0 +1,9 @@ +package dto + +type Dtor interface { + Validate() error + Generate() Dtor + GetPageIndex() int + GetPageSize() int + GetNeedSearch() interface{} +} diff --git a/go.mod b/go.mod index ebe51d77..d0f1955c 100644 --- a/go.mod +++ b/go.mod @@ -30,6 +30,7 @@ require ( github.com/unrolled/secure v1.0.8 go.uber.org/multierr v1.5.0 // indirect golang.org/x/crypto v0.0.0-20200820211705-5c72a883971a + gopkg.in/ffmt.v1 v1.5.6 gorm.io/driver/mysql v0.3.2 gorm.io/driver/postgres v0.2.9 gorm.io/driver/sqlite v1.0.9 diff --git a/middleware/db.go b/middleware/db.go new file mode 100644 index 00000000..272ec3df --- /dev/null +++ b/middleware/db.go @@ -0,0 +1,17 @@ +package middleware + +import ( + "github.com/gin-gonic/gin" + "gorm.io/gorm" +) + +func WithContextDb(dbMap map[string]*gorm.DB) gin.HandlerFunc { + return func(c *gin.Context) { + if db, ok := dbMap["*"]; ok { + c.Set("db", db) + } else { + c.Set("db", dbMap[c.Request.Host]) + } + c.Next() + } +} diff --git a/models/sysjob.go b/models/sysjob.go index 704ef744..5f9d1cac 100644 --- a/models/sysjob.go +++ b/models/sysjob.go @@ -3,6 +3,7 @@ package models import ( orm "go-admin/global" "go-admin/tools" + "go-admin/tools/model" ) type SysJob struct { @@ -27,6 +28,20 @@ func (SysJob) TableName() string { return "sys_job" } +func (e *SysJob) Generate() model.ActiveRecord { + o := *e + return &o +} + +func (e *SysJob) GenerateList() interface{} { + list := make([]SysJob, 0) + return &list +} + +func (e *SysJob) GetId() interface{} { + return e.JobId +} + // 创建SysJob func (e *SysJob) Create() (err error) { return orm.Eloquent.Table(e.TableName()).Create(e).Error diff --git a/router/initrouter.go b/router/initrouter.go index 87ec6374..322e42fd 100644 --- a/router/initrouter.go +++ b/router/initrouter.go @@ -8,6 +8,7 @@ import ( _ "go-admin/pkg/jwtauth" "go-admin/tools" config2 "go-admin/tools/config" + "gorm.io/gorm" ) func InitRouter() *gin.Engine { @@ -20,6 +21,7 @@ func InitRouter() *gin.Engine { if config2.SslConfig.Enable { r.Use(handler.TlsHandler()) } + r.Use(middleware.WithContextDb(map[string]*gorm.DB{"*": global.Eloquent})) middleware.InitMiddleware(r) // the jwt middleware authMiddleware, err := middleware.AuthInit() diff --git a/router/sysrouter.go b/router/sysrouter.go index e56f19a6..f2b71048 100644 --- a/router/sysrouter.go +++ b/router/sysrouter.go @@ -1,6 +1,7 @@ package router import ( + "go-admin/apis/actions" log2 "go-admin/apis/log" "go-admin/apis/monitor" "go-admin/apis/public" @@ -9,8 +10,10 @@ import ( "go-admin/apis/system/dict" . "go-admin/apis/tools" _ "go-admin/docs" + "go-admin/dto" "go-admin/handler" "go-admin/middleware" + "go-admin/models" jwt "go-admin/pkg/jwtauth" "go-admin/pkg/ws" "mime" @@ -110,11 +113,18 @@ func registerSysJobRouter(v1 *gin.RouterGroup) { r := v1.Group("/sysjob") { - r.GET("", sysjob.GetSysJobList) - r.GET("/:id", sysjob.GetSysJob) - r.POST("", sysjob.InsertSysJob) - r.PUT("", sysjob.UpdateSysJob) - r.DELETE("/:id", sysjob.DeleteSysJob) + //r.GET("", sysjob.GetSysJobList) + //r.GET("/:id", sysjob.GetSysJob) + //r.POST("", sysjob.InsertSysJob) + //r.PUT("", sysjob.UpdateSysJob) + //r.DELETE("/:id", sysjob.DeleteSysJob) + + sysJob := &models.SysJob{} + r.GET("", actions.IndexAction(sysJob, new(dto.SysJobSearch))) + r.GET("/:id", actions.ViewAction(sysJob)) + r.POST("", actions.CreateAction(sysJob)) + r.PUT("", actions.UpdateAction(sysJob)) + r.DELETE("/:id", actions.DeleteAction(sysJob)) } v1.GET("/job/remove/:jobId", sysjob.RemoveJob) diff --git a/tools/dto.go b/tools/dto.go index 3f472abf..a50e561d 100644 --- a/tools/dto.go +++ b/tools/dto.go @@ -1,6 +1,7 @@ package tools import ( + "github.com/google/uuid" "go-admin/tools/config" "github.com/matchstalk/go-admin-core/search" @@ -8,8 +9,30 @@ import ( ) type GeneralDelDto struct { - Id string `uri:"id" json:"id" validate:"required"` + Id string `uri:"id" json:"id" validate:"required"` + Ids []string `json:"ids"` } + +func (g GeneralDelDto) GetIds() []string { + ids := make([]string, 0) + if len(g.Ids) > 0 { + for _, id := range g.Ids { + if len(id) > 0 { + ids = append(ids, id) + } + } + } else { + if len(g.Id) > 0 { + ids = append(ids, g.Id) + } + } + if len(ids) <= 0 { + //方式全部删除 + ids = append(ids, uuid.New().String()) + } + return ids +} + type GeneralGetDto struct { Id string `uri:"id" json:"id" validate:"required"` } diff --git a/tools/model/type.go b/tools/model/type.go new file mode 100644 index 00000000..57f34d03 --- /dev/null +++ b/tools/model/type.go @@ -0,0 +1,10 @@ +package model + +import "gorm.io/gorm/schema" + +type ActiveRecord interface { + schema.Tabler + Generate() ActiveRecord + GetId() interface{} + GenerateList() interface{} +}