|
|
@ -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 * * ?") |
|
|
|