WMSServiceImpl.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. package com.galaxis.soap.wms;
  2. import com.galaxis.soap.wms.obj.*;
  3. import lombok.SneakyThrows;
  4. import lombok.extern.slf4j.Slf4j;
  5. import org.apache.commons.io.FileUtils;
  6. import org.apache.commons.io.FilenameUtils;
  7. import org.apache.commons.io.IOUtils;
  8. import org.apache.commons.lang3.StringUtils;
  9. import org.springframework.core.io.ClassPathResource;
  10. import javax.jws.WebService;
  11. import javax.xml.namespace.QName;
  12. import javax.xml.ws.Service;
  13. import java.io.File;
  14. import java.net.URL;
  15. import java.nio.charset.StandardCharsets;
  16. import java.util.ArrayList;
  17. import java.util.HashMap;
  18. import java.util.List;
  19. import java.util.Map;
  20. import java.util.concurrent.*;
  21. import java.util.function.BiFunction;
  22. import java.util.function.Function;
  23. @WebService(serviceName = "WMSService", // 与接口中指定的name一致
  24. targetNamespace = "http://service.jztd.com", // 与接口中的命名空间一致,一般是接口的包名倒
  25. endpointInterface = "com.galaxis.soap.wms.WMSService"// 接口地址
  26. )
  27. @Slf4j
  28. public class WMSServiceImpl implements WMSService {
  29. private static final String errorMsg = "当前下传数据中存在多个仓库的业务数据下传失败,请检查数据,一次只能下传一个仓库的业务数据";
  30. private static final ThreadPoolExecutor EXECUTOR = new ThreadPoolExecutor(
  31. 512,
  32. 512,
  33. 1,
  34. TimeUnit.SECONDS,
  35. new LinkedBlockingQueue<>(4096)
  36. );
  37. private static final String API = "/wms/api";
  38. // 山西
  39. private static final String SX = "http://10.100.0.100:9115";
  40. // 重庆
  41. // private static final String CQ = "http://127.0.0.1:8182";
  42. private static final String CQ = "http://10.100.0.100:9105";
  43. // 广西
  44. private static final String GX = "http://10.100.0.100:9101";
  45. // 四川
  46. private static final String SC = "http://10.100.0.100:9106";
  47. // 成都
  48. private static final String CD = "http://10.100.0.100:9106";
  49. // 贵州
  50. private static final String GZ = "http://10.100.0.100:9102";
  51. // 海南
  52. private static final String HN = "http://10.100.0.100:9109";
  53. // 河南
  54. private static final String HEN = "http://10.0.30.225:8110";
  55. // 云南
  56. private static final String YN = "http://10.0.30.220:18999";
  57. // 云南D300
  58. private static final String YND300 = "http://10.0.30.220:28999";
  59. // 云南D900
  60. private static final String YND900 = "http://10.0.30.220:38999";
  61. // D112
  62. private static final String D112 = "http://10.0.30.225:8999";
  63. // 上海 D907 D107
  64. private static final String D907 = "http://10.0.30.225:18999";
  65. // D118河南一心堂康健药业有些公司
  66. private static final String D118 = "http://10.0.30.220:28118";
  67. // 天津 D117
  68. private static final String D117 = "http://10.0.30.218:18999";
  69. private static final String D908 = "http://10.100.0.100:9908";
  70. // 物流中心编码与WebService服务映射
  71. private static final ConcurrentMap<String, String> WMS_MAPPING = new ConcurrentHashMap<String, String>() {{
  72. put("D104", SX); // 山西鸿翔一心堂药业有限公司
  73. put("D103", SC); // 四川鸿翔一心堂药业有限公司
  74. put("D105", CQ); // 重庆鸿翔一心堂药业有限公司
  75. put("D107", D907); // 上海鸿翔一心堂药业有限公司
  76. put("D101", GX); // 广西鸿翔一心堂药业有限公司
  77. put("D110", HEN); // 河南鸿翔一心堂药业有限公司
  78. put("D115", SX); // 一心堂药业(山西)有限公司
  79. put("D102", GZ); // 贵州鸿翔-一心堂药业有限公司
  80. put("D106", CD); // 成都鸿翔一心堂药业有限公司
  81. put("D109", HN); // 海南鸿翔一心堂药业有限公司
  82. put("D301", HN); // 海南一心堂医药有限公司
  83. put("D100", YN); // 一心堂药业集团股份有限公司
  84. put("D300", YND300); // 云南鸿云药业有限公司
  85. put("D108", ""); // 天津鸿翔一心堂药业有限公司
  86. put("D900", YND900); // 一心到家科技公司(电商仓)
  87. put("D504", ""); // 医云医疗产业发展(云南)有限公司
  88. put("D112", D112); // 山西一心堂广生药业连锁有限公司
  89. put("D118", D118); // 河南一心堂康健药业有些公司
  90. put("D117", D117); // 天津一心堂乾昌药业连锁有限公司
  91. put("D908", D908);
  92. }};
  93. private static final ConcurrentMap<String, WMSService> SERVICE_MAPPING = new ConcurrentHashMap<>(WMS_MAPPING.size());
  94. @SneakyThrows
  95. public WMSServiceImpl() {
  96. // final String wsdl = IOUtils.toString(new ClassPathResource("WMSService.xml").getInputStream(), StandardCharsets.UTF_8);
  97. final String wsdl = IOUtils.toString(new ClassPathResource("YN_WMSService.xml.xml").getInputStream(), StandardCharsets.UTF_8);
  98. final String usrHome = System.getProperty("user.home");
  99. for (Map.Entry<String, String> entry : WMS_MAPPING.entrySet()) {
  100. final String bwkey = entry.getKey();
  101. final String host = entry.getValue();
  102. log.info("### {} -> {}", bwkey, host);
  103. if (StringUtils.isBlank(host)) {
  104. continue;
  105. }
  106. // URL url = new URL(host + API + "?wsdl");
  107. String location = host + API;
  108. File file = new File(FilenameUtils.concat(usrHome, bwkey + ".wsdl.xml"));
  109. FileUtils.writeStringToFile(file, StringUtils.replace(wsdl, "#{__location}", location), StandardCharsets.UTF_8);
  110. URL url = file.toURI().toURL();
  111. QName qName = new QName("http://service.jztd.com", "WMSService");
  112. Service service = Service.create(url, qName);
  113. service.setExecutor(EXECUTOR);
  114. WMSService wmsService = service.getPort(WMSService.class);
  115. SERVICE_MAPPING.put(bwkey, wmsService);
  116. }
  117. }
  118. private <P, R> List<R> distribute(final String method, List<P> params, Function<P, String> getBWKEY, BiFunction<WMSService, List<P>, List<R>> execute) {
  119. log.info("method={} | params={}", method, params);
  120. if (params == null) {
  121. params = new ArrayList<>();
  122. }
  123. // 拆分参数
  124. final Map<String, List<P>> paramsMap = new HashMap<>();
  125. for (P param : params) {
  126. paramsMap.computeIfAbsent(getBWKEY.apply(param), bwkey -> new ArrayList<>()).add(param);
  127. }
  128. // 分发数据
  129. Map<String, Future<List<R>>> futureMap = new HashMap<>(paramsMap.size());
  130. for (Map.Entry<String, List<P>> entry : paramsMap.entrySet()) {
  131. final String bwkey = entry.getKey();
  132. final List<P> list = entry.getValue();
  133. final WMSService wmsService = SERVICE_MAPPING.get(bwkey);
  134. if (wmsService == null || list == null || list.isEmpty()) {
  135. log.error("不能分发数据:method={} | bwkey={} | params={}", method, bwkey, list);
  136. continue;
  137. }
  138. Future<List<R>> future = EXECUTOR.submit(() -> {
  139. log.info("分发数据:method={} | bwkey={} | params={}", method, bwkey, list);
  140. return execute.apply(wmsService, list);
  141. });
  142. futureMap.put(bwkey, future);
  143. }
  144. // 收集返回数据
  145. final List<R> res = new ArrayList<>(params.size());
  146. for (Map.Entry<String, Future<List<R>>> entry : futureMap.entrySet()) {
  147. final String bwkey = entry.getKey();
  148. final Future<List<R>> future = entry.getValue();
  149. try {
  150. res.addAll(future.get());
  151. } catch (Exception e) {
  152. log.info("分发数据失败:method={} | bwkey={}", method, bwkey, e);
  153. }
  154. }
  155. return res;
  156. }
  157. @Override
  158. public List<DT_KUNNR_IN_RT_01> MT_KUNNR_IN_WMS01(List<DT_KUNNR_IN_01> params) {
  159. return distribute(
  160. "MT_KUNNR_IN_WMS01",
  161. params,
  162. DT_KUNNR_IN_01::getBWKEY,
  163. WMSService::MT_KUNNR_IN_WMS01
  164. );
  165. }
  166. @Override
  167. public List<DT_KUNNR_IN_RT_02> MT_KUNNR_IN_WMS02(List<DT_KUNNR_IN_02> params) {
  168. return distribute(
  169. "MT_KUNNR_IN_WMS02",
  170. params,
  171. DT_KUNNR_IN_02::getBWKEY,
  172. WMSService::MT_KUNNR_IN_WMS02
  173. );
  174. }
  175. @Override
  176. public List<DT_KUNNR_IN_RT_03> MT_KUNNR_IN_WMS03(List<DT_KUNNR_IN_03> params) {
  177. return distribute(
  178. "MT_KUNNR_IN_WMS03",
  179. params,
  180. DT_KUNNR_IN_03::getBWKEY,
  181. WMSService::MT_KUNNR_IN_WMS03
  182. );
  183. }
  184. @Override
  185. public List<DT_KUNNR_IN_RT_04> MT_KUNNR_IN_WMS04(List<DT_KUNNR_IN_04> params) {
  186. return distribute(
  187. "MT_KUNNR_IN_WMS04",
  188. params,
  189. DT_KUNNR_IN_04::getBWKEY,
  190. WMSService::MT_KUNNR_IN_WMS04
  191. );
  192. }
  193. @Override
  194. public List<DT_KUNNR_IN_RT_06> MT_KUNNR_IN_WMS06(List<DT_KUNNR_IN_06> params) {
  195. return distribute(
  196. "MT_KUNNR_IN_WMS06",
  197. params,
  198. DT_KUNNR_IN_06::getBWKEY,
  199. WMSService::MT_KUNNR_IN_WMS06
  200. );
  201. }
  202. @Override
  203. public List<DT_KUNNR_IN_RT_08> MT_KUNNR_IN_WMS08(List<DT_KUNNR_IN_08> params) {
  204. return distribute(
  205. "MT_KUNNR_IN_WMS08",
  206. params,
  207. DT_KUNNR_IN_08::getBWKEY,
  208. WMSService::MT_KUNNR_IN_WMS08
  209. );
  210. }
  211. @Override
  212. public List<DT_KUNNR_IN_RT_09> MT_KUNNR_IN_WMS09(List<DT_KUNNR_IN_09> params) {
  213. return distribute(
  214. "MT_KUNNR_IN_WMS09",
  215. params,
  216. DT_KUNNR_IN_09::getBWKEY,
  217. WMSService::MT_KUNNR_IN_WMS09
  218. );
  219. }
  220. @Override
  221. public List<DT_KUNNR_IN_RT_14> MT_KUNNR_IN_WMS14(List<DT_KUNNR_IN_14> params) {
  222. return distribute(
  223. "MT_KUNNR_IN_WMS14",
  224. params,
  225. DT_KUNNR_IN_14::getBWKEY,
  226. WMSService::MT_KUNNR_IN_WMS14
  227. );
  228. }
  229. @Override
  230. public List<DT_KUNNR_IN_RT_10_2> MT_KUNNR_IN_WMS10_2(List<DT_KUNNR_IN_10_2> params) {
  231. return distribute(
  232. "MT_KUNNR_IN_WMS10_2",
  233. params,
  234. DT_KUNNR_IN_10_2::getBWKEY,
  235. WMSService::MT_KUNNR_IN_WMS10_2
  236. );
  237. }
  238. @Override
  239. public List<DT_KUNNR_IN_RT_16> MT_KUNNR_IN_WMS16(List<DT_KUNNR_IN_16> params) {
  240. return distribute(
  241. "MT_KUNNR_IN_WMS16",
  242. params,
  243. DT_KUNNR_IN_16::getBWKEY,
  244. WMSService::MT_KUNNR_IN_WMS16
  245. );
  246. }
  247. @Override
  248. public List<DT_KUNNR_IN_RT_17> MT_KUNNR_IN_WMS17(List<DT_KUNNR_IN_17> params) {
  249. return distribute(
  250. "MT_KUNNR_IN_WMS17",
  251. params,
  252. DT_KUNNR_IN_17::getBWKEY,
  253. WMSService::MT_KUNNR_IN_WMS17
  254. );
  255. }
  256. @Override
  257. public List<DT_KUNNR_IN_RT_18> MT_KUNNR_IN_WMS18(List<DT_KUNNR_IN_18> params) {
  258. return distribute(
  259. "MT_KUNNR_IN_WMS18",
  260. params,
  261. DT_KUNNR_IN_18::getBWKEY,
  262. WMSService::MT_KUNNR_IN_WMS18
  263. );
  264. }
  265. }