package com.hfkj.config;

import cn.binarywang.wx.miniapp.api.WxMaMsgService;
import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.bean.WxMaSubscribeMessage;
import com.hfkj.common.utils.DateUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.*;

public class WxMsgConfig {

    private static Logger log = LoggerFactory.getLogger(WxMsgConfig.class);

    /**
     * 油价优惠活动通知
     * @param openId
     */
    public static void priceDiscountActivity(String content, Date startTime, Date endTime, String remarks,String openId) {
        try {
            List<WxMaSubscribeMessage.Data> list = new ArrayList<>();

            Map<String, String> param = new HashMap<>();
            param.put("thing1", content); // 活动内容
            param.put("time7", DateUtil.date2String(startTime, DateUtil.Y_M_D_HMS )); // 开始时间
            param.put("time8", DateUtil.date2String(endTime, DateUtil.Y_M_D_HMS )); // 结束时间
            param.put("thing4", remarks); // 备注

            for (String key: param.keySet()) {
                WxMaSubscribeMessage.Data msgElement = new WxMaSubscribeMessage.Data();
                msgElement.setName(key);
                msgElement.setValue(param.get(key));
                list.add(msgElement);
            }

            WxMaSubscribeMessage subscribeMessage = new WxMaSubscribeMessage();
            subscribeMessage.setToUser(openId);    // 小程序openId
            subscribeMessage.setPage("/pages/tabBar/index");
            subscribeMessage.setTemplateId("HfFdlnnNSmdMXHMo3fWKW1NXbzjAYNgdqJRS9kW-Bk0");
            subscribeMessage.setData(list);

            final WxMaService wxService = WxMaConfiguration.getMaService();
            WxMaMsgService maMsgService = wxService.getMsgService();
            maMsgService.sendSubscribeMsg(subscribeMessage);
        } catch (Exception e) {
            log.error(String.valueOf(e));
        }
    }

}
