package io.hmit.service.impl;

import io.hmit.config.WeChatMpConfig;
import io.hmit.config.WechatAccountConfig;
import io.hmit.modules.serviceOrder.dto.PensionOrderDTO;
import io.hmit.service.PushMessage;
import io.hmit.service.SmsService;
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.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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

@Slf4j
@Service
public class PushMessageImpl implements PushMessage {

    @Value("${SYSURL.url}")
    private String SYSURL;

    @Autowired
    private WxMpService wxMpService;

    @Autowired
    private WechatAccountConfig wechatAccountConfig;

    @Autowired
    private SmsService smsService;

    @Override
    public void sendWeChatMsg(PensionOrderDTO pensionOrderDTO, String openId, String flag) {
        WxMpTemplateMessage templateMessage = new WxMpTemplateMessage();
        templateMessage.setTemplateId(wechatAccountConfig.getTemplateId());
        templateMessage.setToUser(openId);

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm");
        String appointmentTime = sdf.format(pensionOrderDTO.getAppointmentTime());

        List<WxMpTemplateData> data = Arrays.asList(
                new WxMpTemplateData("first", "【智慧养老】您的订单提醒"),
                new WxMpTemplateData("keyword1", pensionOrderDTO.getId().toString()),
                new WxMpTemplateData("keyword2", pensionOrderDTO.getServiceName()),
                new WxMpTemplateData("keyword3", appointmentTime),
                new WxMpTemplateData("remark", "当前状态：" + pensionOrderDTO.getStatusName())
        );
        templateMessage.setData(data);
        String url = SYSURL + "/#/orderDetail/?openid=" + openId + "&id=" + pensionOrderDTO.getId()+"&flag="+flag;
        templateMessage.setUrl(url);
        try{
            wxMpService.getTemplateMsgService().sendTemplateMsg(templateMessage);
        }catch (WxErrorException e){
            log.error("【微信模板消息】发送失败，{}", e);
        }
    }

    @Override
    public void sendSMSMsg(PensionOrderDTO pensionOrderDTO, String receiver) {
        try {
            SimpleDateFormat sdfYMD = new SimpleDateFormat("yyyy-MM-dd");
            SimpleDateFormat sdfHms = new SimpleDateFormat("HH:mm:ss");
            String ymd = sdfYMD.format(pensionOrderDTO.getAppointmentTime());
            String hms = sdfHms.format(pensionOrderDTO.getAppointmentTime());

            Map<String,Object> params = new HashMap<>();
            params.put("tempId","9ba5e502b15d4fa2b52d287da00b7735");

            params.put("num", "18758806780");
            StringBuffer sb = new StringBuffer();
            sb.append("[");
            sb.append("\""+"8"+"\",");
            sb.append("\""+ymd+"\",");
            sb.append("\""+hms+"\",");
            sb.append("\""+pensionOrderDTO.getServiceName()+"\",");
            sb.append("\""+pensionOrderDTO.getAppointmentPhone()+"\"]");
            params.put("tempParams",sb.toString());
            smsService.sendSms(params);
        }catch (Exception ignored){}
    }
}
