| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315 |
- 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<String, String> business(File sourceFile, VenderInfo venderInfo) {
- Map<String, String> 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<String, Map<String, ObjectInfoRecord>> 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<String, ObjectInfoRecord> infoRecords = new HashMap<>();
- infoRecords.put(infoIdAndTimeStr, objectInfoRecord);;
- obj2InfosMap.put(objId, infoRecords);
- }
- }else {
- // 写入目标文件
- writer.write(lineStr + "\n");
- }
- lineNum ++;
- }
- for(Map<String, ObjectInfoRecord> item : obj2InfosMap.values()) {
- //item对象结构 -> {信息点编码@时间:信息点记录}
- // 信息点编码
- String infoCode = null;
- // 信息点编码集合
- Set<String> 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<String, ObjectInfoRecord> 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<String, ObjectInfoRecord> 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<String, ObjectInfoRecord> 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));
- }
- }
|