Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Vision soul #3

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 12 additions & 10 deletions genConfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,29 @@
"DbType": "mysql",
"DBName": "",
"Host": "",
"Port": 3306,
"Port": ,
"User": "",
"Password": ""
},
"GlobalConfig": {
"ServiceName": "artifact"
"FieldStyle": "m_lc",
"ServiceName": ""
},
"ApiConfig": {
"Switch": "C",
"OutPath": "C:\\Users\\maolin.chen\\Desktop\\me\\workspace\\xyz\\app\\artifact/api",
"OutPath": "",
"GoZeroStyle": "go_zero",
"DateStyle": "string"
"DateStyle": "string",
"Tables": [""]
},
"ProtoConfig": {
"Switch": "B",
"OutPath": "artifact/rpc"
"Switch": "C",
"OutPath": ""
},
"ModelConfig": {
"Switch": "C",
"OutPath": "C:\\Users\\maolin.chen\\Desktop\\me\\workspace\\xyz\\app\\artifact/model",
"Tables": [],
"IsCache": true
"OutPath": "",
"IsCache": true,
"Tables": [""]
}
}
}
26 changes: 2 additions & 24 deletions template/api_common.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,6 @@ info (
version: "{{.Version}}"
)

type SearchItem {
Table string `json:"table,optional"` //表
Field string `json:"field"` // 字段
Value string `json:"value"` // 值
Type string `json:"type"` // 值的数据类型 number string date numberArray stringArray
Operator string `json:"operator"` // 操作符 = != > >= 包含 不包含...
Logic string `json:"logic,optional"` // 逻辑符 and | or
}
type SearchGroup {
Group []SearchItem `json:"group"` // 条件组合
Logic string `json:"logic,optional"` // 逻辑符 and | or
}

// 基本查询参数, 根据自己需要进行修改
type SearchBase {
Keyword string `json:"keyword,optional"` // 关键字
Expand All @@ -30,7 +17,7 @@ type SearchBase {
PageSize int `json:"pageSize,default=20,optional"` // 每页条数
PageCurrent int `json:"pageCurrent,default=1,optional"` // 当前页
OrderSort []string `json:"orderSort,optional"` // 排序 eg: ["create_time asc", "id desc"]
SearchPlus []SearchGroup `json:"searchPlus,optional"` // 加强版自定义搜索参数
SearchPlus string `json:"searchPlus,optional"` // 加强版自定义搜索参数
}

// 统一分页返回
Expand All @@ -40,13 +27,4 @@ type PageBase {
PageSize int `json:"pageSize,omitempty"` // 分页大小
PageTotal int `json:"pageTotal,omitempty"` // 总分页数
LastCursor int64 `json:"lastCursor,omitempty"` // 使用游标分页时, 返回最后一个游标
}

type (
IdReq {
Id int64 `json:"id"`
}
IdsReq {
Ids []int64 `json:"ids"`
}
)
}
2 changes: 1 addition & 1 deletion template/api_types.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ info (
)

import (
"types/common.api"
"common.api"
)

//-----------------------{{.CamelName}}的接口-----------------------
Expand Down
103 changes: 94 additions & 9 deletions template/model_model.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ import (
"database/sql"
{{if .IsCache -}}
"fmt"
"time"
"gitee.com/maolinc/vision-soul-common/collectx"
"gitee.com/maolinc/vision-soul-common/utilx"
"github.com/zeromicro/go-zero/core/stores/cache"
"github.com/zeromicro/go-zero/core/stores/redis"
{{end -}}
"gorm.io/gorm"
{{if .IsDate}}"time"{{end -}}
Expand All @@ -22,7 +26,7 @@ type (
{{range .Fields}} {{.CamelName}} {{.DataType}} `gorm:"{{.Name}} {{- if .IsPrimary}};primary_key{{end}}"` //{{.Comment}}
{{end}}}

// {{.CamelName}} query cond
// {{.CamelName}}Query query cond
{{.CamelName}}Query struct {
SearchBase
{{.CamelName}}
Expand All @@ -48,18 +52,28 @@ type (
// FindListByCursor Cursor is required based on cursor paging, Only the primary key is of type int, and other types can be expanded by themselves
FindListByCursor(ctx context.Context, cond *{{.CamelName}}Query) (list []*{{.CamelName}}, err error)
FindAll(ctx context.Context, cond *{{.CamelName}}Query) (list []*{{.CamelName}}, err error)
// FindListByIds
FindListByIds(ctx context.Context, ids []{{.Primary.DataType}}) (list []*{{.CamelName}}, err error)
// ---------------Write your other interfaces below---------------
}

default{{.CamelName}}Model struct {
*customConn
{{if .IsCache}}
redis *redis.Redis
expire time.Duration
{{end}}
table string
}
)

func New{{.CamelName}}Model(db *gorm.DB {{- if .IsCache}}, c cache.CacheConf{{end}}) {{.CamelName}}Model {
func New{{.CamelName}}Model(db *gorm.DB {{- if .IsCache}}, c cache.CacheConf, redis *redis.Redis{{end}}) {{.CamelName}}Model {
return &default{{.CamelName}}Model{
customConn: {{if .IsCache}}newCustomConn(db, c){{else}}newCustomConnNoCache(db){{end}},
{{if .IsCache}}
redis: redis,
expire: 10,
{{end}}
table: "{{.Name}}",
}
}
Expand Down Expand Up @@ -87,7 +101,7 @@ func (m *default{{.CamelName}}Model) Trans(ctx context.Context, fn func(ctx cont

func (m *default{{.CamelName}}Model) Insert(ctx context.Context, data *{{.CamelName}}, db ...*gorm.DB) (err error) {
{{if .IsCache -}}
cacheKey := fmt.Sprintf("%s{{.PrimaryFmt}}", cache{{.CamelName}}PrimaryPrefix, {{.PrimaryFmtV}})
cacheKey := m.getPrimaryCacheKey(data.{{.Primary.CamelName}})
{{end -}}
return m.Exec(ctx, func() error {
return m.conn(ctx, db...).Create(data).Error
Expand All @@ -96,7 +110,7 @@ func (m *default{{.CamelName}}Model) Insert(ctx context.Context, data *{{.CamelN

func (m *default{{.CamelName}}Model) Update(ctx context.Context, data *{{.CamelName}}, db ...*gorm.DB) (err error) {
{{if .IsCache -}}
cacheKey := fmt.Sprintf("%s{{.PrimaryFmt}}", cache{{.CamelName}}PrimaryPrefix, {{.PrimaryFmtV}})
cacheKey := m.getPrimaryCacheKey(data.{{.Primary.CamelName}})
{{end -}}
return m.Exec(ctx, func() error {
return m.conn(ctx, db...).Model(&data).Updates(data).Error
Expand All @@ -105,7 +119,7 @@ func (m *default{{.CamelName}}Model) Update(ctx context.Context, data *{{.CamelN

func (m *default{{.CamelName}}Model) Delete(ctx context.Context, {{.PrimaryFields}}, db ...*gorm.DB) (err error) {
{{if .IsCache -}}
cacheKey := fmt.Sprintf("%s{{.PrimaryFmt}}", cache{{.CamelName}}PrimaryPrefix, {{.PrimaryFmtV2}})
cacheKey := m.getPrimaryCacheKey({{.PrimaryFmtV2}})
{{end -}}
return m.Exec(ctx, func() error {
return m.conn(ctx, db...).Where("{{.PrimaryFieldWhere}}", {{.PrimaryFmtV2}}).Delete({{.CamelName}}{}).Error
Expand All @@ -114,7 +128,7 @@ func (m *default{{.CamelName}}Model) Delete(ctx context.Context, {{.PrimaryField

func (m *default{{.CamelName}}Model) ForceDelete(ctx context.Context, {{.PrimaryFields}}, db ...*gorm.DB) (err error) {
{{if .IsCache -}}
cacheKey := fmt.Sprintf("%s{{.PrimaryFmt}}", cache{{.CamelName}}PrimaryPrefix, {{.PrimaryFmtV2}})
cacheKey := m.getPrimaryCacheKey({{.PrimaryFmtV2}})
{{end -}}
return m.Exec(ctx, func() error {
return m.conn(ctx, db...).Unscoped().Where("{{.PrimaryFieldWhere}}", {{.PrimaryFmtV2}}).Delete({{.CamelName}}{}).Error
Expand All @@ -130,7 +144,7 @@ func (m *default{{.CamelName}}Model) Count(ctx context.Context, cond *{{.CamelNa

func (m *default{{.CamelName}}Model) FindOne(ctx context.Context, {{.PrimaryFields}}) (data *{{.CamelName}}, err error) {
{{if .IsCache -}}
cacheKey := fmt.Sprintf("%s{{.PrimaryFmt}}", cache{{.CamelName}}PrimaryPrefix, {{.PrimaryFmtV2}})
cacheKey := m.getPrimaryCacheKey({{.PrimaryFmtV2}})
{{end -}}
err = m.QueryRow(ctx, &data, func(v interface{}) error {
tx := m.conn(ctx).Where("{{.PrimaryFieldWhere}}", {{.PrimaryFmtV2}}).Find(v)
Expand Down Expand Up @@ -163,8 +177,22 @@ func (m *default{{.CamelName}}Model) FindListByPage(ctx context.Context, cond *{
searchPlusScope(cond.SearchPlus, m.table),
orderScope(cond.OrderSort...),
pageScope(cond.PageCurrent, cond.PageSize),
)
err = conn.Where(cond.{{.CamelName}}).Find(&list).Error
).Where(cond.{{.CamelName}})

{{if .IsCache}}
ids := make([]{{.Primary.DataType}}, 0, cond.PageSize)
if err = conn.Pluck("{{.Primary.Name}}", &ids).Error; err != nil {
return nil, err
}
if len(ids) == 0 {
return list, nil
}

list, err = m.FindListByIds(ctx, ids)
{{else}}
err = conn.Find(&list).Error
{{end}}

return list, err
}

Expand All @@ -186,3 +214,60 @@ func (m *default{{.CamelName}}Model) FindAll(ctx context.Context, cond *{{.Camel
err = conn.Where(cond.{{.CamelName}}).Find(&list).Error
return list, err
}

func (m *default{{.CamelName}}Model) FindListByIds(ctx context.Context, ids []{{.Primary.DataType}}) (list []*{{.CamelName}}, err error) {
{{if .IsCache}}
idKeys := m.getCacheKeysByIds(ids)
jsonArr, err := m.redis.Mget(idKeys...)
if err != nil {
return list, nil
}
list = utilx.Json2StructArr[{{.CamelName}}](jsonArr)
if len(list) == len(ids) {
return list, nil
}

notExistIds := make([]{{.Primary.DataType}}, 0)
set := collectx.NewSet[int64]()
for _, item := range list {
set.Add(item.{{.Primary.CamelName}})
}
for _, id := range ids {
if set.Add(id) {
notExistIds = append(notExistIds, id)
}
}
dbList := make([]*{{.CamelName}}, 0, len(notExistIds))
err = m.conn(ctx).Where("{{.Primary.Name}} in ?", notExistIds).Find(&dbList).Error
if err != nil {
return nil, err
}

_ = m.redis.PipelinedCtx(ctx, func(pipeliner redis.Pipeliner) error {
for _, item := range dbList {
key := m.getPrimaryCacheKey(item.{{.Primary.CamelName}})
pipeliner.SetEX(ctx, key, item, m.expire)
}
return nil
})

return append(list, dbList...), nil
{{else}}
{{end}}
}


{{if .IsCache}}
func (m *default{{.CamelName}}Model) getPrimaryCacheKey({{.PrimaryFields}}) string {
return fmt.Sprintf("%s{{.PrimaryFmt}}", cache{{.CamelName}}PrimaryPrefix, {{.PrimaryFmtV2}})
}

func (m *default{{.CamelName}}Model) getCacheKeysByIds(ids []{{.Primary.DataType}}) []string {
idKeys := make([]string, 0, len(ids))
for _, id := range ids {
idKeys = append(idKeys, fmt.Sprintf("%s%v", cachePicturePrimaryPrefix, id))
}
return idKeys
}

{{end}}
Loading