package com.yiqi.crm.handle;

import com.yiqi.crm.domain.WxReply;
import com.yiqi.crm.service.IWxReplyService;
import me.chanjar.weixin.common.session.WxSessionManager;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage;
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage;

import java.util.Map;

/**
 * @desc 消息对话处理
 */
public class MsgHandler extends AbstractHandler {

    IWxReplyService wxReplyService;

    public static MsgHandler build(IWxReplyService wxReplyService) {
        MsgHandler handler = new MsgHandler();
        handler.wxReplyService = wxReplyService;
        return handler;
    }

    @Override
    public WxMpXmlOutMessage handle(WxMpXmlMessage wxMessage, Map<String, Object> map,
                                    WxMpService wxMpService,
                                    WxSessionManager sessionManager) {
        //公众号对话处理
        WxReply defaultReply = wxReplyService.getKeyWordsReplyListByDefault();
        if (defaultReply == null) {
            return null;
        }
        return WxMpXmlOutMessage
                .TEXT()
                .content(defaultReply.getPicTextDetailContent())
                .fromUser(wxMessage.getToUser())
                .toUser(wxMessage.getFromUser())
                .build();
    }


}