From 49950a154a7d5021f6bc187d4b2d32f5ee77a1b5 Mon Sep 17 00:00:00 2001 From: nili Date: Wed, 3 Apr 2024 14:35:24 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E4=B8=8B=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../group/game/service/AdminService.java | 93 +++++++++++++------ .../group/game/service/bo/DateIncome.java | 6 ++ .../group/game/service/bo/IncomeQuery.java | 10 ++ .../group/game/service/bo/OverviewBo.java | 8 -- .../group/game/service/util/DateUtil.java | 4 + .../game/web/rest/matrix/AdminController.java | 10 +- 6 files changed, 92 insertions(+), 39 deletions(-) create mode 100644 game-service/src/main/java/awesome/group/game/service/bo/DateIncome.java create mode 100644 game-service/src/main/java/awesome/group/game/service/bo/IncomeQuery.java diff --git a/game-service/src/main/java/awesome/group/game/service/AdminService.java b/game-service/src/main/java/awesome/group/game/service/AdminService.java index 5fb5ee3..aab7b31 100644 --- a/game-service/src/main/java/awesome/group/game/service/AdminService.java +++ b/game-service/src/main/java/awesome/group/game/service/AdminService.java @@ -175,50 +175,85 @@ public class AdminService { } } - public OverviewBo incomeOverview(int adminId) { + public OverviewBo incomeOverview(int adminId, String code) { + if (StringUtils.hasText(code)) { + MatrixApp app = appMapper.queryByCode(code); + if (app == null) { + return new OverviewBo(); + } + return incomeOverviewByAppIds(Collections.singletonList(app.getId())); + } MatrixAdmin admin = adminMapper.selectById(adminId); - List appIds = null; + if (admin.getRole() == SUPER_ADMIN) { + List appIds = appMapper.selectList(null).stream().map(MatrixApp::getId).toList(); + return incomeOverviewByAppIds(appIds); + } if (StringUtils.hasText(admin.getAppIds())) { - appIds = Arrays.stream(admin.getAppIds().split(",")).map(Integer::parseInt).toList(); - } else if (admin.getRole() != SUPER_ADMIN) { - return new OverviewBo(); + List appIds = Arrays.stream(admin.getAppIds().split(",")).map(Integer::parseInt).toList(); + return incomeOverviewByAppIds(appIds); } + return new OverviewBo(); + } + private OverviewBo incomeOverviewByAppIds(List appIds) { + if (CollectionUtils.isEmpty(appIds)) { + return new OverviewBo(); + } LambdaQueryWrapper advQuery = new QueryWrapper() .select("sum(ecpm) as ecpm").lambda(); - if (!CollectionUtils.isEmpty(appIds)) { - advQuery.in(MatrixAdvRecord::getAppId, appIds); - } + advQuery.in(MatrixAdvRecord::getAppId, appIds); advQuery.gt(MatrixAdvRecord::getCreatedAt, new Timestamp(DateUtil.getDayBeginTimestamp(System.currentTimeMillis()))); MatrixAdvRecord record = advRecordMapper.selectOne(advQuery); OverviewBo bo = new OverviewBo(); bo.todayIncome = record == null ? 0 : record.getEcpm(); - LambdaQueryWrapper aggregationQuery = new QueryWrapper() + LambdaQueryWrapper appQuery = new QueryWrapper().select("sum(income) as income").lambda(); + appQuery.in(MatrixApp::getId, appIds); + bo.totalIncome = appMapper.selectOne(appQuery).getIncome() + bo.todayIncome; + return bo; + } + + public List incomeDaily(IncomeQuery query, int adminId) { + LambdaQueryWrapper wrapper = new QueryWrapper() .select("date, sum(income) as income") .lambda(); - if (!CollectionUtils.isEmpty(appIds)) { - aggregationQuery.in(MatrixAdvAggregation::getAppId, appIds); + if (StringUtils.hasText(query.code)) { + MatrixApp app = appMapper.queryByCode(query.code); + if (app == null) { + return new ArrayList<>(); + } + wrapper.eq(MatrixAdvAggregation::getAppId, app.getId()); } - int beginDate = DateUtil.datePlus(DateUtil.currentDate(), -10); - aggregationQuery.gt(MatrixAdvAggregation::getDate, beginDate); - aggregationQuery.groupBy(MatrixAdvAggregation::getDate); - List prev30Days = aggregationMapper.selectList(aggregationQuery); - Map dateIncomeMap = prev30Days.stream().collect(Collectors.toMap(MatrixAdvAggregation::getDate, MatrixAdvAggregation::getIncome)); - bo.prev30DaysIncome = DateUtil.dateRange(beginDate, DateUtil.currentDate()).stream().map(x -> { - OverviewBo.DateIncome dateIncome = new OverviewBo.DateIncome(); - dateIncome.date = x; - dateIncome.income = dateIncomeMap.getOrDefault(x, 0L); - return dateIncome; - }).toList(); - bo.prev30DaysIncome.get(bo.prev30DaysIncome.size() - 1).income = bo.todayIncome; - - LambdaQueryWrapper appQuery = new QueryWrapper().select("sum(income) as income").lambda(); - if (!CollectionUtils.isEmpty(appIds)) { - appQuery.in(MatrixApp::getId, appIds); + if (query.platform != null) { + wrapper.eq(MatrixAdvAggregation::getPlatform, query.platform); } - bo.totalIncome = appMapper.selectOne(appQuery).getIncome(); - return bo; + if (query.advType != null) { + wrapper.eq(MatrixAdvAggregation::getAdvType, query.advType); + } + int end = DateUtil.datePlus(DateUtil.currentDate(), -1), start = DateUtil.datePlus(end, -30); + if (!CollectionUtils.isEmpty(query.date)) { + SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); + try { + Date beginDate = format.parse(query.date.get(0)), endDate = format.parse(query.date.get(1)); + start = DateUtil.long2Number(beginDate.getTime()); + end = DateUtil.long2Number(endDate.getTime()); + } catch (ParseException e) { + throw new PaganiException(PaganiExceptionCode.ILLEGAL_REQUEST, "时间范围有误"); + } + } + wrapper.between(MatrixAdvAggregation::getDate, start, end); + wrapper.groupBy(MatrixAdvAggregation::getDate); + wrapper.orderByAsc(MatrixAdvAggregation::getDate); + List data = aggregationMapper.selectList(wrapper); + Map map = data.stream().collect(Collectors.toMap(MatrixAdvAggregation::getDate, MatrixAdvAggregation::getIncome)); + List res = new ArrayList<>(); + for (int d = start; d <= end; d = DateUtil.datePlus(d, 1)) { + DateIncome income = new DateIncome(); + income.date = d; + income.income = map.getOrDefault(d, 0L); + res.add(income); + } + return res; } @Scheduled(cron = "0 0 1 * * ?") diff --git a/game-service/src/main/java/awesome/group/game/service/bo/DateIncome.java b/game-service/src/main/java/awesome/group/game/service/bo/DateIncome.java new file mode 100644 index 0000000..6b21856 --- /dev/null +++ b/game-service/src/main/java/awesome/group/game/service/bo/DateIncome.java @@ -0,0 +1,6 @@ +package awesome.group.game.service.bo; + +public class DateIncome { + public int date; + public long income; +} diff --git a/game-service/src/main/java/awesome/group/game/service/bo/IncomeQuery.java b/game-service/src/main/java/awesome/group/game/service/bo/IncomeQuery.java new file mode 100644 index 0000000..9b7cc27 --- /dev/null +++ b/game-service/src/main/java/awesome/group/game/service/bo/IncomeQuery.java @@ -0,0 +1,10 @@ +package awesome.group.game.service.bo; + +import java.util.List; + +public class IncomeQuery { + public String code; + public Integer platform; + public Integer advType; + public List date; +} diff --git a/game-service/src/main/java/awesome/group/game/service/bo/OverviewBo.java b/game-service/src/main/java/awesome/group/game/service/bo/OverviewBo.java index f5a2b7c..0370cc9 100644 --- a/game-service/src/main/java/awesome/group/game/service/bo/OverviewBo.java +++ b/game-service/src/main/java/awesome/group/game/service/bo/OverviewBo.java @@ -1,14 +1,6 @@ package awesome.group.game.service.bo; -import java.util.List; - public class OverviewBo { public long todayIncome; public long totalIncome; - public List prev30DaysIncome; - - public static class DateIncome { - public int date; - public long income; - } } diff --git a/game-service/src/main/java/awesome/group/game/service/util/DateUtil.java b/game-service/src/main/java/awesome/group/game/service/util/DateUtil.java index dc2d08a..f5fd8fd 100644 --- a/game-service/src/main/java/awesome/group/game/service/util/DateUtil.java +++ b/game-service/src/main/java/awesome/group/game/service/util/DateUtil.java @@ -42,6 +42,10 @@ public class DateUtil { TimeZone.getDefault().toZoneId()).toLocalDate(); } + public static int long2Number(long timestamp) { + return date2Number(long2Date(timestamp)); + } + public static long date2Long(int date) { LocalDate localDate = number2Date(date); return localDate.atStartOfDay().toInstant(ZONE_OFFSET).toEpochMilli(); diff --git a/game-web/src/main/java/awesome/group/game/web/rest/matrix/AdminController.java b/game-web/src/main/java/awesome/group/game/web/rest/matrix/AdminController.java index d7925ad..dcd3dfb 100644 --- a/game-web/src/main/java/awesome/group/game/web/rest/matrix/AdminController.java +++ b/game-web/src/main/java/awesome/group/game/web/rest/matrix/AdminController.java @@ -76,7 +76,13 @@ public class AdminController { @GetMapping("/incomeOverview") @RestApi - public R incomeOverview() { - return new R<>(R.CODE_SUCCESS, "ok", adminService.incomeOverview(RequestContext.getAdminID())); + public R incomeOverview(@RequestParam(required = false) String appCode) { + return new R<>(R.CODE_SUCCESS, "ok", adminService.incomeOverview(RequestContext.getAdminID(), appCode)); + } + + @PostMapping("/incomeDaily") + @RestApi + public R> incomeDaily(@RequestBody IncomeQuery query) { + return new R<>(R.CODE_SUCCESS, "ok", adminService.incomeDaily(query, RequestContext.getAdminID())); } }