package com.jiashi.service; import com.google.gson.Gson; import com.jiashi.CommonResult; import com.jiashi.FileUploader; import com.jiashi.dao.DataQuery; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import javax.annotation.PostConstruct; import java.io.File; import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Future; import java.util.function.Function; import java.util.stream.Collectors; @Component @Slf4j public class UpdateService { @Autowired private DataQuery dataQuery; public List updateData(){ List cardInfos = dataQuery.dateQuery(); dataQuery.updateBatchState(cardInfos,1); return cardInfos; } //state 状态标识。0 未开始, 1, 正在进行, 3. 已经成功。 4. 失败。5. 不需要上传的 @PostConstruct public void upload() { String uniUrl = "http://192.168.8.74"; List cardInfos = null; ExecutorService executor2 = Executors.newFixedThreadPool(10); ExecutorService executor = Executors.newFixedThreadPool(90); do { try{ cardInfos = this.updateData(); List futures2 = new ArrayList<>(); for (CardInfo cardInfo : cardInfos) { log.info("开始同步"+cardInfo.getPatno()); Future future2 = executor2.submit(()->{ String dir = "c:\\pic\\"+cardInfo.getId(); // 创建File对象 File directory = new File(dir); try{ 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.getFileUrl() + " " + uniUrl + " " + picture.getRotatedegree(); // log.info(cmd); java.lang.Process process = java.lang.Runtime.getRuntime().exec(cmd);//执行命令生成cube process.waitFor(); File pictureFile = new File(picture.getFileUrl()); 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.getFileUrl()); 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()) { log.info("获取图片文件失败,cardInfoId:{}",cardInfo.getId()); dataQuery.updateBatchState(cardInfo,4); // 删除文件 FileUploader.deleteFolder(directory); return; } try { Map uploadInfosMap = uploadInfos.stream().collect(Collectors.toMap(UploadInfo::getUploadFileName, Function.identity())); boolean success = true; // 上传 int totalSize = files.size(); int batchSize = 500; if (totalSize > batchSize) { for (int i = 0; i < totalSize; i += batchSize) { ArrayList batch = new ArrayList<>(); List uploadInfoParams = new ArrayList<>(); // 计算当前批次的结束索引 int end = Math.min(i + batchSize, totalSize); for (int j = i; j < end; j++) { batch.add(files.get(j)); uploadInfoParams.add(uploadInfosMap.get(files.get(j).getName())); } // 额外的表单字段参数 List params = new ArrayList<>(); String s = new Gson().toJson(uploadInfoParams); params.add(new FormField("uploadFileParams", s)); log.info("请求参数:" + cardInfo.getPatno()); CommonResult commonResult = FileUploader.uploadFilesWithParams(batch, "http://192.168.161.102:9511/batchFileUploadJpg", params); boolean res = commonResult.getCode() == 0; success = success && res; } } else { // 额外的表单字段参数 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); success = commonResult.getCode() == 0; } if(success){ dataQuery.updateBatchState(cardInfo,3); }else{ dataQuery.updateBatchState(cardInfo,4); } } catch (Exception e) { dataQuery.updateBatchState(cardInfo,4); log.error(e.getMessage(),e); } if (!complete) { // 不完整 dataQuery.updateBatchState(cardInfo,2); } // 删除文件 FileUploader.deleteFolder(directory); }catch (Exception e){ dataQuery.updateBatchState(cardInfo,4); log.error(e.getMessage(),e); if (directory.exists()) { FileUploader.deleteFolder(directory); } } }); futures2.add(future2); } for (Future future : futures2) { try { future.get(); } catch (InterruptedException e) { throw new RuntimeException(e); } catch (ExecutionException e) { throw new RuntimeException(e); } } }catch(Exception e) { log.error(e.getMessage(),e); } }while (cardInfos != null && cardInfos.size() > 0) ; } }