

package com.tmerclub.cloud.biz.service.impl;

import cn.binarywang.wx.miniapp.bean.WxMaTemplateData;
import cn.binarywang.wx.miniapp.bean.WxMaUniformMessage;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.tmerclub.cloud.api.auth.constant.SysTypeEnum;
import com.tmerclub.cloud.api.multishop.bo.EsShopDetailBO;
import com.tmerclub.cloud.api.multishop.feign.ShopDetailFeignClient;
import com.tmerclub.cloud.api.platform.config.FeignShopConfig;
import com.tmerclub.cloud.api.supplier.bo.SupplierDetailBO;
import com.tmerclub.cloud.api.supplier.feign.SupplierDetailFeignClient;
import com.tmerclub.cloud.api.user.feign.UserFeignClient;
import com.tmerclub.cloud.api.user.vo.UserApiVO;
import com.tmerclub.cloud.biz.config.WxConfig;
import com.tmerclub.cloud.biz.constant.NotifyType;
import com.tmerclub.cloud.biz.constant.RemindType;
import com.tmerclub.cloud.biz.model.NotifyLog;
import com.tmerclub.cloud.biz.service.*;
import com.tmerclub.cloud.biz.vo.NotifyParamVO;
import com.tmerclub.cloud.biz.vo.NotifyTemplateRemindVO;
import com.tmerclub.cloud.biz.vo.NotifyTemplateVO;
import com.tmerclub.cloud.common.constant.Constant;
import com.tmerclub.cloud.common.constant.SendTypeEnum;
import com.tmerclub.cloud.common.constant.StatusEnum;
import com.tmerclub.cloud.common.order.vo.SendNotifyBO;
import com.tmerclub.cloud.common.response.ServerResponseEntity;
import com.tmerclub.cloud.common.util.BeanUtil;
import me.chanjar.weixin.common.error.WxErrorException;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.BooleanUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;


/**
 * 统一发送通知的事件
 *
 * @author lhd
 */
@Service
public class SendMessageServiceImpl implements SendMessageService {

    private static final Logger logger = LoggerFactory.getLogger(SendMessageServiceImpl.class);

    @Autowired
    private NotifyLogService notifyLogService;
    @Autowired
    private SmsLogService smsLogService;
    @Autowired
    private FeignShopConfig feignShopConfig;
    @Autowired
    private WxConfig wxConfig;
    @Autowired
    private NotifyTemplateService notifyTemplateService;
    @DubboReference
    private ShopDetailFeignClient shopDetailFeignClient;

    @DubboReference
    private SupplierDetailFeignClient supplierDetailFeignClient;
    @DubboReference
    private UserFeignClient userFeignClient;
    @Autowired
    private NotifyTemplateRemindService notifyTemplateRemindService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void sendMsg(NotifyParamVO notifyParamVO) {
        if (Objects.isNull(notifyParamVO)) {
            return;
        }
        //发送给供应商的消息提醒
        SupplierDetailBO supplierDetailBO = new SupplierDetailBO();
        if (Objects.isNull(notifyParamVO.getShopId()) && Objects.nonNull(notifyParamVO.getSupplierId())) {
            supplierDetailBO = supplierDetailFeignClient.getSupplierBySupplierId(notifyParamVO.getSupplierId()).getData();
            notifyParamVO.setMobile(supplierDetailBO.getContactPhone());
        }
        EsShopDetailBO shopDetailBO = shopDetailFeignClient.getShopByShopId(notifyParamVO.getShopId()).getData();
        String shopName = getAndSetShopName(notifyParamVO, supplierDetailBO, shopDetailBO);
        // 判断是什么类型，如果是发送给商家的，则修改手机号码，并且只需要推送手机号码即可
        SendTypeEnum sendTypeEnum = SendTypeEnum.instance(notifyParamVO.getSendType());
        if (Objects.nonNull(sendTypeEnum) && Objects.equals(sendTypeEnum.getType(), SendTypeEnum.RECEIPT_ORDER.getType())) {
            notifyParamVO.setMobile(shopDetailBO.getContactPhone());
        }
        //获取模板的发送方式，获取商家选择的发送方式
        NotifyTemplateVO template = notifyTemplateService.getBySendType(notifyParamVO.getSendType());
        //是否开启消息提醒
        Boolean notifyLogStatus = false;

        //判断商家端和供应商的消息提醒设置
        Integer sysType = Objects.nonNull(notifyParamVO.getShopId()) ? SysTypeEnum.MULTISHOP.value() : SysTypeEnum.SUPPLIER.value();
        Long tenantId = Objects.nonNull(notifyParamVO.getShopId()) ? notifyParamVO.getShopId() : notifyParamVO.getSupplierId();
        NotifyTemplateRemindVO notifyTemplateRemindVO = notifyTemplateRemindService.getByTemplateIdAndShopIdAndSysType(template.getTemplateId(), tenantId, sysType);
        if (Objects.equals(template.getStatus(), StatusEnum.DISABLE.value())) {
            return;
        }
        //设置关闭消息提醒 则生不成消息记录
        if (Objects.nonNull(notifyTemplateRemindVO) && Objects.equals(notifyTemplateRemindVO.getIsRemind(), RemindType.CLOSED.value())) {
            notifyLogStatus = true;
        }
        setNotifyParamInfo(notifyParamVO, tenantId);
        String openId = "";
        if(Objects.nonNull(notifyParamVO.getUserId())) {
            ServerResponseEntity<UserApiVO> userData = userFeignClient.getUserAndOpenIdsByUserId(notifyParamVO.getUserId());
            UserApiVO user = userData.getData();

            if (Objects.nonNull(user)) {
                if (CollectionUtils.isNotEmpty(user.getBizUserIdList())) {
                    openId = user.getBizUserIdList().get(0);
                }
                notifyParamVO.setNickName(user.getNickName());
                // 截取字符串,处理过长名称
                user.setNickName(user.getNickName().substring(0, Math.min(19, user.getNickName().length() - 1)));
            }
        }

        // 分别根据平台和商家的配置进行设置可以发送通知的类型
        // 设置平台的消息通知类型
        setNotifyType(template);
        // 获取对应map
        Map<String, String> smsParam = getSmsParam(notifyParamVO, shopName);
        String content = replaceContent(smsParam, template.getMessage());
        // 保存的通知集合
        List<NotifyLog> notifyLogs = new ArrayList<>(Constant.INITIAL_CAPACITY);
        boolean sendSms = false;
        if (template.getSms() && BooleanUtils.isFalse(notifyLogStatus)) {
            if (Objects.nonNull(sendTypeEnum) && Objects.equals(sendTypeEnum.getType(), SendTypeEnum.RECEIPT_ORDER.getType()) && StrUtil.isBlank(notifyParamVO.getMobile())) {
                logger.warn("{}店铺暂未配置接收通知的手机号，无法推送消息", shopName);
                return;
            }
            // 替换消息内容
            addNotifyLog(notifyParamVO, content, template, true, NotifyType.SMS.value(), notifyLogs);
            sendSms = true;
        }
        //站内消息
        if (template.getApp() && BooleanUtils.isFalse(notifyLogStatus)) {
            notifyParamVO.setShopName(shopName);
            addNotifyLog(notifyParamVO, content, template, false, NotifyType.APP.value(), notifyLogs);
        }
        // 公众号
        if (template.getSub() && StrUtil.isNotBlank(openId) && BooleanUtils.isFalse(notifyLogStatus)) {
            sendMessage(template, notifyParamVO, openId, shopName);
            addNotifyLog(notifyParamVO, content, template, true, NotifyType.MP.value(), notifyLogs);
        }

        if (CollectionUtils.isNotEmpty(notifyLogs)) {
            notifyLogService.saveBatch(notifyLogs);
        }
        if (sendSms) {
            smsLogService.sendMsgSms(template.getTemplateCode(), notifyParamVO.getMobile(), smsParam);
        }

    }

    private static void setNotifyParamInfo(NotifyParamVO notifyParamVO, Long tenantId) {
        // 截取字符串,处理过长商品名称
        if (StrUtil.isNotBlank(notifyParamVO.getSpuName())) {
            notifyParamVO.setSpuName(notifyParamVO.getSpuName());
        }
        // 处理数据
        notifyParamVO.setShopId(tenantId);
        notifyParamVO.setHour(Objects.isNull(notifyParamVO.getHour()) ? 0 : notifyParamVO.getHour());
        notifyParamVO.setProdNum(Objects.isNull(notifyParamVO.getProdNum()) ? 0 : notifyParamVO.getProdNum());
        notifyParamVO.setGroupCount(Objects.isNull(notifyParamVO.getGroupCount()) ? 0 : notifyParamVO.getGroupCount());
        notifyParamVO.setPrice(Objects.isNull(notifyParamVO.getPrice()) ? 0.0 : notifyParamVO.getPrice());
        notifyParamVO.setPayType(Objects.isNull(notifyParamVO.getPayType()) ? 1 : notifyParamVO.getPayType());
        notifyParamVO.setDvyTime(Objects.isNull(notifyParamVO.getDvyTime()) ? new Date() : notifyParamVO.getDvyTime());
    }

    private static String getAndSetShopName(NotifyParamVO notifyParamVO, SupplierDetailBO supplierDetailBO, EsShopDetailBO shopDetailBO) {
        String shopName;
        if (Objects.equals(notifyParamVO.getShopId(), Constant.PLATFORM_SHOP_ID)) {
            shopName = Constant.PLATFORM_SHOP_NAME;
        } else {
            shopName = Objects.nonNull(notifyParamVO.getShopId()) ? shopDetailBO.getShopName() : supplierDetailBO.getSupplierName();
        }
        notifyParamVO.setShopName(shopName);
        return shopName;
    }

    @Override
    public void sendOrderNotifyMsgToUser(List<SendNotifyBO> notifyList) {
        SendNotifyBO sendNotifyBO = notifyList.get(0);
        // 注意幂等性，判断下消息是否已经推送过
        if (checkNotifyIsSend(sendNotifyBO.getBizId(), sendNotifyBO.getUserId(), sendNotifyBO.getSendType())) {
            return;
        }
        // 批量推送订单事件消息给用户
        List<NotifyParamVO> notifyParamList = BeanUtil.mapAsList(notifyList, NotifyParamVO.class);
        for (NotifyParamVO notifyParamVO : notifyParamList) {
            sendMsg(notifyParamVO);
        }

    }

    @Override
    public void sendOrderNotifyMsgToShop(SendNotifyBO sendNotifyBO) {
        // 注意幂等性，判断下消息是否已经推送过
        if (checkNotifyIsSend(sendNotifyBO.getBizId(), sendNotifyBO.getShopId(), sendNotifyBO.getSendType())) {
            return;
        }
        // 发送店铺消息（订单确认收货、买家发起退款、买家已退货）
        NotifyParamVO paramVO = BeanUtil.map(sendNotifyBO, NotifyParamVO.class);
        sendMsg(paramVO);
    }

    @Override
    public void sendProductNotifyMsgToShop(List<SendNotifyBO> notifyList) {
        // 批量推送商品事件消息给商家
        List<NotifyParamVO> notifyParamList = BeanUtil.mapAsList(notifyList, NotifyParamVO.class);
        for (NotifyParamVO notifyParamVO : notifyParamList) {
            sendMsg(notifyParamVO);
        }
    }

    @Override
    public void sendOrderToPurchaseNotifyMsgToShop(List<SendNotifyBO> sendNotify) {
        SendNotifyBO sendNotifyBO = sendNotify.get(0);
        // 注意幂等性，判断下消息是否已经推送过
        if (checkNotifyIsSend(sendNotifyBO.getBizId(), sendNotifyBO.getShopId(), sendNotifyBO.getSendType())) {
            return;
        }
        // 批量推送商品事件消息给商家
        List<NotifyParamVO> notifyParamList = BeanUtil.mapAsList(sendNotify, NotifyParamVO.class);
        for (NotifyParamVO notifyParamVO : notifyParamList) {
            sendMsg(notifyParamVO);
        }
    }

    @Override
    public void sendActivityNotifyMsgToShop(SendNotifyBO sendNotifyBO) {
        // 注意幂等性，判断下消息是否已经推送过
//        if(checkNotifyIsSend(sendNotifyBO.getBizId(),null,sendNotifyBO.getShopId(), sendNotifyBO.getSendType())){
//            return;
//        }
        // 发送店铺消息
        NotifyParamVO paramVO = BeanUtil.map(sendNotifyBO, NotifyParamVO.class);
        sendMsg(paramVO);
    }

    /**
     * 检查通知是否已经发送
     *
     * @param orderId  订单id
     * @param userId   接受通知的用户id（店铺id）
     * @param sendType 发送类型
     * @return 结果
     */
    private boolean checkNotifyIsSend(Long orderId, Long userId, Integer sendType) {
        int num = notifyLogService.countNotifyByConditions(orderId, null, userId, sendType);
        return num > 0;
    }

    private void setNotifyType(NotifyTemplateVO template) {
        List<Integer> templates = new ArrayList<>(Constant.INITIAL_CAPACITY);
        for (String templateStr : template.getNotifyTypes().split(StrUtil.COMMA)) {
            templates.add(Integer.valueOf(templateStr));
        }
        template.setSms(false);
        template.setSub(false);
        template.setApp(false);
        for (Integer type : templates) {
            template.setSms(Objects.equals(type, NotifyType.SMS.value()) || template.getSms());
            template.setSub(Objects.equals(type, NotifyType.MP.value()) || template.getSub());
            template.setApp(Objects.equals(type, NotifyType.APP.value()) || template.getApp());
        }
    }

    /**
     * 发送微信模板信息
     */
    private void sendMessage(NotifyTemplateVO template, NotifyParamVO notifyParamVO, String bizUserId, String shopName) {
        WxMaUniformMessage wxMaUniformMessage = new WxMaUniformMessage();
        wxMaUniformMessage.setMpTemplateMsg(true);
        wxMaUniformMessage.setToUser(bizUserId);
        wxMaUniformMessage.setAppid(feignShopConfig.getWxMp().getAppId());
        //模板id
        wxMaUniformMessage.setTemplateId(template.getMpCode());
        //模板数据集合
        wxMaUniformMessage.setData(getWxMaTemplateDataList(notifyParamVO, shopName));
        try {
            wxConfig.getWxMaService().getMsgService().sendUniformMsg(wxMaUniformMessage);
        } catch (WxErrorException e) {
            //用户未关注公众号，导致的消息发送失败异常
            if (Objects.nonNull(e.getError()) && e.getError().getErrorCode() == Constant.WX_MAX_NOT_FOLLOW) {
                logger.info("用户（{}）没有关注公众号，导致消息发送失败", notifyParamVO.getUserId());
            }
            //其他异常
            else {
                logger.error("异常", e);
            }
        }
    }


    /**
     * 获取微信通知模板列表
     *
     * @param notifyParamVO 通知内容
     * @param shopName      店铺名称
     * @return 微信通知模板列表
     */
    private List<WxMaTemplateData> getWxMaTemplateDataList(NotifyParamVO notifyParamVO, String shopName) {
        Map<Integer, List<WxMaTemplateData>> mpMap = new HashMap<>(24);
        List<WxMaTemplateData> dataList;
        String priceStr = notifyParamVO.getPrice() + "元";
        // 订单微信通知模板
        orderWxMaTemplate(notifyParamVO, shopName, mpMap, priceStr);
        // 团购微信通知模板
        groupWxMaTemplate(notifyParamVO, mpMap, priceStr);
        // 商家微信通知模板
        multishopWxMaTemplate(notifyParamVO, mpMap, priceStr);
        // 会员升级微信通知模板
        dataList = new ArrayList<>(Constant.INITIAL_CAPACITY);
        dataList.add(new WxMaTemplateData("first", "会员升级通知"));
        dataList.add(new WxMaTemplateData("keyword1", notifyParamVO.getLevelName()));
        dataList.add(new WxMaTemplateData("keyword2", "成功！"));
        dataList.add(new WxMaTemplateData("remark", "感谢您的支持！"));
        mpMap.put(SendTypeEnum.MEMBER_LEVEL.getValue(), dataList);

        return mpMap.get(notifyParamVO.getSendType());
    }

    /**
     * @param notifyParamVO 通知信息
     * @param shopName      店铺名称
     * @param mpMap         模板map
     * @param priceStr      价格
     */
    private void orderWxMaTemplate(NotifyParamVO notifyParamVO, String shopName, Map<Integer, List<WxMaTemplateData>> mpMap, String priceStr) {
        List<WxMaTemplateData> dataList = new ArrayList<>(Constant.INITIAL_CAPACITY);
        String firstData = "亲爱的" + notifyParamVO.getNickName() + ",您在商城购买的宝贝还未付款哦！为确保您心爱的宝贝早日启程、顺利抵达，记得尽早付款哦！";
        dataList.add(new WxMaTemplateData("first", firstData));
        dataList.add(new WxMaTemplateData("keyword1", notifyParamVO.getBizId().toString()));
        dataList.add(new WxMaTemplateData("keyword2", DateUtil.format(notifyParamVO.getCreateTime(), DatePattern.NORM_DATETIME_PATTERN)));
        dataList.add(new WxMaTemplateData("keyword3", notifyParamVO.getSpuName()));
        dataList.add(new WxMaTemplateData("remark", "请您及时付款以免订单过期。"));
        mpMap.put(SendTypeEnum.PRESS_PAY.getValue(), dataList);

        dataList = new ArrayList<>(Constant.INITIAL_CAPACITY);
        dataList.add(new WxMaTemplateData("first", "你好，你的订单已付款成功。"));
        dataList.add(new WxMaTemplateData("keyword1", notifyParamVO.getBizId().toString()));
        dataList.add(new WxMaTemplateData("keyword2", notifyParamVO.getSpuName()));
        dataList.add(new WxMaTemplateData("keyword3", DateUtil.format(new Date(), DatePattern.NORM_DATETIME_PATTERN)));
        dataList.add(new WxMaTemplateData("keyword4", priceStr));
        dataList.add(new WxMaTemplateData("remark", "感谢您的支持！"));
        mpMap.put(SendTypeEnum.PAY_SUCCESS.getValue(), dataList);

        dataList = new ArrayList<>(Constant.INITIAL_CAPACITY);
        dataList.add(new WxMaTemplateData("first", "您的退款申请商家已同意"));
        dataList.add(new WxMaTemplateData("keyword1", notifyParamVO.getBizId().toString()));
        dataList.add(new WxMaTemplateData("keyword2", notifyParamVO.getSpuName()));
        dataList.add(new WxMaTemplateData("keyword3", priceStr));
        dataList.add(new WxMaTemplateData("remark", "请及时登录商城，查看退款信息"));
        mpMap.put(SendTypeEnum.AGREE_REFUND.getValue(), dataList);

        dataList = new ArrayList<>(Constant.INITIAL_CAPACITY);
        dataList.add(new WxMaTemplateData("first", "你的退款申请被商家驳回，可与商家协商沟通"));
        dataList.add(new WxMaTemplateData("keyword1", notifyParamVO.getBizId().toString()));
        dataList.add(new WxMaTemplateData("keyword2", priceStr));
        dataList.add(new WxMaTemplateData("keyword3", notifyParamVO.getRejectMessage()));
        dataList.add(new WxMaTemplateData("remark", "请及时登录商城，查看退款信息"));
        mpMap.put(SendTypeEnum.REFUSE_REFUND.getValue(), dataList);

        dataList = new ArrayList<>(Constant.INITIAL_CAPACITY);
        dataList.add(new WxMaTemplateData("first", "尊敬的客户，您的订单提货成功啦。"));
        dataList.add(new WxMaTemplateData("keyword1", notifyParamVO.getSpuName()));
        dataList.add(new WxMaTemplateData("keyword2", priceStr));
        dataList.add(new WxMaTemplateData("keyword3", notifyParamVO.getProdNum().toString()));
        dataList.add(new WxMaTemplateData("keyword4", DateUtil.format(new Date(), DatePattern.NORM_DATETIME_PATTERN)));
        dataList.add(new WxMaTemplateData("remark", "商城感谢您的支持！"));
        mpMap.put(SendTypeEnum.WRITE_OFF.getValue(), dataList);

        dataList = new ArrayList<>(Constant.INITIAL_CAPACITY);
        dataList.add(new WxMaTemplateData("first", "商品已在送往客官的路上"));
        dataList.add(new WxMaTemplateData("keyword1", notifyParamVO.getSpuName()));
        dataList.add(new WxMaTemplateData("keyword2", "已发货"));
        dataList.add(new WxMaTemplateData("keyword3", notifyParamVO.getDvyName()));
        dataList.add(new WxMaTemplateData("keyword4", notifyParamVO.getDvyFlowId()));
        dataList.add(new WxMaTemplateData("remark", DateUtil.format(new Date(), DatePattern.NORM_DATETIME_PATTERN)));
        mpMap.put(SendTypeEnum.DELIVERY.getValue(), dataList);

        dataList = new ArrayList<>(Constant.INITIAL_CAPACITY);
        dataList.add(new WxMaTemplateData("first", "您有退款单即将超时，请及时联系商家处理"));
        dataList.add(new WxMaTemplateData("keyword1", shopName));
        dataList.add(new WxMaTemplateData("keyword2", "1笔"));
        dataList.add(new WxMaTemplateData("remark", "如有疑问请及时与客服联系！"));
        mpMap.put(SendTypeEnum.REFUND_OUT_TIME.getValue(), dataList);
    }

    /**
     * 团购微信通知模板
     *
     * @param notifyParamVO 通知信息
     * @param mpMap         模板map
     * @param priceStr      价格
     */
    private void groupWxMaTemplate(NotifyParamVO notifyParamVO, Map<Integer, List<WxMaTemplateData>> mpMap, String priceStr) {
        List<WxMaTemplateData> dataList = new ArrayList<>(Constant.INITIAL_CAPACITY);
        dataList.add(new WxMaTemplateData("first", "您参加的拼团因为有效期内未成团，我们将在3个工作日内为您安排自动退款"));
        dataList.add(new WxMaTemplateData("keyword1", notifyParamVO.getSpuName()));
        dataList.add(new WxMaTemplateData("keyword2", priceStr));
        dataList.add(new WxMaTemplateData("keyword3", "商城"));
        dataList.add(new WxMaTemplateData("remark", "点击进入订单详情页，查看退款进度！"));
        mpMap.put(SendTypeEnum.GROUP_FAIL.getValue(), dataList);

        dataList = new ArrayList<>(Constant.INITIAL_CAPACITY);
        dataList.add(new WxMaTemplateData("first", "恭喜你，拼团已经成功！"));
        dataList.add(new WxMaTemplateData("keyword1", notifyParamVO.getSpuName()));
        dataList.add(new WxMaTemplateData("keyword2", priceStr));
        dataList.add(new WxMaTemplateData("remark", "请登录商城，查看订单发货信息"));
        mpMap.put(SendTypeEnum.GROUP_SUCCESS.getValue(), dataList);

        dataList = new ArrayList<>(Constant.INITIAL_CAPACITY);
        dataList.add(new WxMaTemplateData("first", "恭喜你，开团已经成功！"));
        dataList.add(new WxMaTemplateData("keyword1", notifyParamVO.getSpuName()));
        dataList.add(new WxMaTemplateData("keyword2", priceStr));
        dataList.add(new WxMaTemplateData("remark", "快去小程序将参团链接分享给好友进行参团吧！"));
        mpMap.put(SendTypeEnum.GROUP_START.getValue(), dataList);
    }

    /**
     * 商家微信通知模板
     *
     * @param notifyParamVO 通知信息
     * @param mpMap         模板map
     * @param priceStr      价格
     */
    private void multishopWxMaTemplate(NotifyParamVO notifyParamVO, Map<Integer, List<WxMaTemplateData>> mpMap, String priceStr) {
        List<WxMaTemplateData> dataList = new ArrayList<>(Constant.INITIAL_CAPACITY);
        dataList.add(new WxMaTemplateData("first", "亲，物流显示您的订单买家已经确认收货！"));
        dataList.add(new WxMaTemplateData("keyword1", notifyParamVO.getBizId().toString()));
        dataList.add(new WxMaTemplateData("keyword2", notifyParamVO.getSpuName()));
        dataList.add(new WxMaTemplateData("keyword3", "已确认收货"));
        dataList.add(new WxMaTemplateData("remark", "如有疑问请及时与客服联系！"));
        mpMap.put(SendTypeEnum.RECEIPT_ORDER.getValue(), dataList);

        dataList = new ArrayList<>(Constant.INITIAL_CAPACITY);
        dataList.add(new WxMaTemplateData("first", "店铺有新的退款申请，请及时处理"));
        dataList.add(new WxMaTemplateData("keyword1", priceStr));
        dataList.add(new WxMaTemplateData("keyword2", notifyParamVO.getRejectMessage()));
        dataList.add(new WxMaTemplateData("keyword3", DateUtil.format(new Date(), DatePattern.NORM_DATETIME_PATTERN)));
        dataList.add(new WxMaTemplateData("keyword4", "退款金额原路返回"));
        dataList.add(new WxMaTemplateData("keyword5", notifyParamVO.getRemark()));
        dataList.add(new WxMaTemplateData("remark", "请您尽快请登录商家后台操作处理。"));
        mpMap.put(SendTypeEnum.LAUNCH_REFUND.getValue(), dataList);

        dataList = new ArrayList<>(Constant.INITIAL_CAPACITY);
        dataList.add(new WxMaTemplateData("first", "你的买家已退货，请及时处理"));
        dataList.add(new WxMaTemplateData("keyword1", notifyParamVO.getBizId().toString()));
        dataList.add(new WxMaTemplateData("keyword2", notifyParamVO.getRemark()));
        dataList.add(new WxMaTemplateData("remark", "如有疑问请及时与客服联系！"));
        mpMap.put(SendTypeEnum.RETURN_REFUND.getValue(), dataList);
    }

    /**
     * 添加消息
     *
     * @notifyParamVO notifyParamVO 参数
     * @notifyParamVO content 消息内容
     * @notifyParamVO template 模板
     * @notifyParamVO isSend 发送状态
     * @notifyParamVO value 发送类型
     * @notifyParamVO notifyLogs 消息集合
     */
    private void addNotifyLog(NotifyParamVO notifyParamVO, String content, NotifyTemplateVO template, Boolean isSend, Integer value, List<NotifyLog> notifyLogs) {
        NotifyLog notifyLog = new NotifyLog();
        notifyLog.setCreateTime(new Date());
        notifyLog.setShopId(notifyParamVO.getShopId());
        notifyLog.setShopName(notifyParamVO.getShopName());
        // 如果大于则为店铺自行接收的消息
        if (notifyParamVO.getSendType() >= Constant.MULTI_SHOP_MESSAGE_TYPE) {
            notifyLog.setRemindId(Objects.nonNull(notifyParamVO.getShopId()) ? notifyParamVO.getShopId().toString() : notifyParamVO.getSupplierId().toString());
        } else {
            notifyLog.setRemindId(Objects.isNull(notifyParamVO.getUserId()) ? "" : notifyParamVO.getUserId().toString());
        }
        notifyLog.setSendType(notifyParamVO.getSendType());
        notifyLog.setBizId(notifyParamVO.getBizId());
        notifyLog.setSpuId(notifyParamVO.getSpuId());
        notifyLog.setShopName(notifyParamVO.getShopName());
        notifyLog.setMessage(content);
        notifyLog.setNickName(notifyParamVO.getNickName());
        notifyLog.setUserMobile(notifyParamVO.getMobile());
        notifyLog.setRemindType(value);
        notifyLog.setTemplateId(template.getTemplateId());
        notifyLog.setStatus(isSend ? 1 : 0);

        notifyLogs.add(notifyLog);

    }


    private Map<String, String> getSmsParam(NotifyParamVO notifyParamVO, String shopName) {
        Map<String, String> smsParam = new HashMap<>(20);
        // 没有orderNumber用orderId
        if (Objects.nonNull(notifyParamVO.getBizId())) {
            smsParam.put("orderNumber", notifyParamVO.getBizId().toString());
        }
        smsParam.put("shopName", shopName);
        smsParam.put("prodName", notifyParamVO.getSpuName());
        if (StrUtil.isNotBlank(notifyParamVO.getNickName())) {
            smsParam.put("nickName", notifyParamVO.getNickName());
        } else {
            smsParam.put("nickName", "用户");
        }
        smsParam.put("levelName", notifyParamVO.getLevelName());
        smsParam.put("date", DateUtil.format(notifyParamVO.getDvyTime(),
                DatePattern.NORM_DATETIME_PATTERN));
        smsParam.put("cancelTime", notifyParamVO.getCancelTime());
        smsParam.put("price", String.valueOf(notifyParamVO.getPrice()));
        smsParam.put("dvyFlowId", notifyParamVO.getDvyFlowId());
        smsParam.put("stationName", notifyParamVO.getStationName());
        smsParam.put("dvyName", notifyParamVO.getDvyName());
        smsParam.put("dvyTime", DateUtil.format(notifyParamVO.getDvyTime(),
                DatePattern.NORM_DATETIME_PATTERN));
        smsParam.put("day", String.valueOf(Constant.MAX_REFUND_APPLY_TIME));
        smsParam.put("hour", notifyParamVO.getHour().toString());
        smsParam.put("spuId", Objects.nonNull(notifyParamVO.getSpuId()) ? notifyParamVO.getSpuId().toString() : null);
        smsParam.put("spuName", Objects.nonNull(notifyParamVO.getSpuName()) ? notifyParamVO.getSpuName() : null);
        smsParam.put("activityId", Objects.nonNull(notifyParamVO.getActivityId()) ? notifyParamVO.getActivityId().toString() : null);
        smsParam.put("activityName", Objects.nonNull(notifyParamVO.getActivityName()) ? notifyParamVO.getActivityName() : null);
        smsParam.put("supplierName", Objects.nonNull(notifyParamVO.getSupplierName()) ? notifyParamVO.getSupplierName() : null);
        return smsParam;
    }


    /**
     * 短信内容替换
     *
     * @notifyParamVO maps 替换内容
     */
    private String replaceContent(Map<String, String> maps, String content) {
        for (Map.Entry<String, String> element : maps.entrySet()) {
            if (StrUtil.isNotBlank(element.getValue())) {
                content = content.replace("${" + element.getKey() + "}", element.getValue());
            }
        }
        return content;
    }

}
