feat(device): 设备列表添加导出搜索结果功能

This commit is contained in:
曹鹏飞 2026-02-03 14:38:36 +08:00
parent 25b5f1b99b
commit 2e056a3324
2 changed files with 83 additions and 0 deletions

View File

@ -134,6 +134,47 @@ public class DeviceController extends ControllerBase {
return ApiResult.success(resultData, query, result.getTotal()); return ApiResult.success(resultData, query, result.getTotal());
} }
/**
* 导出设备列表
* @param query
*/
@PostMapping("exportSearch")
public void exportSearch(HttpServletResponse response,@RequestBody DeviceQuery query) throws IOException {
Page<Device> result = deviceService.getList(new Page<>(1, Integer.MAX_VALUE), query);
List<DictionaryItem> states = dictionaryItemService.getListByDictionaryCode(Constant.DICTIONARY_DEVICE_STATE);
List<DeviceExportDTO> datas = result.getRecords().stream().map(d -> {
DeviceExportDTO dto = new DeviceExportDTO();
dto.setId(String.valueOf(d.getId()));
dto.setDeviceNo(d.getDeviceNo());
dto.setDeviceName(d.getCustomerName()+d.getDeviceNo());
dto.setDeviceType(d.getDeviceType());
dto.setDeviceTypeSub(d.getDeviceTypeSub());
dto.setModelNo(d.getModelNo());
dto.setDeviceStateDesc(states.stream().filter(s -> Objects.equals(s.getId(), d.getDeviceState())).findFirst().get().getName());
dto.setCustomerName(d.getCustomerName());
dto.setAgentName(d.getAgentName());
dto.setAreaName(d.getAreaName());
dto.setAddress(d.getAddress());
dto.setShipmentDate(d.getShipmentDate());
dto.setStartWarrantyDate(d.getStartWarrantyDate());
if (Objects.nonNull(d.getWarrantyMonth())) {
dto.setWarrantyMonth(String.valueOf(d.getWarrantyMonth()));
}
dto.setServiceAgentName(d.getServiceAgentName());
return dto;
}).collect(Collectors.toList());
response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
response.setHeader(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=" + URLEncoder.encode("设备导出.xlsx", StandardCharsets.UTF_8));
// ClassPathResource resource = new ClassPathResource("templates/deviceForUpdate.xlsx");
// new Workbook()
// .addSheet(new TemplateSheet(resource.getInputStream())
// .setData(datas)
// .setData("@list:deviceStateDesc", dictionaryItemService.getListByDictionaryCode(Constant.DICTIONARY_DEVICE_STATE)
// .stream().map(DictionaryItem::getName).collect(Collectors.toList())))
// .writeTo(response.getOutputStream());
new Workbook().addSheet(new ListSheet<>(datas)).writeTo(response.getOutputStream());
}
/** /**
* 获取设备详情 * 获取设备详情
* @param id * @param id

View File

@ -137,6 +137,48 @@ public class DeviceController extends ControllerBase {
return ApiResult.success(resultData, query, result.getTotal()); return ApiResult.success(resultData, query, result.getTotal());
} }
/**
* 导出设备列表
* @param query
*/
@PostMapping("exportSearch")
public void exportSearch(HttpServletResponse response,@RequestBody DeviceQuery query) throws IOException {
Page<GongfuDevice> result = deviceService.getList(new Page<>(1, Integer.MAX_VALUE), query);
List<DictionaryItem> states = dictionaryItemService.getListByDictionaryCode(Constant.DICTIONARY_DEVICE_STATE);
List<DeviceExportDTO> datas = result.getRecords().stream().map(d -> {
DeviceExportDTO dto = new DeviceExportDTO();
dto.setId(String.valueOf(d.getId()));
dto.setDeviceNo(d.getDeviceNo());
dto.setDeviceName(d.getCustomerName()+d.getDeviceNo());
dto.setDeviceType(d.getDeviceType());
dto.setDeviceTypeSub(d.getDeviceTypeSub());
dto.setModelNo(d.getModelNo());
dto.setDeviceStateDesc(states.stream().filter(s -> Objects.equals(s.getId(), d.getDeviceState())).findFirst().get().getName());
dto.setCustomerName(d.getCustomerName());
dto.setAgentName(d.getAgentName());
dto.setAreaName(d.getAreaName());
dto.setAddress(d.getAddress());
dto.setShipmentDate(d.getShipmentDate());
dto.setStartWarrantyDate(d.getStartWarrantyDate());
if (Objects.nonNull(d.getWarrantyMonth())) {
dto.setWarrantyMonth(String.valueOf(d.getWarrantyMonth()));
}
dto.setProductLine(d.getProductLine());
dto.setServiceAgentName(d.getServiceAgentName());
return dto;
}).collect(Collectors.toList());
response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
response.setHeader(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=" + URLEncoder.encode("设备导出.xlsx", StandardCharsets.UTF_8));
// ClassPathResource resource = new ClassPathResource("templates/deviceForUpdate.xlsx");
// new Workbook()
// .addSheet(new TemplateSheet(resource.getInputStream())
// .setData(datas)
// .setData("@list:deviceStateDesc", dictionaryItemService.getListByDictionaryCode(Constant.DICTIONARY_DEVICE_STATE)
// .stream().map(DictionaryItem::getName).collect(Collectors.toList())))
// .writeTo(response.getOutputStream());
new Workbook().addSheet(new ListSheet<>(datas)).writeTo(response.getOutputStream());
}
/** /**
* 获取设备详情 * 获取设备详情
* @param id * @param id