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.dao.dto.upms.ThirdInfoLogDTO; import com.platform.dao.entity.upms.ThirdInfoLog; import com.platform.service.upms.ThirdInfoLogService; import handler.yongyou.request.BaseRequest; import handler.yongyou.request.FromPurchaseBaseRequest; import handler.yongyou.request.FromYongYouBaseRequest; import handler.yongyou.response.ApplyOutStoreFormResponse; import handler.yongyou.response.BaseResponse; import handler.yongyou.response.ToYongYouBaseResponse; 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 setServiceType(String serviceType){ this.serviceType = 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 */ public String handler() { 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(); } return responseStr; } public ToYongYouBaseResponse propellingHandle(FromYongYouBaseRequest req,String reqStr){ ToYongYouBaseResponse respData = null; //获取head // 具体处理 R resp = successHandler(req,reqStr); respData = (ToYongYouBaseResponse) resp.getData(); return respData; } public ApplyOutStoreFormResponse propellingHandle2(FromPurchaseBaseRequest req, String reqStr){ ApplyOutStoreFormResponse respData = null; //获取head // 具体处理 R resp = successHandler2(req,reqStr); respData = (ApplyOutStoreFormResponse) resp.getData(); return respData; } public abstract R successHandler(FromYongYouBaseRequest req,String reqStr); public abstract R successHandler2(FromPurchaseBaseRequest req,String reqStr); /** * 生成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); Map bodyMap = JsonUtils.jsonToMap(bodyString); String requestData = JsonUtils.objectToJson(bodyMap); log.info("用友请求参数:" + requestData); String resStr = HttpUtil.post(url, requestData); try { log.info("用友中心响应数据---"+resStr); } catch (Exception e) { throw new Exception("响应失败:" + e); } return resStr; } /** * 推送给用友成功后,对用友的返回数据进行本地处理 * 子类自行实现 * @param req */ public abstract void successResponse(BaseRequest req, BaseResponse response); }