RecommendProcessor.java 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. package com.sagacloud.route.processors;
  2. /*
  3. * Author: Jxing
  4. * Create Time: 2018/11/13
  5. */
  6. import com.alibaba.fastjson.JSONArray;
  7. import com.alibaba.fastjson.JSONObject;
  8. import com.sagacloud.Exceptions.InvalidPostException;
  9. import com.sagacloud.pojos.RecommendObject;
  10. import com.sagacloud.utils.ExternalUrlUtil;
  11. import com.sagacloud.utils.HttpRequests;
  12. import com.sagacloud.utils.VendersUtil;
  13. import org.apache.camel.Exchange;
  14. import java.math.BigDecimal;
  15. import java.util.*;
  16. public class RecommendProcessor {
  17. static Map<String, String> dpVenderNameInfoCodeMap = new HashMap<>();
  18. static double threshold = 0.25d;
  19. static ArrayList<String> uselessWords = VendersUtil.createArrayList("集团", "有限", "公司", "科技", "技术", "信息");
  20. static {
  21. dpVenderNameInfoCodeMap.put("supplier", "Supplier");
  22. dpVenderNameInfoCodeMap.put("insurer", "Insurer");
  23. dpVenderNameInfoCodeMap.put("maintainer", "Maintainer");
  24. }
  25. public static void recommend(Exchange exchange) throws Exception {
  26. String jsonStr = exchange.getIn().getBody(String.class);
  27. JSONObject limit = null;
  28. String venderType = null;
  29. String venderName = null;
  30. String pjId = null;
  31. try {
  32. JSONObject json = JSONObject.parseObject(jsonStr);
  33. limit = json.getJSONObject("limit");
  34. JSONObject criteria = json.getJSONObject("criteria");
  35. venderType = criteria.getString("venderType");
  36. venderName = criteria.getString("venderName");
  37. pjId = criteria.getString("projectId");
  38. if(venderType == null || !(venderType.equals("supplier") || venderType.equals("insurer") || venderType.equals("maintainer")))
  39. throw new Exception();
  40. if(venderName == null || venderName.length() == 0)
  41. throw new Exception();
  42. if(pjId == null || !pjId.startsWith("Pj"))
  43. throw new Exception();
  44. }catch (Exception ex){
  45. throw new InvalidPostException("输入错误");
  46. }
  47. // map<厂商名, 对象JSONObject>
  48. // 获取有厂商名, 并且没有绑定厂商id的对象
  49. Map<String, JSONObject> map = getAllObjectMap(pjId, venderType);
  50. if(map.size() == 0){
  51. exchange.getOut().setBody(VendersUtil.successJsonMsg(""));
  52. return;
  53. }
  54. ArrayList<JSONObject> arr = recommendObject(map, venderName);
  55. if(arr.size() == 0){
  56. exchange.getOut().setBody(VendersUtil.successJsonMsg(""));
  57. return;
  58. }
  59. List<Object> finalArr = paging(arr, limit);
  60. exchange.getOut().setBody(VendersUtil.returnJSONArray(new JSONArray(finalArr), (long)arr.size()));
  61. }
  62. private static List<Object> paging(ArrayList<JSONObject> arr, JSONObject limit) {
  63. List<Object> retArr = new ArrayList<>();
  64. Long from = 0L;
  65. int count = arr.size();
  66. Long limitSkip = limit == null ? null : limit.getLong("skip");
  67. Integer limitCount = limit == null ? null : limit.getInteger("count");
  68. if (limitSkip != null && limitCount != null) {
  69. from = limitSkip;
  70. count = limitCount;
  71. }
  72. for (long i = from; i < arr.size() && (i - from) < count; ++i) {
  73. retArr.add(arr.get((int)i));
  74. }
  75. return retArr;
  76. }
  77. private static ArrayList<JSONObject> recommendObject(Map<String, JSONObject> map, String venderName) {
  78. venderName = preprocessing(venderName);
  79. PriorityQueue<RecommendObject> queue = new PriorityQueue<>();
  80. for(String name : map.keySet()){
  81. JSONObject single = map.get(name);
  82. if(venderName.contains(name) || name.contains(venderName)){
  83. RecommendObject obj = new RecommendObject(0.99d, single);
  84. queue.add(obj);
  85. }else {
  86. double similarity = VendersUtil.levenshtein(venderName, name);
  87. if(similarity > threshold){
  88. RecommendObject obj = new RecommendObject(similarity, single);
  89. queue.add(obj);
  90. }
  91. }
  92. }
  93. ArrayList<JSONObject> arrayList = new ArrayList<>();
  94. while(!queue.isEmpty()){
  95. arrayList.add(queue.poll().getObj());
  96. }
  97. return arrayList;
  98. }
  99. private static String preprocessing(String venderName) {
  100. for(int i = 0; i < uselessWords.size(); ++i){
  101. if(venderName.contains(uselessWords.get(i)))
  102. {
  103. venderName = venderName.replace(uselessWords.get(i), "");
  104. }
  105. }
  106. return venderName;
  107. }
  108. private static Map<String, JSONObject> getAllObjectMap(String pjId, String venderType) throws Exception{
  109. String type = venderType.equals("maintainer") ? "Eq" : "Pe";
  110. String venderInfoCode = null;
  111. if(venderType.equals("supplier"))
  112. venderInfoCode = "DPSupplierID";
  113. else if(venderType.equals("insurer"))
  114. venderInfoCode = "DPInsurerID";
  115. else
  116. venderInfoCode = "DPMaintainerID";
  117. JSONObject requestBody = new JSONObject();
  118. JSONObject criteria = new JSONObject();
  119. JSONArray typeArr = new JSONArray();
  120. JSONArray valueArr = new JSONArray();
  121. valueArr.add(null);
  122. typeArr.add(type);
  123. requestBody.put("criteria", criteria);
  124. criteria.put("type", typeArr);
  125. criteria.put(venderInfoCode, valueArr);
  126. System.out.println(requestBody.toJSONString());
  127. String result = HttpRequests.sendPost(ExternalUrlUtil.getInQueryUrl(pjId), requestBody.toString());
  128. if(result.equals("")){
  129. throw new Exception("数据平台访问异常");
  130. }
  131. JSONObject resultObj = JSONObject.parseObject(result);
  132. if(!"success".equals(resultObj.getString("Result")))
  133. throw new Exception("数据平台访问出错");
  134. JSONArray content = resultObj.getJSONArray("Content");
  135. Map<String, JSONObject> nameMap = new HashMap<>();
  136. String infoCode = dpVenderNameInfoCodeMap.get(venderType);
  137. for(int i = 0; i < content.size(); ++i){
  138. JSONObject single = content.getJSONObject(i);
  139. if(single == null)
  140. continue;
  141. JSONObject infos = single.getJSONObject("infos");
  142. if(infos == null)
  143. continue;
  144. String nameValue = infos.getString(infoCode);
  145. if(nameValue == null || nameValue.length() == 0)
  146. continue;
  147. if(!nameMap.containsKey(nameValue))
  148. nameMap.put(nameValue, single);
  149. }
  150. return nameMap;
  151. }
  152. }