From 04fe5f8ac0b147c9bdc3e1f926ca5750eaba7a25 Mon Sep 17 00:00:00 2001 From: liangzai <2440983361@qq.com> Date: Wed, 16 Apr 2025 19:43:46 +0800 Subject: [PATCH] add updatequery --- app/user/cmd/api/desc/query.api | 5 ++ app/user/cmd/api/desc/query/query.api | 10 +++ .../handler/query/updatequerydatahandler.go | 29 ++++++++ app/user/cmd/api/internal/handler/routes.go | 6 ++ .../logic/query/updatequerydatalogic.go | 70 +++++++++++++++++++ app/user/cmd/api/internal/types/types.go | 10 +++ 6 files changed, 130 insertions(+) create mode 100644 app/user/cmd/api/internal/handler/query/updatequerydatahandler.go create mode 100644 app/user/cmd/api/internal/logic/query/updatequerydatalogic.go diff --git a/app/user/cmd/api/desc/query.api b/app/user/cmd/api/desc/query.api index ec8d195..20f6b87 100644 --- a/app/user/cmd/api/desc/query.api +++ b/app/user/cmd/api/desc/query.api @@ -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) } diff --git a/app/user/cmd/api/desc/query/query.api b/app/user/cmd/api/desc/query/query.api index f7edfd7..e3f38f4 100644 --- a/app/user/cmd/api/desc/query/query.api +++ b/app/user/cmd/api/desc/query/query.api @@ -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"` // 更新时间 + } +) \ No newline at end of file diff --git a/app/user/cmd/api/internal/handler/query/updatequerydatahandler.go b/app/user/cmd/api/internal/handler/query/updatequerydatahandler.go new file mode 100644 index 0000000..1758a10 --- /dev/null +++ b/app/user/cmd/api/internal/handler/query/updatequerydatahandler.go @@ -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) + } +} diff --git a/app/user/cmd/api/internal/handler/routes.go b/app/user/cmd/api/internal/handler/routes.go index e02f718..d1429a3 100644 --- a/app/user/cmd/api/internal/handler/routes.go +++ b/app/user/cmd/api/internal/handler/routes.go @@ -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"), ) diff --git a/app/user/cmd/api/internal/logic/query/updatequerydatalogic.go b/app/user/cmd/api/internal/logic/query/updatequerydatalogic.go new file mode 100644 index 0000000..3ba1589 --- /dev/null +++ b/app/user/cmd/api/internal/logic/query/updatequerydatalogic.go @@ -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 +} diff --git a/app/user/cmd/api/internal/types/types.go b/app/user/cmd/api/internal/types/types.go index 6dbf6e7..f216e8d 100644 --- a/app/user/cmd/api/internal/types/types.go +++ b/app/user/cmd/api/internal/types/types.go @@ -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"`