From a0908b00bc206bffa02293a491e52ede6e83b6bb Mon Sep 17 00:00:00 2001 From: jiang4869 <1121429190@qq.com> Date: Tue, 4 Jan 2022 17:28:11 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E4=BA=86=E5=AF=B9?= =?UTF-8?q?=E5=88=86=E9=A1=B5=E6=9F=A5=E8=AF=A2=E7=9A=84=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- data/view/genfunc/def.go | 165 +++++++++++++++++++++++++++++++++++ data/view/genfunc/genfunc.go | 4 + data/view/model/model.go | 16 ++++ 3 files changed, 185 insertions(+) diff --git a/data/view/genfunc/def.go b/data/view/genfunc/def.go index 13b73f0..7b830dc 100755 --- a/data/view/genfunc/def.go +++ b/data/view/genfunc/def.go @@ -257,6 +257,30 @@ func (obj *_{{$obj.StructName}}Mgr) GetByOptions(opts ...Option) (results []*{{$ {{GenPreloadList $obj.PreloadList true}} 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 //////////////////////////////////////////// {{range $oem := $obj.Em}} @@ -326,4 +350,145 @@ func (obj *_{{$obj.StructName}}Mgr) GetBatchFrom{{$oem.ColStructName}}({{CapLowe } {{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} +} +` ) diff --git a/data/view/genfunc/genfunc.go b/data/view/genfunc/genfunc.go index bcc253b..96bef25 100644 --- a/data/view/genfunc/genfunc.go +++ b/data/view/genfunc/genfunc.go @@ -27,3 +27,7 @@ func GetGenPreloadTemp(multi bool) string { } return genPreload } + +func GetGenPageTemp() string { + return genPage +} \ No newline at end of file diff --git a/data/view/model/model.go b/data/view/model/model.go index abab3ec..19de35a 100755 --- a/data/view/model/model.go +++ b/data/view/model/model.go @@ -323,6 +323,22 @@ func (m *_Model) generateFunc() (genOut []GenOutInfo) { //tools.WriteFile(outDir+"gen_router.go", []string{buf.String()}, true) // -------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 { var pkg genstruct.GenPackage pkg.SetPackage(m.info.PackageName) //package name From b056057690356347f44e3632255fd4269042530c Mon Sep 17 00:00:00 2001 From: jiang4869 <1121429190@qq.com> Date: Tue, 4 Jan 2022 17:42:13 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E4=BA=86=E5=BD=93?= =?UTF-8?q?=E5=AD=97=E6=AE=B5=E7=B1=BB=E5=9E=8B=E4=B8=BAbool=E6=88=96?= =?UTF-8?q?=E8=80=85string=E7=B1=BB=E5=9E=8B=E5=B9=B6=E4=B8=94=E9=BB=98?= =?UTF-8?q?=E8=AE=A4=E5=80=BC=E4=B8=BANULL=E6=97=B6=EF=BC=8C=E5=9C=A8?= =?UTF-8?q?=E7=BB=93=E6=9E=84=E4=B8=AD=E6=B2=A1=E6=9C=89=E8=A2=AB=E8=AE=BE?= =?UTF-8?q?=E7=BD=AE=E6=88=90=E6=8C=87=E9=92=88=E7=B1=BB=E5=9E=8B=E7=9A=84?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- data/view/model/common.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/data/view/model/common.go b/data/view/model/common.go index 59b411b..ba0f6a4 100755 --- a/data/view/model/common.go +++ b/data/view/model/common.go @@ -103,6 +103,12 @@ func fixNullToPorint(name string, isNull bool) string { if strings.HasPrefix(name, "time") { return "*" + name } + if strings.HasPrefix(name, "bool") { + return "*" + name + } + if strings.HasPrefix(name, "string") { + return "*" + name + } } return name