VenderMapProcessor.java 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. package com.sagacloud.route.processors;
  2. import com.alibaba.fastjson.JSON;
  3. import com.alibaba.fastjson.JSONArray;
  4. import com.alibaba.fastjson.JSONObject;
  5. import com.sagacloud.Exceptions.InvalidPostException;
  6. import com.sagacloud.pojos.venderDetail.*;
  7. import org.apache.camel.Exchange;
  8. import org.apache.camel.Processor;
  9. import java.util.*;
  10. /**
  11. * Created by Xiaoyu on 2018/7/13
  12. */
  13. public class VenderMapProcessor implements Processor {
  14. @Override
  15. public void process(Exchange exchange) throws Exception {
  16. //1.第一层为Manufacturer、Brand、Specification、Supplier、Insurer、InsuranceNum、Maintainer时
  17. //第二层map的key为厂商名称、产品名称等,指向厂商id
  18. //2.第一层为DPManufacturerID、DPSpecificationID、DPSupplierID、DPInsurerID、DPMaintainerID时
  19. //第二层为厂商id指向厂商id 或 型号id指向厂商id
  20. Map<String, Map> nameMap = new HashMap<>();
  21. String jsonStr = exchange.getIn().getBody(String.class);
  22. JSONObject json = JSONObject.parseObject(jsonStr);
  23. if (json.getString("result").equals("failure")) {
  24. throw new InvalidPostException("请求厂商库出错");
  25. }
  26. JSONObject content = json.getJSONObject("content");
  27. AllVender venders = JSONObject.parseObject(content.toString(), AllVender.class);
  28. buildNameMap(venders,nameMap);
  29. exchange.setProperty("map",nameMap);
  30. exchange.getIn().setBody(exchange.getProperty("tempBody"));
  31. }
  32. public void buildNameMap(AllVender venders, Map<String, Map> nameMap) {
  33. initMap(nameMap);
  34. List<Manu> manuList = venders.getManuList();
  35. List<Supp> suppList = venders.getSuppList();
  36. List<Insure> insureList = venders.getInsureList();
  37. List<Maintn> maintnList = venders.getMaintnList();
  38. handleMaunfacturer(nameMap,manuList);
  39. handleSupplier(nameMap,suppList);
  40. handleInsurer(nameMap,insureList);
  41. handleMtn(nameMap,maintnList);
  42. }
  43. public void initMap(Map<String, Map> nameMap) {
  44. String[] keyList = new String[]{"Manufacturer", "Brand", "Specification", "Supplier", "Insurer", "InsuranceNum", "Maintainer",
  45. "DPManufacturerID", "DPSpecificationID", "DPSupplierID", "DPInsurerID", "DPMaintainerID"};
  46. for (String key : keyList) {
  47. nameMap.put(key, new HashMap<String, String>());
  48. }
  49. }
  50. public void handleMtn(Map<String, Map> nameMap, List<Maintn> maintnList){
  51. Set<String> visitedMaintainer = new HashSet<>();
  52. Set<String> conflictMaintainer = new HashSet<>();
  53. Map<String, String> maintainer = nameMap.get("Maintainer");
  54. Map<String, String> dpMaintainerID = nameMap.get("DPMaintainerID");
  55. for(Maintn m : maintnList){
  56. fillMap(maintainer,m.getName(),m.getVenderId(),visitedMaintainer,conflictMaintainer);
  57. dpMaintainerID.put(m.getVenderId(),m.getVenderId());
  58. }
  59. }
  60. public void handleInsurer(Map<String, Map> nameMap, List<Insure> insureList){
  61. Date now = new Date();
  62. Set<String> visitedInsurer = new HashSet<>();
  63. Set<String> conflictInsurer = new HashSet<>();
  64. Map<String, String> insurer = nameMap.get("Insurer");
  65. Map<String, String> dpInsurerID = nameMap.get("DPInsurerID");
  66. Map<String, String> insuranceNum = nameMap.get("InsuranceNum");
  67. for(Insure i : insureList ){
  68. fillMap(insurer,i.getName(),i.getVenderId(),visitedInsurer,conflictInsurer);
  69. dpInsurerID.put(i.getVenderId(),i.getVenderId());
  70. for(Contract c : i.getContract()){
  71. if(c.underWarranty(now)){
  72. insuranceNum.put(c.getInsuranceNo(),i.getVenderId());
  73. }
  74. }
  75. }
  76. }
  77. public void handleSupplier(Map<String, Map> nameMap, List<Supp> suppList){
  78. Set<String> visitedSupplier = new HashSet<>();
  79. Set<String> conflictSupplier = new HashSet<>();
  80. Map<String, String> supplier = nameMap.get("Supplier");
  81. Map<String, String> dpSupplierID = nameMap.get("DPSupplierID");
  82. for(Supp s : suppList){
  83. fillMap(supplier,s.getName(),s.getVenderId(),visitedSupplier,conflictSupplier);
  84. dpSupplierID.put(s.getVenderId(),s.getVenderId());
  85. }
  86. }
  87. public void handleMaunfacturer(Map<String, Map> nameMap, List<Manu> manuList) {
  88. Set<String> visitedManufacturer = new HashSet<>();
  89. Set<String> conflictManufacturer = new HashSet<>();
  90. Set<String> visitedBrand = new HashSet<>();
  91. Set<String> conflictBrand = new HashSet<>();
  92. Set<String> visitedSpecification = new HashSet<>();
  93. Set<String> conflictSpecification = new HashSet<>();
  94. Map<String, String> manufacturer = nameMap.get("Manufacturer");
  95. Map<String, String> brand = nameMap.get("Brand");
  96. Map<String, String> specification = nameMap.get("Specification");
  97. Map<String, String> dpManufacturerID = nameMap.get("DPManufacturerID");
  98. Map<String, String> dpSpecificationID = nameMap.get("DPSpecificationID");
  99. for (Manu m : manuList) {
  100. String venderId = m.getVenderId();
  101. fillMap(manufacturer,m.getName(),venderId,visitedManufacturer,conflictManufacturer);
  102. dpManufacturerID.put(venderId, venderId);
  103. for (String specId : m.getSpecId()) {
  104. dpSpecificationID.put(specId,venderId);
  105. }
  106. for(String specName:m.getSpecName()){
  107. fillMap(specification,specName,venderId,visitedSpecification,conflictSpecification);
  108. }
  109. for(String brandName : m.getBrandName()){
  110. fillMap(brand,brandName,venderId,visitedBrand,conflictBrand);
  111. }
  112. }
  113. }
  114. private void fillMap(Map map, String key, String value, Set<String> visited, Set<String> conflict) {
  115. visited.add(key);
  116. if (conflict.contains(key)) {//如果之前产生过冲突,不再添加
  117. return;
  118. } else if (!map.containsKey(key)) {
  119. map.put(key, value);
  120. } else {
  121. if (!map.get(key).equals(value)) {
  122. conflict.add(key);
  123. map.remove(key);
  124. }
  125. }
  126. }
  127. }