21 Commits

Author SHA1 Message Date
dyf
d5a29feca3 Merge pull request 'jingquan' (#20) from liwenlong/fys-Multi-tenant:jingquan into jingquan
Reviewed-on: #20
2025-11-25 14:47:00 +08:00
0457877c09 merge upstream 2025-11-24 08:30:34 +08:00
b18ab98feb 导出设备数据 2025-11-21 13:36:13 +08:00
7c6f3be844 merge upstream 2025-11-20 16:25:33 +08:00
aa69b552aa Merge remote-tracking branch 'liwenlong-fys/jingquan' into jingquan 2025-11-20 16:25:04 +08:00
3dd0d4cc90 feat(video): 支持BGR565格式视频处理及MQTT设备确认消息更新
- 新增BGR565格式转换逻辑,支持RGB565与BGR565两种颜色格式- 视频上传接口增加code参数,默认值为1(RGB565)
- 在VideoProcessUtil中实现convertFramesToBGR565方法
- 添加bgr565ToMp4工具方法用于将BGR565数据编码为MP4文件
- MQTT规则新增对“设备已收到通知”的处理逻辑
- 设备确认消息后更新数据库日志状态并推送SSE消息
- 引入ScheduledExecutorService延时推送SSE消息- 增加设备日志和设备Mapper依赖以支持数据操作
2025-11-20 16:24:45 +08:00
00a4394b43 新增设备 2025-11-20 16:15:15 +08:00
2376a3b42a 修改设备类型 2025-11-20 10:11:14 +08:00
359cabbd2c feat(video): 支持BGR565格式视频处理- 新增code参数用于指定视频转码格式
- 实现BGR565格式的帧数据转换逻辑
- 添加convertFramesToBGR565和convertToBGR565方法
- 支持将BGR565数据通过FFmpeg生成MP4文件- 更新VideoProcessUtil工具类以支持两种颜色格式
- 在视频处理服务中记录处理日志及hex列表信息
2025-11-20 09:06:05 +08:00
76c11fff15 Merge remote-tracking branch 'upstream/6170' into 6170 2025-11-19 17:17:40 +08:00
a0ab5e9fe0 app注销删除相关联数据 2025-11-19 17:17:34 +08:00
891ee7c1c9 Merge branch 'dyf-device' into 6170 2025-11-19 10:56:12 +08:00
a145c372b8 优化设备导出 2025-11-19 10:55:44 +08:00
3798e52ee0 导入设备数据 2025-11-18 15:34:46 +08:00
6488b8a724 Merge branch 'dyf-device' into 6170 2025-11-12 11:25:55 +08:00
88b54a49f4 Merge branch 'dyf-device' into 6170
# Conflicts:
#	fys-admin/src/main/resources/application-prod.yml
2025-11-11 14:34:19 +08:00
dce043f63d Merge branch 'dyf-device' into 6170 2025-11-11 10:51:57 +08:00
759c72fc65 Merge branch 'dyf-device' into 6170 2025-11-10 10:37:43 +08:00
70c416779f Merge branch 'dyf-device' into 6170 2025-11-07 17:11:45 +08:00
f4369f7581 分享设备bug修改 2025-10-30 14:27:01 +08:00
df28eed305 prod配置文件 2025-10-28 10:51:05 +08:00
38 changed files with 1892 additions and 231 deletions

View File

@ -29,10 +29,13 @@ public class AppVideoController extends BaseController {
private final VideoProcessService videoProcessService;
private final AudioProcessService audioProcessService;
/**
* 上传视频转码code默认1RGB565 2BGR565
*/
@PostMapping(value = "/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
@RepeatSubmit(interval = 2, timeUnit = TimeUnit.SECONDS,message = "请勿重复提交!")
public R<List<String>> uploadVideo(@RequestParam("file") MultipartFile file) {
return R.ok(videoProcessService.processVideo(file));
public R<List<String>> uploadVideo(@RequestParam("file") MultipartFile file, @RequestParam(defaultValue = "1") int code) {
return R.ok(videoProcessService.processVideo(file, code));
}
/**

View File

@ -38,6 +38,8 @@ public class AppDeviceXinghanController extends BaseController {
private final DeviceXinghanBizService appDeviceService;
private final DeviceService deviceService;
/**
* 人员信息登记
*/
@ -67,7 +69,7 @@ public class AppDeviceXinghanController extends BaseController {
public R<Void> upload(@Validated @ModelAttribute AppDeviceLogoUploadDto bo) {
MultipartFile file = bo.getFile();
if(file.getSize()>1024*1024*2){
if (file.getSize() > 1024 * 1024 * 2) {
return R.warn("图片不能大于2M");
}
appDeviceService.uploadDeviceLogo(bo);
@ -125,7 +127,7 @@ public class AppDeviceXinghanController extends BaseController {
@GetMapping(value = "/typeAll")
@Operation(summary = "查询所有设备类型")
public R<List<DeviceType>> queryDeviceTypes() {
public R<List<DeviceType>> queryDeviceTypes() {
List<DeviceType> deviceTypes = appDeviceService.queryDeviceTypes();
return R.ok(deviceTypes);
}

View File

@ -148,7 +148,7 @@ public class AppDeviceBJQ6075Controller extends BaseController {
*/
@GetMapping("/getShareInfo/{id}")
public R<AppDevice6075DetailVo> getShareInfo(@NotNull(message = "主键不能为空")
@PathVariable Long id) {
@PathVariable Long id) {
return R.ok(appDeviceService6075.getInfo(id));
}

View File

@ -216,6 +216,7 @@ public class AppDeviceShareService {
uw.eq("phonenumber", bo.getPhonenumber());
uw.set("permission", bo.getPermission());
uw.set("update_by", userId);
uw.set("create_by", userId);
uw.set("update_time", new Date());
return appDeviceShareMapper.update(uw);

View File

@ -4,6 +4,10 @@ import cn.dev33.satoken.exception.NotLoginException;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.fuyuanshen.app.domain.bo.AppDeviceBindRecordBo;
import com.fuyuanshen.app.domain.bo.AppDeviceShareBo;
import com.fuyuanshen.app.domain.vo.AppDeviceBindRecordVo;
import com.fuyuanshen.app.domain.vo.AppDeviceShareVo;
import com.fuyuanshen.app.domain.vo.AppRoleVo;
import com.fuyuanshen.app.domain.vo.AppUserVo;
import com.fuyuanshen.common.core.constant.Constants;
@ -33,6 +37,7 @@ import org.springframework.stereotype.Service;
import java.time.Duration;
import java.util.*;
import java.util.function.Supplier;
import java.util.stream.Collectors;
/**
* 登录校验方法
@ -52,6 +57,8 @@ public class AppLoginService {
private final ISysTenantService tenantService;
private final IAppUserService appUserService;
private final IAppDeviceShareService appDeviceShareService;
private final IAppDeviceBindRecordService appDeviceBindRecordService;
/**
@ -188,10 +195,32 @@ public class AppLoginService {
public void cancelAccount() {
try {
AppLoginUser loginUser = AppLoginHelper.getLoginUser();
// AppLoginUser loginUser = new AppLoginUser();
// loginUser.setUserId(1988398584423133187L);
// loginUser.setUsername("19022528079");
if (ObjectUtil.isNull(loginUser)) {
return;
}
appUserService.deleteWithValidByIds(Collections.singletonList(loginUser.getUserId()),true);
AppDeviceBindRecordBo appDeviceBindRecordBo = new AppDeviceBindRecordBo();
appDeviceBindRecordBo.setBindingUserId(loginUser.getUserId());
List<AppDeviceBindRecordVo> appDeviceBindRecordVos = appDeviceBindRecordService.queryList(appDeviceBindRecordBo);
if(ObjectUtil.length(appDeviceBindRecordVos)>0){
// 根据设备id批量删除
List<Long> deviceIds = appDeviceBindRecordVos.stream().map(AppDeviceBindRecordVo::getDeviceId).toList();
appDeviceShareService.deleteByDeviceIds(deviceIds);
List<Long> ids = appDeviceBindRecordVos.stream()
.map(AppDeviceBindRecordVo::getId)
.collect(Collectors.toList());
appDeviceBindRecordService.deleteWithValidByIds(ids, true);
log.info("删除绑定关系表数据ids={}",ids);
}
if (TenantHelper.isEnable() && LoginHelper.isSuperAdmin()) {
// 超级管理员 登出清除动态租户
TenantHelper.clearDynamic();

View File

@ -28,7 +28,7 @@ public class VideoProcessService {
private final VideoProcessUtil videoProcessUtil;
public List<String> processVideo(MultipartFile file) {
public List<String> processVideo(MultipartFile file, int code) {
// 1. 参数校验
validateVideoFile(file);
@ -39,9 +39,10 @@ public class VideoProcessService {
// 3. 处理视频并提取帧数据
List<String> hexList = videoProcessUtil.processVideoToHex(
tempFile, FRAME_RATE, DURATION, WIDTH, HEIGHT
tempFile, FRAME_RATE, DURATION, WIDTH, HEIGHT, code
);
log.info("code: {} hexList(前100个): {}", code,
hexList.subList(0, Math.min(100, hexList.size())));
log.info("视频处理成功生成Hex数据长度: {}", hexList.size());
return hexList;

View File

@ -1,9 +1,18 @@
package com.fuyuanshen.global.mqtt.rule.xinghan;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fuyuanshen.common.json.utils.JsonUtils;
import com.fuyuanshen.common.redis.utils.RedisUtils;
import com.fuyuanshen.common.satoken.utils.LoginHelper;
import com.fuyuanshen.common.sse.dto.SseMessageDto;
import com.fuyuanshen.common.sse.utils.SseMessageUtils;
import com.fuyuanshen.equipment.domain.Device;
import com.fuyuanshen.equipment.domain.DeviceLog;
import com.fuyuanshen.equipment.mapper.DeviceLogMapper;
import com.fuyuanshen.equipment.mapper.DeviceMapper;
import com.fuyuanshen.global.mqtt.base.MqttMessageRule;
import com.fuyuanshen.global.mqtt.base.MqttRuleContext;
import com.fuyuanshen.global.mqtt.config.MqttGateway;
@ -21,6 +30,8 @@ import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import static com.fuyuanshen.common.core.constant.GlobalConstants.FUNCTION_ACCESS_KEY;
import static com.fuyuanshen.common.core.constant.GlobalConstants.GLOBAL_REDIS_KEY;
@ -40,6 +51,18 @@ public class XinghanSendAlarmMessageRule implements MqttMessageRule {
private final MqttGateway mqttGateway;
private final ObjectMapper objectMapper;
private final ScheduledExecutorService scheduledExecutorService;
private final DeviceLogMapper deviceLogMapper;
private final DeviceMapper deviceMapper;
/**
* 设备上行确认消息
*/
public static final String BREAK_NEWS_CONFIRMATION = "I get it";
/**
* 设备上行成功标记
*/
public static final String BREAK_NEWS_SUCCESS = "cover!";
@Override
public String getCommandType() {
@ -62,9 +85,36 @@ public class XinghanSendAlarmMessageRule implements MqttMessageRule {
log.warn("重复消息丢弃 {}", dedupKey);
return;
}
// 1. I get it —— 表示用户确认收到消息
if (BREAK_NEWS_CONFIRMATION.equalsIgnoreCase(respText)) {
var device = deviceMapper.selectOne(new QueryWrapper<Device>().eq("device_imei", ctx.getDeviceImei()));
// 使用MyBatis-Plus内置方法查询最新一条紧急通知
QueryWrapper<DeviceLog> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("device_id", device.getId())
.eq("device_action", "发送紧急通知") // 根据您的表结构调整
.orderByDesc("create_time")
.last("LIMIT 1");
DeviceLog latestLog = deviceLogMapper.selectOne(queryWrapper);
log.info("设备 {} 最新紧急通知:{}", ctx.getDeviceImei(), latestLog);
if (latestLog == null) {
return;
}
// 更新数据源字段
UpdateWrapper<DeviceLog> updateWrapper = new UpdateWrapper<>();
updateWrapper.eq("id", latestLog.getId()) // 条件ID匹配
.set("data_source", "设备已收到通知"); // 要更新的字段
deviceLogMapper.update(null, updateWrapper);
// 推送SSE消息
scheduledExecutorService.schedule(() -> {
SseMessageDto dto = new SseMessageDto();
dto.setMessage(String.format("%s设备已收到通知", latestLog.getDeviceName()));
dto.setUserIds(List.of(latestLog.getCreateBy()));
SseMessageUtils.publishMessage(dto);
}, 5, TimeUnit.SECONDS);
return;
}
// 1. cover! —— 成功标记
if ("cover!".equalsIgnoreCase(respText)) {
if (BREAK_NEWS_SUCCESS.equalsIgnoreCase(respText)) {
RedisUtils.setCacheObject(functionAccess, FunctionAccessStatus.OK.getCode(), Duration.ofSeconds(20));
log.info("设备 {} 发送紧急通知完成", ctx.getDeviceImei());
return;

View File

@ -69,6 +69,12 @@ public interface DeviceBJQ6075BizService {
*/
public void recordDeviceLog(Long deviceId, String deviceName, String deviceAction, String content, Long operator);
/**
* 注册人员信息
*
* @param bo 参数
* @return 结果
*/
public boolean registerPersonInfo(AppPersonnelInfoBo bo);
public void uploadDeviceLogo2(AppDeviceLogoUploadDto bo);

View File

@ -251,6 +251,12 @@ public class DeviceBJQ6075BizServiceImpl implements DeviceBJQ6075BizService {
}
/**
* 注册人员信息
*
* @param bo
* @return
*/
@Override
public boolean registerPersonInfo(AppPersonnelInfoBo bo) {
Long deviceId = bo.getDeviceId();
@ -264,7 +270,7 @@ public class DeviceBJQ6075BizServiceImpl implements DeviceBJQ6075BizService {
QueryWrapper<AppPersonnelInfo> qw = new QueryWrapper<AppPersonnelInfo>()
.eq("device_id", deviceId);
List<AppPersonnelInfoVo> appPersonnelInfoVos = appPersonnelInfoMapper.selectVoList(qw);
// unitName,position,name,id
// 生成固定长度的点阵数据
byte[] unitName = generateFixedBitmapData(bo.getUnitName(), 120);
byte[] position = generateFixedBitmapData(bo.getPosition(), 120);
byte[] name = generateFixedBitmapData(bo.getName(), 120);

View File

@ -11,6 +11,8 @@ import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.List;
@ -37,15 +39,26 @@ public class VideoProcessUtil {
/**
* 处理视频并转换为Hex字符串列表
*/
public List<String> processVideoToHex(File videoFile, int frameRate, int duration, int width, int height) throws Exception {
public List<String> processVideoToHex(File videoFile, int frameRate, int duration, int width, int height, int code) throws Exception {
// 1. 提取视频帧
List<BufferedImage> frames = extractFramesFromVideo(videoFile, frameRate, duration, width, height);
// 2. 转换为RGB565格式
byte[] binaryData = convertFramesToRGB565(frames);
if (code == 1) {
// 1. 转换为RGB565格式
byte[] binaryData = convertFramesToRGB565(frames);
// 3. 转换为Hex字符串列表
return bytesToHexList(binaryData);
// 2. 转换为Hex字符串列表
return bytesToHexList(binaryData);
} else {
// 1. 转换为BGR565格式
byte[] binaryData = convertFramesToBGR565(frames);
// 新增:直接生成 mp4
//bgr565ToMp4(binaryData, width, height, frameRate, "output.mp4");
// 2. 转换为Hex字符串列表
return bytesToHexList(binaryData);
}
}
/**
@ -110,6 +123,55 @@ public class VideoProcessUtil {
return result;
}
/**
* 将所有帧转换为 BGR565 格式字节数组
*/
private byte[] convertFramesToBGR565(List<BufferedImage> frames) throws Exception {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
for (BufferedImage image : frames) {
byte[] bgr565Bytes = convertToBGR565(image);
byteArrayOutputStream.write(bgr565Bytes);
}
byte[] result = byteArrayOutputStream.toByteArray();
log.debug("转换BGR565数据完成总字节数: {}", result.length);
return result;
}
/**
* 将BufferedImage转换为真正的BGR565格式字节数组
*/
private byte[] convertToBGR565(BufferedImage image) {
int width = image.getWidth();
int height = image.getHeight();
byte[] bgr565Data = new byte[width * height * 2];
int index = 0;
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
int rgb = image.getRGB(x, y);
// 提取RGB分量
int red = (rgb >> 16) & 0xFF;
int green = (rgb >> 8) & 0xFF;
int blue = rgb & 0xFF;
int b = (blue >> 3) & 0x1F; // 5位蓝色
int g = (green >> 2) & 0x3F; // 6位绿色
int r = (red >> 3) & 0x1F; // 5位红色
// 正确的BGR565组合红色在高位蓝色在低位
int bgr565 = (b << 11) | (g << 5) | r;
bgr565Data[index++] = (byte) ((bgr565 >> 8) & 0xFF);
// 小端序存储
bgr565Data[index++] = (byte) (bgr565 & 0xFF);
}
}
return bgr565Data;
}
/**
* 将字节数组转换为Hex字符串列表
*/
@ -191,4 +253,76 @@ public class VideoProcessUtil {
}
}
}
/**
* 把 BGR565 字节流直接写成 MP4H.264
* @param bgr565 完整的 BGR565 裸帧流(每像素 2 字节)
* @param width 帧宽
* @param height 帧高
* @param fps 帧率
* @param outMp4 输出 mp4 文件绝对路径
* @throws IOException 进程启动 / IO 失败
*/
public static void bgr565ToMp4(byte[] bgr565,
int width,
int height,
int fps,
String outMp4) throws IOException {
int framePixels = width * height;
int frameBytes = framePixels * 2;
if (bgr565.length % frameBytes != 0) {
throw new IllegalArgumentException("字节数组长度不是整帧");
}
/* 1. 构造 FFmpeg 命令 */
String[] cmd = {
"ffmpeg",
"-y", // 覆盖输出
"-f", "rawvideo",
"-pixel_format", "bgr24",
"-video_size", width + "x" + height,
"-framerate", String.valueOf(fps),
"-i", "-", // 从 stdin 读
"-c:v", "libx264",
"-pix_fmt", "yuv420p",
"-crf", "23", // 画质可自己调
outMp4
};
/* 2. 启动进程 */
ProcessBuilder pb = new ProcessBuilder(cmd);
pb.redirectError(ProcessBuilder.Redirect.INHERIT); // 把 FFmpeg 日志打到控制台
Process p = pb.start();
try (OutputStream ffmpegIn = p.getOutputStream()) {
/* 3. 逐帧转换并写入管道 */
byte[] bgr24 = new byte[framePixels * 3];
for (int off = 0; off < bgr565.length; off += frameBytes) {
for (int i = 0, j = 0; i < frameBytes; i += 2, j += 3) {
int u = ((bgr565[off + i + 1] & 0xFF) << 8)
| (bgr565[off + i] & 0xFF);
int b = (u & 0x1F) << 3;
int g = ((u >> 5) & 0x3F) << 2;
int r = ((u >> 11) & 0x1F) << 3;
bgr24[j] = (byte) b;
bgr24[j + 1] = (byte) g;
bgr24[j + 2] = (byte) r;
}
ffmpegIn.write(bgr24);
}
ffmpegIn.flush();
}
/* 4. 等待编码结束 */
try {
int exit = p.waitFor();
if (exit != 0) {
throw new IOException("FFmpeg 异常退出code=" + exit);
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new IOException("等待 FFmpeg 被中断", e);
}
}
}

View File

@ -269,4 +269,4 @@ justauth:
server-url: https://demo.gitea.com
client-id: 10**********6
client-secret: 1f7d08**********5b7**********29e
redirect-uri: ${justauth.address}/social-callback?source=gitea
redirect-uri: ${justauth.address}/social-callback?source=gitea

View File

@ -69,9 +69,9 @@ spring:
servlet:
multipart:
# 单个文件大小
max-file-size: 10MB
max-file-size: 100MB
# 设置总上传的文件大小
max-request-size: 20MB
max-request-size: 200MB
mvc:
# 设置静态资源路径 防止所有请求都去查静态资源
static-path-pattern: /static/**

View File

@ -27,14 +27,14 @@ public class Bitmap80x12Generator {
BufferedImage image = convertByteArrayToImage(bytes, 12, 80);
ImageIO.write(image, "PNG", new File("D:\\bitmap_preview.png"));
System.out.println("成功生成预览图片: D:\\bitmap_preview.png");
// 打印十六进制数据
// System.out.println("生成的点阵数据2:");
// printHexData(bitmapData);
// int[] ints = convertHexToDecimal(bitmapData);
System.out.println("打印十进制无符号:"+Arrays.toString(ints));
System.out.println("打印十进制无符号:" + Arrays.toString(ints));
// printDecimalData(bitmapData);
// 生成C文件
generateCFile(bitmapData, "bitmap_data.c", "chinese_text");
}
@ -125,7 +125,7 @@ public class Bitmap80x12Generator {
System.out.println();
}
public static void buildArr(int[] data,List<Integer> intData){
public static void buildArr(int[] data, List<Integer> intData) {
for (int datum : data) {
intData.add(datum);
}
@ -133,8 +133,8 @@ public class Bitmap80x12Generator {
/**
* 生成固定长度的点阵数据
*
* @param text 要转换的文本
*
* @param text 要转换的文本
* @param fixedLength 固定长度(字节)
* @return 固定长度的点阵数据
*/
@ -157,10 +157,11 @@ public class Bitmap80x12Generator {
int copyLength = Math.min(rawData.length, fixedLength);
System.arraycopy(rawData, 0, result, 0, copyLength);
// 剩余部分自动初始化为0
return result;
}
/**
* 创建文本图像
*/
@ -185,14 +186,14 @@ public class Bitmap80x12Generator {
// 获取字体度量
FontMetrics metrics = g.getFontMetrics();
// 计算文本绘制位置(居中)
int textWidth = metrics.stringWidth(text);
// int x = Math.max(0, (width - textWidth) / 2); // 水平居中
// 左对齐
int x = 0;
int y = (height - metrics.getHeight()) / 2 + metrics.getAscent(); // 垂直居中
// 绘制文本
g.drawString(text, x, y);
@ -242,6 +243,7 @@ public class Bitmap80x12Generator {
return byteListToArray(byteList);
}
public static byte[] byteListToArray(List<Byte> byteList) {
byte[] result = new byte[byteList.size()];
for (int i = 0; i < byteList.size(); i++) {
@ -282,7 +284,7 @@ public class Bitmap80x12Generator {
}
}
bitIndex++;
// 如果已经处理完所有像素,则退出
if (bitIndex >= width * height) {
return image;
@ -313,6 +315,7 @@ public class Bitmap80x12Generator {
sb.append("\n};");
return sb.toString();
}
/**
* 打印十六进制数据
*/
@ -320,7 +323,7 @@ public class Bitmap80x12Generator {
for (int i = 0; i < data.length; i++) {
int value = data[i] & 0xFF;
System.out.printf("0x%02X", value);
if (i < data.length - 1) {
System.out.print(", ");
if ((i + 1) % 12 == 0) System.out.println();
@ -348,6 +351,7 @@ public class Bitmap80x12Generator {
}
}
private static void writeByteArray(FileWriter writer, byte[] data) throws IOException {
for (int i = 0; i < data.length; i++) {
int value = data[i] & 0xFF;
@ -359,4 +363,6 @@ public class Bitmap80x12Generator {
}
}
}
}

View File

@ -0,0 +1,206 @@
package com.fuyuanshen.common.core.utils.file;
import lombok.extern.slf4j.Slf4j;
import javax.imageio.ImageIO;
import javax.imageio.ImageWriteParam;
import javax.imageio.ImageWriter;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Iterator;
/**
* 图片压缩工具类
*
* @author AprilWind
*/
@Slf4j
public class ImageCompressUtil {
/**
* 默认压缩目标大小(100KB)
*/
private static final int DEFAULT_COMPRESS_SIZE = 100 * 1024;
/**
* 默认触发压缩的大小(1MB)
*/
private static final int DEFAULT_TRIGGER_SIZE = 1024 * 1024;
/**
* 压缩图片到指定大小以下(默认100KB)
*
* @param imageData 原始图片数据
* @return 压缩后的图片数据
*/
public static byte[] compressImage(byte[] imageData) {
return compressImage(imageData, DEFAULT_COMPRESS_SIZE);
}
/**
* 压缩图片到指定大小以下
*
* @param imageData 原始图片数据
* @param maxSize 最大大小(字节)
* @return 压缩后的图片数据
*/
public static byte[] compressImage(byte[] imageData, int maxSize) {
try {
// 如果图片本身小于等于最大大小,直接返回
if (imageData.length <= maxSize) {
return imageData;
}
// 读取原始图片
BufferedImage originalImage = ImageIO.read(new ByteArrayInputStream(imageData));
if (originalImage == null) {
log.warn("无法读取图片数据");
return imageData;
}
// 检查图片是否包含透明度
boolean hasAlpha = hasAlpha(originalImage);
String formatName = hasAlpha ? "png" : "jpg";
// 对于小尺寸PNG图片可跳过压缩以保持图像质量
if ("png".equals(formatName) && imageData.length <= 2 * maxSize) {
log.debug("PNG图片大小适中({} bytes),跳过压缩", imageData.length);
return imageData;
}
// 先尝试质量压缩
byte[] compressedData = compressImageQuality(originalImage, formatName, 0.8f);
// 如果质量压缩后仍大于目标大小,则进行尺寸压缩
if (compressedData.length > maxSize) {
// 计算缩放比例
double scale = Math.sqrt((double) maxSize / compressedData.length);
scale = Math.max(scale, 0.5); // 最小缩放到原来的一半
// 尺寸压缩
compressedData = compressImageByScale(originalImage, scale, formatName);
}
// 如果压缩后还是太大,继续压缩
int attempts = 0;
while (compressedData.length > maxSize && attempts < 5) {
// 优先降低质量
float quality = Math.max(0.1f, 0.8f - attempts * 0.1f);
compressedData = compressImageQuality(originalImage, formatName, quality);
// 如果质量压缩不够,再缩小尺寸
if (compressedData.length > maxSize) {
double scale = 0.9 - attempts * 0.1; // 逐步缩小尺寸
scale = Math.max(scale, 0.5);
compressedData = compressImageByScale(originalImage, scale, formatName);
}
attempts++;
}
log.info("图片压缩完成,原始大小: {} bytes, 压缩后大小: {} bytes, 压缩率: {}%",
imageData.length, compressedData.length,
String.format("%.2f", (1.0 - (double) compressedData.length / imageData.length) * 100));
// 如果压缩后反而变大了,则使用原始数据
if (compressedData.length >= imageData.length) {
log.debug("压缩后数据变大,使用原始数据");
return imageData;
}
return compressedData;
} catch (Exception e) {
log.error("图片压缩失败: {}", e.getMessage(), e);
return imageData; // 压缩失败时返回原始数据
}
}
/**
* 按比例缩放图片
*
* @param originalImage 原始图片
* @param scale 缩放比例
* @param formatName 图片格式
* @return 缩放后的图片数据
* @throws IOException IO异常
*/
private static byte[] compressImageByScale(BufferedImage originalImage, double scale, String formatName) throws IOException {
int width = (int) (originalImage.getWidth() * scale);
int height = (int) (originalImage.getHeight() * scale);
// 创建缩放后的图片
Image scaledImage = originalImage.getScaledInstance(width, height, Image.SCALE_SMOOTH);
BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = bufferedImage.createGraphics();
// 绘制缩放后的图片
g2d.drawImage(scaledImage, 0, 0, null);
g2d.dispose();
// 输出为指定格式
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(bufferedImage, formatName, baos);
return baos.toByteArray();
}
/**
* 按质量压缩图片
*
* @param originalImage 原始图片
* @param formatName 图片格式
* @param quality 压缩质量(0.1-1.0)
* @return 压缩后的图片数据
* @throws IOException IO异常
*/
private static byte[] compressImageQuality(BufferedImage originalImage, String formatName, float quality) throws IOException {
// 创建压缩参数
Iterator<ImageWriter> writers = ImageIO.getImageWritersByFormatName(formatName);
if (!writers.hasNext()) {
log.warn("找不到合适的图片写入器");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(originalImage, formatName, baos);
return baos.toByteArray();
}
ImageWriter writer = writers.next();
ImageWriteParam param = writer.getDefaultWriteParam();
// 设置压缩参数
if (param.canWriteCompressed()) {
param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
param.setCompressionQuality(quality);
}
// 写入压缩后的图片数据
ByteArrayOutputStream compressedOutputStream = new ByteArrayOutputStream();
writer.setOutput(ImageIO.createImageOutputStream(compressedOutputStream));
writer.write(null, new javax.imageio.IIOImage(originalImage, null, null), param);
writer.dispose();
return compressedOutputStream.toByteArray();
}
/**
* 检查图片是否包含透明度
*
* @param image 图片
* @return 是否包含透明度
*/
private static boolean hasAlpha(BufferedImage image) {
return image.getType() == BufferedImage.TYPE_4BYTE_ABGR ||
image.getType() == BufferedImage.TYPE_INT_ARGB ||
image.getColorModel().hasAlpha();
}
/**
* 判断图片是否需要压缩(超过1MB)
*
* @param imageData 图片数据
* @return 是否需要压缩
*/
public static boolean needCompress(byte[] imageData) {
return imageData.length > DEFAULT_TRIGGER_SIZE;
}
}

View File

@ -216,4 +216,5 @@ public class LogAspect {
return o instanceof MultipartFile || o instanceof HttpServletRequest || o instanceof HttpServletResponse
|| o instanceof BindingResult;
}
}

View File

@ -64,4 +64,5 @@ public class AppPersonnelInfoBo extends BaseEntity {
* ID号
*/
private String code;
}

View File

@ -9,6 +9,8 @@ import com.fuyuanshen.app.domain.vo.AppDeviceShareVo;
import com.fuyuanshen.common.mybatis.core.mapper.BaseMapperPlus;
import org.apache.ibatis.annotations.Param;
import java.util.List;
/**
* 设备分享Mapper接口
*
@ -27,4 +29,6 @@ public interface AppDeviceShareMapper extends BaseMapperPlus<AppDeviceShare, App
* @return 设备分享
*/
Page<AppDeviceShareVo> selectWebDeviceShareList(@Param("bo") AppDeviceShareBo bo, Page<AppDeviceShareVo> page);
void deleteByDeviceIds(@Param("deviceIds") List<Long> deviceIds);
}

View File

@ -67,4 +67,6 @@ public interface IAppDeviceShareService {
Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid);
TableDataInfo<AppDeviceShareVo> otherDeviceShareList(AppDeviceShareBo bo, PageQuery pageQuery);
void deleteByDeviceIds(List<Long> deviceIds);
}

View File

@ -166,4 +166,9 @@ public class AppDeviceShareServiceImpl implements IAppDeviceShareService {
});
return TableDataInfo.build(result);
}
@Override
public void deleteByDeviceIds(List<Long> deviceIds) {
baseMapper.deleteByDeviceIds(deviceIds);
}
}

View File

@ -3,6 +3,12 @@
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.fuyuanshen.app.mapper.AppDeviceShareMapper">
<delete id="deleteByDeviceIds">
delete from app_device_share where device_id in
<foreach item="item" collection="deviceIds" separator="," open="(" close=")">
#{item}
</foreach>
</delete>
<select id="otherDeviceShareList" resultType="com.fuyuanshen.app.domain.vo.AppDeviceShareVo">
select d.device_name,

View File

@ -13,12 +13,14 @@ import com.fuyuanshen.common.satoken.utils.LoginHelper;
import com.fuyuanshen.common.web.core.BaseController;
import com.fuyuanshen.customer.mapper.CustomerMapper;
import com.fuyuanshen.equipment.domain.Device;
import com.fuyuanshen.equipment.domain.DeviceType;
import com.fuyuanshen.equipment.domain.dto.DeviceExcelImportDTO;
import com.fuyuanshen.equipment.domain.dto.ImportResult;
import com.fuyuanshen.equipment.domain.form.DeviceForm;
import com.fuyuanshen.equipment.domain.query.DeviceQueryCriteria;
import com.fuyuanshen.equipment.domain.vo.CustomerVo;
import com.fuyuanshen.equipment.excel.DeviceImportParams;
import com.fuyuanshen.equipment.excel.HeadValidateListener;
import com.fuyuanshen.equipment.excel.UploadDeviceDataListener;
import com.fuyuanshen.equipment.mapper.DeviceMapper;
import com.fuyuanshen.equipment.mapper.DeviceTypeMapper;
@ -39,7 +41,10 @@ import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
/**
* @Description:
@ -163,7 +168,7 @@ public class DeviceController extends BaseController {
@Operation(summary = "导出数据设备")
@GetMapping(value = "/download")
@GetMapping(value = "/download1")
public R<Void> exportDevice(HttpServletResponse response, DeviceQueryCriteria criteria) throws IOException {
List<Device> devices = deviceService.queryAll(criteria);
exportService.export(devices, response);
@ -171,9 +176,36 @@ public class DeviceController extends BaseController {
}
/**
* 导出设备数据(包含完整设备类型信息)
*
* @param response HttpServletResponse对象
* @param criteria 查询条件
* @return R<Void>
*/
@Operation(summary = "导出设备数据(包含完整设备类型信息)")
@GetMapping(value = "/download")
public R<Void> exportDeviceWithFullTypeInfo(HttpServletResponse response, DeviceQueryCriteria criteria) {
// 获取所有符合条件的设备
List<Device> devices = deviceService.queryAll(criteria);
// 获取所有设备类型信息
List<DeviceType> deviceTypes = deviceTypeService.queryDeviceTypes();
// 导出数据(包含完整设备类型信息)
exportService.exportWithTypeInfo(devices, deviceTypes, response);
return R.ok();
}
/**
* 导入设备数据
*
* @param file
* @return
* @throws BadRequestException
*/
@Operation(summary = "导入设备数据")
@PostMapping(value = "/import", consumes = "multipart/form-data")
public R<ImportResult> importData(@Parameter(name = "文件", required = true) @RequestPart("file") MultipartFile file) throws BadRequestException {
@PostMapping(value = "/import1", consumes = "multipart/form-data")
public R<ImportResult> importData1(@Parameter(name = "文件", required = true) @RequestPart("file") MultipartFile file) throws BadRequestException {
String suffix = FileUtil.getExtensionName(file.getOriginalFilename());
if (!("xlsx".equalsIgnoreCase(suffix))) {
@ -207,6 +239,105 @@ public class DeviceController extends BaseController {
}
}
/**
* 导入设备数据
*
* @param file 文件
* @return R<ImportResult>
*/
@Operation(summary = "导入设备数据")
@PostMapping(value = "/import", consumes = "multipart/form-data")
public R<ImportResult> importData(@Parameter(name = "文件", required = true) @RequestPart("file") MultipartFile file) throws BadRequestException {
String suffix = FileUtil.getExtensionName(file.getOriginalFilename());
if (!("xlsx".equalsIgnoreCase(suffix))) {
throw new BadRequestException("只能上传Excel——xlsx格式文件");
}
// 检查文件大小限制为100MB
if (file.getSize() > 100 * 10 * 1024 * 1024) {
throw new BadRequestException("文件大小不能超过100MB");
}
// 校验模板
validateExcelTemplate(file);
ImportResult result = new ImportResult();
try {
LoginUser loginUser = LoginHelper.getLoginUser();
DeviceImportParams params = DeviceImportParams.builder().ossService(ossService)
.deviceService(deviceService).tenantId(loginUser.getTenantId())
.file(file).filePath("").deviceMapper(deviceMapper).deviceTypeService(deviceTypeService)
.deviceTypeMapper(deviceTypeMapper).userId(loginUser.getUserId())
.build();
// 创建监听器
UploadDeviceDataListener listener = new UploadDeviceDataListener(params);
// 读取Excel
EasyExcel.read(file.getInputStream(), DeviceExcelImportDTO.class, listener).sheet().doRead();
// 获取导入结果
result = listener.getImportResult();
// 设置响应消息
String message = String.format("成功导入 %d 条数据,失败 %d 条", result.getSuccessCount(), result.getFailureCount());
// 返回带有正确泛型的响应
return R.ok(message, result);
} catch (Exception e) {
log.error("导入设备数据出错: {}", e.getMessage(), e);
// 在异常情况下,设置默认结果
String errorMessage = String.format("导入失败: %s。成功 %d 条,失败 %d 条", e.getMessage(), result.getSuccessCount(), result.getFailureCount());
// 使用新方法确保类型正确
return R.fail(errorMessage, result);
}
}
/**
* 校验Excel模板是否正确
*
* @param file MultipartFile对象
* @throws BadRequestException 当模板不正确时抛出异常
*/
private void validateExcelTemplate(MultipartFile file) throws BadRequestException {
try {
// 创建一个只读取表头的监听器
HeadValidateListener headValidateListener = new HeadValidateListener();
// 使用EasyExcel读取表头
EasyExcel.read(file.getInputStream(), headValidateListener)
.sheet()
.headRowNumber(0)
.doRead();
// 获取读取到的表头信息
List<String> actualHeaders = headValidateListener.getHeadNames();
// 定义必需的表头
Set<String> requiredHeaders = new HashSet<>(Arrays.asList(
"设备名称", "设备类型名称", "设备图片", "设备MAC", "蓝牙名称", "设备IMEI",
"备注", "是否支持蓝牙", "定位方式", "通讯方式",
"型号字典用于APP页面跳转", "型号字典用于PC页面跳转"
));
// 检查必需的表头是否都存在
Set<String> actualHeaderSet = new HashSet<>(actualHeaders);
if (!actualHeaderSet.containsAll(requiredHeaders)) {
requiredHeaders.removeAll(actualHeaderSet);
throw new BadRequestException("Excel模板缺少必需的列: " + String.join(", ", requiredHeaders));
}
// 检查第三列索引为2是否为"设备图片"
if (actualHeaders.size() > 2 && !"设备图片".equals(actualHeaders.get(2))) {
throw new BadRequestException("Excel模板不正确第三列必须是'设备图片'列");
}
} catch (BadRequestException e) {
throw e; // 直接重新抛出
} catch (Exception e) {
log.error("校验Excel模板时发生错误: {}", e.getMessage(), e);
throw new BadRequestException("校验Excel模板时发生错误: " + e.getMessage());
}
}
}

View File

@ -4,6 +4,8 @@ import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.metadata.property.ExcelContentProperty;
import com.fuyuanshen.common.core.utils.file.ImageCompressUtil;
import com.fuyuanshen.common.redis.utils.RedisUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -12,89 +14,218 @@ import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.Base64;
import java.util.HashSet;
import java.util.Set;
/**
* @author: 默苍璃
* @date: 2025-06-0618:56
*/
public class IgnoreFailedImageConverter implements Converter<URL> {
private static final Logger logger = LoggerFactory.getLogger(IgnoreFailedImageConverter.class);
// 重试次数
private static final int MAX_RETRIES = 3;
// 指数退避初始延迟(毫秒)
private static final int INITIAL_DELAY = 1000;
// 图片压缩阈值(1MB)
private static final int COMPRESSION_THRESHOLD = 1024 * 1024;
// 压缩目标大小(100KB)
private static final int COMPRESSION_TARGET = 100 * 1024;
// 用于跟踪本次任务中使用到的URL缓存键
private static final ThreadLocal<Set<String>> USED_CACHE_KEYS = new ThreadLocal<Set<String>>() {
@Override
protected Set<String> initialValue() {
return new HashSet<>();
}
};
@Override
public Class<?> supportJavaTypeKey() {
return URL.class;
}
// @Override
// public CellDataTypeEnum supportExcelTypeKey() {
// return CellDataTypeEnum.STRING;
// }
@Override
public WriteCellData<?> convertToExcelData(URL value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) {
if (value == null) {
logger.debug("图片URL为空");
logger.info("图片URL为空");
return new WriteCellData<>(new byte[0]);
}
try {
logger.debug("开始加载图片: {}", value);
URLConnection conn = value.openConnection();
// 增加连接和读取超时时间
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);
}
String cacheKey = "excel:image:" + value.toString();
// 将当前使用的缓存键添加到集合中
USED_CACHE_KEYS.get().add(cacheKey);
// 尝试从缓存获取
String cachedData = RedisUtils.getCacheObject(cacheKey);
if (cachedData != null) {
// 从缓存中读取Base64编码的数据并解码
byte[] cachedBytes = Base64.getDecoder().decode(cachedData);
logger.info("从缓存获取图片数据: {}, 大小: {} 字节", value, cachedBytes.length);
return new WriteCellData<>(cachedBytes);
}
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]);
}
// 缓存未命中从URL加载
// 尝试多次加载图片
for (int attempt = 1; attempt <= MAX_RETRIES; attempt++) {
try {
logger.info("开始加载图片: {}, 尝试次数: {}", value, attempt);
URLConnection conn = value.openConnection();
// 增加连接和读取超时时间
conn.setConnectTimeout(10000); // 10秒连接超时
conn.setReadTimeout(30000); // 30秒读取超时
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);
// 添加User-Agent避免被服务器拦截
conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36 ExcelExporter/1.0");
// 如果是HTTP连接设置一些额外的属性
if (conn instanceof HttpURLConnection) {
HttpURLConnection httpConn = (HttpURLConnection) conn;
httpConn.setRequestMethod("GET");
// 不使用缓存
httpConn.setUseCaches(false);
// 跟随重定向
httpConn.setInstanceFollowRedirects(true);
// 检查响应码
int responseCode = httpConn.getResponseCode();
if (responseCode != HttpURLConnection.HTTP_OK) {
logger.info("HTTP响应码异常: {}, URL: {}", responseCode, value);
if (attempt < MAX_RETRIES) {
// 等待后重试
waitForRetry(attempt);
continue;
} else {
// 将空数据写入缓存
RedisUtils.setCacheObject(cacheKey, "");
return new WriteCellData<>(new byte[0]);
}
}
}
long contentLength = conn.getContentLengthLong();
logger.info("连接建立成功,图片大小: {} 字节", contentLength);
// 检查内容长度是否有效
if (contentLength == 0) {
logger.info("图片文件为空: {}", value);
if (attempt < MAX_RETRIES) {
waitForRetry(attempt);
continue;
} else {
// 将空数据写入缓存
RedisUtils.setCacheObject(cacheKey, "");
return new WriteCellData<>(new byte[0]);
}
}
// 限制图片大小(防止过大文件导致内存问题)
if (contentLength > 10 * 1024 * 1024) { // 10MB限制
logger.info("图片文件过大 ({} bytes),跳过加载: {}", contentLength, value);
// 将空数据写入缓存
RedisUtils.setCacheObject(cacheKey, "");
return new WriteCellData<>(new byte[0]);
}
logger.debug("成功读取图片数据,大小: {} 字节", bytes.length);
return new WriteCellData<>(bytes);
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.info("读取到空的图片数据: {}", value);
if (attempt < MAX_RETRIES) {
waitForRetry(attempt);
continue;
} else {
// 将空数据写入缓存
RedisUtils.setCacheObject(cacheKey, "");
return new WriteCellData<>(new byte[0]);
}
}
// 如果图片大于1MB则进行压缩
if (bytes.length > COMPRESSION_THRESHOLD) {
logger.info("图片大小超过1MB ({} bytes),开始压缩", bytes.length);
long beforeCompressSize = bytes.length;
bytes = ImageCompressUtil.compressImage(bytes, COMPRESSION_TARGET);
long afterCompressSize = bytes.length;
logger.info("图片压缩完成,压缩前大小: {} bytes, 压缩后大小: {} bytes, 压缩率: {}%",
beforeCompressSize, afterCompressSize,
String.format("%.2f", (1.0 - (double) afterCompressSize / beforeCompressSize) * 100));
}
logger.info("成功读取图片数据,大小: {} 字节", bytes.length);
// 将数据写入缓存不设置过期时间使用Base64编码存储
String encodedData = Base64.getEncoder().encodeToString(bytes);
RedisUtils.setCacheObject(cacheKey, encodedData);
return new WriteCellData<>(bytes);
}
} catch (Exception e) {
logger.info("图片加载失败: {}, 尝试次数: {}, 原因: {}", value, attempt, e.getMessage(), e);
if (attempt < MAX_RETRIES) {
// 等待后重试
waitForRetry(attempt);
} else {
// 最后一次尝试也失败了
logger.info("图片加载最终失败,已重试 {} 次: {}", MAX_RETRIES, value, e);
// 将空数据写入缓存
RedisUtils.setCacheObject(cacheKey, "");
return new WriteCellData<>(new byte[0]); // 返回空数组而不是 null
}
}
} catch (Exception e) {
// 静默忽略错误,只记录日志
logger.warn("图片加载失败: {}, 原因: {}", value, e.getMessage(), e);
// return null; // 返回null表示不写入图片
return new WriteCellData<>(new byte[0]); // 返回空数组而不是 null
}
// 所有尝试都失败了
// 将空数据写入缓存
RedisUtils.setCacheObject(cacheKey, "");
return new WriteCellData<>(new byte[0]);
}
/**
* 清理未使用的缓存
* 任务结束后调用此方法删除本次任务中未使用的URL缓存
*/
public static void cleanUnusedCache() {
Set<String> usedKeys = USED_CACHE_KEYS.get();
if (usedKeys != null && !usedKeys.isEmpty()) {
// 获取所有图片缓存键
Iterable<String> allKeys = RedisUtils.keys("excel:image:*");
if (allKeys != null) {
// 删除未使用的缓存
for (String key : allKeys) {
if (!usedKeys.contains(key)) {
RedisUtils.deleteObject(key);
logger.info("删除未使用的缓存: {}", key);
}
}
}
// 清理ThreadLocal
USED_CACHE_KEYS.remove();
}
}
/**
* 等待重试,使用指数退避策略
*
* @param attempt 当前尝试次数
*/
private void waitForRetry(int attempt) {
try {
long delay = (long) INITIAL_DELAY * (1L << (attempt - 1)); // 指数退避
logger.info("等待 {} 毫秒后重试...", delay);
Thread.sleep(delay);
} catch (InterruptedException ie) {
Thread.currentThread().interrupt();
}
}
/**
* 替代 FileUtils.readInputStream 的自定义方法
*
@ -111,14 +242,15 @@ public class IgnoreFailedImageConverter implements Converter<URL> {
while ((bytesRead = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
totalBytes += bytesRead;
// 如果读取的数据过大,提前终止
if (totalBytes > 10 * 1024 * 1024) { // 10MB限制
logger.warn("读取的图片数据超过10MB限制提前终止");
logger.info("读取的图片数据超过10MB限制提前终止");
break;
}
}
return outputStream.toByteArray();
}
}

View File

@ -60,7 +60,7 @@ public class DeviceType extends TenantEntity {
private String networkWay;
@Schema(title = "通讯方式", example = "通讯方式 0:4G;1:蓝牙,2 4G&蓝牙")
private Integer communicationMode;
private String communicationMode;
/**
* 创建人名称

View File

@ -1,24 +1,21 @@
package com.fuyuanshen.equipment.domain.dto;
import com.alibaba.excel.annotation.ExcelIgnore;
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.annotation.write.style.ColumnWidth;
import com.alibaba.excel.annotation.write.style.ContentRowHeight;
import com.alibaba.excel.annotation.write.style.HeadRowHeight;
import com.alibaba.excel.converters.bytearray.ByteArrayImageConverter;
import lombok.Data;
/**
* @author: 默苍璃
* @date: 2025-06-0710:00
* @date: 2025-06-07 10:00
*/
@Data
@HeadRowHeight(20) // 表头行高
@ContentRowHeight(100) // 内容行高
public class DeviceExcelImportDTO {
// @ExcelProperty("设备类型")
// private Long deviceType;
@ExcelProperty("设备名称")
@ColumnWidth(20)
private String deviceName;
@ -26,14 +23,23 @@ public class DeviceExcelImportDTO {
@ExcelProperty("设备类型名称")
private String typeName;
// @ExcelProperty(value = "设备图片", converter = ByteArrayImageConverter.class)
// @ColumnWidth(15)
// private byte[] devicePic;
//
// // 添加图片写入方法
// public void setDevicePicFromBytes(byte[] bytes) {
// this.devicePic = bytes;
// }
/**
* 设备图片
* 导入时的图片处理方式:
* 在导入过程中图片不是通过Excel单元格中的文本数据如URL来处理的
* 而是直接从Excel文件中提取嵌入的图片数据
* 代码通过POI库直接读取Excel中的图片对象然后上传到OSS并生成URL
*/
@ExcelProperty("设备图片")
@ColumnWidth(20)
private byte[] devicePic;
/**
* 用于存储实际图片数据的字段
* 使用@ExcelIgnore注解忽略该字段避免创建额外的列
*/
@ExcelIgnore
private byte[] imageData;
@ExcelProperty("设备MAC")
@ColumnWidth(20)
@ -46,18 +52,46 @@ public class DeviceExcelImportDTO {
@ExcelProperty("蓝牙名称")
private String bluetoothName;
// @ExcelProperty("设备SN")
// @ColumnWidth(20)
// private String deviceSn;
//
// @ExcelProperty("经度")
// private String longitude;
//
// @ExcelProperty("纬度")
// private String latitude;
@ExcelProperty("备注")
@ColumnWidth(30)
private String remark;
/*
*设备类型数据
*/
@ExcelProperty("是否支持蓝牙")
@ColumnWidth(15)
private String isSupportBle;
@ExcelProperty("定位方式")
@ColumnWidth(20)
private String locateMode;
@ExcelProperty("通讯方式")
@ColumnWidth(20)
private String communicationMode;
/**
* 型号字典用于APP页面跳转
* app_model_dictionary
*/
@ExcelProperty("型号字典用于APP页面跳转")
@ColumnWidth(20)
private String appModelDictionary;
/**
* 型号字典用于PC页面跳转
* pc_model_dictionary
*/
@ExcelProperty("型号字典用于PC页面跳转")
@ColumnWidth(20)
private String pcModelDictionary;
/**
* 错误原因
*/
@ExcelProperty("错误原因")
@ColumnWidth(30)
private String errorMessage;
}

View File

@ -0,0 +1,100 @@
package com.fuyuanshen.equipment.domain.dto;
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.annotation.write.style.ColumnWidth;
import com.alibaba.excel.annotation.write.style.ContentRowHeight;
import com.alibaba.excel.annotation.write.style.HeadRowHeight;
import com.fuyuanshen.equipment.converter.IgnoreFailedImageConverter;
import lombok.Data;
import java.net.URL;
/**
* 设备及完整类型信息导出DTO
*
* @author: 默苍璃
* @date: 2025-11-0416:25
*/
@Data
@HeadRowHeight(20) // 表头行高
@ContentRowHeight(100) // 内容行高
public class DeviceWithTypeExcelExportDTO {
@ExcelProperty("设备名称")
@ColumnWidth(20)
private String deviceName;
@ExcelProperty("设备类型名称")
@ColumnWidth(20)
private String typeName;
@ExcelProperty(value = "设备图片", converter = IgnoreFailedImageConverter.class)
// @ExcelProperty(value = "设备图片")
@ColumnWidth(30) // 设置图片列宽度
private URL devicePic; // 使用URL类型
@ExcelProperty("设备MAC")
@ColumnWidth(20)
private String deviceMac;
@ExcelProperty("蓝牙名称")
@ColumnWidth(20)
private String bluetoothName;
@ExcelProperty("设备IMEI")
@ColumnWidth(20)
private String deviceImei;
@ExcelProperty("备注")
@ColumnWidth(30)
private String remark;
/**
* 绑定状态
* 0 未绑定
* 1 已绑定
*/
@ExcelProperty("绑定状态")
@ColumnWidth(20)
private String bindingStatus;
@ExcelProperty("创建时间")
@ColumnWidth(20)
private String createTime;
@ExcelProperty("创建人")
@ColumnWidth(20)
private String createBy;
/*
*设备类型数据
*/
@ExcelProperty("是否支持蓝牙")
@ColumnWidth(15)
private String isSupportBle;
@ExcelProperty("定位方式")
@ColumnWidth(20)
private String locateMode;
@ExcelProperty("通讯方式")
@ColumnWidth(20)
private String communicationMode;
/**
* 型号字典用于APP页面跳转
* app_model_dictionary
*/
@ExcelProperty("型号字典用于APP页面跳转")
@ColumnWidth(20)
private String appModelDictionary;
/**
* 型号字典用于PC页面跳转
* pc_model_dictionary
*/
@ExcelProperty("型号字典用于PC页面跳转")
@ColumnWidth(20)
private String pcModelDictionary;
}

View File

@ -26,8 +26,6 @@ public class DeviceForm {
@Schema(title = "客户号")
private Long customerId;
/*@Schema(value = "设备编号")
private String deviceNo;*/
@NotBlank(message = "设备名称不能为空")
@Schema(title = "设备名称", required = true)
@ -56,4 +54,25 @@ public class DeviceForm {
@Schema(title = "备注")
private String remark;
// 设备类型相关字段
@Schema(title = "设备类型名称")
private String typeName;
@Schema(title = "是否支持蓝牙")
private String isSupportBle;
@Schema(title = "定位方式")
private String locateMode;
@Schema(title = "通讯方式")
private String communicationMode;
@Schema(title = "APP模型字典")
private String appModelDictionary;
@Schema(title = "PC模型字典")
private String pcModelDictionary;
}

View File

@ -0,0 +1,54 @@
package com.fuyuanshen.equipment.excel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.fastjson2.JSON;
import lombok.extern.slf4j.Slf4j;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/**
* 读取表头 监听器
*
* @author: 默苍璃
* @date: 2025-11-1809:36
*/
@Slf4j
public class HeadValidateListener extends AnalysisEventListener<Map<Integer, String>> {
private List<String> headNames = new ArrayList<>();
/**
* When analysis one row trigger invoke function.
*
* @param headMap one row value. It is same as {@link AnalysisContext#readRowHolder()}
* @param context analysis context
*/
@Override
public void invoke(Map<Integer, String> headMap, AnalysisContext context) {
log.info("解析到一条数据: {}", JSON.toJSONString(headMap));
// 按顺序收集表头名称
for (int i = 0; i < headMap.size(); i++) {
headNames.add(headMap.get(i));
}
}
/**
* if have something to do after all analysis
*
* @param context
*/
@Override
public void doAfterAllAnalysed(AnalysisContext context) {
// 读取完成后不需要额外操作
}
public List<String> getHeadNames() {
return headNames;
}
}

View File

@ -5,8 +5,7 @@ import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.read.listener.ReadListener;
import com.alibaba.fastjson2.JSON;
import com.fuyuanshen.common.core.domain.model.LoginUser;
import com.fuyuanshen.common.satoken.utils.LoginHelper;
import com.fuyuanshen.common.core.utils.file.ImageCompressUtil;
import com.fuyuanshen.equipment.constants.DeviceConstants;
import com.fuyuanshen.equipment.domain.Device;
import com.fuyuanshen.equipment.domain.DeviceType;
@ -46,38 +45,41 @@ public class UploadDeviceDataListener implements ReadListener<DeviceExcelImportD
}
public ImportResult getImportResult() {
public ImportResult getImportResult1() {
ImportResult result = new ImportResult();
result.setSuccessCount(successCount);
result.setFailureCount(failureCount);
// 准备失败记录(包含图片数据)
// 准备失败记录
List<DeviceExcelImportDTO> failedRecordsWithImages = new ArrayList<>();
for (DeviceExcelImportDTO failedRecord : failedRecords) {
// 创建一个映射,用于存储失败记录行号与图片数据的关系
Map<Integer, byte[]> errorReportImageMap = new HashMap<>();
for (int i = 0; i < failedRecords.size(); i++) {
DeviceExcelImportDTO failedRecord = failedRecords.get(i);
// 创建副本,避免修改原始数据
DeviceExcelImportDTO recordWithImage = new DeviceExcelImportDTO();
BeanUtil.copyProperties(failedRecord, recordWithImage);
// 设置图片占位符,让用户知道这里有图片
// recordWithImage.setDevicePic("[图片]");
failedRecordsWithImages.add(recordWithImage);
// 获取原始行号
Integer rowIndex = null;
Integer originalRowIndex = null;
for (Map.Entry<Integer, DeviceExcelImportDTO> entry : rowDtoMap.entrySet()) {
if (entry.getValue() == failedRecord) {
rowIndex = entry.getKey();
originalRowIndex = entry.getKey();
break;
}
}
if (rowIndex != null) {
// 创建副本,避免修改原始数据
DeviceExcelImportDTO recordWithImage = new DeviceExcelImportDTO();
BeanUtil.copyProperties(failedRecord, recordWithImage);
// 设置图片数据
byte[] imageData = rowImageMap.get(rowIndex);
// 保存图片数据用于错误报告
if (originalRowIndex != null) {
byte[] imageData = rowImageMap.get(originalRowIndex);
if (imageData != null) {
// recordWithImage.setDevicePicFromBytes(imageData);
errorReportImageMap.put(i, imageData); // 使用新的列表索引
}
failedRecordsWithImages.add(recordWithImage);
} else {
failedRecordsWithImages.add(failedRecord);
}
}
@ -99,7 +101,8 @@ public class UploadDeviceDataListener implements ReadListener<DeviceExcelImportD
// 保存错误报告到文件(包含图片)
File errorFile = new File(errorDir, fileName);
EasyExcel.write(errorFile, DeviceExcelImportDTO.class).registerWriteHandler(new ImageWriteHandler()) // 添加图片处理
EasyExcel.write(errorFile, DeviceExcelImportDTO.class)
.registerWriteHandler(new ImageWriteHandler(errorReportImageMap)) // 传递图片数据映射
.sheet("失败数据").doWrite(failedRecordsWithImages);
// 生成访问URL
@ -114,6 +117,79 @@ public class UploadDeviceDataListener implements ReadListener<DeviceExcelImportD
return result;
}
public ImportResult getImportResult() {
ImportResult result = new ImportResult();
result.setSuccessCount(successCount);
result.setFailureCount(failureCount);
// 准备失败记录
List<DeviceExcelImportDTO> failedRecordsWithImages = new ArrayList<>();
for (int i = 0; i < failedRecords.size(); i++) {
DeviceExcelImportDTO failedRecord = failedRecords.get(i);
// 创建副本,避免修改原始数据
DeviceExcelImportDTO recordWithImage = new DeviceExcelImportDTO();
BeanUtil.copyProperties(failedRecord, recordWithImage);
// 获取原始行号
Integer originalRowIndex = null;
for (Map.Entry<Integer, DeviceExcelImportDTO> entry : rowDtoMap.entrySet()) {
if (entry.getValue() == failedRecord) {
originalRowIndex = entry.getKey();
break;
}
}
// 保存图片数据用于错误报告
if (originalRowIndex != null) {
byte[] imageData = rowImageMap.get(originalRowIndex);
if (imageData != null) {
// recordWithImage.setImageData(imageData);
recordWithImage.setDevicePic(imageData);
}
}
failedRecordsWithImages.add(recordWithImage);
}
result.setFailedRecords(failedRecordsWithImages);
// 生成错误报告
if (!failedRecordsWithImages.isEmpty()) {
try {
// 生成唯一的文件名
String fileName = "import_errors_" + System.currentTimeMillis() + ".xlsx";
// 创建错误报告目录
String errorDirPath = params.getFilePath() + DeviceConstants.ERROR_REPORT_DIR;
File errorDir = new File(errorDirPath);
if (!errorDir.exists() && !errorDir.mkdirs()) {
log.error("无法创建错误报告目录: {}", errorDirPath);
return result;
}
// 保存错误报告到文件(包含图片)
File errorFile = new File(errorDir, fileName);
EasyExcel.write(errorFile, DeviceExcelImportDTO.class)
.sheet("失败数据").doWrite(failedRecordsWithImages);
// 生成访问URL
SysOssVo upload = params.getOssService().upload(errorFile);
String url = upload.getUrl();
// 将http://替换为https://但不影响已经是https://的URL
if (url.startsWith("http://")) {
url = "https://" + url.substring(7);
}
result.setErrorExcelUrl(url);
log.info("错误报告已保存: {}", errorFile.getAbsolutePath());
log.info("错误报告已保存: {}", url);
} catch (Exception e) {
log.error("生成错误报告失败", e);
}
}
return result;
}
@Override
public void invoke(DeviceExcelImportDTO data, AnalysisContext context) {
@ -142,14 +218,232 @@ public class UploadDeviceDataListener implements ReadListener<DeviceExcelImportD
private void processDataRowByRow() {
LoginUser loginUser = LoginHelper.getLoginUser();
// 如果没有数据,直接返回
if (rowIndexList.isEmpty()) {
return;
}
// 批量处理设备类型,减少数据库查询次数
Set<String> typeNames = new HashSet<>();
for (Integer rowIndex : rowIndexList) {
Device device = rowDeviceMap.get(rowIndex);
if (device != null && device.getTypeName() != null) {
typeNames.add(device.getTypeName());
}
}
// 批量查询所有设备类型
Map<String, DeviceType> deviceTypeMap = new HashMap<>();
if (!typeNames.isEmpty()) {
List<DeviceType> deviceTypes = params.getDeviceTypeService().queryByNames(new ArrayList<>(typeNames));
for (DeviceType deviceType : deviceTypes) {
deviceTypeMap.put(deviceType.getTypeName(), deviceType);
}
}
for (Integer rowIndex : rowIndexList) {
Device device = rowDeviceMap.get(rowIndex);
DeviceExcelImportDTO originalDto = rowDtoMap.get(rowIndex);
try {
// 从缓存中获取设备类型
DeviceType deviceType = deviceTypeMap.get(device.getTypeName());
DeviceForm deviceForm = new DeviceForm();
deviceForm.setDeviceName(device.getDeviceName());
deviceForm.setDeviceType(deviceType != null ? deviceType.getId() : null);
deviceForm.setTypeName(device.getTypeName());
deviceForm.setRemark(device.getRemark());
deviceForm.setDeviceMac(device.getDeviceMac());
deviceForm.setDeviceImei(device.getDeviceImei());
deviceForm.setBluetoothName(device.getBluetoothName());
deviceForm.setDevicePic(device.getDevicePic());
// 设置设备类型相关信息,供设备服务内部创建设备类型时使用
if (deviceType == null) {
deviceForm.setIsSupportBle(originalDto.getIsSupportBle());
deviceForm.setLocateMode(originalDto.getLocateMode());
deviceForm.setCommunicationMode(originalDto.getCommunicationMode());
deviceForm.setAppModelDictionary(originalDto.getAppModelDictionary());
deviceForm.setPcModelDictionary(originalDto.getPcModelDictionary());
}
params.getDeviceService().addDevice(deviceForm);
successCount++;
log.info("行 {} 数据插入成功", rowIndex);
} catch (Exception e) {
failureCount++;
originalDto.setErrorMessage(e.getMessage());
// originalDto.setErrorMessage("数据有误,请核对模板后,确认数据无误后,重新再试!!!");
failedRecords.add(originalDto);
log.error("行 {} 数据插入失败: {}", rowIndex, e.getMessage());
}
}
}
/**
* 处理图片数据
*/
private void processImages() {
// 如果没有数据行,直接返回
if (rowIndexList.isEmpty()) {
return;
}
// 检查是否有图片需要处理
try {
// 只在确实有图片时才打开文件处理
boolean hasImages = false;
try (OPCPackage opcPackage = OPCPackage.open(params.getFile().getInputStream())) {
ZipSecureFile.setMinInflateRatio(-1.0d);
XSSFWorkbook workbook = new XSSFWorkbook(opcPackage);
XSSFSheet sheet = workbook.getSheetAt(0);
XSSFDrawing drawing = sheet.getDrawingPatriarch();
if (drawing != null) {
for (XSSFShape shape : drawing.getShapes()) {
if (shape instanceof XSSFPicture) {
hasImages = true;
break;
}
}
}
} catch (Exception e) {
log.warn("检查图片时发生异常: {}", e.getMessage());
// 如果检查图片失败,继续处理数据
return;
}
// 如果没有图片,直接返回
if (!hasImages) {
log.info("未检测到图片,跳过图片处理");
return;
}
// 有图片时才进行处理
try (OPCPackage opcPackage = OPCPackage.open(params.getFile().getInputStream())) {
ZipSecureFile.setMinInflateRatio(-1.0d);
XSSFWorkbook workbook = new XSSFWorkbook(opcPackage);
XSSFSheet sheet = workbook.getSheetAt(0);
XSSFDrawing drawing = sheet.getDrawingPatriarch();
if (drawing == null) return;
for (XSSFShape shape : drawing.getShapes()) {
if (shape instanceof XSSFPicture) {
XSSFPicture picture = (XSSFPicture) shape;
XSSFClientAnchor anchor = picture.getPreferredSize();
int rowIndex = anchor.getRow1();
int colIndex = anchor.getCol1();
if (colIndex == 2) {
Device device = rowDeviceMap.get(rowIndex);
if (device != null) {
try {
byte[] imageData = picture.getPictureData().getData();
// 检查图片大小如果超过5MB则拒绝上传
if (imageData.length > 5 * 1024 * 1024) {
String errorMsg = "图片大小超过5MB限制请压缩后重新上传";
log.warn("行 {} 图片过大: {} bytes", rowIndex, imageData.length);
// 将错误信息添加到该行的DTO中
DeviceExcelImportDTO dto = rowDtoMap.get(rowIndex);
if (dto != null) {
dto.setErrorMessage(errorMsg);
failedRecords.add(dto);
failureCount++;
}
device.setDevicePic(null); // 设置为空,让插入继续
continue; // 跳过当前图片处理
}
// 表示Excel表格中的第3列因为索引从0开始计算
String extraValue = getCellValue(sheet, rowIndex, 2);
String imageUrl = uploadAndGenerateUrl(imageData, extraValue);
device.setDevicePic(imageUrl);
// 2. 保存图片数据到DTO用于错误报告
rowImageMap.put(rowIndex, imageData);
} catch (Exception e) {
log.error("行 {} 图片处理失败: {}", rowIndex, e.getMessage());
device.setDevicePic(null); // 设置为空,让插入继续
}
}
}
}
}
} catch (Exception e) {
log.error("图片处理失败:{}", e.getMessage(), e);
}
} catch (Exception e) {
log.warn("图片处理过程中发生异常: {}", e.getMessage());
}
}
private void processDataRowByRow1() {
for (Integer rowIndex : rowIndexList) {
Device device = rowDeviceMap.get(rowIndex);
DeviceExcelImportDTO originalDto = rowDtoMap.get(rowIndex);
try {
// 设备类型
DeviceType deviceType = params.getDeviceTypeService().queryByName(device.getTypeName());
// params.getDeviceService().save(device);
// 如果设备类型不存在,则创建新的设备类型
if (deviceType == null) {
DeviceType newDeviceType = new DeviceType();
newDeviceType.setTypeName(device.getTypeName());
newDeviceType.setIsSupportBle("".equals(originalDto.getIsSupportBle()) || "1".equals(originalDto.getIsSupportBle()));
// 设置定位方式
if (originalDto.getLocateMode() != null) {
switch (originalDto.getLocateMode()) {
case "":
newDeviceType.setLocateMode("0");
break;
case "GPS":
newDeviceType.setLocateMode("1");
break;
case "基站":
newDeviceType.setLocateMode("2");
break;
case "wifi":
newDeviceType.setLocateMode("3");
break;
case "北斗":
newDeviceType.setLocateMode("4");
break;
default:
newDeviceType.setLocateMode(originalDto.getLocateMode());
}
}
// 设置通讯方式
if (originalDto.getCommunicationMode() != null) {
switch (originalDto.getCommunicationMode()) {
case "4G":
newDeviceType.setCommunicationMode("0");
break;
case "蓝牙":
newDeviceType.setCommunicationMode("1");
break;
case "4G&蓝牙":
newDeviceType.setCommunicationMode("2");
break;
default:
newDeviceType.setCommunicationMode(originalDto.getCommunicationMode());
}
}
newDeviceType.setAppModelDictionary(originalDto.getAppModelDictionary());
newDeviceType.setPcModelDictionary(originalDto.getPcModelDictionary());
// 创建新的设备类型
params.getDeviceTypeService().create(newDeviceType);
// 重新查询确保获取到正确的ID
deviceType = params.getDeviceTypeService().queryByName(device.getTypeName());
}
DeviceForm deviceForm = new DeviceForm();
deviceForm.setDeviceName(device.getDeviceName());
deviceForm.setDeviceType(deviceType.getId());
@ -163,13 +457,19 @@ public class UploadDeviceDataListener implements ReadListener<DeviceExcelImportD
log.info("行 {} 数据插入成功", rowIndex);
} catch (Exception e) {
failureCount++;
originalDto.setErrorMessage(e.getMessage());
// originalDto.setErrorMessage("数据有误,请核对模板后,确认数据无误后,重新再试!!!");
failedRecords.add(originalDto);
log.error("行 {} 数据插入失败: {}", rowIndex, e.getMessage());
}
}
}
private void processImages() {
/**
* 处理图片数据
*/
private void processImages1() {
try (OPCPackage opcPackage = OPCPackage.open(params.getFile().getInputStream())) {
ZipSecureFile.setMinInflateRatio(-1.0d);
XSSFWorkbook workbook = new XSSFWorkbook(opcPackage);
@ -190,6 +490,22 @@ public class UploadDeviceDataListener implements ReadListener<DeviceExcelImportD
if (device != null) {
try {
byte[] imageData = picture.getPictureData().getData();
// 检查图片大小如果超过5MB则拒绝上传
if (imageData.length > 5 * 1024 * 1024) {
String errorMsg = "图片大小超过5MB限制请压缩后重新上传";
log.warn("行 {} 图片过大: {} bytes", rowIndex, imageData.length);
// 将错误信息添加到该行的DTO中
DeviceExcelImportDTO dto = rowDtoMap.get(rowIndex);
if (dto != null) {
dto.setErrorMessage(errorMsg);
failedRecords.add(dto);
failureCount++;
}
device.setDevicePic(null); // 设置为空,让插入继续
continue; // 跳过当前图片处理
}
// 表示Excel表格中的第3列因为索引从0开始计算
String extraValue = getCellValue(sheet, rowIndex, 2);
String imageUrl = uploadAndGenerateUrl(imageData, extraValue);
@ -224,9 +540,16 @@ public class UploadDeviceDataListener implements ReadListener<DeviceExcelImportD
return null;
}
try {
// 如果图片超过500KB则进行压缩优化
if (imageData.length > 500 * 1024) {
log.info("检测到大图片 ({} bytes),正在进行压缩优化...", imageData.length);
imageData = ImageCompressUtil.compressImage(imageData, 500 * 1024); // 压缩到500KB以下
}
String fileExtension = "jpg";
String newFileName = "PS_" + new Random(8) + "." + fileExtension;
SysOssVo upload = params.getOssService().upload(imageData, newFileName);
log.info("图片保存成功URL: {}", upload.getUrl());
return upload.getUrl();
} catch (Exception e) {
log.error("保存图片失败", e);
@ -234,4 +557,5 @@ public class UploadDeviceDataListener implements ReadListener<DeviceExcelImportD
}
}
}
}

View File

@ -4,66 +4,95 @@ import com.alibaba.excel.write.handler.SheetWriteHandler;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import com.alibaba.excel.write.metadata.holder.WriteWorkbookHolder;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFClientAnchor;
import org.apache.poi.xssf.usermodel.XSSFDrawing;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import java.util.Map;
/**
* @author: 默苍璃
* @date: 2025-06-0718:05
* @date: 2025-06-07 18:05
*/
public class ImageWriteHandler implements SheetWriteHandler {
private Map<Integer, byte[]> imageMap;
private int imageColIndex;
public ImageWriteHandler(Map<Integer, byte[]> imageMap) {
this(imageMap, 2);
}
public ImageWriteHandler(Map<Integer, byte[]> imageMap, int imageColIndex) {
this.imageMap = imageMap;
this.imageColIndex = imageColIndex;
}
@Override
public void afterSheetCreate(WriteWorkbookHolder writeWorkbookHolder, WriteSheetHolder writeSheetHolder) {
if (imageMap == null || imageMap.isEmpty()) {
return;
}
Workbook workbook = writeWorkbookHolder.getWorkbook();
Sheet sheet = writeSheetHolder.getSheet();
// 设置图片列的宽度
sheet.setColumnWidth(imageColIndex, 20 * 256); // 20个字符宽度
for (Map.Entry<Integer, byte[]> entry : imageMap.entrySet()) {
int dataRowIndex = entry.getKey();
int rowIndex = dataRowIndex + 1; // 跳过标题行
byte[] imageData = entry.getValue();
if (imageData != null && imageData.length > 0) {
insertImage(workbook, sheet, rowIndex, imageData);
}
}
}
private void insertImage(Workbook workbook, Sheet sheet, int rowIndex, byte[] imageData) {
try {
// 获取或创建行(确保行存在)
Row row = sheet.getRow(rowIndex);
if (row == null) {
row = sheet.createRow(rowIndex);
}
// 设置合适的行高(不要设置过大)
row.setHeightInPoints(60); // 60磅足够显示小图
// 添加图片到工作簿
int pictureIdx = workbook.addPicture(imageData, Workbook.PICTURE_TYPE_JPEG);
// 创建绘图对象
Drawing<?> drawing = sheet.createDrawingPatriarch();
// 关键修改使用MOVE_DONT_RESIZE锚点类型避免影响行高
ClientAnchor anchor = workbook.getCreationHelper().createClientAnchor();
anchor.setAnchorType(ClientAnchor.AnchorType.MOVE_DONT_RESIZE);
anchor.setCol1(imageColIndex);
anchor.setRow1(rowIndex);
anchor.setCol2(imageColIndex + 1);
anchor.setRow2(rowIndex + 1);
// 设置较小的偏移量,确保图片在单元格内
anchor.setDx1(0);
anchor.setDy1(0);
anchor.setDx2(512 * 5); // 约5个字符宽度
anchor.setDy2(256 * 4); // 约4行高度
// 插入图片
Picture picture = drawing.createPicture(anchor, pictureIdx);
// 不要调用resize(),避免自动调整影响行高
// picture.resize();
} catch (Exception e) {
System.err.println("插入图片失败,行: " + rowIndex);
e.printStackTrace();
}
}
@Override
public void beforeSheetCreate(WriteWorkbookHolder writeWorkbookHolder, WriteSheetHolder writeSheetHolder) {
// 不需要实现
}
@Override
public void afterSheetCreate(WriteWorkbookHolder writeWorkbookHolder, WriteSheetHolder writeSheetHolder) {
Workbook workbook = writeWorkbookHolder.getWorkbook();
Sheet sheet = writeSheetHolder.getSheet();
// 获取设备图片列索引假设是第4列索引3
int imageColIndex = 3;
// 遍历所有行
for (int rowIndex = 1; rowIndex <= sheet.getLastRowNum(); rowIndex++) { // 从第2行开始跳过标题
Row row = sheet.getRow(rowIndex);
if (row == null) continue;
Cell imageCell = row.getCell(imageColIndex);
if (imageCell == null) continue;
// 获取图片数据
byte[] imageData = null;
if (imageCell.getCellType() == CellType.STRING) {
// 处理Base64编码的图片如果需要
}
if (imageData != null && imageData.length > 0) {
try {
// 添加图片到工作表
int pictureIdx = workbook.addPicture(imageData, Workbook.PICTURE_TYPE_JPEG);
// 创建绘图对象
if (sheet instanceof XSSFSheet) {
XSSFSheet xssfSheet = (XSSFSheet) sheet;
XSSFDrawing drawing = xssfSheet.createDrawingPatriarch();
// 设置图片位置
XSSFClientAnchor anchor = new XSSFClientAnchor(0, 0, 0, 0, imageColIndex, rowIndex, imageColIndex + 1, rowIndex + 1);
// 创建图片
drawing.createPicture(anchor, pictureIdx);
}
// 清除单元格内容
imageCell.setBlank();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}

View File

@ -137,4 +137,13 @@ public interface DeviceMapper extends BaseMapper<Device> {
List<DeviceUsageFrequencyVo> getDeviceUsageFrequency(@Param("days") int days);
List<OnlineStatusVo> queryOnlineStatusList();
/**
* 根据设备类型ID查询设备数量
*
* @param deviceTypeId 设备类型ID
* @return 设备数量
*/
int countByDeviceTypeId(@Param("deviceTypeId") Long deviceTypeId);
}

View File

@ -51,4 +51,13 @@ public interface DeviceTypeMapper extends BaseMapper<DeviceType> {
*/
DeviceType queryByName(@Param("criteria") DeviceTypeQueryCriteria criteria);
/**
* 根据名称列表查询设备类型
*
* @param typeNames
* @return
*/
List<DeviceType> selectByNames(@Param("typeNames") List<String> typeNames);
}

View File

@ -49,6 +49,15 @@ public interface DeviceTypeService extends IService<DeviceType> {
*/
DeviceType queryByName(String typeName);
/**
* 根据设备类型名称列表查询设备类型
*
* @param typeNames 设备类型名称列表
* @return List<DeviceType>
*/
List<DeviceType> queryByNames(List<String> typeNames);
/**
* 新增设备类型
*

View File

@ -3,18 +3,20 @@ package com.fuyuanshen.equipment.service.impl;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.util.DateUtils;
import com.fuyuanshen.equipment.domain.Device;
import com.fuyuanshen.equipment.domain.DeviceType;
import com.fuyuanshen.equipment.domain.dto.DeviceExcelExportDTO;
import com.fuyuanshen.equipment.domain.dto.DeviceWithTypeExcelExportDTO;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.concurrent.Semaphore;
import java.util.Map;
import java.util.concurrent.*;
import java.util.stream.Collectors;
/**
@ -26,6 +28,9 @@ import java.util.stream.Collectors;
public class DeviceExportService {
public void export(List<Device> devices, HttpServletResponse response) {
long startTime = System.currentTimeMillis();
log.info("开始导出设备列表,设备数量: {}", devices.size());
try {
String fileName = "设备列表_" + System.currentTimeMillis() + ".xlsx";
// 使用URLEncoder进行RFC 5987编码
@ -36,26 +41,20 @@ public class DeviceExportService {
// 使用RFC 5987标准编码文件名
response.setHeader("Content-disposition", "attachment;filename*=UTF-8''" + encodedFileName);
// 转换为DTO列表
List<DeviceExcelExportDTO> dtoList = devices.stream().map(device -> {
// 转换为DTO列表,使用并行流加速处理
List<DeviceExcelExportDTO> dtoList = devices.parallelStream().map(device -> {
long deviceProcessStartTime = System.currentTimeMillis();
DeviceExcelExportDTO dto = new DeviceExcelExportDTO();
// 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.setRemark(device.getRemark());
dto.setTypeName(device.getTypeName());
dto.setCreateBy(device.getCreateByName());
Integer deviceStatus = device.getDeviceStatus();
Integer bindingStatus = device.getBindingStatus();
// dto.setDeviceStatus(deviceStatus == 1 ? "正常" : "失效");
dto.setBindingStatus(bindingStatus == 1 ? "已绑定" : "未绑定");
// 时间戳转换
dto.setCreateTime(DateUtils.format(device.getCreateTime(), "yyyy-MM-dd HH:mm:ss"));
@ -63,54 +62,270 @@ public class DeviceExportService {
// 处理图片URL转换
handleDevicePic(device, dto);
long deviceProcessEndTime = System.currentTimeMillis();
log.info("单个设备处理耗时: {} ms, 设备ID: {}", (deviceProcessEndTime - deviceProcessStartTime), device.getId());
return dto;
}).collect(Collectors.toList());
// 写入Excel
long excelWriteStartTime = System.currentTimeMillis();
EasyExcel.write(response.getOutputStream(), DeviceExcelExportDTO.class).sheet("设备数据").doWrite(dtoList);
long excelWriteEndTime = System.currentTimeMillis();
long endTime = System.currentTimeMillis();
log.info("设备列表导出完成,总耗时: {} ms设备数量: {}Excel写入耗时: {} ms",
(endTime - startTime), devices.size(), (excelWriteEndTime - excelWriteStartTime));
} catch (IOException e) {
log.error("导出Excel失败", e);
throw new RuntimeException("导出Excel失败", e);
}
}
/**
* 导出设备数据(包含完整设备类型信息)
*
* @param devices
* @param deviceTypes
* @param response
*/
public void exportWithTypeInfo(List<Device> devices, List<DeviceType> deviceTypes, HttpServletResponse response) {
long startTime = System.currentTimeMillis();
log.info("开始导出设备列表(含类型详情),设备数量: {}", devices.size());
try {
String fileName = "设备列表_含类型详情_" + System.currentTimeMillis() + ".xlsx";
// 使用URLEncoder进行RFC 5987编码
String encodedFileName = URLEncoder.encode(fileName, StandardCharsets.UTF_8.name()).replaceAll("\\+", "%20");
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
response.setCharacterEncoding("utf-8");
// 使用RFC 5987标准编码文件名
response.setHeader("Content-disposition", "attachment;filename*=UTF-8''" + encodedFileName);
// 构建设备类型映射
Map<Long, DeviceType> deviceTypeMap = deviceTypes.stream()
.collect(Collectors.toMap(DeviceType::getId, deviceType -> deviceType));
// 转换为DTO列表使用并行流加速处理
List<DeviceWithTypeExcelExportDTO> dtoList = devices.parallelStream().map(device -> {
long deviceProcessStartTime = System.currentTimeMillis();
DeviceWithTypeExcelExportDTO dto = new DeviceWithTypeExcelExportDTO();
dto.setDeviceName(device.getDeviceName());
dto.setDeviceMac(device.getDeviceMac());
// 设备IMEI
dto.setDeviceImei(device.getDeviceImei());
// 蓝牙名称
dto.setBluetoothName(device.getBluetoothName());
dto.setRemark(device.getRemark());
dto.setTypeName(device.getTypeName());
dto.setCreateBy(device.getCreateByName());
Integer bindingStatus = device.getBindingStatus();
dto.setBindingStatus(bindingStatus == 1 ? "已绑定" : "未绑定");
// 时间戳转换
dto.setCreateTime(DateUtils.format(device.getCreateTime(), "yyyy-MM-dd HH:mm:ss"));
// 获取设备类型详细信息
DeviceType deviceType = deviceTypeMap.get(device.getDeviceType());
if (deviceType != null) {
// 处理是否支持蓝牙
if (deviceType.getIsSupportBle() != null) {
dto.setIsSupportBle(deviceType.getIsSupportBle() ? "" : "");
} else {
dto.setIsSupportBle("未知");
}
// 处理定位方式
if (deviceType.getLocateMode() != null) {
dto.setLocateMode(convertLocateMode(deviceType.getLocateMode()));
} else {
dto.setLocateMode("");
}
// 处理通讯方式
if (deviceType.getCommunicationMode() != null) {
dto.setCommunicationMode(convertCommunicationMode(deviceType.getCommunicationMode()));
} else {
dto.setCommunicationMode("");
}
// 处理APP页面跳转字典
dto.setAppModelDictionary(deviceType.getAppModelDictionary() != null ? deviceType.getAppModelDictionary() : "");
// 处理PC页面跳转字典
dto.setPcModelDictionary(deviceType.getPcModelDictionary() != null ? deviceType.getPcModelDictionary() : "");
} else {
dto.setIsSupportBle("未知");
dto.setLocateMode("");
dto.setCommunicationMode("");
dto.setAppModelDictionary("");
dto.setPcModelDictionary("");
}
// 处理图片URL转换
handleDevicePicForTypeExport(device, dto);
return dto;
}).collect(Collectors.toList());
// 写入Excel
long excelWriteStartTime = System.currentTimeMillis();
EasyExcel.write(response.getOutputStream(), DeviceWithTypeExcelExportDTO.class).sheet("设备数据含类型详情").doWrite(dtoList);
long excelWriteEndTime = System.currentTimeMillis();
long endTime = System.currentTimeMillis();
log.info("设备列表(含类型详情)导出完成,总耗时: {} ms设备数量: {}Excel写入耗时: {} ms",
(endTime - startTime), devices.size(), (excelWriteEndTime - excelWriteStartTime));
} catch (IOException e) {
log.error("导出Excel失败", e);
throw new RuntimeException("导出Excel失败", e);
}
}
/**
* 转换定位方式代码为中文描述
*
* @param locateMode 定位方式代码 (0:无;1:GPS;2:基站;3:wifi;4:北斗)
* @return 中文描述
*/
private String convertLocateMode(String locateMode) {
switch (locateMode) {
case "0":
return "";
case "1":
return "GPS";
case "2":
return "基站";
case "3":
return "wifi";
case "4":
return "北斗";
default:
return locateMode;
}
}
/**
* 转换联网方式代码为中文描述
*
* @param networkWay 联网方式代码 (0:无;1:4G;2:WIFI)
* @return 中文描述
*/
private String convertNetworkWay(String networkWay) {
switch (networkWay) {
case "0":
return "";
case "1":
return "4G";
case "2":
return "WIFI";
default:
return networkWay;
}
}
/**
* 转换通讯方式代码为中文描述
*
* @param communicationMode 通讯方式代码 (0:4G;1:蓝牙)
* @return 中文描述
*/
private String convertCommunicationMode(String communicationMode) {
switch (communicationMode) {
case "0":
return "4G";
case "1":
return "蓝牙";
case "2":
return "4G&蓝牙";
default:
return communicationMode;
}
}
// 在DeviceExportService中添加并发控制
private static final Semaphore imageLoadSemaphore = new Semaphore(5); // 最多同时加载5张图片
private static final Semaphore imageLoadSemaphore = new Semaphore(10); // 增加到最多同时加载10张图片
private void handleDevicePic(Device device, DeviceExcelExportDTO dto) {
String picUrl = device.getDevicePic();
log.debug("处理设备图片设备ID: {}, 图片URL: {}", device.getId(), picUrl);
if (picUrl != null && !picUrl.trim().isEmpty()) {
try {
// 获取加载图片的许可
imageLoadSemaphore.acquire();
// 获取加载图片的许可,带超时控制
if (!imageLoadSemaphore.tryAcquire(5, TimeUnit.SECONDS)) {
dto.setDevicePic(null);
return;
}
try {
// 自动将HTTP转换为HTTPS以避免重定向问题
if (picUrl.startsWith("http://")) {
picUrl = "https://" + picUrl.substring(7);
log.debug("自动将HTTP转换为HTTPS: {}", picUrl);
}
// 尝试创建URL对象会自动验证格式
URL url = new URL(picUrl);
dto.setDevicePic(url);
log.debug("成功设置设备图片URL到DTO");
} finally {
// 释放许可
imageLoadSemaphore.release();
}
} catch (Exception e) {
log.warn("设置设备图片失败设备ID: {}, URL: {}, 错误: {}", device.getId(), picUrl, e.getMessage());
dto.setDevicePic(null);
}
} else {
log.debug("设备没有设置图片设备ID: {}", device.getId());
dto.setDevicePic(null);
}
}
private void handleDevicePicForTypeExport(Device device, DeviceWithTypeExcelExportDTO dto) {
String picUrl = device.getDevicePic();
if (picUrl != null && !picUrl.trim().isEmpty()) {
try {
// 获取加载图片的许可,带超时控制
if (!imageLoadSemaphore.tryAcquire(5, TimeUnit.SECONDS)) {
dto.setDevicePic(null);
return;
}
try {
picUrl = convertUrl(picUrl);
// 尝试创建URL对象会自动验证格式
URL url = new URL(picUrl);
dto.setDevicePic(url);
} finally {
// 释放许可
imageLoadSemaphore.release();
}
} catch (Exception e) {
dto.setDevicePic(null);
}
} else {
dto.setDevicePic(null);
}
}
/**
* 转换图片URL为HTTP
* 转回minio格式
*
* @param originalUrl 原始URL
* @return 转换后的URL
*/
public String convertUrl(String originalUrl) {
String result = originalUrl.replace("https://fuyuanshen.com", "http://120.79.224.186:9000");
result = result.replace("http://fuyuanshen.com", "http://120.79.224.186:9000");
return result;
}
}

View File

@ -192,22 +192,104 @@ public class DeviceServiceImpl extends ServiceImpl<DeviceMapper, Device> impleme
throw new BadRequestException("设备IMEI已存在");
}
DeviceTypeQueryCriteria queryCriteria = new DeviceTypeQueryCriteria();
queryCriteria.setDeviceTypeId(deviceForm.getDeviceType());
queryCriteria.setCustomerId(LoginHelper.getUserId());
DeviceTypeGrants typeGrants = deviceTypeGrantsMapper.selectById(queryCriteria.getDeviceTypeId());
if (typeGrants == null) {
throw new Exception("设备类型不存在!!!");
DeviceTypeGrants typeGrants = new DeviceTypeGrants();
if (deviceForm.getDeviceType() != null) {
DeviceTypeQueryCriteria queryCriteria = new DeviceTypeQueryCriteria();
queryCriteria.setDeviceTypeId(deviceForm.getDeviceType());
typeGrants = deviceTypeGrantsMapper.selectById(queryCriteria.getDeviceTypeId());
if (typeGrants == null) {
throw new Exception("设备类型不存在!!!");
}
}
DeviceType deviceTypes = deviceTypeMapper.selectById(typeGrants.getDeviceTypeId());
if (deviceTypes == null) {
// 检查设备类型是否存在,如果不存在则创建
DeviceType deviceType = null;
if (deviceForm.getDeviceType() != null) {
deviceType = deviceTypeMapper.selectById(typeGrants.getDeviceTypeId());
} else if (deviceForm.getTypeName() != null) {
deviceType = deviceTypeMapper.selectOne(new QueryWrapper<DeviceType>().eq("type_name", deviceForm.getTypeName()));
}
if (deviceType == null && deviceForm.getTypeName() != null) {
// 创建新的设备类型
DeviceType newDeviceType = new DeviceType();
newDeviceType.setTypeName(deviceForm.getTypeName());
newDeviceType.setIsSupportBle("".equals(deviceForm.getIsSupportBle()) || "1".equals(deviceForm.getIsSupportBle()));
// 设置定位方式
if (deviceForm.getLocateMode() != null) {
switch (deviceForm.getLocateMode()) {
case "":
newDeviceType.setLocateMode("0");
break;
case "GPS":
newDeviceType.setLocateMode("1");
break;
case "基站":
newDeviceType.setLocateMode("2");
break;
case "wifi":
newDeviceType.setLocateMode("3");
break;
case "北斗":
newDeviceType.setLocateMode("4");
break;
default:
newDeviceType.setLocateMode(deviceForm.getLocateMode());
}
}
// 设置通讯方式
if (deviceForm.getCommunicationMode() != null) {
switch (deviceForm.getCommunicationMode()) {
case "4G":
newDeviceType.setCommunicationMode("0");
break;
case "蓝牙":
newDeviceType.setCommunicationMode("1");
break;
case "4G&蓝牙":
newDeviceType.setCommunicationMode("2");
break;
default:
newDeviceType.setCommunicationMode(deviceForm.getCommunicationMode());
}
}
newDeviceType.setAppModelDictionary(deviceForm.getAppModelDictionary());
newDeviceType.setPcModelDictionary(deviceForm.getPcModelDictionary());
// 校验设备类型名称
List<DeviceType> existingTypes = deviceTypeMapper.selectList(new QueryWrapper<DeviceType>().eq("type_name", newDeviceType.getTypeName()));
if (CollectionUtil.isNotEmpty(existingTypes)) {
throw new RuntimeException("设备类型名称已存在,无法新增!!!");
}
LoginUser loginUser = LoginHelper.getLoginUser();
newDeviceType.setCreateByName(loginUser.getNickname());
deviceTypeMapper.insert(newDeviceType);
// 重新查询确保获取到正确的ID
deviceType = deviceTypeMapper.selectOne(new QueryWrapper<DeviceType>().eq("type_name", deviceForm.getTypeName()));
// 自动授权给自己
DeviceTypeGrants deviceTypeGrants = new DeviceTypeGrants();
deviceTypeGrants.setDeviceTypeId(deviceType.getId());
deviceTypeGrants.setCustomerId(loginUser.getUserId());
deviceTypeGrants.setGrantorCustomerId(loginUser.getUserId());
deviceTypeGrants.setGrantedAt(new Date());
deviceTypeGrantsMapper.insert(deviceTypeGrants);
}
if (deviceType == null) {
throw new Exception("设备类型不存在!!!");
}
// 保存图片并获取URL
if (deviceForm.getFile() != null) {
String fileHash = fileHashUtil.hash(deviceForm.getFile());
SysOssVo upload = ossService.updateHash(deviceForm.getFile(),fileHash);
SysOssVo upload = ossService.updateHash(deviceForm.getFile(), fileHash);
// 设置图片路径
deviceForm.setDevicePic(upload.getUrl());
}
@ -221,8 +303,8 @@ public class DeviceServiceImpl extends ServiceImpl<DeviceMapper, Device> impleme
device.setCurrentOwnerId(loginUser.getUserId());
device.setOriginalOwnerId(loginUser.getUserId());
device.setCreateByName(loginUser.getNickname());
device.setTypeName(deviceTypes.getTypeName());
device.setDeviceType(deviceTypes.getId());
device.setTypeName(deviceType.getTypeName());
device.setDeviceType(deviceType.getId());
if (device.getDeviceImei() != null) {
device.setPubTopic("A/" + device.getDeviceImei());
device.setSubTopic("B/" + device.getDeviceImei());
@ -282,7 +364,7 @@ public class DeviceServiceImpl extends ServiceImpl<DeviceMapper, Device> impleme
// 处理上传的图片
if (deviceForm.getFile() != null) {
String fileHash = fileHashUtil.hash(deviceForm.getFile());
SysOssVo oss = ossService.updateHash(deviceForm.getFile(),fileHash);
SysOssVo oss = ossService.updateHash(deviceForm.getFile(), fileHash);
// 强制将HTTP替换为HTTPS
if (oss.getUrl() != null && oss.getUrl().startsWith("http://")) {
oss.setUrl(oss.getUrl().replaceFirst("^http://", "https://"));

View File

@ -159,6 +159,21 @@ public class DeviceTypeServiceImpl extends ServiceImpl<DeviceTypeMapper, DeviceT
}
/**
* 根据设备类型名称列表查询设备类型
*
* @param typeNames 设备类型名称列表
* @return List<DeviceType>
*/
@Override
public List<DeviceType> queryByNames(List<String> typeNames) {
if (typeNames == null || typeNames.isEmpty()) {
return new ArrayList<>();
}
return deviceTypeMapper.selectByNames(typeNames);
}
/**
* 新增设备类型
*
@ -209,6 +224,16 @@ public class DeviceTypeServiceImpl extends ServiceImpl<DeviceTypeMapper, DeviceT
throw new RuntimeException("设备类型不存在");
}
if (!deviceType.getTypeName().equals(resources.getTypeName())) {
int count = deviceMapper.countByDeviceTypeId(deviceType.getId());
if (count > 0) {
throw new RuntimeException("该设备类型下已有绑定设备,无法修改设备类型名称!!!");
}
}
// List<Device> devices = deviceMapper.selectList(new QueryWrapper<Device>()
// .eq("device_type", deviceTypeGrants.getDeviceTypeId()));
// if (CollectionUtil.isNotEmpty(devices)) {

View File

@ -497,4 +497,11 @@
FROM device a left join device_type b on a.device_type = b.id where b.communication_mode in (0, 2) and a.online_status in (1,2)
</select>
<!-- 根据设备类型ID查询设备数量 -->
<select id="countByDeviceTypeId" resultType="int">
SELECT COUNT(*)
FROM device
WHERE device_type = #{deviceTypeId}
</select>
</mapper>

View File

@ -58,4 +58,14 @@
</where>
</select>
<!-- 根据名称列表查询设备类型 -->
<select id="selectByNames" resultMap="BaseResultMap">
SELECT dt.*
FROM device_type dt
WHERE dt.type_name IN
<foreach collection="typeNames" item="name" open="(" separator="," close=")">
#{name}
</foreach>
</select>
</mapper>

View File

@ -6,6 +6,7 @@ import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fuyuanshen.common.core.utils.file.ImageCompressUtil;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import com.fuyuanshen.common.core.constant.CacheNames;
@ -143,7 +144,7 @@ public class SysOssServiceImpl implements ISysOssService, OssService {
lqw.eq(StringUtils.isNotBlank(bo.getFileSuffix()), SysOss::getFileSuffix, bo.getFileSuffix());
lqw.eq(StringUtils.isNotBlank(bo.getUrl()), SysOss::getUrl, bo.getUrl());
lqw.between(params.get("beginCreateTime") != null && params.get("endCreateTime") != null,
SysOss::getCreateTime, params.get("beginCreateTime"), params.get("endCreateTime"));
SysOss::getCreateTime, params.get("beginCreateTime"), params.get("endCreateTime"));
lqw.eq(ObjectUtil.isNotNull(bo.getCreateBy()), SysOss::getCreateBy, bo.getCreateBy());
lqw.eq(StringUtils.isNotBlank(bo.getService()), SysOss::getService, bo.getService());
lqw.orderByAsc(SysOss::getOssId);
@ -169,7 +170,7 @@ public class SysOssServiceImpl implements ISysOssService, OssService {
@Override
public int updateHashById(long ossId, String fileHash) {
return baseMapper.updateHashById(ossId,fileHash);
return baseMapper.updateHashById(ossId, fileHash);
}
@ -191,6 +192,7 @@ public class SysOssServiceImpl implements ISysOssService, OssService {
storage.download(sysOss.getFileName(), response.getOutputStream(), response::setContentLengthLong);
}
/**
* 上传 MultipartFile 到对象存储服务,并保存文件信息到数据库
*
@ -209,14 +211,22 @@ public class SysOssServiceImpl implements ISysOssService, OssService {
OssClient storage = OssFactory.instance();
UploadResult uploadResult;
try {
uploadResult = storage.uploadSuffix(file.getBytes(), suffix, file.getContentType());
byte[] imageData = file.getBytes();
// 检查是否需要压缩
if (ImageCompressUtil.needCompress(imageData)) {
// 压缩到100KB以内
imageData = ImageCompressUtil.compressImage(imageData);
// 使用压缩后的数据
}
uploadResult = storage.uploadSuffix(imageData, suffix, file.getContentType());
} catch (IOException e) {
throw new ServiceException(e.getMessage());
}
// 保存文件信息
return buildResultEntity(originalfileName, suffix, storage.getConfigKey(), uploadResult,hash);
return buildResultEntity(originalfileName, suffix, storage.getConfigKey(), uploadResult, hash);
}
/**
* 上传 MultipartFile 到对象存储服务,并保存文件信息到数据库
*
@ -236,7 +246,7 @@ public class SysOssServiceImpl implements ISysOssService, OssService {
throw new ServiceException(e.getMessage());
}
// 保存文件信息
return buildResultEntity(originalfileName, suffix, storage.getConfigKey(), uploadResult,null);
return buildResultEntity(originalfileName, suffix, storage.getConfigKey(), uploadResult, null);
}
/**
@ -252,11 +262,10 @@ public class SysOssServiceImpl implements ISysOssService, OssService {
OssClient storage = OssFactory.instance();
UploadResult uploadResult = storage.uploadSuffix(file, suffix);
// 保存文件信息
return buildResultEntity(originalfileName, suffix, storage.getConfigKey(), uploadResult,null);
return buildResultEntity(originalfileName, suffix, storage.getConfigKey(), uploadResult, null);
}
/**
* 上传二进制数据到对象存储服务,并保存文件信息到数据库
*
@ -281,7 +290,7 @@ public class SysOssServiceImpl implements ISysOssService, OssService {
uploadResult = storage.uploadSuffix(data, suffix, "image/jpeg"); // 假设是图片类型,可以根据实际需要修改
// 保存文件信息
return buildResultEntity(fileName, suffix, storage.getConfigKey(), uploadResult,null);
return buildResultEntity(fileName, suffix, storage.getConfigKey(), uploadResult, null);
}