AutoCreateMealOrderTask.java 6.33 KB
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.time.ZonedDateTime;
import java.time.temporal.TemporalAdjusters;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

@Slf4j
@Component
@RequiredArgsConstructor
public class AutoCreateMealOrderTask {

    private final MealOrderServiceRpcClient mealOrderServiceRpcClient;

    private final ClientInventoryServiceRpcClient clientInventoryServiceRpcClient;

    @Scheduled(cron = "59 59 23 ? * SUN")
    public void executeTask() {
        final int enterpriseId = 449;
        final var today = LocalDate.now();
        final var thisWeekStart = today.with(DayOfWeek.MONDAY).atStartOfDay().atZone(DateUtil.CHINA_ZONE);
        final var thisWeekEnd = today.with(DayOfWeek.SUNDAY).atTime(LocalTime.MAX).atZone(DateUtil.CHINA_ZONE);
        final var nextWeekStart = today.with(TemporalAdjusters.next(DayOfWeek.MONDAY)).atStartOfDay().atZone(DateUtil.CHINA_ZONE);

        // 查询本周已订餐学生 ID
        final var mealOrders = mealOrderServiceRpcClient.queryMealOrdersByCondition(enterpriseId, null, thisWeekStart.toInstant().toEpochMilli(), thisWeekEnd.toInstant().toEpochMilli());
        final var dinerIds = mealOrders.stream().map(SingleMealOrderRpcResponse::getDinerId).collect(Collectors.toSet());

        // 所有学生 & 未订餐学生
        final var allDiners = mealOrderServiceRpcClient.queryDinersByCondition(enterpriseId, null, null, null);
        final var notReservedDiners = allDiners.stream().filter(diner -> !dinerIds.contains(diner.getId())).toList();
        if (notReservedDiners.isEmpty()) {
            log.info("无未订餐学生,跳过自动订餐任务");
            return;
        }
        // 所需关联数据查询
        final var clientIds = notReservedDiners.stream().map(SingleDinerRpcResponse::getClientId).distinct().toList();
        final var classIds = notReservedDiners.stream().map(SingleDinerRpcResponse::getClassId).distinct().toList();
        final var clientMap = clientInventoryServiceRpcClient.getClientsByIds(enterpriseId, clientIds).getClientsList().stream().collect(Collectors.toMap(SingleClientRpcResponse::getId, Function.identity()));
        final var gradeClassMap = mealOrderServiceRpcClient.getGradeClassesByIds(enterpriseId, classIds).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 = notReservedDiners.stream().map(diner -> toMealOrderCreation(diner, clientMap, gradeClassMap, latestMealMenuRecordMap, nextWeekStart)).filter(Objects::nonNull).toList();
        if (creations.isEmpty()) {
            log.info("无可创建自动订餐项,跳过任务");
            return;
        }
        final var loginInfo = LoginContextHolder.LoginInfo.builder()
                .id(0)
                .enterpriseId(enterpriseId)
                .loginSource(LoginSourceEnum.MINI_PROGRAM)
                .build();
        mealOrderServiceRpcClient.batchCreateMealOrders(loginInfo, creations);
        log.info("本周未订餐学生已自动补单 {} 条", creations.size());
    }

    private MealOrderCreation toMealOrderCreation(SingleDinerRpcResponse diner,
                                                  Map<Integer, SingleClientRpcResponse> clientMap,
                                                  Map<Integer, SingleGradeClassRpcResponse> classMap,
                                                  Map<Integer, SingleMealMenuRecordRpcResponse> menuMap,
                                                  ZonedDateTime startOfNextWeek) {
        final var menu = menuMap.get(diner.getClientId());
        final var client = clientMap.get(diner.getClientId());
        final var gradeClass = classMap.get(diner.getClassId());
        if (menu == null || client == null || gradeClass == null) return null;
        return MealOrderCreation.newBuilder()
                .setClientId(diner.getClientId())
                .setMenuId(menu.getId())
                .setAccountId(0)
                .setOrderMethod(MealOrderOrderMethodEnum.MEAL_ORDER_METHOD_SYSTEM)
                .setDinerId(diner.getId())
                .setDinerName(diner.getName())
                .setClientName(client.getName())
                .setGradeName(gradeClass.getGradeName())
                .setClassName(gradeClass.getClassName())
                .setStudentNo(diner.getStudentNo())
                .setReserveDate(Instant.now().toEpochMilli())
                .addAllMeals(buildNextWeekMeals(startOfNextWeek))
                .build();
    }

    private List<MenuSchedule> buildNextWeekMeals(ZonedDateTime nextWeekStart) {
        return IntStream.range(0, 5)
                .mapToObj(i -> MenuSchedule.newBuilder()
                        .setDate(nextWeekStart.plusDays(i).format(DateUtil.YYYY_MM_DD_LEFT_SYMBOL))
                        .setMenu("A")
                        .build())
                .toList();
    }
}