| 58 | } |
| 59 | |
| 60 | func getPriority(group string, model string, retry int) (int, error) { |
| 61 | |
| 62 | var priorities []int |
| 63 | err := DB.Model(&Ability{}). |
| 64 | Select("DISTINCT(priority)"). |
| 65 | Where(commonGroupCol+" = ? and model = ? and enabled = ?", group, model, true). |
| 66 | Order("priority DESC"). // 按优先级降序排序 |
| 67 | Pluck("priority", &priorities).Error // Pluck用于将查询的结果直接扫描到一个切片中 |
| 68 | |
| 69 | if err != nil { |
| 70 | // 处理错误 |
| 71 | return 0, err |
| 72 | } |
| 73 | |
| 74 | if len(priorities) == 0 { |
| 75 | // 如果没有查询到优先级,则返回错误 |
| 76 | return 0, errors.New("数据库一致性被破坏") |
| 77 | } |
| 78 | |
| 79 | // 确定要使用的优先级 |
| 80 | var priorityToUse int |
| 81 | if retry >= len(priorities) { |
| 82 | // 如果重试次数大于优先级数,则使用最小的优先级 |
| 83 | priorityToUse = priorities[len(priorities)-1] |
| 84 | } else { |
| 85 | priorityToUse = priorities[retry] |
| 86 | } |
| 87 | return priorityToUse, nil |
| 88 | } |
| 89 | |
| 90 | func getChannelQuery(group string, model string, retry int) (*gorm.DB, error) { |
| 91 | maxPrioritySubQuery := DB.Model(&Ability{}).Select("MAX(priority)").Where(commonGroupCol+" = ? and model = ? and enabled = ?", group, model, true) |