BaseRequestHandler.java 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. package handler.yongyou;
  2. import com.alibaba.fastjson.JSON;
  3. import com.alibaba.fastjson.JSONObject;
  4. import com.platform.common.cache.DictCache;
  5. import com.platform.common.constant.CommonConstants;
  6. import com.platform.common.enums.DictTypeEnum;
  7. import com.platform.common.util.BeanUtils;
  8. import com.platform.common.util.HttpUtil;
  9. import com.platform.common.util.JsonUtils;
  10. import com.platform.common.util.R;
  11. import com.platform.dao.dto.upms.ThirdInfoLogDTO;
  12. import com.platform.dao.entity.upms.ThirdInfoLog;
  13. import com.platform.service.upms.ThirdInfoLogService;
  14. import handler.yongyou.request.BaseRequest;
  15. import handler.yongyou.request.FromPurchaseBaseRequest;
  16. import handler.yongyou.request.FromYongYouBaseRequest;
  17. import handler.yongyou.response.ApplyOutStoreFormResponse;
  18. import handler.yongyou.response.BaseResponse;
  19. import handler.yongyou.response.ToYongYouBaseResponse;
  20. import lombok.extern.slf4j.Slf4j;
  21. import java.util.Map;
  22. /**
  23. * 1:给用友发送消息
  24. * 1: 封装消息
  25. * 2:发送请求
  26. * 3:
  27. */
  28. @Slf4j
  29. public abstract class BaseRequestHandler {
  30. private BaseRequest request;
  31. private ThirdInfoLogService infoService;
  32. /**
  33. * service name
  34. */
  35. private String serviceType;
  36. public void setServiceType(String serviceType){
  37. this.serviceType = serviceType;
  38. }
  39. public void setRequest(BaseRequest request){
  40. this.request = request;
  41. }
  42. /**
  43. * gain the thirdLogService bean,just prevent many bean
  44. * @return
  45. */
  46. public synchronized ThirdInfoLogService getLogService(){
  47. if(this.infoService == null){
  48. this.infoService = (ThirdInfoLogService) BeanUtils.getBean("thirdInfoLogService");
  49. }
  50. return this.infoService;
  51. }
  52. /**
  53. * handle post request,this method is used to our system request xindian System
  54. * 我们请求新点数据处理
  55. * @return
  56. */
  57. public String handler() {
  58. BaseResponse response = null;
  59. // String logId = generateLogRequest(request, this.serviceType);
  60. // post请求返回值 the request return value
  61. String responseStr = "";
  62. try {
  63. responseStr = sendRequest(request);
  64. } catch (Exception e) {
  65. log.info("请求异常 request exception:" + e.getMessage() + e.getCause());
  66. e.printStackTrace();
  67. }
  68. return responseStr;
  69. }
  70. public ToYongYouBaseResponse propellingHandle(FromYongYouBaseRequest req,String reqStr){
  71. ToYongYouBaseResponse respData = null;
  72. //获取head
  73. // 具体处理
  74. R resp = successHandler(req,reqStr);
  75. respData = (ToYongYouBaseResponse) resp.getData();
  76. return respData;
  77. }
  78. public ApplyOutStoreFormResponse propellingHandle2(FromPurchaseBaseRequest req, String reqStr){
  79. ApplyOutStoreFormResponse respData = null;
  80. //获取head
  81. // 具体处理
  82. R resp = successHandler2(req,reqStr);
  83. respData = (ApplyOutStoreFormResponse) resp.getData();
  84. return respData;
  85. }
  86. public abstract R successHandler(FromYongYouBaseRequest req,String reqStr);
  87. public abstract R successHandler2(FromPurchaseBaseRequest req,String reqStr);
  88. /**
  89. * 生成request log
  90. *
  91. * @param request
  92. */
  93. private String generateLogRequest(BaseRequest request,String serviceType) {
  94. if (request != null) {
  95. ThirdInfoLogDTO dto = new ThirdInfoLogDTO();
  96. String requestJson = "";
  97. // 保函文件传送,日志不存储文件内容,太大了
  98. requestJson = JSONObject.toJSONString(request);
  99. dto.setContent(requestJson);
  100. dto.setType(ThirdInfoLogDTO.TYPE_REQUEST);
  101. dto.setThirdName(CommonConstants.THIRD_PARTY_YONGYOU);
  102. //dto.setApplicationId(request.getApplyno());
  103. dto.setServiceType(serviceType);
  104. dto.setErrorStatus(1);
  105. return addInfoLog(dto);
  106. }
  107. return null;
  108. }
  109. /**
  110. * 保存日志
  111. * @param dto
  112. */
  113. private String addInfoLog(ThirdInfoLogDTO dto) {
  114. return getLogService().saveByDTO(dto).getId();
  115. }
  116. /**
  117. * @return
  118. * @throws Exception
  119. */
  120. public String sendRequest(BaseRequest request) throws Exception {
  121. // 获取推送的接口地址
  122. String url = DictCache.getValueByCode(DictTypeEnum.THIRD_PARTY_YONGYOU.getType(), this.serviceType);
  123. log.info("请求URL----------------"+url);
  124. // 获取请求体
  125. String bodyString = JSON.toJSONString(request);
  126. Map<String, String> bodyMap = JsonUtils.jsonToMap(bodyString);
  127. String requestData = JsonUtils.objectToJson(bodyMap);
  128. log.info("用友请求参数:" + requestData);
  129. String resStr = HttpUtil.post(url, requestData);
  130. try {
  131. log.info("用友中心响应数据---"+resStr);
  132. } catch (Exception e) {
  133. throw new Exception("响应失败:" + e);
  134. }
  135. return resStr;
  136. }
  137. /**
  138. * 推送给用友成功后,对用友的返回数据进行本地处理
  139. * 子类自行实现
  140. * @param req
  141. */
  142. public abstract void successResponse(BaseRequest req, BaseResponse response);
  143. }