package com.kym.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kym.common.enums.MsgTemplateType;
import com.kym.common.utils.wx.WxPbUtil;
import com.kym.entity.MpMsgTemplate;
import com.kym.entity.MpRelation;
import com.kym.entity.PayLog;
import com.kym.entity.WashOrder;
import com.kym.mapper.MpMsgTemplateMapper;
import com.kym.service.MpMsgTemplateService;
import com.kym.service.MpRelationService;
import com.kym.service.wechat.TemplateParamBuilder;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * <p>
 * 微信公众号消息模版表 服务实现类
 * </p>
 *
 * @author skyline
 * @since 2024-08-07
 */
@Service
public class MpMsgTemplateServiceImpl extends ServiceImpl<MpMsgTemplateMapper, MpMsgTemplate> implements MpMsgTemplateService {
    private final Map<MsgTemplateType, TemplateParamBuilder> paramBuilders;

    private final MpRelationService mpRelationService;

    public MpMsgTemplateServiceImpl(Map<MsgTemplateType, TemplateParamBuilder> paramBuilders, MpRelationService mpRelationService) {
        this.paramBuilders = paramBuilders;
        this.mpRelationService = mpRelationService;
    }


    /**
     * 发送充值成功消息
     *
     * @param payLog
     * @param balance
     */
    @Override
    public void sendPaymentSuccessMsg(PayLog payLog, Integer balance) {
        sendTemplateMessage(MsgTemplateType.PAYMENT_SUCCESS, payLog.getUserId(), payLog, balance);
    }

    /**
     * 发送订单完成提醒
     *
     * @param washOrder
     * @param balance
     */
    @Override
    public void sendOrderCompletedMsg(WashOrder washOrder, Integer balance) {
        sendTemplateMessage(MsgTemplateType.ORDER_COMPLETED, washOrder.getUserId(), washOrder, balance);
    }

    /**
     * 发送停车优惠券
     *
     * @param washOrder
     */
    @Override
    public void sendParkingCouponMsg(WashOrder washOrder, String parkingCouponUrl) {
        sendTemplateMessage(MsgTemplateType.PARKING_COUPON, washOrder.getUserId(), washOrder, parkingCouponUrl);
    }


    @Override
    @Async
    public void sendTemplateMessage(MsgTemplateType templateType, Long userId, Object... args) {
        try {
            var mpMsgTemplate = lambdaQuery().eq(MpMsgTemplate::getBizType, templateType.getBizType()).one();
            if (mpMsgTemplate == null) {
                return;
            }

            mpRelationService.lambdaQuery().eq(MpRelation::getUserId, userId).oneOpt().ifPresent(mpRelation -> {
                TemplateParamBuilder builder = paramBuilders.get(templateType);
                Map<String, String> params = builder.buildParams(args);
                WxPbUtil.sendPublicTemplateMessage(
                        mpRelation.getMpOpenid(),
                        mpMsgTemplate.getTemplateId(),
                        params,
                        templateType.equals(MsgTemplateType.PARKING_COUPON) ? args[1].toString() : "",
                        ""
                );
            });
        } catch (Exception e) {
            log.error("发送模板消息失败", e);
        }

    }
}
