feat(platform): 平台数据统计
parent
d419eb53a7
commit
39059b6f4c
|
@ -0,0 +1,111 @@
|
|||
package com.mcwl.web.controller.platformData;
|
||||
|
||||
|
||||
import com.mcwl.common.core.domain.R;
|
||||
import com.mcwl.pay.service.OrderTradeService;
|
||||
import com.mcwl.system.service.ISysUserService;
|
||||
import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@Api(tags = "平台数据")
|
||||
@RestController
|
||||
@RequestMapping("platformData")
|
||||
@RequiredArgsConstructor
|
||||
public class PlatformData {
|
||||
|
||||
private final OrderTradeService orderTradeService;
|
||||
|
||||
private final ISysUserService sysUserService;
|
||||
|
||||
/**
|
||||
* 今日收益总额
|
||||
*/
|
||||
@ApiOperation(value = "今日收益总额")
|
||||
@RequestMapping("getTodayIncome")
|
||||
public R<Double> getTodayIncome() {
|
||||
Double amount = orderTradeService.getTodayIncome();
|
||||
return R.ok(amount);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 本月收益总额
|
||||
*/
|
||||
@ApiOperation(value = "本月收益总额")
|
||||
@RequestMapping("getMonthIncome")
|
||||
public R<Double> getMonthIncome() {
|
||||
Double amount = orderTradeService.getMonthIncome();
|
||||
return R.ok(amount);
|
||||
}
|
||||
|
||||
/**
|
||||
* 年度收益总额
|
||||
*/
|
||||
@ApiOperation(value = "年度收益总额")
|
||||
@RequestMapping("getYearIncome")
|
||||
public R<Double> getYearIncome() {
|
||||
Double amount = orderTradeService.getYearIncome();
|
||||
return R.ok(amount);
|
||||
}
|
||||
|
||||
/**
|
||||
* 总收益总额
|
||||
*/
|
||||
@ApiOperation(value = "总收益总额")
|
||||
@RequestMapping("getTotalIncome")
|
||||
public R<Double> getTotalIncome() {
|
||||
Double amount = orderTradeService.getTotalIncome();
|
||||
return R.ok(amount);
|
||||
}
|
||||
|
||||
/**
|
||||
* 收益同比增长 (Year-on-Year, YoY)
|
||||
* (本期数-同期数)/同期数×100%
|
||||
*/
|
||||
@ApiOperation(value = "收益同比增长")
|
||||
@RequestMapping("getYoYTrend")
|
||||
public R<Double> getYoYTrend() {
|
||||
Double yoyTrend = orderTradeService.getYoYTrend();
|
||||
return R.ok(yoyTrend);
|
||||
}
|
||||
|
||||
/**
|
||||
* 收益环比增长 (Month-on-Month, MoM)
|
||||
* (本期数 - 上期数) / 上期数 × 100%
|
||||
*/
|
||||
@ApiOperation(value = "收益环比增长")
|
||||
@RequestMapping("getMoMTrend")
|
||||
public R<Double> getMoMTrend() {
|
||||
Double momTrend = orderTradeService.getMoMTrend();
|
||||
return R.ok(momTrend);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取用户数量
|
||||
*/
|
||||
@ApiOperation(value = "获取用户数量")
|
||||
@RequestMapping("getUserCount")
|
||||
public R<Integer> getUserCount() {
|
||||
int count = sysUserService.getUserCount();
|
||||
return R.ok(count);
|
||||
}
|
||||
|
||||
/**
|
||||
* 本月新增用户数
|
||||
*/
|
||||
@ApiOperation(value = "本月新增用户数")
|
||||
@RequestMapping("getMonthUserCount")
|
||||
public R<Integer> getMonthUserCount() {
|
||||
int count = sysUserService.getMonthUserCount();
|
||||
return R.ok(count);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
|
@ -34,7 +34,7 @@ server:
|
|||
# 日志配置
|
||||
logging:
|
||||
level:
|
||||
com.mcwl: error
|
||||
com.mcwl: debug
|
||||
org.springframework: warn
|
||||
|
||||
# 用户配置
|
||||
|
|
|
@ -49,11 +49,11 @@ public class GlobalExceptionHandler {
|
|||
/**
|
||||
* redis连接超时异常
|
||||
*/
|
||||
@ExceptionHandler(QueryTimeoutException.class)
|
||||
public AjaxResult commandTimeoutException(QueryTimeoutException e, HttpServletRequest request) {
|
||||
@ExceptionHandler(RedisCommandTimeoutException.class)
|
||||
public AjaxResult commandTimeoutException(RedisCommandTimeoutException e, HttpServletRequest request) {
|
||||
String requestURI = request.getRequestURI();
|
||||
log.error("redis异常{},{}", requestURI, e.getMessage());
|
||||
return AjaxResult.error(HttpStatus.WARN, "超时");
|
||||
return AjaxResult.warn("超时");
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -82,6 +82,9 @@ public class TokenService
|
|||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
if (e.getCause() instanceof io.lettuce.core.RedisCommandTimeoutException) {
|
||||
throw new RedisCommandTimeoutException("连接超时");
|
||||
}
|
||||
log.error("获取用户信息异常'{}'", e.getMessage());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,6 +4,7 @@ import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
|||
import com.mcwl.pay.domain.OrderTrade;
|
||||
import com.mcwl.pay.domain.PaymentResult;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
|
||||
/**
|
||||
* @Author:ChenYan
|
||||
|
@ -18,4 +19,40 @@ public interface OrderTradeMapper extends BaseMapper<OrderTrade> {
|
|||
|
||||
PaymentResult chargeCard(Integer totalAmount, String paymentMethod);
|
||||
|
||||
/**
|
||||
* 获取今日收益
|
||||
* @return Double
|
||||
*/
|
||||
Double getTodayIncome();
|
||||
|
||||
/**
|
||||
* 获取本月收益
|
||||
* @return Double
|
||||
*/
|
||||
Double getMonthIncome();
|
||||
|
||||
/**
|
||||
* 获取指定年月收益
|
||||
* @return Double
|
||||
*/
|
||||
Double getIncomeByMonth(@Param("year") Integer year, @Param("month") Integer month);
|
||||
|
||||
/**
|
||||
* 获取年度收益
|
||||
* @return Double
|
||||
*/
|
||||
Double getYearIncome();
|
||||
|
||||
/**
|
||||
* 获取指定年收益
|
||||
* @return Double
|
||||
*/
|
||||
Double getIncomeByYear(@Param("year") Integer year);
|
||||
|
||||
/**
|
||||
* 获取总收益
|
||||
* @return Double
|
||||
*/
|
||||
Double getTotalIncome();
|
||||
|
||||
}
|
||||
|
|
|
@ -40,4 +40,39 @@ public interface OrderTradeService extends IService<OrderTrade> {
|
|||
*/
|
||||
TableDataInfo getRecord(PageDomain pageDomain, String productName);
|
||||
|
||||
/**
|
||||
* 获取今日收益
|
||||
* @return Double
|
||||
*/
|
||||
Double getTodayIncome();
|
||||
|
||||
/**
|
||||
* 获取本月收益
|
||||
* @return Double
|
||||
*/
|
||||
Double getMonthIncome();
|
||||
|
||||
/**
|
||||
* 获取年度收益
|
||||
* @return Double
|
||||
*/
|
||||
Double getYearIncome();
|
||||
|
||||
/**
|
||||
* 获取总收益
|
||||
* @return Double
|
||||
*/
|
||||
Double getTotalIncome();
|
||||
|
||||
/**
|
||||
* 获取收益同比增长率
|
||||
* @return Double
|
||||
*/
|
||||
Double getYoYTrend();
|
||||
|
||||
/**
|
||||
* 获取收益环比增长
|
||||
* @return Double
|
||||
*/
|
||||
Double getMoMTrend();
|
||||
}
|
||||
|
|
|
@ -46,7 +46,9 @@ import org.springframework.transaction.annotation.Transactional;
|
|||
|
||||
import java.math.BigDecimal;
|
||||
import java.math.RoundingMode;
|
||||
import java.time.LocalDate;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* @Author:ChenYan
|
||||
|
@ -171,6 +173,134 @@ public class OrderTradeServiceImpl extends ServiceImpl<OrderTradeMapper, OrderTr
|
|||
return rspData;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取今日收入
|
||||
*
|
||||
* @return Double
|
||||
*/
|
||||
@Override
|
||||
public Double getTodayIncome() {
|
||||
Double todayIncome = redisCache.getCacheObject("income:todayIncome");
|
||||
if (Objects.nonNull(todayIncome)) {
|
||||
return todayIncome;
|
||||
}
|
||||
todayIncome = orderTradeMapper.getTodayIncome();
|
||||
redisCache.setCacheObject("income:todayIncome", todayIncome, 1, TimeUnit.MINUTES);
|
||||
return todayIncome;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取本月收入
|
||||
*
|
||||
* @return Double
|
||||
*/
|
||||
@Override
|
||||
public Double getMonthIncome() {
|
||||
Double monthIncome = redisCache.getCacheObject("income:monthIncome");
|
||||
if (Objects.nonNull(monthIncome)) {
|
||||
return monthIncome;
|
||||
}
|
||||
monthIncome = orderTradeMapper.getMonthIncome();
|
||||
redisCache.setCacheObject("income:monthIncome", monthIncome, 1, TimeUnit.MINUTES);
|
||||
return monthIncome;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取本年收入
|
||||
*
|
||||
* @return Double
|
||||
*/
|
||||
@Override
|
||||
public Double getYearIncome() {
|
||||
Double yearIncome = redisCache.getCacheObject("income:yearIncome");
|
||||
if (Objects.nonNull(yearIncome)) {
|
||||
return yearIncome;
|
||||
}
|
||||
yearIncome = orderTradeMapper.getYearIncome();
|
||||
redisCache.setCacheObject("income:yearIncome", yearIncome, 1, TimeUnit.MINUTES);
|
||||
return yearIncome;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取总收入
|
||||
*
|
||||
* @return Double
|
||||
*/
|
||||
@Override
|
||||
public Double getTotalIncome() {
|
||||
Double totalIncome = redisCache.getCacheObject("income:totalIncome");
|
||||
if (Objects.nonNull(totalIncome)) {
|
||||
return totalIncome;
|
||||
}
|
||||
totalIncome = orderTradeMapper.getTotalIncome();
|
||||
redisCache.setCacheObject("income:totalIncome", totalIncome, 1, TimeUnit.MINUTES);
|
||||
return totalIncome;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取同比
|
||||
*
|
||||
* @return Double
|
||||
*/
|
||||
@Override
|
||||
public Double getYoYTrend() {
|
||||
// 生成带年月信息的缓存键
|
||||
int currentYear = LocalDate.now().getYear();
|
||||
int currentMonth = LocalDate.now().getMonthValue();
|
||||
String cacheKey = String.format("income:yoyTrend:%d-%02d", currentYear, currentMonth);
|
||||
|
||||
// 尝试从缓存获取
|
||||
Double yoyTrend = redisCache.getCacheObject(cacheKey);
|
||||
if (Objects.nonNull(yoyTrend)) {
|
||||
return yoyTrend;
|
||||
}
|
||||
|
||||
// 查询数据库
|
||||
Double yearIncome = baseMapper.getIncomeByMonth(currentYear, currentMonth);
|
||||
Double lastYearIncome = baseMapper.getIncomeByMonth(currentYear - 1, currentMonth);
|
||||
|
||||
// 检查除零
|
||||
if (lastYearIncome == 0.0) {
|
||||
return 1.0;
|
||||
}
|
||||
|
||||
yoyTrend = (yearIncome - lastYearIncome) / lastYearIncome;
|
||||
redisCache.setCacheObject(cacheKey, yoyTrend, 12, TimeUnit.HOURS);
|
||||
|
||||
return yoyTrend;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取环比
|
||||
*
|
||||
* @return Double
|
||||
*/
|
||||
@Override
|
||||
public Double getMoMTrend() {
|
||||
int currentYear = LocalDate.now().getYear();
|
||||
int currentMonth = LocalDate.now().getMonthValue();
|
||||
String cacheKey = String.format("income:momTrend:%d-%02d", currentYear, currentMonth);
|
||||
|
||||
// 尝试从缓存获取
|
||||
Double momTrend = redisCache.getCacheObject(cacheKey);
|
||||
if (Objects.nonNull(momTrend)) {
|
||||
return momTrend;
|
||||
}
|
||||
|
||||
// 查询数据库
|
||||
Double month = baseMapper.getIncomeByMonth(currentYear, currentMonth);
|
||||
Double lastMonth = baseMapper.getIncomeByMonth(currentYear, currentMonth - 1);
|
||||
|
||||
// 检查除零
|
||||
if (lastMonth == 0.0) {
|
||||
return 1.0;
|
||||
}
|
||||
|
||||
momTrend = (month - lastMonth) / lastMonth;
|
||||
redisCache.setCacheObject(cacheKey, momTrend, 12, TimeUnit.HOURS);
|
||||
|
||||
return momTrend;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<OrderTrade> selectMallProductList(OrderTrade orderTrade) {
|
||||
|
|
|
@ -8,4 +8,47 @@
|
|||
<update id="chargeCard">
|
||||
|
||||
</update>
|
||||
|
||||
<select id="getTodayIncome" resultType="java.lang.Double">
|
||||
SELECT COALESCE(SUM(payment_amount), 0)
|
||||
FROM order_trade
|
||||
WHERE create_time >= CURDATE()
|
||||
AND create_time < CURDATE() + INTERVAL 1 DAY
|
||||
</select>
|
||||
|
||||
<select id="getMonthIncome" resultType="java.lang.Double">
|
||||
SELECT COALESCE(SUM(payment_amount), 0)
|
||||
FROM order_trade
|
||||
WHERE create_time >= DATE_FORMAT(NOW(), '%Y-%m-01')
|
||||
AND create_time < DATE_FORMAT(NOW(), '%Y-%m-01') + INTERVAL 1 MONTH
|
||||
</select>
|
||||
|
||||
<select id="getYearIncome" resultType="java.lang.Double">
|
||||
SELECT COALESCE(SUM(payment_amount), 0)
|
||||
FROM order_trade
|
||||
WHERE create_time >= CONCAT(YEAR(NOW()), '-01-01')
|
||||
AND create_time < CONCAT(YEAR(NOW()) + 1, '-01-01')
|
||||
</select>
|
||||
|
||||
<select id="getIncomeByYear" resultType="java.lang.Double">
|
||||
SELECT COALESCE(SUM(payment_amount), 0)
|
||||
FROM order_trade
|
||||
WHERE create_time >= CONCAT(#{year}, '-01-01')
|
||||
AND create_time < CONCAT(#{year} + 1, '-01-01')
|
||||
</select>
|
||||
|
||||
<select id="getTotalIncome" resultType="java.lang.Double">
|
||||
SELECT COALESCE(SUM(payment_amount), 0)
|
||||
FROM order_trade
|
||||
</select>
|
||||
|
||||
<select id="getIncomeByMonth" resultType="java.lang.Double">
|
||||
SELECT COALESCE(SUM(payment_amount), 0)
|
||||
FROM order_trade
|
||||
WHERE
|
||||
create_time >= CONCAT(#{year}, '-', LPAD(#{month}, 2, '0'), '-01')
|
||||
AND create_time < CONCAT(#{year}, '-', LPAD(#{month}, 2, '0'), '-01') + INTERVAL 1 MONTH
|
||||
</select>
|
||||
|
||||
|
||||
</mapper>
|
||||
|
|
|
@ -144,4 +144,7 @@ public interface SysUserMapper
|
|||
|
||||
void updateBackgroundImg(@Param("id") Long id, @Param("path") String path);
|
||||
|
||||
Integer getUserCount();
|
||||
|
||||
Integer getMonthUserCount();
|
||||
}
|
||||
|
|
|
@ -219,4 +219,8 @@ public interface ISysUserService
|
|||
List<SysUser> listByIds(List<Long> userIdList);
|
||||
|
||||
void updateBackgroundImg(Long id, String path);
|
||||
|
||||
Integer getUserCount();
|
||||
|
||||
Integer getMonthUserCount();
|
||||
}
|
||||
|
|
|
@ -693,4 +693,15 @@ public class SysUserServiceImpl implements ISysUserService
|
|||
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public Integer getUserCount() {
|
||||
return userMapper.getUserCount();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer getMonthUserCount() {
|
||||
return userMapper.getMonthUserCount();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -181,6 +181,17 @@
|
|||
WHERE a.to_user_id = #{userId}
|
||||
</select>
|
||||
|
||||
<select id="getUserCount" resultType="java.lang.Integer">
|
||||
select COALESCE(count(*), 0) from sys_user
|
||||
</select>
|
||||
|
||||
<select id="getMonthUserCount" resultType="java.lang.Integer">
|
||||
select COALESCE(count(*), 0)
|
||||
from sys_user
|
||||
where create_time >= DATE_FORMAT(NOW(), '%Y-%m-01')
|
||||
AND create_time < DATE_FORMAT(NOW(), '%Y-%m-01') + INTERVAL 1 MONTH
|
||||
</select>
|
||||
|
||||
<insert id="insertUser" parameterType="SysUser" useGeneratedKeys="true" keyProperty="userId">
|
||||
insert into sys_user(
|
||||
<if test="userId != null and userId != 0">user_id,</if>
|
||||
|
|
Loading…
Reference in New Issue