KdsServiceImpl.java
21.5 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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
package com.infoloop.tianting.service.impl;
import com.infoloop.tianting.clientcustomerorderservice.OrderStatusEnum;
import com.infoloop.tianting.clientcustomerorderservice.PayStatusEnum;
import com.infoloop.tianting.clientcustomerorderservice.ServeStatusEnum;
import com.infoloop.tianting.clientcustomerorderservice.SingleClientCustomerOrderDetailRpcResponse;
import com.infoloop.tianting.clientcustomerorderservice.SingleClientCustomerOrderRpcResponse;
import com.infoloop.tianting.context.LoginContextHolder;
import com.infoloop.tianting.model.dto.InventoryDbDTO.ClientDto;
import com.infoloop.tianting.model.dto.KdsDTO.KdsMealDetailDto;
import com.infoloop.tianting.model.dto.KdsDTO.KdsOrderLocationDetailDto;
import com.infoloop.tianting.model.dto.KdsDTO.KdsOrderLocationDto;
import com.infoloop.tianting.model.dto.KdsDTO.KdsOrderLocationQueryDto;
import com.infoloop.tianting.model.dto.KdsDTO.KdsOrderSkuDetailDto;
import com.infoloop.tianting.model.dto.KdsDTO.KdsOrderSkuDto;
import com.infoloop.tianting.model.dto.KdsDTO.KdsOrderSkuQueryDto;
import com.infoloop.tianting.model.dto.OrderDbDTO.CustomerDetailDto;
import com.infoloop.tianting.model.dto.OrderDbDTO.CustomerNotice;
import com.infoloop.tianting.model.dto.OrderDbDTO.QueryOrderByConditionDto;
import com.infoloop.tianting.service.KdsService;
import com.infoloop.tianting.service.client.ClientCustomerServiceRpcClient;
import com.infoloop.tianting.service.client.EnterpriseResourceServiceRpcClient;
import com.infoloop.tianting.service.client.InventoryServiceRpcClient;
import com.infoloop.tianting.service.client.MealServiceRpcClient;
import com.infoloop.tianting.service.client.OrderServiceRpcClient;
import com.infoloop.tianting.service.client.SkuServiceRpcClient;
import com.infoloop.tianting.service.client.WOperatorServiceRpcClient;
import com.infoloop.tianting.utils.DateUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
@Slf4j
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class KdsServiceImpl implements KdsService {
private final OrderServiceRpcClient orderServiceRpcClient;
private final EnterpriseResourceServiceRpcClient enterpriseResourceServiceRpcClient;
private final WOperatorServiceRpcClient wOperatorServiceRpcClient;
private final MealServiceRpcClient mealServiceRpcClient;
private final SkuServiceRpcClient skuServiceRpcClient;
private final InventoryServiceRpcClient inventoryServiceRpcClient;
private final ClientCustomerServiceRpcClient clientCustomerServiceRpcClient;
@Override
public List<KdsOrderSkuDto> getOrderDetailBySku(KdsOrderSkuQueryDto queryDto) {
// 获取当日所有的订单 和 订单明细
LocalDate today = LocalDate.now();
LocalDateTime todayZero = today.atStartOfDay();
LocalDate tomorrow = today.plusDays(1);
LocalDateTime tomorrowZero = tomorrow.atStartOfDay();
final var operatorLoginInfo = LoginContextHolder.getLoginInfo();
boolean shouldFilterStallIds = false;
List<Integer> stallIds = new ArrayList<>();
final var operator = wOperatorServiceRpcClient.getWOperatorById(operatorLoginInfo.getId()).getResponse();
if (!operator.getIsSuper()) {
shouldFilterStallIds = true;
final var userResource = enterpriseResourceServiceRpcClient.getUserResources(operatorLoginInfo.getEnterpriseId(), operatorLoginInfo.getId());
stallIds.addAll(userResource.getStallIdsList());
}
final var queryOrderDto = QueryOrderByConditionDto.builder()
.enterpriseId(operatorLoginInfo.getEnterpriseId())
.shouldFilterStallIds(shouldFilterStallIds)
.stallIds(stallIds)
.shouldFilterMealTimeStart(true)
.mealTimeStart(todayZero)
.shouldFilterMealTimeEnd(true)
.mealTimeEnd(tomorrowZero)
.shouldFilterPayStatus(true)
.payStatus(PayStatusEnum.SUCCEED)
.build();
final var orderList = orderServiceRpcClient.getOrdersByCondition(queryOrderDto);
List<Integer> orderIds = orderList.stream()
.filter(o -> o.getStatus() == OrderStatusEnum.ALL_SERVED || o.getStatus() == OrderStatusEnum.PARTIAL_SERVED || o.getStatus() == OrderStatusEnum.PREPARING)
.map(SingleClientCustomerOrderRpcResponse::getId)
.distinct()
.collect(Collectors.toList());
final var clientIds = orderList.stream()
.map(SingleClientCustomerOrderRpcResponse::getClientId)
.distinct()
.collect(Collectors.toList());
final var clients = inventoryServiceRpcClient.getClientsByIds(
operator.getEnterpriseId(), clientIds);
final var orderStallIds = orderList.stream()
.map(SingleClientCustomerOrderRpcResponse::getStallId)
.distinct()
.collect(Collectors.toList());
final var stalls = inventoryServiceRpcClient.getStallsByIds(
operator.getEnterpriseId(), orderStallIds);
final var customerIds = orderList.stream().map(SingleClientCustomerOrderRpcResponse::getCustomerId).collect(Collectors.toList());
final var customerResponse = clientCustomerServiceRpcClient.getClientCustomersByIds(
queryOrderDto.getEnterpriseId(), customerIds);
final var orderDetailList = orderServiceRpcClient.getOrderDetailsByOrderIds(orderIds);
final var filteredOrderDetailList = orderDetailList.stream()
.filter(od -> od.getServeStatus() == queryDto.getServeStatus() && od.getMealId() == queryDto.getMealId())
.collect(Collectors.toList());
//在所有的orderDetail里找出所有所有不同的menuDetailId,然后根据menuDetailId进行分类
final var menuDetailIds = filteredOrderDetailList.stream()
.map(SingleClientCustomerOrderDetailRpcResponse::getMenuDetailId)
.distinct()
.collect(Collectors.toList());
final var skuIds = filteredOrderDetailList.stream()
.map(SingleClientCustomerOrderDetailRpcResponse::getSkuId)
.distinct()
.collect(Collectors.toList());
final var skus = skuServiceRpcClient.getDishSkusByIds(skuIds).getResponseList();
List<KdsOrderSkuDto> kdsOrderSkuList = new ArrayList<>();
for (Integer menuDetailId : menuDetailIds) {
// 找出该menuDetailId 的所有orderDetail
final var curOrderDetails = filteredOrderDetailList.stream()
.filter(d -> d.getMenuDetailId() == menuDetailId)
.collect(Collectors.toList());
final var totalCount = curOrderDetails.stream()
.mapToInt(SingleClientCustomerOrderDetailRpcResponse::getCount)
.sum();
List<KdsOrderSkuDetailDto> kdsOrders = new ArrayList<>();
for (SingleClientCustomerOrderDetailRpcResponse detail : curOrderDetails) {
final var order = orderList.stream()
.filter(o -> o.getId() == detail.getOrderId())
.findFirst();
if (order.isEmpty()) {
continue;
}
final var optionalClient = clients.getClientsList().stream().filter(c -> c.getId() == order.get().getClientId()).findFirst();
ClientDto client = ClientDto.builder().build();
if (optionalClient.isPresent()) {
client = ClientDto.builder()
.id(optionalClient.get().getId())
.name(optionalClient.get().getName())
.build();
}
final var optionalStall = stalls.getClientsList().stream().filter(s -> s.getId() == order.get().getStallId()).findFirst();
ClientDto stall = ClientDto.builder().build();
if (optionalStall.isPresent()) {
stall = ClientDto.builder()
.id(optionalStall.get().getId())
.name(optionalStall.get().getName())
.build();
}
final var optionalCustomer = customerResponse.getResponsesList().stream()
.filter(c -> c.getId() == order.get().getCustomerId())
.findFirst();
CustomerDetailDto customer = CustomerDetailDto.builder().build();
if (optionalCustomer.isPresent()) {
customer = CustomerDetailDto.builder()
.id(optionalCustomer.get().getId())
.phone(optionalCustomer.get().getMobile())
.name(optionalCustomer.get().getName())
.build();
}
final var kdsOrder = KdsOrderSkuDetailDto.builder()
.count(detail.getCount())
.orderDetailId(detail.getId())
.remark(order.get().getRemark())
.orderId(order.get().getId())
.roomNo(order.get().getSyncRoomNo().isEmpty() ? order.get().getRoomNo() : order.get().getSyncRoomNo())
.tableCode(order.get().getTableCode())
.customerNotice(CustomerNotice.builder()
.doctors(order.get().getCustomerNoticeJson().getDoctorsList())
.avoids(order.get().getCustomerNoticeJson().getAvoidsList())
.build())
.adjustSkuRemark(detail.getAdjustSkuRemark())
.mealTime(DateUtil.formatDate(order.get().getMealTime()))
.createdAt(DateUtil.formatDate(order.get().getCreatedAt()))
.client(client)
.stall(stall)
.customer(customer)
.serveStatus(detail.getServeStatus())
.price(detail.getPrice())
.build();
kdsOrders.add(kdsOrder);
}
String skuCode = "";
if (!curOrderDetails.isEmpty()) {
final var skuId = curOrderDetails.get(0).getSkuId();
final var sku = skus.stream().filter(s -> s.getId() == skuId).findFirst();
if (sku.isPresent()) {
skuCode = sku.get().getCode();
}
}
final var kdsOrderSku = KdsOrderSkuDto.builder()
.showName(curOrderDetails.get(0).getShowName())
.skuCode(skuCode)
.totalCount(totalCount)
.orderList(kdsOrders)
.build();
kdsOrderSkuList.add(kdsOrderSku);
}
return kdsOrderSkuList;
}
@Override
public List<KdsMealDetailDto> getMealDetail() {
// 获取当日用餐的所有的订单 和 订单明细
LocalDate today = LocalDate.now();
LocalDateTime todayZero = today.atStartOfDay();
LocalDate tomorrow = today.plusDays(1);
LocalDateTime tomorrowZero = tomorrow.atStartOfDay().minusMinutes(1);
final var operatorLoginInfo = LoginContextHolder.getLoginInfo();
boolean shouldFilterStallIds = false;
List<Integer> stallIds = new ArrayList<>();
final var operator = wOperatorServiceRpcClient.getWOperatorById(operatorLoginInfo.getId()).getResponse();
if (!operator.getIsSuper()) {
shouldFilterStallIds = true;
final var userResource = enterpriseResourceServiceRpcClient.getUserResources(
operatorLoginInfo.getEnterpriseId(), operatorLoginInfo.getId());
stallIds.addAll(userResource.getStallIdsList());
}
final var queryOrderDto = QueryOrderByConditionDto.builder()
.enterpriseId(operatorLoginInfo.getEnterpriseId())
.shouldFilterStallIds(shouldFilterStallIds)
.stallIds(stallIds)
.shouldFilterMealTimeStart(true)
.mealTimeStart(todayZero)
.shouldFilterMealTimeEnd(true)
.mealTimeEnd(tomorrowZero)
.build();
final var orderList = orderServiceRpcClient.getOrdersByCondition(queryOrderDto);
List<Integer> orderIds = orderList.stream()
.filter(o -> o.getStatus() == OrderStatusEnum.ALL_SERVED || o.getStatus() == OrderStatusEnum.PARTIAL_SERVED || o.getStatus() == OrderStatusEnum.PREPARING)
.map(SingleClientCustomerOrderRpcResponse::getId)
.distinct()
.collect(Collectors.toList());
final var orderDetailList = orderServiceRpcClient.getOrderDetailsByOrderIds(orderIds);
//找出所有的不同的meal Ids
final var mealIds = orderDetailList.stream()
.map(SingleClientCustomerOrderDetailRpcResponse::getMealId)
.distinct()
.collect(Collectors.toList());
//获取到所有的餐别
final var allMeals = mealServiceRpcClient.getAllMeals(operatorLoginInfo.getEnterpriseId()).getResponsesList();
List<KdsMealDetailDto> kdsMealDetailDtoList = new ArrayList<>();
for (Integer mealId : mealIds) {
final var meal = allMeals.stream().filter(m -> m.getId() == mealId).findFirst();
String mealName = "无餐别";
if (meal.isEmpty() && mealId != 0) {
continue;
}
if (meal.isPresent()) {
mealName = meal.get().getName();
}
final var serveStatusToFilter = List.of(ServeStatusEnum.SERVE_FINISHED, ServeStatusEnum.SERVE_NOT_START);
final var totalCount = orderDetailList.stream()
.filter(od -> od.getMealId() == mealId && serveStatusToFilter.contains(od.getServeStatus()))
.mapToInt(SingleClientCustomerOrderDetailRpcResponse::getCount)
.sum();
final var mealDetail = KdsMealDetailDto.builder()
.mealId(mealId)
.mealName(mealName)
.totalCount(totalCount)
.build();
kdsMealDetailDtoList.add(mealDetail);
}
return kdsMealDetailDtoList;
}
@Override
public List<KdsOrderLocationDto> getOrderDetailByLocation(KdsOrderLocationQueryDto queryDto) {
// 获取当日用餐的所有的订单 和 订单明细
LocalDate today = LocalDate.now();
LocalDateTime todayZero = today.atStartOfDay();
LocalDate tomorrow = today.plusDays(1);
LocalDateTime tomorrowZero = tomorrow.atStartOfDay().minusMinutes(1);
final var operatorLoginInfo = LoginContextHolder.getLoginInfo();
boolean shouldFilterStallIds = false;
List<Integer> stallIds = new ArrayList<>();
final var operator = wOperatorServiceRpcClient.getWOperatorById(operatorLoginInfo.getId()).getResponse();
if (!operator.getIsSuper()) {
shouldFilterStallIds = true;
final var userResource = enterpriseResourceServiceRpcClient.getUserResources(
operatorLoginInfo.getEnterpriseId(), operatorLoginInfo.getId());
stallIds.addAll(userResource.getStallIdsList());
}
final var queryOrderDto = QueryOrderByConditionDto.builder()
.enterpriseId(operatorLoginInfo.getEnterpriseId())
.shouldFilterStallIds(shouldFilterStallIds)
.stallIds(stallIds)
.shouldFilterMealTimeStart(true)
.mealTimeStart(todayZero)
.shouldFilterMealTimeEnd(true)
.mealTimeEnd(tomorrowZero)
.shouldFilterStatus(true)
.status(queryDto.getOrderStatus())
.shouldFilterPayStatus(true)
.payStatus(PayStatusEnum.SUCCEED)
.build();
final var orderList = orderServiceRpcClient.getOrdersByCondition(queryOrderDto);
List<Integer> orderIds = orderList.stream()
.map(SingleClientCustomerOrderRpcResponse::getId)
.distinct()
.collect(Collectors.toList());
final var clientIds = orderList.stream()
.map(SingleClientCustomerOrderRpcResponse::getClientId)
.distinct()
.collect(Collectors.toList());
final var clients = inventoryServiceRpcClient.getClientsByIds(
operator.getEnterpriseId(), clientIds);
final var orderStallIds = orderList.stream()
.map(SingleClientCustomerOrderRpcResponse::getStallId)
.distinct()
.collect(Collectors.toList());
final var stalls = inventoryServiceRpcClient.getStallsByIds(
operator.getEnterpriseId(), orderStallIds);
final var customerIds = orderList.stream().map(SingleClientCustomerOrderRpcResponse::getCustomerId).collect(Collectors.toList());
final var customerResponse = clientCustomerServiceRpcClient.getClientCustomersByIds(
queryOrderDto.getEnterpriseId(), customerIds);
final var orderDetailList = orderServiceRpcClient.getOrderDetailsByOrderIds(orderIds);
final var filteredOrderDetailList = orderDetailList.stream()
.filter(od -> od.getMealId() == queryDto.getMealId() && od.getServeStatus() != ServeStatusEnum.SERVE_REJECT)
.collect(Collectors.toList());
List<KdsOrderLocationDto> kdsOrderLocationList = new ArrayList<>();
for (SingleClientCustomerOrderRpcResponse order : orderList) {
final var curOrderDetails = filteredOrderDetailList.stream()
.filter(d -> d.getOrderId() == order.getId())
.collect(Collectors.toList());
final var kdsOrderDetails = curOrderDetails.stream().map(od ->
KdsOrderLocationDetailDto.builder()
.adjustSkuRemark(od.getAdjustSkuRemark())
.count(od.getCount())
.orderDetailId(od.getId())
.serveStatus(od.getServeStatus())
.showName(od.getShowName())
.price(od.getPrice())
.build()
).collect(Collectors.toList());
if (kdsOrderDetails.isEmpty()) {
continue;
}
final var detailPriceList = curOrderDetails.stream()
.map(d -> d.getPrice() * d.getCount())
.collect(Collectors.toList());
final var totalPrice = detailPriceList.stream().mapToInt(integer -> integer).sum();
final var optionalClient = clients.getClientsList().stream().filter(c -> c.getId() == order.getClientId()).findFirst();
ClientDto client = ClientDto.builder().build();
if (optionalClient.isPresent()) {
client = ClientDto.builder()
.id(optionalClient.get().getId())
.name(optionalClient.get().getName())
.build();
}
final var optionalStall = stalls.getClientsList().stream().filter(s -> s.getId() == order.getStallId()).findFirst();
ClientDto stall = ClientDto.builder().build();
if (optionalStall.isPresent()) {
stall = ClientDto.builder()
.id(optionalStall.get().getId())
.name(optionalStall.get().getName())
.build();
}
final var optionalCustomer = customerResponse.getResponsesList().stream()
.filter(c -> c.getId() == order.getCustomerId())
.findFirst();
CustomerDetailDto customer = CustomerDetailDto.builder().build();
if (optionalCustomer.isPresent()) {
customer = CustomerDetailDto.builder()
.id(optionalCustomer.get().getId())
.phone(optionalCustomer.get().getMobile())
.name(optionalCustomer.get().getName())
.build();
}
final var kdsOrder = KdsOrderLocationDto.builder()
.totalCount(order.getTotalNum())
.remark(order.getRemark())
.orderId(order.getId())
.roomNo(order.getSyncRoomNo().isEmpty() ? order.getRoomNo() : order.getSyncRoomNo())
.tableCode(order.getTableCode())
.customerNotice(CustomerNotice.builder()
.doctors(order.getCustomerNoticeJson().getDoctorsList())
.avoids(order.getCustomerNoticeJson().getAvoidsList())
.build())
.orderDetailList(kdsOrderDetails)
.mealTime(DateUtil.formatDate(order.getMealTime()))
.orderCode(order.getOrderCode())
.payPrice(order.getPayPrice())
.totalPrice(totalPrice)
.createdAt(DateUtil.formatDate(order.getCreatedAt()))
.client(client)
.stall(stall)
.customer(customer)
.build();
kdsOrderLocationList.add(kdsOrder);
}
return kdsOrderLocationList;
}
}