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

Day12 数据统计-Excel报表

工作台

需求分析和设计

代码导入

package com.sky.controller.admin;import com.sky.result.Result;
import com.sky.service.WorkspaceService;
import com.sky.vo.BusinessDataVO;
import com.sky.vo.DishOverViewVO;
import com.sky.vo.OrderOverViewVO;
import com.sky.vo.SetmealOverViewVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.time.LocalDateTime;
import java.time.LocalTime;/*** 工作台*/
@RestController
@RequestMapping("/admin/workspace")
@Slf4j
@Api(tags = "工作台相关接口")
public class WorkSpaceController {@Autowiredprivate WorkspaceService workspaceService;/*** 工作台今日数据查询* @return*/@GetMapping("/businessData")@ApiOperation("工作台今日数据查询")public Result<BusinessDataVO> businessData(){//获得当天的开始时间LocalDateTime begin = LocalDateTime.now().with(LocalTime.MIN);//获得当天的结束时间LocalDateTime end = LocalDateTime.now().with(LocalTime.MAX);BusinessDataVO businessDataVO = workspaceService.getBusinessData(begin, end);return Result.success(businessDataVO);}/*** 查询订单管理数据* @return*/@GetMapping("/overviewOrders")@ApiOperation("查询订单管理数据")public Result<OrderOverViewVO> orderOverView(){return Result.success(workspaceService.getOrderOverView());}/*** 查询菜品总览* @return*/@GetMapping("/overviewDishes")@ApiOperation("查询菜品总览")public Result<DishOverViewVO> dishOverView(){return Result.success(workspaceService.getDishOverView());}/*** 查询套餐总览* @return*/@GetMapping("/overviewSetmeals")@ApiOperation("查询套餐总览")public Result<SetmealOverViewVO> setmealOverView(){return Result.success(workspaceService.getSetmealOverView());}
}
package com.sky.service.impl;import com.sky.constant.StatusConstant;
import com.sky.entity.Orders;
import com.sky.mapper.DishMapper;
import com.sky.mapper.OrderMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.UserMapper;
import com.sky.service.WorkspaceService;
import com.sky.vo.BusinessDataVO;
import com.sky.vo.DishOverViewVO;
import com.sky.vo.OrderOverViewVO;
import com.sky.vo.SetmealOverViewVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.HashMap;
import java.util.Map;@Service
@Slf4j
public class WorkspaceServiceImpl implements WorkspaceService {@Autowiredprivate OrderMapper orderMapper;@Autowiredprivate UserMapper userMapper;@Autowiredprivate DishMapper dishMapper;@Autowiredprivate SetmealMapper setmealMapper;/*** 根据时间段统计营业数据* @param begin* @param end* @return*/public BusinessDataVO getBusinessData(LocalDateTime begin, LocalDateTime end) {/*** 营业额:当日已完成订单的总金额* 有效订单:当日已完成订单的数量* 订单完成率:有效订单数 / 总订单数* 平均客单价:营业额 / 有效订单数* 新增用户:当日新增用户的数量*/Map map = new HashMap();map.put("begin",begin);map.put("end",end);//查询总订单数Integer totalOrderCount = orderMapper.countByMap(map);map.put("status", Orders.COMPLETED);//营业额Double turnover = orderMapper.sumByMap(map);turnover = turnover == null? 0.0 : turnover;//有效订单数Integer validOrderCount = orderMapper.countByMap(map);Double unitPrice = 0.0;Double orderCompletionRate = 0.0;if(totalOrderCount != 0 && validOrderCount != 0){//订单完成率orderCompletionRate = validOrderCount.doubleValue() / totalOrderCount;//平均客单价unitPrice = turnover / validOrderCount;}//新增用户数Integer newUsers = userMapper.countByMap(map);return BusinessDataVO.builder().turnover(turnover).validOrderCount(validOrderCount).orderCompletionRate(orderCompletionRate).unitPrice(unitPrice).newUsers(newUsers).build();}/*** 查询订单管理数据** @return*/public OrderOverViewVO getOrderOverView() {Map map = new HashMap();map.put("begin", LocalDateTime.now().with(LocalTime.MIN));map.put("status", Orders.TO_BE_CONFIRMED);//待接单Integer waitingOrders = orderMapper.countByMap(map);//待派送map.put("status", Orders.CONFIRMED);Integer deliveredOrders = orderMapper.countByMap(map);//已完成map.put("status", Orders.COMPLETED);Integer completedOrders = orderMapper.countByMap(map);//已取消map.put("status", Orders.CANCELLED);Integer cancelledOrders = orderMapper.countByMap(map);//全部订单map.put("status", null);Integer allOrders = orderMapper.countByMap(map);return OrderOverViewVO.builder().waitingOrders(waitingOrders).deliveredOrders(deliveredOrders).completedOrders(completedOrders).cancelledOrders(cancelledOrders).allOrders(allOrders).build();}/*** 查询菜品总览** @return*/public DishOverViewVO getDishOverView() {Map map = new HashMap();map.put("status", StatusConstant.ENABLE);Integer sold = dishMapper.countByMap(map);map.put("status", StatusConstant.DISABLE);Integer discontinued = dishMapper.countByMap(map);return DishOverViewVO.builder().sold(sold).discontinued(discontinued).build();}/*** 查询套餐总览** @return*/public SetmealOverViewVO getSetmealOverView() {Map map = new HashMap();map.put("status", StatusConstant.ENABLE);Integer sold = setmealMapper.countByMap(map);map.put("status", StatusConstant.DISABLE);Integer discontinued = setmealMapper.countByMap(map);return SetmealOverViewVO.builder().sold(sold).discontinued(discontinued).build();}
}
<select id="countByMap" resultType="java.lang.Integer">
select count(id) from dish
<where>
<if test="status != null"> and status = #{status} </if>
<if test="categoryId != null"> and category_id = #{categoryId} </if>
</where>
</select>
    <select id="countByMap" resultType="java.lang.Integer">select count(id) from sky_take_out.setmeal<where><if test="status != null"> and status = #{status} </if><if test="categoryId != null"> and category_id = #{categoryId} </if></where></select>

功能测试

Apache POI

介绍

入门案例

首先来看写入

package com.sky.test;import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;import java.io.FileNotFoundException;
import java.io.FileOutputStream;public class POITest {/*** 通过 POI 创建 Excel 文件并且写入文件内容*/public static void write() throws Exception {//在文件中创建一个 excel 文件XSSFWorkbook excel = new XSSFWorkbook();//在Excel文件中创建一个 sheet 页XSSFSheet sheet = excel.createSheet("info");//在sheet中创建行对象,rownum 编号从 0 开始XSSFRow row = sheet.createRow(1);//创建单元格并且写入文件内容row.createCell(1).setCellValue("姓名");row.createCell(2).setCellValue("城市");//创建一个新行row = sheet.createRow(2);row.createCell(1).setCellValue("张三");row.createCell(2).setCellValue("北京");//通过输出流将内存中的Excel文件写入到磁盘FileOutputStream fileOutputStream = new FileOutputStream(("D:\\Cangqiong-Waimai\\info.xlsx"));excel.write(fileOutputStream);//关闭资源fileOutputStream.close();excel.close();}public static void main(String[] args) throws Exception {write();}
}

然后来看读出

/*** 通过 POI 读取 Excel 文件中的内容* @throws Exception*/public static void read() throws Exception{InputStream in = new FileInputStream(("D:\\Cangqiong-Waimai\\info.xlsx"));//读取磁盘上已经存在的 Excel 文件XSSFWorkbook excel = new XSSFWorkbook(in);//读取 Excel 文件中的第一个 sheet 页XSSFSheet sheet = excel.getSheetAt(0);//获取 sheet 页中最后一行的行号int lastRowNum = sheet.getLastRowNum();for(int i = 1; i <= lastRowNum; i ++){//获得某一行XSSFRow row = sheet.getRow(i);//获得单元格对象String stringCellValue1 = row.getCell(1).getStringCellValue();String stringCellValue2 = row.getCell(2).getStringCellValue();System.out.println(stringCellValue1 + " " + stringCellValue2);}//关闭资源excel.close();in.close();}

导出运营数据Excel报表

需求分析和设计

注意:当前接口没有返回数据,因为报表导出功能本质上是文件下载,服务端会通过输出流将 Excel 文件下载到客户端浏览器

代码开发

模板文件是已经提供好的

将该文件放在项目中

    /*** 导出运营数据报表*/@GetMapping("/export")@ApiOperation("导出运营数据报表")public void export(HttpServletResponse response){log.info("导出运营数据");reportService.exportBusinessData(response);}
 @Overridepublic void exportBusinessData(HttpServletResponse response) {//1:查询数据库,获取营业数据 -- 查询最近30天LocalDate dateBegin = LocalDate.now().minusDays(30);LocalDate dateEnd = LocalDate.now().minusDays(1);BusinessDataVO businessDataVO = workspaceService.getBusinessData(LocalDateTime.of(dateBegin, LocalTime.MIN), LocalDateTime.of(dateEnd, LocalTime.MAX));//2:通过 POI 将数据写入 Excel 文件中InputStream in = this.getClass().getClassLoader().getResourceAsStream("template/运营数据报表模板.xlsx");try {//基于模板文件创建一个新的 Excel 文件XSSFWorkbook excel = new XSSFWorkbook(in);//获取表格文件的 Sheet 页XSSFSheet sheet = excel.getSheet("Sheet1");//填充数据 -- 时间sheet.getRow(1).getCell(1).setCellValue("时间:" + dateBegin + " 至 " + dateEnd);// 获得第4行XSSFRow row = sheet.getRow(3);row.getCell(2).setCellValue(businessDataVO.getTurnover());row.getCell(4).setCellValue(businessDataVO.getOrderCompletionRate());row.getCell(6).setCellValue(businessDataVO.getNewUsers());// 获得第5行row = sheet.getRow(4);row.getCell(2).setCellValue(businessDataVO.getValidOrderCount());row.getCell(4).setCellValue(businessDataVO.getUnitPrice());//填充明细数据for(int i = 0; i < 30; i ++){LocalDate date = dateBegin.plusDays(i);BusinessDataVO businessData = workspaceService.getBusinessData(LocalDateTime.of(date, LocalTime.MIN), LocalDateTime.of(date, LocalTime.MAX));//获得某一行row = sheet.getRow(7 + i);row.getCell(1).setCellValue(date.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());}//3:通过输出流将 Excel 文件下载到客户端浏览器ServletOutputStream out = response.getOutputStream();excel.write(out);//关闭资源out.close();excel.close();} catch (IOException e) {e.printStackTrace();}}

功能测试

http://www.xdnf.cn/news/18650.html

相关文章:

  • 数据结构——树状数组(Binary Indexed Tree)
  • UE5多人MOBA+GAS 53、测试专属服务器打包和连接,以及配置EOS
  • WiFi有网络但是电脑连不上网是怎么回事?该怎么解决?
  • 云原生高级——K8S总概
  • OpenHands:开源AI软件开发代理平台的革命性突破
  • 2025最新版mgg格式转MP3,mflac转mp3,mgg格式如何转mp3?
  • setup 语法糖核心要点
  • Windows应急响应一般思路(一)
  • MySQL 高级主题:索引优化、ORM 与数据库迁移
  • More Effective C++ 条款02:最好使用C++转型操作符
  • 【0基础PS】蒙版与剪贴蒙版详解
  • NoCode-bench:自然语言驱动功能添加的评估新基准
  • 3.4 缩略词抽取
  • 表格识别技术:通过图像处理与深度学习,将非结构化表格转化为可编辑结构化数据,推动智能化发展
  • Vue Teleport 原理解析与React Portal、 Fragment 组件
  • GEO优化专家孟庆涛发布:《GEO内容优化的四大黄金标准》
  • 普中烧录软件 PZISP,打不开,提示“应用程序无法启动,因为应用程序并行配置不正确.....”
  • 学习嵌入式第三十五天
  • Linux应用软件编程---网络编程1(目的、网络协议、网络配置、UDP编程流程)
  • APP Usage『安卓』:比系统自带强10倍!手机应用使用时长精确到秒
  • MySQL - 视图,事务和索引
  • java8 findAny()、findFirst()空指针NullPointerException问题
  • ​维基框架 (Wiki Framework) 1.1.0 版本发布​ 提供多模型AI辅助开发
  • 图像指针:高效处理像素数据的核心工具
  • Linux虚拟机安装FTP
  • AtCoder Beginner Contest 419(ABCDEF)
  • Python Flask快速实现163邮箱发送验证码
  • 防火墙双机热备
  • 数据结构之深入探索快速排序
  • docker 打包