文章目录
一.Apache ECharts
Apache ECharts 是一款基于 Javascript 的数据可视化图表库,提供直观,生动,可交互,可个性化定制的数据可视化图表。
总结:使用Echarts,重点在于研究当前图表所需的数据格式。通常是需要后端提供符合格式要求的动态数据,然后响应给前端来展示图表。
二.营业额统计
1.需求分析和设计
业务规则:
1.营业额指订单状态为已完成的订单金额合计
2.基于可视化报表的折线图展示营业额数据,X轴为日期,Y轴为营业额
3.根据时间选择区间,展示每天的营业额数据
接口设计:
2.代码开发
根据接口定义创建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) {
return Result.success(reportService.getTurnover(begin, end));
}
}
创建ReportService接口,声明getTurnover方法:
public interface ReportService {
/**
* 根据时间区间统计营业额
* @param begin
* @param end
* @return
*/
TurnoverReportVO getTurnover(LocalDate begin, LocalDate end);
}
创建ReportServiceImpl实现类,实现getTurnover方法
/**
* 根据时间区间统计营业额
*
* @param begin
* @param end
* @return
*/
public TurnoverReportVO getTurnover(LocalDate begin, LocalDate end) {
List<LocalDate> dateList = new ArrayList<>();
dateList.add(begin);
while (!begin.equals(end)) {
begin = begin.plusDays(1);
dateList.add(begin);
}
List<Double> turnoverList = new ArrayList<>();
for (LocalDate date : dateList) {
LocalDateTime beginTime = LocalDateTime.of(date, LocalTime.MIN);
LocalDateTime endTime = LocalDateTime.of(date, LocalTime.MAX);
Map map = new HashMap();
map.put("status", Orders.COMPLETED);
map.put("begin", beginTime);
map.put("end", endTime);
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接口声明sumByMap方法:
/**
* 根据动态条件统计营业额
* @param map
* @return
*/
Double sumByMap(Map map);
在OrderMapper.xml文件中编写动态SQL:
<select id="sumByMap" resultType="java.lang.Double">
select sum(amount) from orders
<where>
<if test="status != null">
and status=#{status}
</if>
<if test="begin != null">
and order_time >= #{begin}
</if>
<if test="end != null">
and order_time <= #{end}
</if>
</where>
</select>
三.用户统计
1.需求分析和设计
业务规则:
1.基于可视化报表的折线图展示用户数据,X轴为日期,Y轴为用户数
2.根据时间选择区间,展示每天的用户总量和新增用户量数据
接口设计:
2.代码开发
根据接口定义,在ReportController中创建userStatistics方法:
/**
* 用户数据统计
* @param begin
* @param end
* @return
*/
@GetMapping("/userStatistics")
@ApiOperation("用户数据统计")
public Result<UserReportVO> userStatistics(
@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end) {
return Result.success(reportService.getUserStatistics(begin,end));
}
在ReportService接口中声明getUserStatistics方法:
/**
* 根据时间区间统计用户数量
* @param begin
* @param end
* @return
*/
UserReportVO getUserStatistics(LocalDate begin, LocalDate end);
在ReportServiceImpl实现类中实现getUserStatistics方法
/**
* 根据时间区间统计用户数量
*
* @param begin
* @param end
* @return
*/
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);
}
List<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);
Map map = new HashMap();
map.put("end", endTime);
Integer totalUser = userMapper.countByMap(map);
map.put("begin",beginTime);
Integer newUser=userMapper.countByMap(map);
totalUserList.add(totalUser);
newUserList.add(newUser);
}
return UserReportVO.builder()
.dateList(StringUtils.join(dateList,","))
.totalUserList(StringUtils.join(totalUserList,","))
.newUserList(StringUtils.join(newUserList,","))
.build();
}
在UserMapper接口中声明countByMap方法:
/**
* 根据动态条件统计用户数量
* @param map
* @return
*/
Integer countByMap(Map map);
在UserMapper.xml文件中编写动态SQL:
<select id="countByMap" 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>
四.订单统计
1.需求分析和设计
业务规则:
1.有效订单指状态为 “已完成” 的订单
2.基于可视化报表的折线图展示订单数据,X轴为日期,Y轴为订单数量
3.根据时间选择区间,展示每天的订单总数和有效订单数
4.展示所选时间区间内的有效订单数、总订单数、订单完成率,订单完成率 = 有效订单数 / 总订单数 * 100%
接口设计:
2.代码开发
根据订单统计接口的返回结果设计VO:
在ReportController中根据订单统计接口创建orderStatistics方法:
/**
* 订单数据统计
*
* @return
*/
@GetMapping("/ordersStatistics")
@ApiOperation("订单数据统计")
public Result<OrderReportVO> orderStatistics(
@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end) {
return Result.success(reportService.getOrderStatistics(begin, end));
}
在ReportService接口中声明getOrderStatistics方法:
/**
* 根据时间区间统计订单数量
* @param begin
* @param end
* @return
*/
OrderReportVO getOrderStatistics(LocalDate begin, LocalDate end);
在ReportServiceImpl实现类中实现getOrderStatistics方法和私有方法getOrderCount
/**
* 根据时间区间统计订单数量
*
* @param begin
* @param end
* @return
*/
public OrderReportVO getOrderStatistics(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> validOrderCountList = new ArrayList<>();
for (LocalDate date : dateList) {
LocalDateTime beginTime = LocalDateTime.of(date, LocalTime.MIN);
LocalDateTime endTime = LocalDateTime.of(date, LocalTime.MAX);
Integer validOrderCount = getOrderCount(beginTime, endTime, Orders.COMPLETED);
Integer orderCount = getOrderCount(beginTime, endTime, null);
validOrderCountList.add(validOrderCount);
orderCountList.add(orderCount);
}
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, ","))
.totalOrderCount(totalOrderCount)
.validOrderCount(validOrderCount).orderCompletionRate(orderCompletionRate)
.build();
}
/**
* 根据时间区间统计指定状态的订单数量
*
* @param beginTime
* @param endTime
* @return
*/
private Integer getOrderCount(LocalDateTime beginTime, LocalDateTime endTime, Integer status) {
Map map = new HashMap();
map.put("status", status);
map.put("begin", beginTime);
map.put("end", endTime);
return orderMapper.countByMap(map);
}
在OrderMapper接口中声明countByMap方法:
/**
* 根据动态条件统计订单数量
* @param map
* @return
*/
Integer countByMap(Map map);
在OrderMapper.xml文件中编写动态SQL:
<select id="countByMap" resultType="java.lang.Integer">
select count(id) from orders
<where>
<if test="status != null">
and status=#{status}
</if>
<if test="begin != null">
and order_time >= #{begin}
</if>
<if test="end != null">
and order_time <= #{end}
</if>
</where>
</select>
五.销量排名Top10
1.需求分析和设计
业务规则:
1.根据时间选择区间,展示销量前10的商品(包括菜品和套餐)
2.基于可视化报表的柱状图降序展示商品销量
3.此处的销量为商品销售的份数
接口设计:
2.代码开发
在ReportController中根据销量排名接口创建top10方法:
/**
* 销量排名统计
*
* @return
*/
@GetMapping("/top10")
@ApiOperation("销量排名统计")
public Result<SalesTop10ReportVO> top10(
@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end) {
return Result.success(reportService.getSalesTop10(begin,end));
}
在ReportService接口中声明getSalesTop10方法:
/**
* 查询指定时间区间内的销量排名top10
* @param begin
* @param end
* @return
*/
SalesTop10ReportVO getSalesTop10(LocalDate begin, LocalDate end);
在ReportServiceImpl实现类中实现getSalesTop10方法:
/**
* 查询指定时间区间内的销量排名top10
* @param begin
* @param end
* @return
*/
public SalesTop10ReportVO getSalesTop10(LocalDate begin, LocalDate end) {
LocalDateTime beginTime = LocalDateTime.of(begin, LocalTime.MIN);
LocalDateTime endTime = LocalDateTime.of(end, LocalTime.MAX);
List<GoodsSalesDTO> goodsSalesDTOList=orderMapper.getSalesTop10(beginTime,endTime);
String nameList=StringUtils.join(goodsSalesDTOList.stream().map(GoodsSalesDTO::getName).collect(Collectors.toList()),",");
String numberList=StringUtils.join(goodsSalesDTOList.stream().map(GoodsSalesDTO::getNumber).collect(Collectors.toList()),",");
return SalesTop10ReportVO.builder()
.nameList(nameList)
.numberList(numberList)
.build();
}
在OrderMapper接口中声明getSalesTop10方法:
/**
* 查询商品销量排名
* @param begin
* @param end
* @return
*/
List<GoodsSalesDTO> getSalesTop10(LocalDateTime begin, LocalDateTime end);
在OrderMapper.xml文件中编写动态SQL:
<select id="getSalesTop10" resultType="com.sky.dto.GoodsSalesDTO">
select od.name name,sum(od.number) number from order_detail od,orders o
where o.id=od.order_id
and o.status=5
<if test="begin != null">
and order_time >= #{begin}
</if>
<if test="end != null">
and order_time <= #{end}
</if>
group by name
order by number desc
limit 0,10
</select>
这段sql语句有点肥复杂,可以反复看下
功能测试
全部采用前后端联调测试即可