fix cleanQueryData
This commit is contained in:
parent
ebdf38e1da
commit
91db6239d9
@ -2,12 +2,15 @@ package queue
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"time"
|
||||||
"github.com/hibiken/asynq"
|
|
||||||
"tydata-server/app/user/cmd/api/internal/svc"
|
"tydata-server/app/user/cmd/api/internal/svc"
|
||||||
|
|
||||||
|
"github.com/hibiken/asynq"
|
||||||
|
"github.com/zeromicro/go-zero/core/logx"
|
||||||
)
|
)
|
||||||
|
|
||||||
const TASKTIME = "32 * * * *"
|
// TASKTIME 定义为每天凌晨3点执行
|
||||||
|
const TASKTIME = "0 3 * * *"
|
||||||
|
|
||||||
type CleanQueryDataHandler struct {
|
type CleanQueryDataHandler struct {
|
||||||
svcCtx *svc.ServiceContext
|
svcCtx *svc.ServiceContext
|
||||||
@ -20,6 +23,19 @@ func NewCleanQueryDataHandler(svcCtx *svc.ServiceContext) *CleanQueryDataHandler
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (l *CleanQueryDataHandler) ProcessTask(ctx context.Context, t *asynq.Task) error {
|
func (l *CleanQueryDataHandler) ProcessTask(ctx context.Context, t *asynq.Task) error {
|
||||||
fmt.Println("企鹅企鹅企鹅企鹅企鹅企鹅企鹅企鹅企鹅企鹅企鹅企鹅企鹅企鹅企鹅企鹅企鹅企鹅企鹅企鹅企鹅企鹅企鹅企鹅企鹅企鹅企鹅企鹅企鹅企鹅企鹅企鹅-ProcessTask")
|
now := time.Now().Format("2006-01-02 15:04:05")
|
||||||
|
logx.Infof("%s - 开始执行查询数据清理任务", now)
|
||||||
|
|
||||||
|
// 计算3天前的时间
|
||||||
|
threeDaysAgo := time.Now().AddDate(0, 0, -3)
|
||||||
|
|
||||||
|
// 调用QueryModel删除3天前的数据
|
||||||
|
result, err := l.svcCtx.QueryModel.DeleteBefore(ctx, threeDaysAgo)
|
||||||
|
if err != nil {
|
||||||
|
logx.Errorf("%s - 清理查询数据失败: %v", time.Now().Format("2006-01-02 15:04:05"), err)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
logx.Infof("%s - 查询数据清理完成,共删除 %d 条记录", time.Now().Format("2006-01-02 15:04:05"), result)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,11 @@
|
|||||||
package model
|
package model
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"time"
|
||||||
|
"tydata-server/common/globalkey"
|
||||||
|
|
||||||
"github.com/zeromicro/go-zero/core/stores/cache"
|
"github.com/zeromicro/go-zero/core/stores/cache"
|
||||||
"github.com/zeromicro/go-zero/core/stores/sqlx"
|
"github.com/zeromicro/go-zero/core/stores/sqlx"
|
||||||
)
|
)
|
||||||
@ -12,6 +17,7 @@ type (
|
|||||||
// and implement the added methods in customQueryModel.
|
// and implement the added methods in customQueryModel.
|
||||||
QueryModel interface {
|
QueryModel interface {
|
||||||
queryModel
|
queryModel
|
||||||
|
DeleteBefore(ctx context.Context, before time.Time) (int64, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
customQueryModel struct {
|
customQueryModel struct {
|
||||||
@ -25,3 +31,30 @@ func NewQueryModel(conn sqlx.SqlConn, c cache.CacheConf) QueryModel {
|
|||||||
defaultQueryModel: newQueryModel(conn, c),
|
defaultQueryModel: newQueryModel(conn, c),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (m *customQueryModel) DeleteBefore(ctx context.Context, before time.Time) (int64, error) {
|
||||||
|
var affected int64 = 0
|
||||||
|
|
||||||
|
// 使用事务处理批量删除
|
||||||
|
err := m.defaultQueryModel.Trans(ctx, func(ctx context.Context, session sqlx.Session) error {
|
||||||
|
query := fmt.Sprintf("DELETE FROM %s WHERE create_time < ? AND del_state = ?", m.defaultQueryModel.table)
|
||||||
|
result, err := session.ExecCtx(ctx, query, before.Format("2006-01-02 15:04:05"), globalkey.DelStateNo)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
rows, err := result.RowsAffected()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
affected = rows
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return affected, nil
|
||||||
|
}
|
||||||
|
@ -12,14 +12,15 @@ func TestAesEcbMobileEncryption(t *testing.T) {
|
|||||||
mobile := "13800138000"
|
mobile := "13800138000"
|
||||||
key := []byte("1234567890abcdef") // 16字节AES-128密钥
|
key := []byte("1234567890abcdef") // 16字节AES-128密钥
|
||||||
|
|
||||||
|
keyStr := hex.EncodeToString(key)
|
||||||
// 测试加密
|
// 测试加密
|
||||||
encrypted, err := EncryptMobile(mobile, key)
|
encrypted, err := EncryptMobile(mobile, keyStr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("手机号加密失败: %v", err)
|
t.Fatalf("手机号加密失败: %v", err)
|
||||||
}
|
}
|
||||||
fmt.Println(encrypted)
|
fmt.Println(encrypted)
|
||||||
// 测试解密
|
// 测试解密
|
||||||
decrypted, err := DecryptMobile(encrypted, key)
|
decrypted, err := DecryptMobile(encrypted, keyStr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("手机号解密失败: %v", err)
|
t.Fatalf("手机号解密失败: %v", err)
|
||||||
}
|
}
|
||||||
@ -30,7 +31,7 @@ func TestAesEcbMobileEncryption(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 测试相同输入产生相同输出(确定性)
|
// 测试相同输入产生相同输出(确定性)
|
||||||
encrypted2, _ := EncryptMobile(mobile, key)
|
encrypted2, _ := EncryptMobile(mobile, keyStr)
|
||||||
if encrypted != encrypted2 {
|
if encrypted != encrypted2 {
|
||||||
t.Errorf("AES-ECB不是确定性的,两次加密结果不同: %s vs %s", encrypted, encrypted2)
|
t.Errorf("AES-ECB不是确定性的,两次加密结果不同: %s vs %s", encrypted, encrypted2)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user