refactor(record): 重构代码以提高可维护性和可测试性

- 将文件获取逻辑移至服务层,减少控制器层的复杂度
- 新增巡检报警记录和母排测温记录查询接口
IOT
zch 1 year ago
parent 23cc2e63b3
commit a712d9ae76

@ -101,4 +101,18 @@ public class RecordBusbarAlarmController extends BaseController
{ {
return toAjax(recordBusbarAlarmService.deleteRecordBusbarAlarmByObjIds(objIds)); return toAjax(recordBusbarAlarmService.deleteRecordBusbarAlarmByObjIds(objIds));
} }
/**
*
* @param recordBusbarAlarm
* @return
*/
@PreAuthorize("@ss.hasPermi('record:recordBusbarAlarm:list')")
@PostMapping("/recordBusbarAlarmList")
public AjaxResult recordBusbarAlarmList(RecordBusbarAlarm recordBusbarAlarm)
{
List<RecordBusbarAlarm> list = recordBusbarAlarmService.selectRecordBusbarAlarmList(recordBusbarAlarm);
return success(list);
}
} }

@ -119,43 +119,7 @@ public class RecordBusbarTempController extends BaseController
*/ */
@GetMapping("/getVisiblePhoto/{filePath}") @GetMapping("/getVisiblePhoto/{filePath}")
public ResponseEntity<FileSystemResource> getVisiblePhoto(@PathVariable String filePath) { public ResponseEntity<FileSystemResource> getVisiblePhoto(@PathVariable String filePath) {
// 获取配置文件中的路径application.yml的profile return recordBusbarTempService.getVisiblePhoto(filePath);
String myPath = RuoYiConfig.getMyPath();
// 构建文件的完整路径
String fileUrl = myPath + "/日志信息/可见光图像/" + filePath;
// 创建文件对象
File file = new File(fileUrl);
// 检查文件是否存在
if (file.exists()) {
// 创建文件系统资源对象包装文件http头不能使用file所以使用FileSystemResource
FileSystemResource resource = new FileSystemResource(file);
// 创建HttpHeaders对象用于设置响应头
HttpHeaders headers = new HttpHeaders();
// 设置文件长度
headers.setContentLength(file.length());
// 动态检测文件的MIME类型
try {
// 尝试获取文件的内容类型
String contentType = Files.probeContentType(file.toPath());
if (contentType != null) {
// 如果内容类型不为空则设置到HTTP响应头中
headers.setContentType(MediaType.parseMediaType(contentType));
} else {
// 如果内容类型为空,则默认设置为图像类型
headers.setContentType(MediaType.parseMediaType("image/*"));
}
} catch (IOException e) {
// 如果发生IO异常则默认设置内容类型为图像类型
headers.setContentType(MediaType.parseMediaType("image/*"));
}
// 返回包含文件资源的响应实体
return ResponseEntity.ok()
.headers(headers)
.body(resource);
} else {
// 如果文件不存在返回404状态
return ResponseEntity.notFound().build();
}
} }
/** /**
@ -165,45 +129,14 @@ public class RecordBusbarTempController extends BaseController
*/ */
@GetMapping("/getThermalPhoto/{filePath}") @GetMapping("/getThermalPhoto/{filePath}")
public ResponseEntity<FileSystemResource> getThermalPhoto (@PathVariable String filePath) { public ResponseEntity<FileSystemResource> getThermalPhoto (@PathVariable String filePath) {
// 获取配置文件中的路径application.yml的profile return recordBusbarTempService.getThermalPhoto(filePath);
String myPath = RuoYiConfig.getMyPath();
// 构建完整的文件路径
String fileUrl = myPath + "/日志信息/红外热成像/" + filePath;
// 创建文件对象
File file = new File(fileUrl);
// 检查文件是否存在
if (file.exists()) {
// 创建文件系统资源对象包装文件http头不能使用file所以使用FileSystemResource
FileSystemResource resource = new FileSystemResource(file);
// 创建HTTP头对象
HttpHeaders headers = new HttpHeaders();
// 设置文件长度
headers.setContentLength(file.length());
// 动态检测文件的MIME类型
try {
// 尝试获取文件的内容类型
String contentType = Files.probeContentType(file.toPath());
if (contentType != null) {
// 如果内容类型不为空则设置到HTTP响应头中
headers.setContentType(MediaType.parseMediaType(contentType));
} else {
// 如果内容类型为空,则默认设置为图像类型
headers.setContentType(MediaType.parseMediaType("image/*"));
}
} catch (IOException e) {
// 如果发生IO异常则默认设置内容类型为图像类型
headers.setContentType(MediaType.parseMediaType("image/*"));
}
// 返回包含文件资源的响应实体
return ResponseEntity.ok()
.headers(headers)
.body(resource);
} else {
// 如果文件不存在返回404状态
return ResponseEntity.notFound().build();
}
} }
/**
* 线
* @param recordBusbarTemp 线
* @return AjaxResult线
*/
@GetMapping("/busbarTempCurve") @GetMapping("/busbarTempCurve")
public AjaxResult busbarTempCurve (RecordBusbarTemp recordBusbarTemp){ public AjaxResult busbarTempCurve (RecordBusbarTemp recordBusbarTemp){
List<RecordBusbarTemp> recordBusbarTempList = recordBusbarTempService List<RecordBusbarTemp> recordBusbarTempList = recordBusbarTempService
@ -212,4 +145,17 @@ public class RecordBusbarTempController extends BaseController
} }
/**
*
* @param recordBusbarTemp 线
* @return AjaxResult线
*/
@PreAuthorize("@ss.hasPermi('record:recordBusbarTemp:list')")
@PostMapping("/recordBusbarTempList")
public AjaxResult recordBusbarTempList(RecordBusbarTemp recordBusbarTemp)
{
List<RecordBusbarTemp> list = recordBusbarTempService.selectRecordBusbarTempList(recordBusbarTemp);
return success(list);
}
} }

@ -121,7 +121,6 @@ public class RecordInspectionCabinetController extends BaseController
} }
/** /**
* HTTP GET * HTTP GET
* @param filePath * @param filePath
@ -129,39 +128,8 @@ public class RecordInspectionCabinetController extends BaseController
*/ */
@GetMapping("/getThermalVideo/{filePath}") @GetMapping("/getThermalVideo/{filePath}")
public ResponseEntity<FileSystemResource> getThermalVideo(@PathVariable String filePath) { public ResponseEntity<FileSystemResource> getThermalVideo(@PathVariable String filePath) {
// 获取配置文件中的路径application.yml的profile
String myPath = RuoYiConfig.getMyPath(); return recordInspectionCabinetService.getThermalVideo(filePath);
// 构建完整的文件路径
String fileUrl = myPath + "/日志信息/巡检录像/热成像/" + filePath;
// 创建文件对象
File file = new File(fileUrl);
// 检查文件是否存在
if (file.exists()) {
// 创建文件系统资源对象包装文件http头不能使用file所以使用FileSystemResource
FileSystemResource resource = new FileSystemResource(file);
// 设置响应头,指定内容类型为视频格式
HttpHeaders headers = new HttpHeaders();
// 设置文件长度的内容类型
headers.setContentLength(file.length());
// 动态检测文件的MIME类型
try {
String contentType = Files.probeContentType(file.toPath());
if (contentType != null) {
headers.setContentType(MediaType.parseMediaType(contentType));
} else {
headers.setContentType(MediaType.parseMediaType("video/*"));
}
} catch (IOException e) {
headers.setContentType(MediaType.parseMediaType("video/*"));
}
// 返回包含文件资源的响应实体
return ResponseEntity.ok()
.headers(headers)
.body(resource);
} else {
// 如果文件不存在返回404状态
return ResponseEntity.notFound().build();
}
} }
/** /**
@ -171,39 +139,7 @@ public class RecordInspectionCabinetController extends BaseController
*/ */
@GetMapping("/getVisibleVideo/{filePath}") @GetMapping("/getVisibleVideo/{filePath}")
public ResponseEntity<FileSystemResource> getVisibleVideo(@PathVariable String filePath) { public ResponseEntity<FileSystemResource> getVisibleVideo(@PathVariable String filePath) {
// 获取配置文件中的路径application.yml的profile return recordInspectionCabinetService.getVisibleVideo(filePath);
String myPath = RuoYiConfig.getMyPath();
// 构建完整的文件路径
String fileUrl = myPath + "/日志信息/巡检录像/可见光/" + filePath;
// 创建文件对象
File file = new File(fileUrl);
// 检查文件是否存在
if (file.exists()) {
// 创建文件系统资源对象包装文件http头不能使用file所以使用FileSystemResource
FileSystemResource resource = new FileSystemResource(file);
// 设置HTTP头信息
HttpHeaders headers = new HttpHeaders();
// 设置文件长度
headers.setContentLength(file.length());
// 动态检测文件的MIME类型
try {
String contentType = Files.probeContentType(file.toPath());
if (contentType != null) {
headers.setContentType(MediaType.parseMediaType(contentType));
} else {
headers.setContentType(MediaType.parseMediaType("video/*"));
}
} catch (IOException e) {
headers.setContentType(MediaType.parseMediaType("video/*"));
}
// 返回包含文件资源的响应实体
return ResponseEntity.ok()
.headers(headers)
.body(resource);
} else {
// 如果文件不存在返回404状态
return ResponseEntity.notFound().build();
}
} }
} }

@ -1,7 +1,16 @@
package com.ruoyi.record.service; package com.ruoyi.record.service;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.List; import java.util.List;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.record.domain.RecordBusbarTemp; import com.ruoyi.record.domain.RecordBusbarTemp;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
/** /**
* Service * Service
@ -58,4 +67,18 @@ public interface IRecordBusbarTempService
* @return * @return
*/ */
public int deleteRecordBusbarTempByObjId(Long objId); public int deleteRecordBusbarTempByObjId(Long objId);
/**
*
* @param filePath
* @return 404
*/
public ResponseEntity<FileSystemResource> getThermalPhoto(String filePath);
/**
*
* @param filePath
* @return 404
*/
public ResponseEntity<FileSystemResource> getVisiblePhoto(String filePath);
} }

@ -2,6 +2,8 @@ package com.ruoyi.record.service;
import java.util.List; import java.util.List;
import com.ruoyi.record.domain.RecordInspectionCabinet; import com.ruoyi.record.domain.RecordInspectionCabinet;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.ResponseEntity;
/** /**
* Service * Service
@ -58,4 +60,18 @@ public interface IRecordInspectionCabinetService
* @return * @return
*/ */
public int deleteRecordInspectionCabinetByObjId(Long objId); public int deleteRecordInspectionCabinetByObjId(Long objId);
/**
*
* @param filePath
* @return 404
*/
public ResponseEntity<FileSystemResource> getThermalVideo(String filePath);
/**
*
* @param filePath
* @return 404
*/
public ResponseEntity<FileSystemResource> getVisibleVideo(String filePath);
} }

@ -1,13 +1,24 @@
package com.ruoyi.record.service.impl; package com.ruoyi.record.service.impl;
import java.io.File;
import java.util.List; import java.util.List;
import java.io.IOException;
import java.nio.file.Files;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import com.ruoyi.base.domain.BaseBusbarInfo; import com.ruoyi.base.domain.BaseBusbarInfo;
import com.ruoyi.base.domain.BaseCabinetInfo; import com.ruoyi.base.domain.BaseCabinetInfo;
import com.ruoyi.base.mapper.BaseBusbarInfoMapper; import com.ruoyi.base.mapper.BaseBusbarInfoMapper;
import com.ruoyi.base.mapper.BaseCabinetInfoMapper; import com.ruoyi.base.mapper.BaseCabinetInfoMapper;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.exception.ServiceException; import com.ruoyi.common.exception.ServiceException;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import com.ruoyi.record.mapper.RecordBusbarTempMapper; import com.ruoyi.record.mapper.RecordBusbarTempMapper;
import com.ruoyi.record.domain.RecordBusbarTemp; import com.ruoyi.record.domain.RecordBusbarTemp;
@ -132,4 +143,97 @@ public class RecordBusbarTempServiceImpl implements IRecordBusbarTempService
{ {
return recordBusbarTempMapper.deleteRecordBusbarTempByObjId(objId); return recordBusbarTempMapper.deleteRecordBusbarTempByObjId(objId);
} }
/**
*
* @param filePath
* @return 404
*/
@Override
public ResponseEntity<FileSystemResource> getThermalPhoto(String filePath){
// 获取配置文件中的路径application.yml的profile
String myPath = RuoYiConfig.getMyPath();
// 构建完整的文件路径
String fileUrl = myPath + "/日志信息/红外热成像/" + filePath;
// 创建文件对象
File file = new File(fileUrl);
// 检查文件是否存在
if (file.exists()) {
// 创建文件系统资源对象包装文件http头不能使用file所以使用FileSystemResource
FileSystemResource resource = new FileSystemResource(file);
// 创建HTTP头对象
HttpHeaders headers = new HttpHeaders();
// 设置文件长度
headers.setContentLength(file.length());
// 动态检测文件的MIME类型
try {
// 尝试获取文件的内容类型
String contentType = Files.probeContentType(file.toPath());
if (contentType != null) {
// 如果内容类型不为空则设置到HTTP响应头中
headers.setContentType(MediaType.parseMediaType(contentType));
} else {
// 如果内容类型为空,则默认设置为图像类型
headers.setContentType(MediaType.parseMediaType("image/*"));
}
} catch (IOException e) {
// 如果发生IO异常则默认设置内容类型为图像类型
headers.setContentType(MediaType.parseMediaType("image/*"));
}
// 返回包含文件资源的响应实体
return ResponseEntity.ok()
.headers(headers)
.body(resource);
} else {
// 如果文件不存在返回404状态
return ResponseEntity.notFound().build();
}
}
/**
*
* @param filePath
* @return 404
*/
@Override
public ResponseEntity<FileSystemResource> getVisiblePhoto(String filePath){
// 获取配置文件中的路径application.yml的profile
String myPath = RuoYiConfig.getMyPath();
// 构建文件的完整路径
String fileUrl = myPath + "/日志信息/可见光图像/" + filePath;
// 创建文件对象
File file = new File(fileUrl);
// 检查文件是否存在
if (file.exists()) {
// 创建文件系统资源对象包装文件http头不能使用file所以使用FileSystemResource
FileSystemResource resource = new FileSystemResource(file);
// 创建HttpHeaders对象用于设置响应头
HttpHeaders headers = new HttpHeaders();
// 设置文件长度
headers.setContentLength(file.length());
// 动态检测文件的MIME类型
try {
// 尝试获取文件的内容类型
String contentType = Files.probeContentType(file.toPath());
if (contentType != null) {
// 如果内容类型不为空则设置到HTTP响应头中
headers.setContentType(MediaType.parseMediaType(contentType));
} else {
// 如果内容类型为空,则默认设置为图像类型
headers.setContentType(MediaType.parseMediaType("image/*"));
}
} catch (IOException e) {
// 如果发生IO异常则默认设置内容类型为图像类型
headers.setContentType(MediaType.parseMediaType("image/*"));
}
// 返回包含文件资源的响应实体
return ResponseEntity.ok()
.headers(headers)
.body(resource);
} else {
// 如果文件不存在返回404状态
return ResponseEntity.notFound().build();
}
}
} }

@ -1,7 +1,21 @@
package com.ruoyi.record.service.impl; package com.ruoyi.record.service.impl;
import java.io.File;
import java.util.List; import java.util.List;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.file.Files;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.ruoyi.common.config.RuoYiConfig;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import com.ruoyi.record.mapper.RecordInspectionCabinetMapper; import com.ruoyi.record.mapper.RecordInspectionCabinetMapper;
import com.ruoyi.record.domain.RecordInspectionCabinet; import com.ruoyi.record.domain.RecordInspectionCabinet;
@ -90,4 +104,89 @@ public class RecordInspectionCabinetServiceImpl implements IRecordInspectionCabi
{ {
return recordInspectionCabinetMapper.deleteRecordInspectionCabinetByObjId(objId); return recordInspectionCabinetMapper.deleteRecordInspectionCabinetByObjId(objId);
} }
/**
*
* @param filePath
* @return 404
*/
@Override
public ResponseEntity<FileSystemResource> getThermalVideo(String filePath){
// 获取配置文件中的路径application.yml的profile
String myPath = RuoYiConfig.getMyPath();
// 构建完整的文件路径
String fileUrl = myPath + "/日志信息/巡检录像/热成像/" + filePath;
// 创建文件对象
File file = new File(fileUrl);
// 检查文件是否存在
if (file.exists()) {
// 创建文件系统资源对象包装文件http头不能使用file所以使用FileSystemResource
FileSystemResource resource = new FileSystemResource(file);
// 设置响应头,指定内容类型为视频格式
HttpHeaders headers = new HttpHeaders();
// 设置文件长度的内容类型
headers.setContentLength(file.length());
// 动态检测文件的MIME类型
try {
String contentType = Files.probeContentType(file.toPath());
if (contentType != null) {
headers.setContentType(MediaType.parseMediaType(contentType));
} else {
headers.setContentType(MediaType.parseMediaType("video/*"));
}
} catch (IOException e) {
headers.setContentType(MediaType.parseMediaType("video/*"));
}
// 返回包含文件资源的响应实体
return ResponseEntity.ok()
.headers(headers)
.body(resource);
} else {
// 如果文件不存在返回404状态
return ResponseEntity.notFound().build();
}
}
/**
*
* @param filePath
* @return 404
*/
@Override
public ResponseEntity<FileSystemResource> getVisibleVideo(String filePath){
// 获取配置文件中的路径application.yml的profile
String myPath = RuoYiConfig.getMyPath();
// 构建完整的文件路径
String fileUrl = myPath + "/日志信息/巡检录像/可见光/" + filePath;
// 创建文件对象
File file = new File(fileUrl);
// 检查文件是否存在
if (file.exists()) {
// 创建文件系统资源对象包装文件http头不能使用file所以使用FileSystemResource
FileSystemResource resource = new FileSystemResource(file);
// 设置HTTP头信息
HttpHeaders headers = new HttpHeaders();
// 设置文件长度
headers.setContentLength(file.length());
// 动态检测文件的MIME类型
try {
String contentType = Files.probeContentType(file.toPath());
if (contentType != null) {
headers.setContentType(MediaType.parseMediaType(contentType));
} else {
headers.setContentType(MediaType.parseMediaType("video/*"));
}
} catch (IOException e) {
headers.setContentType(MediaType.parseMediaType("video/*"));
}
// 返回包含文件资源的响应实体
return ResponseEntity.ok()
.headers(headers)
.body(resource);
} else {
// 如果文件不存在返回404状态
return ResponseEntity.notFound().build();
}
}
} }

Loading…
Cancel
Save