VenderUtil.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. package com.persagy.dptool;
  2. import com.fasterxml.jackson.core.JsonProcessingException;
  3. import com.persagy.dptool.dto.VenderInfo;
  4. import com.persagy.dptool.dto.vender.BrandDTO;
  5. import com.persagy.dptool.dto.vender.ObjectInfoRecord;
  6. import com.persagy.dptool.dto.vender.SpecificatDTO;
  7. import com.persagy.dptool.dto.vender.VenderContactDTO;
  8. import java.io.*;
  9. import java.util.*;
  10. public class VenderUtil {
  11. public static final String keyResult = "RESULT";
  12. public static final String keyError = "ERROR";
  13. public static Map<String, String> business(File sourceFile, VenderInfo venderInfo) {
  14. Map<String, String> result = new HashMap<>();
  15. File newFile = getNewFile(sourceFile);
  16. if(newFile == null) {
  17. result.put(keyError, "在目录"+sourceFile.getParent()+"下创建数据转移文件出错!");
  18. return result;
  19. }
  20. String pjId = venderInfo.pjId;
  21. String pjIdField = "\"project_id\":\""+pjId+"\"";
  22. int lineNum = 1;
  23. FileInputStream fis = null;
  24. Scanner scanner = null;
  25. BufferedWriter writer = null;
  26. try {
  27. fis = new FileInputStream(sourceFile);
  28. scanner = new Scanner(fis, "utf-8");
  29. writer = new BufferedWriter(new FileWriter(newFile));
  30. String lineStr = null, objId = null, infoIdAndTimeStr = null;
  31. // {设备id:{信息点编码@时间:信息点记录}}
  32. Map<String, Map<String, ObjectInfoRecord>> obj2InfosMap = new HashMap<>();
  33. while(scanner.hasNext()) {
  34. lineStr = scanner.nextLine();
  35. if(lineStr == null || !lineStr.startsWith("{")) {
  36. lineNum ++;
  37. continue;
  38. }
  39. if(lineStr.contains("\"obj_type\":\"Eq\"") && lineStr.contains(pjIdField) && lineStr.contains("obj_id") && lineStr.contains("info_id")) {
  40. // 判定为指定项目的设备记录
  41. ObjectInfoRecord objectInfoRecord = CommonUtil.jsonStrToObj(lineStr, ObjectInfoRecord.class);
  42. objId = objectInfoRecord.getObj_id();
  43. infoIdAndTimeStr = objectInfoRecord.getInfo_id() + "@" + objectInfoRecord.getTime();
  44. if(obj2InfosMap.containsKey(objId)) {
  45. obj2InfosMap.get(objId).put(infoIdAndTimeStr, objectInfoRecord);
  46. }else {
  47. Map<String, ObjectInfoRecord> infoRecords = new HashMap<>();
  48. infoRecords.put(infoIdAndTimeStr, objectInfoRecord);;
  49. obj2InfosMap.put(objId, infoRecords);
  50. }
  51. }else {
  52. // 写入目标文件
  53. writer.write(lineStr + "\n");
  54. }
  55. lineNum ++;
  56. }
  57. for(Map<String, ObjectInfoRecord> item : obj2InfosMap.values()) {
  58. //item对象结构 -> {信息点编码@时间:信息点记录}
  59. // 信息点编码
  60. String infoCode = null;
  61. // 信息点编码集合
  62. Set<String> infoCodeSet = new HashSet<>();
  63. for(String infoCodeAndTime : item.keySet()) {
  64. infoCode = infoCodeAndTime.split("@")[0];
  65. infoCodeSet.add(infoCode);
  66. }
  67. if (infoCodeSet.contains("DPBrandID") && infoCodeSet.contains("Brand")) {
  68. processBrand(item);
  69. }
  70. if (infoCodeSet.contains("DPMaintainerID") && (infoCodeSet.contains("Maintainer") || infoCodeSet.contains("MaintainerContactor") || infoCodeSet.contains("MaintainerPhone"))) {
  71. processNameContactPhone(item, "DPMaintainerID", "Maintainer", "MaintainerContactor", "MaintainerPhone");
  72. }
  73. if (infoCodeSet.contains("DPManufacturerID") && (infoCodeSet.contains("Manufacturer") || infoCodeSet.contains("ManufacturerContactor") || infoCodeSet.contains("ManufacturerPhone"))) {
  74. processNameContactPhone(item, "DPManufacturerID", "Manufacturer", "ManufacturerContactor", "ManufacturerPhone");
  75. }
  76. if (infoCodeSet.contains("DPSupplierID") && (infoCodeSet.contains("Supplier") || infoCodeSet.contains("SupplierContactor") || infoCodeSet.contains("SupplierPhone"))) {
  77. processNameContactPhone(item, "DPSupplierID", "Supplier", "SupplierContactor", "SupplierPhone");
  78. }
  79. if (infoCodeSet.contains("DPSpecificationID") && infoCodeSet.contains("Specification")) {
  80. processSpecific(item);
  81. }
  82. // 写入文件
  83. for(ObjectInfoRecord record : item.values()) {
  84. writer.write(record.toString() + "\n");
  85. }
  86. }
  87. result.put(keyResult, "处理完成。数据已存到:" + newFile.getAbsolutePath());
  88. }catch (Exception e) {
  89. e.printStackTrace();
  90. result.put(keyError, "处理第" + lineNum + "行数据出错! ErrorMsg=" + e.getMessage());
  91. newFile.delete();
  92. }finally {
  93. if(fis != null) {
  94. try {
  95. fis.close();
  96. } catch (IOException e) {
  97. e.printStackTrace();
  98. }
  99. }
  100. if(scanner != null) {
  101. try {
  102. scanner.close();
  103. } catch (Exception e) {
  104. e.printStackTrace();
  105. }
  106. }
  107. if(writer != null) {
  108. try {
  109. writer.close();
  110. } catch (IOException e) {
  111. e.printStackTrace();
  112. }
  113. }
  114. }
  115. return result;
  116. }
  117. /**
  118. * 处理自定义信息点 DPSpecificationID,对应的 Specification 信息点值
  119. * @param item {信息点编码@时间:信息点记录}
  120. */
  121. private static void processSpecific(Map<String, ObjectInfoRecord> item) {
  122. // 时间上最新的定义信息点 DPSpecificationID 对应的记录
  123. ObjectInfoRecord newCTMRecord = null;
  124. // 时间上最新的标准信息点 Specification 对应的记录
  125. ObjectInfoRecord newStandRecord = null;
  126. for(String key : item.keySet()) {
  127. if(key.startsWith("DPSpecificationID")) {
  128. if(null == newCTMRecord) {
  129. newCTMRecord = item.get(key);
  130. }else {
  131. newCTMRecord = (newCTMRecord.getTime()+"").compareTo(item.get(key).getTime()+"") > 0 ? newCTMRecord : item.get(key);
  132. }
  133. }
  134. if(key.startsWith("Specification")) {
  135. if(null == newStandRecord) {
  136. newStandRecord = item.get(key);
  137. }else {
  138. newStandRecord = (newStandRecord.getTime()+"").compareTo(item.get(key).getTime()+"") > 0 ? newStandRecord : item.get(key);
  139. }
  140. }
  141. }
  142. if(newCTMRecord == null || !VenderInfo.specificMap.keySet().contains(newCTMRecord.getS_value()) || newStandRecord == null) {
  143. return;
  144. }
  145. SpecificatDTO specificatDTO = VenderInfo.specificMap.get(newCTMRecord.getS_value());
  146. newStandRecord.setS_value(specificatDTO.getSpecName());
  147. }
  148. /**
  149. * 更新自定义信息点关联的标准信息点的值
  150. * @param item {信息点编码@时间:信息点记录}
  151. * @param ctmInfoCode 自定义信息点编码
  152. * @param nameCode 标准信息点 名称信息点编码
  153. * @param contactCode 标准信息点 联系人信息点编码
  154. * @param phoneCode 标准信息点 联系电话信息点编码
  155. */
  156. private static void processNameContactPhone(Map<String, ObjectInfoRecord> item, String ctmInfoCode, String nameCode, String contactCode, String phoneCode) {
  157. // 时间上最新的定义信息点 ctmInfoCode 对应的记录
  158. ObjectInfoRecord newCTMRecord = null;
  159. // 时间上最新的标准信息点 nameCode 对应的记录
  160. ObjectInfoRecord nameRecord = null;
  161. // 时间上最新的标准信息点 contactCode 对应的记录
  162. ObjectInfoRecord contactRecord = null;
  163. // 时间上最新的标准信息点 phoneCode 对应的记录
  164. ObjectInfoRecord phoneRecord = null;
  165. for(String key : item.keySet()) {
  166. String infoCode = key.split("@")[0];
  167. if(infoCode.equals(ctmInfoCode)) {
  168. if(null == newCTMRecord) {
  169. newCTMRecord = item.get(key);
  170. }else {
  171. newCTMRecord = (newCTMRecord.getTime()+"").compareTo(item.get(key).getTime()+"") > 0 ? newCTMRecord : item.get(key);
  172. }
  173. }
  174. if(infoCode.equals(nameCode)) {
  175. if(null == nameRecord) {
  176. nameRecord = item.get(key);
  177. }else {
  178. nameRecord = (nameRecord.getTime()+"").compareTo(item.get(key).getTime()+"") > 0 ? nameRecord : item.get(key);
  179. }
  180. }
  181. if(infoCode.equals(contactCode)) {
  182. if(null == contactRecord) {
  183. contactRecord = item.get(key);
  184. }else {
  185. contactRecord = (contactRecord.getTime()+"").compareTo(item.get(key).getTime()+"") > 0 ? contactRecord : item.get(key);
  186. }
  187. }
  188. if(infoCode.equals(phoneCode)) {
  189. if(null == phoneRecord) {
  190. phoneRecord = item.get(key);
  191. }else {
  192. phoneRecord = (phoneRecord.getTime()+"").compareTo(item.get(key).getTime()+"") > 0 ? phoneRecord : item.get(key);
  193. }
  194. }
  195. }
  196. if(newCTMRecord == null) {
  197. return;
  198. }
  199. String venderId = newCTMRecord.getS_value();
  200. if(VenderInfo.venderMap.containsKey(venderId) && nameRecord != null) {
  201. nameRecord.setS_value(VenderInfo.venderMap.get(venderId).getVenderName());
  202. }
  203. if(VenderInfo.venderContMap.containsKey(venderId)) {
  204. VenderContactDTO venderContactDTO = VenderInfo.venderContMap.get(venderId);
  205. if(contactRecord != null) {
  206. contactRecord.setS_value(venderContactDTO.getName());
  207. }
  208. if(phoneRecord != null) {
  209. phoneRecord.setS_value(venderContactDTO.getPhone());
  210. }
  211. }
  212. }
  213. /**
  214. * 处理自定义信息点 DPBrandID,对应的Brand信息点值
  215. * @param item {信息点编码@时间:信息点记录}
  216. */
  217. private static void processBrand(Map<String, ObjectInfoRecord> item) {
  218. // 时间上最新的定义信息点 DPBrandID对应的记录
  219. ObjectInfoRecord newCTMRecord = null;
  220. // 时间上最新的标准信息点 Brand 对应的记录
  221. ObjectInfoRecord newStandRecord = null;
  222. for(String key : item.keySet()) {
  223. if(key.startsWith("DPBrandID")) {
  224. if(null == newCTMRecord) {
  225. newCTMRecord = item.get(key);
  226. }else {
  227. newCTMRecord = (newCTMRecord.getTime()+"").compareTo(item.get(key).getTime()+"") > 0 ? newCTMRecord : item.get(key);
  228. }
  229. }
  230. if(key.startsWith("Brand")) {
  231. if(null == newStandRecord) {
  232. newStandRecord = item.get(key);
  233. }else {
  234. newStandRecord = (newStandRecord.getTime()+"").compareTo(item.get(key).getTime()+"") > 0 ? newStandRecord : item.get(key);
  235. }
  236. }
  237. }
  238. if(newCTMRecord == null || !VenderInfo.brandMap.keySet().contains(newCTMRecord.getS_value()) || newStandRecord == null) {
  239. return;
  240. }
  241. BrandDTO brandDTO = VenderInfo.brandMap.get(newCTMRecord.getS_value());
  242. newStandRecord.setS_value(brandDTO.getBrandName());
  243. }
  244. private static File getNewFile(File sourceFile) {
  245. File newFile = null;
  246. try {
  247. String oldFileName = sourceFile.getName();
  248. String suffix = oldFileName.substring(oldFileName.lastIndexOf("."));
  249. String subOldFileName = oldFileName.substring(0, oldFileName.lastIndexOf("."));
  250. String newFileName = subOldFileName + "_" + new Date().getTime() + suffix;
  251. newFile = new File(sourceFile.getParent(), newFileName);
  252. if(!newFile.createNewFile()) {
  253. newFile = null;
  254. }
  255. }catch (Exception e) {
  256. e.printStackTrace();
  257. }
  258. return newFile;
  259. }
  260. public static void main(String[] args) throws JsonProcessingException {
  261. String time1 = "20211214212806";
  262. String time2 = "null";
  263. System.out.println(time2.compareTo(time1));
  264. }
  265. }