| | |
| | | |
| | | ContractSearch struct { |
| | | Contract |
| | | Orm *gorm.DB |
| | | |
| | | Orm *gorm.DB |
| | | Keyword string |
| | | OrderBy string |
| | | PageNum int |
| | | PageSize int |
| | | |
| | | } |
| | | ) |
| | | |
| | |
| | | |
| | | func (slf *ContractSearch) build() *gorm.DB { |
| | | var db = slf.Orm.Model(&Contract{}) |
| | | 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 *ContractSearch) FindAll() ([]*Contract, error) { |
| | | func (slf *ContractSearch) FindAll() ([]*Contract, int64, error) { |
| | | var db = slf.build() |
| | | var records = make([]*Contract, 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) |
| | | } |
| | | |
| | | if slf.PageNum > 0 && slf.PageSize > 0 { |
| | | db = db.Limit(slf.PageSize).Offset((slf.PageNum - 1) * slf.PageSize) |
| | | } |
| | | |
| | | err := db.Preload("Quotation").Find(&records).Error |
| | | return records, err |
| | | return records, total, err |
| | | } |
| | | |
| | | func (slf *ContractSearch) SetId(id int) *ContractSearch { |
| | | slf.Id = id |
| | | return slf |
| | | } |
| | | |
| | | func (slf *ContractSearch) SetKeyword(keyword string) *ContractSearch { |
| | | slf.Keyword = keyword |
| | | return slf |
| | | } |
| | | |
| | | func (slf *ContractSearch) SetPage(page, size int) *ContractSearch { |
| | | slf.PageNum, slf.PageSize = page, size |
| | | return slf |
| | | } |
| | | |
| | | func (slf *ContractSearch) SetOrder(order string) *ContractSearch { |
| | | slf.OrderBy = order |
| | | return slf |
| | | } |