package com.storlead.wx.config;

import com.storlead.wx.pojo.vo.SystemWechatConfigEntity;
import com.storlead.wx.properties.CorpWeChatProperties;
import com.storlead.wx.service.SystemWechatConfigService;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Objects;

/**
 * @program: sp-sales
 * @description:
 * @author: chenkq
 * @create: 2022-07-25 12:10
 */
@Configuration
@Log4j2
public class CorpWeChatConfiguration {

    @Autowired
    private SystemWechatConfigService wechatConfigService;

    @Bean
    public void initCorpWeChatConfiguration() {
        try {

            log.error("initCorpWeChatConfiguration--先走");
            SystemWechatConfigEntity wechatConfig = wechatConfigService.getById(1);
            if (Objects.nonNull(wechatConfig)) {
                CorpWeChatProperties.setWechatMessageCallbackUrl(wechatConfig.getWechatMessageCallbackUrl());
                CorpWeChatProperties.setLoading(true);
                CorpWeChatProperties.setCorpId(wechatConfig.getCorpId());
                CorpWeChatProperties.setCorpAgentId(Integer.valueOf(wechatConfig.getCorpAgentId()));
                CorpWeChatProperties.setCorpAddressSecret(wechatConfig.getCorpAddressSecret());
                CorpWeChatProperties.setCorpAgentSecret(wechatConfig.getCorpAgentSecret());
            } else {
                CorpWeChatProperties.setLoading(false);
            }
        } catch (Exception e) {
            CorpWeChatProperties.setLoading(false);
           log.error("initCorpWeChatConfiguration - error",e);
        }
    }
}
