@ -51,7 +51,8 @@ public class WorkbenchServiceImpl implements WorkbenchService {
}
}
private static final String CACHE_KEY = "workbench_card_cache" ;
private static final String CACHE_KEY = "workbench_card_cache" ;
@Override
@Override
public WorkbenchCard getCard ( ) {
public WorkbenchCard getCard ( List < String > markets ) {
Date date = new Date ( ) ;
/ / 获取开始时间和结束时间 ( 当天 )
/ / 获取开始时间和结束时间 ( 当天 )
LocalDateTime startOfDay = LocalDateTime . now ( ) . with ( LocalTime . MIN ) ;
LocalDateTime startOfDay = LocalDateTime . now ( ) . with ( LocalTime . MIN ) ;
LocalDateTime endOfDay = startOfDay . plusDays ( 1 ) . minusSeconds ( 1 ) ;
LocalDateTime endOfDay = startOfDay . plusDays ( 1 ) . minusSeconds ( 1 ) ;
@ -62,7 +63,7 @@ public class WorkbenchServiceImpl implements WorkbenchService {
/ / 获取当前年份的第一天
/ / 获取当前年份的第一天
LocalDate firstDayOfYear = LocalDate . now ( ) . withDayOfYear ( 1 ) ;
LocalDate firstDayOfYear = LocalDate . now ( ) . withDayOfYear ( 1 ) ;
Date yearlyStartDate = Date . from ( firstDayOfYear . atStartOfDay ( ZoneId . systemDefault ( ) ) . toInstant ( ) ) ;
Date yearlyStartDate = Date . from ( firstDayOfYear . atStartOfDay ( ZoneId . systemDefault ( ) ) . toInstant ( ) ) ;
List < String > markets = generalService . getMarket ( ) ;
/ / markets = generalService . getMarket ( ) ;
/ / 批量获取统计数据
/ / 批量获取统计数据
List < Statistics > currentStatsList = statisticsMapper . selectByMarketsAndDate ( markets ,
List < Statistics > currentStatsList = statisticsMapper . selectByMarketsAndDate ( markets ,
@ -89,20 +90,27 @@ public class WorkbenchServiceImpl implements WorkbenchService {
new Date ( ) ) )
new Date ( ) ) )
. collect ( Collectors . toList ( ) ) ;
. collect ( Collectors . toList ( ) ) ;
Integer sumWow = calculateAllWeekOverWeek ( date ) ;
Integer sumDaily = calculateAllDayOverDay ( date ) ;
return new WorkbenchCard ( marketCards , new ArrayList < > ( ) , markets , new Date ( ) , new Date ( ) ) ;
return new WorkbenchCard ( marketCards , new ArrayList < > ( ) , markets , new Date ( ) , new Date ( ) , sumWow , sumDaily ) ;
}
}
@Override
@Override
public WorkbenchCard getCardCache ( ) {
public WorkbenchCard getCardCache ( List < String > markets ) {
markets = generalService . getMarket ( ) ;
/ / 从缓存中获取工作台数据
WorkbenchCard cached = redisTemplate . opsForValue ( ) . get ( CACHE_KEY ) ;
WorkbenchCard cached = redisTemplate . opsForValue ( ) . get ( CACHE_KEY ) ;
if ( cached ! = null ) {
if ( cached ! = null ) {
/ / 直接返回缓存数据
System . out . println ( "读取缓存数据: " + new Date ( ) ) ;
System . out . println ( "读取缓存数据: " + new Date ( ) ) ;
return cached ;
return cached ;
}
}
try {
try {
WorkbenchCard freshData = getCard ( ) ;
/ / 获取新的工作台数据
WorkbenchCard freshData = getCard ( markets ) ;
/ / 存入缓存 , 一小时后过期 ( 与统计表更新时间对应 )
redisTemplate . opsForValue ( ) . set ( CACHE_KEY , freshData , 1 , java . util . concurrent . TimeUnit . HOURS ) ;
redisTemplate . opsForValue ( ) . set ( CACHE_KEY , freshData , 1 , java . util . concurrent . TimeUnit . HOURS ) ;
System . out . println ( "刷新缓存并存储新数据: " + new Date ( ) ) ;
System . out . println ( "刷新缓存并存储新数据: " + new Date ( ) ) ;
return freshData ;
return freshData ;
@ -159,9 +167,8 @@ public class WorkbenchServiceImpl implements WorkbenchService {
card . setYearlyRechargeNum ( currentStatistics . getYearlyRechargeNum ( ) ) ;
card . setYearlyRechargeNum ( currentStatistics . getYearlyRechargeNum ( ) ) ;
/ / 周环比 、 日同比
/ / 周环比 、 日同比
card . setWow ( calculateWeekOverWeek ( market , currentDate ) ) ;
card . setWow ( calculateWeekOverWeek ( market , currentDate ) ) ;
card . setSumWow ( calculateAllWeekOverWeek ( date ) ) ;
card . setDaily ( calculateDayOverDay ( market , currentDate ) ) ;
card . setDaily ( calculateDayOverDay ( market , currentDate ) ) ;
card . setSumDaily ( calculateAllDayOverDay ( date ) ) ;
/ * / / 更新时间
/ * / / 更新时间
card . setUpdateTime ( currentStatistics . getUpdateTime ( ) ) ; * /
card . setUpdateTime ( currentStatistics . getUpdateTime ( ) ) ; * /
}
}
@ -170,22 +177,46 @@ public class WorkbenchServiceImpl implements WorkbenchService {
@Override
@Override
public WorkbenchCard getGraph ( Date startDate , Date endDate , List < String > markets ) {
public WorkbenchCard getGraph ( Date startDate , Date endDate , List < String > markets ) {
List < WorkbenchMarketGraph > marketGraphs = new ArrayList < > ( ) ;
if ( markets = = null | | markets . isEmpty ( ) ) {
return new WorkbenchCard ( new ArrayList < > ( ) , new ArrayList < > ( ) , markets , startDate , endDate , 0 , 0 ) ;
}
/ / 单次批量查询
List < WorkbenchFullStatistics > statsList = workBenchMapper . getFullStatisticsByMarketAndDate1 ( markets , startDate , endDate ) ;
/ / 构建 map : market - > statistics
Map < String , WorkbenchFullStatistics > statMap = statsList . stream ( )
. collect ( Collectors . toMap ( WorkbenchFullStatistics : : getMarket , Function . identity ( ) ) ) ;
/ / 构建最终结果
List < WorkbenchMarketGraph > marketGraphs = new ArrayList < > ( ) ;
for ( String market : markets ) {
for ( String market : markets ) {
Map < String , Integer > sums = calculateAllSum ( market , startDate , endDate ) ;
WorkbenchMarketGraph graphs = new WorkbenchMarketGraph ( ) ;
graphs . setMarket ( market ) ;
graphs . setSumRechargePermanent ( sums . get ( "money" ) ) ;
graphs . setSumRechargeFree ( sums . get ( "rFree" ) ) ;
graphs . setSumConsumePermanent ( sums . get ( "cPermanent" ) ) ;
graphs . setSumConsumeFree ( sums . get ( "cFree" ) ) ;
graphs . setSumConsumeTask ( sums . get ( "cTask" ) ) ;
graphs . setSumConsume ( sums . get ( "consume" ) ) ;
marketGraphs . add ( graphs ) ;
WorkbenchFullStatistics stats = statMap . getOrDefault ( market , new WorkbenchFullStatistics ( ) ) ;
Map < String , Integer > sums = new HashMap < > ( ) ;
sums . put ( "recharge" , stats . getTotalRecharge ( ) ! = null ? stats . getTotalRecharge ( ) : 0 ) ;
sums . put ( "money" , stats . getTotalMoney ( ) ! = null ? stats . getTotalMoney ( ) : 0 ) ;
sums . put ( "rFree" , sums . get ( "recharge" ) - sums . get ( "money" ) ) ;
sums . put ( "cPermanent" , stats . getTotalConsumePermanent ( ) ! = null ? stats . getTotalConsumePermanent ( ) : 0 ) ;
sums . put ( "cFree" , stats . getTotalConsumeFree ( ) ! = null ? stats . getTotalConsumeFree ( ) : 0 ) ;
sums . put ( "cTask" , stats . getTotalConsumeTask ( ) ! = null ? stats . getTotalConsumeTask ( ) : 0 ) ;
sums . put ( "consume" , sums . get ( "cPermanent" ) + sums . get ( "cFree" ) + sums . get ( "cTask" ) ) ;
sums . put ( "refund" , stats . getTotalRefund ( ) ! = null ? stats . getTotalRefund ( ) : 0 ) ;
sums . put ( "rechargeNum" , stats . getTotalRechargeNum ( ) ! = null ? stats . getTotalRechargeNum ( ) : 0 ) ;
WorkbenchMarketGraph graph = new WorkbenchMarketGraph ( ) ;
graph . setMarket ( market ) ;
graph . setSumRechargePermanent ( sums . get ( "money" ) ) ;
graph . setSumRechargeFree ( sums . get ( "rFree" ) ) ;
graph . setSumConsumePermanent ( sums . get ( "cPermanent" ) ) ;
graph . setSumConsumeFree ( sums . get ( "cFree" ) ) ;
graph . setSumConsumeTask ( sums . get ( "cTask" ) ) ;
graph . setSumConsume ( sums . get ( "consume" ) ) ;
marketGraphs . add ( graph ) ;
}
}
return new WorkbenchCard ( new ArrayList < > ( ) , marketGraphs , markets , startDate , endDate ) ;
return new WorkbenchCard ( new ArrayList < > ( ) , marketGraphs , markets , startDate , endDate , 0 , 0 ) ;
}
}
/ *
/ *
@ -196,18 +227,18 @@ public class WorkbenchServiceImpl implements WorkbenchService {
public Map < String , Integer > calculateAllSum ( String market , Date startDate , Date endDate ) {
public Map < String , Integer > calculateAllSum ( String market , Date startDate , Date endDate ) {
WorkbenchFullStatistics stats = workBenchMapper . getFullStatisticsByMarketAndDate ( market , startDate , endDate ) ;
WorkbenchFullStatistics stats = workBenchMapper . getFullStatisticsByMarketAndDate ( market , startDate , endDate ) ;
Map < String , Integer > result = new HashMap < > ( ) ;
Map < String , Integer > result = new HashMap < > ( ) ;
result . put ( "recharge" , stats . getTotalRecharge ( ) ! = null ? stats . getTotalRecharge ( ) : 0 ) ;
result . put ( "money" , stats . getTotalMoney ( ) ! = null ? stats . getTotalMoney ( ) : 0 ) ;
result . put ( "rFree" , ( stats . getTotalRecharge ( ) ! = null ? stats . getTotalRecharge ( ) : 0 )
result . put ( "recharge" , stats . getTotalRecharge ( ) ! = null ? stats . getTotalRecharge ( ) : 0 ) ; / / 获取充值统计数据
result . put ( "money" , stats . getTotalMoney ( ) ! = null ? stats . getTotalMoney ( ) : 0 ) ; / / 充值金额统计数据
result . put ( "rFree" , ( stats . getTotalRecharge ( ) ! = null ? stats . getTotalRecharge ( ) : 0 ) / / 获取充值的免费金币统计数据
- ( stats . getTotalMoney ( ) ! = null ? stats . getTotalMoney ( ) : 0 ) ) ;
- ( stats . getTotalMoney ( ) ! = null ? stats . getTotalMoney ( ) : 0 ) ) ;
result . put ( "cPermanent" , stats . getTotalConsumePermanent ( ) ! = null ? stats . getTotalConsumePermanent ( ) : 0 ) ;
result . put ( "cFree" , stats . getTotalConsumeFree ( ) ! = null ? stats . getTotalConsumeFree ( ) : 0 ) ;
result . put ( "cTask" , stats . getTotalConsumeTask ( ) ! = null ? stats . getTotalConsumeTask ( ) : 0 ) ;
result . put ( "consume" , ( stats . getTotalConsumePermanent ( ) ! = null ? stats . getTotalConsumePermanent ( ) : 0 )
result . put ( "cPermanent" , stats . getTotalConsumePermanent ( ) ! = null ? stats . getTotalConsumePermanent ( ) : 0 ) ; / / 获取消费的永久金币统计数据
result . put ( "cFree" , stats . getTotalConsumeFree ( ) ! = null ? stats . getTotalConsumeFree ( ) : 0 ) ; / / 获取消费的免费金币统计数据
result . put ( "cTask" , stats . getTotalConsumeTask ( ) ! = null ? stats . getTotalConsumeTask ( ) : 0 ) ; / / 获取消费的任金币统计数据
result . put ( "consume" , ( stats . getTotalConsumePermanent ( ) ! = null ? stats . getTotalConsumePermanent ( ) : 0 ) / / 获取全部消费统计数据
+ ( stats . getTotalConsumeFree ( ) ! = null ? stats . getTotalConsumeFree ( ) : 0 )
+ ( stats . getTotalConsumeFree ( ) ! = null ? stats . getTotalConsumeFree ( ) : 0 )
+ ( stats . getTotalConsumeTask ( ) ! = null ? stats . getTotalConsumeTask ( ) : 0 ) ) ;
+ ( stats . getTotalConsumeTask ( ) ! = null ? stats . getTotalConsumeTask ( ) : 0 ) ) ;
result . put ( "refund" , stats . getTotalRefund ( ) ! = null ? stats . getTotalRefund ( ) : 0 ) ;
result . put ( "rechargeNum" , stats . getTotalRechargeNum ( ) ! = null ? stats . getTotalRechargeNum ( ) : 0 ) ;
result . put ( "refund" , stats . getTotalRefund ( ) ! = null ? stats . getTotalRefund ( ) : 0 ) ; / / 获取退款统计数据
result . put ( "rechargeNum" , stats . getTotalRechargeNum ( ) ! = null ? stats . getTotalRechargeNum ( ) : 0 ) ; / / 获取充值人数统计数据
return result ;
return result ;