增加自动化排序功能

This commit is contained in:
piexlmax
2022-10-30 20:27:22 +08:00
parent c300e16de9
commit 192c0e529d
6 changed files with 54 additions and 6 deletions
@@ -17,4 +17,8 @@ type {{.StructName}}Search struct{
{{- end }}
{{- end }}
request.PageInfo
{{- if .NeedSort}}
Sort string `json:"sort" form:"sort"`
Order string `json:"order" form:"order"`
{{- end}}
}
@@ -112,6 +112,23 @@ func ({{.Abbreviation}}Service *{{.StructName}}Service)Get{{.StructName}}InfoLis
if err!=nil {
return
}
{{- if .NeedSort}}
var OrderStr string
orderMap := make(map[string]bool)
{{- range .Fields}}
{{- if .Sort}}
orderMap["{{.FieldJson}}"] = true
{{- end}}
{{- end}}
if orderMap[info.Sort] {
OrderStr = info.Sort
if info.Order == "descending" {
OrderStr = OrderStr + " desc"
}
db = db.Order(OrderStr)
}
{{- end}}
err = db.Limit(limit).Offset(offset).Find(&{{.Abbreviation}}s).Error
return {{.Abbreviation}}s, total, err
}