forked from dyf/fys-Multi-tenant
导入导出设备数据
This commit is contained in:
@ -9,6 +9,7 @@ import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.InputStream;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URL;
|
||||
import java.net.URLConnection;
|
||||
|
||||
@ -33,23 +34,62 @@ public class IgnoreFailedImageConverter implements Converter<URL> {
|
||||
@Override
|
||||
public WriteCellData<?> convertToExcelData(URL value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) {
|
||||
if (value == null) {
|
||||
return null;
|
||||
logger.debug("图片URL为空");
|
||||
return new WriteCellData<>(new byte[0]);
|
||||
}
|
||||
|
||||
try {
|
||||
logger.debug("开始加载图片: {}", value);
|
||||
URLConnection conn = value.openConnection();
|
||||
conn.setConnectTimeout(2000); // 2秒超时
|
||||
conn.setReadTimeout(3000); // 3秒超时
|
||||
// 增加连接和读取超时时间
|
||||
conn.setConnectTimeout(10000); // 10秒连接超时
|
||||
conn.setReadTimeout(30000); // 30秒读取超时
|
||||
|
||||
// 添加User-Agent避免被服务器拦截
|
||||
conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) ExcelExporter/1.0");
|
||||
|
||||
// 如果是HTTP连接,设置一些额外的属性
|
||||
if (conn instanceof HttpURLConnection) {
|
||||
HttpURLConnection httpConn = (HttpURLConnection) conn;
|
||||
httpConn.setRequestMethod("GET");
|
||||
// 不使用缓存
|
||||
httpConn.setUseCaches(false);
|
||||
// 跟随重定向
|
||||
httpConn.setInstanceFollowRedirects(true);
|
||||
}
|
||||
|
||||
long contentLength = conn.getContentLengthLong();
|
||||
logger.debug("连接建立成功,图片大小: {} 字节", contentLength);
|
||||
|
||||
// 检查内容长度是否有效
|
||||
if (contentLength == 0) {
|
||||
logger.warn("图片文件为空: {}", value);
|
||||
return new WriteCellData<>(new byte[0]);
|
||||
}
|
||||
|
||||
// 限制图片大小(防止过大文件导致内存问题)
|
||||
if (contentLength > 10 * 1024 * 1024) { // 10MB限制
|
||||
logger.warn("图片文件过大 ({} bytes),跳过加载: {}", contentLength, value);
|
||||
return new WriteCellData<>(new byte[0]);
|
||||
}
|
||||
|
||||
try (InputStream inputStream = conn.getInputStream()) {
|
||||
// byte[] bytes = FileUtils.readInputStream(inputStream, value.toString());
|
||||
// 替代 FileUtils.readInputStream 的自定义方法
|
||||
byte[] bytes = readInputStream(inputStream);
|
||||
|
||||
// 检查读取到的数据是否为空
|
||||
if (bytes == null || bytes.length == 0) {
|
||||
logger.warn("读取到空的图片数据: {}", value);
|
||||
return new WriteCellData<>(new byte[0]);
|
||||
}
|
||||
|
||||
logger.debug("成功读取图片数据,大小: {} 字节", bytes.length);
|
||||
return new WriteCellData<>(bytes);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
// 静默忽略错误,只记录日志
|
||||
logger.debug("忽略图片加载失败: {}, 原因: {}", value, e.getMessage());
|
||||
logger.warn("图片加载失败: {}, 原因: {}", value, e.getMessage(), e);
|
||||
// return null; // 返回null表示不写入图片
|
||||
return new WriteCellData<>(new byte[0]); // 返回空数组而不是 null
|
||||
}
|
||||
@ -66,9 +106,17 @@ public class IgnoreFailedImageConverter implements Converter<URL> {
|
||||
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
||||
byte[] buffer = new byte[8192];
|
||||
int bytesRead;
|
||||
int totalBytes = 0;
|
||||
|
||||
while ((bytesRead = inputStream.read(buffer)) != -1) {
|
||||
outputStream.write(buffer, 0, bytesRead);
|
||||
totalBytes += bytesRead;
|
||||
|
||||
// 如果读取的数据过大,提前终止
|
||||
if (totalBytes > 10 * 1024 * 1024) { // 10MB限制
|
||||
logger.warn("读取的图片数据超过10MB限制,提前终止");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return outputStream.toByteArray();
|
||||
|
||||
@ -19,24 +19,28 @@ import java.net.URL;
|
||||
@ContentRowHeight(100) // 内容行高
|
||||
public class DeviceExcelExportDTO {
|
||||
|
||||
@ExcelProperty("ID")
|
||||
private Long id;
|
||||
// @ExcelProperty("ID")
|
||||
// private Long id;
|
||||
|
||||
@ExcelProperty("设备类型")
|
||||
private Long deviceType;
|
||||
@ColumnWidth(20)
|
||||
private String typeName;
|
||||
|
||||
// @ExcelProperty("设备类型")
|
||||
// private Long deviceType;
|
||||
|
||||
// @ExcelProperty("客户号")
|
||||
// private Long customerId;
|
||||
|
||||
@ExcelProperty("所属客户")
|
||||
private String customerName;
|
||||
// @ExcelProperty("所属客户")
|
||||
// private String customerName;
|
||||
|
||||
@ExcelProperty("设备名称")
|
||||
@ColumnWidth(20)
|
||||
private String deviceName;
|
||||
|
||||
@ExcelProperty(value = "设备图片", converter = IgnoreFailedImageConverter.class)
|
||||
@ColumnWidth(15) // 设置图片列宽度
|
||||
@ColumnWidth(30) // 设置图片列宽度
|
||||
private URL devicePic; // 使用URL类型
|
||||
|
||||
@ExcelProperty("设备MAC")
|
||||
@ -51,28 +55,24 @@ public class DeviceExcelExportDTO {
|
||||
@ColumnWidth(20)
|
||||
private String deviceImei;
|
||||
|
||||
@ExcelProperty("经度")
|
||||
private String longitude;
|
||||
// @ExcelProperty("经度")
|
||||
// private String longitude;
|
||||
|
||||
@ExcelProperty("纬度")
|
||||
private String latitude;
|
||||
// @ExcelProperty("纬度")
|
||||
// private String latitude;
|
||||
|
||||
@ExcelProperty("备注")
|
||||
@ColumnWidth(30)
|
||||
private String remark;
|
||||
|
||||
@ExcelProperty("设备类型名称")
|
||||
@ColumnWidth(20)
|
||||
private String typeName;
|
||||
|
||||
/**
|
||||
* 设备状态
|
||||
* 0 失效
|
||||
* 1 正常
|
||||
*/
|
||||
@ExcelProperty("设备状态")
|
||||
@ColumnWidth(20)
|
||||
private String deviceStatus;
|
||||
// @ExcelProperty("设备状态")
|
||||
// @ColumnWidth(20)
|
||||
// private String deviceStatus;
|
||||
|
||||
@ExcelProperty("创建时间")
|
||||
@ColumnWidth(20)
|
||||
|
||||
@ -16,24 +16,24 @@ import lombok.Data;
|
||||
@ContentRowHeight(100) // 内容行高
|
||||
public class DeviceExcelImportDTO {
|
||||
|
||||
@ExcelProperty("设备类型")
|
||||
private Long deviceType;
|
||||
|
||||
@ExcelProperty("客户号")
|
||||
private Long customerId;
|
||||
// @ExcelProperty("设备类型")
|
||||
// private Long deviceType;
|
||||
|
||||
@ExcelProperty("设备名称")
|
||||
@ColumnWidth(20)
|
||||
private String deviceName;
|
||||
|
||||
@ExcelProperty(value = "设备图片", converter = ByteArrayImageConverter.class)
|
||||
@ColumnWidth(15)
|
||||
private byte[] devicePic;
|
||||
@ExcelProperty("设备类型名称")
|
||||
private String typeName;
|
||||
|
||||
// 添加图片写入方法
|
||||
public void setDevicePicFromBytes(byte[] bytes) {
|
||||
this.devicePic = bytes;
|
||||
}
|
||||
// @ExcelProperty(value = "设备图片", converter = ByteArrayImageConverter.class)
|
||||
// @ColumnWidth(15)
|
||||
// private byte[] devicePic;
|
||||
//
|
||||
// // 添加图片写入方法
|
||||
// public void setDevicePicFromBytes(byte[] bytes) {
|
||||
// this.devicePic = bytes;
|
||||
// }
|
||||
|
||||
@ExcelProperty("设备MAC")
|
||||
@ColumnWidth(20)
|
||||
@ -43,24 +43,21 @@ public class DeviceExcelImportDTO {
|
||||
@ColumnWidth(20)
|
||||
private String deviceImei;
|
||||
|
||||
@ExcelProperty("设备SN")
|
||||
@ColumnWidth(20)
|
||||
private String deviceSn;
|
||||
@ExcelProperty("蓝牙名称")
|
||||
private String bluetoothName;
|
||||
|
||||
@ExcelProperty("经度")
|
||||
private String longitude;
|
||||
|
||||
@ExcelProperty("纬度")
|
||||
private String latitude;
|
||||
// @ExcelProperty("设备SN")
|
||||
// @ColumnWidth(20)
|
||||
// private String deviceSn;
|
||||
//
|
||||
// @ExcelProperty("经度")
|
||||
// private String longitude;
|
||||
//
|
||||
// @ExcelProperty("纬度")
|
||||
// private String latitude;
|
||||
|
||||
@ExcelProperty("备注")
|
||||
@ColumnWidth(30)
|
||||
private String remark;
|
||||
|
||||
@ExcelProperty("设备类型名称")
|
||||
private String typeName;
|
||||
|
||||
@ExcelProperty("蓝牙名称")
|
||||
private String bluetoothName;
|
||||
|
||||
}
|
||||
@ -43,7 +43,6 @@ public class DeviceForm {
|
||||
@Schema(title = "蓝牙名称")
|
||||
private String bluetoothName;
|
||||
|
||||
|
||||
@Schema(title = "设备IMEI")
|
||||
private String deviceImei;
|
||||
|
||||
|
||||
@ -90,13 +90,13 @@ public class DeviceAlarmVo implements Serializable {
|
||||
* 经度
|
||||
*/
|
||||
@ExcelProperty(value = "经度")
|
||||
private Long longitude;
|
||||
private Double longitude;
|
||||
|
||||
/**
|
||||
* 纬度
|
||||
*/
|
||||
@ExcelProperty(value = "纬度")
|
||||
private Long latitude;
|
||||
private Double latitude;
|
||||
|
||||
/**
|
||||
* 报警位置
|
||||
|
||||
@ -77,13 +77,13 @@ public class DeviceFenceAccessRecordVo implements Serializable {
|
||||
* 纬度
|
||||
*/
|
||||
@ExcelProperty(value = "纬度")
|
||||
private Long latitude;
|
||||
private Double latitude;
|
||||
|
||||
/**
|
||||
* 经度
|
||||
*/
|
||||
@ExcelProperty(value = "经度")
|
||||
private Long longitude;
|
||||
private Double longitude;
|
||||
|
||||
/**
|
||||
* 定位精度
|
||||
|
||||
@ -72,7 +72,7 @@ public class UploadDeviceDataListener implements ReadListener<DeviceExcelImportD
|
||||
// 设置图片数据
|
||||
byte[] imageData = rowImageMap.get(rowIndex);
|
||||
if (imageData != null) {
|
||||
recordWithImage.setDevicePicFromBytes(imageData);
|
||||
// recordWithImage.setDevicePicFromBytes(imageData);
|
||||
}
|
||||
|
||||
failedRecordsWithImages.add(recordWithImage);
|
||||
@ -125,7 +125,6 @@ public class UploadDeviceDataListener implements ReadListener<DeviceExcelImportD
|
||||
rowDeviceMap.put(rowIndex, device);
|
||||
rowDtoMap.put(rowIndex, data);
|
||||
rowIndexList.add(rowIndex);
|
||||
|
||||
}
|
||||
|
||||
|
||||
@ -191,7 +190,8 @@ public class UploadDeviceDataListener implements ReadListener<DeviceExcelImportD
|
||||
if (device != null) {
|
||||
try {
|
||||
byte[] imageData = picture.getPictureData().getData();
|
||||
String extraValue = getCellValue(sheet, rowIndex, 4);
|
||||
// 表示Excel表格中的第3列(因为索引从0开始计算)
|
||||
String extraValue = getCellValue(sheet, rowIndex, 2);
|
||||
String imageUrl = uploadAndGenerateUrl(imageData, extraValue);
|
||||
device.setDevicePic(imageUrl);
|
||||
|
||||
|
||||
@ -5,6 +5,7 @@ import com.alibaba.excel.util.DateUtils;
|
||||
import com.fuyuanshen.equipment.domain.Device;
|
||||
import com.fuyuanshen.equipment.domain.dto.DeviceExcelExportDTO;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.io.IOException;
|
||||
@ -20,6 +21,7 @@ import java.util.stream.Collectors;
|
||||
* @date: 2025-06-0618:22
|
||||
*/
|
||||
@Service
|
||||
@Slf4j
|
||||
public class DeviceExportService {
|
||||
|
||||
public void export(List<Device> devices, HttpServletResponse response) {
|
||||
@ -36,22 +38,22 @@ public class DeviceExportService {
|
||||
// 转换为DTO列表
|
||||
List<DeviceExcelExportDTO> dtoList = devices.stream().map(device -> {
|
||||
DeviceExcelExportDTO dto = new DeviceExcelExportDTO();
|
||||
dto.setId(device.getId());
|
||||
dto.setDeviceType(device.getDeviceType());
|
||||
dto.setCustomerName(device.getCustomerName());
|
||||
// dto.setId(device.getId());
|
||||
// dto.setDeviceType(device.getDeviceType());
|
||||
// dto.setCustomerName(device.getCustomerName());
|
||||
dto.setDeviceName(device.getDeviceName());
|
||||
dto.setDeviceMac(device.getDeviceMac());
|
||||
// 设备IMEI
|
||||
dto.setDeviceImei(device.getDeviceImei());
|
||||
// 蓝牙名称
|
||||
dto.setBluetoothName(device.getBluetoothName());
|
||||
dto.setLongitude(device.getLongitude());
|
||||
dto.setLatitude(device.getLatitude());
|
||||
// dto.setLongitude(device.getLongitude());
|
||||
// dto.setLatitude(device.getLatitude());
|
||||
dto.setRemark(device.getRemark());
|
||||
dto.setTypeName(device.getTypeName());
|
||||
dto.setCreateBy(device.getCreateByName());
|
||||
Integer deviceStatus = device.getDeviceStatus();
|
||||
dto.setDeviceStatus(deviceStatus == 1 ? "正常" : "失效");
|
||||
// dto.setDeviceStatus(deviceStatus == 1 ? "正常" : "失效");
|
||||
// 时间戳转换
|
||||
dto.setCreateTime(DateUtils.format(device.getCreateTime(), "yyyy-MM-dd HH:mm:ss"));
|
||||
|
||||
@ -72,17 +74,31 @@ public class DeviceExportService {
|
||||
|
||||
private void handleDevicePic(Device device, DeviceExcelExportDTO dto) {
|
||||
String picUrl = device.getDevicePic();
|
||||
log.info("处理设备图片,设备ID: {}, 图片URL: {}", device.getId(), picUrl);
|
||||
|
||||
if (picUrl != null && !picUrl.trim().isEmpty()) {
|
||||
try {
|
||||
// 自动将HTTP转换为HTTPS以避免重定向问题
|
||||
if (picUrl.startsWith("http://")) {
|
||||
picUrl = "https://" + picUrl.substring(7);
|
||||
log.info("自动将HTTP转换为HTTPS: {}", picUrl);
|
||||
}
|
||||
|
||||
// 尝试创建URL对象(会自动验证格式)
|
||||
dto.setDevicePic(new URL(picUrl));
|
||||
} catch (MalformedURLException e) {
|
||||
URL url = new URL(picUrl);
|
||||
|
||||
dto.setDevicePic(url);
|
||||
log.info("成功设置设备图片URL到DTO");
|
||||
} catch (Exception e) {
|
||||
// 不是有效URL时设置为null
|
||||
log.info("设置设备图片失败,设备ID: {}, URL: {}, 错误: {}", device.getId(), picUrl, e.getMessage());
|
||||
dto.setDevicePic(null);
|
||||
}
|
||||
} else {
|
||||
log.info("设备没有设置图片,设备ID: {}", device.getId());
|
||||
dto.setDevicePic(null);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@ -139,8 +139,27 @@ public class DeviceServiceImpl extends ServiceImpl<DeviceMapper, Device> impleme
|
||||
|
||||
@Override
|
||||
public List<Device> queryAll(DeviceQueryCriteria criteria) {
|
||||
|
||||
// 角色管理员
|
||||
Long userId = LoginHelper.getUserId();
|
||||
List<SysRoleVo> roles = roleService.selectRolesAuthByUserId(userId);
|
||||
boolean isAdmin = false;
|
||||
if (CollectionUtil.isNotEmpty(roles)) {
|
||||
for (SysRoleVo role : roles) {
|
||||
if (role.getRoleKey().equals("admin")) {
|
||||
isAdmin = true;
|
||||
criteria.setIsAdmin(true);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 只有非admin用户才设置当前用户ID条件
|
||||
if (!isAdmin) {
|
||||
criteria.setCurrentOwnerId(LoginHelper.getUserId());
|
||||
}
|
||||
return deviceMapper.findAll(criteria);
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
@ -136,7 +136,23 @@ public class DeviceTypeServiceImpl extends ServiceImpl<DeviceTypeMapper, DeviceT
|
||||
@Override
|
||||
public DeviceType queryByName(String typeName) {
|
||||
DeviceTypeQueryCriteria criteria = new DeviceTypeQueryCriteria();
|
||||
|
||||
// 角色管理员
|
||||
Long userId = LoginHelper.getUserId();
|
||||
List<SysRoleVo> roles = roleService.selectRolesAuthByUserId(userId);
|
||||
boolean isAdmin = false;
|
||||
if (CollectionUtil.isNotEmpty(roles)) {
|
||||
for (SysRoleVo role : roles) {
|
||||
if (role.getRoleKey().contains("admin")) {
|
||||
isAdmin = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!isAdmin) {
|
||||
criteria.setCustomerId(LoginHelper.getUserId());
|
||||
}
|
||||
|
||||
criteria.setTypeName(typeName);
|
||||
DeviceType deviceType = deviceTypeMapper.queryByName(criteria);
|
||||
return deviceType;
|
||||
|
||||
@ -50,7 +50,12 @@
|
||||
SELECT dt.*, dg.id AS grant_id
|
||||
FROM device_type dt
|
||||
JOIN device_type_grants dg ON dt.id = dg.device_type_id
|
||||
WHERE dt.type_name = #{criteria.typeName}
|
||||
AND dg.customer_id = #{criteria.customerId}
|
||||
<where>
|
||||
dt.type_name = #{criteria.typeName}
|
||||
<if test="criteria.customerId != null">
|
||||
and dg.customer_id = #{criteria.customerId}
|
||||
</if>
|
||||
</where>
|
||||
</select>
|
||||
|
||||
</mapper>
|
||||
Reference in New Issue
Block a user