File was renamed from model/contacts.go |
| | |
| | | |
| | | ContactSearch struct { |
| | | Contact |
| | | |
| | | Orm *gorm.DB |
| | | Keyword string |
| | | OrderBy string |
| | | PageNum int |
| | | PageSize int |
| | | } |
| | | |
| | | ContactDetail struct { |
| | |
| | | |
| | | 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) |
| | | } |
| | |
| | | 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 { |
| | |
| | | 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 |
| | | } |