123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162 |
- 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<String, String> 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);
- }
|