SettlementServiceImpl.java
10.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
package com.infoloop.tianting.service.impl;
import com.infoloop.tianting.clientcustomerorderservice.SingleClientCustomerOrderDetailRpcResponse;
import com.infoloop.tianting.clientcustomerorderservice.SingleClientCustomerOrderRpcResponse;
import com.infoloop.tianting.context.LoginContextHolder;
import com.infoloop.tianting.exception.ClientEndExceptions.BusinessException;
import com.infoloop.tianting.exception.ErrorCodeEnum;
import com.infoloop.tianting.menuservice.SingleMenuDetailRpcResponse;
import com.infoloop.tianting.model.dto.OrderDbDTO.QueryOrderByConditionDto;
import com.infoloop.tianting.model.dto.SettlementDTO.SettlementMenuSkuDto;
import com.infoloop.tianting.model.dto.SettlementDTO.SettlementRequestDto;
import com.infoloop.tianting.service.SettlementService;
import com.infoloop.tianting.service.client.MenuServiceRpcClient;
import com.infoloop.tianting.service.client.OrderServiceRpcClient;
import com.infoloop.tianting.service.client.SkuServiceRpcClient;
import com.infoloop.tianting.utils.DateUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
@Slf4j
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class SettlementServiceImpl implements SettlementService {
private final OrderServiceRpcClient orderServiceRpcClient;
private final MenuServiceRpcClient menuServiceRpcClient;
private final SkuServiceRpcClient skuServiceRpcClient;
@Override
public ByteArrayOutputStream generateOrderSettlementExcel(SettlementRequestDto settlementRequest) {
final var operatorLoginInfo = LoginContextHolder.getLoginInfo();
final var queryOrderDto = QueryOrderByConditionDto.builder()
.enterpriseId(LoginContextHolder.hasLogin() ? operatorLoginInfo.getEnterpriseId() : settlementRequest.getEnterpriseId())
.shouldFilterCreatedAtTimeStart(true)
.createdAtTimeStart(settlementRequest.getTimeStart())
.shouldFilterCreatedAtTimeEnd(true)
.createdAtTimeEnd(settlementRequest.getTimeEnd())
.shouldFilterStallIds(!settlementRequest.getStallIds().isEmpty())
.stallIds(settlementRequest.getStallIds())
.build();
final var orderList = orderServiceRpcClient.getOrdersByCondition(queryOrderDto);
List<Integer> orderIds = orderList.stream().map(
SingleClientCustomerOrderRpcResponse::getId).distinct()
.collect(Collectors.toList());
final var orderDetailList = orderServiceRpcClient.getOrderDetailsByOrderIds(orderIds);
List<Integer> skuIds = orderDetailList.stream()
.map(SingleClientCustomerOrderDetailRpcResponse::getSkuId)
.distinct()
.collect(Collectors.toList());
final var skus = skuServiceRpcClient.getDishSkusByIds(skuIds).getResponseList();
List<Integer> menuIds = orderList.stream().map(
SingleClientCustomerOrderRpcResponse::getMenuId).distinct()
.collect(Collectors.toList());
final var menuList = menuServiceRpcClient
.getMenusByIds(operatorLoginInfo.getEnterpriseId(), menuIds)
.getResponsesList();
final var menuDetailList = menuServiceRpcClient.queryMenuDetailsByMenuIds(
operatorLoginInfo.getEnterpriseId(), menuIds).getResponsesList();
Map<String, Map<String, List<SettlementMenuSkuDto>>> classifyOrders = new HashMap<>();
//找出所有需要的日期
final var dates = orderList.stream()
.map(o -> DateUtil.formatDate(o.getCreatedAt(), DateUtil.YYYY_MM_DD))
.distinct()
.collect(Collectors.toList());
for(String date: dates) {
if (!classifyOrders.containsKey(date)) {
classifyOrders.put(date, new HashMap<>());
}
final var menusMap = classifyOrders.get(date);
// 找出当天所有的订单
final var curOrderList = orderList.stream()
.filter(o -> DateUtil.formatDate(o.getCreatedAt(), DateUtil.YYYY_MM_DD).equals(date))
.collect(Collectors.toList());
final var curOrderIds = curOrderList.stream()
.map(SingleClientCustomerOrderRpcResponse::getId)
.collect(Collectors.toList());
// 找出当天已点的所有餐单
final var curMenuIds = curOrderList.stream()
.map(SingleClientCustomerOrderRpcResponse::getMenuId)
.distinct()
.collect(Collectors.toList());
for (Integer curMenuId: curMenuIds) {
final var curMenu = menuList.stream().filter(m -> m.getId() == curMenuId).findFirst();
if (curMenu.isEmpty()) {
continue;
}
final var curMenuName = curMenu.get().getName();
if (!menusMap.containsKey(curMenuName)) {
menusMap.put(curMenuName, new ArrayList<>());
}
final var menuSkuList = new ArrayList<SettlementMenuSkuDto>();
// 找出当天餐单所有的菜品
final var curMenuDetails = menuDetailList.stream()
.filter(md -> md.getMenuId() == curMenuId)
.collect(Collectors.toList());
final var curMenuDetailIds = curMenuDetails.stream()
.map(SingleMenuDetailRpcResponse::getId)
.collect(Collectors.toList());
//找出当天所有已点的菜品明细
final var curOrderDetails = orderDetailList.stream()
.filter(d -> curOrderIds.contains(d.getOrderId()))
.collect(Collectors.toList());
for (SingleMenuDetailRpcResponse m: curMenuDetails) {
// 找出该餐单下该sku所点菜品的明细
final var curSkuOrderDetails = curOrderDetails.stream()
.filter(rd-> rd.getMenuDetailId() == m.getId())
.collect(Collectors.toList());
final var countList = curSkuOrderDetails.stream()
.map(SingleClientCustomerOrderDetailRpcResponse::getCount)
.collect(Collectors.toList());
int countTotal = countList.stream().mapToInt(Integer::intValue).sum();
if (countTotal <= 0) {
continue;
}
// 找出该sku的code
final var curSku = skus.stream().filter(s -> s.getId() == m.getSkuId()).findFirst();
String skuCode = "";
if (curSku.isPresent()) {
skuCode = curSku.get().getCode();
}
final var settlementMenuSku = SettlementMenuSkuDto.builder()
.skuCode(skuCode)
.count(countTotal)
.showName(m.getShowName())
.price(m.getPrice())
.totalPrice(m.getPrice() * countTotal)
.build();
menuSkuList.add(settlementMenuSku);
}
menusMap.get(curMenuName).addAll(menuSkuList);
}
}
try {
final var bytes = writeToExcel(classifyOrders);
return bytes;
} catch (IOException e) {
throw BusinessException.build(ErrorCodeEnum.GENERATE_EXCEL_ERROR);
}
}
public static ByteArrayOutputStream writeToExcel(Map<String, Map<String, List<SettlementMenuSkuDto>>> classifyOrders)
throws IOException {
Workbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet("订单数据");
int rowNum = 0;
Row titleRow = sheet.createRow(rowNum++);
Cell dateTitleCell = titleRow.createCell(0);
dateTitleCell.setCellValue("点餐日期");
Cell menuTitleCell = titleRow.createCell(1);
menuTitleCell.setCellValue("餐单名称");
Cell skuCodeTitleCell = titleRow.createCell(2);
skuCodeTitleCell.setCellValue("菜品编号");
Cell skuNameTitleCell = titleRow.createCell(3);
skuNameTitleCell.setCellValue("菜品名称");
Cell skuPriceTitleCell = titleRow.createCell(4);
skuPriceTitleCell.setCellValue("单价");
Cell skuCountTitleCell = titleRow.createCell(5);
skuCountTitleCell.setCellValue("数量");
Cell skuTotalPriceTitleCell = titleRow.createCell(6);
skuTotalPriceTitleCell.setCellValue("合计金额");
for (Map.Entry<String, Map<String, List<SettlementMenuSkuDto>>> outerEntry : classifyOrders.entrySet()) {
String orderDate = outerEntry.getKey();
Map<String, List<SettlementMenuSkuDto>> innerMap = outerEntry.getValue();
Integer curDateTotalRow = 0;
for (Map.Entry<String, List<SettlementMenuSkuDto>> innerEntry : innerMap.entrySet()) {
curDateTotalRow += innerEntry.getValue().size();
}
// 写入日期列并合并单元格
int startRowForDate = rowNum;
for (int i = 0; i < curDateTotalRow; i++) {
Row dateRow = sheet.createRow(rowNum++);
Cell dateCell = dateRow.createCell(0);
dateCell.setCellValue(orderDate);
}
int endRowForDate = rowNum - 1;
if (endRowForDate > startRowForDate) {
CellRangeAddress dateRegion = new CellRangeAddress(startRowForDate, endRowForDate, 0, 0);
sheet.addMergedRegion(dateRegion);
}
rowNum = startRowForDate;
for (Map.Entry<String, List<SettlementMenuSkuDto>> innerEntry : innerMap.entrySet()) {
String menuName = innerEntry.getKey();
List<SettlementMenuSkuDto> skuList = innerEntry.getValue();
// 写入菜单ID列并合并单元格
int startRowForMenu = rowNum;
for (int i = 0; i < skuList.size(); i++) {
Row menuRow = sheet.getRow(rowNum++);
Cell menuCell = menuRow.createCell(1);
menuCell.setCellValue(menuName);
}
int endRowForMenu = rowNum - 1;
if (endRowForMenu > startRowForMenu) {
CellRangeAddress menuRegion = new CellRangeAddress(startRowForMenu, endRowForMenu, 1, 1);
sheet.addMergedRegion(menuRegion);
}
// 写入SKU列
rowNum = startRowForMenu;
for (SettlementMenuSkuDto skuDto : skuList) {
Row skuRow = sheet.getRow(rowNum++);
Cell skuCodeCell = skuRow.createCell(2);
skuCodeCell.setCellValue(skuDto.getSkuCode());
Cell showNameCell = skuRow.createCell(3);
showNameCell.setCellValue(skuDto.getShowName());
Cell priceCell = skuRow.createCell(4);
priceCell.setCellValue((double)skuDto.getPrice()/100);
Cell countCell = skuRow.createCell(5);
countCell.setCellValue(skuDto.getCount());
Cell totalPriceCell = skuRow.createCell(6);
totalPriceCell.setCellValue((double)skuDto.getTotalPrice()/100);
}
}
}
// 自动调整列宽
for (int i = 0; i < 7; i++) {
sheet.autoSizeColumn(i);
}
// 将工作簿内容写入字节数组输出流
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
workbook.write(outputStream);
workbook.close();
return outputStream;
}
}