package com.sercurityControl.proteam.util; import com.securityControl.common.core.utils.StringUtils; import com.securityControl.common.core.utils.bean.BeanUtils; import com.sercurityControl.proteam.dutyTask.domain.NoticeVioEntity; import com.sercurityControl.proteam.dutyTask.domain.NoticeVioEntity2; import com.sercurityControl.proteam.dutyTask.service.SuperStatisticsService; import lombok.extern.slf4j.Slf4j; import org.apache.commons.collections4.CollectionUtils; import org.springframework.beans.factory.annotation.Value; import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; import org.springframework.stereotype.Component; import javax.annotation.Resource; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.Objects; import java.util.concurrent.Callable; import java.util.concurrent.Future; /** * @author 10488 */ @Component @Slf4j public class HandleDataUtil { @Resource(name = "testTaskExecutor") private ThreadPoolTaskExecutor testTaskExecutor; @Resource(name = "SuperStatisticsService") private SuperStatisticsService service; @Value("${file.upload_path}") private String upload_path; public List handleData(List list,NoticeVioEntity noticeVioEntity) { String type = noticeVioEntity.getType(); // 多线程执行操作,存放的数据集合 List futureList = new ArrayList<>(); List newList = new ArrayList<>(); try { for (NoticeVioEntity vo : list) { Future future = testTaskExecutor.submit(new Callable() { @Override public NoticeVioEntity2 call() throws Exception { NoticeVioEntity2 noticeVioEntity2 = new NoticeVioEntity2(); List imgList = service.getVoiImgs(vo.getVoiId()); byte[] imageFromNetByUrl = null; BeanUtils.copyProperties(vo, noticeVioEntity2); if (CollectionUtils.isNotEmpty(imgList)) { for (int i1 = 0; i1 < imgList.size(); i1++) { if (new File(upload_path + File.separator + imgList.get(i1)).exists()) { byte[] nweBytes = Objects.equals("2",type) ? PicUtils.compressPicForScale(FileTobyte(new File(upload_path + File.separator + imgList.get(i1))), 300): FileTobyte(new File(upload_path + File.separator + imgList.get(i1))); if (i1 == 0) { noticeVioEntity2.setDisPhoto(nweBytes); } else if (i1 == 1) { noticeVioEntity2.setDisPhoto2(nweBytes); } else if (i1 == 2) { noticeVioEntity2.setDisPhoto3(nweBytes); } else if (i1 == 3) { noticeVioEntity2.setDisPhoto4(nweBytes); } else if (i1 == 4) { noticeVioEntity2.setDisPhoto5(nweBytes); } else if (i1 == 5) { noticeVioEntity2.setDisPhoto6(nweBytes); } else if (i1 == 6) { noticeVioEntity2.setDisPhoto7(nweBytes); } else if (i1 == 7) { noticeVioEntity2.setDisPhoto8(nweBytes); } else if (i1 == 8) { noticeVioEntity2.setDisPhoto9(nweBytes); } else if (i1 == 9) { noticeVioEntity2.setDisPhoto10(nweBytes); } } } } return noticeVioEntity2; } }); futureList.add(future); } for (Future future : futureList) { NoticeVioEntity2 NoticeVioEntity2 = future.get(); newList.add(NoticeVioEntity2); } } catch (Exception e) { log.error("数据处理异常",e); } return newList; } public static byte[] FileTobyte(File file) { FileInputStream fileInputStream = null; byte[] imgData = null; try { imgData = new byte[(int) file.length()]; //read file into bytes[] fileInputStream = new FileInputStream(file); fileInputStream.read(imgData); } catch (IOException e) { e.printStackTrace(); } finally { if (fileInputStream != null) { try { fileInputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } return imgData; } }