f
This commit is contained in:
@@ -0,0 +1,72 @@
|
||||
package agent
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"qnc-server/app/main/api/internal/svc"
|
||||
"qnc-server/app/main/api/internal/types"
|
||||
"qnc-server/app/main/model"
|
||||
"qnc-server/common/xerr"
|
||||
)
|
||||
|
||||
type CheckFeatureWhitelistStatusLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
func NewCheckFeatureWhitelistStatusLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CheckFeatureWhitelistStatusLogic {
|
||||
return &CheckFeatureWhitelistStatusLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *CheckFeatureWhitelistStatusLogic) CheckFeatureWhitelistStatus(req *types.CheckFeatureWhitelistStatusReq) (resp *types.CheckFeatureWhitelistStatusResp, err error) {
|
||||
if req.IdCard == "" {
|
||||
return nil, errors.Wrapf(xerr.NewErrMsg("身份证号不能为空"), "")
|
||||
}
|
||||
if req.FeatureApiId == "" {
|
||||
return nil, errors.Wrapf(xerr.NewErrMsg("模块API标识不能为空"), "")
|
||||
}
|
||||
mainApiId := req.FeatureApiId
|
||||
if idx := strings.Index(req.FeatureApiId, "_"); idx > 0 {
|
||||
mainApiId = req.FeatureApiId[:idx]
|
||||
}
|
||||
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)
|
||||
}
|
||||
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
|
||||
dataDeleted := false
|
||||
if req.QueryId != "" {
|
||||
containsFeature, err := l.svcCtx.WhitelistService.CheckQueryDataContainsFeature(l.ctx, req.QueryId, req.FeatureApiId)
|
||||
if err != nil {
|
||||
logx.Errorf("检查报告数据是否包含模块失败:%v", err)
|
||||
dataDeleted = true
|
||||
} else {
|
||||
dataDeleted = !containsFeature
|
||||
}
|
||||
} else {
|
||||
dataDeleted = true
|
||||
}
|
||||
return &types.CheckFeatureWhitelistStatusResp{
|
||||
IsWhitelisted: isWhitelisted,
|
||||
WhitelistPrice: feature.WhitelistPrice,
|
||||
FeatureId: feature.Id,
|
||||
DataDeleted: dataDeleted,
|
||||
}, nil
|
||||
}
|
||||
53
app/main/api/internal/logic/agent/checkorderagentlogic.go
Normal file
53
app/main/api/internal/logic/agent/checkorderagentlogic.go
Normal file
@@ -0,0 +1,53 @@
|
||||
package agent
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"qnc-server/app/main/api/internal/svc"
|
||||
"qnc-server/app/main/api/internal/types"
|
||||
"qnc-server/app/main/model"
|
||||
"qnc-server/common/ctxdata"
|
||||
"qnc-server/common/xerr"
|
||||
)
|
||||
|
||||
type CheckOrderAgentLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
func NewCheckOrderAgentLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CheckOrderAgentLogic {
|
||||
return &CheckOrderAgentLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
// CheckOrderAgent 判断订单是否为当前代理推广的订单(通过 agent_order 关联)
|
||||
func (l *CheckOrderAgentLogic) CheckOrderAgent(req *types.CheckOrderAgentReq) (resp *types.CheckOrderAgentResp, err error) {
|
||||
if req.OrderId == "" {
|
||||
return &types.CheckOrderAgentResp{IsAgentOrder: false}, nil
|
||||
}
|
||||
userID, err := ctxdata.GetUidFromCtx(l.ctx)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "获取用户信息失败, %v", err)
|
||||
}
|
||||
agent, err := l.svcCtx.AgentModel.FindOneByUserId(l.ctx, userID)
|
||||
if err != nil {
|
||||
if errors.Is(err, model.ErrNotFound) {
|
||||
return &types.CheckOrderAgentResp{IsAgentOrder: false}, nil
|
||||
}
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "查询代理信息失败, %v", err)
|
||||
}
|
||||
agentOrder, err := l.svcCtx.AgentOrderModel.FindOneByOrderId(l.ctx, req.OrderId)
|
||||
if err != nil {
|
||||
if errors.Is(err, model.ErrNotFound) {
|
||||
return &types.CheckOrderAgentResp{IsAgentOrder: false}, nil
|
||||
}
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "查询订单关联失败, %v", err)
|
||||
}
|
||||
return &types.CheckOrderAgentResp{IsAgentOrder: agentOrder.AgentId == agent.Id}, nil
|
||||
}
|
||||
127
app/main/api/internal/logic/agent/createwhitelistorderlogic.go
Normal file
127
app/main/api/internal/logic/agent/createwhitelistorderlogic.go
Normal file
@@ -0,0 +1,127 @@
|
||||
package agent
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"github.com/zeromicro/go-zero/core/stores/sqlx"
|
||||
"qnc-server/app/main/api/internal/svc"
|
||||
"qnc-server/app/main/api/internal/types"
|
||||
"qnc-server/app/main/model"
|
||||
"qnc-server/common/ctxdata"
|
||||
"qnc-server/common/xerr"
|
||||
)
|
||||
|
||||
type CreateWhitelistOrderLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
func NewCreateWhitelistOrderLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CreateWhitelistOrderLogic {
|
||||
return &CreateWhitelistOrderLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *CreateWhitelistOrderLogic) CreateWhitelistOrder(req *types.CreateWhitelistOrderReq) (resp *types.CreateWhitelistOrderResp, err error) {
|
||||
userID, err := ctxdata.GetUidFromCtx(l.ctx)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "获取用户信息失败, %v", err)
|
||||
}
|
||||
_, 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)
|
||||
}
|
||||
if req.IdCard == "" {
|
||||
return nil, errors.Wrapf(xerr.NewErrMsg("身份证号不能为空"), "")
|
||||
}
|
||||
if len(req.FeatureIds) == 0 {
|
||||
return nil, errors.Wrapf(xerr.NewErrMsg("请至少选择一个模块"), "")
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
whitelistPrice := feature.WhitelistPrice
|
||||
if whitelistPrice <= 0 {
|
||||
return nil, errors.Wrapf(xerr.NewErrMsg(fmt.Sprintf("模块 %s 不支持白名单屏蔽", feature.Name)), "")
|
||||
}
|
||||
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)
|
||||
}
|
||||
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,
|
||||
})
|
||||
}
|
||||
|
||||
base := l.svcCtx.AlipayService.GenerateOutTradeNo()
|
||||
orderNo := "W_" + base
|
||||
if len(orderNo) > 32 {
|
||||
orderNo = orderNo[:32]
|
||||
}
|
||||
|
||||
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,
|
||||
}
|
||||
if _, err := l.svcCtx.WhitelistOrderItemModel.Insert(ctx, session, orderItem); 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
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
package agent
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"qnc-server/app/main/api/internal/svc"
|
||||
"qnc-server/app/main/api/internal/types"
|
||||
"qnc-server/common/xerr"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
type GetWhitelistFeaturesLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
func NewGetWhitelistFeaturesLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetWhitelistFeaturesLogic {
|
||||
return &GetWhitelistFeaturesLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
// GetWhitelistFeatures 返回支持白名单屏蔽的 feature 列表(whitelist_price > 0)
|
||||
func (l *GetWhitelistFeaturesLogic) GetWhitelistFeatures(req *types.GetWhitelistFeaturesReq) (resp *types.GetWhitelistFeaturesResp, err error) {
|
||||
builder := l.svcCtx.FeatureModel.SelectBuilder().Where("whitelist_price > ?", 0)
|
||||
list, err := l.svcCtx.FeatureModel.FindAll(l.ctx, builder, "id ASC")
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "查询功能列表失败, %v", err)
|
||||
}
|
||||
items := make([]types.WhitelistFeatureItem, 0, len(list))
|
||||
for _, f := range list {
|
||||
items = append(items, types.WhitelistFeatureItem{
|
||||
FeatureId: f.Id,
|
||||
FeatureApiId: f.ApiId,
|
||||
FeatureName: f.Name,
|
||||
WhitelistPrice: f.WhitelistPrice,
|
||||
})
|
||||
}
|
||||
return &types.GetWhitelistFeaturesResp{List: items}, nil
|
||||
}
|
||||
77
app/main/api/internal/logic/agent/getwhitelistlistlogic.go
Normal file
77
app/main/api/internal/logic/agent/getwhitelistlistlogic.go
Normal file
@@ -0,0 +1,77 @@
|
||||
package agent
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"qnc-server/app/main/api/internal/svc"
|
||||
"qnc-server/app/main/api/internal/types"
|
||||
"qnc-server/common/ctxdata"
|
||||
"qnc-server/common/xerr"
|
||||
)
|
||||
|
||||
type GetWhitelistListLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
func NewGetWhitelistListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetWhitelistListLogic {
|
||||
return &GetWhitelistListLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
func statusToText(status int64) string {
|
||||
switch status {
|
||||
case 1:
|
||||
return "生效"
|
||||
case 2:
|
||||
return "已失效"
|
||||
default:
|
||||
return ""
|
||||
}
|
||||
}
|
||||
|
||||
func (l *GetWhitelistListLogic) GetWhitelistList(req *types.GetWhitelistListReq) (resp *types.GetWhitelistListResp, err error) {
|
||||
userID, err := ctxdata.GetUidFromCtx(l.ctx)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "获取用户信息失败, %v", err)
|
||||
}
|
||||
if req.Page <= 0 {
|
||||
req.Page = 1
|
||||
}
|
||||
if req.PageSize <= 0 || req.PageSize > 100 {
|
||||
req.PageSize = 10
|
||||
}
|
||||
builder := l.svcCtx.UserFeatureWhitelistModel.SelectBuilder().Where("user_id = ?", userID)
|
||||
if req.IdCard != "" {
|
||||
builder = builder.Where("id_card = ?", req.IdCard)
|
||||
}
|
||||
list, total, err := l.svcCtx.UserFeatureWhitelistModel.FindPageListByPageWithTotal(l.ctx, builder, req.Page, req.PageSize, "create_time DESC")
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "查询白名单列表失败, %v", err)
|
||||
}
|
||||
items := make([]types.WhitelistItem, 0, len(list))
|
||||
for _, r := range list {
|
||||
featureName := r.FeatureApiId
|
||||
if f, e := l.svcCtx.FeatureModel.FindOne(l.ctx, r.FeatureId); e == nil {
|
||||
featureName = f.Name
|
||||
}
|
||||
items = append(items, types.WhitelistItem{
|
||||
Id: r.Id,
|
||||
IdCard: r.IdCard,
|
||||
FeatureId: r.FeatureId,
|
||||
FeatureApiId: r.FeatureApiId,
|
||||
FeatureName: featureName,
|
||||
Amount: r.Amount,
|
||||
Status: r.Status,
|
||||
StatusText: statusToText(r.Status),
|
||||
CreateTime: r.CreateTime.Format("2006-01-02 15:04:05"),
|
||||
})
|
||||
}
|
||||
return &types.GetWhitelistListResp{Total: total, List: items}, nil
|
||||
}
|
||||
70
app/main/api/internal/logic/agent/offlinefeaturelogic.go
Normal file
70
app/main/api/internal/logic/agent/offlinefeaturelogic.go
Normal file
@@ -0,0 +1,70 @@
|
||||
package agent
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"qnc-server/app/main/api/internal/svc"
|
||||
"qnc-server/app/main/api/internal/types"
|
||||
"qnc-server/common/ctxdata"
|
||||
"qnc-server/common/xerr"
|
||||
)
|
||||
|
||||
type OfflineFeatureLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
func NewOfflineFeatureLogic(ctx context.Context, svcCtx *svc.ServiceContext) *OfflineFeatureLogic {
|
||||
return &OfflineFeatureLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *OfflineFeatureLogic) OfflineFeature(req *types.OfflineFeatureReq) (resp *types.OfflineFeatureResp, err error) {
|
||||
userID, err := ctxdata.GetUidFromCtx(l.ctx)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "获取用户信息失败, %v", err)
|
||||
}
|
||||
if req.FeatureApiId == "" || req.QueryId == "" {
|
||||
return nil, errors.Wrapf(xerr.NewErrMsg("模块标识和查询记录ID不能为空"), "")
|
||||
}
|
||||
// 从 Query 获取 id_card
|
||||
query, err := l.svcCtx.QueryModel.FindOne(l.ctx, req.QueryId)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "查询记录不存在或查询失败, %v", err)
|
||||
}
|
||||
idCard := ""
|
||||
if query.QueryParams != "" {
|
||||
var params map[string]interface{}
|
||||
if e := json.Unmarshal([]byte(query.QueryParams), ¶ms); e == nil {
|
||||
if v, ok := params["id_card"]; ok {
|
||||
if s, ok := v.(string); ok {
|
||||
idCard = s
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if idCard == "" {
|
||||
return nil, errors.Wrapf(xerr.NewErrMsg("无法从查询记录中获取身份证号"), "")
|
||||
}
|
||||
needPay, amount, whitelistCreated, err := l.svcCtx.WhitelistService.ProcessOfflineFeature(l.ctx, nil, idCard, req.FeatureApiId, userID, req.QueryId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if whitelistCreated && !needPay {
|
||||
if err := l.svcCtx.WhitelistService.DeleteFeatureFromQueryData(l.ctx, nil, req.QueryId, req.FeatureApiId); err != nil {
|
||||
logx.Errorf("从报告数据中删除模块失败: %v", err)
|
||||
}
|
||||
return &types.OfflineFeatureResp{Success: true, NeedPay: false, Amount: 0}, nil
|
||||
}
|
||||
if needPay {
|
||||
return &types.OfflineFeatureResp{Success: false, NeedPay: true, Amount: amount}, nil
|
||||
}
|
||||
return &types.OfflineFeatureResp{Success: true, NeedPay: false, Amount: 0}, nil
|
||||
}
|
||||
Reference in New Issue
Block a user