Browse Source

改下接口

master
nili 9 months ago
parent
commit
49950a154a
  1. 93
      game-service/src/main/java/awesome/group/game/service/AdminService.java
  2. 6
      game-service/src/main/java/awesome/group/game/service/bo/DateIncome.java
  3. 10
      game-service/src/main/java/awesome/group/game/service/bo/IncomeQuery.java
  4. 8
      game-service/src/main/java/awesome/group/game/service/bo/OverviewBo.java
  5. 4
      game-service/src/main/java/awesome/group/game/service/util/DateUtil.java
  6. 10
      game-web/src/main/java/awesome/group/game/web/rest/matrix/AdminController.java

93
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<Integer> appIds = null;
if (admin.getRole() == SUPER_ADMIN) {
List<Integer> 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<Integer> appIds = Arrays.stream(admin.getAppIds().split(",")).map(Integer::parseInt).toList();
return incomeOverviewByAppIds(appIds);
}
return new OverviewBo();
}
private OverviewBo incomeOverviewByAppIds(List<Integer> appIds) {
if (CollectionUtils.isEmpty(appIds)) {
return new OverviewBo();
}
LambdaQueryWrapper<MatrixAdvRecord> advQuery = new QueryWrapper<MatrixAdvRecord>()
.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<MatrixAdvAggregation> aggregationQuery = new QueryWrapper<MatrixAdvAggregation>()
LambdaQueryWrapper<MatrixApp> appQuery = new QueryWrapper<MatrixApp>().select("sum(income) as income").lambda();
appQuery.in(MatrixApp::getId, appIds);
bo.totalIncome = appMapper.selectOne(appQuery).getIncome() + bo.todayIncome;
return bo;
}
public List<DateIncome> incomeDaily(IncomeQuery query, int adminId) {
LambdaQueryWrapper<MatrixAdvAggregation> wrapper = new QueryWrapper<MatrixAdvAggregation>()
.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<MatrixAdvAggregation> prev30Days = aggregationMapper.selectList(aggregationQuery);
Map<Integer, Long> 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<MatrixApp> appQuery = new QueryWrapper<MatrixApp>().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<MatrixAdvAggregation> data = aggregationMapper.selectList(wrapper);
Map<Integer, Long> map = data.stream().collect(Collectors.toMap(MatrixAdvAggregation::getDate, MatrixAdvAggregation::getIncome));
List<DateIncome> 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 * * ?")

6
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;
}

10
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<String> date;
}

8
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<DateIncome> prev30DaysIncome;
public static class DateIncome {
public int date;
public long income;
}
}

4
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();

10
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<OverviewBo> incomeOverview() {
return new R<>(R.CODE_SUCCESS, "ok", adminService.incomeOverview(RequestContext.getAdminID()));
public R<OverviewBo> incomeOverview(@RequestParam(required = false) String appCode) {
return new R<>(R.CODE_SUCCESS, "ok", adminService.incomeOverview(RequestContext.getAdminID(), appCode));
}
@PostMapping("/incomeDaily")
@RestApi
public R<List<DateIncome>> incomeDaily(@RequestBody IncomeQuery query) {
return new R<>(R.CODE_SUCCESS, "ok", adminService.incomeDaily(query, RequestContext.getAdminID()));
}
}

Loading…
Cancel
Save