| | |
| | | |
| | | ClientSearch struct { |
| | | Client |
| | | Orm *gorm.DB |
| | | |
| | | Orm *gorm.DB |
| | | Keyword string |
| | | OrderBy string |
| | | PageNum int |
| | | PageSize int |
| | | } |
| | | ) |
| | | |
| | |
| | | |
| | | func (slf *ClientSearch) build() *gorm.DB { |
| | | var db = slf.Orm.Model(&Client{}) |
| | | if slf.Keyword != "" { |
| | | db = db.Where("name LIKE ?", "%"+slf.Keyword+"%") |
| | | } |
| | | if slf.Id != 0 { |
| | | db.Where("id = ?", slf.Id) |
| | | } |
| | |
| | | return record, err |
| | | } |
| | | |
| | | func (slf *ClientSearch) Find() ([]*Client, error) { |
| | | func (slf *ClientSearch) Find() ([]*Client, int64, error) { |
| | | var db = slf.build() |
| | | var records = make([]*Client, 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("ClientStatus").Preload("ClientType").Preload("ClientOrigin").Preload("ClientLevel").Preload("FollowRecord").Preload("EnterpriseNature").Preload("RegisteredCapital").Preload("Industry").Preload("EnterpriseScale").Preload("Contacts").Preload("Country").Preload("Province").Preload("City").Preload("Region").Find(&records).Error |
| | | return records, err |
| | | return records, total, err |
| | | } |
| | | |
| | | func (slf *ClientSearch) UpdateByMap(data map[string]interface{}) error { |
| | | var db = slf.build() |
| | | return db.Updates(data).Error |
| | | } |
| | | |
| | | func (slf *ClientSearch) SetKeyword(keyword string) *ClientSearch { |
| | | slf.Keyword = keyword |
| | | return slf |
| | | } |
| | | |
| | | func (slf *ClientSearch) SetPage(page, size int) *ClientSearch { |
| | | slf.PageNum, slf.PageSize = page, size |
| | | return slf |
| | | } |
| | | |
| | | func (slf *ClientSearch) SetOrder(order string) *ClientSearch { |
| | | slf.OrderBy = order |
| | | return slf |
| | | } |
| | | func (slf *ClientSearch) SetIds(ids []int) *ClientSearch { |
| | | slf.Orm = slf.Orm.Where("id in (?)", ids) |
| | | return slf |
| | | } |