package com.jiashi.controller; import com.google.gson.Gson; import com.jiashi.CommonResult; import com.jiashi.FileUploader; import com.jiashi.dao.DataQuery; import com.jiashi.service.CardInfo; import com.jiashi.service.FormField; import com.jiashi.service.Picture; import com.jiashi.service.UploadInfo; import com.sun.xml.internal.ws.util.CompletedFuture; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import org.springframework.util.CollectionUtils; import org.springframework.util.ObjectUtils; import org.springframework.util.StringUtils; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import java.io.File; import java.io.IOException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.List; import java.util.Objects; import java.util.concurrent.*; /** * @author YongBin Wen * @date 2024/9/26 15:32 */ @RestController @Slf4j @Api(tags = "广州市一联众同步接口") @RequestMapping("/gzsy/lianzhong") public class LianZhongSyncController { @Autowired private DataQuery dataQuery; ExecutorService executor = Executors.newFixedThreadPool(10); @ApiOperation(value = "根据病案号和出院日期(yyyy-MM-dd)同步联众文件数据 ") @GetMapping("/sync/byInpNoAndDisDate") public CommonResult syncByInpNoAndDisDate(@RequestParam("inpatientNo") String inpatientNo, @RequestParam("disDate") String disDate) { try { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); List cardInfos = dataQuery.dateQueryByInpNo(inpatientNo); CardInfo cardInfo = null; for (CardInfo info : cardInfos) { if (disDate.equals(sdf.format(info.getOutdate()))) { cardInfo = info; break; } } if (Objects.isNull(cardInfo)) { return CommonResult.failed("未找到 cardInfo 信息!"); } sync(cardInfo); return CommonResult.success("同步完成!"); } catch (Exception ex) { log.error(ex.getMessage(), ex); return CommonResult.failed(ex.getMessage()); } } @ApiOperation(value = "联众图片数据同步检查") @GetMapping("/sync/checkReport") public CommonResult syncCheckReport() { try { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); List cardInfos = dataQuery.dateQuery(2); if (CollectionUtils.isEmpty(cardInfos)) { return CommonResult.success("没有可检查的患者,状态=2"); } StringBuilder stringBuilder = new StringBuilder(); List> futures=new ArrayList<>(); for (CardInfo cardInfo : cardInfos) { Future future = executor.submit(() -> check(cardInfo)); futures.add(future); } for (Future future : futures) { String result = future.get(); if (!ObjectUtils.isEmpty(result)) { stringBuilder.append(result).append(";\n"); } } log.info("检查结果:{}",stringBuilder); return CommonResult.success(stringBuilder.toString()); } catch (Exception ex) { log.error(ex.getMessage(), ex); return CommonResult.failed(ex.getMessage()); } } private String check(CardInfo cardInfo) throws IOException { String dir = "c:\\pic-check\\" + cardInfo.getId(); // 创建File对象 File directory = new File(dir); try { String uniUrl = "http://192.168.8.74"; List pictures = dataQuery.getPictures(cardInfo.getId()); // 判断目录是否存在 if (!directory.exists()) { // 目录不存在,创建目录 boolean created = directory.mkdirs(); if (created) { log.info("目录创建成功:" + dir); } else { log.info("目录创建失败:" + dir); } } List faultNames = new ArrayList<>(); for (Picture picture : pictures) { String cmd = "C:\\Debug\\lianzhong.exe 003 192.168.8.74 " + cardInfo.getId() + " " + picture.getPicid() + " " + cardInfo.getPatno() + " " + cardInfo.getOutdateStr() + " " + picture.getPicname() + " " + picture.getCheckUpFileUrl() + " " + uniUrl + " " + picture.getRotatedegree(); File pictureFile = new File(picture.getCheckUpFileUrl()); int tryNum = 0; while (tryNum <= 3) { tryNum++; try { getPic(cmd); if (pictureFile.exists()) { break; } } catch (Exception ex) { // no } } if (!pictureFile.exists()) { faultNames.add(picture.getPicname()); } } // 删除文件 FileUploader.deleteFolder(directory); if (faultNames.isEmpty()) { return null; } String join = String.join(",", faultNames); String result = "id" + cardInfo.getId() + ",住院号:" + cardInfo.getPatno() + ",出院日期:" + cardInfo.getOutdateStr() + ",文件:" + join + " 缺失"; return result; } catch (Exception ex) { log.error("检查出错:" + ex.getMessage(), ex); FileUploader.deleteFolder(directory); return "住院号:" + cardInfo.getPatno() + " 出院日期:" + cardInfo.getOutdateStr() + " 检查失败!"; } } private void getPic(String cmd) throws Exception { java.lang.Process process = java.lang.Runtime.getRuntime().exec(cmd);//执行命令生成cube process.waitFor(); } @ApiOperation(value = "根据病案号和出院日期(yyyy-MM-dd),更新联众文件数据状态为0 ,需要重新同步 ") @GetMapping("/updateState/byInpNoAndDisDate") public CommonResult updateStateByInpNoAndDisDate(@RequestParam("inpatientNo") String inpatientNo, @RequestParam("disDate") String disDate) { try { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); List cardInfos = dataQuery.dateQueryByInpNo(inpatientNo); CardInfo cardInfo = null; for (CardInfo info : cardInfos) { if (disDate.equals(sdf.format(info.getOutdate()))) { cardInfo = info; break; } } if (Objects.isNull(cardInfo)) { return CommonResult.failed("未找到 cardInfo 信息!"); } dataQuery.updateBatchState(cardInfo, 0); return CommonResult.success("更新完成!"); } catch (Exception ex) { log.error(ex.getMessage(), ex); return CommonResult.failed(ex.getMessage()); } } public void sync(CardInfo cardInfo) throws IOException { String dir = "c:\\pic-makup\\" + cardInfo.getId(); // 创建File对象 File directory = new File(dir); try { String uniUrl = "http://192.168.8.74"; List pictures = dataQuery.getPictures(cardInfo.getId()); if (pictures == null || pictures.size() == 0) { //如果是空的则不同步 dataQuery.updateBatchState(cardInfo, 5); return; } // 判断目录是否存在 if (!directory.exists()) { // 目录不存在,创建目录 boolean created = directory.mkdirs(); if (created) { log.info("目录创建成功:" + dir); } else { log.info("目录创建失败:" + dir); } } List futures = new ArrayList<>(); for (Picture picture : pictures) { Future future = executor.submit(() -> { try { String cmd = "C:\\Debug\\lianzhong.exe 003 192.168.8.74 " + cardInfo.getId() + " " + picture.getPicid() + " " + cardInfo.getPatno() + " " + cardInfo.getOutdateStr() + " " + picture.getPicname() + " " + picture.getMakeUpFileUrl() + " " + uniUrl + " " + picture.getRotatedegree(); // log.info(cmd); java.lang.Process process = java.lang.Runtime.getRuntime().exec(cmd);//执行命令生成cube process.waitFor(); File pictureFile = new File(picture.getMakeUpFileUrl()); if (!pictureFile.exists()) { log.error("图片转换失败,命令为:{}", cmd); } } catch (Exception e) { log.error(e.getMessage(), e); e.printStackTrace(); } }); futures.add(future); } for (Future future : futures) { try { future.get(); } catch (InterruptedException e) { throw new RuntimeException(e); } catch (ExecutionException e) { throw new RuntimeException(e); } } boolean complete = true; List files = new ArrayList<>(); List uploadInfos = new ArrayList<>(); for (Picture picture : pictures) { File pictureFile = new File(picture.getMakeUpFileUrl()); if (!pictureFile.exists()) { complete = false; continue; } files.add(pictureFile); UploadInfo uploadInfo = new UploadInfo(cardInfo.getPatno(), cardInfo.getOutdateStr2(), picture.getPicname(), picture.getPicname(), picture.getPickind(), cardInfo.getId(), cardInfo.getPatname(), cardInfo.getIndateStr(), cardInfo.getPatsex(), cardInfo.getPatnum(), cardInfo.getIcdecode11(), cardInfo.getIcdename11(), cardInfo.getMjwesticde(), cardInfo.getMjwestname(), cardInfo.getOutdeptname(), cardInfo.getPatciticard(), cardInfo.getOutwardname(), cardInfo.getIndeptname(), cardInfo.getIndeptcode(), cardInfo.getGestno(), cardInfo.getPatbirthdayStr()); uploadInfos.add(uploadInfo); } if (files.isEmpty()) { dataQuery.updateBatchState(cardInfo, 4); // 删除文件 FileUploader.deleteFolder(directory); return; } // 额外的表单字段参数 List params = new ArrayList<>(); String s = new Gson().toJson(uploadInfos); params.add(new FormField("uploadFileParams", s)); log.info("请求参数:" + cardInfo.getPatno()); // 上传 CommonResult commonResult = FileUploader.uploadFilesWithParams(files, "http://192.168.161.102:9511/fileUploadJpg", params); if (commonResult.getCode() == 0) { dataQuery.updateBatchState(cardInfo, 3); } if (!complete) { // 不完整 dataQuery.updateBatchState(cardInfo, 2); } // 删除文件 FileUploader.deleteFolder(directory); } catch (IOException ex) { if (directory.exists()) { FileUploader.deleteFolder(directory); } throw ex; } } }