台桌数据导出修改
This commit is contained in:
parent
0c5192e86e
commit
8e0f237e2e
|
|
@ -23,9 +23,11 @@ import cn.hutool.poi.excel.ExcelUtil;
|
|||
import cn.hutool.poi.excel.ExcelWriter;
|
||||
import cn.ysk.cashier.exception.BadRequestException;
|
||||
import org.apache.poi.ss.usermodel.*;
|
||||
import org.apache.poi.ss.util.CellRangeAddress;
|
||||
import org.apache.poi.util.IOUtils;
|
||||
import org.apache.poi.xssf.streaming.SXSSFSheet;
|
||||
import org.apache.poi.xssf.usermodel.XSSFSheet;
|
||||
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
|
@ -37,10 +39,7 @@ import java.io.*;
|
|||
import java.security.MessageDigest;
|
||||
import java.text.DecimalFormat;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.ConcurrentLinkedQueue;
|
||||
|
||||
/**
|
||||
|
|
@ -238,6 +237,51 @@ 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
|
||||
*
|
||||
|
|
|
|||
|
|
@ -16,6 +16,7 @@ import cn.ysk.cashier.utils.DateUtil;
|
|||
import cn.ysk.cashier.utils.FileUtil;
|
||||
import cn.ysk.cashier.vo.*;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.beust.ah.A;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
|
@ -558,38 +559,77 @@ public class SummaryServiceImpl implements SummaryService {
|
|||
Map<String, List<TbOrderSalesCountByTable>> countByTableMap = countByTables.stream()
|
||||
.collect(Collectors.groupingBy(TbOrderSalesCountByTable::getTableId));
|
||||
|
||||
ConcurrentLinkedQueue<Map<String, Object>> list = new ConcurrentLinkedQueue<>();
|
||||
|
||||
ArrayList<Integer> mergeRowIndex = new ArrayList<>();
|
||||
// 比较 shopTableSeleInfoDto 的 startTime 和 endTime 是不是同一天
|
||||
boolean sameDay = cn.hutool.core.date.DateUtil.isSameDay(shopTableSeleInfoDto.getStartTime(), shopTableSeleInfoDto.getEndTime());
|
||||
|
||||
String queryDate = cn.hutool.core.date.DateUtil.format(shopTableSeleInfoDto.getStartTime(), "yyyy-MM-dd");
|
||||
if (!sameDay) {
|
||||
queryDate += " 至 " + cn.hutool.core.date.DateUtil.format(shopTableSeleInfoDto.getEndTime(), "yyyy-MM-dd");
|
||||
}
|
||||
|
||||
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("开始时间", shopTableSeleInfoDto.getStartTime());
|
||||
map.put("结束时间", shopTableSeleInfoDto.getEndTime());
|
||||
map.put("区域名称", all.getAreaName());
|
||||
map.put("桌台名称", all.getTableName());
|
||||
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.getSalesAmount());
|
||||
map.put("单价", table.getPrice());
|
||||
map.put("金额", table.getSalesAmount());
|
||||
map.put("销售额", total);
|
||||
map.put("退单量", table.getRefNum());
|
||||
map.put("退单额", table.getRefAmount());
|
||||
list.add(map);
|
||||
rowIndex++;
|
||||
}
|
||||
|
||||
if (!tables.isEmpty()) {
|
||||
if (mergeRowIndex.isEmpty()) {
|
||||
mergeRowIndex.add(tables.size());
|
||||
}else {
|
||||
mergeRowIndex.add(mergeRowIndex.get(mergeRowIndex.size() - 1) + tables.size());
|
||||
}
|
||||
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));
|
||||
}
|
||||
}
|
||||
FileUtil.downloadExcelAndMerge(list, 4, response, mergeRowIndex);
|
||||
|
||||
List<String> keyList = new ArrayList<>();
|
||||
keyList.add("日期");
|
||||
keyList.add("台桌");
|
||||
keyList.add("商品分类");
|
||||
keyList.add("商品名称");
|
||||
keyList.add("单位");
|
||||
keyList.add("商品规格");
|
||||
keyList.add("销量");
|
||||
keyList.add("单价");
|
||||
keyList.add("金额");
|
||||
keyList.add("销售额");
|
||||
keyList.add("退单量");
|
||||
keyList.add("退单额");
|
||||
|
||||
FileUtil.downloadAndMergeExcel(list, mergeList, keyList, response);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue