f
This commit is contained in:
@@ -34,6 +34,14 @@ func (l *AdminCreateFeatureLogic) AdminCreateFeature(req *types.AdminCreateFeatu
|
|||||||
ApiId: req.ApiId,
|
ApiId: req.ApiId,
|
||||||
Name: req.Name,
|
Name: req.Name,
|
||||||
}
|
}
|
||||||
|
// 设置白名单屏蔽价格
|
||||||
|
if req.WhitelistPrice != nil {
|
||||||
|
data.WhitelistPrice = *req.WhitelistPrice
|
||||||
|
}
|
||||||
|
// 设置成本价
|
||||||
|
if req.CostPrice != nil {
|
||||||
|
data.CostPrice = *req.CostPrice
|
||||||
|
}
|
||||||
|
|
||||||
// 2. 数据库操作
|
// 2. 数据库操作
|
||||||
result, err := l.svcCtx.FeatureModel.Insert(l.ctx, nil, data)
|
result, err := l.svcCtx.FeatureModel.Insert(l.ctx, nil, data)
|
||||||
|
|||||||
@@ -36,7 +36,7 @@ func (l *AdminUpdateFeatureLogic) AdminUpdateFeature(req *types.AdminUpdateFeatu
|
|||||||
record, err := l.svcCtx.FeatureModel.FindOne(l.ctx, req.Id)
|
record, err := l.svcCtx.FeatureModel.FindOne(l.ctx, req.Id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR),
|
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR),
|
||||||
"查找功能失败, err: %v, id: %d", err, req.Id)
|
"查找功能失败, err: %v, id: %s", err, req.Id)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 3. 直接更新record的字段(只更新非空字段)
|
// 3. 直接更新record的字段(只更新非空字段)
|
||||||
@@ -46,12 +46,18 @@ func (l *AdminUpdateFeatureLogic) AdminUpdateFeature(req *types.AdminUpdateFeatu
|
|||||||
if req.Name != nil && *req.Name != "" {
|
if req.Name != nil && *req.Name != "" {
|
||||||
record.Name = *req.Name
|
record.Name = *req.Name
|
||||||
}
|
}
|
||||||
|
if req.WhitelistPrice != nil {
|
||||||
|
record.WhitelistPrice = *req.WhitelistPrice
|
||||||
|
}
|
||||||
|
if req.CostPrice != nil {
|
||||||
|
record.CostPrice = *req.CostPrice
|
||||||
|
}
|
||||||
|
|
||||||
// 4. 执行更新操作
|
// 4. 执行更新操作
|
||||||
err = l.svcCtx.FeatureModel.UpdateWithVersion(l.ctx, nil, record)
|
err = l.svcCtx.FeatureModel.UpdateWithVersion(l.ctx, nil, record)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR),
|
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR),
|
||||||
"更新功能失败, err: %v, id: %d", err, req.Id)
|
"更新功能失败, err: %v, id: %s", err, req.Id)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 5. 返回成功结果
|
// 5. 返回成功结果
|
||||||
|
|||||||
@@ -2,10 +2,14 @@ package agent
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"bdqr-server/app/main/api/internal/svc"
|
"bdqr-server/app/main/api/internal/svc"
|
||||||
"bdqr-server/app/main/api/internal/types"
|
"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"
|
"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) {
|
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
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,11 +2,18 @@ package agent
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
"bdqr-server/app/main/api/internal/svc"
|
"bdqr-server/app/main/api/internal/svc"
|
||||||
"bdqr-server/app/main/api/internal/types"
|
"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/logx"
|
||||||
|
"github.com/zeromicro/go-zero/core/stores/sqlx"
|
||||||
)
|
)
|
||||||
|
|
||||||
type CreateWhitelistOrderLogic struct {
|
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) {
|
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
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user