Parcourir la source

实现buildVenderMapProcessor

FanXiaoyu il y a 6 ans
Parent
commit
9dce42e523

+ 45 - 0
src/main/java/com/sagacloud/pojos/venderDetail/AllVender.java

@@ -0,0 +1,45 @@
+package com.sagacloud.pojos.venderDetail;
+
+import java.util.List;
+
+/**
+ * Created by Xiaoyu on 2018/7/13
+ */
+public class AllVender {
+    private List<Manu> manuList;
+    private List<Maintn> maintnList;
+    private List<Insure> insureList;
+    private List<Supp> suppList;
+
+    public List<Manu> getManuList() {
+        return manuList;
+    }
+
+    public void setManuList(List<Manu> manuList) {
+        this.manuList = manuList;
+    }
+
+    public List<Maintn> getMaintnList() {
+        return maintnList;
+    }
+
+    public void setMaintnList(List<Maintn> maintnList) {
+        this.maintnList = maintnList;
+    }
+
+    public List<Insure> getInsureList() {
+        return insureList;
+    }
+
+    public void setInsureList(List<Insure> insureList) {
+        this.insureList = insureList;
+    }
+
+    public List<Supp> getSuppList() {
+        return suppList;
+    }
+
+    public void setSuppList(List<Supp> suppList) {
+        this.suppList = suppList;
+    }
+}

+ 42 - 0
src/main/java/com/sagacloud/pojos/venderDetail/Contract.java

@@ -0,0 +1,42 @@
+package com.sagacloud.pojos.venderDetail;
+
+import java.text.SimpleDateFormat;
+import java.util.Date;
+
+/**
+ * Created by Xiaoyu on 2018/7/13
+ */
+public class Contract {
+    private String insuranceNo;
+    private String expireDate;
+
+    public String getInsuranceNo() {
+        return insuranceNo;
+    }
+
+    public void setInsuranceNo(String insuranceNo) {
+        this.insuranceNo = insuranceNo;
+    }
+
+    public String getExpireDate() {
+        return expireDate;
+    }
+
+    public void setExpireDate(String expireDate) {
+        this.expireDate = expireDate;
+    }
+
+    public boolean underWarranty(Date now){
+        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
+        try {
+            Date expire = sdf.parse(expireDate);
+            if(now.compareTo(expire) <= 0){
+                return true;//在质保期内
+            }
+            return false;
+        }catch (Exception ignore){
+            return true;
+        }
+
+    }
+}

+ 36 - 0
src/main/java/com/sagacloud/pojos/venderDetail/Insure.java

@@ -0,0 +1,36 @@
+package com.sagacloud.pojos.venderDetail;
+
+import java.util.List;
+
+/**
+ * Created by Xiaoyu on 2018/7/13
+ */
+public class Insure {
+    private String venderId;
+    private String name;
+    private List<Contract> contract;
+
+    public String getVenderId() {
+        return venderId;
+    }
+
+    public void setVenderId(String venderId) {
+        this.venderId = venderId;
+    }
+
+    public String getName() {
+        return name;
+    }
+
+    public void setName(String name) {
+        this.name = name;
+    }
+
+    public List<Contract> getContract() {
+        return contract;
+    }
+
+    public void setContract(List<Contract> contract) {
+        this.contract = contract;
+    }
+}

+ 25 - 0
src/main/java/com/sagacloud/pojos/venderDetail/Maintn.java

@@ -0,0 +1,25 @@
+package com.sagacloud.pojos.venderDetail;
+
+/**
+ * Created by Xiaoyu on 2018/7/13
+ */
+public class Maintn {
+    private String venderId;
+    private String name;
+
+    public String getVenderId() {
+        return venderId;
+    }
+
+    public void setVenderId(String venderId) {
+        this.venderId = venderId;
+    }
+
+    public String getName() {
+        return name;
+    }
+
+    public void setName(String name) {
+        this.name = name;
+    }
+}

+ 54 - 0
src/main/java/com/sagacloud/pojos/venderDetail/Manu.java

@@ -0,0 +1,54 @@
+package com.sagacloud.pojos.venderDetail;
+
+import java.util.List;
+
+/**
+ * Created by Xiaoyu on 2018/7/13
+ */
+public class Manu {
+    private String venderId;
+    private String name;
+    List<String> brandName;
+    List<String> specName;
+    List<String> specId;
+
+    public String getVenderId() {
+        return venderId;
+    }
+
+    public void setVenderId(String venderId) {
+        this.venderId = venderId;
+    }
+
+    public String getName() {
+        return name;
+    }
+
+    public void setName(String name) {
+        this.name = name;
+    }
+
+    public List<String> getBrandName() {
+        return brandName;
+    }
+
+    public void setBrandName(List<String> brandName) {
+        this.brandName = brandName;
+    }
+
+    public List<String> getSpecName() {
+        return specName;
+    }
+
+    public void setSpecName(List<String> specName) {
+        this.specName = specName;
+    }
+
+    public List<String> getSpecId() {
+        return specId;
+    }
+
+    public void setSpecId(List<String> specId) {
+        this.specId = specId;
+    }
+}

+ 25 - 0
src/main/java/com/sagacloud/pojos/venderDetail/Supp.java

@@ -0,0 +1,25 @@
+package com.sagacloud.pojos.venderDetail;
+
+/**
+ * Created by Xiaoyu on 2018/7/13
+ */
+public class Supp {
+    private String venderId;
+    private String name;
+
+    public String getVenderId() {
+        return venderId;
+    }
+
+    public void setVenderId(String venderId) {
+        this.venderId = venderId;
+    }
+
+    public String getName() {
+        return name;
+    }
+
+    public void setName(String name) {
+        this.name = name;
+    }
+}

+ 5 - 0
src/main/java/com/sagacloud/route/Route.java

@@ -12,6 +12,7 @@ import com.sagacloud.route.processors.PeriodTask.StatisticsProcessor;
 import com.sagacloud.route.processors.VenderMapProcessor;
 import com.sagacloud.route.processors.supplier.SupplierFilter;
 import com.sagacloud.utils.Const;
+import org.apache.camel.Exchange;
 import org.apache.commons.io.IOUtils;
 
 import javax.ws.rs.core.MediaType;
@@ -143,6 +144,10 @@ public class Route extends ExceptionHandlerBaseRoute {
                 .to("direct:getPropertiesByPj");
 
         from("direct:buildVenderMap")
+                .process(exchange -> {
+                    Object temp = exchange.getIn().getBody();
+                    exchange.setProperty("tempBody",temp);
+                })
                 .to(Const.venders+"/auxiliary/vender/query_detail").
                 process(new VenderMapProcessor());
     }

+ 124 - 8
src/main/java/com/sagacloud/route/processors/VenderMapProcessor.java

@@ -1,12 +1,14 @@
 package com.sagacloud.route.processors;
 
+import com.alibaba.fastjson.JSON;
+import com.alibaba.fastjson.JSONArray;
+import com.alibaba.fastjson.JSONObject;
+import com.sagacloud.Exceptions.InvalidPostException;
+import com.sagacloud.pojos.venderDetail.*;
 import org.apache.camel.Exchange;
 import org.apache.camel.Processor;
 
-import java.util.HashMap;
-import java.util.HashSet;
-import java.util.Map;
-import java.util.Set;
+import java.util.*;
 
 /**
  * Created by Xiaoyu on 2018/7/13
@@ -14,9 +16,123 @@ import java.util.Set;
 public class VenderMapProcessor implements Processor {
     @Override
     public void process(Exchange exchange) throws Exception {
-        //第一层为Manufacturer、Brand、Specification、Supplier、SupplierContractID、Insurer、InsuranceNum、Maintainer
-        Map<String,Map> nameMap = new HashMap<>();
-        Map<String,String> idMap = new HashMap<>();//厂商id->厂商id,型号id->厂商id
-        Set<String> venderSet = new HashSet<>();
+        //1.第一层为Manufacturer、Brand、Specification、Supplier、Insurer、InsuranceNum、Maintainer时
+        //第二层map的key为厂商名称、产品名称等,指向厂商id
+        //2.第一层为DPManufacturerID、DPSpecificationID、DPSupplierID、DPInsurerID、DPMaintainerID时
+        //第二层为厂商id指向厂商id 或 型号id指向厂商id
+        Map<String, Map> nameMap = new HashMap<>();
+        String jsonStr = exchange.getIn().getBody(String.class);
+        JSONObject json = JSONObject.parseObject(jsonStr);
+        if (json.getString("result").equals("failure")) {
+            throw new InvalidPostException("请求厂商库出错");
+        }
+        JSONObject content = json.getJSONObject("content");
+        AllVender venders = JSONObject.parseObject(content.toString(), AllVender.class);
+        buildNameMap(venders,nameMap);
+        exchange.setProperty("map",nameMap);
+        exchange.getIn().setBody(exchange.getProperty("tempBody"));
+    }
+
+    public void buildNameMap(AllVender venders, Map<String, Map> nameMap) {
+        initMap(nameMap);
+        List<Manu> manuList = venders.getManuList();
+        List<Supp> suppList = venders.getSuppList();
+        List<Insure> insureList = venders.getInsureList();
+        List<Maintn> maintnList = venders.getMaintnList();
+        handleMaunfacturer(nameMap,manuList);
+        handleSupplier(nameMap,suppList);
+        handleInsurer(nameMap,insureList);
+        handleMtn(nameMap,maintnList);
+    }
+
+    public void initMap(Map<String, Map> nameMap) {
+        String[] keyList = new String[]{"Manufacturer", "Brand", "Specification", "Supplier", "Insurer", "InsuranceNum", "Maintainer",
+                "DPManufacturerID", "DPSpecificationID", "DPSupplierID", "DPInsurerID", "DPMaintainerID"};
+        for (String key : keyList) {
+            nameMap.put(key, new HashMap<String, String>());
+        }
+    }
+
+    public void handleMtn(Map<String, Map> nameMap, List<Maintn> maintnList){
+        Set<String> visitedMaintainer = new HashSet<>();
+        Set<String> conflictMaintainer = new HashSet<>();
+        Map<String, String> maintainer = nameMap.get("Maintainer");
+        Map<String, String> dpMaintainerID = nameMap.get("DPMaintainerID");
+        for(Maintn m : maintnList){
+            fillMap(maintainer,m.getName(),m.getVenderId(),visitedMaintainer,conflictMaintainer);
+            dpMaintainerID.put(m.getVenderId(),m.getVenderId());
+        }
+    }
+
+    public void handleInsurer(Map<String, Map> nameMap, List<Insure> insureList){
+        Date now = new Date();
+        Set<String> visitedInsurer = new HashSet<>();
+        Set<String> conflictInsurer = new HashSet<>();
+        Map<String, String> insurer = nameMap.get("Insurer");
+        Map<String, String> dpInsurerID = nameMap.get("DPInsurerID");
+        Map<String, String> insuranceNum = nameMap.get("InsuranceNum");
+        for(Insure i : insureList ){
+            fillMap(insurer,i.getName(),i.getVenderId(),visitedInsurer,conflictInsurer);
+            dpInsurerID.put(i.getVenderId(),i.getVenderId());
+            for(Contract c : i.getContract()){
+                if(c.underWarranty(now)){
+                    insuranceNum.put(c.getInsuranceNo(),i.getVenderId());
+                }
+            }
+        }
+    }
+
+    public void handleSupplier(Map<String, Map> nameMap, List<Supp> suppList){
+        Set<String> visitedSupplier = new HashSet<>();
+        Set<String> conflictSupplier = new HashSet<>();
+        Map<String, String> supplier = nameMap.get("Supplier");
+        Map<String, String> dpSupplierID = nameMap.get("DPSupplierID");
+        for(Supp s : suppList){
+            fillMap(supplier,s.getName(),s.getVenderId(),visitedSupplier,conflictSupplier);
+            dpSupplierID.put(s.getVenderId(),s.getVenderId());
+        }
+    }
+
+    public void handleMaunfacturer(Map<String, Map> nameMap, List<Manu> manuList) {
+        Set<String> visitedManufacturer = new HashSet<>();
+        Set<String> conflictManufacturer = new HashSet<>();
+        Set<String> visitedBrand = new HashSet<>();
+        Set<String> conflictBrand = new HashSet<>();
+        Set<String> visitedSpecification = new HashSet<>();
+        Set<String> conflictSpecification = new HashSet<>();
+        Map<String, String> manufacturer = nameMap.get("Manufacturer");
+        Map<String, String> brand = nameMap.get("Brand");
+        Map<String, String> specification = nameMap.get("Specification");
+        Map<String, String> dpManufacturerID = nameMap.get("DPManufacturerID");
+        Map<String, String> dpSpecificationID = nameMap.get("DPSpecificationID");
+        for (Manu m : manuList) {
+            String venderId = m.getVenderId();
+            fillMap(manufacturer,m.getName(),venderId,visitedManufacturer,conflictManufacturer);
+            dpManufacturerID.put(venderId, venderId);
+            for (String specId : m.getSpecId()) {
+                dpSpecificationID.put(specId,venderId);
+            }
+            for(String specName:m.getSpecName()){
+                fillMap(specification,specName,venderId,visitedSpecification,conflictSpecification);
+            }
+            for(String brandName : m.getBrandName()){
+                fillMap(brand,brandName,venderId,visitedBrand,conflictBrand);
+            }
+        }
+
+    }
+
+    private void fillMap(Map map, String key, String value, Set<String> visited, Set<String> conflict) {
+        visited.add(key);
+        if (conflict.contains(key)) {//如果之前产生过冲突,不再添加
+            return;
+        } else if (!map.containsKey(key)) {
+            map.put(key, value);
+        } else {
+            if (map.get(key).equals(value)) {
+                conflict.add(key);
+                map.remove(key);
+            }
+        }
     }
 }