From b4201a0054369a8cd89e940947fd6f1a89f357c2 Mon Sep 17 00:00:00 2001 From: wangpengfei <274878379@qq.com> Date: 星期三, 02 八月 2023 13:38:24 +0800 Subject: [PATCH] Merge branch 'master' into fly --- model/subOrder.go | 40 ++++++++++++++++++++++++++++++++++++---- 1 files changed, 36 insertions(+), 4 deletions(-) diff --git a/model/subOrder.go b/model/subOrder.go index 60eb98c..42bb6eb 100644 --- a/model/subOrder.go +++ b/model/subOrder.go @@ -19,7 +19,12 @@ SubOrderSearch struct { SubOrder - Orm *gorm.DB + + Orm *gorm.DB + Keyword string + OrderBy string + PageNum int + PageSize int } ) @@ -35,6 +40,9 @@ func (slf *SubOrderSearch) build() *gorm.DB { var db = slf.Orm.Model(&SubOrder{}) + if slf.Keyword != "" { + db = db.Where("name LIKE ?", "%"+slf.Keyword+"%") + } if slf.Id != 0 { db = db.Where("id = ?", slf.Id) } @@ -63,13 +71,37 @@ return } -func (slf *SubOrderSearch) FindAll() (result []*SubOrder, err error) { +func (slf *SubOrderSearch) FindAll() ([]*SubOrder, int64, error) { var db = slf.build() - err = db.Preload("Products").Preload("Client").Find(&result).Error - return + var records = make([]*SubOrder, 0) + var total int64 + if err := db.Count(&total).Error; err != nil { + return records, total, err + } + if slf.PageNum > 0 && slf.PageSize > 0 { + db = db.Limit(slf.PageSize).Offset((slf.PageNum - 1) * slf.PageSize) + } + + err := db.Preload("Products").Preload("Client").Find(&records).Error + return records, total, err } func (slf *SubOrderSearch) SetId(id int) *SubOrderSearch { slf.Id = id return slf } + +func (slf *SubOrderSearch) SetKeyword(keyword string) *SubOrderSearch { + slf.Keyword = keyword + return slf +} + +func (slf *SubOrderSearch) SetPage(page, size int) *SubOrderSearch { + slf.PageNum, slf.PageSize = page, size + return slf +} + +func (slf *SubOrderSearch) SetOrder(order string) *SubOrderSearch { + slf.OrderBy = order + return slf +} \ No newline at end of file -- Gitblit v1.8.0