Browse Source

完善厂商库信息相关逻辑

cuixubin 4 years ago
parent
commit
31de82963f

+ 3 - 0
src/main/java/com/persagy/dptool/PrimaryController.java

@@ -40,6 +40,8 @@ public class PrimaryController implements Initializable {
     @FXML
     public Label lblVenderTip;
     @FXML
+    public Label lblVenderResult;
+    @FXML
     public TextField txfObjInfosJson;
     @FXML
     public Button btnCheck;
@@ -123,6 +125,7 @@ public class PrimaryController implements Initializable {
      */
     public void venderValueHandle(ActionEvent e) {
         lblVenderTip.setText("");
+        lblVenderResult.setText("");
 
         String projectId = txfProjectVender.getText();
 

+ 30 - 1
src/main/java/com/persagy/dptool/TaskFactory.java

@@ -21,10 +21,39 @@ public class TaskFactory {
         return new Task<Boolean>() {
             @Override
             protected Boolean call() throws Exception {
+                Platform.runLater(()->{
+                    controller.setDisable(true, 3);
+                    controller.piState.setVisible(true);
+                    controller.lblState.setText("准备...");
+                });
+
                 if(venderInfo.initData()) {
-                    System.out.println("初始化完成");
+                    Thread.sleep(1000L);
+
+                    Platform.runLater(()->{
+                        controller.lblState.setText("正在处理数据...请不要编辑被处理的文件。");
+                    });
+
+                    Map<String, String> resultMap = VenderUtil.business(jsonFile, venderInfo);
+
+                    Platform.runLater(()->{
+                        if(resultMap.containsKey(VenderUtil.keyError)) {
+                            controller.lblVenderTip.setText(resultMap.get(VenderUtil.keyError));
+                        }else {
+                            controller.lblVenderResult.setText(resultMap.get(VenderUtil.keyResult));
+                        }
+                    });
+                }else {
+                    Platform.runLater(()->{
+                        controller.lblVenderTip.setText("初始化厂商库配置信息失败!请联系开发人员。");
+                    });
                 }
 
+                Platform.runLater(()->{
+                    controller.setDisable(false, 3);
+                    controller.piState.setVisible(false);
+                    controller.lblState.setText("");
+                });
                 return true;
             }
         };

+ 315 - 0
src/main/java/com/persagy/dptool/VenderUtil.java

@@ -0,0 +1,315 @@
+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("MaintainerContacto") || infoCodeSet.contains("MaintainerPhone"))) {
+                    processNameContactPhone(item, "DPMaintainerID", "Maintainer", "MaintainerContacto", "MaintainerPhone");
+                }
+                if (infoCodeSet.contains("DPManufacturerID") && (infoCodeSet.contains("Manufacturer") || infoCodeSet.contains("ManufacturerContacto") || infoCodeSet.contains("ManufacturerPhone"))) {
+                    processNameContactPhone(item, "DPManufacturerID", "Manufacturer", "ManufacturerContacto", "ManufacturerPhone");
+                }
+                if (infoCodeSet.contains("DPSupplierID") && (infoCodeSet.contains("Supplier") || infoCodeSet.contains("SupplierContacto") || infoCodeSet.contains("SupplierPhone"))) {
+                    processNameContactPhone(item, "DPSupplierID", "Supplier", "SupplierContacto", "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()) {
+            if(key.startsWith(ctmInfoCode)) {
+                if(null == newCTMRecord) {
+                    newCTMRecord = item.get(key);
+                }else {
+                    newCTMRecord = (newCTMRecord.getTime()+"").compareTo(item.get(key).getTime()+"") > 0 ? newCTMRecord : item.get(key);
+                }
+            }
+
+            if(key.startsWith(nameCode)) {
+                if(null == nameRecord) {
+                    nameRecord = item.get(key);
+                }else {
+                    nameRecord = (nameRecord.getTime()+"").compareTo(item.get(key).getTime()+"") > 0 ? nameRecord : item.get(key);
+                }
+            }
+
+            if(key.startsWith(contactCode)) {
+                if(null == contactRecord) {
+                    contactRecord = item.get(key);
+                }else {
+                    contactRecord = (contactRecord.getTime()+"").compareTo(item.get(key).getTime()+"") > 0 ? contactRecord : item.get(key);
+                }
+            }
+
+            if(key.startsWith(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;
+
+            System.out.println(oldFileName + ", " + suffix + ", " + subOldFileName + ", " + newFileName);
+
+            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));
+    }
+}

+ 4 - 0
src/main/java/com/persagy/dptool/dto/VenderInfo.java

@@ -18,9 +18,13 @@ public class VenderInfo {
     }
 
     public static boolean init = false;
+    /** {venderId:VenderDTO} */
     public static Map<String, VenderDTO> venderMap = new HashMap<>();
+    /** {brandId:BrandDTO} */
     public static Map<String, BrandDTO> brandMap = new HashMap<>();
+    /** {venderId:VenderContactDTO} */
     public static Map<String, VenderContactDTO> venderContMap = new HashMap<>();
+    /** {specId:SpecificatDTO} */
     public static Map<String, SpecificatDTO> specificMap = new HashMap<>();
 
     public boolean initData() {

+ 118 - 0
src/main/java/com/persagy/dptool/dto/vender/ObjectInfoRecord.java

@@ -0,0 +1,118 @@
+package com.persagy.dptool.dto.vender;
+
+public class ObjectInfoRecord {
+    private String project_id;
+    private String obj_type;
+    private String obj_id;
+    private String info_id;
+    private String time;
+    private Boolean b_value;
+    private Long l_value;
+    private String s_value;
+    private Double d_value;
+    private String json_value;
+
+    public String getProject_id() {
+        return project_id;
+    }
+
+    public void setProject_id(String project_id) {
+        this.project_id = project_id;
+    }
+
+    public String getObj_type() {
+        return obj_type;
+    }
+
+    public void setObj_type(String obj_type) {
+        this.obj_type = obj_type;
+    }
+
+    public String getObj_id() {
+        return obj_id;
+    }
+
+    public void setObj_id(String obj_id) {
+        this.obj_id = obj_id;
+    }
+
+    public String getInfo_id() {
+        return info_id;
+    }
+
+    public void setInfo_id(String info_id) {
+        this.info_id = info_id;
+    }
+
+    public String getTime() {
+        return time;
+    }
+
+    public void setTime(String time) {
+        this.time = time;
+    }
+
+    public Boolean getB_value() {
+        return b_value;
+    }
+
+    public void setB_value(Boolean b_value) {
+        this.b_value = b_value;
+    }
+
+    public Long getL_value() {
+        return l_value;
+    }
+
+    public void setL_value(Long l_value) {
+        this.l_value = l_value;
+    }
+
+    public String getS_value() {
+        return s_value;
+    }
+
+    public void setS_value(String s_value) {
+        this.s_value = s_value;
+    }
+
+    public Double getD_value() {
+        return d_value;
+    }
+
+    public void setD_value(Double d_value) {
+        this.d_value = d_value;
+    }
+
+    public String getJson_value() {
+        return json_value;
+    }
+
+    public void setJson_value(String json_value) {
+        this.json_value = json_value;
+    }
+
+    @Override
+    public String toString() {
+        StringBuilder sb = new StringBuilder("{");
+        if(project_id != null) {sb.append("\"project_id\":\""+project_id+"\",");}
+        if(obj_type != null) {sb.append("\"obj_type\":\""+obj_type+"\",");}
+        if(obj_id != null) {sb.append("\"obj_id\":\""+obj_id+"\",");}
+        if(info_id != null) {sb.append("\"info_id\":\""+info_id+"\",");}
+        if(time != null) {sb.append("\"time\":\""+time+"\",");}
+        if(s_value != null) {sb.append("\"s_value\":\""+s_value+"\",");}
+        if(l_value != null) {sb.append("\"l_value\":"+l_value+",");}
+        if(d_value != null) {sb.append("\"d_value\":"+d_value+",");}
+        if(b_value != null) {sb.append("\"b_value\":"+b_value+",");}
+        if(json_value != null) {sb.append("\"json_value\":\""+json_value+"\",");}
+
+        String result = sb.toString();
+        if(result.endsWith(",")) {
+            result = result.substring(0, result.length() - 1);
+        }
+
+        result += "}";
+
+        return result;
+    }
+}

+ 3 - 1
src/main/resources/primary.fxml

@@ -1,5 +1,6 @@
 <?xml version="1.0" encoding="UTF-8"?>
 
+<?import javafx.scene.paint.*?>
 <?import javafx.scene.effect.*?>
 <?import javafx.scene.text.*?>
 <?import javafx.geometry.*?>
@@ -102,7 +103,8 @@
                         <Label layoutX="55.0" layoutY="142.0" />
                         <Label layoutX="21.0" layoutY="171.0" />
                         <Button layoutX="405.0" layoutY="137.0" mnemonicParsing="false" onAction="#venderValueHandle" text="处理" />
-                        <Label fx:id="lblVenderTip" layoutX="91.0" layoutY="257.0" prefHeight="32.0" prefWidth="697.0" textFill="RED" />
+                        <Label fx:id="lblVenderTip" layoutX="39.0" layoutY="191.0" prefHeight="32.0" prefWidth="782.0" textFill="RED" />
+                        <Label fx:id="lblVenderResult" layoutX="39.0" layoutY="250.0" prefHeight="35.0" prefWidth="783.0" textFill="#3b8d4d" />
                      </children></AnchorPane>
                     </content>
                 </Tab>