OrderReminderServiceImpl.java 3 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;

/**
 * 订餐提醒服务实现
 * 将业务参数转换为微信订阅消息模板格式并发送
 */
@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 reminderContent, 
                                                             String orderTime,
                                                             String tips) {
        log.info("Sending order reminder to openId: {}, page: {}", openId, page);
        
        // 将业务参数转换为微信模板数据格式
        // 根据文档,模板字段为:thing1 (提醒内容), time2 (订餐时间), thing3 (温馨提示)
        Map<String, Object> data = new HashMap<>();
        
        // thing1: 提醒内容
        Map<String, String> thing1Value = new HashMap<>();
        thing1Value.put("value", reminderContent);
        data.put("thing1", thing1Value);
        
        // time2: 订餐时间
        Map<String, String> time2Value = new HashMap<>();
        time2Value.put("value", orderTime);
        data.put("time2", time2Value);
        
        // thing3: 温馨提示
        Map<String, String> thing3Value = new HashMap<>();
        thing3Value.put("value", tips);
        data.put("thing3", thing3Value);
        
        try {
            SendSubscriptionMessageResponse response = wxMiniProgramService.sendSubscriptionMessage(
                    openId, templateId, page, data);
            
            if (response.getErrcode() != null && response.getErrcode() == 0) {
                log.info("Order reminder sent successfully 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 to openId: {}", openId, e);
            throw e;
        }
    }
}