zhuyifan

自动创建餐单

package com.infoloop.tianting.logic.task;
import com.infoloop.tianting.clientinventoryservice.SingleClientRpcResponse;
import com.infoloop.tianting.context.LoginContextHolder;
import com.infoloop.tianting.enums.LoginSourceEnum;
import com.infoloop.tianting.mealorderservice.MealOrderCreation;
import com.infoloop.tianting.mealorderservice.MealOrderOrderMethodEnum;
import com.infoloop.tianting.mealorderservice.MenuSchedule;
import com.infoloop.tianting.mealorderservice.SingleDinerRpcResponse;
import com.infoloop.tianting.mealorderservice.SingleGradeClassRpcResponse;
import com.infoloop.tianting.mealorderservice.SingleMealMenuRecordRpcResponse;
import com.infoloop.tianting.mealorderservice.SingleMealOrderRpcResponse;
import com.infoloop.tianting.service.client.ClientInventoryServiceRpcClient;
import com.infoloop.tianting.service.client.MealOrderServiceRpcClient;
import com.infoloop.tianting.utils.DateUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.time.DayOfWeek;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.function.Function;
import java.util.stream.Collectors;
@Slf4j
@Component
@RequiredArgsConstructor
public class AutoCreateMealOrderTask {
private final MealOrderServiceRpcClient mealOrderServiceRpcClient;
private final ClientInventoryServiceRpcClient clientInventoryServiceRpcClient;
@Scheduled(cron = "0 0 13 ? * WED")
public void executeTask() {
int enterpriseId = 449;
final var today = LocalDate.now();
final var zonedDateTime = today.with(DayOfWeek.MONDAY).atStartOfDay().atZone(DateUtil.CHINA_ZONE);
final var startTimestamp = zonedDateTime.toInstant().toEpochMilli();
final var endTimestamp = today.with(DayOfWeek.SUNDAY).atTime(LocalTime.MAX).atZone(DateUtil.CHINA_ZONE).toInstant().toEpochMilli();
final var mealOrders = mealOrderServiceRpcClient.queryMealOrdersByCondition(449, null, startTimestamp, endTimestamp);
final var dinerIds = mealOrders.stream().map(SingleMealOrderRpcResponse::getDinerId).toList();
final var allDiners = mealOrderServiceRpcClient.queryDinersByCondition(enterpriseId, null, null, null);
final var notReservedDiners = allDiners.stream().filter(diner -> !dinerIds.contains(diner.getId())).toList();
final var clientIds = notReservedDiners.stream().map(SingleDinerRpcResponse::getClientId).distinct().toList();
final var clientMap = clientInventoryServiceRpcClient.getClientsByIds(enterpriseId, clientIds).getClientsList().stream().collect(Collectors.toMap(SingleClientRpcResponse::getId, Function.identity()));
final var gradeClassMap = mealOrderServiceRpcClient.getGradeClassesByIds(enterpriseId, notReservedDiners.stream().map(SingleDinerRpcResponse::getClassId).distinct().toList()).stream().collect(Collectors.toMap(SingleGradeClassRpcResponse::getId, Function.identity()));
final var latestMealMenuRecordMap = mealOrderServiceRpcClient.queryLatestMealMenuRecordsByClientIds(enterpriseId, clientIds).stream().collect(Collectors.toMap(SingleMealMenuRecordRpcResponse::getClientId, Function.identity()));
final var creations = new ArrayList<MealOrderCreation>();
for (var diner : notReservedDiners) {
final var mealMenuRecordRpcResponse = latestMealMenuRecordMap.get(diner.getClientId());
if (mealMenuRecordRpcResponse == null) {
continue;
}
final var singleClientRpcResponse = clientMap.get(diner.getClientId());
if (singleClientRpcResponse == null) {
continue;
}
final var singleGradeClassRpcResponse = gradeClassMap.get(diner.getClassId());
if (singleGradeClassRpcResponse == null) {
continue;
}
MealOrderCreation.newBuilder()
.setClientId(diner.getClientId())
.setMenuId(mealMenuRecordRpcResponse.getId())
.setAccountId(0)
.setOrderMethod(MealOrderOrderMethodEnum.MEAL_ORDER_METHOD_SYSTEM)
.setDinerId(diner.getId())
.setDinerName(diner.getName())
.setClientName(singleClientRpcResponse.getName())
.setGradeName(singleGradeClassRpcResponse.getGradeName())
.setClassName(singleGradeClassRpcResponse.getClassName())
.setStudentNo(diner.getStudentNo())
.setReserveDate(Instant.now().toEpochMilli())
.addMeals(MenuSchedule.newBuilder().setDate(zonedDateTime.format(DateUtil.YYYY_MM_DD_LEFT_SYMBOL)).setMenu("A").build())
.addMeals(MenuSchedule.newBuilder().setDate(zonedDateTime.plusDays(1).format(DateUtil.YYYY_MM_DD_LEFT_SYMBOL)).setMenu("A").build())
.addMeals(MenuSchedule.newBuilder().setDate(zonedDateTime.plusDays(2).format(DateUtil.YYYY_MM_DD_LEFT_SYMBOL)).setMenu("A").build())
.addMeals(MenuSchedule.newBuilder().setDate(zonedDateTime.plusDays(3).format(DateUtil.YYYY_MM_DD_LEFT_SYMBOL)).setMenu("A").build())
.addMeals(MenuSchedule.newBuilder().setDate(zonedDateTime.plusDays(4).format(DateUtil.YYYY_MM_DD_LEFT_SYMBOL)).setMenu("A").build())
.build();
}
final var loginInfo = LoginContextHolder.LoginInfo.builder()
.id(0)
.enterpriseId(enterpriseId)
.loginSource(LoginSourceEnum.MINI_PROGRAM)
.build();
mealOrderServiceRpcClient.batchCreateMealOrders(loginInfo, creations);
}
}
......@@ -33,7 +33,7 @@ import com.infoloop.tianting.mealorderservice.MpAccountDinerRefCreation;
import com.infoloop.tianting.mealorderservice.MpAccountModification;
import com.infoloop.tianting.mealorderservice.QueryDinersByConditionRpcRequest;
import com.infoloop.tianting.mealorderservice.QueryGradeClassesByConditionRpcRequest;
import com.infoloop.tianting.mealorderservice.QueryLatestMealMenuRecordByClientIdRpcRequest;
import com.infoloop.tianting.mealorderservice.QueryLatestMealMenuRecordsByClientIdsRpcRequest;
import com.infoloop.tianting.mealorderservice.QueryMealOrdersByConditionRpcRequest;
import com.infoloop.tianting.mealorderservice.QueryMpAccountDinerRefsByConditionRpcRequest;
import com.infoloop.tianting.mealorderservice.QueryReserveMealOrdersByDinerIdsRpcRequest;
......@@ -48,10 +48,13 @@ import com.infoloop.tianting.mealorderservice.UpdateDinerRpcResponse;
import com.infoloop.tianting.mealorderservice.UpdateMpAccountRpcRequest;
import com.infoloop.tianting.mealorderservice.UpdateMpAccountRpcResponse;
import lombok.RequiredArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Nullable;
import java.util.Collections;
import java.util.List;
@Service
......@@ -129,9 +132,9 @@ public class MealOrderServiceRpcClient {
public List<SingleDinerRpcResponse> queryDinersByCondition(int enterpriseId, List<Integer> clientIds, List<Integer> gradeClassIds, @Nullable String studentNo) {
return mealOrderServiceRpcBlockingStub.queryDinersByCondition(QueryDinersByConditionRpcRequest.newBuilder()
.setEnterpriseId(enterpriseId)
.setShouldFilterClientIds(!clientIds.isEmpty()).addAllClientIds(clientIds)
.setShouldFilterClassIds(!gradeClassIds.isEmpty()).addAllClassIds(gradeClassIds)
.setShouldFilterStudentNo(studentNo != null).setStudentNo(studentNo == null ? "" : studentNo)
.setShouldFilterClientIds(CollectionUtils.isNotEmpty(clientIds)).addAllClientIds(CollectionUtils.isEmpty(clientIds) ? Collections.emptyList() : clientIds)
.setShouldFilterClassIds(CollectionUtils.isNotEmpty(gradeClassIds)).addAllClassIds(CollectionUtils.isNotEmpty(gradeClassIds) ? gradeClassIds : Collections.emptyList())
.setShouldFilterStudentNo(StringUtils.isNotEmpty(studentNo)).setStudentNo(StringUtils.isNotEmpty(studentNo) ? studentNo : "")
.build()).getResponsesList();
}
......@@ -211,16 +214,11 @@ public class MealOrderServiceRpcClient {
.build()).getResponsesList();
}
@Nullable
public SingleMealMenuRecordRpcResponse queryLatestMealMenuRecordByClientId(int enterpriseId, int id) {
final var queryLatestMealMenuRecordByClientIdRpcResponse = mealOrderServiceRpcBlockingStub.queryLatestMealMenuRecordByClientId(QueryLatestMealMenuRecordByClientIdRpcRequest.newBuilder()
public List<SingleMealMenuRecordRpcResponse> queryLatestMealMenuRecordsByClientIds(int enterpriseId, List<Integer> ids) {
return mealOrderServiceRpcBlockingStub.queryLatestMealMenuRecordsByClientIds(QueryLatestMealMenuRecordsByClientIdsRpcRequest.newBuilder()
.setEnterpriseId(enterpriseId)
.setClientId(id)
.build());
if (!queryLatestMealMenuRecordByClientIdRpcResponse.hasResponse()) {
return null;
}
return queryLatestMealMenuRecordByClientIdRpcResponse.getResponse();
.addAllClientId(ids)
.build()).getResponsesList();
}
@Nullable
......@@ -253,10 +251,12 @@ public class MealOrderServiceRpcClient {
.build());
}
public List<SingleMealOrderRpcResponse> queryMealOrdersByCondition(int enterpriseId, int accountId) {
public List<SingleMealOrderRpcResponse> queryMealOrdersByCondition(int enterpriseId, Integer accountId, Long reserveDateStart, Long reserveDateEnd) {
return mealOrderServiceRpcBlockingStub.queryMealOrdersByCondition(QueryMealOrdersByConditionRpcRequest.newBuilder()
.setEnterpriseId(enterpriseId)
.setShouldFilterAccountIds(true).addAccountIds(accountId)
.setShouldFilterAccountIds(accountId != null).addAccountIds(accountId == null ? 0 : accountId)
.setShouldFilterReserveDateStart(true).setReserveDateStart(reserveDateStart == null ? 0 : reserveDateStart)
.setShouldFilterReserveDateEnd(true).setReserveDateEnd(reserveDateEnd == null ? 0 : reserveDateEnd)
.build()).getResponsesList();
}
}
......
......@@ -39,7 +39,7 @@ public class MealOrderServiceImpl implements MealOrderService {
@Override
public List<MealOrderVO> queryMealOrders(MealOrderDTO.QueryMealOrderDTO queryMealOrderDTO) {
final var loginInfo = LoginContextHolder.getLoginInfo();
var mealOrderRpcResponses = mealOrderServiceRpcClient.queryMealOrdersByCondition(loginInfo.getEnterpriseId(), loginInfo.getId());
var mealOrderRpcResponses = mealOrderServiceRpcClient.queryMealOrdersByCondition(loginInfo.getEnterpriseId(), loginInfo.getId(), null, null);
final var today = LocalDate.now();
final var startTimestamp = today.with(DayOfWeek.MONDAY).atStartOfDay().atZone(DateUtil.CHINA_ZONE).toInstant().toEpochMilli();
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 {
if (gradeClassById == null) {
throw ClientEndExceptions.IncorrectRequestValue.build(ErrorCodeEnum.GRADE_CLASS_NOT_FOUND);
}
final var latestMealMenuRecord = mealOrderServiceRpcClient.queryLatestMealMenuRecordByClientId(loginInfo.getEnterpriseId(), dinerById.getClientId());
if (latestMealMenuRecord == null) {
final var latestMealMenuRecords = mealOrderServiceRpcClient.queryLatestMealMenuRecordsByClientIds(loginInfo.getEnterpriseId(), Collections.singletonList(dinerById.getClientId()));
if (latestMealMenuRecords.isEmpty()) {
throw ClientEndExceptions.IncorrectRequestValue.build(ErrorCodeEnum.MEAL_MENU_NOT_FOUND);
}
final var mealOrder = mealOrderServiceRpcClient.createMealOrder(loginInfo, MealOrderCreation.newBuilder()
.setClientId(dinerById.getClientId())
.setMenuId(latestMealMenuRecord.getId())
.setMenuId(latestMealMenuRecords.get(0).getId())
.setAccountId(loginInfo.getId())
.setOrderMethod(MealOrderOrderMethodEnum.MEAL_ORDER_METHOD_MICRO)
.setDinerId(createMealOrderDTO.getDinerId())
......
......@@ -215,8 +215,8 @@ public class MenuServiceImpl implements MenuService {
@Override
public String queryClientLatestMealMenuRecord(SingleClientRpcResponse client) {
final var singleMealMenuRecordRpcResponse = mealOrderServiceRpcClient.queryLatestMealMenuRecordByClientId(LoginContextHolder.getEnterpriseId(), client.getId());
return singleMealMenuRecordRpcResponse == null ? "" : singleMealMenuRecordRpcResponse.getImageUrl();
final var singleMealMenuRecordRpcResponses = mealOrderServiceRpcClient.queryLatestMealMenuRecordsByClientIds(LoginContextHolder.getEnterpriseId(), List.of(client.getId()));
return singleMealMenuRecordRpcResponses.isEmpty() ? "" : singleMealMenuRecordRpcResponses.get(0).getImageUrl();
}
......
......@@ -51,7 +51,7 @@ service MealOrderServiceRpc {
rpc GetMealMenuRecordsByIds (GetMealMenuRecordsByIdsRpcRequest) returns (GetMealMenuRecordsByIdsRpcResponse) {}
rpc QueryMealMenuRecordsByCondition (QueryMealMenuRecordsByConditionRpcRequest) returns (QueryMealMenuRecordsByConditionRpcResponse) {}
rpc CreateMealMenuRecord (CreateMealMenuRecordRpcRequest) returns (CreateMealMenuRecordRpcResponse) {}
rpc QueryLatestMealMenuRecordByClientId (QueryLatestMealMenuRecordByClientIdRpcRequest) returns (QueryLatestMealMenuRecordByClientIdRpcResponse) {}
rpc QueryLatestMealMenuRecordsByClientIds (QueryLatestMealMenuRecordsByClientIdsRpcRequest) returns (QueryLatestMealMenuRecordsByClientIdsRpcResponse) {}
}
enum MealOrderOrderMethodEnum {
......@@ -788,11 +788,11 @@ message CreateMealMenuRecordRpcResponse {
bool isCreated = 2;
}
message QueryLatestMealMenuRecordByClientIdRpcRequest {
message QueryLatestMealMenuRecordsByClientIdsRpcRequest {
int32 enterpriseId = 1;
int32 clientId = 2;
repeated int32 clientId = 2;
}
message QueryLatestMealMenuRecordByClientIdRpcResponse {
SingleMealMenuRecordRpcResponse response = 1;
message QueryLatestMealMenuRecordsByClientIdsRpcResponse {
repeated SingleMealMenuRecordRpcResponse responses = 1;
}
......