123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175 |
- package com.sagacloud.route.processors;
- /*
- * Author: Jxing
- * Create Time: 2018/11/13
- */
- import com.alibaba.fastjson.JSONArray;
- import com.alibaba.fastjson.JSONObject;
- import com.sagacloud.Exceptions.InvalidPostException;
- import com.sagacloud.pojos.RecommendObject;
- import com.sagacloud.utils.ExternalUrlUtil;
- import com.sagacloud.utils.HttpRequests;
- import com.sagacloud.utils.VendersUtil;
- import org.apache.camel.Exchange;
- import java.math.BigDecimal;
- import java.util.*;
- public class RecommendProcessor {
- static Map<String, String> dpVenderNameInfoCodeMap = new HashMap<>();
- static double threshold = 0.25d;
- static ArrayList<String> uselessWords = VendersUtil.createArrayList("集团", "有限", "公司", "科技", "技术", "信息");
- static {
- dpVenderNameInfoCodeMap.put("supplier", "Supplier");
- dpVenderNameInfoCodeMap.put("insurer", "Insurer");
- dpVenderNameInfoCodeMap.put("maintainer", "Maintainer");
- }
- public static void recommend(Exchange exchange) throws Exception {
- String jsonStr = exchange.getIn().getBody(String.class);
- JSONObject limit = null;
- String venderType = null;
- String venderName = null;
- String pjId = null;
- try {
- JSONObject json = JSONObject.parseObject(jsonStr);
- limit = json.getJSONObject("limit");
- JSONObject criteria = json.getJSONObject("criteria");
- venderType = criteria.getString("venderType");
- venderName = criteria.getString("venderName");
- pjId = criteria.getString("projectId");
- if(venderType == null || !(venderType.equals("supplier") || venderType.equals("insurer") || venderType.equals("maintainer")))
- throw new Exception();
- if(venderName == null || venderName.length() == 0)
- throw new Exception();
- if(pjId == null || !pjId.startsWith("Pj"))
- throw new Exception();
- }catch (Exception ex){
- throw new InvalidPostException("输入错误");
- }
- // map<厂商名, 对象JSONObject>
- // 获取有厂商名, 并且没有绑定厂商id的对象
- Map<String, JSONObject> map = getAllObjectMap(pjId, venderType);
- if(map.size() == 0){
- exchange.getOut().setBody(VendersUtil.successJsonMsg(""));
- return;
- }
- ArrayList<JSONObject> arr = recommendObject(map, venderName);
- if(arr.size() == 0){
- exchange.getOut().setBody(VendersUtil.successJsonMsg(""));
- return;
- }
- List<Object> finalArr = paging(arr, limit);
- exchange.getOut().setBody(VendersUtil.returnJSONArray(new JSONArray(finalArr), (long)arr.size()));
- }
- public static List<Object> paging(ArrayList<JSONObject> arr, JSONObject limit) {
- List<Object> retArr = new ArrayList<>();
- Long from = 0L;
- int count = arr.size();
- Long limitSkip = limit == null ? null : limit.getLong("skip");
- Integer limitCount = limit == null ? null : limit.getInteger("count");
- if (limitSkip != null && limitCount != null) {
- from = limitSkip;
- count = limitCount;
- }
- for (long i = from; i < arr.size() && (i - from) < count; ++i) {
- retArr.add(arr.get((int)i));
- }
- return retArr;
- }
- public static List<Object> paging(JSONArray arr, JSONObject limit) {
- List<Object> retArr = new ArrayList<>();
- Long from = 0L;
- int count = arr.size();
- Long limitSkip = limit == null ? null : limit.getLong("skip");
- Integer limitCount = limit == null ? null : limit.getInteger("count");
- if (limitSkip != null && limitCount != null) {
- from = limitSkip;
- count = limitCount;
- }
- for (long i = from; i < arr.size() && (i - from) < count; ++i) {
- retArr.add(arr.get((int)i));
- }
- return retArr;
- }
- private static ArrayList<JSONObject> recommendObject(Map<String, JSONObject> map, String venderName) {
- venderName = preprocessing(venderName);
- PriorityQueue<RecommendObject> queue = new PriorityQueue<>();
- for(String name : map.keySet()){
- JSONObject single = map.get(name);
- if(venderName.contains(name) || name.contains(venderName)){
- RecommendObject obj = new RecommendObject(0.99d, single);
- queue.add(obj);
- }else {
- double similarity = VendersUtil.levenshtein(venderName, name);
- if(similarity > threshold){
- RecommendObject obj = new RecommendObject(similarity, single);
- queue.add(obj);
- }
- }
- }
- ArrayList<JSONObject> arrayList = new ArrayList<>();
- while(!queue.isEmpty()){
- arrayList.add(queue.poll().getObj());
- }
- return arrayList;
- }
- private static String preprocessing(String venderName) {
- for(int i = 0; i < uselessWords.size(); ++i){
- if(venderName.contains(uselessWords.get(i)))
- {
- venderName = venderName.replace(uselessWords.get(i), "");
- }
- }
- return venderName;
- }
- private static Map<String, JSONObject> getAllObjectMap(String pjId, String venderType) throws Exception{
- String type = venderType.equals("maintainer") ? "Eq" : "Pe";
- String venderInfoCode = null;
- if(venderType.equals("supplier"))
- venderInfoCode = "DPSupplierID";
- else if(venderType.equals("insurer"))
- venderInfoCode = "DPInsurerID";
- else
- venderInfoCode = "DPMaintainerID";
- JSONObject requestBody = new JSONObject();
- JSONObject criteria = new JSONObject();
- JSONArray typeArr = new JSONArray();
- JSONArray valueArr = new JSONArray();
- valueArr.add(null);
- typeArr.add(type);
- requestBody.put("criteria", criteria);
- criteria.put("type", typeArr);
- criteria.put(venderInfoCode, valueArr);
- System.out.println(requestBody.toJSONString());
- String result = HttpRequests.sendPost(ExternalUrlUtil.getInQueryUrl(pjId), requestBody.toString());
- if(result.equals("")){
- throw new Exception("数据平台访问异常");
- }
- JSONObject resultObj = JSONObject.parseObject(result);
- if(!"success".equals(resultObj.getString("Result")))
- throw new Exception("数据平台访问出错");
- JSONArray content = resultObj.getJSONArray("Content");
- Map<String, JSONObject> nameMap = new HashMap<>();
- String infoCode = dpVenderNameInfoCodeMap.get(venderType);
- for(int i = 0; i < content.size(); ++i){
- JSONObject single = content.getJSONObject(i);
- if(single == null)
- continue;
- JSONObject infos = single.getJSONObject("infos");
- if(infos == null)
- continue;
- String nameValue = infos.getString(infoCode);
- if(nameValue == null || nameValue.length() == 0)
- continue;
- if(!nameMap.containsKey(nameValue))
- nameMap.put(nameValue, single);
- }
- return nameMap;
- }
- }
|