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 dpVenderNameInfoCodeMap = new HashMap<>(); static double threshold = 0.25d; static ArrayList 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 map = getAllObjectMap(pjId, venderType); if(map.size() == 0){ exchange.getOut().setBody(VendersUtil.successJsonMsg("")); return; } ArrayList arr = recommendObject(map, venderName); if(arr.size() == 0){ exchange.getOut().setBody(VendersUtil.successJsonMsg("")); return; } List finalArr = paging(arr, limit); exchange.getOut().setBody(VendersUtil.returnJSONArray(new JSONArray(finalArr), (long)arr.size())); } public static List paging(ArrayList arr, JSONObject limit) { List 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 paging(JSONArray arr, JSONObject limit) { List 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 recommendObject(Map map, String venderName) { venderName = preprocessing(venderName); PriorityQueue 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 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 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 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; } }