package io.hmit.service.impl;

import io.hmit.modules.serviceOrder.dto.PensionOrderDTO;
import io.hmit.service.PushMessage;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateData;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * @Description :
 * @Author : Shen Yuanfeng
 * @Date: 2021/3/12 14:40
 */

@Slf4j
@Service
public class PushMessageImpl implements PushMessage {

    @Autowired
    private WxMpService wxMpService;

    @Override
    public void sendMsg(PensionOrderDTO pensionOrderDTO, String openId) {
        WxMpTemplateMessage templateMessage = new WxMpTemplateMessage();
        templateMessage.setTemplateId("GsGydVGl-vzOv5aA0LaIdA-FcMM88TEpe37U-lZ6HJ8");
        templateMessage.setToUser(openId);

        List<WxMpTemplateData> data = Arrays.asList(
                new WxMpTemplateData("first", "订单预约成功！"),
                new WxMpTemplateData("keyword1", pensionOrderDTO.getServiceName()),
                new WxMpTemplateData("keyword2", pensionOrderDTO.getAppointmentTime().toString()),
                new WxMpTemplateData("keyword3", pensionOrderDTO.getStatusName())
        );
        templateMessage.setData(data);
        try{
            wxMpService.getTemplateMsgService().sendTemplateMsg(templateMessage);
        }catch (WxErrorException e){
            log.error("【微信模板消息】发送失败，{}", e);
        }
    }
}
