This commit is contained in:
2026-02-27 17:18:20 +08:00
parent c960fd099a
commit 0bfcf25ea8
4 changed files with 210 additions and 6 deletions

View File

@@ -2,10 +2,14 @@ package agent
import (
"context"
"strings"
"bdqr-server/app/main/api/internal/svc"
"bdqr-server/app/main/api/internal/types"
"bdqr-server/common/xerr"
"bdqr-server/app/main/model"
"github.com/pkg/errors"
"github.com/zeromicro/go-zero/core/logx"
)
@@ -24,7 +28,61 @@ func NewCheckFeatureWhitelistStatusLogic(ctx context.Context, svcCtx *svc.Servic
}
func (l *CheckFeatureWhitelistStatusLogic) CheckFeatureWhitelistStatus(req *types.CheckFeatureWhitelistStatusReq) (resp *types.CheckFeatureWhitelistStatusResp, err error) {
// todo: add your logic here and delete this line
// 1. 验证参数
if req.IdCard == "" {
return nil, errors.Wrapf(xerr.NewErrMsg("身份证号不能为空"), "")
}
if req.FeatureApiId == "" {
return nil, errors.Wrapf(xerr.NewErrMsg("模块API标识不能为空"), "")
}
return
// 2. 提取主模块ID去掉下划线后的部分
mainApiId := req.FeatureApiId
if idx := strings.Index(req.FeatureApiId, "_"); idx > 0 {
mainApiId = req.FeatureApiId[:idx]
}
// 3. 查询feature信息使用主模块ID
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)
}
// 4. 检查是否在白名单中使用主模块ID
whitelistBuilder := l.svcCtx.UserFeatureWhitelistModel.SelectBuilder().
Where("id_card = ? AND feature_api_id = ? AND status = ?", req.IdCard, mainApiId, 1)
whitelists, err := l.svcCtx.UserFeatureWhitelistModel.FindAll(l.ctx, whitelistBuilder, "")
if err != nil {
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "查询白名单记录失败, %v", err)
}
isWhitelisted := len(whitelists) > 0
// 5. 如果提供了 queryId检查当前报告数据是否已删除
dataDeleted := false
if req.QueryId != "" {
containsFeature, err := l.svcCtx.WhitelistService.CheckQueryDataContainsFeature(l.ctx, req.QueryId, req.FeatureApiId)
if err != nil {
// 检查失败不影响主流程,记录日志即可
logx.Errorf("检查报告数据是否包含模块失败:查询记录 %s模块 %s错误%v", req.QueryId, req.FeatureApiId, err)
// 默认认为数据已删除(保守处理)
dataDeleted = true
} else {
// 如果数据中不包含该模块,说明已删除
dataDeleted = !containsFeature
}
} else {
// 未提供 queryId无法判断数据是否已删除默认认为已删除保守处理
dataDeleted = true
}
return &types.CheckFeatureWhitelistStatusResp{
IsWhitelisted: isWhitelisted,
WhitelistPrice: feature.WhitelistPrice,
FeatureId: feature.Id,
DataDeleted: dataDeleted,
}, nil
}

View File

@@ -2,11 +2,18 @@ package agent
import (
"context"
"fmt"
"bdqr-server/app/main/api/internal/svc"
"bdqr-server/app/main/api/internal/types"
"bdqr-server/app/main/model"
"bdqr-server/common/ctxdata"
"bdqr-server/common/xerr"
"github.com/google/uuid"
"github.com/pkg/errors"
"github.com/zeromicro/go-zero/core/logx"
"github.com/zeromicro/go-zero/core/stores/sqlx"
)
type CreateWhitelistOrderLogic struct {
@@ -24,7 +31,132 @@ func NewCreateWhitelistOrderLogic(ctx context.Context, svcCtx *svc.ServiceContex
}
func (l *CreateWhitelistOrderLogic) CreateWhitelistOrder(req *types.CreateWhitelistOrderReq) (resp *types.CreateWhitelistOrderResp, err error) {
// todo: add your logic here and delete this line
userID, err := ctxdata.GetUidFromCtx(l.ctx)
if err != nil {
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "获取用户信息失败, %v", err)
}
return
// 1. 获取代理信息并验证(任意等级代理均可操作白名单)
_, err = l.svcCtx.AgentModel.FindOneByUserId(l.ctx, userID)
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)
}
// 2. 验证参数
if req.IdCard == "" {
return nil, errors.Wrapf(xerr.NewErrMsg("身份证号不能为空"), "")
}
if len(req.FeatureIds) == 0 {
return nil, errors.Wrapf(xerr.NewErrMsg("请至少选择一个模块"), "")
}
// 3. 查询feature信息并计算总金额
var totalAmount float64
var orderItems []struct {
FeatureId string
FeatureApiId string
FeatureName string
Price float64
}
for _, featureId := range req.FeatureIds {
feature, err := l.svcCtx.FeatureModel.FindOne(l.ctx, featureId)
if err != nil {
if errors.Is(err, model.ErrNotFound) {
return nil, errors.Wrapf(xerr.NewErrMsg(fmt.Sprintf("模块不存在: %s", featureId)), "")
}
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "查询模块信息失败, %v", err)
}
// 直接使用feature的WhitelistPrice字段
whitelistPrice := feature.WhitelistPrice
if whitelistPrice <= 0 {
return nil, errors.Wrapf(xerr.NewErrMsg(fmt.Sprintf("模块 %s 不支持白名单屏蔽", feature.Name)), "")
}
// 检查该身份证号+feature是否已经存在白名单记录
whitelistBuilder := l.svcCtx.UserFeatureWhitelistModel.SelectBuilder().
Where("id_card = ? AND feature_id = ?", req.IdCard, featureId)
existing, err := l.svcCtx.UserFeatureWhitelistModel.FindAll(l.ctx, whitelistBuilder, "")
if err != nil {
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "查询白名单记录失败, %v", err)
}
// 检查是否有生效的记录status=1
for _, item := range existing {
if item.Status == 1 {
return nil, errors.Wrapf(xerr.NewErrMsg(fmt.Sprintf("身份证号 %s 的模块 %s 已经加入白名单", req.IdCard, feature.Name)), "")
}
}
totalAmount += whitelistPrice
orderItems = append(orderItems, struct {
FeatureId string
FeatureApiId string
FeatureName string
Price float64
}{
FeatureId: feature.Id,
FeatureApiId: feature.ApiId,
FeatureName: feature.Name,
Price: whitelistPrice,
})
}
// 4. 生成订单号(白名单订单前缀 W_限制长度不超过32
base := l.svcCtx.AlipayService.GenerateOutTradeNo()
orderNo := "W_" + base
if len(orderNo) > 32 {
orderNo = orderNo[:32]
}
// 5. 使用事务创建订单和订单明细
var orderId string
err = l.svcCtx.WhitelistOrderModel.Trans(l.ctx, func(ctx context.Context, session sqlx.Session) error {
// 创建订单
order := &model.WhitelistOrder{
Id: uuid.NewString(),
OrderNo: orderNo,
UserId: userID,
IdCard: req.IdCard,
TotalAmount: totalAmount,
Status: 1, // 待支付
}
_, err := l.svcCtx.WhitelistOrderModel.Insert(ctx, session, order)
if err != nil {
return errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "创建订单失败, %v", err)
}
orderId = order.Id
// 创建订单明细
for _, item := range orderItems {
orderItem := &model.WhitelistOrderItem{
Id: uuid.NewString(),
OrderId: orderId,
FeatureId: item.FeatureId,
FeatureApiId: item.FeatureApiId,
FeatureName: item.FeatureName,
Price: item.Price,
}
_, err := l.svcCtx.WhitelistOrderItemModel.Insert(ctx, session, orderItem)
if err != nil {
return errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "创建订单明细失败, %v", err)
}
}
return nil
})
if err != nil {
return nil, err
}
return &types.CreateWhitelistOrderResp{
OrderId: orderId,
OrderNo: orderNo,
TotalAmount: totalAmount,
}, nil
}