Merge pull request #187 from jiang4869/master

增加了对分页查询的支持
master
xxj 3 years ago committed by GitHub
commit 3c8e9fbe13
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -257,6 +257,30 @@ func (obj *_{{$obj.StructName}}Mgr) GetByOptions(opts ...Option) (results []*{{$
{{GenPreloadList $obj.PreloadList true}} {{GenPreloadList $obj.PreloadList true}}
return return
} }
// SelectPage 分页查询
func (obj *_{{$obj.StructName}}Mgr) SelectPage(page IPage,opts ...Option) (resultPage IPage, err error) {
options := options{
query: make(map[string]interface{}, len(opts)),
}
for _, o := range opts {
o.apply(&options)
}
resultPage = page
results := make([]{{$obj.StructName}},0)
var count int64 // 统计总的记录数
query := obj.DB.WithContext(obj.ctx).Model({{$obj.StructName}}{}).Where(options.query)
query.Count(&count)
resultPage.SetTotal(count)
if len(page.GetOrederItemsString()) > 0 {
query = query.Order(page.GetOrederItemsString())
}
err = query.Limit(int(page.GetSize())).Offset(int(page.Offset())).Find(&results).Error
{{GenPreloadList $obj.PreloadList true}}
resultPage.SetRecords(results)
return
}
//////////////////////////enume case //////////////////////////////////////////// //////////////////////////enume case ////////////////////////////////////////////
{{range $oem := $obj.Em}} {{range $oem := $obj.Em}}
@ -326,4 +350,145 @@ func (obj *_{{$obj.StructName}}Mgr) GetBatchFrom{{$oem.ColStructName}}({{CapLowe
} {{end}} {{end}} } {{end}} {{end}}
} }
}` }`
genPage = `package {{.PackageName}}
import (
"fmt"
"strings"
)
type IPage interface {
GetRecords() interface{} // 获取查询的记录
SetRecords(interface{}) // 设置查询的记录
GetTotal() int64 // 获取总记录数
SetTotal(int64) // 设置总记录数
GetCurrent() int64 // 获取当前页
SetCurrent(int64) // 设置当前页
GetSize() int64 // 获取每页显示大小
SetSize(int64) // 设置每页显示大小
AddOrderItem(OrderItem) // 设置排序条件
AddOrderItems([]OrderItem) // 批量设置排序条件
GetOrederItemsString() string // 将排序条件拼接成字符串
Offset() int64 // 获取偏移量
GetPages() int64 // 获取总的分页数
}
type Page struct {
total int64 // 总的记录数
size int64 // 每页显示的大小
current int64 // 当前页
orders []OrderItem // 排序条件
Records interface{} // 查询数据列表
}
func (page *Page) GetRecords() interface{} {
return page.Records
}
func (page *Page) SetRecords(records interface{}) {
page.Records = records
}
func (page *Page) GetTotal() int64 {
return page.total
}
func (page *Page) SetTotal(total int64) {
page.total = total
}
func (page *Page) GetCurrent() int64 {
return page.current
}
func (page *Page) SetCurrent(current int64) {
page.current = current
}
func (page *Page) GetSize() int64 {
return page.size
}
func (page *Page) SetSize(size int64) {
page.size = size
}
func (page *Page) AddOrderItem(orderItem OrderItem) {
page.orders = append(page.orders, orderItem)
}
func (page *Page) AddOrderItems(orderItems []OrderItem) {
page.orders = append(page.orders, orderItems...)
}
func (page *Page) GetOrederItemsString() string {
arr := make([]string, 0)
var order string
for _, val := range page.orders {
if val.asc {
order = ""
} else {
order = "desc"
}
arr = append(arr, fmt.Sprintf("%s %s", val.column, order))
}
return strings.Join(arr, ",")
}
func (page *Page) Offset() int64 {
if page.GetCurrent() > 0 {
return (page.GetCurrent() - 1) * page.GetSize()
} else {
return 0
}
}
func (page *Page) GetPages() int64 {
if page.GetSize() == 0 {
return 0
}
pages := page.GetTotal() / page.GetSize()
if page.GetTotal()%page.size != 0 {
pages++
}
return pages
}
type OrderItem struct {
column string // 需要排序的字段
asc bool // 是否正序排列默认true
}
func BuildAsc(column string) OrderItem {
return OrderItem{column: column, asc: true}
}
func BuildDesc(column string) OrderItem {
return OrderItem{column: column, asc: false}
}
func BuildAscs(columns ...string) []OrderItem {
items := make([]OrderItem, 0)
for _, val := range columns {
items = append(items, BuildAsc(val))
}
return items
}
func BuildDescs(columns ...string) []OrderItem {
items := make([]OrderItem, 0)
for _, val := range columns {
items = append(items, BuildDesc(val))
}
return items
}
func NewPage(size, current int64, orderItems ...OrderItem) *Page {
return &Page{size: size, current: current, orders: orderItems}
}
`
) )

@ -27,3 +27,7 @@ func GetGenPreloadTemp(multi bool) string {
} }
return genPreload return genPreload
} }
func GetGenPageTemp() string {
return genPage
}

@ -103,6 +103,12 @@ func fixNullToPorint(name string, isNull bool) string {
if strings.HasPrefix(name, "time") { if strings.HasPrefix(name, "time") {
return "*" + name return "*" + name
} }
if strings.HasPrefix(name, "bool") {
return "*" + name
}
if strings.HasPrefix(name, "string") {
return "*" + name
}
} }
return name return name

@ -323,6 +323,22 @@ func (m *_Model) generateFunc() (genOut []GenOutInfo) {
//tools.WriteFile(outDir+"gen_router.go", []string{buf.String()}, true) //tools.WriteFile(outDir+"gen_router.go", []string{buf.String()}, true)
// -------end------ // -------end------
// gen page 分页查询的基础
genPage, err := template.New("gen_page").Parse(genfunc.GetGenPageTemp())
if err != nil {
panic(err)
}
var bufPage bytes.Buffer
genPage.Execute(&bufPage, m.info)
genOut = append(genOut, GenOutInfo{
FileName: "gen.page.go",
FileCtx: bufPage.String(),
})
// -------end------
for _, tab := range m.info.TabList { for _, tab := range m.info.TabList {
var pkg genstruct.GenPackage var pkg genstruct.GenPackage
pkg.SetPackage(m.info.PackageName) //package name pkg.SetPackage(m.info.PackageName) //package name

Loading…
Cancel
Save