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/contact.go | 38 +++++++++++++++++++++++++++++++++++--- 1 files changed, 35 insertions(+), 3 deletions(-) diff --git a/model/contacts.go b/model/contact.go similarity index 81% rename from model/contacts.go rename to model/contact.go index 98d7805..3fc6048 100644 --- a/model/contacts.go +++ b/model/contact.go @@ -26,7 +26,12 @@ ContactSearch struct { Contact - Orm *gorm.DB + + Orm *gorm.DB + Keyword string + OrderBy string + PageNum int + PageSize int } ContactDetail struct { @@ -51,6 +56,9 @@ func (slf *ContactSearch) build() *gorm.DB { var db = slf.Orm.Model(&Contact{}) + if slf.Keyword != "" { + db = db.Where("name LIKE ?", "%"+slf.Keyword+"%") + } if slf.Id != 0 { db = db.Where("id = ?", slf.Id) } @@ -104,11 +112,20 @@ return record, err } -func (slf *ContactSearch) FindAll() ([]*ContactDetail, error) { +func (slf *ContactSearch) FindAll() ([]*ContactDetail, int64, error) { var db = slf.build() var records = make([]*ContactDetail, 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("FollowRecord").Preload("Client").Preload("Country").Preload("Province").Preload("City").Preload("Region").Find(&records).Error - return records, err + return records, total, err } func (slf *ContactSearch) SetId(id int) *ContactSearch { @@ -137,3 +154,18 @@ var db = slf.build() return db.Updates(data).Error } + +func (slf *ContactSearch) SetKeyword(keyword string) *ContactSearch { + slf.Keyword = keyword + return slf +} + +func (slf *ContactSearch) SetPage(page, size int) *ContactSearch { + slf.PageNum, slf.PageSize = page, size + return slf +} + +func (slf *ContactSearch) SetOrder(order string) *ContactSearch { + slf.OrderBy = order + return slf +} \ No newline at end of file -- Gitblit v1.8.0