RecommendProcessor.java 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. public 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. public static List<Object> paging(JSONArray arr, JSONObject limit) {
  78. List<Object> retArr = new ArrayList<>();
  79. Long from = 0L;
  80. int count = arr.size();
  81. Long limitSkip = limit == null ? null : limit.getLong("skip");
  82. Integer limitCount = limit == null ? null : limit.getInteger("count");
  83. if (limitSkip != null && limitCount != null) {
  84. from = limitSkip;
  85. count = limitCount;
  86. }
  87. for (long i = from; i < arr.size() && (i - from) < count; ++i) {
  88. retArr.add(arr.get((int)i));
  89. }
  90. return retArr;
  91. }
  92. private static ArrayList<JSONObject> recommendObject(Map<String, JSONObject> map, String venderName) {
  93. venderName = preprocessing(venderName);
  94. PriorityQueue<RecommendObject> queue = new PriorityQueue<>();
  95. for(String name : map.keySet()){
  96. JSONObject single = map.get(name);
  97. if(venderName.contains(name) || name.contains(venderName)){
  98. RecommendObject obj = new RecommendObject(0.99d, single);
  99. queue.add(obj);
  100. }else {
  101. double similarity = VendersUtil.levenshtein(venderName, name);
  102. if(similarity > threshold){
  103. RecommendObject obj = new RecommendObject(similarity, single);
  104. queue.add(obj);
  105. }
  106. }
  107. }
  108. ArrayList<JSONObject> arrayList = new ArrayList<>();
  109. while(!queue.isEmpty()){
  110. arrayList.add(queue.poll().getObj());
  111. }
  112. return arrayList;
  113. }
  114. private static String preprocessing(String venderName) {
  115. for(int i = 0; i < uselessWords.size(); ++i){
  116. if(venderName.contains(uselessWords.get(i)))
  117. {
  118. venderName = venderName.replace(uselessWords.get(i), "");
  119. }
  120. }
  121. return venderName;
  122. }
  123. private static Map<String, JSONObject> getAllObjectMap(String pjId, String venderType) throws Exception{
  124. String type = venderType.equals("maintainer") ? "Eq" : "Pe";
  125. String venderInfoCode = null;
  126. if(venderType.equals("supplier"))
  127. venderInfoCode = "DPSupplierID";
  128. else if(venderType.equals("insurer"))
  129. venderInfoCode = "DPInsurerID";
  130. else
  131. venderInfoCode = "DPMaintainerID";
  132. JSONObject requestBody = new JSONObject();
  133. JSONObject criteria = new JSONObject();
  134. JSONArray typeArr = new JSONArray();
  135. JSONArray valueArr = new JSONArray();
  136. valueArr.add(null);
  137. typeArr.add(type);
  138. requestBody.put("criteria", criteria);
  139. criteria.put("type", typeArr);
  140. criteria.put(venderInfoCode, valueArr);
  141. System.out.println(requestBody.toJSONString());
  142. String result = HttpRequests.sendPost(ExternalUrlUtil.getInQueryUrl(pjId), requestBody.toString());
  143. if(result.equals("")){
  144. throw new Exception("数据平台访问异常");
  145. }
  146. JSONObject resultObj = JSONObject.parseObject(result);
  147. if(!"success".equals(resultObj.getString("Result")))
  148. throw new Exception("数据平台访问出错");
  149. JSONArray content = resultObj.getJSONArray("Content");
  150. Map<String, JSONObject> nameMap = new HashMap<>();
  151. String infoCode = dpVenderNameInfoCodeMap.get(venderType);
  152. for(int i = 0; i < content.size(); ++i){
  153. JSONObject single = content.getJSONObject(i);
  154. if(single == null)
  155. continue;
  156. JSONObject infos = single.getJSONObject("infos");
  157. if(infos == null)
  158. continue;
  159. String nameValue = infos.getString(infoCode);
  160. if(nameValue == null || nameValue.length() == 0)
  161. continue;
  162. if(!nameMap.containsKey(nameValue))
  163. nameMap.put(nameValue, single);
  164. }
  165. return nameMap;
  166. }
  167. }