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.
LianzhongCollect/src/main/java/com/jiashi/service/UpdateService.java

159 lines
7.4 KiB
Java

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 org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.PostConstruct;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
@Component
@Slf4j
public class UpdateService {
@Autowired
private DataQuery dataQuery;
public List<CardInfo> updateData(){
List<CardInfo> cardInfos = dataQuery.dateQuery();
dataQuery.updateBatchState(cardInfos,1);
return cardInfos;
}
@PostConstruct
public void upload() {
String uniUrl = "http://192.168.8.74";
List<CardInfo> cardInfos = null;
ExecutorService executor2 = Executors.newFixedThreadPool(2);
ExecutorService executor = Executors.newFixedThreadPool(5);
do {
try{
cardInfos = this.updateData();
List<Future> futures2 = new ArrayList<>();
for (CardInfo cardInfo : cardInfos) {
log.info("开始同步"+cardInfo.getPatno());
Future future2 = executor2.submit(()->{
try{
List<Picture> pictures = dataQuery.getPictures(cardInfo.getId());
if(pictures==null||pictures.size()==0){
//如果是空的则不同步
dataQuery.updateBatchState(cardInfo,5);
return;
}
List<Future> futures = new ArrayList<>();
for (Picture picture : pictures) {
Future future = executor.submit(() -> {
try {
String dir = "d:\\pic\\"+picture.getFileid();
// 创建File对象
File directory = new File(dir);
// 判断目录是否存在
if (!directory.exists()) {
// 目录不存在,创建目录
boolean created = directory.mkdirs();
if (created) {
log.info("目录创建成功:" + dir);
} else {
log.info("目录创建失败:" + dir);
}
}
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();
} 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);
}
}
List<File> files = new ArrayList<>();
List<UploadInfo> uploadInfos = new ArrayList<>();
for(Picture picture : pictures){
files.add(new File(picture.getFileUrl()));
UploadInfo uploadInfo = new UploadInfo(cardInfo.getPatno(), cardInfo.getOutdateStr2(), picture.getPicname(), picture.getPicname(), picture.getPickind(), cardInfo.getId(), cardInfo.getPatname(), cardInfo.getIndateStr(), cardInfo.getPatsex());
uploadInfos.add(uploadInfo);
}
// 额外的表单字段参数
List<FormField> params = new ArrayList<>();
String s = new Gson().toJson(uploadInfos);
params.add(new FormField("uploadFileParams", s));
log.info("请求参数:"+s);
// 上传
try {
CommonResult commonResult = FileUploader.uploadFilesWithParams(files, "http://10.2.130.59:8712/api/downplatform/fileUploadJpg", params);
if(commonResult.getCode()==0){
dataQuery.updateBatchState(cardInfo,3);
}else{
dataQuery.updateBatchState(cardInfo,4);
log.error(commonResult.getMsg());
}
dataQuery.updateBatchState(cardInfo,4);
} catch (Exception e) {
dataQuery.updateBatchState(cardInfo,4);
log.error(e.getMessage(),e);
}
// 删除文件
// String dir = "d:\\pic\\"+cardInfo.getId();
// File file = new File(dir);
// FileUploader.deleteFolder(file);
}catch (Exception e){
dataQuery.updateBatchState(cardInfo,4);
log.error(e.getMessage(),e);
e.printStackTrace();
}
});
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) ;
}
}