|
|
@@ -1,39 +1,21 @@
|
|
|
package com.ytpm.controller;
|
|
|
|
|
|
import cn.hutool.core.collection.CollUtil;
|
|
|
-import com.alibaba.fastjson.JSON;
|
|
|
-import com.ytpm.advertise.enums.AdPlatformTypeEnum;
|
|
|
-import com.ytpm.advertise.param.ComprehensiveReportParam;
|
|
|
-import com.ytpm.advertise.param.ConcurrentHourReportsParams;
|
|
|
-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.model.YtApp;
|
|
|
-import com.ytpm.agent.param.AppListParam;
|
|
|
-import com.ytpm.agent.param.DateRangeParams;
|
|
|
-import com.ytpm.agent.param.IndexResParam;
|
|
|
-import com.ytpm.agent.view.AgentAppView;
|
|
|
-import com.ytpm.agent.view.AgentChannelView;
|
|
|
+import com.ytpm.agent.view.AgentAdGroupStaticsVO;
|
|
|
import com.ytpm.agent.view.AgentTopCountView;
|
|
|
import com.ytpm.agent.view.AgentUserInfo;
|
|
|
-import com.ytpm.agent.view.IndexHourReportView;
|
|
|
import com.ytpm.app.param.AppQueryUserTodayTimeParam;
|
|
|
import com.ytpm.app.param.AppUserQueryParam;
|
|
|
import com.ytpm.dao.AppMapper;
|
|
|
-import com.ytpm.dao.ChannelMapper;
|
|
|
-import com.ytpm.feign.AdvertiseFeign;
|
|
|
import com.ytpm.feign.AppFeign;
|
|
|
import com.ytpm.feign.RiskFeign;
|
|
|
import com.ytpm.general.Result;
|
|
|
import com.ytpm.service.AgentAppService;
|
|
|
import com.ytpm.util.DateUtil;
|
|
|
-import com.ytpm.util.RedisService;
|
|
|
-import com.ytpm.utils.GetTakuInfoUtil;
|
|
|
import io.swagger.annotations.Api;
|
|
|
import io.swagger.annotations.ApiOperation;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
-import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.security.core.annotation.AuthenticationPrincipal;
|
|
|
import org.springframework.web.bind.annotation.GetMapping;
|
|
|
import org.springframework.web.bind.annotation.PostMapping;
|
|
|
@@ -42,20 +24,10 @@ import org.springframework.web.bind.annotation.RestController;
|
|
|
import springfox.documentation.annotations.ApiIgnore;
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
-import java.math.BigDecimal;
|
|
|
-import java.math.RoundingMode;
|
|
|
-import java.time.LocalDate;
|
|
|
-import java.time.format.DateTimeFormatter;
|
|
|
-import java.time.temporal.TemporalAdjusters;
|
|
|
-import java.util.ArrayList;
|
|
|
-import java.util.Arrays;
|
|
|
-import java.util.Collections;
|
|
|
import java.util.Date;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
-import java.util.Objects;
|
|
|
import java.util.concurrent.ConcurrentHashMap;
|
|
|
-import java.util.function.Function;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
@Api(tags = "首页数据统计模块")
|
|
|
@@ -64,26 +36,15 @@ import java.util.stream.Collectors;
|
|
|
@RequestMapping("/index")
|
|
|
public class AgentIndexController {
|
|
|
|
|
|
- @Resource
|
|
|
- private AdvertiseFeign advertiseFeign;
|
|
|
-
|
|
|
- @Resource
|
|
|
- private ChannelMapper channelMapper;
|
|
|
-
|
|
|
@Resource
|
|
|
private AppFeign appFeign;
|
|
|
|
|
|
@Resource
|
|
|
private RiskFeign riskFeign;
|
|
|
|
|
|
- @Autowired
|
|
|
- private RedisService redisService;
|
|
|
-
|
|
|
@Resource
|
|
|
private AgentAppService agentAppService;
|
|
|
|
|
|
- @Resource
|
|
|
- private GetTakuInfoUtil getTakuInfoUtil;
|
|
|
@Resource
|
|
|
private AppMapper appMapper;
|
|
|
|
|
|
@@ -110,302 +71,17 @@ public class AgentIndexController {
|
|
|
*/
|
|
|
@ApiOperation("查询广告平台收益")
|
|
|
@PostMapping("/profit")
|
|
|
- public Result<?> profit(@ApiIgnore @AuthenticationPrincipal AgentUserInfo userInfo) {
|
|
|
- // 1. 准备日期参数
|
|
|
- DateRangeParams dateParams = prepareDateParams();
|
|
|
-
|
|
|
- // 2. 构建查询参数
|
|
|
- ComprehensiveReportParam reportParam = buildReportParam(dateParams, userInfo);
|
|
|
-
|
|
|
- // 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");
|
|
|
-
|
|
|
- return new DateRangeParams(
|
|
|
- today.format(formatter),
|
|
|
- today.minusDays(1).format(formatter),
|
|
|
- today.with(TemporalAdjusters.firstDayOfMonth()).format(formatter),
|
|
|
- today.with(TemporalAdjusters.lastDayOfMonth()).format(formatter)
|
|
|
- );
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 构建报表查询参数
|
|
|
- */
|
|
|
- private ComprehensiveReportParam buildReportParam(DateRangeParams dateParams, AgentUserInfo userInfo) {
|
|
|
- ComprehensiveReportParam param = new ComprehensiveReportParam();
|
|
|
-
|
|
|
- // 设置日期范围
|
|
|
- param.setStartdate(Integer.parseInt(dateParams.getMonthStart()));
|
|
|
- param.setEnddate(Integer.parseInt(dateParams.getMonthEnd()));
|
|
|
-
|
|
|
- // 添加所有平台ID
|
|
|
- param.setNetwork_firm_id_list(Arrays.stream(AdPlatformTypeEnum.values())
|
|
|
- .map(AdPlatformTypeEnum::getCode)
|
|
|
- .collect(Collectors.toList()));
|
|
|
- AppListParam appListParam = new AppListParam();
|
|
|
- appListParam.setUserId(userInfo.getUserId());
|
|
|
- //设置takuPublicKey
|
|
|
- String takuPublicKey = getTakuInfoUtil.getTakuPublicKey(userInfo.getUserId());
|
|
|
- param.setTakuPublicKey(takuPublicKey);
|
|
|
- //增加appId查询
|
|
|
- List<AgentAppView> agentAppViews = agentAppService.searchAppIdList(appListParam);
|
|
|
- ArrayList<String> list = new ArrayList<>();
|
|
|
- if (CollUtil.isNotEmpty(agentAppViews)) {
|
|
|
- //获取的是superiorId
|
|
|
- param.setApp_id_list(agentAppViews.stream().map(AgentAppView::getSuperiorId).collect(Collectors.toList()));
|
|
|
- }
|
|
|
- else {
|
|
|
- param.setApp_id_list(list);
|
|
|
- }
|
|
|
- // 设置分组
|
|
|
- param.setGroup_by(Arrays.asList("date", "network_firm_id","app"));
|
|
|
-
|
|
|
- return param;
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 获取报表数据
|
|
|
- */
|
|
|
- private List<ComprehensiveAppReport> fetchReportData(ComprehensiveReportParam param) {
|
|
|
- long startTime = System.currentTimeMillis();
|
|
|
- List<ComprehensiveAppReport> appReport = new ArrayList<>();
|
|
|
- if (CollUtil.isNotEmpty(param.getApp_id_list())){
|
|
|
- appReport=advertiseFeign.getAppReport(param);
|
|
|
- log.info("调用API接口耗时: {}ms", System.currentTimeMillis() - startTime);
|
|
|
- }
|
|
|
- //appIdList为空 返回空数据
|
|
|
- return appReport;
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 处理平台报告
|
|
|
- */
|
|
|
- private List<IndexResParam> processPlatformReports(List<ComprehensiveAppReport> appReport,
|
|
|
- DateRangeParams dateParams) {
|
|
|
- long startTime = System.currentTimeMillis();
|
|
|
-
|
|
|
- List<IndexResParam> result = Arrays.stream(AdPlatformTypeEnum.values())
|
|
|
- .map(platform -> buildPlatformReport(platform, appReport, dateParams))
|
|
|
- .collect(Collectors.toList());
|
|
|
-
|
|
|
- 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
|
|
|
- */
|
|
|
- @ApiOperation("查询分小时报表")
|
|
|
- @PostMapping("/hourReport")
|
|
|
- public Result<?> hourReport(@ApiIgnore @AuthenticationPrincipal AgentUserInfo userInfo) {
|
|
|
- if (redisService.hasKey("hourReport"+userInfo.getUserId())) {
|
|
|
- List<IndexHourReportView> hourReportResList = JSON.parseArray(redisService.getStr("hourReport"+userInfo.getUserId()), IndexHourReportView.class);
|
|
|
- return Result.resultObjOk(hourReportResList);
|
|
|
- } else {
|
|
|
- // 1. 准备日期参数
|
|
|
- LocalDate today = LocalDate.now();
|
|
|
- DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
|
|
|
-
|
|
|
- // 今日和昨日日期
|
|
|
- String todayStr = today.format(formatter);
|
|
|
- Integer todayInt = Integer.valueOf(todayStr);
|
|
|
- List<AgentChannelView> agentChannelViews = channelMapper.channelList();
|
|
|
- ArrayList<IndexHourReportView> indexHourReportViews = new ArrayList<>();
|
|
|
- ArrayList<HourReportParam> hourReportParams = new ArrayList<>();
|
|
|
- ConcurrentHourReportsParams concurrentHourReportsParams = new ConcurrentHourReportsParams();
|
|
|
- AppListParam appListParam = new AppListParam();
|
|
|
- appListParam.setUserId(userInfo.getUserId());
|
|
|
- List<AgentAppView> agentAppViews = agentAppService.searchAppIdList(appListParam);
|
|
|
- List<String> appIdList;
|
|
|
- if (CollUtil.isNotEmpty(agentAppViews)){
|
|
|
- appIdList = agentAppViews.stream().map(AgentAppView::getSuperiorId).collect(Collectors.toList());
|
|
|
- }
|
|
|
- else {
|
|
|
- return Result.resultObjOk(indexHourReportViews);
|
|
|
- }
|
|
|
- //查询takuPublicKey
|
|
|
- String takuPublicKey = getTakuInfoUtil.getTakuPublicKey(userInfo.getUserId());
|
|
|
- for (AgentChannelView agentChannelView : agentChannelViews) {
|
|
|
- HourReportParam param = new HourReportParam();
|
|
|
- Integer networkId = agentChannelView.getNetworkId();
|
|
|
- param.setNetwork_id_list(Collections.singletonList(networkId));
|
|
|
- param.setStart_date(todayInt);
|
|
|
- param.setEnd_date(todayInt);
|
|
|
- param.setCurrency("CNY");
|
|
|
- param.setLimit(1000);
|
|
|
- param.setStart(0);
|
|
|
- param.setTime_zone("UTC+8");
|
|
|
- param.setChannelName(agentChannelView.getChannelName());
|
|
|
- param.setApp_id_list(appIdList);
|
|
|
- //增加taku公钥
|
|
|
- param.setTakuPublicKey(takuPublicKey);
|
|
|
- hourReportParams.add(param);
|
|
|
- }
|
|
|
- concurrentHourReportsParams.setParam(hourReportParams);
|
|
|
- //进行远程调用获取数据
|
|
|
- Result<List<HourReportRes>> listResult = advertiseFeign.concurrentHourReports(concurrentHourReportsParams);
|
|
|
- List<HourReportRes> hourReportResList = listResult.getData();
|
|
|
- for (HourReportRes hourReportRes : hourReportResList) {
|
|
|
- //处理数据
|
|
|
- IndexHourReportView indexHourReportView = buildIndexHourReportView(hourReportRes);
|
|
|
- //加入数组中
|
|
|
- indexHourReportViews.add(indexHourReportView);
|
|
|
- }
|
|
|
- //50分钟过期
|
|
|
- redisService.setTimeOutStr("hourReport"+userInfo.getUserId(), JSON.toJSONString(indexHourReportViews), 50 * 60 * 1000);
|
|
|
- return Result.resultObjOk(indexHourReportViews);
|
|
|
- }
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 对返回的小时报表数据进行处理
|
|
|
- *
|
|
|
- * @param hourReportRes 小时数据
|
|
|
- * @return
|
|
|
- */
|
|
|
- private IndexHourReportView buildIndexHourReportView(HourReportRes hourReportRes) {
|
|
|
- IndexHourReportView indexHourReportView = new IndexHourReportView();
|
|
|
- indexHourReportView.setChannelName(hourReportRes.getChannelName());
|
|
|
- List<BigDecimal> hourlyData = new ArrayList<>(Collections.nCopies(24, BigDecimal.ZERO));
|
|
|
- List<BigDecimal> ecpmData = new ArrayList<>(Collections.nCopies(24, BigDecimal.ZERO));
|
|
|
- if (CollUtil.isNotEmpty(hourReportRes.getList())) {
|
|
|
- for (HourReportView hourReport : hourReportRes.getList()) {
|
|
|
- int hour = hourReport.getHour();
|
|
|
- if (hour >= 0 && hour < 24) {
|
|
|
- BigDecimal revenue = hourReport.getEstimated_revenue();
|
|
|
- BigDecimal ecpm = hourReport.getEstimated_revenue_ecpm();
|
|
|
-
|
|
|
- hourlyData.set(hour, revenue != null ? revenue : BigDecimal.ZERO);
|
|
|
- ecpmData.set(hour, ecpm != null ? ecpm : BigDecimal.ZERO);
|
|
|
- }
|
|
|
- }
|
|
|
+ public Result<List<AgentAdGroupStaticsVO>> profit(@ApiIgnore @AuthenticationPrincipal AgentUserInfo userInfo) {
|
|
|
+ //查询代理商拥有的app
|
|
|
+ List<YtApp> ytApps = appMapper.queryAll(userInfo.getUserId());
|
|
|
+ if(CollUtil.isEmpty(ytApps)){
|
|
|
+ return Result.resultErr("未找到您的应用,请在应用管理新增");
|
|
|
}
|
|
|
- indexHourReportView.setEstimatedRevenueList(hourlyData);
|
|
|
- indexHourReportView.setEstimatedRevenueEcpmList(ecpmData);
|
|
|
- return indexHourReportView;
|
|
|
+ String appIds = ytApps.stream().map(YtApp::getAppId).collect(Collectors.joining(","));
|
|
|
+ //分广告平台,分别统计用户当天、昨日、本月的数据封装并返回
|
|
|
+ return Result.resultObjOk(appFeign.getAgentProfit(appIds));
|
|
|
}
|
|
|
|
|
|
-
|
|
|
@ApiOperation("查询用户行为数据统计")
|
|
|
@PostMapping("/userStatistic")
|
|
|
public Result<Map<String, Object>> getDashboardData(@ApiIgnore @AuthenticationPrincipal AgentUserInfo userInfo) throws InterruptedException {
|