You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

312 lines
12 KiB
Java

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package com.docus.bgts.handler;
import com.alibaba.fastjson.JSON;
import com.docus.bgts.entity.CanlcelDto;
import com.docus.bgts.entity.ReportDownDto;
import com.docus.bgts.entity.ReportDownPatientDto;
import com.docus.bgts.entity.ReportDownScanFileDto;
import com.docus.bgts.enums.Codes;
import com.docus.bgts.facade.IAfCollectTaskService;
import com.docus.bgts.facade.IMrReportErrorService;
import com.docus.bgts.utils.FileUtils;
import com.docus.bgts.utils.HttpUtils;
import com.docus.bgts.utils.ResultUtils;
import com.docus.bgts.utils.XmlUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.springframework.beans.factory.annotation.Autowired;
import javax.jws.WebService;
import java.io.ByteArrayInputStream;
import java.io.StringReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@WebService
public class TBasicWebService implements ITBasicWebService {
@Autowired
IAfCollectTaskService afCollectTaskService;
@Autowired
IMrReportErrorService mrReportErrorService;
private Logger logger = LogManager.getLogger(TBasicWebService.class);
private Element getElementFromString(String s) {
SAXReader builder = new SAXReader();
Document doc = null;
Element element = null;
try {
doc = builder.read(new StringReader(s));
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (doc != null) {
element = doc.getRootElement();
}
return element;
}
@Override
public String pushSurveyReport(String xml) {
try {
logger.info("报告推送入参:" + xml);
// 1、创建document对象
// Document document = DocumentHelper.createDocument();
// Element rootElement = getElementFromString(xml);
// document.setRootElement(rootElement);
xml = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>" + xml;
//解析XML
XmlUtils xmlUtils = new XmlUtils(new ByteArrayInputStream(xml.getBytes("UTF-8")));
// 获取患者主索引号
String empId = String.valueOf(FileUtils.getJsonByName(Codes.EMP_ID.getMessage()));
Map<String, Object> headMap = new HashMap<>();
headMap.put("Content-Type", "application/json");
ReportDownDto reportDownDto;
//获取数据存在的节点节点
Element elementRoot = xmlUtils.getMsgElement();
//key
String serialnmnKey = String.valueOf(FileUtils.getJsonByName("serialnum"));
// String empId = String.valueOf(FileUtils.getJsonByName("indexFlag"));
String filetitleKey = String.valueOf(FileUtils.getJsonByName("filetitle"));
String downurlKey = String.valueOf(FileUtils.getJsonByName("downurl"));
ReportDownScanFileDto reportDownScanFileDto;
ReportDownPatientDto reportDownPatientDto;
//临时资料存储
String serialnum;
String filetitle;
String downurl;
String collectorid = String.valueOf(FileUtils.getJsonByName("collectorid"));
// String collectorid = getCollectorid(elementList.get(0));
// String assortid = String.valueOf(FileUtils.getJsonByName("assortid"));
String assortid = getAssortid(elementRoot);
reportDownDto = new ReportDownDto();
reportDownDto.setCollectorid(collectorid);
reportDownDto.setAssortid(assortid);
//获取基本数据信息
List<ReportDownScanFileDto> reportDownDtoArr = new ArrayList<>();
reportDownPatientDto = new ReportDownPatientDto();
// String empIdText = elementList.get(0).element(empId).getText();
//获取jzh
String empIdText = getElementText(elementRoot, empId.split(","));
//获取采集流水号
serialnum = getElementText(elementRoot, serialnmnKey.split(","));
reportDownPatientDto.setJzh(empIdText);
logger.info("患者主索引号:" + empIdText);
reportDownDto.setPatient(reportDownPatientDto);
List<Element> elementList = xmlUtils.getJsonByName(elementRoot);
for (Element element : elementList) {
// serialnum = element.element(serialnmnKey).getText();
// filetitle = element.element(filetitleKey).getText();
filetitle = getElementText(element, filetitleKey.split(","));
// downurl = element.element(downurlKey).getText();
downurl = getElementText(element, downurlKey.split(","));
downurl = handleDownUrl(downurl);
reportDownScanFileDto = new ReportDownScanFileDto();
reportDownScanFileDto.setDownurl(downurl);
reportDownScanFileDto.setFiletitle(filetitle);
reportDownScanFileDto.setSerialnum(serialnum);
reportDownScanFileDto.setFilesource(Integer.parseInt(String.valueOf(FileUtils.getJsonByName("filesource"))));
reportDownScanFileDto.setFilestoragetype(Integer.parseInt(String.valueOf(FileUtils.getJsonByName("filestoragetype"))));
//判断是否为base64
if (reportDownScanFileDto.getDownurl().length() > 200) {
reportDownScanFileDto.setFiletype(2);
}
reportDownDtoArr.add(reportDownScanFileDto);
}
if (reportDownDtoArr.size() <= 0) {
throw new RuntimeException("不存在必要的基本信息");
} else {
reportDownDto.setScanfiles(reportDownDtoArr);
}
//插入文件af_collect_task表数据
afCollectTaskService.insert(reportDownDto);
String post = "";
logger.info("--------执行上传功能----------");
Map params = JSON.parseObject(JSON.toJSONString(reportDownDto), Map.class);
logger.info("上传参数:" + params);
post = HttpUtils.post(String.valueOf(FileUtils.getJsonByName(Codes.UPLOAD.getMessage())), headMap, params);
// String post = HttpUtils.post(String.valueOf(FileUtils.getJsonByName(Codes.UPLOAD.getMessage())), map, headMap);
if (StringUtils.isBlank(post)) {
logger.info("--------上传时出现错误,可能是文件服务没有启动----------");
throw new RuntimeException("上传时出现错误,可能是文件服务没有启动");
}
Map resMap = JSON.parseObject(post, Map.class);
if (String.valueOf(resMap.get("code")).equals("500")) {
throw new RuntimeException(String.valueOf(resMap.get("msg")));
} else {
return ResultUtils.success().asXML();
}
} catch (RuntimeException e) {
e.printStackTrace();
mrReportErrorService.insert(xml);
return ResultUtils.fail(e.getMessage());
} catch (Exception e) {
e.printStackTrace();
mrReportErrorService.insert(xml);
return ResultUtils.fail(e.getMessage());
}
}
/**
* 特殊处理字符
*
* @param downurl
* @return
*/
private String handleDownUrl(String downurl) {
String url = "";
int pdfIndex = -1;
int httpIndex = downurl.indexOf("http");
if (httpIndex == -1) {
return downurl;
}
if (downurl.indexOf(".pdf") == -1) {
if (downurl.indexOf(".PDF") == -1) {
if (downurl.indexOf(".jpg") == -1) {
if (downurl.indexOf(".JPG") == -1) {
if (downurl.indexOf(".png") == -1) {
if (downurl.indexOf(".PNG") == -1) {
return downurl;
} else {
pdfIndex = downurl.indexOf(".PNG") + 4;
}
} else {
pdfIndex = downurl.indexOf(".png") + 4;
}
} else {
pdfIndex = downurl.indexOf(".JPG") + 4;
}
} else {
pdfIndex = downurl.indexOf(".jpg") + 4;
}
} else {
pdfIndex = downurl.indexOf(".PDF") + 4;
}
} else {
pdfIndex = downurl.indexOf(".pdf") + 4;
}
url = downurl.substring(httpIndex, pdfIndex);
return url;
}
@Override
public String EmrDocumentArchive(String xml) {
return pushSurveyReport(xml);
}
@Override
public String EmrDocumentArchiveCancel(String xml) {
try {
logger.info("--------------撤销接收到xml--------------");
logger.info(xml);
//解析xml并提取CanlcelDto对象
CanlcelDto canlcelDto = getCanlCelDtoByXml(xml);
//调用上传服务撤销功能
afCollectTaskService.invokeRepoalFile(canlcelDto);
logger.info("--------------撤销操作完成-------------");
return ResultUtils.success().asXML();
} catch (RuntimeException e) {
logger.info("发送错误:" + e.getMessage());
mrReportErrorService.insert(xml);
e.printStackTrace();
return ResultUtils.fail(e.getMessage());
} catch (Exception e) {
logger.info("发送错误:" + e.getMessage());
e.printStackTrace();
mrReportErrorService.insert(xml);
return ResultUtils.fail(e.getMessage());
}
}
/**
* 解析xml返回CanlcelDto对象
*
* @param xml
* @return
*/
private CanlcelDto getCanlCelDtoByXml(String xml) throws UnsupportedEncodingException {
XmlUtils xmlUtils = new XmlUtils(new ByteArrayInputStream(xml.getBytes("UTF-8")));
//采集流水号所在位置目录
List<String> repealDirectory = (List<String>) FileUtils.getJsonByName("repealDirectory");
Element element = xmlUtils.getElement(repealDirectory);
String repealSerialnum = String.valueOf(FileUtils.getJsonByName("repealSerialnum"));
Element repealSerialnumElement = element.element(repealSerialnum);
CanlcelDto canlcelDto = new CanlcelDto();
if (null == repealSerialnumElement || StringUtils.isBlank(repealSerialnumElement.getText())) {
throw new RuntimeException(repealSerialnum + "标签必须有值");
} else {
String serialnum = repealSerialnumElement.getText();
logger.info("当前流水号为:" + serialnum);
canlcelDto.setSerialnum(serialnum);
}
return canlcelDto;
}
/**
* 获取key内容
*
* @param element
* @param args
* @return
*/
private String getElementText(Element element, String[] args) {
String resText = "";
Element byElement = (Element) element.clone();
Element byElement1 = null;
String[] split;
for (String arg : args) {
split = arg.split("&");
if (split.length > 1) {
for (String spi : split) {
byElement1 = byElement.element(spi);
if (byElement1 != null && StringUtils.isNotBlank(byElement1.getText())) {
break;
}
}
} else {
byElement1 = byElement.element(arg);
}
if (byElement1 != null) {
resText = byElement1.getText();
} else {
break;
}
}
return resText;
}
/**
* 获取系统id
*
* @return
*/
private String getAssortid(Element element) {
String assortId = String.valueOf(FileUtils.getJsonByName("assortid"));
if (StringUtils.isBlank(assortId)) {
String assortIdKey = String.valueOf(FileUtils.getJsonByName("assortidParamKey"));
if (StringUtils.isBlank(assortIdKey)) {
assortId = "";
} else {
assortId = element.element(assortIdKey).getText();
}
}
return assortId;
}
}