
package com.fs.event;

import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.bean.WxMaSubscribeMessage;
import cn.hutool.core.util.StrUtil;
import com.fs.core.config.WxMaConfiguration;
import com.fs.his.domain.FsUser;
import com.fs.his.service.IFsUserService;
import com.fs.his.utils.ConfigUtil;
import lombok.extern.slf4j.Slf4j;
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.HashMap;
import java.util.Map;

/**
 * @ClassName 微信公众号模板通知
 **/
@Slf4j
@Service
public class WeixinTemplateService {

    @Autowired
    private ConfigUtil configUtil;
    @Autowired
    private IFsUserService userService;


    public void cancelOrderNotice(String orderId,String title,String remark,Long uid){
        String openId = this.getUserOpenid(uid);
        if(StrUtil.isBlank(openId)) {
            return;
        }
        Map<String,String> map = new HashMap<>();
        //订单号
        map.put("thing1",title);
        map.put("thing4",remark);
        String tempId = "YyaBF62vH1GPKY0tume8C2C5UETxcKrVsaTKdBW5eok";
        if(StrUtil.isNotBlank(tempId)) {
            this.sendWxMpTemplateMessage(openId, tempId, "pages_order/inquiryOrderDetails?orderId=" + orderId, map);
        }
    }
    public void receiveOrderNotice(String orderId, String title, String remark, Long uid){
        String openId = this.getUserOpenid(uid);
        if(StrUtil.isBlank(openId)) {
            return;
        }
        Map<String,String> map = new HashMap<>();
        //订单号
        map.put("thing1",title);
        map.put("thing4",remark);
        String tempId = "YyaBF62vH1GPKY0tume8C2C5UETxcKrVsaTKdBW5eok";
        if(StrUtil.isNotBlank(tempId)) {
            this.sendWxMpTemplateMessage(openId, tempId, "pages_order/inquiryOrderDetails?orderId=" + orderId, map);
        }
    }
    //待支付推送 套餐包
    public void unPayPackageOrderNotice(String orderId,Date createTime, String orderCode, String remark, Long uid){
        String openId = this.getUserOpenid(uid);
        if(StrUtil.isBlank(openId)) {
            return;
        }
        Map<String,String> map = new HashMap<>();
        //订单号 2020年5月10日 14:35:40 2020年5月10日14:35:40
        SimpleDateFormat df = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
        map.put("time7",df.format(createTime));
        map.put("character_string9",orderCode);
        map.put("thing4",remark);
        String tempId = "jARl4BpoBkRu-2MxPMkQVhIfGMG0V9qW-X3V_7NtEOU";
        if(StrUtil.isNotBlank(tempId)) {
            this.sendWxMpTemplateMessage(openId, tempId, "pages_order/packageOrderDetails?orderId=" + orderId, map);
        }
    }
    //随访创建咨询
    public void followCreateNotice( String followId,String title, String remark, Long uid){
        String openId = this.getUserOpenid(uid);
        if(StrUtil.isBlank(openId)) {
            return;
        }
        Map<String,String> map = new HashMap<>();
        map.put("thing1",title);
        map.put("thing4",remark);
        String tempId = "YyaBF62vH1GPKY0tume8C2C5UETxcKrVsaTKdBW5eok";
        if(StrUtil.isNotBlank(tempId)) {
            this.sendWxMpTemplateMessage(openId, tempId, "pages_user/followList", map);
        }
    }
    //医生回复咨询
    public void doctorReplyNotice( String title, String remark, Long uid){
        String openId = this.getUserOpenid(uid);
        if(StrUtil.isBlank(openId)) {
            return;
        }
        Map<String,String> map = new HashMap<>();
        map.put("thing1",title);
        map.put("thing4",remark);
        String tempId = "YyaBF62vH1GPKY0tume8C2C5UETxcKrVsaTKdBW5eok";
        if(StrUtil.isNotBlank(tempId)) {
            this.sendWxMpTemplateMessage(openId, tempId, "pages/TUIKit/TUIPages/TUIConversation/index" , map);
        }
    }

    private void sendWxMpTemplateMessage(String openId, String templateId, String pages, Map<String,String> map){
        WxMaSubscribeMessage subscribeMessage = new WxMaSubscribeMessage();
       //获取模板配置文件
       //模板消息id
       subscribeMessage.setTemplateId(templateId);
       subscribeMessage.setToUser(openId);

       subscribeMessage.setPage(pages);
        ArrayList<WxMaSubscribeMessage.MsgData> wxMaSubscribeData = new ArrayList<>();

        for (Map.Entry<String, String> entry : map.entrySet()) {
            System.out.println("key = " + entry.getKey() + ", value = " + entry.getValue());
            WxMaSubscribeMessage.MsgData data = new WxMaSubscribeMessage.MsgData();
            data.setName(entry.getKey());
            data.setValue(entry.getValue());
            wxMaSubscribeData.add(data);
        }
       //把集合给大的data
       subscribeMessage.setData(wxMaSubscribeData);
       try {
           //获取微信小程序配置：
           final WxMaService wxService = WxMaConfiguration.getMaService(configUtil.getSysConfig().getAppid());
           //进行推送
           wxService.getMsgService().sendSubscribeMsg(subscribeMessage);
       } catch (Exception e) {
           e.printStackTrace();
       }

    }
//    /**
//     * 获取模板消息id
//     * @param key 模板key
//     * @return string
//     */
//    private String getTempId(String key){
//        FsWechatTemplate template=wechatTemplateService.selectFsWechatTemplateByKey(key);
//        if(template!=null){
//            return template.getTempId();
//        }
//        else{
//            return "";
//        }
//
//    }
    /**
     * 获取openid
     */
    private String getUserOpenid(Long uid){
        FsUser user = userService.selectFsUserByUserId(uid);
        if(user == null) {
            return "";
        }
        return user.getMaOpenId();
    }


}
