修改台桌统计导出数据

This commit is contained in:
yijiegong 2024-09-20 17:15:05 +08:00
parent 35d66973bc
commit c3597cd28e
5 changed files with 181 additions and 108 deletions

View File

@ -237,51 +237,6 @@ public class FileUtil extends cn.hutool.core.io.FileUtil {
IoUtil.close(out);
}
public static void downloadAndMergeExcel(List<Map<String, Object>> list, List<List<Integer>> mergeData, List<String> keyList, HttpServletResponse response) throws IOException {
String tempPath = SYS_TEM_DIR + IdUtil.fastSimpleUUID() + ".xlsx";
// 创建工作簿
Workbook workbook = new XSSFWorkbook();
// 创建工作表
Sheet sheet = workbook.createSheet("Sheet1");
// 合并单元格从第 0 行第 0 列到第 0 行第 2
// sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 2));
for (List<Integer> mergeDatum : mergeData) {
sheet.addMergedRegion(new CellRangeAddress(mergeDatum.get(0), mergeDatum.get(1), mergeDatum.get(2), mergeDatum.get(3)));
}
Row row0 = sheet.createRow(0);
for (int i = 0; i < keyList.size(); i++) {
Cell cell = row0.createCell(i);
cell.setCellValue(keyList.get(i));
}
for (int i = 0; i < list.size(); i++) {
Map<String, Object> map = list.get(i);
Row row = sheet.createRow(i + 1);
for (int j = 0; j < keyList.size(); j++) {
Cell cell = row.createCell(j);
cell.setCellValue(map.get(keyList.get(j)) == null ? "" : map.get(keyList.get(j)).toString());
}
}
// response为HttpServletResponse对象
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
response.setHeader("Content-Disposition", "attachment;filename=file.xlsx");
try (FileOutputStream outputStream = new FileOutputStream(tempPath);
ServletOutputStream out = response.getOutputStream()) {
workbook.write(outputStream);
workbook.write(out);
} catch (IOException e) {
// 更详细的错误处理
e.printStackTrace();
// 可以考虑返回一个错误响应给客户端
}
}
/**
* 输入标题到excel
*

View File

@ -56,7 +56,6 @@ public class SummaryController {
}
@GetMapping("/table")
@AnonymousGetMapping
private Object shopSummaryTable(@RequestParam Integer shopId,
@RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date startTime,
@RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date endTime) {
@ -64,7 +63,6 @@ public class SummaryController {
}
@PostMapping("/table/download")
@AnonymousPostMapping
private void downloadShopSummaryTable(HttpServletResponse response, @RequestBody ShopTableSaleInfoDto exportRequest) throws IOException {
summaryService.downloadTableSaleInfo(exportRequest, response);
}

View File

@ -0,0 +1,18 @@
package cn.ysk.cashier.dto.shop;
import lombok.Data;
import java.util.List;
import java.util.Map;
/**
* @author yijiegong
*/
@Data
public class ExportTableStsDataDto {
private String dateStr;
private List<Map<String, Object>> data;
private List<List<Integer>> mergeCells;
}

View File

@ -1,6 +1,7 @@
package cn.ysk.cashier.service.impl;
import cn.ysk.cashier.dto.ShopSummaryDto;
import cn.ysk.cashier.dto.shop.ExportTableStsDataDto;
import cn.ysk.cashier.dto.shop.ShopTableSaleInfoDto;
import cn.ysk.cashier.enums.PayTypeEnum;
import cn.ysk.cashier.exception.BadRequestException;
@ -14,6 +15,7 @@ import cn.ysk.cashier.repository.shop.TbShopUserRepository;
import cn.ysk.cashier.service.SummaryService;
import cn.ysk.cashier.utils.DateUtil;
import cn.ysk.cashier.utils.FileUtil;
import cn.ysk.cashier.utils.FileUtils;
import cn.ysk.cashier.vo.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
@ -541,78 +543,94 @@ public class SummaryServiceImpl implements SummaryService {
@Override
public void downloadTableSaleInfo(ShopTableSaleInfoDto shopTableSaleInfoDto, HttpServletResponse response) throws IOException {
Date now = new Date();
if (shopTableSaleInfoDto.getStartTime() == null) {
shopTableSaleInfoDto.setStartTime(DateUtil.toDate(DateUtil.fromTimeStamp(1704038400L)));
shopTableSaleInfoDto.setStartTime(cn.hutool.core.date.DateUtil.beginOfMonth(now));
}
if (shopTableSaleInfoDto.getEndTime() == null) {
shopTableSaleInfoDto.setEndTime(new Date());
shopTableSaleInfoDto.setEndTime(cn.hutool.core.date.DateUtil.endOfDay(now));
}
List<ShopTableSaleInfoVo> infoVos = selectSummaryTable(shopTableSaleInfoDto.getShopId(), shopTableSaleInfoDto.getStartTime(), shopTableSaleInfoDto.getEndTime());
infoVos.add(new ShopTableSaleInfoVo(99999, shopTableSaleInfoDto.getShopId(), null, "", "收银台", null, null, null, null));
List<TbOrderSalesCountByTable> countByTables = tbOrderDetailRepository.queryTbOrderSalesCountByTable(shopTableSaleInfoDto.getShopId(), shopTableSaleInfoDto.getStartTime(), shopTableSaleInfoDto.getEndTime());
Map<String, List<TbOrderSalesCountByTable>> countByTableMap = countByTables.stream()
.collect(Collectors.groupingBy(TbOrderSalesCountByTable::getTableId));
// 比较 shopTableSaleInfoDto startTime endTime 是不是同一天
boolean sameDay = cn.hutool.core.date.DateUtil.isSameDay(shopTableSaleInfoDto.getStartTime(), shopTableSaleInfoDto.getEndTime());
String queryDate = cn.hutool.core.date.DateUtil.format(shopTableSaleInfoDto.getStartTime(), "yyyy-MM-dd");
if (!sameDay) {
queryDate += "" + cn.hutool.core.date.DateUtil.format(shopTableSaleInfoDto.getEndTime(), "yyyy-MM-dd");
// 比较开始和结束日期 相差 天数
long betweenDay = cn.hutool.core.date.DateUtil.betweenDay(shopTableSaleInfoDto.getStartTime(), shopTableSaleInfoDto.getEndTime(), true);
if (betweenDay > 31) {
throw new BadRequestException("导出数据不能超过31天");
}
List<Map<String, Object>> list = new ArrayList<>();
List<ExportTableStsDataDto> result = new ArrayList<>();
List<List<Integer>> mergeList = new ArrayList<>();
Map<String, Integer> tableStartIndexMap = new HashMap<>();
int rowIndex = 1;
for (ShopTableSaleInfoVo all : infoVos) {
List<TbOrderSalesCountByTable> tables = countByTableMap.get(all.getTableCode());
if (tables == null) {
continue;
for (int i = 0; i <= betweenDay; i++) {
Date date = cn.hutool.core.date.DateUtil.offsetDay(shopTableSaleInfoDto.getStartTime(), i);
String dateStr = cn.hutool.core.date.DateUtil.format(date, "yyyy-MM-dd");
ExportTableStsDataDto dataDto = new ExportTableStsDataDto();
dataDto.setDateStr(dateStr);
Date startTime = cn.hutool.core.date.DateUtil.beginOfDay(date);
Date endTime = cn.hutool.core.date.DateUtil.endOfDay(date);
List<ShopTableSaleInfoVo> infoVos = selectSummaryTable(shopTableSaleInfoDto.getShopId(), startTime, endTime);
infoVos.add(new ShopTableSaleInfoVo(99999, shopTableSaleInfoDto.getShopId(), null, "", "收银台", null, null, null, null));
List<TbOrderSalesCountByTable> countByTables = tbOrderDetailRepository.queryTbOrderSalesCountByTable(shopTableSaleInfoDto.getShopId(), startTime, endTime);
Map<String, List<TbOrderSalesCountByTable>> countByTableMap = countByTables.stream()
.collect(Collectors.groupingBy(TbOrderSalesCountByTable::getTableId));
List<Map<String, Object>> list = new ArrayList<>();
List<List<Integer>> mergeList = new ArrayList<>();
Map<String, Integer> tableStartIndexMap = new HashMap<>();
int rowIndex = 1;
for (ShopTableSaleInfoVo all : infoVos) {
List<TbOrderSalesCountByTable> tables = countByTableMap.get(all.getTableCode());
if (tables == null) {
continue;
}
BigDecimal total = BigDecimal.ZERO;
for (TbOrderSalesCountByTable table : tables) {
total = total.add(table.getSalesAmount().abs());
}
String tableCode = all.getTableName().toString();
if (!tableStartIndexMap.containsKey(tableCode)) {
tableStartIndexMap.put(tableCode, rowIndex);
}
for (TbOrderSalesCountByTable table : tables) {
Map<String, Object> map = new LinkedHashMap<>();
map.put("日期", dateStr);
map.put("台桌", all.getTableName());
map.put("商品分类", table.getCateName());
map.put("商品名称", table.getProductName());
map.put("单位", table.getUnitName());
map.put("商品规格", table.getProductSkuName());
map.put("销量", table.getSalesNum());
map.put("单价", table.getPrice());
map.put("金额", table.getSalesAmount());
map.put("销售额", total);
map.put("退单量", table.getRefNum());
map.put("退单额", table.getRefAmount());
list.add(map);
rowIndex++;
}
int start = tableStartIndexMap.get(tableCode);
int end = rowIndex - 1;
if (end - start > 0) {
mergeList.add(Arrays.asList(start, end, 0, 0));
mergeList.add(Arrays.asList(start, end, 1, 1));
mergeList.add(Arrays.asList(start, end, 9, 9));
}
}
BigDecimal total = BigDecimal.ZERO;
for (TbOrderSalesCountByTable table : tables) {
total = total.add(table.getSalesAmount().abs());
}
dataDto.setMergeCells(mergeList);
dataDto.setData(list);
String tableCode = all.getTableName().toString();
if (!tableStartIndexMap.containsKey(tableCode)) {
tableStartIndexMap.put(tableCode, rowIndex);
}
for (TbOrderSalesCountByTable table : tables) {
Map<String, Object> map = new LinkedHashMap<>();
map.put("日期", queryDate);
map.put("台桌", all.getTableName());
map.put("商品分类", table.getCateName());
map.put("商品名称", table.getProductName());
map.put("单位", table.getUnitName());
map.put("商品规格", table.getProductSkuName());
map.put("销量", table.getSalesNum());
map.put("单价", table.getPrice());
map.put("金额", table.getSalesAmount());
map.put("销售额", total);
map.put("退单量", table.getRefNum());
map.put("退单额", table.getRefAmount());
list.add(map);
rowIndex++;
}
int start = tableStartIndexMap.get(tableCode);
int end = rowIndex - 1;
if (end - start > 0) {
mergeList.add(Arrays.asList(start, end, 0, 0));
mergeList.add(Arrays.asList(start, end, 1, 1));
mergeList.add(Arrays.asList(start, end, 9, 9));
}
result.add(dataDto);
}
List<String> keyList = new ArrayList<>();
@ -629,6 +647,6 @@ public class SummaryServiceImpl implements SummaryService {
keyList.add("退单量");
keyList.add("退单额");
FileUtil.downloadAndMergeExcel(list, mergeList, keyList, response);
FileUtils.downloadTableDataStsToExcel(result, keyList, response);
}
}

View File

@ -0,0 +1,84 @@
package cn.ysk.cashier.utils;
import cn.hutool.core.util.IdUtil;
import cn.ysk.cashier.dto.shop.ExportTableStsDataDto;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import static cn.ysk.cashier.utils.FileUtil.SYS_TEM_DIR;
/**
* @author yijiegong
*/
public class FileUtils {
/**
* 该方法用于导出台桌数据统计不通用
*/
public static void downloadTableDataStsToExcel(List<ExportTableStsDataDto> dataList, List<String> keyList, HttpServletResponse response) throws IOException {
// String tempPath = SYS_TEM_DIR + IdUtil.fastSimpleUUID() + ".xlsx";
// 创建工作簿
Workbook workbook = new XSSFWorkbook();
for (ExportTableStsDataDto exportTableStsDataDto : dataList) {
Sheet sheet = workbook.createSheet(exportTableStsDataDto.getDateStr());
for (List<Integer> mergeDatum : exportTableStsDataDto.getMergeCells()) {
sheet.addMergedRegion(new CellRangeAddress(mergeDatum.get(0), mergeDatum.get(1), mergeDatum.get(2), mergeDatum.get(3)));
}
Row row0 = sheet.createRow(0);
for (int i = 0; i < keyList.size(); i++) {
Cell cell = row0.createCell(i);
cell.setCellValue(keyList.get(i));
}
for (int i = 0; i < exportTableStsDataDto.getData().size(); i++) {
Map<String, Object> map = exportTableStsDataDto.getData().get(i);
Row row = sheet.createRow(i + 1);
for (int j = 0; j < keyList.size(); j++) {
Cell cell = row.createCell(j);
String value = map.get(keyList.get(j)) == null ? "" : map.get(keyList.get(j)).toString();
cell.setCellValue(value);
if (j == 10 && !"0".equals(value)) {
setCellBackground(cell, IndexedColors.YELLOW, workbook);
}
if (j == 11 && !"0.00".equals(value)) {
setCellBackground(cell, IndexedColors.YELLOW, workbook);
}
}
}
}
// response为HttpServletResponse对象
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
response.setHeader("Content-Disposition", "attachment;filename=file.xlsx");
try (ServletOutputStream out = response.getOutputStream()) {
//
// workbook.write(outputStream);
workbook.write(out);
out.flush();
} catch (IOException e) {
// 更详细的错误处理
e.printStackTrace();
// 可以考虑返回一个错误响应给客户端
}
}
private static void setCellBackground(Cell cell, IndexedColors color, Workbook workbook) {
CellStyle style = workbook.createCellStyle();
style.setFillForegroundColor(color.getIndex());
style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
cell.setCellStyle(style);
}
}