package com.bofeng.service;

import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
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 java.util.List;
import java.util.Map;

@Service
@Slf4j
public class TemplateMessageService {


    /*
     * 预定信息
     * 场地:{{placeName.DATA}}
     * 房间:{{roomName.DATA}}
     * 销售员:{{saleName.DATA}}
     * 客户:{{customerName.DATA}}
     * 时间:{{time.DATA}}
     * */

    /*
     * 修改预定信息
     * 场地:{{placeName.DATA}}
     * 包厢:{{roomName.DATA}}
     * 销售:{{saleName.DATA}}
     * 客户:{{customerName.DATA}}
     * 原时间:{{oldTime.DATA}}
     * 新时间:{{newTime.DATA}}
     * */

    /*
     * 结账信息
     * 场地:{{placeName.DATA}}
     * 包厢:{{roomName.DATA}}
     * 销售:{{saleName.DATA}}
     * 客户:{{customerName.DATA}}
     * 金额:{{payAmount.DATA}}
     * 时间:{{time.DATA}}
     * */

    /*
     * 结算信息
     * 场地:{{placeName.DATA}}
     * 金额:{{payAmount.DATA}}
     * 订单数:{{orderCount.DATA}}
     * 起始时间:{{startTime.DATA}}
     * 截止时间:{{endTime.DATA}}
     * */

    @Value("${yvan.wechat.templates.预定信息}")
    private String newMarkMessageId;

    @Value("${yvan.wechat.templates.修改预定信息}")
    private String updateMarkMessageId;

    @Value("${yvan.wechat.templates.结账信息}")
    private String accountMessageId;

    @Value("${yvan.wechat.templates.结算信息}")
    private String settleMessageId;


    @Autowired
    private WxMpService wxMpService;

    public void sendNewMarkMessage(String openId, String url, Map<String, String> data) {

        sendTemplateMessage(openId, newMarkMessageId, url, data);
    }

    public void sendUpdateMarkMessage(String openId, String url, Map<String, String> data) {
        sendTemplateMessage(openId, updateMarkMessageId, url, data);
    }

    public void sendSettleMessage(String openId, String url, Map<String, String> data) {
        sendTemplateMessage(openId, settleMessageId, url, data);
    }

    public void sendAccountMessage(String openId, String url, Map<String, String> data) {
        sendTemplateMessage(openId, accountMessageId, url, data);
    }

    public void sendTemplateMessage(String openId, String templateId, String url, Map<String, String> data) {
        try {
            WxMpTemplateMessage wxMpTemplateMessage = new WxMpTemplateMessage();
            wxMpTemplateMessage.setToUser(openId);
            wxMpTemplateMessage.setTemplateId(templateId);
            wxMpTemplateMessage.setUrl(url);
            List<WxMpTemplateData> wxMpTemplateDataList = Lists.newArrayList();

            for (Map.Entry<String, String> m : data.entrySet()) {
                wxMpTemplateDataList.add(new WxMpTemplateData(m.getKey(), m.getValue()));
            }
            wxMpTemplateMessage.setData(wxMpTemplateDataList);
            wxMpService.getTemplateMsgService().sendTemplateMsg(wxMpTemplateMessage);

        } catch (Exception e) {
            log.error("sendTemplateMessageError", e);

        }
    }
}
