package com.docus.bgts.handler; import com.alibaba.fastjson.JSON; import com.docus.bgts.entity.*; 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.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.Element; import org.dom4j.io.SAXReader; import org.slf4j.Logger; import org.slf4j.LoggerFactory; 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.*; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; @WebService public class TBasicWebService implements ITBasicWebService { @Autowired IAfCollectTaskService afCollectTaskService; @Autowired IMrReportErrorService mrReportErrorService; // private Object lock = new Object(); private final Logger logger = LoggerFactory.getLogger(getClass()); 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对象 xml = "" + xml; //解析XML XmlUtils xmlUtils = new XmlUtils(new ByteArrayInputStream(xml.getBytes("UTF-8"))); //获取数据存在的节点节点 Element elementRoot = xmlUtils.getMsgElement(); //key String serialnmnKey = String.valueOf(FileUtils.getJsonByName("serialnum")); String filetitleKey = String.valueOf(FileUtils.getJsonByName("filetitle")); String downurlKey = String.valueOf(FileUtils.getJsonByName("downurl")); // 获取患者主索引号 String empId = String.valueOf(FileUtils.getJsonByName(Codes.EMP_ID.getMessage())); //判断是否为撤销 String isJudge = String.valueOf(FileUtils.getJsonByName("isJudge")); if (StringUtils.isBlank(isJudge) || isJudge.equals("0")) { } else { //获取撤回路径 List judgeDirectory = (List) FileUtils.getJsonByName("judgeDirectory"); Element current = xmlUtils.getElement(judgeDirectory); //获取撤回标签 String judgeIndex = String.valueOf(FileUtils.getJsonByName("judgeIndex")); Element judgeIndexElement = current.element(judgeIndex); if (judgeIndexElement == null || StringUtils.isBlank(judgeIndexElement.getText()) || (!judgeIndexElement.getText().equals("1"))) { logger.info("------新增/修改------"); } else if (judgeIndexElement.getText().equals("1")) { logger.info("---------撤回--------"); CanlcelDto canlcelDto = new CanlcelDto(); String serialnum = String.valueOf(FileUtils.getJsonByName("serialnum")); String serialnumElement = getElementText(elementRoot, serialnum.split(",")); canlcelDto.setSerialnum(serialnumElement); //调用上传服务撤销功能 afCollectTaskService.invokeRepoalFile(canlcelDto); logger.info("----------当前操作完成--------------"); return ResultUtils.success().asXML(); } } Map headMap = new HashMap<>(); headMap.put("Content-Type", "application/json"); ReportDownDto reportDownDto; ReportDownScanFileDto reportDownScanFileDto; ReportDownPatientDto reportDownPatientDto; //临时资料存储 String serialnum; String filetitle; String downurl; String collectorid = String.valueOf(FileUtils.getJsonByName("collectorid")); String assortid = getAssortid(elementRoot); reportDownDto = new ReportDownDto(); reportDownDto.setCollectorid(collectorid); reportDownDto.setAssortid(assortid); //获取基本数据信息 List reportDownDtoArr = new ArrayList<>(); reportDownPatientDto = new ReportDownPatientDto(); //获取jzh String empIdText = getElementText(elementRoot, empId.split(",")); reportDownPatientDto.setJzh(empIdText); logger.info("患者主索引号:" + empIdText); reportDownDto.setPatient(reportDownPatientDto); List elementList = xmlUtils.getJsonByName(elementRoot); for (Element element : elementList) { filetitle = getElementText(element, filetitleKey.split(",")); downurl = getElementText(element, downurlKey.split(",")); downurl = handleDownUrl(downurl); //获取采集流水号 serialnum = getElementText(element, serialnmnKey.split(",")); 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表数据 // synchronized (lock) { afCollectTaskService.insert(reportDownDto); // } String post = ""; logger.info("--------执行上传功能----------"); Map params = JSON.parseObject(JSON.toJSONString(reportDownDto), Map.class); if (reportDownDto.getScanfiles().get(0).getFiletype() == 1) { 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 { logger.info("----------执行成功-----------"); return ResultUtils.success().asXML(); } } catch (RuntimeException e) { e.printStackTrace(); mrReportErrorService.insert(xml); logger.error(e.getMessage()); return ResultUtils.fail(e.getMessage()); } catch (Exception e) { e.printStackTrace(); mrReportErrorService.insert(xml); logger.error(e.getMessage()); return ResultUtils.fail(e.getMessage()); } } /** * 特殊处理字符 * * @param downurl * @return */ private String handleDownUrl(String downurl) { String url = ""; try { XmlUtils xmlUtils = new XmlUtils(new ByteArrayInputStream(downurl.getBytes("UTF-8"))); if (null != xmlUtils) { url = downurl; List urlReplaces = (List) FileUtils.getJsonByName("urlReplace"); for (String urlReplace : urlReplaces) { url = url.replaceAll(urlReplace, ""); } return url; } } catch (Exception e) { } return ""; } @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 = null; try { xmlUtils = new XmlUtils(new ByteArrayInputStream(xml.getBytes("UTF-8"))); } catch (Exception e) { e.printStackTrace(); } //采集流水号所在位置目录 List repealDirectory = (List) 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) { if (byElement1 == null) { byElement1 = byElement.element(spi); } else { byElement1 = byElement1.element(spi); } if (byElement1 != null && StringUtils.isNotBlank(byElement1.getText())) { break; } else { byElement1 = null; } } } else { if (byElement1 == null) { byElement1 = byElement.element(arg); } else { byElement1 = byElement1.element(arg); } } if (byElement1 != null) { resText = byElement1.getText(); } else { break; } } if (StringUtils.isBlank(resText)) { throw new RuntimeException(args[args.length - 1] + "的值不能为空"); } 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(); } String myAssortId = afCollectTaskService.getAssortIdByAssortId(assortId); if (StringUtils.isBlank(myAssortId)) { myAssortId = afCollectTaskService.getAssortIdByAssortName(assortId); if (StringUtils.isBlank(myAssortId)) { String uuid = UUID.randomUUID().toString().replaceAll("-", ""); ZdAssort zdAssort = new ZdAssort(); zdAssort.setAssortName(assortId); zdAssort.setAssortId(uuid); zdAssort.setEffective(1); afCollectTaskService.insertZdAssort(zdAssort); assortId = uuid; } else { assortId = myAssortId; } } else { assortId = myAssortId; } } return assortId; } }