package com.persagy.dptool; import com.fasterxml.jackson.core.JsonProcessingException; import com.persagy.dptool.dto.VenderInfo; import com.persagy.dptool.dto.vender.BrandDTO; import com.persagy.dptool.dto.vender.ObjectInfoRecord; import com.persagy.dptool.dto.vender.SpecificatDTO; import com.persagy.dptool.dto.vender.VenderContactDTO; import java.io.*; import java.util.*; public class VenderUtil { public static final String keyResult = "RESULT"; public static final String keyError = "ERROR"; public static Map business(File sourceFile, VenderInfo venderInfo) { Map result = new HashMap<>(); File newFile = getNewFile(sourceFile); if(newFile == null) { result.put(keyError, "在目录"+sourceFile.getParent()+"下创建数据转移文件出错!"); return result; } String pjId = venderInfo.pjId; String pjIdField = "\"project_id\":\""+pjId+"\""; int lineNum = 1; FileInputStream fis = null; Scanner scanner = null; BufferedWriter writer = null; try { fis = new FileInputStream(sourceFile); scanner = new Scanner(fis, "utf-8"); writer = new BufferedWriter(new FileWriter(newFile)); String lineStr = null, objId = null, infoIdAndTimeStr = null; // {设备id:{信息点编码@时间:信息点记录}} Map> obj2InfosMap = new HashMap<>(); while(scanner.hasNext()) { lineStr = scanner.nextLine(); if(lineStr == null || !lineStr.startsWith("{")) { lineNum ++; continue; } if(lineStr.contains("\"obj_type\":\"Eq\"") && lineStr.contains(pjIdField) && lineStr.contains("obj_id") && lineStr.contains("info_id")) { // 判定为指定项目的设备记录 ObjectInfoRecord objectInfoRecord = CommonUtil.jsonStrToObj(lineStr, ObjectInfoRecord.class); objId = objectInfoRecord.getObj_id(); infoIdAndTimeStr = objectInfoRecord.getInfo_id() + "@" + objectInfoRecord.getTime(); if(obj2InfosMap.containsKey(objId)) { obj2InfosMap.get(objId).put(infoIdAndTimeStr, objectInfoRecord); }else { Map infoRecords = new HashMap<>(); infoRecords.put(infoIdAndTimeStr, objectInfoRecord);; obj2InfosMap.put(objId, infoRecords); } }else { // 写入目标文件 writer.write(lineStr + "\n"); } lineNum ++; } for(Map item : obj2InfosMap.values()) { //item对象结构 -> {信息点编码@时间:信息点记录} // 信息点编码 String infoCode = null; // 信息点编码集合 Set infoCodeSet = new HashSet<>(); for(String infoCodeAndTime : item.keySet()) { infoCode = infoCodeAndTime.split("@")[0]; infoCodeSet.add(infoCode); } if (infoCodeSet.contains("DPBrandID") && infoCodeSet.contains("Brand")) { processBrand(item); } if (infoCodeSet.contains("DPMaintainerID") && (infoCodeSet.contains("Maintainer") || infoCodeSet.contains("MaintainerContactor") || infoCodeSet.contains("MaintainerPhone"))) { processNameContactPhone(item, "DPMaintainerID", "Maintainer", "MaintainerContactor", "MaintainerPhone"); } if (infoCodeSet.contains("DPManufacturerID") && (infoCodeSet.contains("Manufacturer") || infoCodeSet.contains("ManufacturerContactor") || infoCodeSet.contains("ManufacturerPhone"))) { processNameContactPhone(item, "DPManufacturerID", "Manufacturer", "ManufacturerContactor", "ManufacturerPhone"); } if (infoCodeSet.contains("DPSupplierID") && (infoCodeSet.contains("Supplier") || infoCodeSet.contains("SupplierContactor") || infoCodeSet.contains("SupplierPhone"))) { processNameContactPhone(item, "DPSupplierID", "Supplier", "SupplierContactor", "SupplierPhone"); } if (infoCodeSet.contains("DPSpecificationID") && infoCodeSet.contains("Specification")) { processSpecific(item); } // 写入文件 for(ObjectInfoRecord record : item.values()) { writer.write(record.toString() + "\n"); } } result.put(keyResult, "处理完成。数据已存到:" + newFile.getAbsolutePath()); }catch (Exception e) { e.printStackTrace(); result.put(keyError, "处理第" + lineNum + "行数据出错! ErrorMsg=" + e.getMessage()); newFile.delete(); }finally { if(fis != null) { try { fis.close(); } catch (IOException e) { e.printStackTrace(); } } if(scanner != null) { try { scanner.close(); } catch (Exception e) { e.printStackTrace(); } } if(writer != null) { try { writer.close(); } catch (IOException e) { e.printStackTrace(); } } } return result; } /** * 处理自定义信息点 DPSpecificationID,对应的 Specification 信息点值 * @param item {信息点编码@时间:信息点记录} */ private static void processSpecific(Map item) { // 时间上最新的定义信息点 DPSpecificationID 对应的记录 ObjectInfoRecord newCTMRecord = null; // 时间上最新的标准信息点 Specification 对应的记录 ObjectInfoRecord newStandRecord = null; for(String key : item.keySet()) { if(key.startsWith("DPSpecificationID")) { if(null == newCTMRecord) { newCTMRecord = item.get(key); }else { newCTMRecord = (newCTMRecord.getTime()+"").compareTo(item.get(key).getTime()+"") > 0 ? newCTMRecord : item.get(key); } } if(key.startsWith("Specification")) { if(null == newStandRecord) { newStandRecord = item.get(key); }else { newStandRecord = (newStandRecord.getTime()+"").compareTo(item.get(key).getTime()+"") > 0 ? newStandRecord : item.get(key); } } } if(newCTMRecord == null || !VenderInfo.specificMap.keySet().contains(newCTMRecord.getS_value()) || newStandRecord == null) { return; } SpecificatDTO specificatDTO = VenderInfo.specificMap.get(newCTMRecord.getS_value()); newStandRecord.setS_value(specificatDTO.getSpecName()); } /** * 更新自定义信息点关联的标准信息点的值 * @param item {信息点编码@时间:信息点记录} * @param ctmInfoCode 自定义信息点编码 * @param nameCode 标准信息点 名称信息点编码 * @param contactCode 标准信息点 联系人信息点编码 * @param phoneCode 标准信息点 联系电话信息点编码 */ private static void processNameContactPhone(Map item, String ctmInfoCode, String nameCode, String contactCode, String phoneCode) { // 时间上最新的定义信息点 ctmInfoCode 对应的记录 ObjectInfoRecord newCTMRecord = null; // 时间上最新的标准信息点 nameCode 对应的记录 ObjectInfoRecord nameRecord = null; // 时间上最新的标准信息点 contactCode 对应的记录 ObjectInfoRecord contactRecord = null; // 时间上最新的标准信息点 phoneCode 对应的记录 ObjectInfoRecord phoneRecord = null; for(String key : item.keySet()) { String infoCode = key.split("@")[0]; if(infoCode.equals(ctmInfoCode)) { if(null == newCTMRecord) { newCTMRecord = item.get(key); }else { newCTMRecord = (newCTMRecord.getTime()+"").compareTo(item.get(key).getTime()+"") > 0 ? newCTMRecord : item.get(key); } } if(infoCode.equals(nameCode)) { if(null == nameRecord) { nameRecord = item.get(key); }else { nameRecord = (nameRecord.getTime()+"").compareTo(item.get(key).getTime()+"") > 0 ? nameRecord : item.get(key); } } if(infoCode.equals(contactCode)) { if(null == contactRecord) { contactRecord = item.get(key); }else { contactRecord = (contactRecord.getTime()+"").compareTo(item.get(key).getTime()+"") > 0 ? contactRecord : item.get(key); } } if(infoCode.equals(phoneCode)) { if(null == phoneRecord) { phoneRecord = item.get(key); }else { phoneRecord = (phoneRecord.getTime()+"").compareTo(item.get(key).getTime()+"") > 0 ? phoneRecord : item.get(key); } } } if(newCTMRecord == null) { return; } String venderId = newCTMRecord.getS_value(); if(VenderInfo.venderMap.containsKey(venderId) && nameRecord != null) { nameRecord.setS_value(VenderInfo.venderMap.get(venderId).getVenderName()); } if(VenderInfo.venderContMap.containsKey(venderId)) { VenderContactDTO venderContactDTO = VenderInfo.venderContMap.get(venderId); if(contactRecord != null) { contactRecord.setS_value(venderContactDTO.getName()); } if(phoneRecord != null) { phoneRecord.setS_value(venderContactDTO.getPhone()); } } } /** * 处理自定义信息点 DPBrandID,对应的Brand信息点值 * @param item {信息点编码@时间:信息点记录} */ private static void processBrand(Map item) { // 时间上最新的定义信息点 DPBrandID对应的记录 ObjectInfoRecord newCTMRecord = null; // 时间上最新的标准信息点 Brand 对应的记录 ObjectInfoRecord newStandRecord = null; for(String key : item.keySet()) { if(key.startsWith("DPBrandID")) { if(null == newCTMRecord) { newCTMRecord = item.get(key); }else { newCTMRecord = (newCTMRecord.getTime()+"").compareTo(item.get(key).getTime()+"") > 0 ? newCTMRecord : item.get(key); } } if(key.startsWith("Brand")) { if(null == newStandRecord) { newStandRecord = item.get(key); }else { newStandRecord = (newStandRecord.getTime()+"").compareTo(item.get(key).getTime()+"") > 0 ? newStandRecord : item.get(key); } } } if(newCTMRecord == null || !VenderInfo.brandMap.keySet().contains(newCTMRecord.getS_value()) || newStandRecord == null) { return; } BrandDTO brandDTO = VenderInfo.brandMap.get(newCTMRecord.getS_value()); newStandRecord.setS_value(brandDTO.getBrandName()); } private static File getNewFile(File sourceFile) { File newFile = null; try { String oldFileName = sourceFile.getName(); String suffix = oldFileName.substring(oldFileName.lastIndexOf(".")); String subOldFileName = oldFileName.substring(0, oldFileName.lastIndexOf(".")); String newFileName = subOldFileName + "_" + new Date().getTime() + suffix; newFile = new File(sourceFile.getParent(), newFileName); if(!newFile.createNewFile()) { newFile = null; } }catch (Exception e) { e.printStackTrace(); } return newFile; } public static void main(String[] args) throws JsonProcessingException { String time1 = "20211214212806"; String time2 = "null"; System.out.println(time2.compareTo(time1)); } }