diff --git a/src/main/java/com/glxp/api/idc/controller/IdcController.java b/src/main/java/com/glxp/api/idc/controller/IdcController.java index e3ce5dbf..1d9eb78d 100644 --- a/src/main/java/com/glxp/api/idc/controller/IdcController.java +++ b/src/main/java/com/glxp/api/idc/controller/IdcController.java @@ -64,6 +64,7 @@ public class IdcController { //@Log("数据同步任务列表拉取") @RequestMapping(value = "/spssync/common/list") + @ResponseBody public BaseResponse list(HttpServletRequest request, @RequestBody(required = false) Map params) { return idcService.taskList(request, params); } diff --git a/src/main/java/com/glxp/api/idc/service/IdcService.java b/src/main/java/com/glxp/api/idc/service/IdcService.java index f079c9b0..c8e65099 100644 --- a/src/main/java/com/glxp/api/idc/service/IdcService.java +++ b/src/main/java/com/glxp/api/idc/service/IdcService.java @@ -25,7 +25,7 @@ public interface IdcService { BaseResponse download(HttpServletRequest request,Map params); public void asyncFetchTask(); - + public void asyncFetchUdiTask(); BaseResponse downlaodSuccess(HttpServletRequest request,Map params); public void asyncIdcTask(); diff --git a/src/main/java/com/glxp/api/idc/service/impl/IdcServiceImpl.java b/src/main/java/com/glxp/api/idc/service/impl/IdcServiceImpl.java index 191d713f..b76fc6fc 100644 --- a/src/main/java/com/glxp/api/idc/service/impl/IdcServiceImpl.java +++ b/src/main/java/com/glxp/api/idc/service/impl/IdcServiceImpl.java @@ -79,8 +79,7 @@ public class IdcServiceImpl implements IdcService { "//sup_cert_set/id///updateTime///供应商资质证书设置","companyCert//sup_company/customerId///updateTime///配送企业", "manufacturerCert//sup_manufacturer/id///updateTime///生产企业","productCert//sup_product/id///updateTime///产品资质信息", "//udicompany/id///updateTime///国际库医疗器械注册人信息"}; - //"dbDiProducts//productinfo/id///updateTime///DI产品信息", - + //"dbDiProducts//productinfo/id///updateTime///DI产品信息", private int orderNum=0; @Resource @@ -109,7 +108,13 @@ public class IdcServiceImpl implements IdcService { if(map==null) ResultVOUtils.error(9000, "下载任务不存在"); String json = FileUtils.readFileAll(map.get("cacheFilePath").toString()); + JSONObject object = JSON.parseObject(json); + if(object!=null&&object.getInteger("code")!=null) { + BaseResponse response = JSON.parseObject(json,BaseResponse.class); + return response; + } + return ResultVOUtils.success(object); } @@ -130,12 +135,29 @@ public class IdcServiceImpl implements IdcService { /*拉取前一级中继服务数据*/ @Async @Override + public void asyncFetchUdiTask() { + fetchTask(false); + } + @Async + @Override public void asyncFetchTask() { - - Map query = new HashMap(); + fetchTask(true); + + + } + + + private void fetchTask(boolean isIdc) { String host = getNextHost(); + if(!isIdc) { + Map map = dbDao.get("select * from sync_data_set limit 1"); + if(map!=null&&map.get("syncIp")!=null) + host = map.get("syncIp").toString(); + } + logger.info("fetch from ip:"+host); if(!StringUtils.isEmpty(host)) { - String result = post(host+"/spssync/common/list",query); + String result = post(host+"/spssync/common/list",null); + if(IDCUtils.isJson(result)) { JSONObject json = JSON.parseObject(result); if(json!=null&&json.getInteger("code")==20000&&json.getString("data")!=null) { @@ -144,15 +166,14 @@ public class IdcServiceImpl implements IdcService { for(Map map:list) { Map params = new HashMap(); params.put("taskId", map.get("taskId")); - fetchData(host+"/spssync/common/list",params); + fetchData(host,params); } } } } } - } - + @Async @Override public void asyncIdcTask() { @@ -233,13 +254,13 @@ public class IdcServiceImpl implements IdcService { - private String fetchData(String url,Map params) { + private String fetchData(String host,Map params) { OkHttpClient client = new OkHttpClient().newBuilder() .build(); MediaType mediaType = MediaType.parse("application/json"); RequestBody body = RequestBody.create(mediaType, JSON.toJSONString(params)); Request request = new Request.Builder() - .url(url) + .url(host+"/spssync/common/download") .method("POST", body) .addHeader("Content-Type", "application/json") .build(); @@ -248,15 +269,25 @@ public class IdcServiceImpl implements IdcService { try { boolean success=true; Response response = client.newCall(request).execute(); - result = response.body().toString(); + result = response.body().string(); JSONObject json = JSONObject.parseObject(result); + if(isLastLevel()) { - success = analyToDB(json,files); + if(json.get("code")!=null) { + success = analyToDB(json.getJSONObject("data"),files); + } else { + success = analyToDB(json,files); + } + if(!success) + logger.info("fetchData-->解析失败"); } else { - syncAddTaskStatus(json,false,success); + if(json.get("code")!=null) { + syncAddTaskStatus(json.getJSONObject("data"),false,success); + } else { + syncAddTaskStatus(json,false,success); + } } - String host = getNextHost(); if(success) { String ret = post(host+"/spssync/common/success",params); @@ -265,6 +296,7 @@ public class IdcServiceImpl implements IdcService { } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); + logger.error("fetchData-->"+e.getMessage()); } return result; } @@ -273,7 +305,9 @@ public class IdcServiceImpl implements IdcService { OkHttpClient client = new OkHttpClient().newBuilder() .build(); MediaType mediaType = MediaType.parse("application/json"); - RequestBody body = RequestBody.create(mediaType, JSON.toJSONString(params)); + RequestBody body = RequestBody.create(mediaType, ""); + if(params!=null) + body = RequestBody.create(mediaType,JSON.toJSONString(params)); Request request = new Request.Builder() .url(url) .method("POST", body) @@ -282,7 +316,7 @@ public class IdcServiceImpl implements IdcService { String result = ""; try { Response response = client.newCall(request).execute(); - result = response.body().toString(); + result = response.body().string(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); @@ -527,7 +561,7 @@ public class IdcServiceImpl implements IdcService { } } else { success = false; - saveIdcLog(messageType,"",tableName +">fail:上传地址未连通",i *limit,total); + saveIdcLog(messageType,"",syncIp+":"+tableName +">fail:上传地址未连通",i *limit,total); } } else { if(!StringUtils.isEmpty(recordSql)) { @@ -567,16 +601,38 @@ public class IdcServiceImpl implements IdcService { map.put("taskId", json.get("messageId")); map.put("cacheFilePath", fileName); map.put("status", success ? "1" : "0"); + saveExportStatus(map); + } /*解析到数据库*/ private boolean analyToDB(JSONObject jsonObject,MultipartFile[] files) { - if(!StringUtils.isEmpty(jsonObject.getString("tableName"))) { - List> list = jsonObject.getObject("data", List.class); - return analyData(jsonObject.getString("tableName"),jsonObject.getString("uniqueColumn"),list); + + if(jsonObject.get("data")!=null) { + logger.error("json>>>>>>>>>"); + String tableName = ""; + String uniqueColumn = ""; + Object object = jsonObject.get("data"); + List> list = new ArrayList<>(); + if(object instanceof List) { + list = jsonObject.getObject("data", List.class); + tableName = jsonObject.getString("tableName"); + uniqueColumn = jsonObject.getString("uniqueColumn"); + } else { + JSONObject obj = jsonObject.getJSONObject("data"); + list = obj.getObject("data", List.class); + tableName = obj.getString("tableName"); + uniqueColumn = obj.getString("uniqueColumn"); + } + logger.error("table-->"+tableName); + if(!StringUtils.isEmpty(tableName)) { + return analyData(tableName,uniqueColumn,list); + } else { + logger.error("数据格式错误:无数据标记"); + } //图片处理 } return false; @@ -584,7 +640,7 @@ public class IdcServiceImpl implements IdcService { /*按表名解析数据到数据库,子表暂未处理*/ private boolean analyData(String tableName,String uniqueColumn,List> list) { - + logger.error("ana-->"+tableName+"--list-->"+JSON.toJSONString(list)); String tName = DBAUtils.tableRealName(tableName); String sql="replace "+tName +"("; String del="delete from "+tName+" where "; @@ -698,6 +754,8 @@ public class IdcServiceImpl implements IdcService { } if(n>0) { result = (dbDao.save(sql)>0); + if(!result) + logger.error(tableName+"-->fetchSave Fail"); } } diff --git a/src/main/java/com/glxp/api/idc/thread/AsyncFetchTask.java b/src/main/java/com/glxp/api/idc/thread/AsyncFetchTask.java deleted file mode 100644 index 9312f3c8..00000000 --- a/src/main/java/com/glxp/api/idc/thread/AsyncFetchTask.java +++ /dev/null @@ -1,55 +0,0 @@ -package com.glxp.api.idc.thread; - -import javax.annotation.Resource; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.scheduling.annotation.EnableScheduling; -import org.springframework.scheduling.annotation.SchedulingConfigurer; -import org.springframework.scheduling.config.ScheduledTaskRegistrar; -import org.springframework.scheduling.support.CronTrigger; -import org.springframework.stereotype.Component; - -import com.glxp.api.dao.schedule.ScheduledDao; -import com.glxp.api.entity.system.ScheduledEntity; -import com.glxp.api.idc.service.IdcService; -import com.glxp.api.req.system.ScheduledRequest; - - -@Component -@EnableScheduling -public class AsyncFetchTask implements SchedulingConfigurer { - - final Logger logger = LoggerFactory.getLogger(AsyncFetchTask.class); - - @Resource - private ScheduledDao scheduledDao; - - @Resource - private IdcService idcService; - - @Override - public void configureTasks(ScheduledTaskRegistrar scheduledTaskRegistrar) { - scheduledTaskRegistrar.addTriggerTask(() -> process(), - triggerContext -> { - ScheduledRequest scheduledRequest = new ScheduledRequest(); - scheduledRequest.setCronName("syncFetch"); - logger.info("syncFetch----------------"); - ScheduledEntity scheduledEntity = scheduledDao.findScheduled(scheduledRequest); - String cron = scheduledEntity!=null ? scheduledEntity.getCron() : "* 0/5 * * * ?"; - - if (cron.isEmpty()) { - logger.error("cron is null"); - } - logger.info("syncFetch----------------"); - return new CronTrigger(cron).nextExecutionTime(triggerContext); - }); - } - - private void process() { - logger.info("syncFetch----process------------"); - - idcService.asyncFetchTask(); - } - -}