package com.ematou.wxservice.mp.handler;

import com.ematou.wxservice.entity.pojo.SubscribeItem;
import com.ematou.wxservice.mp.message.WeChatMessage;
import com.ematou.wxservice.mp.message.WeChatMpXmlOutMessage;
import com.ematou.wxservice.mp.message.WeChatMpXmlOutNewsMessage;
import com.ematou.wxservice.service.SubscribeItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * 关注事件
 * @author lhm
 * @version 1.0
 * 2021-05-14 17:04
 */
@Component
public class WeChatSubscribeEventHandler implements WeChatMessageHandler {

    @Autowired
    private SubscribeItemService subscribeItemService;
    @Value("${wechat}")
    String wechat;

    /**
     * 处理消息，并封装返回消息
     *
     * @param weChatMessage 接收到的消息
     * @return 响应消息
     */
    @Override
    public WeChatMpXmlOutMessage handlerMessage(WeChatMessage weChatMessage) {
        SubscribeItem subscribeItem = subscribeItemService.queryByWechatAccount(wechat);

        WeChatMpXmlOutNewsMessage.Item item = new WeChatMpXmlOutNewsMessage.Item();
        /*item.setTitle("欢迎关注【e码头】");
        item.setDescription("【e码头跨境电商新零售】\n>>>门店购买的订单,点击绑定手机号,立即取件!");
        item.setPicUrl("https://wxservice.fu-qing.com/images/emtnew.png");
        item.setUrl("https://wxservice.fu-qing.com/view/binding?openid="+weChatMessage.getFromUser());*/
        item.setTitle(subscribeItem.getTitle());
        item.setDescription(subscribeItem.getDescription());
        item.setPicUrl(subscribeItem.getPicUrl());
        item.setUrl(subscribeItem.getUrl() + weChatMessage.getFromUser());
        return WeChatMpXmlOutMessage.NEWS().addArticle(item).toUser(weChatMessage.getFromUser()).fromUser(weChatMessage.getToUser()).build();
       }

}
