|
@@ -10,10 +10,10 @@ import com.sagacloud.Exceptions.InvalidPostException;
|
|
|
import com.sagacloud.pojos.RecommendObject;
|
|
|
import com.sagacloud.utils.ExternalUrlUtil;
|
|
|
import com.sagacloud.utils.HttpRequests;
|
|
|
+import com.sagacloud.utils.PropUtil;
|
|
|
import com.sagacloud.utils.VendersUtil;
|
|
|
import org.apache.camel.Exchange;
|
|
|
|
|
|
-import java.math.BigDecimal;
|
|
|
import java.util.*;
|
|
|
|
|
|
public class RecommendProcessor {
|
|
@@ -30,18 +30,24 @@ public class RecommendProcessor {
|
|
|
JSONObject limit = null;
|
|
|
String venderType = null;
|
|
|
String venderName = null;
|
|
|
+ String venderId = null;
|
|
|
String pjId = null;
|
|
|
+ JSONObject filter = null;
|
|
|
try {
|
|
|
JSONObject json = JSONObject.parseObject(jsonStr);
|
|
|
limit = json.getJSONObject("limit");
|
|
|
+ filter = json.getJSONObject("filter");
|
|
|
JSONObject criteria = json.getJSONObject("criteria");
|
|
|
venderType = criteria.getString("venderType");
|
|
|
+ venderId = criteria.getString("venderId");
|
|
|
venderName = criteria.getString("venderName");
|
|
|
- pjId = criteria.getString("projectId");
|
|
|
+ pjId = (String) exchange.getIn().getHeaders().get("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(venderId == null || venderId.length() == 0)
|
|
|
+ throw new Exception();
|
|
|
if(pjId == null || !pjId.startsWith("Pj"))
|
|
|
throw new Exception();
|
|
|
}catch (Exception ex){
|
|
@@ -49,21 +55,101 @@ public class RecommendProcessor {
|
|
|
}
|
|
|
// map<厂商名, 对象JSONObject>
|
|
|
// 获取有厂商名, 并且没有绑定厂商id的对象
|
|
|
+ // key --> id, value 对象Object
|
|
|
Map<String, JSONObject> map = getAllObjectMap(pjId, venderType);
|
|
|
if(map.size() == 0){
|
|
|
exchange.getOut().setBody(VendersUtil.successJsonMsg(""));
|
|
|
return;
|
|
|
}
|
|
|
- ArrayList<JSONObject> arr = recommendObject(map, venderName);
|
|
|
+ // 获取该厂商的维护名录, venders/product/directory/query
|
|
|
+ JSONObject bodyObj = new JSONObject();
|
|
|
+ bodyObj.put("venderId", venderId);
|
|
|
+ String result = HttpRequests.sendPost(ExternalUrlUtil.getVenderSpecDirectoryUrl(), bodyObj.toString());
|
|
|
+ Set<String> brandNameSet = new HashSet<>();
|
|
|
+ Set<String> specIdSet = new HashSet<>();
|
|
|
+ Set<String> specNameSet = new HashSet<>();
|
|
|
+ Set<String> familySet = new HashSet<>();
|
|
|
+ // 填充厂商维护型号清单的各个Map
|
|
|
+ composeVenderSpecDirMap(result, brandNameSet, specIdSet, specNameSet, familySet);
|
|
|
+ // 根据厂商维护的产品目录来推荐资产
|
|
|
+ Map<String, JSONObject> recommendMap = recommendObjectByVenderSpecDir(map, brandNameSet, specIdSet, specNameSet, familySet);
|
|
|
+ for(String id : recommendMap.keySet()){
|
|
|
+ map.remove(id);
|
|
|
+ }
|
|
|
+ // 根据维护名录中的品牌, 产品, 型号[id|name]匹配
|
|
|
+ ArrayList<JSONObject> arr = recommendObject(map, venderName, venderType);
|
|
|
+ if(recommendMap.size() > 0)
|
|
|
+ arr.addAll(0, recommendMap.values());
|
|
|
if(arr.size() == 0){
|
|
|
- exchange.getOut().setBody(VendersUtil.successJsonMsg(""));
|
|
|
+ exchange.getOut().setBody(VendersUtil.successJsonMsg("", new JSONArray()));
|
|
|
return;
|
|
|
}
|
|
|
- List<Object> finalArr = paging(arr, limit);
|
|
|
+ List<Object> finalArr = paging(arr, limit, filter);
|
|
|
exchange.getOut().setBody(VendersUtil.returnJSONArray(new JSONArray(finalArr), (long)arr.size()));
|
|
|
}
|
|
|
|
|
|
- public static List<Object> paging(ArrayList<JSONObject> arr, JSONObject limit) {
|
|
|
+ private static Map<String, JSONObject> recommendObjectByVenderSpecDir(Map<String, JSONObject> map, Set<String> brandNameSet, Set<String> specIdSet, Set<String> specNameSet, Set<String> familySet) {
|
|
|
+ Map<String, JSONObject> recommendMap = new HashMap<>();
|
|
|
+ if(brandNameSet.size() == 0 && specIdSet.size() == 0 && specNameSet.size() == 0)
|
|
|
+ return recommendMap;
|
|
|
+ for(String id : map.keySet()){
|
|
|
+ JSONObject single = map.get(id);
|
|
|
+ String family = single.getString("family");
|
|
|
+ if(familySet.contains(family)){
|
|
|
+ recommendMap.putIfAbsent(id, single);
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ JSONObject infos = single.getJSONObject("infos");
|
|
|
+ String brandName = infos.getString("Brand") ;
|
|
|
+ String specName = infos.getString("Specification");
|
|
|
+ String specId = infos.getString("DPSpecificationID");
|
|
|
+ if(brandNameSet.contains(brandName == null ? "" : brandName.trim()) || specNameSet.contains(specName == null? "" : specName.trim()) ||
|
|
|
+ specIdSet.contains(specId == null ? "" : specId.trim())){
|
|
|
+ recommendMap.putIfAbsent(id, single);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return recommendMap;
|
|
|
+ }
|
|
|
+
|
|
|
+ private static void composeVenderSpecDirMap(String result, Set<String> brandNameSet, Set<String> specIdSet, Set<String> specNameSet, Set<String> familySet) {
|
|
|
+ JSONObject resultObj = JSONObject.parseObject(result);
|
|
|
+ if(resultObj == null)
|
|
|
+ return;
|
|
|
+ if(!"success".equals(resultObj.getString("result"))){
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ JSONArray content = resultObj.getJSONArray("content");
|
|
|
+// {
|
|
|
+// "specificationId":"",
|
|
|
+// "specificationName":"",
|
|
|
+// "venderId":"",
|
|
|
+// "venderName":"",
|
|
|
+// "brandId":"",
|
|
|
+// "brandName":"",
|
|
|
+// "productId":"",
|
|
|
+// "productName":"",
|
|
|
+// "eqFamily":"ABC"
|
|
|
+// }
|
|
|
+ for(int i = 0; i < content.size(); ++i){
|
|
|
+ JSONObject single = content.getJSONObject(i);
|
|
|
+ String specName = single.getString("specificationName");
|
|
|
+ String specid = single.getString("specificationId");
|
|
|
+ String brandName = single.getString("brandName");
|
|
|
+ String family = single.getString("eqFamily");
|
|
|
+ if(specName != null && specName.trim().length() != 0 && !specNameSet.contains(specName.trim()))
|
|
|
+ specNameSet.add(specName.trim());
|
|
|
+ if(specid != null && specid.trim().length() != 0 && !specIdSet.contains(specid.trim()))
|
|
|
+ specIdSet.add(specid.trim());
|
|
|
+ if(brandName != null && brandName.trim().length() != 0 && !brandNameSet.contains(brandName.trim()))
|
|
|
+ brandNameSet.add(brandName.trim());
|
|
|
+ if(family != null && family.trim().length() != 0 && !familySet.contains(family.trim()))
|
|
|
+ familySet.add(family.trim());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public static List<Object> paging(ArrayList<JSONObject> arr, JSONObject limit, JSONObject filter) {
|
|
|
+ // 先过滤, 再分页
|
|
|
+ filter(arr, filter);
|
|
|
List<Object> retArr = new ArrayList<>();
|
|
|
Long from = 0L;
|
|
|
int count = arr.size();
|
|
@@ -79,6 +165,42 @@ public class RecommendProcessor {
|
|
|
return retArr;
|
|
|
}
|
|
|
|
|
|
+ private static void filter(ArrayList<JSONObject> arr, JSONObject filter) {
|
|
|
+ if(filter == null || filter.size() != 1)
|
|
|
+ return;
|
|
|
+ String infoCode = null;
|
|
|
+ Object value = null;
|
|
|
+ for(String key : filter.keySet()){
|
|
|
+ infoCode = key;
|
|
|
+ value = filter.get(key);
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ for(int i = arr.size() - 1; i > -1; --i){
|
|
|
+ JSONObject single = arr.get(i);
|
|
|
+ JSONObject infos = single.getJSONObject("infos");
|
|
|
+ Object objValue = infos.get(infoCode);
|
|
|
+ if(value == null){
|
|
|
+ if(objValue == null)
|
|
|
+ continue;
|
|
|
+ arr.remove(i);
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ if(objValue == null)
|
|
|
+ {
|
|
|
+ arr.remove(i);
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ if(value.equals(objValue))
|
|
|
+ continue;
|
|
|
+ if(value instanceof String && objValue instanceof String){
|
|
|
+ if(((String) objValue).contains((String) value))
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ arr.remove(i);
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
public static List<Object> paging(JSONArray arr, JSONObject limit) {
|
|
|
List<Object> retArr = new ArrayList<>();
|
|
|
Long from = 0L;
|
|
@@ -95,11 +217,16 @@ public class RecommendProcessor {
|
|
|
return retArr;
|
|
|
}
|
|
|
|
|
|
- private static ArrayList<JSONObject> recommendObject(Map<String, JSONObject> map, String venderName) {
|
|
|
+ private static ArrayList<JSONObject> recommendObject(Map<String, JSONObject> map, String venderName, String venderType) {
|
|
|
venderName = preprocessing(venderName);
|
|
|
PriorityQueue<RecommendObject> queue = new PriorityQueue<>();
|
|
|
- for(String name : map.keySet()){
|
|
|
- JSONObject single = map.get(name);
|
|
|
+ String infoCode = dpVenderNameInfoCodeMap.get(venderType);
|
|
|
+ for(String id : map.keySet()){
|
|
|
+ JSONObject single = map.get(id);
|
|
|
+ JSONObject infos = single.getJSONObject("infos");
|
|
|
+ String name = infos.getString(infoCode);
|
|
|
+ if(name == null)
|
|
|
+ continue;
|
|
|
if(venderName.contains(name) || name.contains(venderName)){
|
|
|
RecommendObject obj = new RecommendObject(0.99d, single);
|
|
|
queue.add(obj);
|
|
@@ -111,6 +238,19 @@ public class RecommendProcessor {
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
+// 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());
|
|
@@ -161,14 +301,17 @@ public class RecommendProcessor {
|
|
|
JSONObject single = content.getJSONObject(i);
|
|
|
if(single == null)
|
|
|
continue;
|
|
|
+ String id = single.getString("id");
|
|
|
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);
|
|
|
+ if(!nameMap.containsKey(id))
|
|
|
+ nameMap.put(id, single);
|
|
|
+// String nameValue = infos.getString(infoCode);
|
|
|
+// if(nameValue == null || nameValue.length() == 0)
|
|
|
+// continue;
|
|
|
+// if(!nameMap.containsKey(nameValue))
|
|
|
+// nameMap.put(nameValue, single);
|
|
|
}
|
|
|
return nameMap;
|
|
|
}
|