package com.cpop.oam.framework.handler.wxOpen;

import com.cpop.oam.framework.config.wxOpen.WxOpenProperties;
import lombok.Getter;
import me.chanjar.weixin.open.api.impl.WxOpenInRedisTemplateConfigStorage;
import me.chanjar.weixin.open.api.impl.WxOpenMessageRouter;
import me.chanjar.weixin.open.api.impl.WxOpenServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;

/**
 * @author DB
 * @createTime 2023/10/27 9:30
 * @description
 */
@Service
@EnableConfigurationProperties({WxOpenProperties.class})
public class WxOpenService extends WxOpenServiceImpl {

    private final Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private WxOpenProperties wxOpenProperties;

    @Getter
    private WxOpenMessageRouter wxOpenMessageRouter;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @PostConstruct
    public void init() {
        WxOpenInRedisTemplateConfigStorage inRedisConfigStorage = new WxOpenInRedisTemplateConfigStorage(stringRedisTemplate, "Cpop:sdk:wxOpen:");
        inRedisConfigStorage.setComponentAppId(wxOpenProperties.getComponentAppId());
        inRedisConfigStorage.setComponentAppSecret(wxOpenProperties.getComponentSecret());
        inRedisConfigStorage.setComponentToken(wxOpenProperties.getComponentToken());
        inRedisConfigStorage.setComponentAesKey(wxOpenProperties.getComponentAesKey());
        setWxOpenConfigStorage(inRedisConfigStorage);
        wxOpenMessageRouter = new WxOpenMessageRouter(this);
        wxOpenMessageRouter.rule().handler((wxMpXmlMessage, map, wxMpService, wxSessionManager) -> {
            logger.info("\n接收到 {} 公众号请求消息，内容：{}", wxMpService.getWxMpConfigStorage().getAppId(), wxMpXmlMessage);
            return null;
        }).next();
    }

}

