Compare commits
2 Commits
9c27154677
...
9d206c6c64
Author | SHA1 | Date |
---|---|---|
|
9d206c6c64 | |
|
18eb840ecc |
|
@ -2,7 +2,9 @@ package com.sky.controller.admin;
|
|||
|
||||
import com.sky.result.Result;
|
||||
import com.sky.service.ReportService;
|
||||
import com.sky.vo.OrderReportVO;
|
||||
import com.sky.vo.TurnoverReportVO;
|
||||
import com.sky.vo.UserReportVO;
|
||||
import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
@ -31,10 +33,39 @@ public class ReportController {
|
|||
*/
|
||||
@ApiOperation("营业额统计")
|
||||
@GetMapping("/turnoverStatistics")
|
||||
public Result<TurnoverReportVO> turnoverReport(@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
|
||||
@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end) {
|
||||
public Result<TurnoverReportVO> turnoverReport(@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin, @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end) {
|
||||
log.info("营业额统计,开始时间:{};结束时间:{}", begin, end);
|
||||
TurnoverReportVO turnoverStatistics = reportService.getTurnoverStatistics(begin, end);
|
||||
return Result.success(turnoverStatistics);
|
||||
}
|
||||
|
||||
/**
|
||||
* 用户统计
|
||||
*
|
||||
* @param begin 看起始时间
|
||||
* @param end 结束时间
|
||||
* @return UserReportVO
|
||||
*/
|
||||
@ApiOperation("用户统计")
|
||||
@GetMapping("/userStatistics")
|
||||
public Result<UserReportVO> userStatistics(@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin, @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end) {
|
||||
log.info("用户统计,开始时间:{};结束时间:{}", begin, end);
|
||||
UserReportVO userStatistics = reportService.getUserStatistics(begin, end);
|
||||
return Result.success(userStatistics);
|
||||
}
|
||||
|
||||
/**
|
||||
* 订单统计
|
||||
*
|
||||
* @param begin 看起始时间
|
||||
* @param end 结束时间
|
||||
* @return OrderReportVO
|
||||
*/
|
||||
@ApiOperation("订单统计")
|
||||
@GetMapping("/ordersStatistics")
|
||||
public Result<OrderReportVO> ordersStatistics(@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
|
||||
@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end) {
|
||||
log.info("订单统计,开始时间:{};结束时间:{}", begin, end);
|
||||
return Result.success(reportService.ordersStatistics(begin, end));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -8,6 +8,7 @@ import org.apache.ibatis.annotations.Mapper;
|
|||
import java.time.LocalDateTime;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@Mapper
|
||||
public interface OrderMapper {
|
||||
|
@ -79,4 +80,12 @@ public interface OrderMapper {
|
|||
* @return Double
|
||||
*/
|
||||
Double sumByMap(HashMap map);
|
||||
|
||||
/**
|
||||
* 根据动态条件统计用户数量
|
||||
*
|
||||
* @param map Map
|
||||
* @return Integer
|
||||
*/
|
||||
Integer countByMap(Map map);
|
||||
}
|
||||
|
|
|
@ -3,6 +3,8 @@ package com.sky.mapper;
|
|||
import com.sky.entity.User;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
@Mapper
|
||||
public interface UserMapper {
|
||||
|
||||
|
@ -28,4 +30,12 @@ public interface UserMapper {
|
|||
* @return User
|
||||
*/
|
||||
User getById(Long userId);
|
||||
|
||||
/**
|
||||
* 根据动态条件统计统计营业数量
|
||||
*
|
||||
* @param map Map
|
||||
* @return Integer
|
||||
*/
|
||||
Integer sumByMap(Map map);
|
||||
}
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
package com.sky.service;
|
||||
|
||||
import com.sky.vo.OrderReportVO;
|
||||
import com.sky.vo.TurnoverReportVO;
|
||||
import com.sky.vo.UserReportVO;
|
||||
|
||||
import java.time.LocalDate;
|
||||
|
||||
|
@ -13,4 +15,22 @@ public interface ReportService {
|
|||
* @return TurnoverReportVO
|
||||
*/
|
||||
TurnoverReportVO getTurnoverStatistics(LocalDate begin, LocalDate end);
|
||||
|
||||
/**
|
||||
* 用户统计
|
||||
*
|
||||
* @param begin 看起始时间
|
||||
* @param end 结束时间
|
||||
* @return UserReportVO
|
||||
*/
|
||||
UserReportVO getUserStatistics(LocalDate begin, LocalDate end);
|
||||
|
||||
/**
|
||||
* 订单统计
|
||||
*
|
||||
* @param begin 看起始时间
|
||||
* @param end 结束时间
|
||||
* @return OrderReportVO
|
||||
*/
|
||||
OrderReportVO ordersStatistics(LocalDate begin, LocalDate end);
|
||||
}
|
||||
|
|
|
@ -2,8 +2,11 @@ package com.sky.service.impl;
|
|||
|
||||
import com.sky.entity.Orders;
|
||||
import com.sky.mapper.OrderMapper;
|
||||
import com.sky.mapper.UserMapper;
|
||||
import com.sky.service.ReportService;
|
||||
import com.sky.vo.OrderReportVO;
|
||||
import com.sky.vo.TurnoverReportVO;
|
||||
import com.sky.vo.UserReportVO;
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
|
@ -13,11 +16,14 @@ import java.time.LocalDateTime;
|
|||
import java.time.LocalTime;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
|
||||
@Service
|
||||
public class ReportServiceImpl implements ReportService {
|
||||
@Resource
|
||||
private OrderMapper orderMapper;
|
||||
@Resource
|
||||
private UserMapper userMapper;
|
||||
|
||||
/**
|
||||
* 营业额统计
|
||||
|
@ -53,4 +59,104 @@ public class ReportServiceImpl implements ReportService {
|
|||
|
||||
return TurnoverReportVO.builder().dateList(StringUtils.join(dateArrayList, ",")).turnoverList(StringUtils.join(turnoverList, ",")).build();
|
||||
}
|
||||
|
||||
/**
|
||||
* 用户统计
|
||||
*
|
||||
* @param begin 看起始时间
|
||||
* @param end 结束时间
|
||||
* @return UserReportVO
|
||||
*/
|
||||
@Override
|
||||
public UserReportVO getUserStatistics(LocalDate begin, LocalDate end) {
|
||||
List<LocalDate> dateList = new ArrayList<>();
|
||||
dateList.add(begin);
|
||||
|
||||
while (!begin.equals(end)) {
|
||||
begin = begin.plusDays(1);
|
||||
dateList.add(begin);
|
||||
}
|
||||
// 存放每天的新增用户数量
|
||||
ArrayList<Integer> newUserList = new ArrayList<>();
|
||||
// 存放每天总用户数量
|
||||
List<Integer> totalUserList = new ArrayList<>();
|
||||
for (LocalDate date : dateList) {
|
||||
LocalDateTime beginTime = LocalDateTime.of(date, LocalTime.MIN);
|
||||
LocalDateTime endTime = LocalDateTime.of(date, LocalTime.MAX);
|
||||
|
||||
HashMap map = new HashMap<>();
|
||||
map.put("end", endTime);
|
||||
// 总用户数量
|
||||
Integer totalUser = userMapper.sumByMap(map);
|
||||
map.put("begin", beginTime);
|
||||
// 新增用户数量
|
||||
Integer newUser = userMapper.sumByMap(map);
|
||||
totalUserList.add(totalUser);
|
||||
newUserList.add(newUser);
|
||||
}
|
||||
return UserReportVO.builder()
|
||||
.dateList(StringUtils.join(dateList, ","))
|
||||
.totalUserList(StringUtils.join(totalUserList, ","))
|
||||
.newUserList(StringUtils.join(newUserList, ","))
|
||||
.build();
|
||||
}
|
||||
|
||||
/**
|
||||
* 订单统计
|
||||
*
|
||||
* @param begin 看起始时间
|
||||
* @param end 结束时间
|
||||
* @return OrderReportVO
|
||||
*/
|
||||
@Override
|
||||
public OrderReportVO ordersStatistics(LocalDate begin, LocalDate end) {
|
||||
List<LocalDate> dateList = new ArrayList<>();
|
||||
dateList.add(begin);
|
||||
|
||||
while (!begin.equals(end)) {
|
||||
begin = begin.plusDays(1);
|
||||
dateList.add(begin);
|
||||
}
|
||||
// 存放每天订单数
|
||||
List<Integer> orderCountList = new ArrayList<>();
|
||||
// 存放每天有效订单数
|
||||
List<Integer> vailOrderCountList = new ArrayList<>();
|
||||
|
||||
// 遍历dateList集合,查询每天的有效订单和订单总数
|
||||
for (LocalDate date : dateList) {
|
||||
// 查询订单数
|
||||
LocalDateTime beginTime = LocalDateTime.of(date, LocalTime.MIN);
|
||||
LocalDateTime endTime = LocalDateTime.of(date, LocalTime.MAX);
|
||||
Integer orderCount = getOrderCount(beginTime, endTime, null);
|
||||
Integer vailOrderCount = getOrderCount(beginTime, endTime, Orders.COMPLETED);// 查询每天有效订单数
|
||||
|
||||
orderCountList.add(orderCount);
|
||||
vailOrderCountList.add(vailOrderCount);
|
||||
}
|
||||
// 计算时间区间订单查询
|
||||
int totalOrderCount = orderCountList.stream().reduce(Integer::sum).get();
|
||||
// 计算时间区间内有效订单数量
|
||||
Integer vailOrderCount = vailOrderCountList.stream().reduce(Integer::sum).get();
|
||||
// 订单完成率
|
||||
double orderCompletionRate = 0.0;
|
||||
if (totalOrderCount != 0) {
|
||||
orderCompletionRate = vailOrderCount.doubleValue() / totalOrderCount;
|
||||
}
|
||||
return OrderReportVO.builder()
|
||||
.dateList(StringUtils.join(dateList, ","))
|
||||
.orderCountList(StringUtils.join(orderCountList, ","))
|
||||
.validOrderCountList((StringUtils.join(vailOrderCountList, ",")))
|
||||
.totalOrderCount(totalOrderCount)
|
||||
.validOrderCount(vailOrderCount)
|
||||
.orderCompletionRate(orderCompletionRate)
|
||||
.build();
|
||||
}
|
||||
|
||||
private Integer getOrderCount(LocalDateTime endTime, LocalDateTime beginTime, Integer status) {
|
||||
HashMap map = new HashMap<>();
|
||||
map.put("end", endTime);
|
||||
map.put("begin", beginTime);
|
||||
map.put("status", status);
|
||||
return orderMapper.countByMap(map);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -125,4 +125,21 @@
|
|||
</if>
|
||||
</where>
|
||||
</select>
|
||||
|
||||
<!-- 根据动态条件统计用户数量 -->
|
||||
<select id="countByMap" resultType="java.lang.Integer">
|
||||
select count(id)
|
||||
from orders
|
||||
<where>
|
||||
<if test="begin != null">
|
||||
and order_time > #{begin}
|
||||
</if>
|
||||
<if test="end != null">
|
||||
and order_time < #{end}
|
||||
</if>
|
||||
<if test="status != null">
|
||||
and status = #{status}
|
||||
</if>
|
||||
</where>
|
||||
</select>
|
||||
</mapper>
|
||||
|
|
|
@ -21,4 +21,18 @@
|
|||
from user
|
||||
where id = #{id};
|
||||
</select>
|
||||
|
||||
<!-- 根据动态条件统计用户数量 -->
|
||||
<select id="sumByMap" resultType="java.lang.Integer">
|
||||
select count(id)
|
||||
from user
|
||||
<where>
|
||||
<if test="begin != null">
|
||||
and create_time > #{begin}
|
||||
</if>
|
||||
<if test="end != null">
|
||||
and create_time < #{end}
|
||||
</if>
|
||||
</where>
|
||||
</select>
|
||||
</mapper>
|
||||
|
|
Loading…
Reference in New Issue