| 25 | } |
| 26 | |
| 27 | func GetAllRedemptions(startIdx int, num int) (redemptions []*Redemption, total int64, err error) { |
| 28 | // 开始事务 |
| 29 | tx := DB.Begin() |
| 30 | if tx.Error != nil { |
| 31 | return nil, 0, tx.Error |
| 32 | } |
| 33 | defer func() { |
| 34 | if r := recover(); r != nil { |
| 35 | tx.Rollback() |
| 36 | } |
| 37 | }() |
| 38 | |
| 39 | // 获取总数 |
| 40 | err = tx.Model(&Redemption{}).Count(&total).Error |
| 41 | if err != nil { |
| 42 | tx.Rollback() |
| 43 | return nil, 0, err |
| 44 | } |
| 45 | |
| 46 | // 获取分页数据 |
| 47 | err = tx.Order("id desc").Limit(num).Offset(startIdx).Find(&redemptions).Error |
| 48 | if err != nil { |
| 49 | tx.Rollback() |
| 50 | return nil, 0, err |
| 51 | } |
| 52 | |
| 53 | // 提交事务 |
| 54 | if err = tx.Commit().Error; err != nil { |
| 55 | return nil, 0, err |
| 56 | } |
| 57 | |
| 58 | return redemptions, total, nil |
| 59 | } |
| 60 | |
| 61 | func SearchRedemptions(keyword string, startIdx int, num int) (redemptions []*Redemption, total int64, err error) { |
| 62 | tx := DB.Begin() |