OrderReminderServiceImpl.java 3.56 KB
package com.infoloop.tianting.service.impl;

import com.infoloop.tianting.model.dto.WxUserDto.SendSubscriptionMessageResponse;
import com.infoloop.tianting.service.OrderReminderService;
import com.infoloop.tianting.service.WxMiniProgramService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

import static com.infoloop.tianting.constant.ConfigConstants.MINI_PROGRAM_ORDER_REMINDER_TEMPLATE_ID;

/**
 * 订餐提醒服务实现
 * 使用小程序订阅消息接口发送提醒
 * 
 * 小程序模板字段(模板ID: BCROwaS_oj1_b0fzc_qrgvtSeCtfxctghcLMu-Obkfw):
 * - thing8: 用餐类型(如:下次订单即将开始)
 * - time1: 订餐时间(如:13:00~21:00)
 * - thing2: 温馨提示(如:为了保证您明日正常用餐,立即订餐!)
 * 
 * 注意:
 * 1. 使用的是小程序的 access_token 和 template_id
 * 2. openId 是用户在小程序下的 openId
 */
@Slf4j
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class OrderReminderServiceImpl implements OrderReminderService {

    private final WxMiniProgramService wxMiniProgramService;

    @Value(MINI_PROGRAM_ORDER_REMINDER_TEMPLATE_ID)
    private String templateId;

    @Override
    public SendSubscriptionMessageResponse sendOrderReminder(String openId, 
                                                             String page,
                                                             String mealType,
                                                             String orderTime,
                                                             String tips) {
        log.info("Sending order reminder via MiniProgram to openId: {}, page: {}, mealType: {}, orderTime: {}, tips: {}", 
                openId, page, mealType, orderTime, tips);
        
        // 构建小程序订阅消息模板数据
        Map<String, Object> data = new HashMap<>();
        
        // thing8: 用餐类型(如:下次订单即将开始)
        Map<String, String> thing8Value = new HashMap<>();
        thing8Value.put("value", mealType);
        data.put("thing8", thing8Value);
        
        // time1: 订餐时间(如:13:00~21:00)
        Map<String, String> time1Value = new HashMap<>();
        time1Value.put("value", orderTime);
        data.put("time1", time1Value);
        
        // thing2: 温馨提示
        Map<String, String> thing2Value = new HashMap<>();
        thing2Value.put("value", tips);
        data.put("thing2", thing2Value);
        
        try {
            // 使用小程序订阅消息接口
            SendSubscriptionMessageResponse response = wxMiniProgramService.sendSubscriptionMessage(
                    openId,
                    templateId,
                    page,
                    data
            );
            
            if (response.getErrcode() != null && response.getErrcode() == 0) {
                log.info("Order reminder sent successfully via MiniProgram to openId: {}", openId);
            } else {
                log.warn("Order reminder sent with error. openId: {}, errcode: {}, errmsg: {}", 
                        openId, response.getErrcode(), response.getErrmsg());
            }
            
            return response;
        } catch (Exception e) {
            log.error("Failed to send order reminder via MiniProgram to openId: {}", openId, e);
            throw e;
        }
    }
}