|
@@ -0,0 +1,148 @@
|
|
|
+package handler.yongyou;
|
|
|
+
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.platform.common.cache.DictCache;
|
|
|
+import com.platform.common.constant.CommonConstants;
|
|
|
+import com.platform.common.enums.DictTypeEnum;
|
|
|
+import com.platform.common.util.BeanUtils;
|
|
|
+import com.platform.common.util.HttpUtil;
|
|
|
+import com.platform.common.util.JsonUtils;
|
|
|
+import com.platform.common.util.R;
|
|
|
+import com.platform.common.yongyou.request.BaseRequest;
|
|
|
+import com.platform.common.yongyou.response.BaseResponse;
|
|
|
+import com.platform.dao.dto.upms.ThirdInfoLogDTO;
|
|
|
+import com.platform.dao.entity.upms.ThirdInfoLog;
|
|
|
+import com.platform.service.upms.ThirdInfoLogService;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 1:给用友发送消息
|
|
|
+ * 1: 封装消息
|
|
|
+ * 2:发送请求
|
|
|
+ * 3:
|
|
|
+ */
|
|
|
+@Slf4j
|
|
|
+public abstract class BaseRequestHandler {
|
|
|
+ private BaseRequest request;
|
|
|
+
|
|
|
+ private ThirdInfoLogService infoService;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * service name
|
|
|
+ */
|
|
|
+ private String serviceType;
|
|
|
+
|
|
|
+ public void setRequest(BaseRequest request){
|
|
|
+ this.request = request;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * gain the thirdLogService bean,just prevent many bean
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public synchronized ThirdInfoLogService getLogService(){
|
|
|
+ if(this.infoService == null){
|
|
|
+ this.infoService = (ThirdInfoLogService) BeanUtils.getBean("thirdInfoLogService");
|
|
|
+ }
|
|
|
+ return this.infoService;
|
|
|
+ }
|
|
|
+ /**
|
|
|
+ * handle post request,this method is used to our system request xindian System
|
|
|
+ * 我们请求新点数据处理
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @SuppressWarnings("unchecked")
|
|
|
+ public R handler() {
|
|
|
+ R r = new R();
|
|
|
+ BaseResponse response = null;
|
|
|
+ String logId = generateLogRequest(request,this.serviceType);
|
|
|
+ // post请求返回值 the request return value
|
|
|
+ String responseStr = "";
|
|
|
+ try {
|
|
|
+ responseStr = sendRequest(request);
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.info("请求异常 request exception:" + e.getMessage() + e.getCause());
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ log.info("返回结果 result:" + responseStr);
|
|
|
+ try {
|
|
|
+ response = JSONObject.parseObject(responseStr, BaseResponse.class);
|
|
|
+ r.setData(response);
|
|
|
+ ThirdInfoLog log = new ThirdInfoLog();
|
|
|
+ log.setId(logId);
|
|
|
+ if(!"true".equals(response.getResult())){
|
|
|
+ log.setErrorMsg(response.getMsg());
|
|
|
+ log.setErrorStatus(1);
|
|
|
+ }else{
|
|
|
+ log.setErrorStatus(0);
|
|
|
+ successResponse(request);
|
|
|
+ }
|
|
|
+ getLogService().modModelByPrimaryKey(log);
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.info("返回结果异常");
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return r;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 生成request log
|
|
|
+ *
|
|
|
+ * @param request
|
|
|
+ */
|
|
|
+ private String generateLogRequest(BaseRequest request,String serviceType) {
|
|
|
+ if (request != null) {
|
|
|
+ ThirdInfoLogDTO dto = new ThirdInfoLogDTO();
|
|
|
+ String requestJson = "";
|
|
|
+ // 保函文件传送,日志不存储文件内容,太大了
|
|
|
+ requestJson = JSONObject.toJSONString(request);
|
|
|
+ dto.setContent(requestJson);
|
|
|
+ dto.setType(ThirdInfoLogDTO.TYPE_REQUEST);
|
|
|
+ dto.setThirdName(CommonConstants.THIRD_PARTY_YONGYOU);
|
|
|
+ //dto.setApplicationId(request.getApplyno());
|
|
|
+ dto.setServiceType(serviceType);
|
|
|
+ dto.setErrorStatus(1);
|
|
|
+ return addInfoLog(dto);
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 保存日志
|
|
|
+ * @param dto
|
|
|
+ */
|
|
|
+ private String addInfoLog(ThirdInfoLogDTO dto) {
|
|
|
+ return getLogService().saveByDTO(dto).getId();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @return
|
|
|
+ * @throws Exception
|
|
|
+ */
|
|
|
+ public String sendRequest(BaseRequest request) throws Exception {
|
|
|
+ // 获取推送的接口地址
|
|
|
+ String url = DictCache.getValueByCode(DictTypeEnum.THIRD_PARTY_YONGYOU.getType(), this.serviceType);
|
|
|
+ log.info("请求URL----------------"+url);
|
|
|
+ // 获取请求体
|
|
|
+ String bodyString = JSON.toJSONString(request.getBody());
|
|
|
+ Map<String, String> bodyMap = JsonUtils.jsonToMap(bodyString);
|
|
|
+ String resStr = HttpUtil.post(url, JsonUtils.objectToJson(bodyMap));
|
|
|
+ try {
|
|
|
+ log.info("用友中心响应数据---"+resStr);
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw new Exception("响应失败:" + e);
|
|
|
+ }
|
|
|
+ return resStr;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 推送给金融支撑平台成功后,本地操作
|
|
|
+ * 子类自行实现
|
|
|
+ * @param req
|
|
|
+ */
|
|
|
+ public abstract void successResponse(BaseRequest req);
|
|
|
+
|
|
|
+}
|