Route.java 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. package com.sagacloud.route;
  2. import com.alibaba.fastjson.JSONArray;
  3. import com.alibaba.fastjson.JSONObject;
  4. import com.github.rjeschke.txtmark.Processor;
  5. import com.sagacloud.Exceptions.InvalidPostException;
  6. import com.sagacloud.cache.ProPasCache;
  7. import com.sagacloud.pojos.DPSelectPropertyResult;
  8. import com.sagacloud.route.processors.Insurance.*;
  9. import com.sagacloud.route.processors.GetPropByPjProcessor;
  10. import com.sagacloud.route.processors.Maintainance.FilterPropertyProcessor;
  11. import com.sagacloud.route.processors.Maintainance.QueryPropertyProcesspr;
  12. import com.sagacloud.route.processors.Manufacturer.MaunfacturerFilter;
  13. import com.sagacloud.route.processors.Manufacturer.GetVenderBySpecProcessor;
  14. import com.sagacloud.route.processors.PeriodTask.StatisticsProcessor;
  15. import com.sagacloud.route.processors.VenderMapProcessor;
  16. import com.sagacloud.route.processors.supplier.SupplierFilter;
  17. import com.sagacloud.utils.Const;
  18. import org.apache.camel.Exchange;
  19. import org.apache.commons.io.IOUtils;
  20. import javax.ws.rs.core.MediaType;
  21. import java.io.StringWriter;
  22. import java.util.ArrayList;
  23. import java.util.List;
  24. /**
  25. * Created by Xiaoyu on 2018/7/10
  26. */
  27. public class Route extends ExceptionHandlerBaseRoute {
  28. @Override
  29. public void configure() throws Exception {
  30. configExceptionHandler();
  31. rest().get("/doc")
  32. .produces("text/html;charset=UTF-8")
  33. .route()
  34. .process(msg -> {
  35. String docContentStr = Processor.process(Route.class.getClassLoader().getResourceAsStream("documentation.md"));
  36. StringWriter writer = new StringWriter();
  37. IOUtils.copy(Route.class.getClassLoader().getResourceAsStream("template.html"), writer, "utf-8");
  38. String tmplateContentStr = writer.toString();
  39. msg.getOut().setBody(tmplateContentStr.replace("#replaceMePlease#", docContentStr));
  40. });
  41. // 生产商查询项目下的资产
  42. rest("/manufacturer/property/").post("query")
  43. .consumes(MediaType.APPLICATION_JSON)
  44. .produces(MediaType.APPLICATION_JSON)
  45. .route()
  46. .process(exchange -> {
  47. String jsonStr = exchange.getIn().getBody(String.class);
  48. JSONObject json = JSONObject.parseObject(jsonStr);
  49. exchange.setProperty("venderId", json.getString("venderId"));
  50. exchange.getIn().setBody(jsonStr);
  51. })
  52. .to("direct:getPropertiesByPj")
  53. .process(new GetVenderBySpecProcessor())
  54. .to(Const.venders + "/manufacturer/specification/query_vender")
  55. .process(new MaunfacturerFilter());
  56. // 供应商查询项目下的资产
  57. rest("/supplier/property/").post("query")
  58. .consumes(MediaType.APPLICATION_JSON)
  59. .produces(MediaType.APPLICATION_JSON)
  60. .route()
  61. .process(exchange -> {
  62. String jsonStr = exchange.getIn().getBody(String.class);
  63. JSONObject json = JSONObject.parseObject(jsonStr);
  64. exchange.setProperty("venderId", json.getString("venderId"));
  65. exchange.getIn().setBody(jsonStr);
  66. })
  67. .to("direct:getPropertiesByPj")
  68. .process(new GetVenderBySpecProcessor())
  69. .to(Const.venders + "/manufacturer/specification/query_vender")
  70. .process(new SupplierFilter());
  71. // 获取在保资产清单
  72. rest("/insurance/project/").post("query")
  73. .consumes(MediaType.APPLICATION_JSON)
  74. .produces(MediaType.APPLICATION_JSON)
  75. .route()
  76. .process(new GetInsuranceNoListProcessor())
  77. .to(String.join("", InitEnvRoute.venders, "/insurance/contract/query"))
  78. .process(new HandleWarrantyFromVenderProcessor())
  79. .to("direct:getPropertiesByPj")
  80. //.to(String.join("", InitEnvRoute.dataPlatform, "/property/relation_query"))
  81. .process(new GetPropertyUnderWarrantyProcessor());
  82. // 获取所有保单列表
  83. rest("/insurance/contract/").post("query")
  84. .consumes(MediaType.APPLICATION_JSON)
  85. .produces(MediaType.APPLICATION_JSON)
  86. .route()
  87. .process(new GetInsuranceNoListProcessor())
  88. .to(String.join("", InitEnvRoute.venders, "/insurance/contract/query"))
  89. .process(new HandleWarrantyFromVenderProcessor())
  90. .to("direct:getPropertiesByPj")
  91. .process(new GetPropertyCountInAllWarrantyProcessor());
  92. // 根据保单获取资产
  93. rest("/insurance/contract/property/").post("query")
  94. .consumes(MediaType.APPLICATION_JSON)
  95. .produces(MediaType.APPLICATION_JSON)
  96. .route()
  97. .process(new StoreInsuranceNoProcessor())
  98. .to("direct:getPropertiesByPj")
  99. .process(new GetPropertyInCertainWarrantyProcessor());
  100. // 查询指定项目内合同有效期内的资产/查询指定项目内历史维护资产
  101. rest("/maintainance/property/").post("query")
  102. .consumes(MediaType.APPLICATION_JSON)
  103. .produces(MediaType.APPLICATION_JSON)
  104. .route()
  105. .process(new QueryPropertyProcesspr())
  106. .to("direct:getPropertiesByPj")
  107. .process(new FilterPropertyProcessor());
  108. rest("/test/").get().produces(MediaType.APPLICATION_JSON).route()
  109. .process(msg -> {
  110. List<String> lst = new ArrayList<>();
  111. for (int i = 0; i < 1000; i++) {
  112. lst.add("hh" + i);
  113. }
  114. msg.getOut().setBody(lst);
  115. }).split(body(), (oldmsg, newmsg) -> {
  116. if (oldmsg == null) {
  117. return newmsg;
  118. }
  119. oldmsg.getIn().setBody(oldmsg.getIn().getBody(String.class) + newmsg.getIn().getBody(String.class));
  120. return oldmsg;
  121. }).parallelProcessing().parallelAggregate().to("direct://test");
  122. rest("/project/sync").post()
  123. .consumes(MediaType.APPLICATION_JSON)
  124. .produces(MediaType.APPLICATION_JSON)
  125. .route()
  126. .process(exchange -> {
  127. String jsonStr = exchange.getIn().getBody(String.class);
  128. JSONObject json = JSONObject.parseObject(jsonStr);
  129. JSONArray projectArr = json.getJSONArray("project");
  130. List<String> projectList = projectArr.toJavaList(String.class);
  131. if (projectList == null || projectList.size() == 0) {
  132. throw new InvalidPostException("project不能为空");
  133. }
  134. exchange.setProperty("projectList", projectList);
  135. })
  136. .to("direct:buildVenderMap");
  137. //同步全部项目的资产信息
  138. from("direct:syncAllProject")
  139. .process(exchange -> {
  140. exchange.setProperty("projectList", ProPasCache.pjList);
  141. })
  142. .to("direct:buildVenderMap");
  143. // 需要输入 body是Pj1101080001, getProperty("propertyData") 是 DPSelectPropertyResult类型,
  144. // getProperty("idMap") 是 Map<String, String>类型, 说明: 所有DPID --> venderId
  145. // getProperty("nameMap") 是 Map<String, Map<String, String>>类型 说明: 信息点Name --> (厂商库该信息点的值 --> 所属厂商)
  146. from("direct:getPropertyStatisticsByProject").process(new StatisticsProcessor())
  147. .to(String.join("", InitEnvRoute.venders, "/auxiliary/property/create_by_project"));
  148. //body为JSONObject,含有projectId字段
  149. from("direct:getPropertiesByPj")
  150. .process(new GetPropByPjProcessor())
  151. .to(Const.dataPlatform + "/property/relation_query");
  152. from("direct:buildVenderMap")
  153. .to(Const.venders + "/auxiliary/vender/query_detail?bridgeEndpoint=true&method=POST")
  154. .process(new VenderMapProcessor())
  155. .split(body()).parallelProcessing()
  156. .process(exchange -> {
  157. String projectId = exchange.getIn().getBody(String.class);
  158. JSONObject json = new JSONObject();
  159. json.put("projectId", projectId);
  160. exchange.setProperty("projectId" + exchange.getExchangeId(), projectId);
  161. exchange.getIn().setBody(json.toString());
  162. })
  163. .to("direct:getPropertiesByPj")
  164. .process(exchange -> {
  165. String jsonStr = exchange.getIn().getBody(String.class);
  166. DPSelectPropertyResult dpResult = JSONObject.parseObject(jsonStr, DPSelectPropertyResult.class);
  167. exchange.setProperty("propertyData" + exchange.getExchangeId(), dpResult);
  168. exchange.getIn().setBody(exchange.getProperty("projectId" + exchange.getExchangeId()));
  169. })
  170. .to("direct:getPropertyStatisticsByProject");
  171. }
  172. }