f
This commit is contained in:
@@ -0,0 +1,256 @@
|
||||
package admin_dashboard
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"qnc-server/app/main/api/internal/service"
|
||||
"qnc-server/app/main/api/internal/svc"
|
||||
"qnc-server/app/main/api/internal/types"
|
||||
"qnc-server/common/globalkey"
|
||||
"qnc-server/common/xerr"
|
||||
)
|
||||
|
||||
type AdminGetDashboardStatisticsLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
func NewAdminGetDashboardStatisticsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AdminGetDashboardStatisticsLogic {
|
||||
return &AdminGetDashboardStatisticsLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *AdminGetDashboardStatisticsLogic) AdminGetDashboardStatistics() (resp *types.AdminGetDashboardStatisticsResp, err error) {
|
||||
loc, _ := time.LoadLocation("Asia/Shanghai")
|
||||
now := time.Now().In(loc)
|
||||
todayStart := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, loc)
|
||||
todayEnd := todayStart.AddDate(0, 0, 1)
|
||||
yesterdayStart := todayStart.AddDate(0, 0, -1)
|
||||
yesterdayEnd := todayStart
|
||||
monthStart := time.Date(now.Year(), now.Month(), 1, 0, 0, 0, 0, loc)
|
||||
monthEnd := monthStart.AddDate(0, 1, 0)
|
||||
|
||||
orderStats, err := l.calculateOrderStatistics(todayStart, todayEnd, yesterdayStart, yesterdayEnd, monthStart, monthEnd)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "计算订单统计失败, %v", err)
|
||||
}
|
||||
revenueStats, err := l.calculateRevenueStatistics(todayStart, todayEnd, yesterdayStart, yesterdayEnd, monthStart, monthEnd)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "计算营收统计失败, %v", err)
|
||||
}
|
||||
agentStats, err := l.calculateAgentStatistics(todayStart, monthStart)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "计算代理统计失败, %v", err)
|
||||
}
|
||||
profitStats, err := l.calculateProfitStatistics(todayStart, todayEnd, monthStart, monthEnd, revenueStats)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "计算利润统计失败, %v", err)
|
||||
}
|
||||
orderTrend, err := l.calculateOrderTrend(now, loc)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "计算订单趋势失败, %v", err)
|
||||
}
|
||||
revenueTrend, err := l.calculateRevenueTrend(now, loc)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "计算营收趋势失败, %v", err)
|
||||
}
|
||||
return &types.AdminGetDashboardStatisticsResp{
|
||||
OrderStats: orderStats,
|
||||
RevenueStats: revenueStats,
|
||||
AgentStats: agentStats,
|
||||
ProfitStats: profitStats,
|
||||
OrderTrend: orderTrend,
|
||||
RevenueTrend: revenueTrend,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (l *AdminGetDashboardStatisticsLogic) calculateOrderStatistics(todayStart, todayEnd, yesterdayStart, yesterdayEnd, monthStart, monthEnd time.Time) (types.AdminOrderStatistics, error) {
|
||||
var stats types.AdminOrderStatistics
|
||||
todayBuilder := l.svcCtx.OrderModel.SelectBuilder().
|
||||
Where("status = ? AND create_time >= ? AND create_time < ?", "paid", todayStart, todayEnd)
|
||||
todayCount, err := l.svcCtx.OrderModel.FindCount(l.ctx, todayBuilder, "id")
|
||||
if err != nil {
|
||||
return stats, err
|
||||
}
|
||||
stats.TodayCount = todayCount
|
||||
yesterdayBuilder := l.svcCtx.OrderModel.SelectBuilder().
|
||||
Where("status = ? AND create_time >= ? AND create_time < ?", "paid", yesterdayStart, yesterdayEnd)
|
||||
yesterdayCount, _ := l.svcCtx.OrderModel.FindCount(l.ctx, yesterdayBuilder, "id")
|
||||
stats.YesterdayCount = yesterdayCount
|
||||
monthBuilder := l.svcCtx.OrderModel.SelectBuilder().
|
||||
Where("status = ? AND create_time >= ? AND create_time < ?", "paid", monthStart, monthEnd)
|
||||
monthCount, _ := l.svcCtx.OrderModel.FindCount(l.ctx, monthBuilder, "id")
|
||||
stats.MonthCount = monthCount
|
||||
totalBuilder := l.svcCtx.OrderModel.SelectBuilder().Where("status = ?", "paid")
|
||||
totalCount, _ := l.svcCtx.OrderModel.FindCount(l.ctx, totalBuilder, "id")
|
||||
stats.TotalCount = totalCount
|
||||
if stats.YesterdayCount > 0 {
|
||||
stats.ChangeRate = float64(stats.TodayCount-stats.YesterdayCount) / float64(stats.YesterdayCount) * 100
|
||||
} else if stats.TodayCount > 0 {
|
||||
stats.ChangeRate = 100
|
||||
}
|
||||
return stats, nil
|
||||
}
|
||||
|
||||
func (l *AdminGetDashboardStatisticsLogic) calculateRevenueStatistics(todayStart, todayEnd, yesterdayStart, yesterdayEnd, monthStart, monthEnd time.Time) (types.AdminRevenueStatistics, error) {
|
||||
var stats types.AdminRevenueStatistics
|
||||
todayBuilder := l.svcCtx.OrderModel.SelectBuilder().
|
||||
Where("status = ? AND create_time >= ? AND create_time < ?", "paid", todayStart, todayEnd)
|
||||
todayAmount, _ := l.svcCtx.OrderModel.FindSum(l.ctx, todayBuilder, "amount")
|
||||
stats.TodayAmount = todayAmount
|
||||
yesterdayBuilder := l.svcCtx.OrderModel.SelectBuilder().
|
||||
Where("status = ? AND create_time >= ? AND create_time < ?", "paid", yesterdayStart, yesterdayEnd)
|
||||
yesterdayAmount, _ := l.svcCtx.OrderModel.FindSum(l.ctx, yesterdayBuilder, "amount")
|
||||
stats.YesterdayAmount = yesterdayAmount
|
||||
monthBuilder := l.svcCtx.OrderModel.SelectBuilder().
|
||||
Where("status = ? AND create_time >= ? AND create_time < ?", "paid", monthStart, monthEnd)
|
||||
monthAmount, _ := l.svcCtx.OrderModel.FindSum(l.ctx, monthBuilder, "amount")
|
||||
stats.MonthAmount = monthAmount
|
||||
totalBuilder := l.svcCtx.OrderModel.SelectBuilder().Where("status = ?", "paid")
|
||||
totalAmount, _ := l.svcCtx.OrderModel.FindSum(l.ctx, totalBuilder, "amount")
|
||||
stats.TotalAmount = totalAmount
|
||||
if stats.YesterdayAmount > 0 {
|
||||
stats.ChangeRate = (stats.TodayAmount - stats.YesterdayAmount) / stats.YesterdayAmount * 100
|
||||
} else if stats.TodayAmount > 0 {
|
||||
stats.ChangeRate = 100
|
||||
}
|
||||
return stats, nil
|
||||
}
|
||||
|
||||
func (l *AdminGetDashboardStatisticsLogic) calculateAgentStatistics(todayStart, monthStart time.Time) (types.AdminAgentStatistics, error) {
|
||||
var stats types.AdminAgentStatistics
|
||||
totalCount, _ := l.svcCtx.AgentModel.FindCount(l.ctx, l.svcCtx.AgentModel.SelectBuilder(), "id")
|
||||
stats.TotalCount = totalCount
|
||||
todayBuilder := l.svcCtx.AgentModel.SelectBuilder().Where("create_time >= ?", todayStart)
|
||||
todayNew, _ := l.svcCtx.AgentModel.FindCount(l.ctx, todayBuilder, "id")
|
||||
stats.TodayNew = todayNew
|
||||
monthBuilder := l.svcCtx.AgentModel.SelectBuilder().Where("create_time >= ?", monthStart)
|
||||
monthNew, _ := l.svcCtx.AgentModel.FindCount(l.ctx, monthBuilder, "id")
|
||||
stats.MonthNew = monthNew
|
||||
return stats, nil
|
||||
}
|
||||
|
||||
func (l *AdminGetDashboardStatisticsLogic) calculateProfitStatistics(todayStart, todayEnd, monthStart, monthEnd time.Time, revenueStats types.AdminRevenueStatistics) (types.AdminProfitStatistics, error) {
|
||||
var stats types.AdminProfitStatistics
|
||||
const companyTaxRate = 0.06
|
||||
todayRevenue := revenueStats.TodayAmount
|
||||
todayCommissionBuilder := l.svcCtx.AgentCommissionModel.SelectBuilder().
|
||||
Where("del_state = ? AND status != ? AND create_time >= ? AND create_time < ?", globalkey.DelStateNo, 3, todayStart, todayEnd)
|
||||
todayCommission, _ := l.svcCtx.AgentCommissionModel.FindSum(l.ctx, todayCommissionBuilder, "amount")
|
||||
todayRebateBuilder := l.svcCtx.AgentRebateModel.SelectBuilder().
|
||||
Where("del_state = ? AND status != ? AND create_time >= ? AND create_time < ?", globalkey.DelStateNo, 3, todayStart, todayEnd)
|
||||
todayRebate, _ := l.svcCtx.AgentRebateModel.FindSum(l.ctx, todayRebateBuilder, "rebate_amount")
|
||||
todayCompanyTax := todayRevenue * companyTaxRate
|
||||
todayTaxIncomeBuilder := l.svcCtx.AgentWithdrawalTaxModel.SelectBuilder().
|
||||
Where("del_state = ? AND tax_status = ? AND create_time >= ? AND create_time < ?", globalkey.DelStateNo, 2, todayStart, todayEnd)
|
||||
todayTaxIncome, _ := l.svcCtx.AgentWithdrawalTaxModel.FindSum(l.ctx, todayTaxIncomeBuilder, "tax_amount")
|
||||
todayApiCost := 0.0
|
||||
if l.svcCtx.TianyuanapiCallLogService != nil {
|
||||
todayApiStats, e := l.svcCtx.TianyuanapiCallLogService.GetStatistics(l.ctx, service.StatisticsFilter{StartDate: todayStart, EndDate: todayEnd})
|
||||
if e == nil {
|
||||
todayApiCost = todayApiStats.TotalCost
|
||||
}
|
||||
}
|
||||
stats.TodayProfit = todayRevenue - todayCommission - todayRebate - todayCompanyTax - todayApiCost + todayTaxIncome
|
||||
if todayRevenue > 0 {
|
||||
stats.TodayProfitRate = stats.TodayProfit / todayRevenue * 100
|
||||
}
|
||||
stats.TodayDetail = types.AdminProfitDetail{
|
||||
Revenue: todayRevenue, Commission: todayCommission, Rebate: todayRebate,
|
||||
CompanyTax: todayCompanyTax, ApiCost: todayApiCost, TaxIncome: todayTaxIncome,
|
||||
Profit: stats.TodayProfit, ProfitRate: stats.TodayProfitRate,
|
||||
}
|
||||
monthRevenue := revenueStats.MonthAmount
|
||||
monthCommissionBuilder := l.svcCtx.AgentCommissionModel.SelectBuilder().
|
||||
Where("del_state = ? AND status != ? AND create_time >= ? AND create_time < ?", globalkey.DelStateNo, 3, monthStart, monthEnd)
|
||||
monthCommission, _ := l.svcCtx.AgentCommissionModel.FindSum(l.ctx, monthCommissionBuilder, "amount")
|
||||
monthRebateBuilder := l.svcCtx.AgentRebateModel.SelectBuilder().
|
||||
Where("del_state = ? AND status != ? AND create_time >= ? AND create_time < ?", globalkey.DelStateNo, 3, monthStart, monthEnd)
|
||||
monthRebate, _ := l.svcCtx.AgentRebateModel.FindSum(l.ctx, monthRebateBuilder, "rebate_amount")
|
||||
monthCompanyTax := monthRevenue * companyTaxRate
|
||||
monthTaxIncomeBuilder := l.svcCtx.AgentWithdrawalTaxModel.SelectBuilder().
|
||||
Where("del_state = ? AND tax_status = ? AND create_time >= ? AND create_time < ?", globalkey.DelStateNo, 2, monthStart, monthEnd)
|
||||
monthTaxIncome, _ := l.svcCtx.AgentWithdrawalTaxModel.FindSum(l.ctx, monthTaxIncomeBuilder, "tax_amount")
|
||||
monthApiCost := 0.0
|
||||
if l.svcCtx.TianyuanapiCallLogService != nil {
|
||||
monthApiStats, e := l.svcCtx.TianyuanapiCallLogService.GetStatistics(l.ctx, service.StatisticsFilter{StartDate: monthStart, EndDate: monthEnd})
|
||||
if e == nil {
|
||||
monthApiCost = monthApiStats.TotalCost
|
||||
}
|
||||
}
|
||||
stats.MonthProfit = monthRevenue - monthCommission - monthRebate - monthCompanyTax - monthApiCost + monthTaxIncome
|
||||
if monthRevenue > 0 {
|
||||
stats.MonthProfitRate = stats.MonthProfit / monthRevenue * 100
|
||||
}
|
||||
stats.MonthDetail = types.AdminProfitDetail{
|
||||
Revenue: monthRevenue, Commission: monthCommission, Rebate: monthRebate,
|
||||
CompanyTax: monthCompanyTax, ApiCost: monthApiCost, TaxIncome: monthTaxIncome,
|
||||
Profit: stats.MonthProfit, ProfitRate: stats.MonthProfitRate,
|
||||
}
|
||||
totalRevenue := revenueStats.TotalAmount
|
||||
totalCommissionBuilder := l.svcCtx.AgentCommissionModel.SelectBuilder().Where("del_state = ? AND status != ?", globalkey.DelStateNo, 3)
|
||||
totalCommission, _ := l.svcCtx.AgentCommissionModel.FindSum(l.ctx, totalCommissionBuilder, "amount")
|
||||
totalRebateBuilder := l.svcCtx.AgentRebateModel.SelectBuilder().Where("status != ?", 3)
|
||||
totalRebate, _ := l.svcCtx.AgentRebateModel.FindSum(l.ctx, totalRebateBuilder, "rebate_amount")
|
||||
totalCompanyTax := totalRevenue * companyTaxRate
|
||||
totalTaxIncomeBuilder := l.svcCtx.AgentWithdrawalTaxModel.SelectBuilder().Where("tax_status = ?", 2)
|
||||
totalTaxIncome, _ := l.svcCtx.AgentWithdrawalTaxModel.FindSum(l.ctx, totalTaxIncomeBuilder, "tax_amount")
|
||||
totalApiCost := 0.0
|
||||
if l.svcCtx.TianyuanapiCallLogService != nil {
|
||||
totalApiStats, e := l.svcCtx.TianyuanapiCallLogService.GetStatistics(l.ctx, service.StatisticsFilter{})
|
||||
if e == nil {
|
||||
totalApiCost = totalApiStats.TotalCost
|
||||
}
|
||||
}
|
||||
stats.TotalProfit = totalRevenue - totalCommission - totalRebate - totalCompanyTax - totalApiCost + totalTaxIncome
|
||||
if totalRevenue > 0 {
|
||||
stats.TotalProfitRate = stats.TotalProfit / totalRevenue * 100
|
||||
}
|
||||
stats.TotalDetail = types.AdminProfitDetail{
|
||||
Revenue: totalRevenue, Commission: totalCommission, Rebate: totalRebate,
|
||||
CompanyTax: totalCompanyTax, ApiCost: totalApiCost, TaxIncome: totalTaxIncome,
|
||||
Profit: stats.TotalProfit, ProfitRate: stats.TotalProfitRate,
|
||||
}
|
||||
return stats, nil
|
||||
}
|
||||
|
||||
func (l *AdminGetDashboardStatisticsLogic) calculateOrderTrend(now time.Time, loc *time.Location) ([]types.AdminTrendData, error) {
|
||||
var trend []types.AdminTrendData
|
||||
for i := 6; i >= 0; i-- {
|
||||
date := now.AddDate(0, 0, -i)
|
||||
dateStart := time.Date(date.Year(), date.Month(), date.Day(), 0, 0, 0, 0, loc)
|
||||
dateEnd := dateStart.AddDate(0, 0, 1)
|
||||
builder := l.svcCtx.OrderModel.SelectBuilder().
|
||||
Where("status = ? AND create_time >= ? AND create_time < ?", "paid", dateStart, dateEnd)
|
||||
count, err := l.svcCtx.OrderModel.FindCount(l.ctx, builder, "id")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
trend = append(trend, types.AdminTrendData{Date: date.Format("01-02"), Value: float64(count)})
|
||||
}
|
||||
return trend, nil
|
||||
}
|
||||
|
||||
func (l *AdminGetDashboardStatisticsLogic) calculateRevenueTrend(now time.Time, loc *time.Location) ([]types.AdminTrendData, error) {
|
||||
var trend []types.AdminTrendData
|
||||
for i := 6; i >= 0; i-- {
|
||||
date := now.AddDate(0, 0, -i)
|
||||
dateStart := time.Date(date.Year(), date.Month(), date.Day(), 0, 0, 0, 0, loc)
|
||||
dateEnd := dateStart.AddDate(0, 0, 1)
|
||||
builder := l.svcCtx.OrderModel.SelectBuilder().
|
||||
Where("status = ? AND create_time >= ? AND create_time < ?", "paid", dateStart, dateEnd)
|
||||
amount, err := l.svcCtx.OrderModel.FindSum(l.ctx, builder, "amount")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
trend = append(trend, types.AdminTrendData{Date: date.Format("01-02"), Value: amount})
|
||||
}
|
||||
return trend, nil
|
||||
}
|
||||
@@ -34,6 +34,12 @@ func (l *AdminCreateFeatureLogic) AdminCreateFeature(req *types.AdminCreateFeatu
|
||||
ApiId: req.ApiId,
|
||||
Name: req.Name,
|
||||
}
|
||||
if req.WhitelistPrice != nil {
|
||||
data.WhitelistPrice = *req.WhitelistPrice
|
||||
}
|
||||
if req.CostPrice != nil {
|
||||
data.CostPrice = *req.CostPrice
|
||||
}
|
||||
|
||||
// 2. 数据库操作
|
||||
result, err := l.svcCtx.FeatureModel.Insert(l.ctx, nil, data)
|
||||
|
||||
@@ -35,11 +35,13 @@ func (l *AdminGetFeatureDetailLogic) AdminGetFeatureDetail(req *types.AdminGetFe
|
||||
|
||||
// 2. 构建响应
|
||||
resp = &types.AdminGetFeatureDetailResp{
|
||||
Id: record.Id,
|
||||
ApiId: record.ApiId,
|
||||
Name: record.Name,
|
||||
CreateTime: record.CreateTime.Format("2006-01-02 15:04:05"),
|
||||
UpdateTime: record.UpdateTime.Format("2006-01-02 15:04:05"),
|
||||
Id: record.Id,
|
||||
ApiId: record.ApiId,
|
||||
Name: record.Name,
|
||||
WhitelistPrice: record.WhitelistPrice,
|
||||
CostPrice: record.CostPrice,
|
||||
CreateTime: record.CreateTime.Format("2006-01-02 15:04:05"),
|
||||
UpdateTime: record.UpdateTime.Format("2006-01-02 15:04:05"),
|
||||
}
|
||||
|
||||
return resp, nil
|
||||
|
||||
@@ -49,11 +49,13 @@ func (l *AdminGetFeatureListLogic) AdminGetFeatureList(req *types.AdminGetFeatur
|
||||
items := make([]types.FeatureListItem, 0, len(list))
|
||||
for _, item := range list {
|
||||
listItem := types.FeatureListItem{
|
||||
Id: item.Id,
|
||||
ApiId: item.ApiId,
|
||||
Name: item.Name,
|
||||
CreateTime: item.CreateTime.Format("2006-01-02 15:04:05"),
|
||||
UpdateTime: item.UpdateTime.Format("2006-01-02 15:04:05"),
|
||||
Id: item.Id,
|
||||
ApiId: item.ApiId,
|
||||
Name: item.Name,
|
||||
WhitelistPrice: item.WhitelistPrice,
|
||||
CostPrice: item.CostPrice,
|
||||
CreateTime: item.CreateTime.Format("2006-01-02 15:04:05"),
|
||||
UpdateTime: item.UpdateTime.Format("2006-01-02 15:04:05"),
|
||||
}
|
||||
items = append(items, listItem)
|
||||
}
|
||||
|
||||
@@ -46,6 +46,12 @@ func (l *AdminUpdateFeatureLogic) AdminUpdateFeature(req *types.AdminUpdateFeatu
|
||||
if req.Name != nil && *req.Name != "" {
|
||||
record.Name = *req.Name
|
||||
}
|
||||
if req.WhitelistPrice != nil {
|
||||
record.WhitelistPrice = *req.WhitelistPrice
|
||||
}
|
||||
if req.CostPrice != nil {
|
||||
record.CostPrice = *req.CostPrice
|
||||
}
|
||||
|
||||
// 4. 执行更新操作
|
||||
err = l.svcCtx.FeatureModel.UpdateWithVersion(l.ctx, nil, record)
|
||||
|
||||
@@ -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
|
||||
}
|
||||
@@ -115,6 +115,8 @@ func BuildEncryptedQuery(ctx context.Context, svcCtx *svc.ServiceContext, queryM
|
||||
if err != nil {
|
||||
return "", errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "报告查询, 报告结构体复制失败, %v", err)
|
||||
}
|
||||
// 显式写入查询记录 ID,供前端报告页「模块下架」等白名单能力使用
|
||||
query.Id = queryModel.Id
|
||||
product, err := svcCtx.ProductModel.FindOne(ctx, queryModel.ProductId)
|
||||
if err != nil {
|
||||
return "", errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "报告查询, 获取商品信息失败, %v", err)
|
||||
|
||||
Reference in New Issue
Block a user