
package com.fs.common.event;

import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.bean.WxMaSubscribeMessage;
import cn.hutool.core.util.StrUtil;
import com.fs.wx.miniapp.config.WxMaConfiguration;
import com.fs.wx.miniapp.config.WxMaProperties;
import com.fs.store.domain.FsUser;
import com.fs.store.domain.FsWechatTemplate;
import com.fs.store.enums.WechatTempateEnum;
import com.fs.store.service.IFsUserService;
import com.fs.store.service.IFsWechatTemplateService;
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 WxMaProperties properties;
    @Autowired
    private IFsUserService userService;
    @Autowired
    IFsWechatTemplateService wechatTemplateService;

    public void cancelNotice(String orderId,String orderCode,String reason,Long uid){
        String openId = this.getUserOpenid(uid);
        if(StrUtil.isBlank(openId)) {
            return;
        }
        Map<String,String> map = new HashMap<>();
        //订单号
        map.put("character_string2",orderCode);
        map.put("phrase4",reason);
        String tempId = this.getTempId(WechatTempateEnum.CANCEL_SUCCESS.getValue());
        if(StrUtil.isNotBlank(tempId)) {
            this.sendWxMpTemplateMessage(openId, tempId, "pages_user/user/storeOrderDetail?id=" + orderId, map);
        }
    }
    public void finishNotice(String orderId, String orderCode, String remark, Date finishTime, Long uid){
        String openId = this.getUserOpenid(uid);
        if(StrUtil.isBlank(openId)) {
            return;
        }
        Map<String,String> map = new HashMap<>();
        //订单号

        map.put("character_string1",orderCode);
        map.put("thing2",remark);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        map.put("time3",sdf.format(finishTime));
        String tempId = this.getTempId(WechatTempateEnum.FINISH_SUCCESS.getValue());
        if(StrUtil.isNotBlank(tempId)) {
            this.sendWxMpTemplateMessage(openId, tempId, "pages_user/user/storeOrderDetail?id=" + orderId, map);
        }
    }

    public void deliveryNotice(String orderId, String orderCode, String deliveryName, String deliveryId, Long uid){
        String openId = this.getUserOpenid(uid);
        if(StrUtil.isBlank(openId)) {
            return;
        }
        Map<String,String> map = new HashMap<>();
        //订单号
        map.put("character_string1",orderCode);
        map.put("thing3",deliveryName);
        map.put("character_string4",deliveryId);
        String tempId = this.getTempId(WechatTempateEnum.DELIVERY_SUCCESS.getValue());
        if(StrUtil.isNotBlank(tempId)) {
            this.sendWxMpTemplateMessage(openId, tempId, "pages_user/user/storeOrderDetail?id=" + orderId, map);
        }
    }

    public void prescribeNotice(String orderId,String rpId, Date rpDate, String status,  Long uid){
        String openId = this.getUserOpenid(uid);
        if(StrUtil.isBlank(openId)) {
            return;
        }
        Map<String,String> map = new HashMap<>();
        //订单号

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm");
        map.put("character_string1",rpId);
        map.put("time5",sdf.format(rpDate));
        map.put("phrase4",status);
        String tempId = this.getTempId(WechatTempateEnum.PRESCRIBE.getValue());
        if(StrUtil.isNotBlank(tempId)) {
            this.sendWxMpTemplateMessage(openId, tempId, "pages_user/user/storeOrderDetail?id=" + orderId, 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(properties.getConfigs().get(0).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.selectFsUserById(uid);
        if(user == null) {
            return "";
        }
        return user.getMaOpenId();
    }


}
