|
|
@@ -8,6 +8,7 @@ import com.ytpm.advertise.param.HourReportParam;
|
|
|
import com.ytpm.advertise.view.ComprehensiveAppReport;
|
|
|
import com.ytpm.advertise.view.HourReportRes;
|
|
|
import com.ytpm.advertise.view.HourReportView;
|
|
|
+import com.ytpm.agent.param.DateRangeParams;
|
|
|
import com.ytpm.agent.param.IndexResParam;
|
|
|
import com.ytpm.agent.view.AgentChannelView;
|
|
|
import com.ytpm.agent.view.IndexHourReportView;
|
|
|
@@ -39,6 +40,7 @@ import java.util.HashMap;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
import java.util.Objects;
|
|
|
+import java.util.function.Function;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
@Api(tags = "首页数据统计模块")
|
|
|
@@ -66,114 +68,187 @@ public class AgentIndexController {
|
|
|
@PostMapping("/profit")
|
|
|
public Result<?> profit() {
|
|
|
// 1. 准备日期参数
|
|
|
+ DateRangeParams dateParams = prepareDateParams();
|
|
|
+
|
|
|
+ // 2. 构建查询参数
|
|
|
+ ComprehensiveReportParam reportParam = buildReportParam(dateParams);
|
|
|
+
|
|
|
+ // 3. 获取报表数据
|
|
|
+ List<ComprehensiveAppReport> appReport = fetchReportData(reportParam);
|
|
|
+
|
|
|
+ // 4. 按平台分类统计
|
|
|
+ List<IndexResParam> result = processPlatformReports(appReport, dateParams);
|
|
|
+
|
|
|
+ return Result.resultObjOk(result);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 准备日期参数
|
|
|
+ */
|
|
|
+ private DateRangeParams prepareDateParams() {
|
|
|
LocalDate today = LocalDate.now();
|
|
|
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
|
|
|
|
|
|
- // 今日和昨日日期
|
|
|
- String todayStr = today.format(formatter);
|
|
|
- String yesterdayStr = today.minusDays(1).format(formatter);
|
|
|
-
|
|
|
- // 本月日期范围
|
|
|
- LocalDate firstDayOfMonth = today.with(TemporalAdjusters.firstDayOfMonth());
|
|
|
- LocalDate lastDayOfMonth = today.with(TemporalAdjusters.lastDayOfMonth());
|
|
|
- String monthStartStr = firstDayOfMonth.format(formatter);
|
|
|
- String monthEndStr = lastDayOfMonth.format(formatter);
|
|
|
+ return new DateRangeParams(
|
|
|
+ today.format(formatter),
|
|
|
+ today.minusDays(1).format(formatter),
|
|
|
+ today.with(TemporalAdjusters.firstDayOfMonth()).format(formatter),
|
|
|
+ today.with(TemporalAdjusters.lastDayOfMonth()).format(formatter)
|
|
|
+ );
|
|
|
+ }
|
|
|
|
|
|
- // 2. 构建查询参数(查询本月所有平台数据)
|
|
|
+ /**
|
|
|
+ * 构建报表查询参数
|
|
|
+ */
|
|
|
+ private ComprehensiveReportParam buildReportParam(DateRangeParams dateParams) {
|
|
|
ComprehensiveReportParam param = new ComprehensiveReportParam();
|
|
|
- param.setStartdate(Integer.parseInt(monthStartStr));
|
|
|
- param.setEnddate(Integer.parseInt(monthEndStr));
|
|
|
+
|
|
|
+ // 设置日期范围
|
|
|
+ param.setStartdate(Integer.parseInt(dateParams.getMonthStart()));
|
|
|
+ param.setEnddate(Integer.parseInt(dateParams.getMonthEnd()));
|
|
|
|
|
|
// 添加所有平台ID
|
|
|
- List<Integer> platformIds = Arrays.stream(AdPlatformTypeEnum.values())
|
|
|
+ param.setNetwork_firm_id_list(Arrays.stream(AdPlatformTypeEnum.values())
|
|
|
.map(AdPlatformTypeEnum::getCode)
|
|
|
- .collect(Collectors.toList());
|
|
|
- param.setNetwork_firm_id_list(new ArrayList<>(platformIds));
|
|
|
+ .collect(Collectors.toList()));
|
|
|
|
|
|
// 设置分组
|
|
|
param.setGroup_by(Arrays.asList("date", "network_firm_id"));
|
|
|
|
|
|
- // 3. 获取报表数据
|
|
|
- long currentTime = System.currentTimeMillis();
|
|
|
+ return param;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取报表数据
|
|
|
+ */
|
|
|
+ private List<ComprehensiveAppReport> fetchReportData(ComprehensiveReportParam param) {
|
|
|
+ long startTime = System.currentTimeMillis();
|
|
|
List<ComprehensiveAppReport> appReport = advertiseFeign.getAppReport(param);
|
|
|
- long endTime = System.currentTimeMillis();
|
|
|
- log.info("调用api接口时间{}", endTime - currentTime);
|
|
|
+ log.info("调用API接口耗时: {}ms", System.currentTimeMillis() - startTime);
|
|
|
+ return appReport;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 处理平台报告
|
|
|
+ */
|
|
|
+ private List<IndexResParam> processPlatformReports(List<ComprehensiveAppReport> appReport,
|
|
|
+ DateRangeParams dateParams) {
|
|
|
+ long startTime = System.currentTimeMillis();
|
|
|
|
|
|
- // 4. 按平台分类统计
|
|
|
List<IndexResParam> result = Arrays.stream(AdPlatformTypeEnum.values())
|
|
|
- .map(platform -> {
|
|
|
- IndexResParam resParam = new IndexResParam();
|
|
|
- resParam.setPlatform(platform.getDesc());
|
|
|
-
|
|
|
- // 过滤当前平台的数据
|
|
|
- List<ComprehensiveAppReport> platformReports = appReport.stream()
|
|
|
- .filter(item -> String.valueOf(platform.getCode()).equals(item.getNetwork_firm_id()))
|
|
|
- .collect(Collectors.toList());
|
|
|
-
|
|
|
- // 统计今日数据
|
|
|
- BigDecimal todayRevenue = platformReports.stream()
|
|
|
- .filter(item -> todayStr.equals(item.getDate()))
|
|
|
- .map(ComprehensiveAppReport::getRevenue)
|
|
|
- .filter(Objects::nonNull)
|
|
|
- .reduce(BigDecimal.ZERO, BigDecimal::add);
|
|
|
- //ecpm数据
|
|
|
- BigDecimal todayEcpm = platformReports.stream()
|
|
|
- .filter(item -> todayStr.equals(item.getDate()))
|
|
|
- .map(ComprehensiveAppReport::getEcpm)
|
|
|
- .filter(Objects::nonNull)
|
|
|
- .reduce(BigDecimal.ZERO, BigDecimal::add);
|
|
|
- resParam.setToday(todayRevenue.setScale(2, RoundingMode.HALF_UP).toString());
|
|
|
- resParam.setEcpmToday(todayEcpm.setScale(2, RoundingMode.HALF_UP).toString());
|
|
|
-
|
|
|
- // 统计昨日数据
|
|
|
- BigDecimal yesterdayRevenue = platformReports.stream()
|
|
|
- .filter(item -> yesterdayStr.equals(item.getDate()))
|
|
|
- .map(ComprehensiveAppReport::getRevenue)
|
|
|
- .filter(Objects::nonNull)
|
|
|
- .reduce(BigDecimal.ZERO, BigDecimal::add);
|
|
|
- resParam.setYesterday(yesterdayRevenue.setScale(2, RoundingMode.HALF_UP).toString());
|
|
|
-
|
|
|
- //昨天ecpm数据
|
|
|
- BigDecimal yesterdayEcpm = platformReports.stream()
|
|
|
- .filter(item -> yesterdayStr.equals(item.getDate()))
|
|
|
- .map(ComprehensiveAppReport::getEcpm)
|
|
|
- .filter(Objects::nonNull)
|
|
|
- .reduce(BigDecimal.ZERO, BigDecimal::add);
|
|
|
- resParam.setEcpmYesterday(yesterdayEcpm.setScale(2, RoundingMode.HALF_UP).toString());
|
|
|
-
|
|
|
- // 统计本月数据
|
|
|
- BigDecimal monthRevenue = platformReports.stream()
|
|
|
- .filter(item -> {
|
|
|
- String date = item.getDate();
|
|
|
- return date != null && date.compareTo(monthStartStr) >= 0
|
|
|
- && date.compareTo(monthEndStr) <= 0;
|
|
|
- })
|
|
|
- .map(ComprehensiveAppReport::getRevenue)
|
|
|
- .filter(Objects::nonNull)
|
|
|
- .reduce(BigDecimal.ZERO, BigDecimal::add);
|
|
|
-
|
|
|
- BigDecimal monthEcpm = platformReports.stream()
|
|
|
- .filter(
|
|
|
- item -> {
|
|
|
- String date = item.getDate();
|
|
|
- return date != null && date.compareTo(monthStartStr) >= 0
|
|
|
- && date.compareTo(monthEndStr) <= 0;
|
|
|
- })
|
|
|
- .map(ComprehensiveAppReport::getEcpm)
|
|
|
- .filter(Objects::nonNull)
|
|
|
- .reduce(BigDecimal.ZERO, BigDecimal::add);
|
|
|
- resParam.setMonth(monthRevenue.setScale(2, RoundingMode.HALF_UP).toString());
|
|
|
- resParam.setEcpmMonth(monthEcpm.setScale(2, RoundingMode.HALF_UP).toString());
|
|
|
-
|
|
|
- return resParam;
|
|
|
- })
|
|
|
+ .map(platform -> buildPlatformReport(platform, appReport, dateParams))
|
|
|
.collect(Collectors.toList());
|
|
|
- long endResolve = System.currentTimeMillis();
|
|
|
- log.debug("stream执行时间{}", endResolve - endTime);
|
|
|
|
|
|
- return Result.resultObjOk(result);
|
|
|
+ log.debug("报告处理耗时: {}ms", System.currentTimeMillis() - startTime);
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 构建单个平台报告
|
|
|
+ */
|
|
|
+ private IndexResParam buildPlatformReport(AdPlatformTypeEnum platform,
|
|
|
+ List<ComprehensiveAppReport> allReports,
|
|
|
+ DateRangeParams dateParams) {
|
|
|
+ IndexResParam resParam = new IndexResParam();
|
|
|
+ resParam.setPlatform(platform.getDesc());
|
|
|
+
|
|
|
+ // 过滤当前平台的数据
|
|
|
+ List<ComprehensiveAppReport> platformReports = filterByPlatform(allReports, platform);
|
|
|
+
|
|
|
+ // 设置各项指标
|
|
|
+ setRevenueMetrics(resParam, platformReports, dateParams);
|
|
|
+ setEcpmMetrics(resParam, platformReports, dateParams);
|
|
|
+
|
|
|
+ return resParam;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 按平台过滤报告
|
|
|
+ */
|
|
|
+ private List<ComprehensiveAppReport> filterByPlatform(List<ComprehensiveAppReport> reports,
|
|
|
+ AdPlatformTypeEnum platform) {
|
|
|
+ return reports.stream()
|
|
|
+ .filter(item -> String.valueOf(platform.getCode()).equals(item.getNetwork_firm_id()))
|
|
|
+ .collect(Collectors.toList());
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 设置收入指标
|
|
|
+ */
|
|
|
+ private void setRevenueMetrics(IndexResParam resParam,
|
|
|
+ List<ComprehensiveAppReport> reports,
|
|
|
+ DateRangeParams dateParams) {
|
|
|
+ // 今日收入
|
|
|
+ BigDecimal todayRevenue = sumByDate(reports, dateParams.getToday(), ComprehensiveAppReport::getRevenue);
|
|
|
+ resParam.setToday(formatAmount(todayRevenue));
|
|
|
+
|
|
|
+ // 昨日收入
|
|
|
+ BigDecimal yesterdayRevenue = sumByDate(reports, dateParams.getYesterday(), ComprehensiveAppReport::getRevenue);
|
|
|
+ resParam.setYesterday(formatAmount(yesterdayRevenue));
|
|
|
+
|
|
|
+ // 本月收入
|
|
|
+ BigDecimal monthRevenue = sumByDateRange(reports, dateParams.getMonthStart(), dateParams.getMonthEnd(), ComprehensiveAppReport::getRevenue);
|
|
|
+ resParam.setMonth(formatAmount(monthRevenue));
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 设置eCPM指标
|
|
|
+ */
|
|
|
+ private void setEcpmMetrics(IndexResParam resParam,
|
|
|
+ List<ComprehensiveAppReport> reports,
|
|
|
+ DateRangeParams dateParams) {
|
|
|
+ // 今日eCPM
|
|
|
+ BigDecimal todayEcpm = sumByDate(reports, dateParams.getToday(), ComprehensiveAppReport::getEcpm);
|
|
|
+ resParam.setEcpmToday(formatAmount(todayEcpm));
|
|
|
+
|
|
|
+ // 昨日eCPM
|
|
|
+ BigDecimal yesterdayEcpm = sumByDate(reports, dateParams.getYesterday(), ComprehensiveAppReport::getEcpm);
|
|
|
+ resParam.setEcpmYesterday(formatAmount(yesterdayEcpm));
|
|
|
+
|
|
|
+ // 本月eCPM
|
|
|
+ BigDecimal monthEcpm = sumByDateRange(reports, dateParams.getMonthStart(), dateParams.getMonthEnd(), ComprehensiveAppReport::getEcpm);
|
|
|
+ resParam.setEcpmMonth(formatAmount(monthEcpm));
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 按日期求和
|
|
|
+ */
|
|
|
+ private BigDecimal sumByDate(List<ComprehensiveAppReport> reports,
|
|
|
+ String date,
|
|
|
+ Function<ComprehensiveAppReport, BigDecimal> extractor) {
|
|
|
+ return reports.stream()
|
|
|
+ .filter(item -> date.equals(item.getDate()))
|
|
|
+ .map(extractor)
|
|
|
+ .filter(Objects::nonNull)
|
|
|
+ .reduce(BigDecimal.ZERO, BigDecimal::add);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 按日期范围求和
|
|
|
+ */
|
|
|
+ private BigDecimal sumByDateRange(List<ComprehensiveAppReport> reports,
|
|
|
+ String startDate,
|
|
|
+ String endDate,
|
|
|
+ Function<ComprehensiveAppReport, BigDecimal> extractor) {
|
|
|
+ return reports.stream()
|
|
|
+ .filter(item -> {
|
|
|
+ String date = item.getDate();
|
|
|
+ return date != null && date.compareTo(startDate) >= 0 && date.compareTo(endDate) <= 0;
|
|
|
+ })
|
|
|
+ .map(extractor)
|
|
|
+ .filter(Objects::nonNull)
|
|
|
+ .reduce(BigDecimal.ZERO, BigDecimal::add);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 格式化金额(保留2位小数,四舍五入)
|
|
|
+ */
|
|
|
+ private String formatAmount(BigDecimal amount) {
|
|
|
+ return amount.setScale(2, RoundingMode.HALF_UP).toString();
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
/**
|
|
|
* 查询分小时报表 折线图数据
|
|
|
* 预估收益以及ecpm
|