fix(repository): 优化 OTA 查询排序规则,确保取最新录入记录

- 将 GetLatestOTA 查询结果排序规则由单一 verCode 降序调整为 verCode 降序、id 降序
- 同步更新 GetLatestOTANotInBlackList 查询排序规则,保持一致性
- 同步更新 GetLatestOTANotTarget 查询排序规则,保持一致性
- 在 GetVerNameByVersion 方法中,增加按 id 降序排序以获取最后录入的版本名称
- 添加注释说明排序规则调整及其目的
This commit is contained in:
eafonyang
2026-09-03 17:57:28 +08:00
parent dd92845bc2
commit 0aad0f9c1d
2 changed files with 10 additions and 6 deletions
+9 -5
View File
@@ -16,12 +16,13 @@ func NewOTARepository(db *sql.DB) *OTARepository {
return &OTARepository{db: db}
}
// GetLatestOTA 按 model+hw+beta+status=1 查询最新一条 OTA 记录verCode 降序)
// GetLatestOTA 按 model+hw+beta+status=1 查询最新一条 OTA 记录
// 排序规则:verCode 降序;verCode 相同时取 id 较大的(即最后录入的)那条
func (r *OTARepository) GetLatestOTA(ctx context.Context, modelVal string, hw, beta int) (*model.OTA, error) {
const query = `SELECT id, verCode, verName, url, md5, ` + "`force`" + `, ` + "`desc`" + `, model, hw, target, beta,
startTime, endTime, status
FROM ota WHERE model = ? AND hw = ? AND beta = ? AND status = 1
ORDER BY verCode DESC LIMIT 1`
ORDER BY verCode DESC, id DESC LIMIT 1`
var o model.OTA
var desc, mdl sql.NullString
@@ -55,12 +56,13 @@ func (r *OTARepository) GetLatestOTA(ctx context.Context, modelVal string, hw, b
}
// GetLatestOTANotInBlackList 查询最新一条不在黑名单中的 OTA 记录
// 排序规则与 GetLatestOTA 一致:verCode 降序,verCode 相同时取 id 较大的那条
func (r *OTARepository) GetLatestOTANotInBlackList(ctx context.Context, modelVal string, hw, beta int, mac string) (*model.OTA, error) {
const query = `SELECT id, verCode, verName, url, md5, ` + "`force`" + `, ` + "`desc`" + `, model, hw, target, beta,
startTime, endTime, status
FROM ota WHERE status = 1 AND model = ? AND hw = ? AND beta = ?
AND id NOT IN (SELECT ota_id FROM black_list WHERE mac = ?)
ORDER BY verCode DESC LIMIT 1`
ORDER BY verCode DESC, id DESC LIMIT 1`
var o model.OTA
var desc, mdl sql.NullString
@@ -94,11 +96,12 @@ func (r *OTARepository) GetLatestOTANotInBlackList(ctx context.Context, modelVal
}
// GetLatestOTANotTarget 查询最新一条非定向的 OTA 记录
// 排序规则与 GetLatestOTA 一致:verCode 降序,verCode 相同时取 id 较大的那条
func (r *OTARepository) GetLatestOTANotTarget(ctx context.Context, modelVal string, hw, beta int) (*model.OTA, error) {
const query = `SELECT id, verCode, verName, url, md5, ` + "`force`" + `, ` + "`desc`" + `, model, hw, target, beta,
startTime, endTime, status
FROM ota WHERE status = 1 AND model = ? AND hw = ? AND beta = ? AND target = 0
ORDER BY verCode DESC LIMIT 1`
ORDER BY verCode DESC, id DESC LIMIT 1`
var o model.OTA
var desc, mdl sql.NullString
@@ -132,8 +135,9 @@ func (r *OTARepository) GetLatestOTANotTarget(ctx context.Context, modelVal stri
}
// GetVerNameByVersion 按 model+verCode 查询版本名称
// 同一 model+verCode 存在多条记录时,取 id 较大的(即最后录入的)那条
func (r *OTARepository) GetVerNameByVersion(ctx context.Context, modelVal string, version int) (string, bool, error) {
const query = `SELECT verName FROM ota WHERE model = ? AND verCode = ? LIMIT 1`
const query = `SELECT verName FROM ota WHERE model = ? AND verCode = ? ORDER BY id DESC LIMIT 1`
var verName string
err := r.db.QueryRowContext(ctx, query, modelVal, version).Scan(&verName)