71 lines
2.1 KiB
Go
71 lines
2.1 KiB
Go
package query
|
||
|
||
import (
|
||
"context"
|
||
"database/sql"
|
||
"encoding/hex"
|
||
|
||
"tyc-server/app/user/cmd/api/internal/svc"
|
||
"tyc-server/app/user/cmd/api/internal/types"
|
||
"tyc-server/app/user/model"
|
||
"tyc-server/common/xerr"
|
||
"tyc-server/pkg/lzkit/crypto"
|
||
|
||
"github.com/pkg/errors"
|
||
"github.com/zeromicro/go-zero/core/logx"
|
||
)
|
||
|
||
type UpdateQueryDataLogic struct {
|
||
logx.Logger
|
||
ctx context.Context
|
||
svcCtx *svc.ServiceContext
|
||
}
|
||
|
||
func NewUpdateQueryDataLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateQueryDataLogic {
|
||
return &UpdateQueryDataLogic{
|
||
Logger: logx.WithContext(ctx),
|
||
ctx: ctx,
|
||
svcCtx: svcCtx,
|
||
}
|
||
}
|
||
|
||
func (l *UpdateQueryDataLogic) UpdateQueryData(req *types.UpdateQueryDataReq) (resp *types.UpdateQueryDataResp, err error) {
|
||
// 1. 从数据库中获取查询记录
|
||
query, err := l.svcCtx.QueryModel.FindOne(l.ctx, req.Id)
|
||
if err != nil {
|
||
if err == model.ErrNotFound {
|
||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "查询记录不存在, 查询ID: %d", req.Id)
|
||
}
|
||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "查询数据库失败, 查询ID: %d, err: %v", req.Id, err)
|
||
}
|
||
|
||
// 2. 获取加密密钥
|
||
secretKey := l.svcCtx.Config.Encrypt.SecretKey
|
||
key, decodeErr := hex.DecodeString(secretKey)
|
||
if decodeErr != nil {
|
||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "获取AES密钥失败: %v", decodeErr)
|
||
}
|
||
|
||
// 3. 加密数据 - 传入的是JSON,需要加密处理
|
||
encryptData, aesEncryptErr := crypto.AesEncrypt([]byte(req.QueryData), key)
|
||
if aesEncryptErr != nil {
|
||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "加密查询数据失败: %v", aesEncryptErr)
|
||
}
|
||
|
||
// 4. 更新数据库记录
|
||
query.QueryData = sql.NullString{
|
||
String: encryptData,
|
||
Valid: true,
|
||
}
|
||
updateErr := l.svcCtx.QueryModel.UpdateWithVersion(l.ctx, nil, query)
|
||
if updateErr != nil {
|
||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "更新查询数据失败: %v", updateErr)
|
||
}
|
||
|
||
// 5. 返回结果
|
||
return &types.UpdateQueryDataResp{
|
||
Id: query.Id,
|
||
UpdatedAt: query.UpdateTime.Format("2006-01-02 15:04:05"),
|
||
}, nil
|
||
}
|