package com.qmth.themis.business.util;

import com.qmth.themis.business.constant.SystemConstant;
import com.qmth.themis.business.entity.SysConfig;
import com.qmth.themis.business.service.ThemisCacheService;
import com.qmth.themis.common.exception.BusinessException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Map;
import java.util.Optional;

/**
 * @Description: rocket mq util
 * @Param:
 * @return:
 * @Author: wangliang
 * @Date: 2020/11/13
 */
@Component
public class MqUtil {
    private final static Logger log = LoggerFactory.getLogger(MqUtil.class);

    String topic;

    Map<String, String> map;

    @Resource
    ThemisCacheService themisCacheService;

    @Bean
    public void mqUtilEnv() {
        SysConfig sysConfigMqTopic = themisCacheService.addSysConfigCache(SystemConstant.MQ_CONFIG_TOPIC);
        Optional.ofNullable(sysConfigMqTopic).orElseThrow(() -> new BusinessException("未配置消息队列topic"));

        SysConfig sysConfigMqBody = themisCacheService.addSysConfigCache(SystemConstant.MQ_CONFIG_BODY);
        Optional.ofNullable(sysConfigMqBody).orElseThrow(() -> new BusinessException("未配置消息队列主体"));

        this.topic = sysConfigMqTopic.getConfigValue();
        this.map = JacksonUtil.readJson(sysConfigMqBody.getConfigValue(), Map.class);
    }

    public String getTopic() {
        return topic;
    }

    public void setTopic(String topic) {
        this.topic = topic;
    }

    public Map<String, String> getMap() {
        return map;
    }

    public void setMap(Map<String, String> map) {
        this.map = map;
    }
}
