package com.fs.core.service.impl;

import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.bean.WxMaSubscribeMessage;
import com.fs.core.config.WxMaConfiguration;
import com.fs.core.service.IMsgTemeplateService;

import com.fs.his.config.FsSysConfig;
import com.fs.his.utils.ConfigUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Map;


@Service
public class MsgTemeplateServiceImpl implements IMsgTemeplateService
{
    @Autowired
    private ConfigUtil configUtil;


    @Override
    public boolean yuyueTemplate(Long liveId, String openId, Map<String,Object> data) {

        WxMaSubscribeMessage subscribeMessage = new WxMaSubscribeMessage();
        subscribeMessage.setTemplateId("jDxO1HuHMdQE_XqBS9JvEBNkg-fd09uaRjQLSucdoRc");
        //查询推送用户openid （可以调用根据code换openid接口)
        subscribeMessage.setToUser(openId);
        subscribeMessage.setPage("pages/home/yuyuePage?liveId="+liveId);
        ArrayList<WxMaSubscribeMessage.MsgData> wxMaSubscribeData = new ArrayList<>();

        WxMaSubscribeMessage.MsgData wxMaSubscribeData1 = new WxMaSubscribeMessage.MsgData();
        wxMaSubscribeData1.setName("thing1");
        wxMaSubscribeData1.setValue(data.get("title").toString());
        wxMaSubscribeData.add(wxMaSubscribeData1);

        WxMaSubscribeMessage.MsgData wxMaSubscribeData2 = new WxMaSubscribeMessage.MsgData();
        wxMaSubscribeData2.setName("thing4");
        wxMaSubscribeData2.setValue(data.get("content").toString());
        wxMaSubscribeData.add(wxMaSubscribeData2);

        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        WxMaSubscribeMessage.MsgData wxMaSubscribeData3 = new WxMaSubscribeMessage.MsgData();
        wxMaSubscribeData3.setName("time3");
        wxMaSubscribeData3.setValue(dateFormat.format(data.get("time")));
        wxMaSubscribeData.add(wxMaSubscribeData3);


        //把集合给大的data
        subscribeMessage.setData(wxMaSubscribeData);
        try {
            FsSysConfig sysConfig = configUtil.getSysConfig();
            //获取微信小程序配置：
            final WxMaService wxService = WxMaConfiguration.getMaService(sysConfig.getAppid());
            //进行推送
            wxService.getMsgService().sendSubscribeMsg(subscribeMessage);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return true;
    }
}
