修改台桌统计导出数据

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

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);
}
}