| | |
| | | |
| | | SubOrderSearch struct { |
| | | SubOrder |
| | | |
| | | Orm *gorm.DB |
| | | Keyword string |
| | | OrderBy string |
| | | PageNum int |
| | | PageSize int |
| | | } |
| | | ) |
| | | |
| | |
| | | |
| | | 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) |
| | | } |
| | |
| | | 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 |
| | | } |