苍穹外卖--订单统计功能实现
This commit is contained in:
@@ -15,19 +15,14 @@ public class OrderReportVO implements Serializable {
|
|||||||
|
|
||||||
//日期,以逗号分隔,例如:2022-10-01,2022-10-02,2022-10-03
|
//日期,以逗号分隔,例如:2022-10-01,2022-10-02,2022-10-03
|
||||||
private String dateList;
|
private String dateList;
|
||||||
|
|
||||||
//每日订单数,以逗号分隔,例如:260,210,215
|
//每日订单数,以逗号分隔,例如:260,210,215
|
||||||
private String orderCountList;
|
private String orderCountList;
|
||||||
|
|
||||||
//每日有效订单数,以逗号分隔,例如:20,21,10
|
//每日有效订单数,以逗号分隔,例如:20,21,10
|
||||||
private String validOrderCountList;
|
private String validOrderCountList;
|
||||||
|
|
||||||
//订单总数
|
//订单总数
|
||||||
private Integer totalOrderCount;
|
private Integer totalOrderCount;
|
||||||
|
|
||||||
//有效订单数
|
//有效订单数
|
||||||
private Integer validOrderCount;
|
private Integer validOrderCount;
|
||||||
|
|
||||||
//订单完成率
|
//订单完成率
|
||||||
private Double orderCompletionRate;
|
private Double orderCompletionRate;
|
||||||
|
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package com.sky.controller.admin;
|
|||||||
|
|
||||||
import com.sky.result.Result;
|
import com.sky.result.Result;
|
||||||
import com.sky.service.ReportService;
|
import com.sky.service.ReportService;
|
||||||
|
import com.sky.vo.OrderReportVO;
|
||||||
import com.sky.vo.TurnoverReportVO;
|
import com.sky.vo.TurnoverReportVO;
|
||||||
import com.sky.vo.UserReportVO;
|
import com.sky.vo.UserReportVO;
|
||||||
import io.swagger.annotations.Api;
|
import io.swagger.annotations.Api;
|
||||||
@@ -54,4 +55,19 @@ public class ReportController {
|
|||||||
|
|
||||||
return Result.success(vo);
|
return Result.success(vo);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@GetMapping("/ordersStatistics")
|
||||||
|
@ApiOperation("订单统计")
|
||||||
|
public Result<OrderReportVO> ordersStatistics(
|
||||||
|
@DateTimeFormat(pattern = "yyyy-MM-dd")
|
||||||
|
LocalDate begin,
|
||||||
|
@DateTimeFormat(pattern = "yyyy-MM-dd")
|
||||||
|
LocalDate end){
|
||||||
|
log.info("订单统计:{},{}",begin,end);
|
||||||
|
//调用业务层,订单统计功能
|
||||||
|
OrderReportVO vo = reportService.getordersStatistics(begin,end);
|
||||||
|
|
||||||
|
return Result.success(vo);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -60,4 +60,6 @@ public interface OrderMapper {
|
|||||||
List<Orders> getByStatusAndOrderTime(Integer status, LocalDateTime time);
|
List<Orders> getByStatusAndOrderTime(Integer status, LocalDateTime time);
|
||||||
|
|
||||||
Double sumByMap(Map map);
|
Double sumByMap(Map map);
|
||||||
|
|
||||||
|
Integer countByMap(Map map);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
package com.sky.service;
|
package com.sky.service;
|
||||||
|
|
||||||
|
import com.sky.vo.OrderReportVO;
|
||||||
import com.sky.vo.TurnoverReportVO;
|
import com.sky.vo.TurnoverReportVO;
|
||||||
import com.sky.vo.UserReportVO;
|
import com.sky.vo.UserReportVO;
|
||||||
|
|
||||||
@@ -21,4 +22,12 @@ public interface ReportService {
|
|||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
UserReportVO getUserStatistics(LocalDate begin, LocalDate end);
|
UserReportVO getUserStatistics(LocalDate begin, LocalDate end);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 订单统计
|
||||||
|
* @param begin
|
||||||
|
* @param end
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
OrderReportVO getordersStatistics(LocalDate begin, LocalDate end);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import com.sky.entity.Orders;
|
|||||||
import com.sky.mapper.OrderMapper;
|
import com.sky.mapper.OrderMapper;
|
||||||
import com.sky.mapper.UserMapper;
|
import com.sky.mapper.UserMapper;
|
||||||
import com.sky.service.ReportService;
|
import com.sky.service.ReportService;
|
||||||
|
import com.sky.vo.OrderReportVO;
|
||||||
import com.sky.vo.TurnoverReportVO;
|
import com.sky.vo.TurnoverReportVO;
|
||||||
import com.sky.vo.UserReportVO;
|
import com.sky.vo.UserReportVO;
|
||||||
import org.apache.commons.lang3.StringUtils;
|
import org.apache.commons.lang3.StringUtils;
|
||||||
@@ -119,4 +120,69 @@ public class ReportServiceImpl implements ReportService {
|
|||||||
|
|
||||||
return userReportVO;
|
return userReportVO;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 订单统计
|
||||||
|
* @param begin
|
||||||
|
* @param end
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public OrderReportVO getordersStatistics(LocalDate begin, LocalDate end) {
|
||||||
|
List<LocalDate> dateList = new ArrayList<>();
|
||||||
|
dateList.add(begin);
|
||||||
|
//不停地创建日期对象,直到end
|
||||||
|
while (!begin.equals(end)){
|
||||||
|
begin = begin.plusDays(1);
|
||||||
|
dateList.add(begin);
|
||||||
|
}
|
||||||
|
//定义VO对象所需的4个局部变量
|
||||||
|
List<Integer> orderCountList = new ArrayList<>();
|
||||||
|
List<Integer> validOrderCountList = new ArrayList<>();
|
||||||
|
//订单总数
|
||||||
|
Integer totalOrderCount = 0;
|
||||||
|
//有效订单数
|
||||||
|
Integer validOrderCount = 0;
|
||||||
|
//订单完成率
|
||||||
|
Double orderCompletionRate = 0.0;
|
||||||
|
|
||||||
|
//根据日期,计算出以上5个数据
|
||||||
|
for (LocalDate localDate : dateList) {
|
||||||
|
//准备好开始和结束时间
|
||||||
|
LocalDateTime beginTime = LocalDateTime.of(localDate, LocalTime.MIN);
|
||||||
|
LocalDateTime endTime = LocalDateTime.of(localDate, LocalTime.MAX);
|
||||||
|
|
||||||
|
//select count(1) from orders where order_time > ? and order_time < ?
|
||||||
|
//select count(1) from orders where order_time > ? and order_time < ? and status = 5;
|
||||||
|
Map map = new HashMap();
|
||||||
|
map.put("end", endTime);
|
||||||
|
map.put("begin", beginTime);
|
||||||
|
Integer totalCount = orderMapper.countByMap(map);
|
||||||
|
orderCountList.add(totalCount);
|
||||||
|
//累加对应日期的订单数量
|
||||||
|
totalOrderCount += totalCount;
|
||||||
|
|
||||||
|
map.put("status", Orders.COMPLETED);
|
||||||
|
Integer validCount = orderMapper.countByMap(map);
|
||||||
|
validOrderCountList.add(validCount);
|
||||||
|
//累加对应日期的订单数量
|
||||||
|
validOrderCount += validCount;
|
||||||
|
|
||||||
|
}
|
||||||
|
//循环结束,对时间范围内的总订单数和有效订单数,已经准备好了,计算订单完成率
|
||||||
|
if (totalOrderCount != 0) {
|
||||||
|
orderCompletionRate = validOrderCount.doubleValue()/totalOrderCount;//注意:java中的运算符前后数据类型,计算的结果随大范围的数据类型
|
||||||
|
}
|
||||||
|
|
||||||
|
//封装数据
|
||||||
|
OrderReportVO orderReportVO = OrderReportVO.builder()
|
||||||
|
.validOrderCountList(StringUtils.join(validOrderCountList, ","))
|
||||||
|
.dateList(StringUtils.join(dateList, ","))
|
||||||
|
.orderCountList(StringUtils.join(orderCountList, ","))
|
||||||
|
.totalOrderCount(totalOrderCount)
|
||||||
|
.validOrderCount(validOrderCount)
|
||||||
|
.orderCompletionRate(orderCompletionRate).build();
|
||||||
|
|
||||||
|
return orderReportVO;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -71,5 +71,19 @@
|
|||||||
</if>
|
</if>
|
||||||
</where>
|
</where>
|
||||||
</select>
|
</select>
|
||||||
|
<select id="countByMap" resultType="java.lang.Integer" parameterType="java.util.Map">
|
||||||
|
select count(1) 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>
|
</mapper>
|
||||||
Reference in New Issue
Block a user