refactor(ota): 移除 OTA 相关冗余字段及清理代码
- 从 OTA 结构体中删除 pawVerCode、pawVerName、pawUrl、pawMd5 字段 - 更新数据库查询,去除对上述字段的扫描 - 调整 SQL 迁移脚本,删除对应的数据库列 - 简化相关存储库方法的查询逻辑,提升代码维护性
This commit is contained in:
+14
-18
@@ -28,24 +28,20 @@ var _ sql.Scanner = (*BoolInt)(nil)
|
|||||||
|
|
||||||
// OTA 固件升级记录
|
// OTA 固件升级记录
|
||||||
type OTA struct {
|
type OTA struct {
|
||||||
ID int `json:"-"`
|
ID int `json:"-"`
|
||||||
VerCode int `json:"verCode"`
|
VerCode int `json:"verCode"`
|
||||||
VerName string `json:"verName"`
|
VerName string `json:"verName"`
|
||||||
URL string `json:"url"`
|
URL string `json:"url"`
|
||||||
MD5 string `json:"md5"`
|
MD5 string `json:"md5"`
|
||||||
Force BoolInt `json:"force"`
|
Force BoolInt `json:"force"`
|
||||||
Desc *string `json:"desc,omitempty"`
|
Desc *string `json:"desc,omitempty"`
|
||||||
Model *string `json:"model,omitempty"`
|
Model *string `json:"model,omitempty"`
|
||||||
HW int `json:"hw"`
|
HW int `json:"hw"`
|
||||||
Target BoolInt `json:"target"`
|
Target BoolInt `json:"target"`
|
||||||
Beta BoolInt `json:"beta"`
|
Beta BoolInt `json:"beta"`
|
||||||
PawVerCode int `json:"-"`
|
StartTime *time.Time `json:"startTime,omitempty"`
|
||||||
PawVerName string `json:"-"`
|
EndTime *time.Time `json:"endTime,omitempty"`
|
||||||
PawURL string `json:"-"`
|
Status int `json:"-"`
|
||||||
PawMD5 string `json:"-"`
|
|
||||||
StartTime *time.Time `json:"startTime,omitempty"`
|
|
||||||
EndTime *time.Time `json:"endTime,omitempty"`
|
|
||||||
Status int `json:"-"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// BlackList OTA 黑名单
|
// BlackList OTA 黑名单
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ func NewOTARepository(db *sql.DB) *OTARepository {
|
|||||||
// GetLatestOTA 按 model+hw+beta+status=1 查询最新一条 OTA 记录(verCode 降序)
|
// GetLatestOTA 按 model+hw+beta+status=1 查询最新一条 OTA 记录(verCode 降序)
|
||||||
func (r *OTARepository) GetLatestOTA(ctx context.Context, modelVal string, hw, beta int) (*model.OTA, error) {
|
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,
|
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
|
FROM ota WHERE model = ? AND hw = ? AND beta = ? AND status = 1
|
||||||
ORDER BY verCode DESC LIMIT 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(
|
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.ID, &o.VerCode, &o.VerName, &o.URL, &o.MD5, &o.Force, &desc, &mdl,
|
||||||
&o.HW, &o.Target, &o.Beta,
|
&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 {
|
if err == sql.ErrNoRows {
|
||||||
return nil, nil
|
return nil, nil
|
||||||
@@ -57,7 +57,7 @@ func (r *OTARepository) GetLatestOTA(ctx context.Context, modelVal string, hw, b
|
|||||||
// GetLatestOTANotInBlackList 查询最新一条不在黑名单中的 OTA 记录
|
// GetLatestOTANotInBlackList 查询最新一条不在黑名单中的 OTA 记录
|
||||||
func (r *OTARepository) GetLatestOTANotInBlackList(ctx context.Context, modelVal string, hw, beta int, mac string) (*model.OTA, error) {
|
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,
|
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 = ?
|
FROM ota WHERE status = 1 AND model = ? AND hw = ? AND beta = ?
|
||||||
AND id NOT IN (SELECT ota_id FROM black_list WHERE mac = ?)
|
AND id NOT IN (SELECT ota_id FROM black_list WHERE mac = ?)
|
||||||
ORDER BY verCode DESC LIMIT 1`
|
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(
|
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.ID, &o.VerCode, &o.VerName, &o.URL, &o.MD5, &o.Force, &desc, &mdl,
|
||||||
&o.HW, &o.Target, &o.Beta,
|
&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 {
|
if err == sql.ErrNoRows {
|
||||||
return nil, nil
|
return nil, nil
|
||||||
@@ -96,7 +96,7 @@ func (r *OTARepository) GetLatestOTANotInBlackList(ctx context.Context, modelVal
|
|||||||
// GetLatestOTANotTarget 查询最新一条非定向的 OTA 记录
|
// GetLatestOTANotTarget 查询最新一条非定向的 OTA 记录
|
||||||
func (r *OTARepository) GetLatestOTANotTarget(ctx context.Context, modelVal string, hw, beta int) (*model.OTA, error) {
|
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,
|
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
|
FROM ota WHERE status = 1 AND model = ? AND hw = ? AND beta = ? AND target = 0
|
||||||
ORDER BY verCode DESC LIMIT 1`
|
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(
|
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.ID, &o.VerCode, &o.VerName, &o.URL, &o.MD5, &o.Force, &desc, &mdl,
|
||||||
&o.HW, &o.Target, &o.Beta,
|
&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 {
|
if err == sql.ErrNoRows {
|
||||||
return nil, nil
|
return nil, nil
|
||||||
|
|||||||
@@ -33,10 +33,6 @@ CREATE TABLE `ota` (
|
|||||||
`hw` int NOT NULL DEFAULT 0 COMMENT '硬件版本号',
|
`hw` int NOT NULL DEFAULT 0 COMMENT '硬件版本号',
|
||||||
`target` tinyint NOT NULL DEFAULT 0 COMMENT '是否定向,1-是,0-否;否表示面向所有用户',
|
`target` tinyint NOT NULL DEFAULT 0 COMMENT '是否定向,1-是,0-否;否表示面向所有用户',
|
||||||
`beta` 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 '升级开始时间',
|
`startTime` datetime NULL DEFAULT NULL COMMENT '升级开始时间',
|
||||||
`endTime` datetime NULL DEFAULT NULL COMMENT '升级结束时间',
|
`endTime` datetime NULL DEFAULT NULL COMMENT '升级结束时间',
|
||||||
`status` tinyint NOT NULL DEFAULT 1 COMMENT '是否可用',
|
`status` tinyint NOT NULL DEFAULT 1 COMMENT '是否可用',
|
||||||
|
|||||||
Reference in New Issue
Block a user