feat(platform): 平台数据统计

master
yang 2025-03-08 15:18:42 +08:00
parent d419eb53a7
commit 39059b6f4c
12 changed files with 392 additions and 4 deletions

View File

@ -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);
}
}

View File

@ -34,7 +34,7 @@ server:
# 日志配置
logging:
level:
com.mcwl: error
com.mcwl: debug
org.springframework: warn
# 用户配置

View File

@ -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("超时");
}

View File

@ -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());
}
}

View File

@ -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;
/**
* @AuthorChenYan
@ -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();
}

View File

@ -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();
}

View File

@ -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;
/**
* @AuthorChenYan
@ -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) {

View File

@ -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 &lt; 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 &lt; 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 &lt; 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 &lt; 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 &lt; CONCAT(#{year}, '-', LPAD(#{month}, 2, '0'), '-01') + INTERVAL 1 MONTH
</select>
</mapper>

View File

@ -144,4 +144,7 @@ public interface SysUserMapper
void updateBackgroundImg(@Param("id") Long id, @Param("path") String path);
Integer getUserCount();
Integer getMonthUserCount();
}

View File

@ -219,4 +219,8 @@ public interface ISysUserService
List<SysUser> listByIds(List<Long> userIdList);
void updateBackgroundImg(Long id, String path);
Integer getUserCount();
Integer getMonthUserCount();
}

View File

@ -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();
}
}

View File

@ -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 &lt; 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>