From 056041f5a3a26c1f2470904a22f5e93ff82f46be Mon Sep 17 00:00:00 2001 From: admin Date: Tue, 4 Apr 2023 00:13:10 +0800 Subject: [PATCH] =?UTF-8?q?=E5=9B=BE=E7=89=87=E4=B8=8B=E8=BD=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/glxp/api/idc/service/IdcService.java | 3 +- .../api/idc/service/impl/IdcServiceImpl.java | 152 ++++++++++++++---- .../java/com/glxp/api/idc/utils/UriUtils.java | 79 +++++++++ 3 files changed, 203 insertions(+), 31 deletions(-) create mode 100644 src/main/java/com/glxp/api/idc/utils/UriUtils.java 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 320b0694f..8bbf9cdf2 100644 --- a/src/main/java/com/glxp/api/idc/service/IdcService.java +++ b/src/main/java/com/glxp/api/idc/service/IdcService.java @@ -3,6 +3,7 @@ package com.glxp.api.idc.service; import java.util.Map; import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; import org.springframework.web.multipart.MultipartFile; @@ -30,5 +31,5 @@ public interface IdcService { public void asyncIdcTask(); void asyncUdiTask(); void asyncSpsTask(); - + void downloadFile(String fileName,HttpServletResponse response) ; } 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 f683a604b..77ca67091 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 @@ -3,6 +3,8 @@ package com.glxp.api.idc.service.impl; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; import java.util.ArrayList; import java.util.Date; import java.util.HashMap; @@ -13,6 +15,7 @@ import java.util.concurrent.TimeUnit; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; import org.apache.commons.lang3.StringUtils; import org.apache.commons.text.WordUtils; @@ -79,7 +82,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 @@ -142,8 +145,6 @@ public class IdcServiceImpl implements IdcService { @Override public void asyncFetchTask() { fetchTask(true); - - } @@ -210,7 +211,7 @@ public class IdcServiceImpl implements IdcService { boolean sync = true; String[] tnames = t.split("/"); - String lastUpdateTime = getUpdateTime(tnames[2]); + String lastUpdateTime = getUpdateTime(tnames[3]); Date nowUpdateTime = new Date(); if(!StringUtils.isEmpty(tnames[0])) { @@ -219,7 +220,7 @@ public class IdcServiceImpl implements IdcService { Map map = new HashMap(); String sqlWhere = ""; if(!StringUtils.isEmpty(tnames[6])) { - sqlWhere+=" "+tnames[6]+">=cast('"+lastUpdateTime+"' as datetime)"; + sqlWhere+=" "+tnames[6]+"between cast('"+lastUpdateTime+"' as datetime) and cast('"+DateUtil.formatDate(nowUpdateTime, "yyyy-MM-dd HH:mm:ss")+"' as datetime)"; } else { sqlWhere = "not exists (select fkId from idc_record where type='"+tnames[2]+"' and fkId="+tnames[2]+"."+keyColumn+")"; map.put("isEnd", "1"); @@ -272,11 +273,12 @@ public class IdcServiceImpl implements IdcService { Response response = client.newCall(request).execute(); result = response.body().string(); JSONObject json = JSONObject.parseObject(result); + if(isLastLevel()) { if(json.get("code")!=null) { - success = analyToDB(json.getJSONObject("data"),files); + success = analyToDB(host,json.getJSONObject("data"),files,false); } else { - success = analyToDB(json,files); + success = analyToDB(host,json,files,false); } if(!success) logger.info("fetchData-->解析失败"); @@ -339,14 +341,15 @@ public class IdcServiceImpl implements IdcService { String secretKey = request.getHeader("secretKey"); /*******/ JSONObject json = JSONObject.parseObject(content); - + String filePathSlash = filePath.substring(filePath.length() -1).equals("/") ? "" : "/"; + String backFileSlash = backFilePath.substring(filePath.length() -1).equals("/") ? "" : "/"; String datePath = DateUtil.formatDate(new Date(),"yyyy-MM-dd"); - String fileName = filePath +datePath+"/"+json.getString("messageType")+"-"+json.getString("messageId")+".udi"; - String backFileName = backFilePath +datePath+"/"+json.getString("messageType")+"-"+json.getString("messageId")+".udi"; - if(!FileUtils.makeDirectory( filePath +datePath)) - IDCUtils.createDirectory(filePath +datePath+"/"); - if(!FileUtils.makeDirectory(backFilePath +datePath)) - IDCUtils.createDirectory(filePath +datePath+"/"); + String fileName =filePath +filePathSlash +datePath+"/"+json.getString("messageType")+"-"+json.getString("messageId")+".udi"; + String backFileName = backFilePath +backFileSlash +datePath+"/"+json.getString("messageType")+"-"+json.getString("messageId")+".udi"; + if(!FileUtils.makeDirectory( filePath +filePathSlash +datePath)) + IDCUtils.createDirectory(filePath +filePathSlash +datePath+"/"); + if(!FileUtils.makeDirectory(backFilePath +backFileSlash +datePath)) + IDCUtils.createDirectory(filePath +backFileSlash +datePath+"/"); FileUtils.SaveFileAs(content, fileName); FileUtils.SaveFileAs(content, backFileName); @@ -356,9 +359,10 @@ public class IdcServiceImpl implements IdcService { int n=0; try { for(MultipartFile file:files) { - saveFiles[n] = filePath +datePath+"/"+json.getString("messageId")+"/"+file.getOriginalFilename(); - writeFile(file.getBytes(), filePath +datePath+"/"+json.getString("messageId")+"/", file.getOriginalFilename()); - writeFile(file.getBytes(), backFilePath +datePath+"/"+json.getString("messageId")+"/", file.getOriginalFilename()); + String imageName = filePath +filePathSlash+"register/file/image2/"+file.getOriginalFilename(); + saveFiles[n] = imageName; + writeFile(file.getBytes(), filePath +filePathSlash+"register/file/image2/", file.getOriginalFilename()); + writeFile(file.getBytes(), backFilePath +backFileSlash+datePath+"register/file/image2/", file.getOriginalFilename()); n++; } } catch (IOException e) { @@ -373,7 +377,7 @@ public class IdcServiceImpl implements IdcService { syncAddTaskStatus(json,true,true); /*解析入库*/ if(isLastLevel()) { - if(!analyToDB(json,files)) + if(!analyToDB("",json,files,true)) return ResultVOUtils.error(9000, "解析失败"); } else { //需要转发 @@ -406,7 +410,22 @@ public class IdcServiceImpl implements IdcService { return ResultVOUtils.error(9999, ""); } - + + @Override + public void downloadFile(String fileName,HttpServletResponse response) { + OutputStream os; + String filePathSlash = filePath.substring(filePath.length() -1).equals("/") ? "" : fileName.substring(0,1).equals("/") ? "" : "/"; + try { + byte[] bytes = FileUtils.readFileByBytes(filePath+filePathSlash+fileName); + os = response.getOutputStream(); + os.write(bytes); + os.flush(); + os.close(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } private void saveUploadStatus(Map params) { Map map = new HashMap(); @@ -496,7 +515,7 @@ public class IdcServiceImpl implements IdcService { orderNum++; String messageType = params.get("messageType").toString(); saveIdcLog(messageType,"",tableName +">"+where,0,total); - + String filePathSlash = filePath.substring(filePath.length() -1).equals("/") ? "" : "/"; if(total>0) { success = true; @@ -519,7 +538,7 @@ public class IdcServiceImpl implements IdcService { if(list!=null&&list.size()>0) { for(int m=0;m> childList = getList(chidTnames[2],childMap); list.get(m).put("tableName"+z, chidTnames[2]); list.get(m).put("data"+z, childList); - logger.info("child-data-->"+JSON.toJSONString(childList)); } } } @@ -554,6 +572,7 @@ public class IdcServiceImpl implements IdcService { if(!StringUtils.isEmpty(recordSql)) { dbDao.save("insert into idc_record (id,type,fkId,isEnd,createTime) values "+recordSql); } + saveDownloadStatus(messageType,json.getString("reqNo"),messageType+" 记录数:"+list.size()+"/"+total); saveIdcLog(messageType,"",tableName +">success",i *limit,total); } else { success = false; @@ -610,27 +629,28 @@ public class IdcServiceImpl implements IdcService { /*解析到数据库*/ - private boolean analyToDB(JSONObject jsonObject,MultipartFile[] files) { + private boolean analyToDB(String host,JSONObject jsonObject,MultipartFile[] files,boolean isUpload) { if(jsonObject.get("data")!=null) { - logger.error("json>>>>>>>>>"); String tableName = ""; String uniqueColumn = ""; + String filePathColumn = ""; 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"); + filePathColumn = jsonObject.getString("filePathColumn"); } else { JSONObject obj = jsonObject.getJSONObject("data"); list = obj.getObject("data", List.class); tableName = obj.getString("tableName"); uniqueColumn = obj.getString("uniqueColumn"); + filePathColumn = obj.getString("filePathColumn"); } - logger.error("table-->"+tableName); if(!StringUtils.isEmpty(tableName)) { - return analyData(tableName,uniqueColumn,list); + return analyData(host,tableName,uniqueColumn,filePathColumn,list,isUpload); } else { logger.error("数据格式错误:无数据标记"); } @@ -639,9 +659,8 @@ public class IdcServiceImpl implements IdcService { return false; } - /*按表名解析数据到数据库,子表暂未处理*/ - private boolean analyData(String tableName,String uniqueColumn,List> list) { + private boolean analyData(String host,String tableName,String uniqueColumn,String filePathColumn,List> list,boolean isUpload) { String tName = DBAUtils.tableRealName(tableName); String sql="replace "+tName +"("; @@ -677,10 +696,15 @@ public class IdcServiceImpl implements IdcService { if(list!=null&&list.size()>0) { int n = 0; int d = 0; + String[] files = new String[list.size()]; for(int i=0;i) obj) { chList.add((Map)o); } - analyData(list.get(i).get("tableName"+m).toString(),"",chList); + analyData(host,list.get(i).get("tableName"+m).toString(),"","",chList,isUpload); } else { break; } @@ -759,6 +783,9 @@ public class IdcServiceImpl implements IdcService { if(!result) logger.error(tableName+"-->fetchSave Fail"); } + if(!isUpload&&!StringUtils.isEmpty(filePathColumn)) { + batchDownloadFile(host,files); + } } return result; @@ -818,6 +845,68 @@ public class IdcServiceImpl implements IdcService { return result; } + /*下载文件*/ + private boolean batchDownloadFile(String syncIp,String[] files) { + boolean success = true; + for(String fileName:files) { + if(!StringUtils.isEmpty(fileName)) { + if(!signleDownloadFile(syncIp,fileName)) + success = false; + } + } + return success; + } + private boolean signleDownloadFile(String syncIp,String fileName) { + OkHttpClient client = new OkHttpClient(); + RequestBody requestBody = new MultipartBody.Builder() + .setType(MultipartBody.FORM) + .addFormDataPart("fileName", fileName) + .build(); + + + Request request = new Request.Builder() + .url(syncIp+"/spssync/common/downloadFile") + .post(requestBody) + .build(); + try { + Response result = client.newCall(request).execute(); + + + + if (MediaType.parse("application/force-download").equals(result.body().contentType())) { + try (InputStream inputStream = result.body().byteStream()) { + String filePathSlash = filePath.substring(filePath.length() -1).equals("/") ? "" : "/"; + + FileOutputStream outputStream =new FileOutputStream(filePath +filePathSlash+fileName); + + byte b[]=new byte[1024]; + + int len=0; + + while ((len=inputStream.read(b))!=-1){ + + outputStream.write(b,0,len); + + } + + outputStream.flush(); + + + + } catch (Exception e) { + + + } + } + + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + return true; + } + /*获取转发服务地址,当前值允许单向,只使用参数upper_server_ip*/ private String getNextHost() { SystemParamConfigEntity systemParamConfigEntity = systemParamConfigService.selectByParamKey("upper_server_ip"); @@ -884,7 +973,6 @@ public class IdcServiceImpl implements IdcService { systemParamConfigEntity.setParamType(0); systemParamConfigService.insert(systemParamConfigEntity); } - System.out.print(JSON.toJSONString(systemParamConfigEntity)+"\n"); return systemParamConfigEntity; } private void writeFile(byte[] file, String filePath, String fileName) throws Exception { @@ -941,6 +1029,10 @@ public class IdcServiceImpl implements IdcService { if(map!=null&&map.get("id")!=null) { updateTime = DateUtil.formatDate(IDCUtils.parseDate(map.get("statusTime").toString()),"yyyy-MM-dd HH:mm:ss"); } else { + //国家库数据 + if(id.equals("productinfo")) { + + } updateTime = "2000-01-01 00:00:00"; String sql = "insert into idc_status (id,statusTime) values ('"+id+"',cast('"+updateTime+"' as datetime))"; dbDao.save(sql); diff --git a/src/main/java/com/glxp/api/idc/utils/UriUtils.java b/src/main/java/com/glxp/api/idc/utils/UriUtils.java new file mode 100644 index 000000000..1478eb257 --- /dev/null +++ b/src/main/java/com/glxp/api/idc/utils/UriUtils.java @@ -0,0 +1,79 @@ +package com.glxp.api.idc.utils; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.Enumeration; +import java.util.HashMap; +import java.util.Map; + +import javax.servlet.http.HttpServletRequest; + +import org.springframework.http.HttpEntity; +import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpMethod; +import org.springframework.stereotype.Service; + +import com.alibaba.fastjson.JSON; + +import cn.hutool.core.io.IoUtil; +import lombok.extern.slf4j.Slf4j; + +@Slf4j +@Service +public class UriUtils { + + public String parseUri(String url) { + String uri = url; + int a1 = uri.indexOf("://"); + int a2 = 0; + if (a1 > 0) { + a2 = uri.indexOf("/", a1 + 4); + if (a2 > 0) { + uri = uri.substring(a2 + 1); + } + } + return uri; + } + + + public HttpEntity buildHeader(HttpServletRequest request) { + + log.info(request.getMethod()); + String bodyContent = ""; + Map headerParam = new HashMap<>(); + if (request.getMethod().equals(HttpMethod.GET)) { + Map bodyParam = new HashMap(); + Enumeration pNames = request.getParameterNames(); + while (pNames.hasMoreElements()) { + String name = (String) pNames.nextElement(); + String value = request.getParameter(name); + bodyParam.put(name, value); + } + + bodyContent = JSON.toJSONString(bodyParam); + } else { + BufferedReader reader = null; + try { + reader = new BufferedReader(new InputStreamReader(request.getInputStream())); + bodyContent = IoUtil.read(reader); + log.info(bodyContent); + } catch (IOException e) { + e.printStackTrace(); + } + } + HttpHeaders headers = new HttpHeaders(); + Enumeration enumeration = request.getHeaderNames(); + while (enumeration.hasMoreElements()) { + String name = enumeration.nextElement(); + String value = request.getHeader(name); + headerParam.put(name, value); + headers.add(name, value); + } + //headers.add("Content-Type", "application/json;charset=UTF-8"); + HttpEntity http = new HttpEntity<>(bodyContent, headers); + return http; + } + + +}