package cn.newfeifan.mall.module.system.config;

import cn.newfeifan.mall.framework.common.util.json.JsonUtils;
import cn.newfeifan.mall.module.system.dal.dataobject.config.SystemConfigDO;
import cn.newfeifan.mall.module.system.dal.dataobject.wechatmsgtemplate.WechatMsgTemplateDO;
import cn.newfeifan.mall.module.system.service.config.SystemConfigService;
import cn.newfeifan.mall.module.system.service.wechatmsgtemplate.WechatMsgTemplateService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.List;

import static cn.newfeifan.mall.module.system.constant.SystemConstants.SYSTEM_CONFIG;
import static cn.newfeifan.mall.module.system.constant.SystemConstants.SYSTEM_WX_MESSAGE_TEMPLATE;


/**
 * 启动配置类
 */
@Component
@Slf4j
public class StartConfig {

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Resource
    private WechatMsgTemplateService wechatMsgTemplateService;

    @Resource
    private SystemConfigService configService;

    @PostConstruct
    public void init() {
        initWechatMsgTemplate();

        initSystemConfig();
    }

    /**
     * 初始化微信模板配置
     */
    public void initWechatMsgTemplate() {
        List<WechatMsgTemplateDO> wechatMsgTemplateDOS = wechatMsgTemplateService.selectList();
        wechatMsgTemplateDOS.forEach(wechatMsgTemplateDO -> stringRedisTemplate.opsForValue().set(SYSTEM_WX_MESSAGE_TEMPLATE + wechatMsgTemplateDO.getId(), JsonUtils.toJsonString(wechatMsgTemplateDO)));

        log.info("======初始化微信模板配置成功======");
    }

    /**
     * 初始化系统配置
     */
    public void initSystemConfig(){
        SystemConfigDO config = configService.getConfig();
        stringRedisTemplate.opsForValue().set(SYSTEM_CONFIG, JsonUtils.toJsonString(config));

        log.info("======初始化系统配置成功new APP======");
    }
}
