zhuyifan

自动创建餐单

1 +package com.infoloop.tianting.logic.task;
2 +
3 +import com.infoloop.tianting.clientinventoryservice.SingleClientRpcResponse;
4 +import com.infoloop.tianting.context.LoginContextHolder;
5 +import com.infoloop.tianting.enums.LoginSourceEnum;
6 +import com.infoloop.tianting.mealorderservice.MealOrderCreation;
7 +import com.infoloop.tianting.mealorderservice.MealOrderOrderMethodEnum;
8 +import com.infoloop.tianting.mealorderservice.MenuSchedule;
9 +import com.infoloop.tianting.mealorderservice.SingleDinerRpcResponse;
10 +import com.infoloop.tianting.mealorderservice.SingleGradeClassRpcResponse;
11 +import com.infoloop.tianting.mealorderservice.SingleMealMenuRecordRpcResponse;
12 +import com.infoloop.tianting.mealorderservice.SingleMealOrderRpcResponse;
13 +import com.infoloop.tianting.service.client.ClientInventoryServiceRpcClient;
14 +import com.infoloop.tianting.service.client.MealOrderServiceRpcClient;
15 +import com.infoloop.tianting.utils.DateUtil;
16 +import lombok.RequiredArgsConstructor;
17 +import lombok.extern.slf4j.Slf4j;
18 +import org.springframework.scheduling.annotation.Scheduled;
19 +import org.springframework.stereotype.Component;
20 +
21 +import java.time.DayOfWeek;
22 +import java.time.Instant;
23 +import java.time.LocalDate;
24 +import java.time.LocalTime;
25 +import java.util.ArrayList;
26 +import java.util.function.Function;
27 +import java.util.stream.Collectors;
28 +
29 +@Slf4j
30 +@Component
31 +@RequiredArgsConstructor
32 +public class AutoCreateMealOrderTask {
33 +
34 + private final MealOrderServiceRpcClient mealOrderServiceRpcClient;
35 +
36 + private final ClientInventoryServiceRpcClient clientInventoryServiceRpcClient;
37 +
38 + @Scheduled(cron = "0 0 13 ? * WED")
39 + public void executeTask() {
40 + int enterpriseId = 449;
41 + final var today = LocalDate.now();
42 + final var zonedDateTime = today.with(DayOfWeek.MONDAY).atStartOfDay().atZone(DateUtil.CHINA_ZONE);
43 + final var startTimestamp = zonedDateTime.toInstant().toEpochMilli();
44 + final var endTimestamp = today.with(DayOfWeek.SUNDAY).atTime(LocalTime.MAX).atZone(DateUtil.CHINA_ZONE).toInstant().toEpochMilli();
45 + final var mealOrders = mealOrderServiceRpcClient.queryMealOrdersByCondition(449, null, startTimestamp, endTimestamp);
46 + final var dinerIds = mealOrders.stream().map(SingleMealOrderRpcResponse::getDinerId).toList();
47 + final var allDiners = mealOrderServiceRpcClient.queryDinersByCondition(enterpriseId, null, null, null);
48 + final var notReservedDiners = allDiners.stream().filter(diner -> !dinerIds.contains(diner.getId())).toList();
49 + final var clientIds = notReservedDiners.stream().map(SingleDinerRpcResponse::getClientId).distinct().toList();
50 + final var clientMap = clientInventoryServiceRpcClient.getClientsByIds(enterpriseId, clientIds).getClientsList().stream().collect(Collectors.toMap(SingleClientRpcResponse::getId, Function.identity()));
51 + final var gradeClassMap = mealOrderServiceRpcClient.getGradeClassesByIds(enterpriseId, notReservedDiners.stream().map(SingleDinerRpcResponse::getClassId).distinct().toList()).stream().collect(Collectors.toMap(SingleGradeClassRpcResponse::getId, Function.identity()));
52 + final var latestMealMenuRecordMap = mealOrderServiceRpcClient.queryLatestMealMenuRecordsByClientIds(enterpriseId, clientIds).stream().collect(Collectors.toMap(SingleMealMenuRecordRpcResponse::getClientId, Function.identity()));
53 + final var creations = new ArrayList<MealOrderCreation>();
54 + for (var diner : notReservedDiners) {
55 + final var mealMenuRecordRpcResponse = latestMealMenuRecordMap.get(diner.getClientId());
56 + if (mealMenuRecordRpcResponse == null) {
57 + continue;
58 + }
59 + final var singleClientRpcResponse = clientMap.get(diner.getClientId());
60 + if (singleClientRpcResponse == null) {
61 + continue;
62 + }
63 + final var singleGradeClassRpcResponse = gradeClassMap.get(diner.getClassId());
64 + if (singleGradeClassRpcResponse == null) {
65 + continue;
66 + }
67 + MealOrderCreation.newBuilder()
68 + .setClientId(diner.getClientId())
69 + .setMenuId(mealMenuRecordRpcResponse.getId())
70 + .setAccountId(0)
71 + .setOrderMethod(MealOrderOrderMethodEnum.MEAL_ORDER_METHOD_SYSTEM)
72 + .setDinerId(diner.getId())
73 + .setDinerName(diner.getName())
74 + .setClientName(singleClientRpcResponse.getName())
75 + .setGradeName(singleGradeClassRpcResponse.getGradeName())
76 + .setClassName(singleGradeClassRpcResponse.getClassName())
77 + .setStudentNo(diner.getStudentNo())
78 + .setReserveDate(Instant.now().toEpochMilli())
79 + .addMeals(MenuSchedule.newBuilder().setDate(zonedDateTime.format(DateUtil.YYYY_MM_DD_LEFT_SYMBOL)).setMenu("A").build())
80 + .addMeals(MenuSchedule.newBuilder().setDate(zonedDateTime.plusDays(1).format(DateUtil.YYYY_MM_DD_LEFT_SYMBOL)).setMenu("A").build())
81 + .addMeals(MenuSchedule.newBuilder().setDate(zonedDateTime.plusDays(2).format(DateUtil.YYYY_MM_DD_LEFT_SYMBOL)).setMenu("A").build())
82 + .addMeals(MenuSchedule.newBuilder().setDate(zonedDateTime.plusDays(3).format(DateUtil.YYYY_MM_DD_LEFT_SYMBOL)).setMenu("A").build())
83 + .addMeals(MenuSchedule.newBuilder().setDate(zonedDateTime.plusDays(4).format(DateUtil.YYYY_MM_DD_LEFT_SYMBOL)).setMenu("A").build())
84 + .build();
85 + }
86 + final var loginInfo = LoginContextHolder.LoginInfo.builder()
87 + .id(0)
88 + .enterpriseId(enterpriseId)
89 + .loginSource(LoginSourceEnum.MINI_PROGRAM)
90 + .build();
91 + mealOrderServiceRpcClient.batchCreateMealOrders(loginInfo, creations);
92 + }
93 +}
...@@ -33,7 +33,7 @@ import com.infoloop.tianting.mealorderservice.MpAccountDinerRefCreation; ...@@ -33,7 +33,7 @@ import com.infoloop.tianting.mealorderservice.MpAccountDinerRefCreation;
33 import com.infoloop.tianting.mealorderservice.MpAccountModification; 33 import com.infoloop.tianting.mealorderservice.MpAccountModification;
34 import com.infoloop.tianting.mealorderservice.QueryDinersByConditionRpcRequest; 34 import com.infoloop.tianting.mealorderservice.QueryDinersByConditionRpcRequest;
35 import com.infoloop.tianting.mealorderservice.QueryGradeClassesByConditionRpcRequest; 35 import com.infoloop.tianting.mealorderservice.QueryGradeClassesByConditionRpcRequest;
36 -import com.infoloop.tianting.mealorderservice.QueryLatestMealMenuRecordByClientIdRpcRequest; 36 +import com.infoloop.tianting.mealorderservice.QueryLatestMealMenuRecordsByClientIdsRpcRequest;
37 import com.infoloop.tianting.mealorderservice.QueryMealOrdersByConditionRpcRequest; 37 import com.infoloop.tianting.mealorderservice.QueryMealOrdersByConditionRpcRequest;
38 import com.infoloop.tianting.mealorderservice.QueryMpAccountDinerRefsByConditionRpcRequest; 38 import com.infoloop.tianting.mealorderservice.QueryMpAccountDinerRefsByConditionRpcRequest;
39 import com.infoloop.tianting.mealorderservice.QueryReserveMealOrdersByDinerIdsRpcRequest; 39 import com.infoloop.tianting.mealorderservice.QueryReserveMealOrdersByDinerIdsRpcRequest;
...@@ -48,10 +48,13 @@ import com.infoloop.tianting.mealorderservice.UpdateDinerRpcResponse; ...@@ -48,10 +48,13 @@ import com.infoloop.tianting.mealorderservice.UpdateDinerRpcResponse;
48 import com.infoloop.tianting.mealorderservice.UpdateMpAccountRpcRequest; 48 import com.infoloop.tianting.mealorderservice.UpdateMpAccountRpcRequest;
49 import com.infoloop.tianting.mealorderservice.UpdateMpAccountRpcResponse; 49 import com.infoloop.tianting.mealorderservice.UpdateMpAccountRpcResponse;
50 import lombok.RequiredArgsConstructor; 50 import lombok.RequiredArgsConstructor;
51 +import org.apache.commons.collections4.CollectionUtils;
52 +import org.apache.commons.lang3.StringUtils;
51 import org.springframework.beans.factory.annotation.Autowired; 53 import org.springframework.beans.factory.annotation.Autowired;
52 import org.springframework.stereotype.Service; 54 import org.springframework.stereotype.Service;
53 55
54 import javax.annotation.Nullable; 56 import javax.annotation.Nullable;
57 +import java.util.Collections;
55 import java.util.List; 58 import java.util.List;
56 59
57 @Service 60 @Service
...@@ -129,9 +132,9 @@ public class MealOrderServiceRpcClient { ...@@ -129,9 +132,9 @@ public class MealOrderServiceRpcClient {
129 public List<SingleDinerRpcResponse> queryDinersByCondition(int enterpriseId, List<Integer> clientIds, List<Integer> gradeClassIds, @Nullable String studentNo) { 132 public List<SingleDinerRpcResponse> queryDinersByCondition(int enterpriseId, List<Integer> clientIds, List<Integer> gradeClassIds, @Nullable String studentNo) {
130 return mealOrderServiceRpcBlockingStub.queryDinersByCondition(QueryDinersByConditionRpcRequest.newBuilder() 133 return mealOrderServiceRpcBlockingStub.queryDinersByCondition(QueryDinersByConditionRpcRequest.newBuilder()
131 .setEnterpriseId(enterpriseId) 134 .setEnterpriseId(enterpriseId)
132 - .setShouldFilterClientIds(!clientIds.isEmpty()).addAllClientIds(clientIds) 135 + .setShouldFilterClientIds(CollectionUtils.isNotEmpty(clientIds)).addAllClientIds(CollectionUtils.isEmpty(clientIds) ? Collections.emptyList() : clientIds)
133 - .setShouldFilterClassIds(!gradeClassIds.isEmpty()).addAllClassIds(gradeClassIds) 136 + .setShouldFilterClassIds(CollectionUtils.isNotEmpty(gradeClassIds)).addAllClassIds(CollectionUtils.isNotEmpty(gradeClassIds) ? gradeClassIds : Collections.emptyList())
134 - .setShouldFilterStudentNo(studentNo != null).setStudentNo(studentNo == null ? "" : studentNo) 137 + .setShouldFilterStudentNo(StringUtils.isNotEmpty(studentNo)).setStudentNo(StringUtils.isNotEmpty(studentNo) ? studentNo : "")
135 .build()).getResponsesList(); 138 .build()).getResponsesList();
136 } 139 }
137 140
...@@ -211,16 +214,11 @@ public class MealOrderServiceRpcClient { ...@@ -211,16 +214,11 @@ public class MealOrderServiceRpcClient {
211 .build()).getResponsesList(); 214 .build()).getResponsesList();
212 } 215 }
213 216
214 - @Nullable 217 + public List<SingleMealMenuRecordRpcResponse> queryLatestMealMenuRecordsByClientIds(int enterpriseId, List<Integer> ids) {
215 - public SingleMealMenuRecordRpcResponse queryLatestMealMenuRecordByClientId(int enterpriseId, int id) { 218 + return mealOrderServiceRpcBlockingStub.queryLatestMealMenuRecordsByClientIds(QueryLatestMealMenuRecordsByClientIdsRpcRequest.newBuilder()
216 - final var queryLatestMealMenuRecordByClientIdRpcResponse = mealOrderServiceRpcBlockingStub.queryLatestMealMenuRecordByClientId(QueryLatestMealMenuRecordByClientIdRpcRequest.newBuilder()
217 .setEnterpriseId(enterpriseId) 219 .setEnterpriseId(enterpriseId)
218 - .setClientId(id) 220 + .addAllClientId(ids)
219 - .build()); 221 + .build()).getResponsesList();
220 - if (!queryLatestMealMenuRecordByClientIdRpcResponse.hasResponse()) {
221 - return null;
222 - }
223 - return queryLatestMealMenuRecordByClientIdRpcResponse.getResponse();
224 } 222 }
225 223
226 @Nullable 224 @Nullable
...@@ -253,10 +251,12 @@ public class MealOrderServiceRpcClient { ...@@ -253,10 +251,12 @@ public class MealOrderServiceRpcClient {
253 .build()); 251 .build());
254 } 252 }
255 253
256 - public List<SingleMealOrderRpcResponse> queryMealOrdersByCondition(int enterpriseId, int accountId) { 254 + public List<SingleMealOrderRpcResponse> queryMealOrdersByCondition(int enterpriseId, Integer accountId, Long reserveDateStart, Long reserveDateEnd) {
257 return mealOrderServiceRpcBlockingStub.queryMealOrdersByCondition(QueryMealOrdersByConditionRpcRequest.newBuilder() 255 return mealOrderServiceRpcBlockingStub.queryMealOrdersByCondition(QueryMealOrdersByConditionRpcRequest.newBuilder()
258 .setEnterpriseId(enterpriseId) 256 .setEnterpriseId(enterpriseId)
259 - .setShouldFilterAccountIds(true).addAccountIds(accountId) 257 + .setShouldFilterAccountIds(accountId != null).addAccountIds(accountId == null ? 0 : accountId)
258 + .setShouldFilterReserveDateStart(true).setReserveDateStart(reserveDateStart == null ? 0 : reserveDateStart)
259 + .setShouldFilterReserveDateEnd(true).setReserveDateEnd(reserveDateEnd == null ? 0 : reserveDateEnd)
260 .build()).getResponsesList(); 260 .build()).getResponsesList();
261 } 261 }
262 } 262 }
......
...@@ -39,7 +39,7 @@ public class MealOrderServiceImpl implements MealOrderService { ...@@ -39,7 +39,7 @@ public class MealOrderServiceImpl implements MealOrderService {
39 @Override 39 @Override
40 public List<MealOrderVO> queryMealOrders(MealOrderDTO.QueryMealOrderDTO queryMealOrderDTO) { 40 public List<MealOrderVO> queryMealOrders(MealOrderDTO.QueryMealOrderDTO queryMealOrderDTO) {
41 final var loginInfo = LoginContextHolder.getLoginInfo(); 41 final var loginInfo = LoginContextHolder.getLoginInfo();
42 - var mealOrderRpcResponses = mealOrderServiceRpcClient.queryMealOrdersByCondition(loginInfo.getEnterpriseId(), loginInfo.getId()); 42 + var mealOrderRpcResponses = mealOrderServiceRpcClient.queryMealOrdersByCondition(loginInfo.getEnterpriseId(), loginInfo.getId(), null, null);
43 final var today = LocalDate.now(); 43 final var today = LocalDate.now();
44 final var startTimestamp = today.with(DayOfWeek.MONDAY).atStartOfDay().atZone(DateUtil.CHINA_ZONE).toInstant().toEpochMilli(); 44 final var startTimestamp = today.with(DayOfWeek.MONDAY).atStartOfDay().atZone(DateUtil.CHINA_ZONE).toInstant().toEpochMilli();
45 final var endTimestamp = today.with(DayOfWeek.SUNDAY).atTime(LocalTime.MAX).atZone(DateUtil.CHINA_ZONE).toInstant().toEpochMilli(); 45 final var endTimestamp = today.with(DayOfWeek.SUNDAY).atTime(LocalTime.MAX).atZone(DateUtil.CHINA_ZONE).toInstant().toEpochMilli();
...@@ -75,13 +75,13 @@ public class MealOrderServiceImpl implements MealOrderService { ...@@ -75,13 +75,13 @@ public class MealOrderServiceImpl implements MealOrderService {
75 if (gradeClassById == null) { 75 if (gradeClassById == null) {
76 throw ClientEndExceptions.IncorrectRequestValue.build(ErrorCodeEnum.GRADE_CLASS_NOT_FOUND); 76 throw ClientEndExceptions.IncorrectRequestValue.build(ErrorCodeEnum.GRADE_CLASS_NOT_FOUND);
77 } 77 }
78 - final var latestMealMenuRecord = mealOrderServiceRpcClient.queryLatestMealMenuRecordByClientId(loginInfo.getEnterpriseId(), dinerById.getClientId()); 78 + final var latestMealMenuRecords = mealOrderServiceRpcClient.queryLatestMealMenuRecordsByClientIds(loginInfo.getEnterpriseId(), Collections.singletonList(dinerById.getClientId()));
79 - if (latestMealMenuRecord == null) { 79 + if (latestMealMenuRecords.isEmpty()) {
80 throw ClientEndExceptions.IncorrectRequestValue.build(ErrorCodeEnum.MEAL_MENU_NOT_FOUND); 80 throw ClientEndExceptions.IncorrectRequestValue.build(ErrorCodeEnum.MEAL_MENU_NOT_FOUND);
81 } 81 }
82 final var mealOrder = mealOrderServiceRpcClient.createMealOrder(loginInfo, MealOrderCreation.newBuilder() 82 final var mealOrder = mealOrderServiceRpcClient.createMealOrder(loginInfo, MealOrderCreation.newBuilder()
83 .setClientId(dinerById.getClientId()) 83 .setClientId(dinerById.getClientId())
84 - .setMenuId(latestMealMenuRecord.getId()) 84 + .setMenuId(latestMealMenuRecords.get(0).getId())
85 .setAccountId(loginInfo.getId()) 85 .setAccountId(loginInfo.getId())
86 .setOrderMethod(MealOrderOrderMethodEnum.MEAL_ORDER_METHOD_MICRO) 86 .setOrderMethod(MealOrderOrderMethodEnum.MEAL_ORDER_METHOD_MICRO)
87 .setDinerId(createMealOrderDTO.getDinerId()) 87 .setDinerId(createMealOrderDTO.getDinerId())
......
...@@ -215,8 +215,8 @@ public class MenuServiceImpl implements MenuService { ...@@ -215,8 +215,8 @@ public class MenuServiceImpl implements MenuService {
215 215
216 @Override 216 @Override
217 public String queryClientLatestMealMenuRecord(SingleClientRpcResponse client) { 217 public String queryClientLatestMealMenuRecord(SingleClientRpcResponse client) {
218 - final var singleMealMenuRecordRpcResponse = mealOrderServiceRpcClient.queryLatestMealMenuRecordByClientId(LoginContextHolder.getEnterpriseId(), client.getId()); 218 + final var singleMealMenuRecordRpcResponses = mealOrderServiceRpcClient.queryLatestMealMenuRecordsByClientIds(LoginContextHolder.getEnterpriseId(), List.of(client.getId()));
219 - return singleMealMenuRecordRpcResponse == null ? "" : singleMealMenuRecordRpcResponse.getImageUrl(); 219 + return singleMealMenuRecordRpcResponses.isEmpty() ? "" : singleMealMenuRecordRpcResponses.get(0).getImageUrl();
220 } 220 }
221 221
222 222
......
...@@ -51,7 +51,7 @@ service MealOrderServiceRpc { ...@@ -51,7 +51,7 @@ service MealOrderServiceRpc {
51 rpc GetMealMenuRecordsByIds (GetMealMenuRecordsByIdsRpcRequest) returns (GetMealMenuRecordsByIdsRpcResponse) {} 51 rpc GetMealMenuRecordsByIds (GetMealMenuRecordsByIdsRpcRequest) returns (GetMealMenuRecordsByIdsRpcResponse) {}
52 rpc QueryMealMenuRecordsByCondition (QueryMealMenuRecordsByConditionRpcRequest) returns (QueryMealMenuRecordsByConditionRpcResponse) {} 52 rpc QueryMealMenuRecordsByCondition (QueryMealMenuRecordsByConditionRpcRequest) returns (QueryMealMenuRecordsByConditionRpcResponse) {}
53 rpc CreateMealMenuRecord (CreateMealMenuRecordRpcRequest) returns (CreateMealMenuRecordRpcResponse) {} 53 rpc CreateMealMenuRecord (CreateMealMenuRecordRpcRequest) returns (CreateMealMenuRecordRpcResponse) {}
54 - rpc QueryLatestMealMenuRecordByClientId (QueryLatestMealMenuRecordByClientIdRpcRequest) returns (QueryLatestMealMenuRecordByClientIdRpcResponse) {} 54 + rpc QueryLatestMealMenuRecordsByClientIds (QueryLatestMealMenuRecordsByClientIdsRpcRequest) returns (QueryLatestMealMenuRecordsByClientIdsRpcResponse) {}
55 } 55 }
56 56
57 enum MealOrderOrderMethodEnum { 57 enum MealOrderOrderMethodEnum {
...@@ -788,11 +788,11 @@ message CreateMealMenuRecordRpcResponse { ...@@ -788,11 +788,11 @@ message CreateMealMenuRecordRpcResponse {
788 bool isCreated = 2; 788 bool isCreated = 2;
789 } 789 }
790 790
791 -message QueryLatestMealMenuRecordByClientIdRpcRequest { 791 +message QueryLatestMealMenuRecordsByClientIdsRpcRequest {
792 int32 enterpriseId = 1; 792 int32 enterpriseId = 1;
793 - int32 clientId = 2; 793 + repeated int32 clientId = 2;
794 } 794 }
795 795
796 -message QueryLatestMealMenuRecordByClientIdRpcResponse { 796 +message QueryLatestMealMenuRecordsByClientIdsRpcResponse {
797 - SingleMealMenuRecordRpcResponse response = 1; 797 + repeated SingleMealMenuRecordRpcResponse responses = 1;
798 } 798 }
......