From 0744adcfeb032b8bbf45a38a9b4141acfd3ab9c7 Mon Sep 17 00:00:00 2001 From: nili Date: Wed, 3 Jul 2024 17:34:50 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8A=A0=E7=82=B9=E6=95=B0=E6=8D=AE=E5=B1=95?= =?UTF-8?q?=E7=A4=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../dao/mapper/MatrixAdvRecordMapper.java | 8 ++-- .../group/game/service/MatrixService.java | 6 +-- .../game/service/bo/matrix/AdvTypeIncome.java | 14 ++++++ .../game/service/matrix/AdminService.java | 14 ++++++ .../service/matrix/AggregationService.java | 23 +++++++++ .../group/game/service/util/DateUtil.java | 48 +++---------------- .../game/web/rest/matrix/AdminController.java | 17 +++++++ 7 files changed, 83 insertions(+), 47 deletions(-) create mode 100644 game-service/src/main/java/awesome/group/game/service/bo/matrix/AdvTypeIncome.java diff --git a/game-dao/src/main/java/awesome/group/game/dao/mapper/MatrixAdvRecordMapper.java b/game-dao/src/main/java/awesome/group/game/dao/mapper/MatrixAdvRecordMapper.java index 7507491..126ed7d 100644 --- a/game-dao/src/main/java/awesome/group/game/dao/mapper/MatrixAdvRecordMapper.java +++ b/game-dao/src/main/java/awesome/group/game/dao/mapper/MatrixAdvRecordMapper.java @@ -9,6 +9,11 @@ import org.apache.ibatis.annotations.Select; import java.util.List; public interface MatrixAdvRecordMapper extends BaseMapper { + @Select("select id from `matrix_adv_record` where `app_id` = #{appId} and `created_at` >= #{beginTime} order by id limit 1") + Integer getBeginId(Integer appId, String beginTime); + + @Select("select adv_type, sum(ecpm) as ecpm from matrix_adv_record where `app_id` = #{appId} and id >= #{id}") + List groupByAdvType(Integer id, Integer appId); @Select("select * from matrix_app where code = #{code}") MatrixApp queryApp(String code); @@ -19,9 +24,6 @@ public interface MatrixAdvRecordMapper extends BaseMapper { @Insert("insert ignore into matrix_white_device (device_id) values (#{deviceId})") void insertWhiteList(String deviceId); - @Select("select device_id from matrix_white_device") - List whiteList(); - @Select("select device_id, device_brand, device_name, ip from matrix_adv_record where device_id in (select device_id from matrix_white_device where channel = #{channel}) limit 200") List getWhiteDevice(String channel); } diff --git a/game-service/src/main/java/awesome/group/game/service/MatrixService.java b/game-service/src/main/java/awesome/group/game/service/MatrixService.java index fdeeb6b..1e629ce 100644 --- a/game-service/src/main/java/awesome/group/game/service/MatrixService.java +++ b/game-service/src/main/java/awesome/group/game/service/MatrixService.java @@ -42,7 +42,7 @@ public class MatrixService { public void saveRecord(MatrixAdvRecordEditBo bo, String ip) { Assert.isTrue(StringUtils.hasText(bo.appCode), "appCode不能为空"); - MatrixApp app = mapper.queryApp(bo.appCode); + MatrixApp app = appMapper.queryByCode(bo.appCode); if (app == null) { return;//非法请求,直接忽略 } @@ -73,7 +73,7 @@ public class MatrixService { if (!StringUtils.hasText(bo.appCode) || !StringUtils.hasText(bo.sign)) { return; } - MatrixApp app = mapper.queryApp(bo.appCode); + MatrixApp app = appMapper.queryByCode(bo.appCode); if (app == null) { return; } @@ -87,7 +87,7 @@ public class MatrixService { } public List getAdvList(String appCode, String deviceId) { - MatrixApp app = mapper.queryApp(appCode); + MatrixApp app = appMapper.queryByCode(appCode); if (app == null) { return null; } diff --git a/game-service/src/main/java/awesome/group/game/service/bo/matrix/AdvTypeIncome.java b/game-service/src/main/java/awesome/group/game/service/bo/matrix/AdvTypeIncome.java new file mode 100644 index 0000000..1cc953c --- /dev/null +++ b/game-service/src/main/java/awesome/group/game/service/bo/matrix/AdvTypeIncome.java @@ -0,0 +1,14 @@ +package awesome.group.game.service.bo.matrix; + +public class AdvTypeIncome { + public int advType; + public long income; + + public AdvTypeIncome() { + } + + public AdvTypeIncome(int advType, long income) { + this.advType = advType; + this.income = income; + } +} diff --git a/game-service/src/main/java/awesome/group/game/service/matrix/AdminService.java b/game-service/src/main/java/awesome/group/game/service/matrix/AdminService.java index 760979e..d3bf1ca 100644 --- a/game-service/src/main/java/awesome/group/game/service/matrix/AdminService.java +++ b/game-service/src/main/java/awesome/group/game/service/matrix/AdminService.java @@ -4,6 +4,7 @@ import awesome.group.game.dao.bean.*; import awesome.group.game.dao.mapper.*; import awesome.group.game.service.bo.*; import awesome.group.game.service.bo.citrus.UserBo; +import awesome.group.game.service.bo.matrix.AdvTypeIncome; import awesome.group.game.service.bo.matrix.MatrixAppBo; import awesome.group.game.service.common.exception.PaganiException; import awesome.group.game.service.common.exception.PaganiExceptionCode; @@ -379,6 +380,19 @@ public class AdminService { mockScheduleMapper.insertBatch(list); } + + public List advTypeIncomeToday(String code) { + MatrixApp app = appMapper.queryByCode(code); + Integer appId = app.getId(); + String beginTime = DateUtil.getDayBegin(); + Integer id = advRecordMapper.getBeginId(appId, beginTime); + if (id == null) { + return null; + } + List list = advRecordMapper.groupByAdvType(id, appId); + return list.stream().map(x -> new AdvTypeIncome(x.getAdvType(), x.getEcpm())).toList(); + } + public OverviewBo incomeOverview(int adminId, String code) { if (StringUtils.hasText(code)) { MatrixApp app = appMapper.queryByCode(code); diff --git a/game-service/src/main/java/awesome/group/game/service/matrix/AggregationService.java b/game-service/src/main/java/awesome/group/game/service/matrix/AggregationService.java index edec320..9d50bef 100644 --- a/game-service/src/main/java/awesome/group/game/service/matrix/AggregationService.java +++ b/game-service/src/main/java/awesome/group/game/service/matrix/AggregationService.java @@ -1,7 +1,10 @@ package awesome.group.game.service.matrix; import awesome.group.game.dao.bean.MatrixAdvAggregation; +import awesome.group.game.dao.bean.MatrixApp; import awesome.group.game.dao.mapper.MatrixAdvAggregationMapper; +import awesome.group.game.dao.mapper.MatrixAppMapper; +import awesome.group.game.service.bo.matrix.AdvTypeIncome; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import jakarta.annotation.Resource; @@ -15,6 +18,9 @@ public class AggregationService { @Resource private MatrixAdvAggregationMapper aggregationMapper; + @Resource + private MatrixAppMapper appMapper; + public long getIncome(List appIds, int startDate, int endDate) { if (CollectionUtils.isEmpty(appIds)) { return 0L; @@ -29,4 +35,21 @@ public class AggregationService { } return data.getIncome(); } + + public List advTypeIncomeHistory(String code, Integer beginDate, Integer endDate) { + MatrixApp app = appMapper.queryByCode(code); + Integer appId = app.getId(); + LambdaQueryWrapper query = new QueryWrapper() + .select("adv_type, sum(income) as income").lambda(); + query.eq(MatrixAdvAggregation::getAppId, appId); + if (beginDate != null) { + query.ge(MatrixAdvAggregation::getDate, beginDate); + } + if (endDate != null) { + query.le(MatrixAdvAggregation::getDate, endDate); + } + query.groupBy(MatrixAdvAggregation::getAdvType); + List list = aggregationMapper.selectList(query); + return list.stream().map(x -> new AdvTypeIncome(x.getAdvType(), x.getIncome())).toList(); + } } 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 acb2ac2..c3eaa44 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 @@ -36,6 +36,13 @@ public class DateUtil { return x.getTimeInMillis(); } + public static String getDayBegin() { + LocalDateTime today0 = LocalDate.now().atStartOfDay(); + + // 格式化输出 + DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); + return today0.format(formatter); + } public static LocalDate long2Date(long timestamp) { return LocalDateTime.ofInstant(Instant.ofEpochMilli(timestamp), @@ -73,52 +80,11 @@ public class DateUtil { return Integer.parseInt(formatter.format(date)); } - public static Integer date2Number(Date date) { - SimpleDateFormat sdf = getSdf(); - return Integer.parseInt(sdf.format(date)); - } - public static Integer datePlus(Integer date, int plus) { LocalDate localDate = number2Date(date); return date2Number(localDate.plusDays(plus)); } - public static List dateRange(Integer startDate, Integer endDate) { - LocalDate begin = number2Date(startDate); - LocalDate end = number2Date(endDate); - List res = new ArrayList<>(); - while (!begin.isAfter(end)) { - res.add(date2Number(begin)); - begin = begin.plusDays(1); - } - return res; - } - - public static List latestDateRange(int minus, int plus) { - LocalDate todayDate = LocalDate.now(); - List res = new ArrayList<>(); - for (int i = 0; i < minus; i++) { - LocalDate minusDate = todayDate.minusDays(minus - i); - res.add(date2Number(minusDate)); - } - res.add(date2Number(todayDate)); - for (int i = 1; i <= plus; i++) { - LocalDate plusDate = todayDate.plusDays(i); - res.add(date2Number(plusDate)); - } - return res; - } - - public static List latestDate(int dayCnt) { - return latestDateRange(dayCnt, dayCnt); - } - - public static int dayCntBetween(int date1, int date2) { - LocalDate localDate1 = number2Date(date1); - LocalDate localDate2 = number2Date(date2); - return (int) (localDate2.toEpochDay() - localDate1.toEpochDay()); - } - public static long dayCntBetween(long timestamp1, long timestamp2) { return 1 + (timestamp2 - timestamp1) / 86400_000; } 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 116d36f..16bbfaa 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 @@ -7,9 +7,11 @@ import awesome.group.game.dao.bean.MatrixWhiteDevice; import awesome.group.game.service.MatrixService; import awesome.group.game.service.bo.*; import awesome.group.game.service.bo.citrus.UserBo; +import awesome.group.game.service.bo.matrix.AdvTypeIncome; import awesome.group.game.service.bo.matrix.MatrixAppBo; import awesome.group.game.service.common.response.R; import awesome.group.game.service.matrix.AdminService; +import awesome.group.game.service.matrix.AggregationService; import awesome.group.game.service.matrix.OSSService; import awesome.group.game.service.util.JwtUtils; import awesome.group.game.web.RequestContext; @@ -36,6 +38,9 @@ public class AdminController { @Resource private OSSService ossService; + @Resource + private AggregationService aggregationService; + @PostMapping("/login") @RestApi public R adminLogin(@RequestParam String name, @RequestParam String pwd) { @@ -119,6 +124,18 @@ public class AdminController { return new R<>(R.CODE_SUCCESS, "ok", adminService.incomeDaily(query, RequestContext.getAdminID())); } + @GetMapping("/advTypeIncomeHistory") + @RestApi + public R> advTypeIncomeHistory(@RequestParam String code, @RequestParam(required = false) Integer beginDate, @RequestParam(required = false) Integer endDate) { + return new R<>(R.CODE_SUCCESS, "ok", aggregationService.advTypeIncomeHistory(code, beginDate, endDate)); + } + + @GetMapping("/advTypeIncomeToday") + @RestApi + public R> advTypeIncomeToday(@RequestParam String code) { + return new R<>(R.CODE_SUCCESS, "ok", adminService.advTypeIncomeToday(code)); + } + @PostMapping("/saveApp") @RestApi public R saveApp(@RequestBody MatrixApp matrixApp) {