package com.qiwuai.garden.common.utils; import com.alibaba.fastjson.JSON; import com.qiwuai.garden.dto.RegionIPDto; import lombok.Data; import org.springframework.beans.factory.annotation.Value; import org.springframework.core.io.ClassPathResource; import org.springframework.stereotype.Component; import org.yaml.snakeyaml.Yaml; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.InputStream; import java.net.URL; import java.util.*; import java.util.concurrent.ConcurrentHashMap; @Component @Data public class YamlUtil { //获取话术存放的路径 public static String speechFilePath = YamlUtil.getValueByKey("speechFilePath"); private static LinkedHashMap>> properties = new LinkedHashMap<>(); /** * 将种植话术转成map格式 * @param * @return Map>> */ public static Map>> plantSpeechToMap() { Map>> speechMap = new HashMap<>(); try { InputStream in = new FileInputStream(speechFilePath + "plant.yaml"); Yaml yaml = new Yaml(); speechMap = yaml.load(in); } catch (Exception e) { e.printStackTrace(); } return speechMap; } /** * 将园丁话术转成map格式 * @param * @return Map>> */ public static Map>> gardenerSpeechToMap() { Map>> speechMap = new HashMap<>(); try { InputStream in = new FileInputStream(speechFilePath + "gardener.yaml"); Yaml yaml = new Yaml(); speechMap = yaml.load(in); } catch (Exception e) { e.printStackTrace(); } return speechMap; } /** * 将宠物话术转成map格式 * @param * @return Map>> */ public static Map>> petSpeechToMap() { Map>> speechMap = new HashMap<>(); try { InputStream in = new FileInputStream(speechFilePath + "pet.yaml"); Yaml yaml = new Yaml(); speechMap = yaml.load(in); } catch (Exception e) { e.printStackTrace(); } return speechMap; } /** * 将市场话术转成map格式 * @param * @return Map>> */ public static Map>> marketSpeechToMap() { Map>> speechMap = new HashMap<>(); try { InputStream in = new FileInputStream(speechFilePath + "market.yaml"); Yaml yaml = new Yaml(); speechMap = yaml.load(in); } catch (Exception e) { e.printStackTrace(); } return speechMap; } /** * 将社交话术转成map格式 * @param * @return Map>> */ public static Map>> socialSpeechToMap() { Map>> speechMap = new HashMap<>(); try { InputStream in = new FileInputStream(speechFilePath + "social.yaml"); Yaml yaml = new Yaml(); speechMap = yaml.load(in); } catch (Exception e) { e.printStackTrace(); } return speechMap; } /** * 将任务话术转成map格式 * @param * @return Map>> */ public static Map>> taskSpeechToMap() { Map>> speechMap = new HashMap<>(); try { InputStream in = new FileInputStream(speechFilePath + "task.yaml"); Yaml yaml = new Yaml(); speechMap = yaml.load(in); } catch (Exception e) { e.printStackTrace(); } return speechMap; } /** * 将帮助话术转成map格式 * @param * @return Map>> */ public static Map>> helpSpeechToMap() { Map>> speechMap = new HashMap<>(); try { InputStream in = new FileInputStream(speechFilePath + "help.yaml"); Yaml yaml = new Yaml(); speechMap = yaml.load(in); } catch (Exception e) { e.printStackTrace(); } return speechMap; } /** * 将建议话术转成map格式 * @param * @return Map>> */ public static Map>> adviseSpeechToMap() { Map>> adviseMap = new HashMap<>(); try { InputStream in = new FileInputStream(speechFilePath + "advise.yaml"); Yaml yaml = new Yaml(); adviseMap = yaml.load(in); } catch (Exception e) { e.printStackTrace(); } return adviseMap; } /** * 读取配置文件yml中的某个值,只支持取第一层的值 * @param * @return Map>> */ public static String getValueByKey(String key) { String value = ""; try { Yaml yaml = new Yaml(); ClassPathResource resource = new ClassPathResource("\\application.yml"); properties = yaml.load(resource.getInputStream()); String val = properties.get("spring").get("profiles").get("active"); ClassPathResource resourceNext = new ClassPathResource("\\application-" + val + ".yml"); Map map = yaml.load(resourceNext.getInputStream()); value = String.valueOf(map.get(key)); } catch (Exception e) { e.printStackTrace(); } return value; } public static void main(String[] args) throws FileNotFoundException { // Map>> speech = plantSpeechToMap(); // System.out.println("yaml文件读取结果:" + plantSpeechToMap()); // Integer i = RandomDropUtil.getRandomNum(0, speech.get("man").get("plant_fail_near_plant").size()); // System.out.println("随机数i:" + i); // System.out.println("植物因没有伴生宠物种植失败的回复:" + speech.get("man").get("plant_fail_near_plant").get(i)); System.out.println("从yml文件取出的值" + getValueByKey("userFilePath")); Map> IPRegionsMap = new ConcurrentHashMap<>(); List regionIPList = new ArrayList<>(); RegionIPDto regionIP = new RegionIPDto(); regionIP.setId(1); regionIP.setPlace("昆仑"); regionIP.setServerIP("192.168.1.142"); regionIP.setNumMax(10000); regionIP.setPopulation(4); regionIPList.add(regionIP); IPRegionsMap.put("192.168.1.142", regionIPList); String IPRegions = JSON.toJSONString(IPRegionsMap); System.out.println(IPRegions); } }