forked from dyf/fys-Multi-tenant
WEB:导出数据设备
This commit is contained in:
@ -28,6 +28,14 @@ public interface DeviceService extends IService<Device> {
|
||||
*/
|
||||
TableDataInfo<Device> queryAll(DeviceQueryCriteria criteria, Page<Device> page) throws IOException;
|
||||
|
||||
/**
|
||||
* 查询所有数据不分页
|
||||
*
|
||||
* @param criteria 条件参数
|
||||
* @return List<DeviceDto>
|
||||
*/
|
||||
List<Device> queryAll(DeviceQueryCriteria criteria);
|
||||
|
||||
/**
|
||||
* 查询所有设备信息
|
||||
*
|
||||
|
@ -0,0 +1,86 @@
|
||||
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.dto.DeviceExcelExportDTO;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
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.stream.Collectors;
|
||||
|
||||
/**
|
||||
* @author: 默苍璃
|
||||
* @date: 2025-06-0618:22
|
||||
*/
|
||||
@Service
|
||||
public class DeviceExportService {
|
||||
|
||||
public void export(List<Device> devices, HttpServletResponse response) {
|
||||
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);
|
||||
|
||||
// 转换为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.setDeviceName(device.getDeviceName());
|
||||
dto.setDeviceMac(device.getDeviceMac());
|
||||
// 设备IMEI
|
||||
dto.setDeviceImei(device.getDeviceImei());
|
||||
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.setCreateTime(DateUtils.format(device.getCreateTime(), "yyyy-MM-dd HH:mm:ss"));
|
||||
|
||||
// 处理图片URL转换
|
||||
handleDevicePic(device, dto);
|
||||
|
||||
return dto;
|
||||
}).collect(Collectors.toList());
|
||||
|
||||
// 写入Excel
|
||||
EasyExcel.write(response.getOutputStream(), DeviceExcelExportDTO.class).sheet("设备数据").doWrite(dtoList);
|
||||
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException("导出Excel失败", e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private void handleDevicePic(Device device, DeviceExcelExportDTO dto) {
|
||||
String picUrl = device.getDevicePic();
|
||||
if (picUrl != null && !picUrl.trim().isEmpty()) {
|
||||
try {
|
||||
// 尝试创建URL对象(会自动验证格式)
|
||||
dto.setDevicePic(new URL(picUrl));
|
||||
} catch (MalformedURLException e) {
|
||||
// 不是有效URL时设置为null
|
||||
dto.setDevicePic(null);
|
||||
}
|
||||
} else {
|
||||
dto.setDevicePic(null);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -4,11 +4,11 @@ import cn.hutool.core.bean.BeanUtil;
|
||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.fuyuanshen.common.core.domain.PageResult;
|
||||
import com.fuyuanshen.common.core.utils.PageUtil;
|
||||
import com.fuyuanshen.common.core.domain.model.LoginUser;
|
||||
import com.fuyuanshen.common.mybatis.core.page.TableDataInfo;
|
||||
import com.fuyuanshen.common.satoken.utils.LoginHelper;
|
||||
import com.fuyuanshen.customer.domain.vo.ConsumerVo;
|
||||
import com.fuyuanshen.customer.domain.Customer;
|
||||
import com.fuyuanshen.customer.mapper.CustomerMapper;
|
||||
import com.fuyuanshen.equipment.constants.DeviceConstants;
|
||||
import com.fuyuanshen.equipment.domain.Device;
|
||||
import com.fuyuanshen.equipment.domain.DeviceType;
|
||||
@ -19,8 +19,8 @@ import com.fuyuanshen.equipment.domain.vo.CustomerVo;
|
||||
import com.fuyuanshen.equipment.mapper.DeviceMapper;
|
||||
import com.fuyuanshen.equipment.mapper.DeviceTypeMapper;
|
||||
import com.fuyuanshen.equipment.service.DeviceService;
|
||||
import com.fuyuanshen.customer.domain.Customer;
|
||||
import com.fuyuanshen.customer.mapper.CustomerMapper;
|
||||
import com.fuyuanshen.system.domain.vo.SysOssVo;
|
||||
import com.fuyuanshen.system.service.ISysOssService;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
@ -55,6 +55,8 @@ public class DeviceServiceImpl extends ServiceImpl<DeviceMapper, Device> impleme
|
||||
|
||||
private final CustomerMapper customerMapper;
|
||||
|
||||
private final ISysOssService ossService;
|
||||
|
||||
|
||||
/**
|
||||
* 分页查询设备
|
||||
@ -83,6 +85,12 @@ public class DeviceServiceImpl extends ServiceImpl<DeviceMapper, Device> impleme
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public List<Device> queryAll(DeviceQueryCriteria criteria) {
|
||||
return deviceMapper.findAll(criteria);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 查询所有设备
|
||||
*
|
||||
@ -113,13 +121,16 @@ public class DeviceServiceImpl extends ServiceImpl<DeviceMapper, Device> impleme
|
||||
}
|
||||
|
||||
// 保存图片并获取URL
|
||||
String imageUrl = saveDeviceImage(deviceForm.getFile(), deviceForm.getDeviceName());
|
||||
// String imageUrl = saveDeviceImage(deviceForm.getFile(), deviceForm.getDeviceName());
|
||||
SysOssVo upload = ossService.upload(deviceForm.getFile());
|
||||
// 设置图片路径
|
||||
deviceForm.setDevicePic(imageUrl);
|
||||
deviceForm.setDevicePic(upload.getUrl());
|
||||
|
||||
// 转换对象并插入数据库
|
||||
Device device = new Device();
|
||||
device.setCurrentOwnerId(LoginHelper.getUserId());
|
||||
LoginUser loginUser = LoginHelper.getLoginUser();
|
||||
device.setCurrentOwnerId(loginUser.getUserId());
|
||||
device.setCreateByName(loginUser.getNickname());
|
||||
BeanUtil.copyProperties(deviceForm, device, true);
|
||||
|
||||
deviceMapper.insert(device);
|
||||
@ -155,8 +166,10 @@ public class DeviceServiceImpl extends ServiceImpl<DeviceMapper, Device> impleme
|
||||
Device device = devices.get(0);
|
||||
|
||||
// 处理上传的图片
|
||||
String imageUrl = saveDeviceImage(deviceForm.getFile(), device.getDeviceName());
|
||||
deviceForm.setDevicePic(imageUrl);
|
||||
// String imageUrl = saveDeviceImage(deviceForm.getFile(), device.getDeviceName());
|
||||
SysOssVo upload = ossService.upload(deviceForm.getFile());
|
||||
// 设置图片路径
|
||||
deviceForm.setDevicePic(upload.getUrl());
|
||||
|
||||
// 更新字段
|
||||
BeanUtil.copyProperties(deviceForm, device, true);
|
||||
@ -253,14 +266,14 @@ public class DeviceServiceImpl extends ServiceImpl<DeviceMapper, Device> impleme
|
||||
|
||||
devices.forEach((device) -> {
|
||||
deviceMapper.updateById(device);
|
||||
device.setId( null);
|
||||
device.setId(null);
|
||||
device.setCurrentOwnerId(customerId);
|
||||
deviceMapper.insert( device);
|
||||
deviceMapper.insert(device);
|
||||
|
||||
DeviceType deviceType = deviceTypeMapper.selectById(device.getDeviceType());
|
||||
deviceType.setId( null);
|
||||
deviceType.setId(null);
|
||||
device.setCurrentOwnerId(customerId);
|
||||
deviceTypeMapper.insert( deviceType);
|
||||
deviceTypeMapper.insert(deviceType);
|
||||
|
||||
});
|
||||
}
|
||||
|
Reference in New Issue
Block a user