重调接口代码优化

release
xuliangwei 2 years ago
parent 6a791ac327
commit 5668fb1193
  1. 114
      dxhy-erp/src/main/java/com/dxhy/erp/service/impl/RequestRecordServiceImpl.java

@ -17,6 +17,7 @@ import com.dxhy.erp.service.RequestRecordService;
import com.dxhy.erp.service.SNPushCheckRecordService;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
@ -118,70 +119,79 @@ public class RequestRecordServiceImpl extends MpBaseServiceImpl<RequestRecordDao
@Override
public ResponseEntity<?> reCall(RequestRecord requestRecord) {
DynamicContextHolder.push(dbname+DbConstant.BUSINESS_READ);
RequestRecord dbRecord = requestRecordDao.selectById(requestRecord.getId());
log.info("数据库匹配信息:{}", JSONObject.toJSONString(dbRecord));
if (dbRecord == null || dbRecord.getStatus() != 0) {
return ResponseEntity.ok(JSONObject.toJSONString(R.error("请求信息错误,请确认后再发送!")));
}
if (dbRecord.getIsRecall() == 0) {
return ResponseEntity.ok(JSONObject.toJSONString(R.error("该请求参数不符合标准,无法进行重调,请校验参数后再申请!")));
}
RequestRecordEnum[] RequestRecordEnums = RequestRecordEnum.values();
log.info("获取到的枚举类型:{}",JSONObject.toJSONString(RequestRecordEnums));
for (RequestRecordEnum recordenum : RequestRecordEnums) {
if (recordenum.getMethodName().equals(dbRecord.getActiveCall())) {
snPushCheckRecordService = JSONObject.parseObject(JSONObject.toJSONString(applicationContext.getBean(recordenum.getClassName())),SNPushCheckRecordService.class);
log.info("通过反射获取到的snPushCheckRecordService是否为空:{}",JSONObject.toJSONString(snPushCheckRecordService == null));
Method method = ReflectUtil.getMethodByName(snPushCheckRecordService.getClass(),recordenum.getMethodName());
log.info("通过反射获取到的方法是否为空:{}",JSONObject.toJSONString(method == null));
if (method != null) {
log.info("通过反射获取到的方法名:{}",JSONObject.toJSONString(method.getName()));
}
try {
DynamicContextHolder.push(dbname+DbConstant.BUSINESS_READ);
RequestRecord dbRecord = requestRecordDao.selectById(requestRecord.getId());
log.info("数据库匹配信息:{}", JSONObject.toJSONString(dbRecord));
if (dbRecord == null) {
return ResponseEntity.ok(JSONObject.toJSONString(R.error("请求信息错误,请确认后再发送!")));
}
if (dbRecord.getStatus() == 0) {
return ResponseEntity.ok(JSONObject.toJSONString(R.error("该请求已成功,无法继续重调!")));
}
if (dbRecord.getIsRecall() == 0) {
return ResponseEntity.ok(JSONObject.toJSONString(R.error("该请求参数不符合标准,无法进行重调!")));
}
Object invokeResponse = null;
R data = new R(1000, "default success");
try {
DynamicContextHolder.push(dbname+DbConstant.BUSINESS_WRITE);
if (recordenum.getArgClass().length>1) {
List<String> list = JSONObject.parseObject(dbRecord.getActiveCallRequestDetail(),List.class);
invokeResponse = method.invoke(snPushCheckRecordService, JSONObject.parseObject(list.get(0), recordenum.getArgClass()[0]),JSONObject.parseObject(list.get(1), recordenum.getArgClass()[1]));
}else {
invokeResponse = method.invoke(snPushCheckRecordService, JSONObject.parseObject(dbRecord.getActiveCallRequestDetail(), recordenum.getArgClass()[0]));
RequestRecordEnum[] RequestRecordEnums = RequestRecordEnum.values();
log.info("获取到的枚举类型:{}",JSONObject.toJSONString(RequestRecordEnums));
for (RequestRecordEnum recordenum : RequestRecordEnums) {
if (recordenum.getMethodName().equals(dbRecord.getActiveCall())) {
snPushCheckRecordService = JSONObject.parseObject(JSONObject.toJSONString(applicationContext.getBean(recordenum.getClassName())),SNPushCheckRecordService.class);
log.info("通过反射获取到的snPushCheckRecordService是否为空:{}",JSONObject.toJSONString(snPushCheckRecordService == null));
Method method = ReflectUtil.getMethodByName(snPushCheckRecordService.getClass(),recordenum.getMethodName());
log.info("通过反射获取到的方法是否为空:{}",JSONObject.toJSONString(method == null));
if (method != null) {
log.info("通过反射获取到的方法名:{}",JSONObject.toJSONString(method.getName()));
}
log.info("重调后得到的结果:{}",JSONObject.toJSONString(invokeResponse));
Map<String,String> map = JSONObject.parseObject(JSONObject.toJSONString(invokeResponse), Map.class);
Object invokeResponse = null;
R data = new R(1000, "default success");
try {
DynamicContextHolder.push(dbname+DbConstant.BUSINESS_WRITE);
if (recordenum.getArgClass().length>1) {
List<String> list = JSONObject.parseObject(dbRecord.getActiveCallRequestDetail(),List.class);
invokeResponse = method.invoke(snPushCheckRecordService, JSONObject.parseObject(list.get(0), recordenum.getArgClass()[0]),JSONObject.parseObject(list.get(1), recordenum.getArgClass()[1]));
}else {
invokeResponse = method.invoke(snPushCheckRecordService, JSONObject.parseObject(dbRecord.getActiveCallRequestDetail(), recordenum.getArgClass()[0]));
}
log.info("重调后得到的结果:{}",JSONObject.toJSONString(invokeResponse));
data.put("data", invokeResponse);
Map<String,String> map = JSONObject.parseObject(JSONObject.toJSONString(invokeResponse), Map.class);
if (map.containsKey("SAPKEY")) {
if (!map.containsValue("处理成功")) {
updateFailedRecallRecord(dbRecord, 1, JSONObject.toJSONString(invokeResponse), JSONObject.toJSONString(data), map.get("ZMESSAGE"), 0);
return ResponseEntity.ok(JSONObject.toJSONString(data));
data.put("data", invokeResponse);
if (map.containsKey("SAPKEY")) {
if (!map.containsValue("处理成功")) {
updateFailedRecallRecord(dbRecord, 1, JSONObject.toJSONString(invokeResponse), JSONObject.toJSONString(data), map.get("ZMESSAGE"), 0);
return ResponseEntity.ok(JSONObject.toJSONString(data));
}
}
}
if (map.containsKey("code")) {
updateFailedRecallRecord(dbRecord, 1, JSONObject.toJSONString(invokeResponse), JSONObject.toJSONString(data), map.get("message"), 0);
return ResponseEntity.ok(JSONObject.toJSONString(data));
} else {
if (map.containsKey("cyjg")) {
updateFailedRecallRecord(dbRecord, 1, JSONObject.toJSONString(invokeResponse), JSONObject.toJSONString(data), map.get("cyjgxx"), 0);
if (map.containsKey("code")) {
updateFailedRecallRecord(dbRecord, 1, JSONObject.toJSONString(invokeResponse), JSONObject.toJSONString(data), map.get("message"), 0);
return ResponseEntity.ok(JSONObject.toJSONString(data));
} else {
if (map.containsKey("cyjg")) {
updateFailedRecallRecord(dbRecord, 1, JSONObject.toJSONString(invokeResponse), JSONObject.toJSONString(data), map.get("cyjgxx"), 0);
return ResponseEntity.ok(JSONObject.toJSONString(data));
}
}
updateSuccessfulRecallRecord(dbRecord, 0, JSONObject.toJSONString(invokeResponse), JSONObject.toJSONString(data));
return ResponseEntity.ok(JSONObject.toJSONString(data));
}
catch (Exception e) {
log.info("重调出现异常,异常信息:{}",e);
updateFailedRecallRecord(dbRecord, 1, JSONObject.toJSONString(invokeResponse),
JSONObject.toJSONString(data), "重调用失败,异常信息:{}"+JSONObject.toJSONString(e.getMessage()), 0);
return ResponseEntity.ok(JSONObject.toJSONString(R.error(CommonConstants.MSG_ERR_DEFAULT)));
}
updateSuccessfulRecallRecord(dbRecord, 0, JSONObject.toJSONString(invokeResponse), JSONObject.toJSONString(data));
return ResponseEntity.ok(JSONObject.toJSONString(data));
}
catch (Exception e) {
log.info("重调出现异常,异常信息:{}",e);
e.printStackTrace();
return ResponseEntity.ok(JSONObject.toJSONString(R.error(CommonConstants.MSG_ERR_DEFAULT)));
}
}
} catch (Exception e) {
log.info("请求处理异常,异常信息:{}", JSONObject.toJSONString(e));
return ResponseEntity.ok(JSONObject.toJSONString(R.error("请求处理异常,请校正参数!")));
}
return ResponseEntity.ok(JSONObject.toJSONString(R.error("未找到相应信息!")));

Loading…
Cancel
Save