feat: 营销分析增加积分健康度汇总接口
All checks were successful
Build and Deploy / deploy (push) Successful in 2m4s
All checks were successful
Build and Deploy / deploy (push) Successful in 2m4s
按流水时间统计发放/扣回/调账与环比趋势,并返回账户可用积分时点沉淀。 Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
parent
26ef78d01e
commit
9e45e7f185
@ -203,3 +203,103 @@ public class MarketingCouponTemplateRankOutput
|
|||||||
/// <summary>折扣占 GMV;无 GMV 时为 null。</summary>
|
/// <summary>折扣占 GMV;无 GMV 时为 null。</summary>
|
||||||
public decimal? DiscountToGmvRate { get; set; }
|
public decimal? DiscountToGmvRate { get; set; }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 积分健康度分析(定级引擎,非营销成本 ROI)。
|
||||||
|
/// </summary>
|
||||||
|
public class MarketingAnalysisPointsOutput
|
||||||
|
{
|
||||||
|
/// <summary>当前区间 KPI。</summary>
|
||||||
|
public MarketingPointsKpiOutput Kpis { get; set; } = new();
|
||||||
|
|
||||||
|
/// <summary>上一同等天数 KPI。</summary>
|
||||||
|
public MarketingPointsKpiOutput PreviousKpis { get; set; } = new();
|
||||||
|
|
||||||
|
/// <summary>环比。</summary>
|
||||||
|
public MarketingPointsGrowthOutput Growth { get; set; } = new();
|
||||||
|
|
||||||
|
/// <summary>日趋势(发放/扣回/调账)。</summary>
|
||||||
|
public List<MarketingPointsTrendPointOutput> Trend { get; set; } = [];
|
||||||
|
|
||||||
|
/// <summary>时点账户沉淀(不参与环比)。</summary>
|
||||||
|
public MarketingPointsLiabilityOutput Liability { get; set; } = new();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 积分区间 KPI。
|
||||||
|
/// </summary>
|
||||||
|
public class MarketingPointsKpiOutput
|
||||||
|
{
|
||||||
|
/// <summary>发放积分合计(Earn)。</summary>
|
||||||
|
public int EarnPoints { get; set; }
|
||||||
|
|
||||||
|
/// <summary>发放笔数。</summary>
|
||||||
|
public int EarnCount { get; set; }
|
||||||
|
|
||||||
|
/// <summary>退款/删单扣回积分合计(绝对值)。</summary>
|
||||||
|
public int ClawbackPoints { get; set; }
|
||||||
|
|
||||||
|
/// <summary>扣回笔数。</summary>
|
||||||
|
public int ClawbackCount { get; set; }
|
||||||
|
|
||||||
|
/// <summary>人工调账净额(可正可负)。</summary>
|
||||||
|
public int AdjustPoints { get; set; }
|
||||||
|
|
||||||
|
/// <summary>人工调账笔数。</summary>
|
||||||
|
public int AdjustCount { get; set; }
|
||||||
|
|
||||||
|
/// <summary>大额调账笔数(|积分|≥阈值)。</summary>
|
||||||
|
public int LargeAdjustCount { get; set; }
|
||||||
|
|
||||||
|
/// <summary>消耗积分合计(Consume,绝对值)。</summary>
|
||||||
|
public int ConsumePoints { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 积分 KPI 环比(百分数)。
|
||||||
|
/// </summary>
|
||||||
|
public class MarketingPointsGrowthOutput
|
||||||
|
{
|
||||||
|
public decimal? EarnPoints { get; set; }
|
||||||
|
|
||||||
|
public decimal? ClawbackPoints { get; set; }
|
||||||
|
|
||||||
|
public decimal? AdjustPoints { get; set; }
|
||||||
|
|
||||||
|
public decimal? AdjustCount { get; set; }
|
||||||
|
|
||||||
|
public decimal? LargeAdjustCount { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 积分日趋势。
|
||||||
|
/// </summary>
|
||||||
|
public class MarketingPointsTrendPointOutput
|
||||||
|
{
|
||||||
|
/// <summary>日期 yyyy-MM-dd。</summary>
|
||||||
|
public string Date { get; set; } = string.Empty;
|
||||||
|
|
||||||
|
public int EarnPoints { get; set; }
|
||||||
|
|
||||||
|
public int ClawbackPoints { get; set; }
|
||||||
|
|
||||||
|
public int AdjustPoints { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 积分账户时点沉淀。
|
||||||
|
/// </summary>
|
||||||
|
public class MarketingPointsLiabilityOutput
|
||||||
|
{
|
||||||
|
/// <summary>有积分账户数(AvailablePoints>0)。</summary>
|
||||||
|
public int AccountWithPoints { get; set; }
|
||||||
|
|
||||||
|
/// <summary>可用积分合计。</summary>
|
||||||
|
public int AvailablePointsTotal { get; set; }
|
||||||
|
|
||||||
|
/// <summary>累计积分合计。</summary>
|
||||||
|
public int TotalPointsSum { get; set; }
|
||||||
|
|
||||||
|
/// <summary>人均可用积分;无账户时为 null。</summary>
|
||||||
|
public decimal? AvgAvailablePoints { get; set; }
|
||||||
|
}
|
||||||
|
|||||||
@ -3,6 +3,8 @@ using Fake.Domain.Exceptions;
|
|||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
using MiniExcelLibs;
|
using MiniExcelLibs;
|
||||||
using Oms.Application.Dtos.Marketing;
|
using Oms.Application.Dtos.Marketing;
|
||||||
|
using Oms.Domain.MarketingAggregate.Entities;
|
||||||
|
using Oms.Domain.MarketingAggregate.Enums;
|
||||||
using Oms.Domain.MarketingAggregate.Repositories;
|
using Oms.Domain.MarketingAggregate.Repositories;
|
||||||
using Oms.Domain.OrdersAggregate;
|
using Oms.Domain.OrdersAggregate;
|
||||||
using Oms.Domain.OrdersAggregate.Repositories;
|
using Oms.Domain.OrdersAggregate.Repositories;
|
||||||
@ -10,11 +12,14 @@ using Oms.Domain.OrdersAggregate.Repositories;
|
|||||||
namespace Oms.Application.Services.Marketing;
|
namespace Oms.Application.Services.Marketing;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 营销分析 API(管理端总览 + 优惠券)。
|
/// 营销分析 API(管理端总览 + 优惠券 + 积分健康度)。
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[ApiExplorerSettings(GroupName = "OMS-Marketing")]
|
[ApiExplorerSettings(GroupName = "OMS-Marketing")]
|
||||||
public class MarketingAnalysisService : ApplicationService
|
public class MarketingAnalysisService : ApplicationService
|
||||||
{
|
{
|
||||||
|
/// <summary>大额调账阈值(绝对值)。</summary>
|
||||||
|
private const int LargeAdjustThreshold = 500;
|
||||||
|
|
||||||
private IOrderRepository OrderRepository =>
|
private IOrderRepository OrderRepository =>
|
||||||
LazyServiceProvider.GetRequiredService<IOrderRepository>();
|
LazyServiceProvider.GetRequiredService<IOrderRepository>();
|
||||||
|
|
||||||
@ -24,6 +29,12 @@ public class MarketingAnalysisService : ApplicationService
|
|||||||
private ICouponTemplateRepository TemplateRepository =>
|
private ICouponTemplateRepository TemplateRepository =>
|
||||||
LazyServiceProvider.GetRequiredService<ICouponTemplateRepository>();
|
LazyServiceProvider.GetRequiredService<ICouponTemplateRepository>();
|
||||||
|
|
||||||
|
private ICustomerPointsFlowRepository PointsFlowRepository =>
|
||||||
|
LazyServiceProvider.GetRequiredService<ICustomerPointsFlowRepository>();
|
||||||
|
|
||||||
|
private ICustomerPointsAccountRepository PointsAccountRepository =>
|
||||||
|
LazyServiceProvider.GetRequiredService<ICustomerPointsAccountRepository>();
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 营销分析总览:折前 GMV、营销成本、折扣率、客户实收、用券占比、日趋势与折扣构成;含上一同等天数环比。
|
/// 营销分析总览:折前 GMV、营销成本、折扣率、客户实收、用券占比、日趋势与折扣构成;含上一同等天数环比。
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@ -100,6 +111,45 @@ public class MarketingAnalysisService : ApplicationService
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 积分健康度:区间发放/扣回/调账与环比、日趋势;账户可用积分时点沉淀。按流水 CreateTime,与支付字段无关。
|
||||||
|
/// </summary>
|
||||||
|
[HttpGet("/marketing/analysis/points")]
|
||||||
|
public async Task<MarketingAnalysisPointsOutput> GetPointsAsync(
|
||||||
|
[FromQuery] MarketingAnalysisQueryInput input)
|
||||||
|
{
|
||||||
|
var (start, endExclusive, _) = NormalizeRange(input);
|
||||||
|
var (prevStart, prevEndExclusive) = PreviousRange(start, endExclusive);
|
||||||
|
|
||||||
|
// 串行查询,避免同一 SqlSugar 连接并发复用
|
||||||
|
var currentFlows = await PointsFlowRepository.GetListAsync(x =>
|
||||||
|
x.CreateTime >= start && x.CreateTime < endExclusive);
|
||||||
|
var previousFlows = await PointsFlowRepository.GetListAsync(x =>
|
||||||
|
x.CreateTime >= prevStart && x.CreateTime < prevEndExclusive);
|
||||||
|
var accounts = await PointsAccountRepository.GetListAsync(x => x.AvailablePoints > 0 || x.TotalPoints > 0);
|
||||||
|
|
||||||
|
var kpis = BuildPointsKpis(currentFlows);
|
||||||
|
var previousKpis = BuildPointsKpis(previousFlows);
|
||||||
|
var withAvailable = accounts.Where(x => x.AvailablePoints > 0).ToList();
|
||||||
|
|
||||||
|
return new MarketingAnalysisPointsOutput
|
||||||
|
{
|
||||||
|
Kpis = kpis,
|
||||||
|
PreviousKpis = previousKpis,
|
||||||
|
Growth = BuildPointsGrowth(kpis, previousKpis),
|
||||||
|
Trend = BuildPointsTrend(currentFlows, start, endExclusive),
|
||||||
|
Liability = new MarketingPointsLiabilityOutput
|
||||||
|
{
|
||||||
|
AccountWithPoints = withAvailable.Count,
|
||||||
|
AvailablePointsTotal = withAvailable.Sum(x => x.AvailablePoints),
|
||||||
|
TotalPointsSum = accounts.Sum(x => x.TotalPoints),
|
||||||
|
AvgAvailablePoints = withAvailable.Count > 0
|
||||||
|
? Math.Round((decimal)withAvailable.Sum(x => x.AvailablePoints) / withAvailable.Count, 1)
|
||||||
|
: null
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
private async Task<MarketingAnalysisCouponsOutput> BuildCouponsAsync(MarketingAnalysisQueryInput input)
|
private async Task<MarketingAnalysisCouponsOutput> BuildCouponsAsync(MarketingAnalysisQueryInput input)
|
||||||
{
|
{
|
||||||
var (start, endExclusive, usePayTime) = NormalizeRange(input);
|
var (start, endExclusive, usePayTime) = NormalizeRange(input);
|
||||||
@ -382,4 +432,77 @@ public class MarketingAnalysisService : ApplicationService
|
|||||||
|
|
||||||
return points;
|
return points;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static MarketingPointsKpiOutput BuildPointsKpis(List<CustomerPointsFlow> flows)
|
||||||
|
{
|
||||||
|
var earn = flows.Where(x => x.ChangeType == PointsChangeType.Earn).ToList();
|
||||||
|
var clawback = flows.Where(x => x.ChangeType == PointsChangeType.RefundClawback).ToList();
|
||||||
|
var adjust = flows.Where(x => x.ChangeType == PointsChangeType.Adjust).ToList();
|
||||||
|
var consume = flows.Where(x => x.ChangeType == PointsChangeType.Consume).ToList();
|
||||||
|
|
||||||
|
return new MarketingPointsKpiOutput
|
||||||
|
{
|
||||||
|
EarnPoints = earn.Sum(x => x.Points),
|
||||||
|
EarnCount = earn.Count,
|
||||||
|
ClawbackPoints = clawback.Sum(x => Math.Abs(x.Points)),
|
||||||
|
ClawbackCount = clawback.Count,
|
||||||
|
AdjustPoints = adjust.Sum(x => x.Points),
|
||||||
|
AdjustCount = adjust.Count,
|
||||||
|
LargeAdjustCount = adjust.Count(x => Math.Abs(x.Points) >= LargeAdjustThreshold),
|
||||||
|
ConsumePoints = consume.Sum(x => Math.Abs(x.Points))
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
private static MarketingPointsGrowthOutput BuildPointsGrowth(
|
||||||
|
MarketingPointsKpiOutput current,
|
||||||
|
MarketingPointsKpiOutput previous)
|
||||||
|
{
|
||||||
|
return new MarketingPointsGrowthOutput
|
||||||
|
{
|
||||||
|
EarnPoints = RateGrowth(current.EarnPoints, previous.EarnPoints),
|
||||||
|
ClawbackPoints = RateGrowth(current.ClawbackPoints, previous.ClawbackPoints),
|
||||||
|
AdjustPoints = RateGrowth(current.AdjustPoints, previous.AdjustPoints),
|
||||||
|
AdjustCount = RateGrowth(current.AdjustCount, previous.AdjustCount),
|
||||||
|
LargeAdjustCount = RateGrowth(current.LargeAdjustCount, previous.LargeAdjustCount)
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
private static List<MarketingPointsTrendPointOutput> BuildPointsTrend(
|
||||||
|
List<CustomerPointsFlow> flows,
|
||||||
|
DateTime start,
|
||||||
|
DateTime endExclusive)
|
||||||
|
{
|
||||||
|
var map = flows
|
||||||
|
.GroupBy(x => x.CreateTime.Date)
|
||||||
|
.ToDictionary(
|
||||||
|
g => g.Key,
|
||||||
|
g => new MarketingPointsTrendPointOutput
|
||||||
|
{
|
||||||
|
Date = g.Key.ToString("yyyy-MM-dd"),
|
||||||
|
EarnPoints = g.Where(x => x.ChangeType == PointsChangeType.Earn).Sum(x => x.Points),
|
||||||
|
ClawbackPoints = g.Where(x => x.ChangeType == PointsChangeType.RefundClawback)
|
||||||
|
.Sum(x => Math.Abs(x.Points)),
|
||||||
|
AdjustPoints = g.Where(x => x.ChangeType == PointsChangeType.Adjust).Sum(x => x.Points)
|
||||||
|
});
|
||||||
|
|
||||||
|
var points = new List<MarketingPointsTrendPointOutput>();
|
||||||
|
for (var day = start.Date; day < endExclusive; day = day.AddDays(1))
|
||||||
|
{
|
||||||
|
if (map.TryGetValue(day, out var point))
|
||||||
|
{
|
||||||
|
points.Add(point);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
points.Add(new MarketingPointsTrendPointOutput
|
||||||
|
{
|
||||||
|
Date = day.ToString("yyyy-MM-dd")
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
if (points.Count > 400) break;
|
||||||
|
}
|
||||||
|
|
||||||
|
return points;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user