add updatequery

This commit is contained in:
liangzai 2025-04-16 19:43:46 +08:00
parent 68804850c7
commit 04fe5f8ac0
6 changed files with 130 additions and 0 deletions

View File

@ -65,5 +65,10 @@ service main {
service main {
@handler querySingleTest
post /query/single/test (QuerySingleTestReq) returns (QuerySingleTestResp)
@doc "更新查询数据"
@handler updateQueryData
post /query/update_data (UpdateQueryDataReq) returns (UpdateQueryDataResp)
}

View File

@ -113,3 +113,13 @@ type QuerySingleTestResp {
Api string `json:"api"`
}
type (
UpdateQueryDataReq {
Id int64 `json:"id"` // 查询ID
QueryData string `json:"query_data"` // 查询数据(未加密的JSON)
}
UpdateQueryDataResp {
Id int64 `json:"id"`
UpdatedAt string `json:"updated_at"` // 更新时间
}
)

View File

@ -0,0 +1,29 @@
package query
import (
"net/http"
"github.com/zeromicro/go-zero/rest/httpx"
"tyc-server/app/user/cmd/api/internal/logic/query"
"tyc-server/app/user/cmd/api/internal/svc"
"tyc-server/app/user/cmd/api/internal/types"
"tyc-server/common/result"
"tyc-server/pkg/lzkit/validator"
)
func UpdateQueryDataHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req types.UpdateQueryDataReq
if err := httpx.Parse(r, &req); err != nil {
result.ParamErrorResult(r, w, err)
return
}
if err := validator.Validate(req); err != nil {
result.ParamValidateErrorResult(r, w, err)
return
}
l := query.NewUpdateQueryDataLogic(r.Context(), svcCtx)
resp, err := l.UpdateQueryData(&req)
result.HttpResult(r, w, resp, err)
}
}

View File

@ -181,6 +181,12 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
Path: "/query/single/test",
Handler: query.QuerySingleTestHandler(serverCtx),
},
{
// 更新查询数据
Method: http.MethodPost,
Path: "/query/update_data",
Handler: query.UpdateQueryDataHandler(serverCtx),
},
},
rest.WithPrefix("/api/v1"),
)

View File

@ -0,0 +1,70 @@
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
}

View File

@ -198,6 +198,16 @@ type RegisterResp struct {
RefreshAfter int64 `json:"refreshAfter"`
}
type UpdateQueryDataReq struct {
Id int64 `json:"id"` // 查询ID
QueryData string `json:"query_data"` // 查询数据(未加密的JSON)
}
type UpdateQueryDataResp struct {
Id int64 `json:"id"`
UpdatedAt string `json:"updated_at"` // 更新时间
}
type User struct {
Id int64 `json:"id"`
Mobile string `json:"mobile"`