package com.jpsoft.shinestar.mq.listener;

import com.jpsoft.shinestar.config.WxConfig;
import com.jpsoft.shinestar.modules.base.entity.MessageNotice;
import com.jpsoft.shinestar.modules.base.entity.MessageNoticePerson;
import com.jpsoft.shinestar.modules.base.entity.PersonInfo;
import com.jpsoft.shinestar.modules.base.service.MessageNoticeService;
import com.jpsoft.shinestar.modules.base.service.PersonInfoService;
import com.jpsoft.shinestar.modules.common.utils.WechatMessageUtil;
import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONObject;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Slf4j
@Component
@RabbitListener(queues = "schoolMessageNoticeQueue",concurrency = "1")
public class MessageNoticeListener {
    @Autowired
    private MessageNoticeService messageNoticeService;

    @Autowired
    private PersonInfoService personInfoService;

    @Autowired
    private WechatMessageUtil wechatMessageUtil;

    @Autowired
    private WxConfig wxConfig;

    @RabbitHandler
    public void process(MessageNoticePerson messageNoticePerson) {
        PersonInfo personInfo = personInfoService.get(messageNoticePerson.getPersonId());
        MessageNotice messageNotice = messageNoticeService.get(messageNoticePerson.getNoticeId());

        if(personInfo!=null && messageNotice!=null) {
            String content = messageNotice.getContent();

            String regEx_html="<[^>]+>";
            Pattern p_html = Pattern.compile(regEx_html,Pattern.CASE_INSENSITIVE);
            Matcher m_html = p_html.matcher(content);
            content = m_html.replaceAll(""); //过滤html标签

            JSONObject sendData = JSONObject.fromObject(content);

            String url = messageNotice.getNoticeLink();

            wechatMessageUtil.sendNativeTemplate(
                    wxConfig.getAppId(), wxConfig.getAppSecret(),
                    messageNotice.getWechatCode(),
                    personInfo.getOpenId(), sendData, url);
        }
    }

    public static void main(String[] args) {
        String json = "{\n" +
                "  \"first\": {\n" +
                "    \"DATA\": \"开课通知：亲爱的家长，《人工智能课》开课啦，因电脑室座位有限，我们将按报名顺序限额安排入班。\"\n" +
                "  },\n" +
                "  \"keyword1\": {\n" +
                "    \"DATA\": \"《人工智能课》\"\n" +
                "  },\n" +
                "  \"keyword2\": {\n" +
                "    \"DATA\": \"松滋市实验小学拓展课\"\n" +
                "  },\n" +
                "  \"remark\": {\n" +
                "    \"DATA\": \"点击“查看详情”，抢先报名获得入班资格！\"\n" +
                "  }\n" +
                "}";

        JSONObject sendData = JSONObject.fromObject(json);

        System.out.println(sendData.toString());
    }
}
