perf👌 : 配置文件功能代码优化

This commit is contained in:
zhangwenjian
2021-01-11 16:26:12 +08:00
parent e1f15bcff1
commit 0db58170ee
4 changed files with 366 additions and 50 deletions
+59 -35
View File
@@ -1,7 +1,9 @@
package dto
import (
"encoding/json"
"github.com/gin-gonic/gin"
"github.com/gin-gonic/gin/binding"
"go-admin/app/admin/models/system"
"go-admin/common/dto"
"go-admin/common/log"
@@ -9,6 +11,7 @@ import (
"go-admin/tools"
)
// SysConfigSearch 列表或者搜索使用结构体
type SysConfigSearch struct {
dto.Pagination `search:"-"`
ConfigName string `form:"configName" search:"type:exact;column:config_name;table:sys_config" comment:""`
@@ -20,6 +23,7 @@ func (m *SysConfigSearch) GetNeedSearch() interface{} {
return *m
}
// Bind 映射上下文中的结构体数据
func (m *SysConfigSearch) Bind(ctx *gin.Context) error {
msgID := tools.GenerateMsgIDFromContext(ctx)
err := ctx.ShouldBind(m)
@@ -29,11 +33,7 @@ func (m *SysConfigSearch) Bind(ctx *gin.Context) error {
return err
}
func (m *SysConfigSearch) Generate() dto.Index {
o := *m
return &o
}
// SysConfigControl 增、改使用的结构体
type SysConfigControl struct {
ID int `uri:"ID" comment:"编码"` // 编码
ConfigName string `json:"configName" comment:""`
@@ -43,7 +43,60 @@ type SysConfigControl struct {
Remark string `json:"remark" comment:""`
}
// Bind 映射上下文中的结构体数据
func (s *SysConfigControl) Bind(ctx *gin.Context) error {
msgID := tools.GenerateMsgIDFromContext(ctx)
err := ctx.ShouldBindUri(s)
if err != nil {
log.Debugf("MsgID[%s] ShouldBindUri error: %s", msgID, err.Error())
return err
}
err = ctx.ShouldBindBodyWith(s, binding.JSON)
if err != nil {
log.Debugf("MsgID[%s] ShouldBind error: %#v", msgID, err.Error())
}
var jsonStr []byte
jsonStr, err = json.Marshal(s)
if err != nil {
log.Debugf("MsgID[%s] ShouldBind error: %#v", msgID, err.Error())
}
ctx.Set("body", string(jsonStr))
return err
}
// Generate 结构体数据转化 从 SysConfigControl 至 system.SysConfig 对应的模型
func (s *SysConfigControl) Generate() (*system.SysConfig, error) {
return &system.SysConfig{
Model: common.Model{ID: s.ID},
ConfigName: s.ConfigName,
ConfigKey: s.ConfigKey,
ConfigValue: s.ConfigValue,
ConfigType: s.ConfigType,
Remark: s.Remark,
}, nil
}
// GetId 获取数据对应的ID
func (s *SysConfigControl) GetId() interface{} {
return s.ID
}
// SysConfigById 获取单个或者删除的结构体
type SysConfigById struct {
Id int `uri:"id"`
Ids []int `json:"ids"`
}
func (s *SysConfigById) Generate() *SysConfigById {
cp := *s
return &cp
}
func (s *SysConfigById) GetId() interface{} {
return s.Id
}
func (s *SysConfigById) Bind(ctx *gin.Context) error {
msgID := tools.GenerateMsgIDFromContext(ctx)
err := ctx.ShouldBindUri(s)
if err != nil {
@@ -57,35 +110,6 @@ func (s *SysConfigControl) Bind(ctx *gin.Context) error {
return err
}
func (s *SysConfigControl) Generate() dto.Control {
cp := *s
return &cp
}
func (s *SysConfigControl) GenerateM() (common.ActiveRecord, error) {
return &system.SysConfig{
Model: common.Model{ID: s.ID},
ConfigName: s.ConfigName,
ConfigKey: s.ConfigKey,
ConfigValue: s.ConfigValue,
ConfigType: s.ConfigType,
Remark: s.Remark,
}, nil
}
func (s *SysConfigControl) GetId() interface{} {
return s.ID
}
type SysConfigById struct {
dto.ObjectById
}
func (s *SysConfigById) Generate() dto.Control {
cp := *s
return &cp
}
func (s *SysConfigById) GenerateM() (common.ActiveRecord, error) {
func (s *SysConfigById) GenerateM() (*system.SysConfig, error) {
return &system.SysConfig{}, nil
}