|
|
package com.ann.service.impl;
|
|
|
|
|
|
import com.alibaba.fastjson.JSON;
|
|
|
import com.ann.entity.constant.AliasName;
|
|
|
import com.ann.entity.constant.InterfaceName;
|
|
|
import com.ann.entity.filing.ArchiveDetail;
|
|
|
import com.ann.entity.filing.MessageSubordinate;
|
|
|
import com.ann.entity.filing.dto.MessageDto;
|
|
|
import com.ann.entity.interfaceEntity.ArchiveMaster;
|
|
|
import com.ann.entity.interfaceEntity.MessageLog;
|
|
|
import com.ann.entity.interfaceEntity.ScanningFile;
|
|
|
import com.ann.service.ArchiveDetailService;
|
|
|
import com.ann.service.ArchiveMasterService;
|
|
|
import com.ann.service.MessageService;
|
|
|
import com.ann.service.MessageSubordinateService;
|
|
|
import com.ann.utils.*;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
import java.io.File;
|
|
|
import java.util.Date;
|
|
|
import java.util.List;
|
|
|
import java.util.Objects;
|
|
|
|
|
|
/**
|
|
|
* @Author: LeiJiaXin
|
|
|
* @Date: 2019/7/11 22:00
|
|
|
*/
|
|
|
@Service
|
|
|
public class QueueService {
|
|
|
|
|
|
@Autowired
|
|
|
MessageSubordinateService messageSubordinateService;
|
|
|
@Autowired
|
|
|
ArchiveMasterService archiveMasterService;
|
|
|
@Autowired
|
|
|
ArchiveDetailService archiveDetailService;
|
|
|
@Autowired
|
|
|
MessageService messageService;
|
|
|
|
|
|
//---------------------定时器
|
|
|
public void doSomething(List<MessageSubordinate> all) throws Exception {
|
|
|
if (all.size() > 0) {
|
|
|
for (MessageSubordinate messageSubordinate : all) {
|
|
|
Integer status = 2;
|
|
|
ArchiveDetail archiveDetail = null;
|
|
|
try {
|
|
|
// 解析内容
|
|
|
MessageLog messageLog = JSON.parseObject(messageSubordinate.getContentJson(), MessageLog.class);
|
|
|
if (messageLog != null) {
|
|
|
String outJson = messageLog.getOutJson();
|
|
|
if (outJson != null) {
|
|
|
MessageDto messageDto = JSON.parseObject(outJson, MessageDto.class);
|
|
|
if (messageDto.getSource().equals(InterfaceName.NURSE_DATA) && (messageDto.getReportAddress().endsWith(".jpg")
|
|
|
|| messageDto.getReportAddress().endsWith(".JPG") )){
|
|
|
messageSubordinate.setRemark("住院号:" + messageDto.getInpNo() + "pdf路径为空(可能出现下载问题)。。。");
|
|
|
} else {
|
|
|
ArchiveMaster archiveMaster = archiveMasterService.findByInpNoAndVisitIdAndIsValid(messageDto.getInpNo(), messageDto.getVisitId());
|
|
|
if (archiveMaster != null) {
|
|
|
if(!Objects.equals(messageDto.getType(),AliasName.PATHOLOGY_REPORT)){
|
|
|
if(archiveMaster.getArchiveState().equals("初审") || archiveMaster.getArchiveState().equals("已归档")
|
|
|
|| archiveMaster.getArchiveState().equals("复审退回")
|
|
|
|| archiveMaster.getArchiveState().equals("病案召回") ){
|
|
|
messageSubordinate.setStatus(3);
|
|
|
messageSubordinateService.save(messageSubordinate);
|
|
|
continue;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
String address = messageDto.getReportAddress();
|
|
|
if (address != null && (!address.equals(""))) {
|
|
|
// 如果是老数据
|
|
|
if(Objects.equals(messageDto.getIsOld(),"0")){
|
|
|
archiveDetail = new ArchiveDetail(archiveMaster.getId(), new Date(), messageDto.getType(), messageDto.getSource(), messageDto.getApplyId(),messageDto.getDetailType(),1);
|
|
|
archiveDetailService.save(archiveDetail);
|
|
|
}else{
|
|
|
// 查询库中是否存在 如果存在就更新
|
|
|
archiveDetail = archiveDetailService.isExit(archiveMaster.getId(), messageDto.getType(), messageDto.getSource(), messageDto.getApplyId());
|
|
|
if (archiveDetail == null) {
|
|
|
archiveDetail = new ArchiveDetail(archiveMaster.getId(), new Date(), messageDto.getType(), messageDto.getSource(), messageDto.getApplyId(),
|
|
|
messageDto.getDetailType(),1);
|
|
|
archiveDetailService.save(archiveDetail);
|
|
|
}
|
|
|
}
|
|
|
String pdfPath = null;
|
|
|
String emrPath = "";
|
|
|
|
|
|
if(messageDto.getSource().equals(InterfaceName.NURSE_DATA)){
|
|
|
//医生 护理单独做处理
|
|
|
if(archiveMaster.getEmrNurseCmt() == null){
|
|
|
archiveMaster.setEmrNurseCmt("提交");
|
|
|
archiveMasterService.save(archiveMaster);
|
|
|
}
|
|
|
String a = "";
|
|
|
if(messageDto.getApplyId().lastIndexOf("_") != -1){
|
|
|
a = messageDto.getApplyId().substring(0, messageDto.getApplyId().indexOf(".pdf"));
|
|
|
}else{
|
|
|
a = address.substring(0, address.lastIndexOf("_"));
|
|
|
}
|
|
|
emrPath = messageDto.getType() + "~" + (a.substring(a.lastIndexOf("_") + 1, a.length())) + "~";
|
|
|
|
|
|
} else if(messageDto.getSource().equals(InterfaceName.DOCTOR_DATA)){
|
|
|
//护士 护理单独做处理
|
|
|
if(archiveMaster.getEmrDoctorCmt() == null){
|
|
|
archiveMaster.setEmrDoctorCmt("提交");
|
|
|
archiveMasterService.save(archiveMaster);
|
|
|
}
|
|
|
emrPath = messageDto.getType() + "~" + messageDto.getCaptionDateTime() + "~";
|
|
|
}else{
|
|
|
// 20200518 给重症系统的文件排序
|
|
|
if(messageDto.getType().equals(AliasName.ICU_REPORT)){
|
|
|
emrPath = messageDto.getDetailType() + "~" + messageDto.getCaptionDateTime() + "~";
|
|
|
}
|
|
|
}
|
|
|
|
|
|
File pdfFile = FileUtils.createFile("pdfs", messageDto.getInpNo(), messageDto.getVisitId(), archiveDetail.getId(), emrPath);
|
|
|
if (pdfFile != null) {
|
|
|
if (address.endsWith(".pdf") || address.endsWith(".PDF")) {
|
|
|
String path = DownloadUtils.downLoadPdfFile(pdfFile, address, messageDto.getType());
|
|
|
if (path != null) {
|
|
|
pdfPath = path;
|
|
|
}
|
|
|
} else {
|
|
|
if (Objects.equals(messageDto.getType(), AliasName.ICU_REPORT)) {
|
|
|
// 多个图片那么转成pdf
|
|
|
File imageFile = FileUtils.createDir("images", messageDto.getInpNo(), messageDto.getVisitId());
|
|
|
String imageFiles = DownloadUtils.downloadImageFiles(address, imageFile.getParent());
|
|
|
pdfPath = PdfUtils.imageDirToPdf(imageFiles, pdfFile);
|
|
|
} else {
|
|
|
// 一张图片转成pdf
|
|
|
File imageFile = FileUtils.createFile("images", messageDto.getInpNo(), messageDto.getVisitId(), archiveDetail.getId(), emrPath);
|
|
|
if (DownloadUtils.downloadImageFile(address, messageDto.getType(), imageFile)) {
|
|
|
pdfPath = PdfUtils.imagesToPdf(imageFile, pdfFile);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
//存入pdf地址
|
|
|
if (pdfPath != null) {
|
|
|
// 转成格式
|
|
|
archiveDetail.setPdfPath(pdfPath);
|
|
|
archiveDetailService.save(archiveDetail);
|
|
|
|
|
|
//更新messageLog的最后执行时间
|
|
|
Date xmlCreateTime = messageLog.getXmlCreateTime();
|
|
|
long beginTime = xmlCreateTime.getTime();
|
|
|
long endTime = new Date().getTime();
|
|
|
messageLog.setXmlExecutionTime(endTime - beginTime);
|
|
|
|
|
|
messageService.save(messageLog);
|
|
|
// 成功
|
|
|
status = 1;
|
|
|
} else {
|
|
|
status = 2;
|
|
|
messageSubordinate.setRemark("住院号:" + messageDto.getInpNo() + "pdf路径为空(可能出现下载问题)。。。");
|
|
|
if (archiveDetail.getId() != null) {
|
|
|
archiveDetailService.delete(archiveDetail.getId());
|
|
|
}
|
|
|
}
|
|
|
} else {
|
|
|
// 判断是否是扫描文件
|
|
|
if(messageDto.getType().equals(AliasName.SCANNING_FILE)){
|
|
|
String outContent = this.handlePdfFile(messageDto, archiveMaster.getId());
|
|
|
if(outContent == null){
|
|
|
status = 1;
|
|
|
//更新messageLog的最后执行时间
|
|
|
Date xmlCreateTime = messageLog.getXmlCreateTime();
|
|
|
long beginTime = xmlCreateTime.getTime();
|
|
|
long endTime = new Date().getTime();
|
|
|
messageLog.setXmlExecutionTime(endTime - beginTime);
|
|
|
|
|
|
messageService.save(messageLog);
|
|
|
}else{
|
|
|
messageSubordinate.setRemark(outContent);
|
|
|
}
|
|
|
|
|
|
}else{
|
|
|
messageSubordinate.setRemark("地址为空。。。。");
|
|
|
}
|
|
|
}
|
|
|
} else {
|
|
|
messageSubordinate.setRemark("住院号为:" + messageDto.getInpNo() + "的ArchiveMaster为空。。。。");
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
} catch (Exception e) {
|
|
|
// 设置参数
|
|
|
messageSubordinate.setRemark(e + "");
|
|
|
// 报错删掉患者报告详情信息
|
|
|
if (archiveDetail != null) {
|
|
|
if (archiveDetail.getId() != null) {
|
|
|
archiveDetailService.delete(archiveDetail.getId());
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
messageSubordinate.setStatus(status);
|
|
|
|
|
|
// 判断次数等于多少
|
|
|
if (messageSubordinate.getRuns() < 6 && messageSubordinate.getStatus() == 2) {
|
|
|
Integer runs = messageSubordinate.getRuns();
|
|
|
messageSubordinate.setRuns(runs + 1);
|
|
|
}
|
|
|
messageSubordinateService.save(messageSubordinate);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// 定时器:根据第一次 没有成功的消息 重新尝试
|
|
|
public void doSomethingByError(List<MessageSubordinate> all) throws Exception {
|
|
|
if (all.size() > 0) {
|
|
|
for (MessageSubordinate messageSubordinate : all) {
|
|
|
messageSubordinate.setStatus(0);
|
|
|
messageSubordinateService.save(messageSubordinate);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
public String handlePdfFile(MessageDto messageDto,String masterId) throws Exception {
|
|
|
try {
|
|
|
ScanningFile scanningFile = JSON.parseObject(messageDto.getObject().toString(), ScanningFile.class);
|
|
|
if (scanningFile == null) {
|
|
|
return "扫描文件解析实体错误。。。";
|
|
|
}
|
|
|
|
|
|
Object[] objects = WebServiceUtils.connect(scanningFile.getPath() + "\\" + scanningFile.getFileName());
|
|
|
if (objects == null) {
|
|
|
return "扫描文件下载图片出错了。。。";
|
|
|
}
|
|
|
|
|
|
byte[] bytes = (byte[]) objects[0];
|
|
|
String path = (scanningFile.getApplyId() != null ? scanningFile.getApplyId() : "") + "~" + (scanningFile.getNumber() != null ? scanningFile.getNumber() : "");
|
|
|
// 给临时图片填充内容
|
|
|
File imageTempFile = FileUtils.createFile("images", messageDto.getInpNo(), messageDto.getVisitId(), path);
|
|
|
File imageFile = FileUtils.getImageFileByByteArray(bytes, imageTempFile);
|
|
|
if (imageFile == null) {
|
|
|
return "扫描文件填充临时图片出错了。。。";
|
|
|
}
|
|
|
|
|
|
File pdfFile = FileUtils.createFile("pdfs", messageDto.getInpNo(), messageDto.getVisitId(), path);
|
|
|
String pdfPath = PdfUtils.imagesToPdf(imageFile, pdfFile);
|
|
|
//存入pdf地址
|
|
|
if (pdfPath == null) {
|
|
|
return "扫描文件image转成pdf出错了。。。";
|
|
|
}
|
|
|
|
|
|
// 如果存在 那就更新
|
|
|
ArchiveDetail archiveDetail = archiveDetailService.isExit(masterId, messageDto.getType(), messageDto.getSource(), messageDto.getApplyId() + "~" + (scanningFile.getNumber() != null ? scanningFile.getNumber() : ""));
|
|
|
if (archiveDetail == null) {
|
|
|
archiveDetail = new ArchiveDetail(masterId, new Date(), messageDto.getType(), messageDto.getSource(), messageDto.getApplyId() + "~" + (scanningFile.getNumber() != null ? scanningFile.getNumber() : ""), pdfPath);
|
|
|
archiveDetailService.save(archiveDetail);
|
|
|
}
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
return e + "";
|
|
|
}
|
|
|
|
|
|
return null;
|
|
|
}
|
|
|
}
|