137 lines
4.3 KiB
Go
137 lines
4.3 KiB
Go
package admin_query
|
|
|
|
import (
|
|
"context"
|
|
"encoding/hex"
|
|
"encoding/json"
|
|
"fmt"
|
|
"strings"
|
|
|
|
"ycc-server/app/main/api/internal/svc"
|
|
"ycc-server/app/main/api/internal/types"
|
|
"ycc-server/app/main/model"
|
|
"ycc-server/common/ctxdata"
|
|
"ycc-server/common/xerr"
|
|
"ycc-server/pkg/lzkit/crypto"
|
|
|
|
"github.com/pkg/errors"
|
|
"github.com/zeromicro/go-zero/core/logx"
|
|
"github.com/zeromicro/go-zero/core/stores/sqlx"
|
|
)
|
|
|
|
type AdminDeleteQueryFeatureDataLogic struct {
|
|
logx.Logger
|
|
ctx context.Context
|
|
svcCtx *svc.ServiceContext
|
|
}
|
|
|
|
func NewAdminDeleteQueryFeatureDataLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AdminDeleteQueryFeatureDataLogic {
|
|
return &AdminDeleteQueryFeatureDataLogic{
|
|
Logger: logx.WithContext(ctx),
|
|
ctx: ctx,
|
|
svcCtx: svcCtx,
|
|
}
|
|
}
|
|
|
|
func (l *AdminDeleteQueryFeatureDataLogic) AdminDeleteQueryFeatureData(req *types.AdminDeleteQueryFeatureDataReq) (resp *types.AdminDeleteQueryFeatureDataResp, err error) {
|
|
if req.QueryId == "" {
|
|
return nil, errors.Wrapf(xerr.NewErrMsg("QueryId 不能为空"), "")
|
|
}
|
|
if req.FeatureApiId == "" {
|
|
return nil, errors.Wrapf(xerr.NewErrMsg("feature_api_id 不能为空"), "")
|
|
}
|
|
|
|
queryModel, err := l.svcCtx.QueryModel.FindOne(l.ctx, req.QueryId)
|
|
if err != nil {
|
|
if errors.Is(err, model.ErrNotFound) {
|
|
return nil, errors.Wrapf(xerr.NewErrMsg("查询记录不存在"), "")
|
|
}
|
|
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "查询报告记录失败, %v", err)
|
|
}
|
|
|
|
queryParams, err := decryptQueryParams(l.svcCtx, queryModel)
|
|
if err != nil {
|
|
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "获取查询参数失败, %v", err)
|
|
}
|
|
|
|
idCard, _ := queryParams["id_card"].(string)
|
|
if idCard == "" {
|
|
return nil, errors.Wrapf(xerr.NewErrMsg("查询参数中缺少身份证号,无法添加白名单"), "")
|
|
}
|
|
|
|
name, _ := queryParams["name"].(string)
|
|
if strings.TrimSpace(name) == "" {
|
|
name = "*"
|
|
}
|
|
|
|
adminUserID, err := ctxdata.GetUidFromCtx(l.ctx)
|
|
if err != nil {
|
|
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "获取管理员信息失败, %v", err)
|
|
}
|
|
|
|
mainApiId := extractMainApiId(req.FeatureApiId)
|
|
feature, err := l.svcCtx.FeatureModel.FindOneByApiId(l.ctx, mainApiId)
|
|
if err != nil {
|
|
if errors.Is(err, model.ErrNotFound) {
|
|
return nil, errors.Wrapf(xerr.NewErrMsg("模块不存在"), "")
|
|
}
|
|
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "查询模块信息失败, %v", err)
|
|
}
|
|
|
|
remark := fmt.Sprintf("后台删除报告模块自动同步 query_id=%s feature=%s", req.QueryId, feature.ApiId)
|
|
l.svcCtx.QueryWhitelistSyncService.TrySync(
|
|
l.ctx,
|
|
name,
|
|
idCard,
|
|
[]string{feature.ApiId},
|
|
remark,
|
|
)
|
|
|
|
err = l.svcCtx.QueryModel.Trans(l.ctx, func(ctx context.Context, session sqlx.Session) error {
|
|
if wlErr := l.svcCtx.WhitelistService.EnsureFreeWhitelist(ctx, session, idCard, feature, adminUserID, req.QueryId); wlErr != nil {
|
|
return wlErr
|
|
}
|
|
return l.svcCtx.WhitelistService.DeleteFeatureFromQueryData(ctx, session, req.QueryId, req.FeatureApiId)
|
|
})
|
|
if err != nil {
|
|
l.Errorf("Admin 删除查询模块数据失败, queryId=%s, featureApiId=%s, err=%+v", req.QueryId, req.FeatureApiId, err)
|
|
return nil, errors.Wrapf(xerr.NewErrMsg("删除失败,请稍后重试"), "")
|
|
}
|
|
|
|
return &types.AdminDeleteQueryFeatureDataResp{
|
|
Success: true,
|
|
Message: "删除成功,已同步本地白名单并尽力同步天远查询白名单(若原本不存在该模块数据,则视为已删除)",
|
|
}, nil
|
|
}
|
|
|
|
func extractMainApiId(featureApiId string) string {
|
|
if idx := strings.Index(featureApiId, "_"); idx > 0 {
|
|
return featureApiId[:idx]
|
|
}
|
|
return featureApiId
|
|
}
|
|
|
|
func decryptQueryParams(svcCtx *svc.ServiceContext, queryModel *model.Query) (map[string]interface{}, error) {
|
|
if queryModel.QueryParams == "" {
|
|
return nil, errors.New("查询参数为空")
|
|
}
|
|
|
|
secretKey := svcCtx.Config.Encrypt.SecretKey
|
|
key, decodeErr := hex.DecodeString(secretKey)
|
|
if decodeErr != nil {
|
|
return nil, errors.Wrap(decodeErr, "获取AES密钥失败")
|
|
}
|
|
|
|
decryptedParams, decryptErr := crypto.AesDecrypt(queryModel.QueryParams, key)
|
|
if decryptErr != nil {
|
|
return nil, errors.Wrap(decryptErr, "解密查询参数失败")
|
|
}
|
|
|
|
var params map[string]interface{}
|
|
if unmarshalErr := json.Unmarshal(decryptedParams, ¶ms); unmarshalErr != nil {
|
|
return nil, errors.Wrap(unmarshalErr, "解析查询参数失败")
|
|
}
|
|
|
|
return params, nil
|
|
}
|