当前位置: 首页 > news >正文

苍穹外卖day12笔记

一、工作台

联系昨天

要实现的功能和昨天差不多,都是查询数据。

所以我们就写出查询语句,然后直接导入已经写好的代码。

实现效果

查询语句

今日数据

营业额

select count(amount) from orders

where status=5 and order_time >= #{begin} and order_time <= #{end}

有效订单

select count(*) from orders

where status=5 and order_time >= #{begin} and order_time <= #{end}

订单完成率

所有订单:

select count(*) from orders

where order_time >= #{begin} and order_time <= #{end}

订单完成率 = 有效订单 / 所有订单

平均客单价

平均客单价 = 营业额 / 有效订单

新增用户数

select count(*) from user

where create_time >= #{begin} and create_time <= #{end}

订单数据

待接单

select count(*) from orders

where status=2 and order_time >=#{begin} and order_time <= #{end}

待派送

select count(*) from orders

where status=3 and order_time >=#{begin} and order_time <= #{end}

已完成

select count(*) from orders

where status=5 and order_time >=#{begin} and order_time <= #{end}

已取消

select count(*) from orders

where status=6 and order_time >=#{begin} and order_time <= #{end}

全部订单

select count(*) from orders

where order_time >=#{begin} and order_time <= #{end}

菜品总览

起售菜品

select count(*) from dish

where status=1

停售菜品

select count(*) from dish

where status=1

套餐总览

起售套餐

select count(*) from setmeal

where status=1

停售套餐

select count(*) from setmeal

where status=0

导入代码

下载好黑马该项目的资料:

然后自己导入。

二、Apache POI

介绍

操作Office文件的包。本文该项目中主要用来读写excel表。

应用场景主要在:交易明细、销量统计、批量数据导入(批量添加)

写入Excel

步骤

  1. 先创建Excel文档/工作簿

  2. 在工作簿中创建表格

  3. 在表格中创建行

  4. 在行中创建单元格

  5. 往单元格中设置数据

  6. 将整个Excel文档写到硬盘上

代码

直接在测试类中写例子的测试的。

@Test
public void testWrite() throws IOException {// 1. 创建整个工作簿XSSFWorkbook workbook = new XSSFWorkbook();// 2. 在工作簿中创建表格XSSFSheet sheet1 = workbook.createSheet("表格1");// 3. 在表格中创建行XSSFRow row_1 = sheet1.createRow(1);// 4. 在行中创建单元格XSSFCell cell_1_0 = row_1.createCell(0);// 5. 网单元格中设置数据cell_1_0.setCellValue("哈哈,我是cell_1_0");// 6. 将整个Excel文档写到硬盘上FileOutputStream fos = new FileOutputStream("D:/a.xlsx");workbook.write(fos);// 7. 释放资源fos.close();workbook.close();
}

读出Excel

步骤

  1. 先创建工作簿,关联本地Excel文档

  2. 从工作簿中获取表格

  3. 从表格中获取行

  4. 从行中获取单元格

  5. 从单元格中获取数据

代码

@Test
public void testRead() throws IOException {// 1. 先创建工作簿,关联本地Excel文档XSSFWorkbook workbook = new XSSFWorkbook("D:/a.xlsx");// 2. 从工作簿中获取表格XSSFSheet sheet = workbook.getSheetAt(0);// 3. 从表格中获取行XSSFRow row4 = sheet.getRow(3);// 4. 从行中获取单元格 以及 5. 从单元格中获取数据String name = row4.getCell(0).getStringCellValue();String age = row4.getCell(1).getStringCellValue();System.out.println(name);System.out.println(age);XSSFRow row5 = sheet.getRow(4);System.out.println(row5.getCell(0).getStringCellValue());System.out.println(row5.getCell(1).getNumericCellValue());workbook.close();
}

三、导出运营数据

需求分析

导出近30天的运营数据。

步骤

  1. 读取Excel模版到内存中。

  2. 准备运营数据

  3. 将数据写到Excel模板中。

  4. 将Excel文档响应回浏览器(文件下载)

代码

Controller:

@GetMapping("/export")
@ApiOperation("导出运营数据报表")
public String export(HttpServletResponse response) throws IOException {reportService.exportBusinessData(response);return "OK";
}

Service:

@Override
public void exportBusinessData(HttpServletResponse response) throws IOException{InputStream is = ClassLoader.getSystemResourceAsStream("运营数据报表模板.xlsx");XSSFWorkbook workbook = new XSSFWorkbook(is);LocalDate begin = LocalDate.now().plusDays(-30);LocalDate end = LocalDate.now().plusDays(-1);BusinessDataVO businessDataVO = workspaceService.getBusinessData(LocalDateTime.of(begin, LocalTime.MIN),LocalDateTime.of(end, LocalTime.MAX));XSSFSheet sheet = workbook.getSheetAt(0);sheet.getRow(1).getCell(1).setCellValue("时间:" + begin + "至" + end);XSSFRow row4 = sheet.getRow(3);row4.getCell(2).setCellValue(businessDataVO.getTurnover());row4.getCell(4).setCellValue(businessDataVO.getOrderCompletionRate());row4.getCell(6).setCellValue(businessDataVO.getNewUsers());XSSFRow row5 = sheet.getRow(4);row5.getCell(2).setCellValue(businessDataVO.getValidOrderCount());row5.getCell(4).setCellValue(businessDataVO.getUnitPrice());int i = 0;while (begin.compareTo(end) <= 0) {BusinessDataVO businessData = workspaceService.getBusinessData(LocalDateTime.of(begin, LocalTime.MIN),LocalDateTime.of(begin, LocalTime.MAX));XSSFRow row = sheet.getRow(7 + i++);row.getCell(1).setCellValue(begin.toString());row.getCell(2).setCellValue(businessData.getTurnover());row.getCell(3).setCellValue(businessData.getValidOrderCount());row.getCell(4).setCellValue(businessData.getOrderCompletionRate());row.getCell(5).setCellValue(businessData.getUnitPrice());row.getCell(6).setCellValue(businessData.getNewUsers());begin = begin.plusDays(1);}workbook.write(response.getOutputStream());}

注意的点

ClassLoader能加载的文件位置

ClassLoader能加载的文件位置在resources下。

放入resources后需要的操作

需要用maven构建管理的complie编译一下,才能保证类加载器ClassLoader加载到。

创建的POI与Office对应的下标

下标中getRow(0)与getCell(1)对应的分别是第一列第2行的数据。

http://www.lryc.cn/news/119336.html

相关文章:

  • Prometheus技术文档-基本使用-配置文件全解!!!!!
  • 宋浩高等数学笔记(十一)曲线积分与曲面积分
  • 安卓如何快速定位native内存泄露。
  • redis学习笔记(二)
  • 不侵入代码的rem适配,支持桌面缩放,vue2的适配方案,包含echarts适配
  • 智能合约 -- 常规漏洞分析 + 实例
  • JavaScript 操作历史记录api怎样使用 JavaScript
  • Spring 容器
  • 【腾讯云Cloud Studio实战训练营】使用React快速构建点餐H5
  • Java培训课程哪个品牌好?快拿小本本记好
  • leetcode19. 删除链表的倒数第 N 个结点
  • c51单片机串行通信示例代码(单片机--单片机通信)(附带proteus线路图)
  • UML之四种事物
  • 盒子模型和新盒子模型及区别
  • 移动端Vue组件库-vant
  • Java课题笔记~ JSP内置对象
  • 数据结构笔记--链表经典高频题
  • Android Ble蓝牙App(三)特性和属性
  • 日常BUG——使用Long类型作id,后端返回给前段后精度丢失问题
  • 【C++初阶】string类的常见基本使用
  • 【ArcGIS Pro二次开发】(60):按图层导出布局
  • docker-desktop数据目录迁移
  • 03.利用Redis实现缓存功能---解决缓存穿透版
  • 全景图!最近20年,自然语言处理领域的发展
  • Mybatis参数传递
  • 手动实现 Spring 底层机制 实现任务阶段一编写自己 Spring 容器-准备篇【2】
  • 部署模型并与 TVM 集成
  • Android Navigation 导航切换fragment用法
  • Anaconda Prompt使用pip安装PyQt5-tools后无法打开Spyder或闪退
  • 【jvm】jvm整体结构(hotspot)