订单统计
This commit is contained in:
parent
18eb840ecc
commit
9d206c6c64
|
@ -2,6 +2,7 @@ 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;
|
||||
|
@ -52,4 +53,19 @@ public class ReportController {
|
|||
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);
|
||||
}
|
||||
|
|
|
@ -32,10 +32,10 @@ public interface UserMapper {
|
|||
User getById(Long userId);
|
||||
|
||||
/**
|
||||
* 根据动态条件统计用户数量
|
||||
* 根据动态条件统计统计营业数量
|
||||
*
|
||||
* @param map
|
||||
* @return
|
||||
* @param map Map
|
||||
* @return Integer
|
||||
*/
|
||||
Integer countByMap(Map map);
|
||||
Integer sumByMap(Map map);
|
||||
}
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
package com.sky.service;
|
||||
|
||||
import com.sky.vo.OrderReportVO;
|
||||
import com.sky.vo.TurnoverReportVO;
|
||||
import com.sky.vo.UserReportVO;
|
||||
|
||||
|
@ -23,4 +24,13 @@ public interface ReportService {
|
|||
* @return UserReportVO
|
||||
*/
|
||||
UserReportVO getUserStatistics(LocalDate begin, LocalDate end);
|
||||
|
||||
/**
|
||||
* 订单统计
|
||||
*
|
||||
* @param begin 看起始时间
|
||||
* @param end 结束时间
|
||||
* @return OrderReportVO
|
||||
*/
|
||||
OrderReportVO ordersStatistics(LocalDate begin, LocalDate end);
|
||||
}
|
||||
|
|
|
@ -4,6 +4,7 @@ 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;
|
||||
|
@ -86,10 +87,10 @@ public class ReportServiceImpl implements ReportService {
|
|||
HashMap map = new HashMap<>();
|
||||
map.put("end", endTime);
|
||||
// 总用户数量
|
||||
Integer totalUser = userMapper.countByMap(map);
|
||||
Integer totalUser = userMapper.sumByMap(map);
|
||||
map.put("begin", beginTime);
|
||||
// 新增用户数量
|
||||
Integer newUser = userMapper.countByMap(map);
|
||||
Integer newUser = userMapper.sumByMap(map);
|
||||
totalUserList.add(totalUser);
|
||||
newUserList.add(newUser);
|
||||
}
|
||||
|
@ -99,4 +100,63 @@ public class ReportServiceImpl implements ReportService {
|
|||
.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>
|
||||
|
|
|
@ -23,7 +23,7 @@
|
|||
</select>
|
||||
|
||||
<!-- 根据动态条件统计用户数量 -->
|
||||
<select id="countByMap" resultType="java.lang.Integer">
|
||||
<select id="sumByMap" resultType="java.lang.Integer">
|
||||
select count(id)
|
||||
from user
|
||||
<where>
|
||||
|
|
Loading…
Reference in New Issue