目录
一、工作台模块
1、查询今日运营数据 - GET接口
(1)需求分析
(2)代码开发
2、查询今日运营数据 - GET接口
(1)需求分析
(2)代码开发
3、查询菜品总览 - GET接口
(1)需求分析
(2)代码开发
4、查询套餐总览 - GET接口
(1)需求分析
(2)代码开发
二、Excel表格导出
1、Apache POI
(1)入门案例
2、导出Excel表格模块
(1)需求分析
(2)代码开发
【1】导入excel模板文件
【2】controller层
【3】service层
一、工作台模块
1、查询今日运营数据 - GET接口
(1)需求分析
(2)代码开发
【1】controller层
1 /** 2 * 工作台今日数据查询 3 * @return 4 */ 5 @GetMapping("/businessData") 6 @ApiOperation("工作台今日数据查询") 7 public Result<BusinessDataVO> businessData(){ 8 //获得当天的开始时间 9 LocalDateTime begin = LocalDateTime.now().with(LocalTime.MIN); 10 //获得当天的结束时间 11 LocalDateTime end = LocalDateTime.now().with(LocalTime.MAX); 12 13 BusinessDataVO businessDataVO = workspaceService.getBusinessData(begin, end); 14 return Result.success(businessDataVO); 15 }【2】service层
- 这里接口都可以复用之前开发过的
- 我们需要返回的数据有:
- 订单完成率 = 有效订单数 ÷ 订单总数
- 平均客单价 = 营业额 ÷ 有效订单数
- 营业额
- 有效订单数
- 新增用户数
- 那我们需要的数据就是【订单总数】【有效订单数】【营业额】【新增用户数】,这些都是之前统计模块开发的mapper接口,直接复用即可
1 /** 2 * 根据时间段统计营业数据 3 * @param begin 4 * @param end 5 * @return 6 */ 7 public BusinessDataVO getBusinessData(LocalDateTime begin, LocalDateTime end) { 8 /** 9 * 营业额:当日已完成订单的总金额 10 * 有效订单:当日已完成订单的数量 11 * 订单完成率:有效订单数 / 总订单数 12 * 平均客单价:营业额 / 有效订单数 13 * 新增用户:当日新增用户的数量 14 */ 15 16 Map map = new HashMap(); 17 map.put("begin",begin); 18 map.put("end",end); 19 20 //查询总订单数 21 Integer totalOrderCount = orderMapper.countByMap(map); 22 23 map.put("status", Orders.COMPLETED); 24 //营业额 25 Double turnover = orderMapper.sumByMap(map); 26 turnover = turnover == null? 0.0 : turnover; 27 28 //有效订单数 29 Integer validOrderCount = orderMapper.countByMap(map); 30 31 Double unitPrice = 0.0; 32 33 Double orderCompletionRate = 0.0; 34 if(totalOrderCount != 0 && validOrderCount != 0){ 35 //订单完成率 36 orderCompletionRate = validOrderCount.doubleValue() / totalOrderCount; 37 //平均客单价 38 unitPrice = turnover / validOrderCount; 39 } 40 41 //新增用户数 42 Integer newUsers = userMapper.countByMap(map); 43 44 return BusinessDataVO.builder() 45 .turnover(turnover) 46 .validOrderCount(validOrderCount) 47 .orderCompletionRate(orderCompletionRate) 48 .unitPrice(unitPrice) 49 .newUsers(newUsers) 50 .build(); 51 }
2、查询今日运营数据 - GET接口
(1)需求分析
(2)代码开发
【1】controller层
1 /** 2 * 查询订单管理数据 3 * @return 4 */ 5 @GetMapping("/overviewOrders") 6 @ApiOperation("查询订单管理数据") 7 public Result<OrderOverViewVO> orderOverView(){ 8 9 return Result.success(workspaceService.getOrderOverView()); 10 }【2】service层
1 /** 2 * 查询订单管理数据 3 * 4 * @return 5 */ 6 public OrderOverViewVO getOrderOverView() { 7 Map map = new HashMap(); 8 map.put("begin", LocalDateTime.now().with(LocalTime.MIN)); 9 map.put("status", Orders.TO_BE_CONFIRMED); 10 11 //待接单 12 Integer waitingOrders = orderMapper.countByMap(map); 13 14 //待派送 15 map.put("status", Orders.CONFIRMED); 16 Integer deliveredOrders = orderMapper.countByMap(map); 17 18 //已完成 19 map.put("status", Orders.COMPLETED); 20 Integer completedOrders = orderMapper.countByMap(map); 21 22 //已取消 23 map.put("status", Orders.CANCELLED); 24 Integer cancelledOrders = orderMapper.countByMap(map); 25 26 //全部订单 27 map.put("status", null); 28 Integer allOrders = orderMapper.countByMap(map); 29 30 return OrderOverViewVO.builder() 31 .waitingOrders(waitingOrders) 32 .deliveredOrders(deliveredOrders) 33 .completedOrders(completedOrders) 34 .cancelledOrders(cancelledOrders) 35 .allOrders(allOrders) 36 .build(); 37 }
3、查询菜品总览 - GET接口
(1)需求分析
(2)代码开发
【1】controller层
1 /** 2 * 查询菜品总览 3 * @return 4 */ 5 @GetMapping("/overviewDishes") 6 @ApiOperation("查询菜品总览") 7 public Result<DishOverViewVO> dishOverView(){ 8 9 return Result.success(workspaceService.getDishOverView()); 10 }【2】service层
1 /** 2 * 查询菜品总览 3 * 4 * @return 5 */ 6 public DishOverViewVO getDishOverView() { 7 Map map = new HashMap(); 8 map.put("status", StatusConstant.ENABLE); 9 Integer sold = dishMapper.countByMap(map); 10 11 map.put("status", StatusConstant.DISABLE); 12 Integer discontinued = dishMapper.countByMap(map); 13 14 return DishOverViewVO.builder() 15 .sold(sold) 16 .discontinued(discontinued) 17 .build(); 18 }【3】mapper层
1 /** 2 * 根据条件统计菜品数量 3 * @param map 4 * @return 5 */ 6 Integer countByMap(Map map);【4】mybatis文件
1 <select id="countByMap" resultType="java.lang.Integer"> 2 select count(id) from sky_take_out.dish 3 <where> 4 <if test="status != null">and status = #{status}</if> 5 <if test="categoryId != null">and category_id = #{categoryId}</if> 6 </where> 7 </select>
4、查询套餐总览 - GET接口
(1)需求分析
(2)代码开发
【1】controller层
1 /** 2 * 查询套餐总览 3 * @return 4 */ 5 @GetMapping("/overviewSetmeals") 6 @ApiOperation("查询套餐总览") 7 public Result<SetmealOverViewVO> setmealOverView(){ 8 return Result.success(workspaceService.getSetmealOverView()); 9 }【2】service层
1 /** 2 * 查询套餐总览 3 * 4 * @return 5 */ 6 public SetmealOverViewVO getSetmealOverView() { 7 Map map = new HashMap(); 8 map.put("status", StatusConstant.ENABLE); 9 Integer sold = setmealMapper.countByMap(map); 10 11 map.put("status", StatusConstant.DISABLE); 12 Integer discontinued = setmealMapper.countByMap(map); 13 14 return SetmealOverViewVO.builder() 15 .sold(sold) 16 .discontinued(discontinued) 17 .build(); 18 }【3】mapper层
1 /** 2 * 根据条件统计套餐数量 3 * @param map 4 * @return 5 */ 6 Integer countByMap(Map map);【4】mybatis文件
1 <select id="countByMap" resultType="java.lang.Integer"> 2 select count(id) from sky_take_out.setmeal 3 <where> 4 <if test="status != null">and status = #{status}</if> 5 <if test="categoryId != null">and category_id = #{categoryId}</if> 6 </where> 7 </select>
工作台模块开发完毕!

二、Excel表格导出
1、Apache POI
Apache POI 是一个开源的Java库,用于读写Microsoft Office格式文件,它提供了API来操作Excel、Word、PowerPoint等文件。
(1)入门案例
【1】导入maven
1 <dependency> 2 <groupId>org.apache.poi</groupId> 3 <artifactId>poi</artifactId> 4 </dependency> 5 <dependency> 6 <groupId>org.apache.poi</groupId> 7 <artifactId>poi-ooxml</artifactId> 8 </dependency>【2】创建测试代码
1/** 2* 使用POI操作Excel文件 3*/ 4public class POITest { 5 6 /** 7 * 通过POI创建Excel文件并且写入文件内容 8 */ 9 public static void write() throws Exception{ 10 //在内存中创建一个Excel文件 11 XSSFWorkbook excel = new XSSFWorkbook(); 12 //在Excel文件中创建一个Sheet页 13 XSSFSheet sheet = excel.createSheet("info"); 14 //在Sheet中创建行对象,rownum编号从0开始 15 XSSFRow row = sheet.createRow(1); 16 //创建单元格并且写入文件内容 17 row.createCell(1).setCellValue("姓名"); 18 row.createCell(2).setCellValue("城市"); 19 20 //创建一个新行 21 row = sheet.createRow(2); 22 row.createCell(1).setCellValue("张三"); 23 row.createCell(2).setCellValue("北京"); 24 25 row = sheet.createRow(3); 26 row.createCell(1).setCellValue("李四"); 27 row.createCell(2).setCellValue("南京"); 28 29 //通过输出流将内存中的Excel文件写入到磁盘 30 FileOutputStream out = new FileOutputStream(new File("D:\\info.xlsx")); 31 excel.write(out); 32 33 //关闭资源 34 out.close(); 35 excel.close(); 36 } 37 38 39 /** 40 * 通过POI读取Excel文件中的内容 41 * @throws Exception 42 */ 43 public static void read() throws Exception{ 44 InputStream in = new FileInputStream(new File("D:\\info.xlsx")); 45 46 //读取磁盘上已经存在的Excel文件 47 XSSFWorkbook excel = new XSSFWorkbook(in); 48 //读取Excel文件中的第一个Sheet页 49 XSSFSheet sheet = excel.getSheetAt(0); 50 51 //获取Sheet中最后一行的行号 52 int lastRowNum = sheet.getLastRowNum(); 53 54 for (int i = 1; i <= lastRowNum ; i++) { 55 //获得某一行 56 XSSFRow row = sheet.getRow(i); 57 //获得单元格对象 58 String cellValue1 = row.getCell(1).getStringCellValue(); 59 String cellValue2 = row.getCell(2).getStringCellValue(); 60 System.out.println(cellValue1 + " " + cellValue2); 61 } 62 63 //关闭资源 64 in.close(); 65 excel.close(); 66 } 67 68 public static void main(String[] args) throws Exception { 69 //write(); 70 read(); 71 } 72}
2、导出Excel表格模块
(1)需求分析
(2)代码开发
实现步骤:
- 设计 Excel 模板文件
- 查询近30天的运营数据
- 将查询到的运营数据写入模板文件
- 通过输出流将 Excel 文件下载到客户端浏览器
【1】导入excel模板文件
资料包day12中获取运营数据报表模板并导入
【2】controller层
HttpServletResponse response作用:
提供一个输出通道,让服务器能够将生成的Excel文件直接流式传输到客户端浏览器,实现文件下载功能
1 /** 2 * 导出运营数据报表 3 * @param response 4 */ 5 @GetMapping("/export") 6 @ApiOperation("导出运营数据报表") 7 //HttpServletResponse response作用 8 //提供一个输出通道,让服务器能够将生成的Excel文件直接流式传输到客户端浏览器,实现文件下载功能 9 public void export(HttpServletResponse response){ 10 reportService.exportBusinessData(response); 11 }【3】service层
- 查询数据库获取营业数据
- 通过POI将数据写入excel表
- 基于模板文件创建新excel文件
- 填充时间
- 填充概览数据
- 填充详细数据
- 通过输出流将excel下载到客户端浏览器
1 /** 2 * 导出运营数据报表 3 * @param response 4 */ 5 public void exportBusinessData(HttpServletResponse response) { 6 //1.查询数据库获取营业数据 7 //概览数据包括:营业额、订单完成率、新增用户数、有效订单、平均客单价 8 //上述数据刚好在工作台模块统计过,所以直接调用WorkspaceService即可 9 LocalDate dateBegin = LocalDate.now().minusDays(30); //30天前的日期 10 LocalDate dateEnd = LocalDate.now().minusDays(1);//昨天的日期 11 12 //查询概览数据 13 BusinessDataVO businessDataVO = workspaceService.getBusinessData(LocalDateTime.of(dateBegin, LocalTime.MIN), 14 LocalDateTime.of(dateEnd, LocalTime.MAX)); 15 16 //2.通过POI将数据写入到Excel 17 InputStream in = this.getClass().getClassLoader().getResourceAsStream("template/运营数据报表模板.xlsx"); 18 /** 19 * this.getClass() - 获取当前类的Class对象 20 * .getClassLoader() - 获取类加载器 21 * .getResourceAsStream("template/运营数据报表模板.xlsx") - 从类路径加载指定文件为输入流 22 */ 23 24 try { 25 //基于模板文件创建一个新的excel文件 26 XSSFWorkbook excel = new XSSFWorkbook(in); 27 //获取表格文件的sheet页 28 XSSFSheet sheet = excel.getSheet("Sheet1"); 29 30 //(1)填充时间 31 sheet.getRow(1).getCell(1).setCellValue("时间:"+dateBegin+"至"+dateEnd); 32 33 //(2)填充营业概览数据 34 //获得第4行 35 XSSFRow row = sheet.getRow(3); 36 row.getCell(2).setCellValue(businessDataVO.getTurnover()); 37 row.getCell(4).setCellValue(businessDataVO.getOrderCompletionRate()); 38 row.getCell(6).setCellValue(businessDataVO.getNewUsers()); 39 40 //获得第5行 41 row = sheet.getRow(4); 42 row.getCell(2).setCellValue(businessDataVO.getValidOrderCount()); 43 row.getCell(4).setCellValue(businessDataVO.getUnitPrice()); 44 45 //(3)填充明细数据 46 for (int i = 0; i < 30; i++) { 47 LocalDate date = dateBegin.plusDays(i); 48 //查询某一天的营业数据 49 BusinessDataVO businessData = workspaceService.getBusinessData(LocalDateTime.of(date, LocalTime.MIN), 50 LocalDateTime.of(date, LocalTime.MAX)); 51 52 //获得某一行 53 row = sheet.getRow(i+7); 54 row.getCell(1).setCellValue(date.toString()); 55 row.getCell(2).setCellValue(businessData.getTurnover()); 56 row.getCell(3).setCellValue(businessData.getValidOrderCount()); 57 row.getCell(4).setCellValue(businessData.getOrderCompletionRate()); 58 row.getCell(5).setCellValue(businessData.getUnitPrice()); 59 row.getCell(6).setCellValue(businessData.getNewUsers()); 60 } 61 62 //3.通过输出流将Excel下载到客户端浏览器 63 ServletOutputStream out = response.getOutputStream(); 64 excel.write(out); 65 66 //关闭资源 67 out.close(); 68 excel.close(); 69 70 } catch (IOException e) { 71 throw new RuntimeException(e); 72 } 73 }
导出excel文件成功!

《【项目实战 Day12】springboot + vue 苍穹外卖系统(Apache POI + 工作台模块 + Excel表格导出 完结)》 是转载文章,点击查看原文。









