统计数据导出
This commit is contained in:
@@ -1,11 +1,11 @@
|
||||
package com.czg.service.account.service.impl;
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.http.HttpUtil;
|
||||
import com.alibaba.fastjson2.JSONObject;
|
||||
import com.czg.account.service.GeoService;
|
||||
import com.czg.exception.CzgException;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.client.RestClient;
|
||||
|
||||
@@ -49,7 +49,7 @@ public class GeoServiceImpl implements GeoService {
|
||||
param.put("key", "7a7f2e4790ea222660a027352ee3af39");
|
||||
param.put("keywords", keywords);
|
||||
param.put("subdistrict", "1");
|
||||
if (StringUtils.isNotBlank(subdistrict)) {
|
||||
if (StrUtil.isNotBlank(subdistrict)) {
|
||||
param.put("subdistrict", "2");
|
||||
}
|
||||
param.put("extensions", "base");
|
||||
|
||||
@@ -7,13 +7,13 @@ import com.czg.config.RedisCst;
|
||||
import com.czg.exception.CzgException;
|
||||
import com.czg.sa.StpKit;
|
||||
import com.czg.service.RedisService;
|
||||
import com.czg.utils.CzgRandomUtils;
|
||||
import com.mybatisflex.core.query.QueryWrapper;
|
||||
import com.mybatisflex.spring.service.impl.ServiceImpl;
|
||||
import com.czg.account.entity.ShopSong;
|
||||
import com.czg.account.service.ShopSongService;
|
||||
import com.czg.service.account.mapper.ShopSongMapper;
|
||||
import jakarta.annotation.Resource;
|
||||
import org.apache.commons.lang3.RandomStringUtils;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
/**
|
||||
@@ -23,7 +23,7 @@ import org.springframework.stereotype.Service;
|
||||
* @since 2025-03-01
|
||||
*/
|
||||
@Service
|
||||
public class ShopSongServiceImpl extends ServiceImpl<ShopSongMapper, ShopSong> implements ShopSongService{
|
||||
public class ShopSongServiceImpl extends ServiceImpl<ShopSongMapper, ShopSong> implements ShopSongService {
|
||||
@Resource
|
||||
private RedisService redisService;
|
||||
|
||||
@@ -49,18 +49,12 @@ public class ShopSongServiceImpl extends ServiceImpl<ShopSongMapper, ShopSong> i
|
||||
public String getSongUrl(Long shopId) {
|
||||
String code;
|
||||
String key = RedisCst.getSongUrlKey(shopId);
|
||||
if(redisService.hasKey(key)){
|
||||
if (redisService.hasKey(key)) {
|
||||
code = (String) redisService.get(key);
|
||||
}else {
|
||||
code = RandomStringUtils.randomAlphanumeric(12);
|
||||
} else {
|
||||
code = CzgRandomUtils.randomString(12);
|
||||
redisService.set(key, code);
|
||||
}
|
||||
return code;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
String string = RandomStringUtils.randomAlphanumeric(12);
|
||||
System.out.println(string);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,16 +1,21 @@
|
||||
package com.czg.service.order.service.impl;
|
||||
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import com.alibaba.excel.write.handler.SheetWriteHandler;
|
||||
import com.czg.excel.ExcelExportUtil;
|
||||
import com.czg.excel.SheetData;
|
||||
import com.czg.order.param.TableSummaryParam;
|
||||
import com.czg.order.service.TableSummaryService;
|
||||
import com.czg.order.vo.TableSummaryExportVo;
|
||||
import com.czg.service.order.mapper.ShopTableOrderStatisticMapper;
|
||||
import jakarta.annotation.Resource;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.time.LocalDate;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
@@ -29,7 +34,7 @@ public class TableSummaryServiceImpl implements TableSummaryService {
|
||||
|
||||
|
||||
@Override
|
||||
public List<TableSummaryExportVo> summaryExportList(TableSummaryParam param) {
|
||||
public void summaryExportList(TableSummaryParam param, HttpServletResponse response) {
|
||||
if (param.getBeginDate() == null && param.getEndDate() == null) {
|
||||
// 获取当前日期
|
||||
LocalDate currentDate = LocalDate.now();
|
||||
@@ -48,7 +53,8 @@ public class TableSummaryServiceImpl implements TableSummaryService {
|
||||
}
|
||||
List<TableSummaryExportVo> list = shopTableOrderStatisticMapper.findSummaryExportList(param);
|
||||
if (CollUtil.isEmpty(list)) {
|
||||
return List.of();
|
||||
ExcelExportUtil.exportToResponse(new ArrayList<>(), TableSummaryExportVo.class, "台桌统计", response);
|
||||
return;
|
||||
}
|
||||
record TableSummary(String tableKey, BigDecimal totalSales, Map<Long, BigDecimal> productSales) {}
|
||||
|
||||
@@ -85,10 +91,106 @@ public class TableSummaryServiceImpl implements TableSummaryService {
|
||||
vo.setSalesAmount(summary.productSales().getOrDefault(vo.getProductId(), BigDecimal.ZERO));
|
||||
}
|
||||
});
|
||||
// 追加个空行用于处理表格样式
|
||||
TableSummaryExportVo nullVo = new TableSummaryExportVo();
|
||||
list.add(nullVo);
|
||||
return list;
|
||||
|
||||
List<SheetWriteHandler> strategies = calculateMergeCells(list);
|
||||
ExcelExportUtil.exportMultipleSheetsToResponse(
|
||||
List.of(new SheetData()
|
||||
.setData(list)
|
||||
.setSheetName("台桌统计")
|
||||
.setHandlers(strategies)
|
||||
.setClazz(TableSummaryExportVo.class)),
|
||||
"台桌统计",
|
||||
response);
|
||||
}
|
||||
|
||||
/**
|
||||
* 计算需要合并的单元格信息
|
||||
*/
|
||||
private List<SheetWriteHandler> calculateMergeCells(List<TableSummaryExportVo> list) {
|
||||
List<SheetWriteHandler> mergeInfos = new ArrayList<>();
|
||||
|
||||
if (CollUtil.isEmpty(list)) {
|
||||
return mergeInfos;
|
||||
}
|
||||
|
||||
// 按createDate分组
|
||||
Map<String, List<TableSummaryExportVo>> byCreateDate = list.stream()
|
||||
.collect(Collectors.groupingBy(TableSummaryExportVo::getCreateDate));
|
||||
|
||||
List<String> keyList = byCreateDate.keySet().stream().sorted().toList();
|
||||
|
||||
// Excel行号从1开始(假设第0行是标题)
|
||||
int startRow = 1;
|
||||
|
||||
for (String key : keyList) {
|
||||
List<TableSummaryExportVo> dateGroup = byCreateDate.get(key);
|
||||
int dateGroupSize = dateGroup.size();
|
||||
|
||||
// 合并createDate列(假设是第1列)
|
||||
if (dateGroupSize > 1) {
|
||||
mergeInfos.add(ExcelExportUtil.createMergeStrategy(startRow, startRow + dateGroupSize - 1, 0, 0));
|
||||
mergeInfos.add(ExcelExportUtil.createMergeStrategy(startRow, startRow + dateGroupSize - 1, 9, 9));
|
||||
}
|
||||
|
||||
// 在销售额组内按tableName分组
|
||||
Map<String, List<TableSummaryExportVo>> byTableName = dateGroup.stream()
|
||||
.collect(Collectors.groupingBy(TableSummaryExportVo::getTableName));
|
||||
|
||||
List<String> nameKeyList = byTableName.keySet().stream().sorted().toList();
|
||||
|
||||
int currentRow = startRow;
|
||||
for (String nameKey : nameKeyList) {
|
||||
int nameGroupSize = byTableName.get(nameKey).size();
|
||||
|
||||
// 合并tableName列(假设是第4列,根据实际Excel列调整)
|
||||
if (nameGroupSize > 1) {
|
||||
mergeInfos.add(ExcelExportUtil.createMergeStrategy(currentRow, currentRow + nameGroupSize - 1, 1, 1));
|
||||
}
|
||||
|
||||
// 移动到下一组
|
||||
currentRow += nameGroupSize;
|
||||
}
|
||||
|
||||
// 在日期组内按totalSalesAmount分组(同台桌同日期)
|
||||
Map<BigDecimal, List<TableSummaryExportVo>> byTotalSales = dateGroup.stream()
|
||||
.collect(Collectors.groupingBy(TableSummaryExportVo::getTotalSalesAmount));
|
||||
|
||||
List<BigDecimal> salesKeyList = byTotalSales.keySet().stream().sorted().toList();
|
||||
|
||||
int nameRow = startRow;
|
||||
|
||||
for (BigDecimal saleKey : salesKeyList) {
|
||||
List<TableSummaryExportVo> salesGroup = byTotalSales.get(saleKey);
|
||||
int salesGroupSize = salesGroup.size();
|
||||
|
||||
// 合并totalSalesAmount列(假设是第8列,根据实际Excel列调整)
|
||||
if (salesGroupSize > 1) {
|
||||
mergeInfos.add(ExcelExportUtil.createMergeStrategy(nameRow, nameRow + salesGroupSize - 1, 9, 9));
|
||||
}
|
||||
|
||||
// // 在销售额组内按tableName分组
|
||||
// Map<String, List<TableSummaryExportVo>> byTableName = salesGroup.stream()
|
||||
// .collect(Collectors.groupingBy(TableSummaryExportVo::getTableName));
|
||||
//
|
||||
// List<String> nameKeyList = byTableName.keySet().stream().sorted().toList();
|
||||
//
|
||||
// for (List<TableSummaryExportVo> nameGroup : byTableName.values()) {
|
||||
// int nameGroupSize = nameGroup.size();
|
||||
//
|
||||
// // 合并tableName列(假设是第4列,根据实际Excel列调整)
|
||||
// if (nameGroupSize > 1) {
|
||||
// mergeInfos.add(ExcelExportUtil.createMergeStrategy(currentRow, currentRow + nameGroupSize - 1, 1, 1));
|
||||
// }
|
||||
//
|
||||
// // 移动到下一组
|
||||
// currentRow += nameGroupSize;
|
||||
// }
|
||||
}
|
||||
|
||||
startRow += dateGroupSize;
|
||||
}
|
||||
|
||||
return mergeInfos;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -327,8 +327,7 @@ public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> impl
|
||||
}
|
||||
|
||||
private void addMergeHandler(List<SheetWriteHandler> handlers, int firstRow, int lastRow, int firstCol, int lastCol) {
|
||||
OnceAbsoluteMergeStrategy strategy = new OnceAbsoluteMergeStrategy(firstRow, lastRow, firstCol, lastCol);
|
||||
handlers.add(strategy);
|
||||
handlers.add(ExcelExportUtil.createMergeStrategy(firstRow, lastRow, firstCol, lastCol));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
Reference in New Issue
Block a user