| | |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.apache.commons.lang3.StringUtils; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.beans.factory.annotation.Value; |
| | | import org.springframework.web.bind.annotation.*; |
| | | |
| | | import javax.servlet.http.HttpServletRequest; |
| | | import javax.servlet.http.HttpServletResponse; |
| | | import java.io.File; |
| | | import java.io.IOException; |
| | | import java.util.Arrays; |
| | | import java.util.HashMap; |
| | | import java.util.Map; |
| | | import java.util.Random; |
| | | |
| | | /** |
| | | * OilRecord |
| | |
| | | @Autowired |
| | | private IOilRecordService oilRecordService; |
| | | |
| | | @Value("${init.local.image-path}") |
| | | private String localImagePath; |
| | | |
| | | @Value("${server.port:8080}") |
| | | private String serverPort; |
| | | |
| | | private Random random = new Random(); |
| | | |
| | | private String getRandomImagePath() { |
| | | File imageDir = new File(localImagePath); |
| | | if (!imageDir.exists() || !imageDir.isDirectory()) { |
| | | log.warn("本地图片目录不存在: {}", localImagePath); |
| | | return null; |
| | | } |
| | | |
| | | File[] imageFiles = imageDir.listFiles((dir, name) -> { |
| | | String lowerName = name.toLowerCase(); |
| | | return lowerName.endsWith(".jpg") || lowerName.endsWith(".jpeg") |
| | | || lowerName.endsWith(".png") || lowerName.endsWith(".bmp"); |
| | | }); |
| | | |
| | | if (imageFiles == null || imageFiles.length == 0) { |
| | | log.warn("本地图片目录中没有图片文件: {}", localImagePath); |
| | | return null; |
| | | } |
| | | |
| | | int randomIndex = random.nextInt(imageFiles.length); |
| | | File selectedFile = imageFiles[randomIndex]; |
| | | String fileName = selectedFile.getName(); |
| | | return "http://localhost:" + serverPort + "/cube/jyz/images/" + fileName; |
| | | } |
| | | |
| | | /** |
| | | * 分页列表查询 |
| | | */ |
| | |
| | | Page<OilRecord> page = new Page<>(pageNo, pageSize); |
| | | queryWrapper.orderByDesc("start_time"); |
| | | IPage<OilRecord> pageList = oilRecordService.page(page, queryWrapper); |
| | | // |
| | | // for(OilRecord tmpOilRecord:pageList.getRecords()){ |
| | | // try{ |
| | | // if(StringUtils.isNotEmpty(tmpOilRecord.getImgPath())){ |
| | | // String[] arr = tmpOilRecord.getImgPath().split(":"); |
| | | // tmpOilRecord.setImgPath(MultiMinioUtil.getObjectURL(arr[0],arr[1],3600)); |
| | | // } |
| | | // if(StringUtils.isNotEmpty(tmpOilRecord.getOutImgPath())){ |
| | | // String[] arr = tmpOilRecord.getOutImgPath().split(":"); |
| | | // tmpOilRecord.setOutImgPath(MultiMinioUtil.getObjectURL(arr[0],arr[1],3600)); |
| | | // } |
| | | // }catch (Exception ex){ |
| | | // log.error("生成s3资源链接地址失败",ex); |
| | | // } |
| | | // } |
| | | |
| | | for(OilRecord tmpOilRecord:pageList.getRecords()){ |
| | | try{ |
| | | if(StringUtils.isNotEmpty(tmpOilRecord.getImgPath())){ |
| | | String[] arr = tmpOilRecord.getImgPath().split(":"); |
| | | tmpOilRecord.setImgPath(MultiMinioUtil.getObjectURL(arr[0],arr[1],3600)); |
| | | String randomImagePath = getRandomImagePath(); |
| | | if(randomImagePath != null){ |
| | | tmpOilRecord.setImgPath(randomImagePath); |
| | | } |
| | | } |
| | | if(StringUtils.isNotEmpty(tmpOilRecord.getOutImgPath())){ |
| | | String[] arr = tmpOilRecord.getOutImgPath().split(":"); |
| | | tmpOilRecord.setOutImgPath(MultiMinioUtil.getObjectURL(arr[0],arr[1],3600)); |
| | | String randomImagePath = getRandomImagePath(); |
| | | if(randomImagePath != null){ |
| | | tmpOilRecord.setOutImgPath(randomImagePath); |
| | | } |
| | | } |
| | | }catch (Exception ex){ |
| | | log.error("生成s3资源链接地址失败",ex); |
| | | log.error("生成图片路径失败",ex); |
| | | } |
| | | } |
| | | |
| | | return Result.ok(pageList); |
| | | } |
| | | |