/game-maven-plugin/src/main/java/com/jzy/game/plugin/util/MongoUtil.java

https://github.com/jzyong/game-server · Java · 213 lines · 139 code · 17 blank · 57 comment · 29 complexity · 63f41166195cc11d2b87785a1fb0f744 MD5 · raw file

  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package com.jzy.game.plugin.util;
  7. import com.google.gson.JsonArray;
  8. import com.google.gson.JsonElement;
  9. import com.google.gson.JsonObject;
  10. import com.google.gson.JsonParser;
  11. import com.mongodb.MongoClient;
  12. import com.mongodb.MongoClientURI;
  13. import com.mongodb.client.MongoCollection;
  14. import com.mongodb.client.MongoDatabase;
  15. import java.util.ArrayList;
  16. import java.util.Date;
  17. import java.util.List;
  18. import java.util.Map;
  19. import java.util.Map.Entry;
  20. import java.util.Set;
  21. import org.bson.BsonArray;
  22. import org.bson.BsonDateTime;
  23. import org.bson.BsonDocument;
  24. import org.bson.BsonDouble;
  25. import org.bson.BsonInt32;
  26. import org.bson.BsonInt64;
  27. import org.bson.BsonNull;
  28. import org.bson.BsonString;
  29. import org.bson.BsonValue;
  30. import org.bson.Document;
  31. import org.slf4j.Logger;
  32. import org.slf4j.LoggerFactory;
  33. /**
  34. * mongodb
  35. *
  36. * @author JiangZhiYong
  37. * @QQ 359135103
  38. */
  39. public class MongoUtil {
  40. private static final Logger LOGGER = LoggerFactory.getLogger(MongoUtil.class);
  41. // private static MongoClient mongoClient;
  42. //
  43. // /**
  44. // * 获取连接
  45. // *
  46. // * @return
  47. // */
  48. // public static MongoClient getMongoClient() {
  49. // if (mongoClient == null) {
  50. // MongoClientURI connectionString = new MongoClientURI("mongodb://127.0.0.1");
  51. // mongoClient = new MongoClient(connectionString);
  52. // }
  53. // return mongoClient;
  54. // }
  55. /**
  56. * 获取数据库
  57. *
  58. * @param client
  59. * @param name
  60. * @return
  61. */
  62. public static MongoDatabase getMongoDatabase(MongoClient client, String name) {
  63. return client.getDatabase(name);
  64. }
  65. /**
  66. * 插入配置数据 先删除,在插入
  67. *
  68. * @param filePath
  69. * @param sheetName
  70. * @param dbName
  71. */
  72. public static String insertConfigData(MongoClient client, String filePath, String sheetName, String dbName) throws Exception {
  73. String retString = sheetName + "更新成功";
  74. Args.Four<List<String>, List<String>, List<String>, List<List<Object>>> excel = ExcelUtil.readExcel(filePath, sheetName);
  75. if (excel == null) {
  76. LOGGER.warn("{}--{}未找到数据", filePath, sheetName);
  77. return "内部错误";
  78. }
  79. List<Document> documents = new ArrayList<>();
  80. MongoDatabase database = getMongoDatabase(client, dbName);
  81. if (database == null) {
  82. LOGGER.warn("{}数据库不存在", dbName);
  83. return "检查配置数据库不存在";
  84. }
  85. MongoCollection<Document> collection = database.getCollection(sheetName);
  86. if (collection == null) {
  87. LOGGER.warn("{}数据库集合{}不存在", dbName, collection);
  88. return "表不存在";
  89. }
  90. int row = excel.d().size(); //数据行数
  91. int column = excel.a().size(); //字段列数
  92. try {
  93. for (int i = 0; i < row; i++) {
  94. Document document = new Document();
  95. List<Object> datas = excel.d().get(i);
  96. for (int j = 0; j < column; j++) {
  97. document.append(excel.a().get(j), datas.get(j));
  98. }
  99. documents.add(document);
  100. }
  101. } catch (Exception e) {
  102. LOGGER.error(e.getMessage());
  103. LOGGER.error("Excel表{}有空行,空列,请删除", sheetName);
  104. retString = sheetName + "表有空行,空列,请删除";
  105. return retString;
  106. }
  107. if (documents.size() < 1) {
  108. return sheetName + "数据为空";
  109. }
  110. collection.drop();
  111. collection.insertMany(documents);
  112. return retString;
  113. }
  114. /**
  115. * json转docment
  116. *
  117. * @param jsonStr
  118. * @return
  119. */
  120. public static Document getDocument(String jsonStr) {
  121. Document document = new Document();
  122. JsonParser parser = new JsonParser();
  123. JsonElement jsonElement = parser.parse(jsonStr);
  124. if (jsonElement.isJsonObject()) {
  125. JsonObject jsonObject = jsonElement.getAsJsonObject();
  126. for (Entry<String, JsonElement> entry : jsonObject.entrySet()) {
  127. document.append(entry.getKey(), getRealValue(entry.getValue().toString())); //TODO 数据类型检查
  128. }
  129. }
  130. return document;
  131. }
  132. /**
  133. * json 数组转文档
  134. *
  135. * @param jsonStr
  136. * @return
  137. */
  138. public static List<Object> getDocuments(String jsonStr) {
  139. JsonParser parser = new JsonParser();
  140. JsonElement jsonElement = parser.parse(jsonStr);
  141. if (jsonElement.isJsonArray()) {
  142. JsonArray jsonArray = jsonElement.getAsJsonArray();
  143. List<Object> list = new ArrayList<>();
  144. jsonArray.forEach(element -> {
  145. if (element.isJsonObject()) {
  146. Document d = new Document();
  147. for (Entry<String, JsonElement> entry : element.getAsJsonObject().entrySet()) {
  148. d.append(entry.getKey(), getRealValue(entry.getValue().toString())); //TODO 数据类型检查
  149. }
  150. list.add(d);
  151. } else if (element.isJsonPrimitive()) {
  152. list.add(getBsonValue(getRealValue(element.getAsString())));
  153. } else {
  154. LOGGER.warn("{}数据复杂,不支持多重嵌套", jsonStr);
  155. }
  156. });
  157. return list;
  158. }
  159. LOGGER.warn("{}不是json数组", jsonStr);
  160. return null;
  161. }
  162. /**
  163. * 获取真实值
  164. *
  165. * @param valueStr
  166. * @return
  167. */
  168. public static Object getRealValue(String valueStr) {
  169. if (StringUtil.isDouble(valueStr)) {
  170. return Double.parseDouble(valueStr);
  171. } else if (StringUtil.isInteger(valueStr)) {
  172. return Integer.parseInt(valueStr);
  173. } else if ("false".equalsIgnoreCase(valueStr) || "true".equalsIgnoreCase(valueStr)) {
  174. return Boolean.parseBoolean(valueStr);
  175. }
  176. return valueStr;
  177. }
  178. public static BsonValue getBsonValue(Object obj) {
  179. if (obj instanceof Integer) {
  180. return new BsonInt32((Integer) obj);
  181. }
  182. if (obj instanceof String) {
  183. return new BsonString((String) obj);
  184. }
  185. if (obj instanceof Long) {
  186. return new BsonInt64((Long) obj);
  187. }
  188. if (obj instanceof Date) {
  189. return new BsonDateTime(((Date) obj).getTime());
  190. }
  191. if (obj instanceof Double || obj instanceof Float) {
  192. return new BsonDouble((Double) obj);
  193. }
  194. return new BsonNull();
  195. }
  196. }