com.sky.controller.admin.ReportController
@RestController
@RequestMapping("/admin/report")
@Api(tags = "数据统计相关接口")
@Slf4j
public class ReportController {
@Autowired
private ReportService reportService;
/**
* 营业额统计
* @param begin
* @param end
* @return
*/
@GetMapping("/turnoverStatistics")
@ApiOperation("营业额统计")
public Result<TurnoverReportVO> turnoverStatistics(
@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);
}
}
com.sky.service.impl.ReportServiceImpl.java
@Service
public class ReportServiceImpl implements ReportService {
@Autowired
private OrderMapper orderMapper;
/**
* 统计指定时间内的营业额
* @param begin
* @param end
* @return
*/
public TurnoverReportVO getTurnoverStatistics(@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end) {
//存放从begin到end的日期
List<LocalDate> dateList = new ArrayList<>();
//范围是[begin,end)
dateList.add(begin);
while(!begin.equals(end)){//相同是最后一天,也有isbefore这种方法,注意plusDays是返回值
begin = begin.plusDays(1);
dateList.add(begin);
}
//存放明天的营业额
List<Double> turnoverList = new ArrayList<>();
for (LocalDate date : dateList) {
//查询date日期对应的营业额,营业额值 status=已完成 的金额合计
LocalDateTime beginOfDay = LocalDateTime.of(date, LocalTime.MIN);
LocalDateTime endOfDay = LocalDateTime.of(date, LocalTime.MAX);
//select sum(Amount) from where order_time > ? and order_time < ? and status = 5
Map map = new HashMap();
map.put("begin",beginOfDay);
map.put("end",endOfDay);
map.put("status", Orders.COMPLETED);
Double turnover =orderMapper.sumByMap(map);
turnover = turnover == null ? 0.0 : turnover;
turnoverList.add(turnover);
}
return TurnoverReportVO.builder()
.dateList(StringUtils.join(dateList,","))
.turnoverList(StringUtils.join(turnoverList,","))
.build();
}
}
orderMapper
/**
* 根据动态条件统计营业额数据
* @param map
* @return
*/
Double sumByMap(Map map);
orderMapper.xml
<select id="sumByMap" resultType="java.lang.Double">
select sum(amount) 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>
/**
* 统计订单
* @param begin
* @param end
* @return
*/
@GetMapping("/ordersStatistics")
public Result<OrderReportVO> orderStatistics(@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end){
log.info("统计订单");
OrderReportVO orderReportVO = reportService.getOrderStatistics(begin,end);
return Result.success(orderReportVO);
}
ReportServiceImpl
/**
* 统计订单数量
* @param begin
* @param end
* @return
*/
public OrderReportVO getOrderStatistics(LocalDate begin, LocalDate end) {
//这个获取dateList是相同的,复制上面的就可以
//存放从begin到end的日期
List<LocalDate> dateList = new ArrayList<>();
//范围是[begin,end)
dateList.add(begin);
while(!begin.equals(end)){//相同是最后一天,也有isbefore这种方法,注意plusDays是返回值
begin = begin.plusDays(1);
dateList.add(begin);
}
List<Integer> orderCountList = new ArrayList<>();
List<Integer> validOrderCountList = new ArrayList<>();
//遍历dateList查询有效订单数和订单总数
for (LocalDate date : dateList) {
//查询每天的订单数量 select count(id) from orders where order_time < end and order_time >begin;
LocalDateTime beginTime = LocalDateTime.of(date, LocalTime.MIN);
LocalDateTime endTime = LocalDateTime.of(date, LocalTime.MAX);
Integer orderCount = getOrderCount(beginTime, endTime, null);
//查询每天的有效订单数 select count() from orders where status = 5 and order_time < end and order_time >begin
Integer validOrderCount = getOrderCount(beginTime, endTime, Orders.COMPLETED);
orderCountList.add(orderCount);
validOrderCountList.add(validOrderCount);
}
//计算闭区间的订单总数量
Integer totalOrderCount = orderCountList.stream().reduce(Integer::sum).get();
//计算时间区间内的有效订单数量
Integer validOrderCount = validOrderCountList.stream().reduce(Integer::sum).get();
//计算订单完成率
Double orderCompletionRate = 0.0;
if(totalOrderCount != 0) {
orderCompletionRate = validOrderCount.doubleValue() / totalOrderCount;
}
return OrderReportVO.builder()
.dateList(StringUtils.join(dateList,","))
.orderCountList(StringUtils.join(orderCountList,","))
.validOrderCountList(StringUtils.join(validOrderCountList,","))
.validOrderCount(validOrderCount)
.totalOrderCount(totalOrderCount)
.orderCompletionRate(orderCompletionRate)
.build();
}
/**
* 根据条件统计订单数量
* @param begin
* @param end
* @param status
* @return
*/
private Integer getOrderCount(LocalDateTime begin,LocalDateTime end,Integer status){
Map map = new HashMap();
map.put("begin",begin);
map.put("end",end);
map.put("status",status);
return orderMapper.countByMap(map);
}
orderMapper.xml
<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>
/**
* 销量排名
* @param begin
* @param end
* @return
*/
@ApiOperation("销量排名top10")
@GetMapping("/top10")
public Result<SalesTop10ReportVO> top10(@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end){
log.info("销量排名top10:{},{}",begin,end);
return Result.success(reportService.getSalesTop10(begin,end));
}
ReportServiceImpl
/**
* 统计销量排名前十
* @param begin
* @param end
* @return
*/
public SalesTop10ReportVO getSalesTop10(@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end) {
//select od.name,sum(od.number) number from order_detail od,orders o where od.order_id = o.id
// and o.status = 5 and o.order_time > ? and o.order_time < ?
//group by od.name order by number desc
//limit 0,10
LocalDateTime beginOfDay = LocalDateTime.of(begin, LocalTime.MIN);
LocalDateTime endOfDay = LocalDateTime.of(end, LocalTime.MAX);
List<GoodsSalesDTO> salesTop10 = orderMapper.getSalesTop10(beginOfDay, endOfDay);
List<String> names = salesTop10.stream().map(GoodsSalesDTO::getName).collect(Collectors.toList());
String nameList = StringUtils.join(names, ",");
List<Integer> numbers = salesTop10.stream().map(GoodsSalesDTO::getNumber).collect(Collectors.toList());
String numberList = StringUtils.join(numbers, ",");
return SalesTop10ReportVO.builder()
.nameList(nameList)
.numberList(numberList)
.build();
}
orderMapper.xml
<select id="getSalesTop10" resultType="com.sky.dto.GoodsSalesDTO">
select od.name,sum(od.number) number
from order_detail od,orders o
where od.order_id = o.id and o.status = 5
<if test="begin != null">
and o.order_time > #{begin}
if>
<if test="end != null">
and o.order_time < #{end}
if>
group by od.name
order by number desc
limit 0,10
select>