fix
This commit is contained in:
@@ -0,0 +1,92 @@
|
||||
package admin_feature
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/hex"
|
||||
|
||||
"tydata-server/app/main/api/internal/svc"
|
||||
"tydata-server/app/main/api/internal/types"
|
||||
"tydata-server/app/main/model"
|
||||
"tydata-server/common/xerr"
|
||||
"tydata-server/pkg/lzkit/crypto"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type AdminConfigFeatureExampleLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
func NewAdminConfigFeatureExampleLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AdminConfigFeatureExampleLogic {
|
||||
return &AdminConfigFeatureExampleLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *AdminConfigFeatureExampleLogic) AdminConfigFeatureExample(req *types.AdminConfigFeatureExampleReq) (resp *types.AdminConfigFeatureExampleResp, err error) {
|
||||
// 1. 验证功能是否存在
|
||||
feature, err := l.svcCtx.FeatureModel.FindOne(l.ctx, req.FeatureId)
|
||||
if err != nil && !errors.Is(err, model.ErrNotFound) {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR),
|
||||
"查询功能失败, featureId: %d, err: %v", req.FeatureId, err)
|
||||
}
|
||||
if feature == nil {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.REUQEST_PARAM_ERROR),
|
||||
"功能不存在, featureId: %d", req.FeatureId)
|
||||
}
|
||||
|
||||
// 2. 检查是否已存在示例数据
|
||||
existingExample, err := l.svcCtx.ExampleModel.FindOneByFeatureId(l.ctx, req.FeatureId)
|
||||
if err != nil && !errors.Is(err, model.ErrNotFound) {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR),
|
||||
"查询示例数据失败, featureId: %d, err: %v", req.FeatureId, err)
|
||||
}
|
||||
|
||||
// 3. 加密示例数据
|
||||
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)
|
||||
}
|
||||
|
||||
encryptedData, aesEncryptErr := crypto.AesEncrypt([]byte(req.Data), key)
|
||||
if aesEncryptErr != nil {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR),
|
||||
"加密示例数据失败: %v", aesEncryptErr)
|
||||
}
|
||||
|
||||
// 4. 准备示例数据
|
||||
exampleData := &model.Example{
|
||||
ApiId: feature.ApiId,
|
||||
FeatureId: req.FeatureId,
|
||||
Content: encryptedData,
|
||||
}
|
||||
|
||||
// 4. 根据是否存在决定新增或更新
|
||||
if existingExample == nil {
|
||||
// 新增示例数据
|
||||
_, err = l.svcCtx.ExampleModel.Insert(l.ctx, nil, exampleData)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR),
|
||||
"创建示例数据失败, featureId: %d, err: %v", req.FeatureId, err)
|
||||
}
|
||||
} else {
|
||||
// 更新示例数据
|
||||
exampleData.Id = existingExample.Id
|
||||
exampleData.Version = existingExample.Version
|
||||
_, err = l.svcCtx.ExampleModel.Update(l.ctx, nil, exampleData)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR),
|
||||
"更新示例数据失败, featureId: %d, err: %v", req.FeatureId, err)
|
||||
}
|
||||
}
|
||||
|
||||
// 5. 返回成功结果
|
||||
return &types.AdminConfigFeatureExampleResp{Success: true}, nil
|
||||
}
|
||||
@@ -3,10 +3,10 @@ package admin_feature
|
||||
import (
|
||||
"context"
|
||||
|
||||
"hm-server/app/main/api/internal/svc"
|
||||
"hm-server/app/main/api/internal/types"
|
||||
"hm-server/app/main/model"
|
||||
"hm-server/common/xerr"
|
||||
"tydata-server/app/main/api/internal/svc"
|
||||
"tydata-server/app/main/api/internal/types"
|
||||
"tydata-server/app/main/model"
|
||||
"tydata-server/common/xerr"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
|
||||
@@ -3,9 +3,9 @@ package admin_feature
|
||||
import (
|
||||
"context"
|
||||
|
||||
"hm-server/app/main/api/internal/svc"
|
||||
"hm-server/app/main/api/internal/types"
|
||||
"hm-server/common/xerr"
|
||||
"tydata-server/app/main/api/internal/svc"
|
||||
"tydata-server/app/main/api/internal/types"
|
||||
"tydata-server/common/xerr"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
|
||||
@@ -3,9 +3,9 @@ package admin_feature
|
||||
import (
|
||||
"context"
|
||||
|
||||
"hm-server/app/main/api/internal/svc"
|
||||
"hm-server/app/main/api/internal/types"
|
||||
"hm-server/common/xerr"
|
||||
"tydata-server/app/main/api/internal/svc"
|
||||
"tydata-server/app/main/api/internal/types"
|
||||
"tydata-server/common/xerr"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
|
||||
@@ -0,0 +1,82 @@
|
||||
package admin_feature
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/hex"
|
||||
|
||||
"tydata-server/app/main/api/internal/svc"
|
||||
"tydata-server/app/main/api/internal/types"
|
||||
"tydata-server/app/main/model"
|
||||
"tydata-server/common/xerr"
|
||||
"tydata-server/pkg/lzkit/crypto"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type AdminGetFeatureExampleLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
func NewAdminGetFeatureExampleLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AdminGetFeatureExampleLogic {
|
||||
return &AdminGetFeatureExampleLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *AdminGetFeatureExampleLogic) AdminGetFeatureExample(req *types.AdminGetFeatureExampleReq) (resp *types.AdminGetFeatureExampleResp, err error) {
|
||||
// 1. 查询示例数据
|
||||
example, err := l.svcCtx.ExampleModel.FindOneByFeatureId(l.ctx, req.FeatureId)
|
||||
if err != nil {
|
||||
if errors.Is(err, model.ErrNotFound) {
|
||||
// 示例数据不存在,返回空数据
|
||||
return &types.AdminGetFeatureExampleResp{
|
||||
Id: 0,
|
||||
FeatureId: req.FeatureId,
|
||||
ApiId: "",
|
||||
Data: "",
|
||||
CreateTime: "",
|
||||
UpdateTime: "",
|
||||
}, nil
|
||||
}
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR),
|
||||
"查询示例数据失败, featureId: %d, err: %v", req.FeatureId, 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. 解密示例数据
|
||||
var decryptedData string
|
||||
if example.Content == "000" {
|
||||
// 特殊值,直接返回
|
||||
decryptedData = example.Content
|
||||
} else {
|
||||
// 解密数据
|
||||
decryptedBytes, decryptErr := crypto.AesDecrypt(example.Content, key)
|
||||
if decryptErr != nil {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR),
|
||||
"解密示例数据失败: %v", decryptErr)
|
||||
}
|
||||
decryptedData = string(decryptedBytes)
|
||||
}
|
||||
|
||||
// 4. 返回解密后的数据
|
||||
return &types.AdminGetFeatureExampleResp{
|
||||
Id: example.Id,
|
||||
FeatureId: example.FeatureId,
|
||||
ApiId: example.ApiId,
|
||||
Data: decryptedData,
|
||||
CreateTime: example.CreateTime.Format("2006-01-02 15:04:05"),
|
||||
UpdateTime: example.UpdateTime.Format("2006-01-02 15:04:05"),
|
||||
}, nil
|
||||
}
|
||||
@@ -3,9 +3,9 @@ package admin_feature
|
||||
import (
|
||||
"context"
|
||||
|
||||
"hm-server/app/main/api/internal/svc"
|
||||
"hm-server/app/main/api/internal/types"
|
||||
"hm-server/common/xerr"
|
||||
"tydata-server/app/main/api/internal/svc"
|
||||
"tydata-server/app/main/api/internal/types"
|
||||
"tydata-server/common/xerr"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
|
||||
@@ -3,9 +3,11 @@ package admin_feature
|
||||
import (
|
||||
"context"
|
||||
|
||||
"hm-server/app/main/api/internal/svc"
|
||||
"hm-server/app/main/api/internal/types"
|
||||
"tydata-server/app/main/api/internal/svc"
|
||||
"tydata-server/app/main/api/internal/types"
|
||||
"tydata-server/common/xerr"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
@@ -24,7 +26,34 @@ func NewAdminUpdateFeatureLogic(ctx context.Context, svcCtx *svc.ServiceContext)
|
||||
}
|
||||
|
||||
func (l *AdminUpdateFeatureLogic) AdminUpdateFeature(req *types.AdminUpdateFeatureReq) (resp *types.AdminUpdateFeatureResp, err error) {
|
||||
// todo: add your logic here and delete this line
|
||||
// 1. 参数验证
|
||||
if req.Id <= 0 {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.PARAM_VERIFICATION_ERROR),
|
||||
"功能ID必须大于0, id: %d", req.Id)
|
||||
}
|
||||
|
||||
return
|
||||
// 2. 查询记录是否存在
|
||||
record, err := l.svcCtx.FeatureModel.FindOne(l.ctx, req.Id)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR),
|
||||
"查找功能失败, err: %v, id: %d", err, req.Id)
|
||||
}
|
||||
|
||||
// 3. 直接更新record的字段(只更新非空字段)
|
||||
if req.ApiId != nil && *req.ApiId != "" {
|
||||
record.ApiId = *req.ApiId
|
||||
}
|
||||
if req.Name != nil && *req.Name != "" {
|
||||
record.Name = *req.Name
|
||||
}
|
||||
|
||||
// 4. 执行更新操作
|
||||
err = l.svcCtx.FeatureModel.UpdateWithVersion(l.ctx, nil, record)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR),
|
||||
"更新功能失败, err: %v, id: %d", err, req.Id)
|
||||
}
|
||||
|
||||
// 5. 返回成功结果
|
||||
return &types.AdminUpdateFeatureResp{Success: true}, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user