Route.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. package com.sagacloud.route;
  2. import com.alibaba.fastjson.JSON;
  3. import com.alibaba.fastjson.JSONArray;
  4. import com.alibaba.fastjson.JSONObject;
  5. import com.github.rjeschke.txtmark.Processor;
  6. import com.sagacloud.Exceptions.InvalidPostException;
  7. import com.sagacloud.cache.ProPasCache;
  8. import com.sagacloud.pojos.DPSelectPropertyResult;
  9. import com.sagacloud.route.processors.GetEquipmentByPjProcessor;
  10. import com.sagacloud.route.processors.GetNameByProjectIds;
  11. import com.sagacloud.route.processors.Insurance.*;
  12. import com.sagacloud.route.processors.GetPropByPjProcessor;
  13. import com.sagacloud.route.processors.Maintainance.FilterPropertyProcessor;
  14. import com.sagacloud.route.processors.Maintainance.QueryPropertyProcesspr;
  15. import com.sagacloud.route.processors.Manufacturer.MaunfacturerFilter;
  16. import com.sagacloud.route.processors.Manufacturer.GetVenderBySpecProcessor;
  17. import com.sagacloud.route.processors.PeriodTask.OnTimeTaskProcessor;
  18. import com.sagacloud.route.processors.PeriodTask.StatisticsProcessor;
  19. import com.sagacloud.route.processors.VenderMapProcessor;
  20. import com.sagacloud.route.processors.supplier.SupplierFilter;
  21. import com.sagacloud.utils.Const;
  22. import org.apache.camel.Exchange;
  23. import org.apache.commons.io.IOUtils;
  24. import org.slf4j.Logger;
  25. import org.slf4j.LoggerFactory;
  26. import javax.ws.rs.core.MediaType;
  27. import java.io.StringWriter;
  28. import java.lang.reflect.ParameterizedType;
  29. import java.util.ArrayList;
  30. import java.util.HashSet;
  31. import java.util.List;
  32. import java.util.Set;
  33. /**
  34. * Created by Xiaoyu on 2018/7/10
  35. */
  36. public class Route extends ExceptionHandlerBaseRoute {
  37. private static final Logger log = LoggerFactory.getLogger(Route.class);
  38. @Override
  39. public void configure() throws Exception {
  40. configExceptionHandler();
  41. fromF("timer://simpleTimer?period=1m").process(new OnTimeTaskProcessor()).to("direct:syncAllProject");
  42. fromF("timer://simpleTimer?period=30m").to("direct:synProjectName");
  43. rest().get("/doc")
  44. .produces("text/html;charset=UTF-8")
  45. .route()
  46. .process(msg -> {
  47. String docContentStr = Processor.process(Route.class.getClassLoader().getResourceAsStream("documentation.md"));
  48. StringWriter writer = new StringWriter();
  49. IOUtils.copy(Route.class.getClassLoader().getResourceAsStream("template.html"), writer, "utf-8");
  50. String tmplateContentStr = writer.toString();
  51. msg.getOut().setBody(tmplateContentStr.replace("#replaceMePlease#", docContentStr));
  52. });
  53. // 生产商查询项目下的资产
  54. rest("/manufacturer/property/").post("query")
  55. .consumes(MediaType.APPLICATION_JSON)
  56. .produces(MediaType.APPLICATION_JSON)
  57. .route()
  58. .process(exchange -> {
  59. String jsonStr = exchange.getIn().getBody(String.class);
  60. JSONObject json = JSONObject.parseObject(jsonStr);
  61. exchange.setProperty("venderId", json.getString("venderId"));
  62. exchange.getIn().setBody(jsonStr);
  63. })
  64. .to("direct:getPropertiesByPj")
  65. .process(new GetVenderBySpecProcessor())
  66. .to(Const.venders + "/manufacturer/specification/query_vender")
  67. .process(new MaunfacturerFilter());
  68. // 供应商查询项目下的资产
  69. rest("/supplier/property/").post("query")
  70. .consumes(MediaType.APPLICATION_JSON)
  71. .produces(MediaType.APPLICATION_JSON)
  72. .route()
  73. .process(exchange -> {
  74. String jsonStr = exchange.getIn().getBody(String.class);
  75. JSONObject json = JSONObject.parseObject(jsonStr);
  76. exchange.setProperty("venderId", json.getString("venderId"));
  77. exchange.getIn().setBody(jsonStr);
  78. })
  79. .to("direct:getPropertiesByPj")
  80. .process(new GetVenderBySpecProcessor())
  81. .to(Const.venders + "/manufacturer/specification/query_vender")
  82. .process(new SupplierFilter());
  83. // 获取在保资产清单
  84. rest("/insurance/project/").post("query")
  85. .consumes(MediaType.APPLICATION_JSON)
  86. .produces(MediaType.APPLICATION_JSON)
  87. .route()
  88. .process(new GetInsuranceNoListProcessor())
  89. .to(String.join("", InitEnvRoute.venders, "/insurance/contract/query"))
  90. .process(new HandleWarrantyFromVenderProcessor())
  91. .to("direct:getPropertiesByPj")
  92. //.to(String.join("", InitEnvRoute.dataPlatform, "/property/relation_query"))
  93. .process(new GetPropertyUnderWarrantyProcessor());
  94. // 获取所有保单列表
  95. rest("/insurance/contract/").post("query")
  96. .consumes(MediaType.APPLICATION_JSON)
  97. .produces(MediaType.APPLICATION_JSON)
  98. .route()
  99. .process(new GetInsuranceNoListProcessor())
  100. .to(String.join("", InitEnvRoute.venders, "/insurance/contract/query"))
  101. .process(new HandleWarrantyFromVenderProcessor())
  102. .to("direct:getPropertiesByPj")
  103. .process(new GetPropertyCountInAllWarrantyProcessor());
  104. // 根据保单获取资产
  105. rest("/insurance/contract/property/").post("query")
  106. .consumes(MediaType.APPLICATION_JSON)
  107. .produces(MediaType.APPLICATION_JSON)
  108. .route()
  109. .process(new StoreInsuranceNoProcessor())
  110. .to("direct:getPropertiesByPj")
  111. .process(new GetPropertyInCertainWarrantyProcessor());
  112. // 查询指定项目内合同有效期内的资产/查询指定项目内历史维护资产
  113. rest("/maintainance/property/").post("query")
  114. .consumes(MediaType.APPLICATION_JSON)
  115. .produces(MediaType.APPLICATION_JSON)
  116. .route()
  117. .process(new QueryPropertyProcesspr())
  118. .to("direct:getEquipmentByPj")
  119. .process(new FilterPropertyProcessor());
  120. rest("/project/sync").post()
  121. .consumes(MediaType.APPLICATION_JSON)
  122. .produces(MediaType.APPLICATION_JSON)
  123. .route()
  124. .process(exchange -> {
  125. String jsonStr = exchange.getIn().getBody(String.class);
  126. JSONObject json = JSONObject.parseObject(jsonStr);
  127. JSONArray projectArr = json.getJSONArray("project");
  128. List<String> projectList = projectArr.toJavaList(String.class);
  129. if (projectList == null || projectList.size() == 0) {
  130. throw new InvalidPostException("project不能为空");
  131. }
  132. exchange.setProperty("projectList", projectList);
  133. })
  134. .to("direct:buildVenderMap");
  135. rest("/project/sync_all").post()
  136. .consumes(MediaType.APPLICATION_JSON)
  137. .produces(MediaType.APPLICATION_JSON)
  138. .route()
  139. .to("direct:syncAllProject");
  140. //同步全部项目的资产信息
  141. from("direct:syncAllProject")
  142. .process(exchange -> {
  143. System.out.println("someThing happened");
  144. exchange.setProperty("projectList", ProPasCache.pjList);
  145. log.warn("统计数据开始更新");
  146. })
  147. .to("direct:buildVenderMap");
  148. // 需要输入 body是Pj1101080001, getProperty("propertyData") 是 DPSelectPropertyResult类型,
  149. // getProperty("map") 是 Map<String, Map<String, String>>类型 说明: 信息点Name --> (厂商库该信息点的值 --> 所属厂商)
  150. from("direct:getPropertyStatisticsByProject").process(new StatisticsProcessor())
  151. .to(String.join("", InitEnvRoute.venders, "/auxiliary/property/create_by_project"));
  152. //body为JSONObject,含有projectId字段
  153. from("direct:getPropertiesByPj")
  154. .process(new GetPropByPjProcessor()).setHeader(Exchange.HTTP_METHOD, constant("POST"))
  155. .to(InitEnvRoute.dataPlatform + "/property/relation_query?bridgeEndpoint=true&method=POST");
  156. from("direct:getEquipmentByPj")
  157. .process(new GetEquipmentByPjProcessor())
  158. .to(Const.dataPlatform + "/object/subset_query");
  159. // 统计数据平台资产和设备信息(数量)
  160. from("direct:buildVenderMap").setHeader(Exchange.HTTP_METHOD, constant("POST"))
  161. .to(Const.venders + "/auxiliary/vender/query_detail?bridgeEndpoint=true&method=POST")
  162. .process(new VenderMapProcessor())
  163. .split(body())
  164. //.parallelProcessing()
  165. .process(exchange -> {
  166. String projectId = exchange.getIn().getBody(String.class);
  167. System.out.println("统计:" + projectId);
  168. JSONObject json = new JSONObject();
  169. json.put("projectId", projectId);
  170. exchange.setProperty("projectId" + exchange.getExchangeId(), projectId);
  171. exchange.getIn().setBody(json.toString());
  172. })
  173. .to("direct:getPropertiesByPj")
  174. .process(exchange -> {
  175. String jsonStr = exchange.getIn().getBody(String.class);
  176. DPSelectPropertyResult dpResult = JSONObject.parseObject(jsonStr, DPSelectPropertyResult.class);
  177. exchange.setProperty("propertyData" + exchange.getExchangeId(), dpResult);
  178. // 为下个路由准备输入数据
  179. String projectId = (String) exchange.getProperty("projectId" + exchange.getExchangeId());
  180. JSONObject json = new JSONObject();
  181. json.put("projectId", projectId);
  182. exchange.getIn().setBody(json.toString());
  183. })
  184. .to("direct:getEquipmentByPj")
  185. .process(exchange -> {
  186. String jsonStr = exchange.getIn().getBody(String.class);
  187. DPSelectPropertyResult eqResult = JSONObject.parseObject(jsonStr, DPSelectPropertyResult.class);
  188. exchange.setProperty("facilityData" + exchange.getExchangeId(), eqResult);
  189. exchange.getIn().setBody(exchange.getProperty("projectId" + exchange.getExchangeId()));
  190. })
  191. .to("direct:getPropertyStatisticsByProject");
  192. // 主动同步数据平台项目名接口
  193. rest("/project/").post("sync_name")
  194. .consumes(MediaType.APPLICATION_JSON)
  195. .produces(MediaType.APPLICATION_JSON)
  196. .route()
  197. .to("direct:synProjectName");
  198. // 根据项目id获取项目的名称.
  199. rest("/project/names/").post("query")
  200. .consumes(MediaType.APPLICATION_JSON)
  201. .produces(MediaType.APPLICATION_JSON)
  202. .route()
  203. .process(exchange -> {
  204. try {
  205. String body = exchange.getIn().getBody(String.class);
  206. JSONObject obj = JSONObject.parseObject(body);
  207. if(obj != null && obj.getJSONArray("project") != null && obj.getJSONArray("project").size() > 0)
  208. {
  209. // List<String> projectIds = JSONArray.parseArray(obj.getJSONArray("project").toJSONString(), String.class);
  210. List<String> projectIds = obj.getJSONArray("project").toJavaList(String.class);
  211. exchange.setProperty("projectIds", new HashSet<String>(projectIds));
  212. }else
  213. throw new InvalidPostException("输入格式错误");
  214. }catch (Exception ex){
  215. throw new InvalidPostException("输入格式错误");
  216. }
  217. exchange.getIn().setHeader(Exchange.HTTP_URI, String.join("", InitEnvRoute.dataPlatform, "/mng/project/query?bridgeEndpoint=true&secret=A123456"));
  218. }).setHeader(Exchange.HTTP_METHOD, constant("GET"))
  219. .to(InitEnvRoute.dataPlatform + "/mng/project/query?bridgeEndpoint=true&secret=A123456")
  220. .process(new GetNameByProjectIds());
  221. from("direct:synProjectName")
  222. .process(exchange -> {
  223. log.info("开始同步项目名");
  224. JSONArray criterias = new JSONArray();
  225. for (String pjId : ProPasCache.nameMap.keySet()) {
  226. JSONObject obj = new JSONObject();
  227. obj.put("projectId",pjId);
  228. obj.put("projectLocalName",ProPasCache.nameMap.get(pjId));
  229. criterias.add(obj);
  230. }
  231. JSONObject inserJson = new JSONObject();
  232. inserJson.put("criterias",criterias);
  233. log.info("数据:"+inserJson.toString());
  234. exchange.getIn().setBody(inserJson.toString());
  235. exchange.getIn().setHeader(Exchange.HTTP_URI, InitEnvRoute.venders+"/auxiliary/project/create?method=POST");
  236. log.info(InitEnvRoute.venders+"/auxiliary/project/create?method=POST");
  237. })
  238. .to(InitEnvRoute.venders+"/auxiliary/project/create");
  239. }
  240. }