From a52adf7e2751c907b901f8ade83f3e987ad621a5 Mon Sep 17 00:00:00 2001 From: eafonyang Date: Tue, 30 Jun 2026 11:05:55 +0800 Subject: [PATCH] =?UTF-8?q?refactor(ota):=20=E7=A7=BB=E9=99=A4=20OTA=20?= =?UTF-8?q?=E7=9B=B8=E5=85=B3=E5=86=97=E4=BD=99=E5=AD=97=E6=AE=B5=E5=8F=8A?= =?UTF-8?q?=E6=B8=85=E7=90=86=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 从 OTA 结构体中删除 pawVerCode、pawVerName、pawUrl、pawMd5 字段 - 更新数据库查询,去除对上述字段的扫描 - 调整 SQL 迁移脚本,删除对应的数据库列 - 简化相关存储库方法的查询逻辑,提升代码维护性 --- internal/model/ota.go | 32 ++++++++++++++------------------ internal/repository/ota.go | 12 ++++++------ sql/ota.sql | 4 ---- 3 files changed, 20 insertions(+), 28 deletions(-) diff --git a/internal/model/ota.go b/internal/model/ota.go index 63e57b0..3a2eeb6 100644 --- a/internal/model/ota.go +++ b/internal/model/ota.go @@ -28,24 +28,20 @@ var _ sql.Scanner = (*BoolInt)(nil) // OTA 固件升级记录 type OTA struct { - ID int `json:"-"` - VerCode int `json:"verCode"` - VerName string `json:"verName"` - URL string `json:"url"` - MD5 string `json:"md5"` - Force BoolInt `json:"force"` - Desc *string `json:"desc,omitempty"` - Model *string `json:"model,omitempty"` - HW int `json:"hw"` - Target BoolInt `json:"target"` - Beta BoolInt `json:"beta"` - PawVerCode int `json:"-"` - PawVerName string `json:"-"` - PawURL string `json:"-"` - PawMD5 string `json:"-"` - StartTime *time.Time `json:"startTime,omitempty"` - EndTime *time.Time `json:"endTime,omitempty"` - Status int `json:"-"` + ID int `json:"-"` + VerCode int `json:"verCode"` + VerName string `json:"verName"` + URL string `json:"url"` + MD5 string `json:"md5"` + Force BoolInt `json:"force"` + Desc *string `json:"desc,omitempty"` + Model *string `json:"model,omitempty"` + HW int `json:"hw"` + Target BoolInt `json:"target"` + Beta BoolInt `json:"beta"` + StartTime *time.Time `json:"startTime,omitempty"` + EndTime *time.Time `json:"endTime,omitempty"` + Status int `json:"-"` } // BlackList OTA 黑名单 diff --git a/internal/repository/ota.go b/internal/repository/ota.go index b863fd9..a9feb0f 100644 --- a/internal/repository/ota.go +++ b/internal/repository/ota.go @@ -19,7 +19,7 @@ func NewOTARepository(db *sql.DB) *OTARepository { // GetLatestOTA 按 model+hw+beta+status=1 查询最新一条 OTA 记录(verCode 降序) 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, - pawVerCode, pawVerName, pawUrl, pawMd5, startTime, endTime, status + startTime, endTime, status FROM ota WHERE model = ? AND hw = ? AND beta = ? AND status = 1 ORDER BY verCode DESC LIMIT 1` @@ -30,7 +30,7 @@ func (r *OTARepository) GetLatestOTA(ctx context.Context, modelVal string, hw, b err := r.db.QueryRowContext(ctx, query, modelVal, hw, beta).Scan( &o.ID, &o.VerCode, &o.VerName, &o.URL, &o.MD5, &o.Force, &desc, &mdl, &o.HW, &o.Target, &o.Beta, - &o.PawVerCode, &o.PawVerName, &o.PawURL, &o.PawMD5, &startTime, &endTime, &o.Status, + &startTime, &endTime, &o.Status, ) if err == sql.ErrNoRows { return nil, nil @@ -57,7 +57,7 @@ func (r *OTARepository) GetLatestOTA(ctx context.Context, modelVal string, hw, b // GetLatestOTANotInBlackList 查询最新一条不在黑名单中的 OTA 记录 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, - pawVerCode, pawVerName, pawUrl, pawMd5, startTime, endTime, status + 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` @@ -69,7 +69,7 @@ func (r *OTARepository) GetLatestOTANotInBlackList(ctx context.Context, modelVal err := r.db.QueryRowContext(ctx, query, modelVal, hw, beta, mac).Scan( &o.ID, &o.VerCode, &o.VerName, &o.URL, &o.MD5, &o.Force, &desc, &mdl, &o.HW, &o.Target, &o.Beta, - &o.PawVerCode, &o.PawVerName, &o.PawURL, &o.PawMD5, &startTime, &endTime, &o.Status, + &startTime, &endTime, &o.Status, ) if err == sql.ErrNoRows { return nil, nil @@ -96,7 +96,7 @@ func (r *OTARepository) GetLatestOTANotInBlackList(ctx context.Context, modelVal // GetLatestOTANotTarget 查询最新一条非定向的 OTA 记录 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, - pawVerCode, pawVerName, pawUrl, pawMd5, startTime, endTime, status + startTime, endTime, status FROM ota WHERE status = 1 AND model = ? AND hw = ? AND beta = ? AND target = 0 ORDER BY verCode DESC LIMIT 1` @@ -107,7 +107,7 @@ func (r *OTARepository) GetLatestOTANotTarget(ctx context.Context, modelVal stri err := r.db.QueryRowContext(ctx, query, modelVal, hw, beta).Scan( &o.ID, &o.VerCode, &o.VerName, &o.URL, &o.MD5, &o.Force, &desc, &mdl, &o.HW, &o.Target, &o.Beta, - &o.PawVerCode, &o.PawVerName, &o.PawURL, &o.PawMD5, &startTime, &endTime, &o.Status, + &startTime, &endTime, &o.Status, ) if err == sql.ErrNoRows { return nil, nil diff --git a/sql/ota.sql b/sql/ota.sql index e8653b6..7edfbdd 100644 --- a/sql/ota.sql +++ b/sql/ota.sql @@ -33,10 +33,6 @@ CREATE TABLE `ota` ( `hw` int NOT NULL DEFAULT 0 COMMENT '硬件版本号', `target` tinyint NOT NULL DEFAULT 0 COMMENT '是否定向,1-是,0-否;否表示面向所有用户', `beta` tinyint NOT NULL DEFAULT 0 COMMENT '是否灰度,1-是,0-否', - `pawVerCode` int NOT NULL DEFAULT 0, - `pawVerName` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL DEFAULT '', - `pawUrl` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL DEFAULT '', - `pawMd5` char(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL DEFAULT '', `startTime` datetime NULL DEFAULT NULL COMMENT '升级开始时间', `endTime` datetime NULL DEFAULT NULL COMMENT '升级结束时间', `status` tinyint NOT NULL DEFAULT 1 COMMENT '是否可用',