package com.qiwu.inference.common; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.TypeReference; import com.qiwu.inference.common.utils.PersistenceUtil; import com.qiwu.inference.dao.*; import com.qiwu.inference.dto.Person; import com.qiwu.inference.dto.Player; import com.qiwu.inference.pojo.*; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.InitializingBean; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; import java.io.File; import java.util.List; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; @Component public class InitSysServlet implements InitializingBean { Logger log = LoggerFactory.getLogger(InitSysServlet.class); @Value("${userFilePath}") private String userFilePath; @Value("${configFilePath}") private String configFilePath; @Value("${speechFilePath}") private String speechFilePath; @Autowired private SystemConfigMapper systemConfigMapper; @Autowired private LocationMapper locationMapper; @Autowired private ClueMapper clueMapper; @Autowired private DemandTaskMapper demandTaskMapper; @Autowired private DispositionMapper dispositionMapper; /** * 保存配置信息到缓存 */ @Override public void afterPropertiesSet() throws Exception { //--------------------------缓存关卡数据--------------------- log.info("开始缓存关卡数据"); String gateInfo = PersistenceUtil.getConfigFromFile(configFilePath, "results.json"); Map> gateInfoMap = JSON.parseObject(gateInfo, new TypeReference>>(){}); TimingAcquisition.gateInfoMap.putAll(gateInfoMap); log.info("关卡数据缓存完成"); //--------------------------加载玩家数据---------------------- log.info("开始加载玩家数据"); //从yml配置文件中取出存放玩家数据的地址 File file = new File(userFilePath); File[] files = file.listFiles(); if (files == null) { log.info("路径" + userFilePath+ "下没有文件"); return; } Map playerMap = new ConcurrentHashMap<>(); for (File f : files) { if (f.isFile()) { String s = PersistenceUtil.getDataFromFile(String.valueOf(f)); //JSON转map对象 Player player = JSON.parseObject(s, new TypeReference(){}); String fileName = f.getName().replace(".json", ""); playerMap.put(fileName, player); TimingAcquisition.playerMap.putAll(playerMap); } } log.info("玩家数据缓存完成"); //--------------------加载话术----------------- TimingAcquisition.speechLoading(speechFilePath); //-----------------缓存场所信息------------------ Map locationMap = new ConcurrentHashMap<>(); List locationList = locationMapper.findAll(); for (Location location : locationList) { locationMap.put(location.getId().toString(), location); } TimingAcquisition.locationMap.putAll(locationMap); log.info("场所配置信息缓存完成"); //-----------------缓存线索信息------------------ Map clueMap = new ConcurrentHashMap<>(); List clueList = clueMapper.findAll(); for (Clue clue : clueList) { clueMap.put(clue.getId(), clue); } TimingAcquisition.clueMap.putAll(clueMap); log.info("线索配置信息缓存完成"); //-----------------缓存线索类型信息------------------ Map clueTypeMap = new ConcurrentHashMap<>(); List clueTypeList = clueMapper.getTypeInfo(); for (ClueType clueType : clueTypeList) { clueTypeMap.put(clueType.getId(), clueType.getName()); } TimingAcquisition.clueTypeMap.putAll(clueTypeMap); log.info("线索类型配置信息缓存完成"); //-----------------缓存需求任务配置信息------------------ Map demandTaskMap = new ConcurrentHashMap<>(); List demandTaskList = demandTaskMapper.findAll(); for (DemandTask demandTask : demandTaskList) { demandTaskMap.put(demandTask.getId(), demandTask); } TimingAcquisition.demandTaskMap.putAll(demandTaskMap); log.info("需求任务配置信息缓存完成"); //-----------------缓存性格信息------------------ Map dispositionMap = new ConcurrentHashMap<>(); List dispositionList = dispositionMapper.findAll(); for (Disposition disposition : dispositionList) { dispositionMap.put(disposition.getId(), disposition); } TimingAcquisition.dispositionMap.putAll(dispositionMap); log.info("性格配置信息缓存完成"); //-----------------缓存系统配置信息------------------ Map systemConfigMap = new ConcurrentHashMap<>(); List systemConfigList = systemConfigMapper.findAll(); for (SystemConfigPojo systemConfig : systemConfigList) { systemConfigMap.put(systemConfig.getName(), systemConfig); } TimingAcquisition.systemConfigMap.putAll(systemConfigMap); log.info("系统配置信息缓存完成"); } }