Commit 1db09dc1 by zzrdark

1.完成批量导出

parent d9cd3586
...@@ -363,7 +363,7 @@ MODIFY COLUMN `ca_certificate_status` varchar(255) CHARACTER SET utf8 COLLATE ut ...@@ -363,7 +363,7 @@ MODIFY COLUMN `ca_certificate_status` varchar(255) CHARACTER SET utf8 COLLATE ut
-- 2020-07-27 -- 2020-07-27
CREATE TABLE `device_info_export` ( CREATE TABLE `device_info_export` (
`device_info_export_id` bigint(20) NOT NULL COMMENT '主键', `device_info_export_id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '主键',
`name` varchar(255) DEFAULT NULL COMMENT '名字', `name` varchar(255) DEFAULT NULL COMMENT '名字',
`filepath` varchar(255) DEFAULT NULL COMMENT '路径', `filepath` varchar(255) DEFAULT NULL COMMENT '路径',
`status` int(10) DEFAULT NULL COMMENT '状态:0未完成,1完成', `status` int(10) DEFAULT NULL COMMENT '状态:0未完成,1完成',
......
...@@ -20,9 +20,14 @@ public class DeviceInfoExportEntity { ...@@ -20,9 +20,14 @@ public class DeviceInfoExportEntity {
private Long deviceInfoExportId; private Long deviceInfoExportId;
private String name; private String name;
/**
* 文件路径
*/
private String filepath; private String filepath;
/**
* 0未完成,1完成 -1失败
*/
private Integer status; private Integer status;
private Date updateTime; private Date updateTime;
......
...@@ -33,7 +33,7 @@ public class UploadDeviceChannelExcelVo implements Serializable { ...@@ -33,7 +33,7 @@ public class UploadDeviceChannelExcelVo implements Serializable {
private List<Long> channelNums; private List<Long> channelNums;
@Data @Data
static class UploadDeviceChannelExcelInfoVo implements Serializable { public static class UploadDeviceChannelExcelInfoVo implements Serializable {
private String imei; private String imei;
private String iccid; private String iccid;
......
...@@ -84,6 +84,17 @@ ...@@ -84,6 +84,17 @@
<artifactId>fastjson</artifactId> <artifactId>fastjson</artifactId>
<version>1.2.71</version> <version>1.2.71</version>
</dependency> </dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>4.0.1</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.0.1</version>
</dependency>
</dependencies> </dependencies>
<build> <build>
......
...@@ -10,4 +10,5 @@ public class FilePath { ...@@ -10,4 +10,5 @@ public class FilePath {
public static String LogFilePath = "/cneedsFile/LogFilePath/"; public static String LogFilePath = "/cneedsFile/LogFilePath/";
public static String wechatFilePath = "/cneedsFile/wechatPath/"; public static String wechatFilePath = "/cneedsFile/wechatPath/";
public static String deviceInfoExportFilePath = "/cneedsFile/deviceInfoExportFilePath/";
} }
package com.mx.cneeds.common.utils;
import com.mx.cneeds.common.dto.DeviceInfoDto;
import com.mx.cneeds.common.vo.DeviceInfoVo;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class ExcelDeviceInfoWriter {
private static List<String> CELL_HEADS; //列头
static{
// 类装载时就载入指定好的列头信息,如有需要,可以考虑做成动态生成的列头
CELL_HEADS = new ArrayList<>();
CELL_HEADS.add("imei");
CELL_HEADS.add("iccid");
CELL_HEADS.add("sn");
CELL_HEADS.add("证书下载状态");
CELL_HEADS.add("版本信息");
CELL_HEADS.add("激活时间");
CELL_HEADS.add("激活状态");
}
/**
* 生成Excel并写入数据信息
* @param dataList 数据列表
* @return 写入数据后的工作簿对象
*/
public static Workbook exportData(List<DeviceInfoDto> dataList){
// 生成xlsx的Excel
Workbook workbook = new SXSSFWorkbook();
// 如需生成xls的Excel,请使用下面的工作簿对象,注意后续输出时文件后缀名也需更改为xls
//Workbook workbook = new HSSFWorkbook();
// 生成Sheet表,写入第一行的列头
Sheet sheet = buildDataSheet(workbook);
//构建每行的数据内容
int rowNum = 1;
for (Iterator<DeviceInfoDto> it = dataList.iterator(); it.hasNext(); ) {
DeviceInfoDto data = it.next();
if (data == null) {
continue;
}
//输出行数据
Row row = sheet.createRow(rowNum++);
convertDataToRow(data, row);
}
return workbook;
}
/**
* 生成sheet表,并写入第一行数据(列头)
* @param workbook 工作簿对象
* @return 已经写入列头的Sheet
*/
private static Sheet buildDataSheet(Workbook workbook) {
Sheet sheet = workbook.createSheet();
// 设置列头宽度
for (int i=0; i<CELL_HEADS.size(); i++) {
sheet.setColumnWidth(i, 4000);
}
// 设置默认行高
sheet.setDefaultRowHeight((short) 400);
// 构建头单元格样式
CellStyle cellStyle = buildHeadCellStyle(sheet.getWorkbook());
// 写入第一行各列的数据
Row head = sheet.createRow(0);
for (int i = 0; i < CELL_HEADS.size(); i++) {
Cell cell = head.createCell(i);
cell.setCellValue(CELL_HEADS.get(i));
cell.setCellStyle(cellStyle);
}
return sheet;
}
/**
* 设置第一行列头的样式
* @param workbook 工作簿对象
* @return 单元格样式对象
*/
private static CellStyle buildHeadCellStyle(Workbook workbook) {
CellStyle style = workbook.createCellStyle();
//对齐方式设置
style.setAlignment(HorizontalAlignment.CENTER);
//边框颜色和宽度设置
style.setBorderBottom(BorderStyle.THIN);
style.setBottomBorderColor(IndexedColors.BLACK.getIndex()); // 下边框
style.setBorderLeft(BorderStyle.THIN);
style.setLeftBorderColor(IndexedColors.BLACK.getIndex()); // 左边框
style.setBorderRight(BorderStyle.THIN);
style.setRightBorderColor(IndexedColors.BLACK.getIndex()); // 右边框
style.setBorderTop(BorderStyle.THIN);
style.setTopBorderColor(IndexedColors.BLACK.getIndex()); // 上边框
//设置背景颜色
style.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
//粗体字设置
Font font = workbook.createFont();
font.setBold(true);
style.setFont(font);
return style;
}
/**
* 将数据转换成行
* @param data 源数据
* @param row 行对象
* @return
*/
private static void convertDataToRow(DeviceInfoDto data, Row row){
int cellNum = 0;
Cell cell;
cell = row.createCell(cellNum++);
cell.setCellValue(null == data.getImei() ? "" : data.getImei());
cell = row.createCell(cellNum++);
if (null != data.getIccid()) {
cell.setCellValue(data.getIccid());
} else {
cell.setCellValue("");
}
cell = row.createCell(cellNum++);
cell.setCellValue(null == data.getSn() ? "" : data.getSn());
cell = row.createCell(cellNum++);
cell.setCellValue(null == data.getCaCertificateStatus() ? "" : data.getCaCertificateStatus());
cell = row.createCell(cellNum++);
cell.setCellValue(null == data.getDeviceVersion() ? "" : data.getDeviceVersion());
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
cell = row.createCell(cellNum++);
cell.setCellValue(null == data.getActiveTime() ? "" : simpleDateFormat.format(data.getActiveTime()));
cell = row.createCell(cellNum++);
cell.setCellValue(null == data.getActiveStatus() ? "" : data.getActiveStatus());
}
}
\ No newline at end of file
...@@ -53,6 +53,9 @@ public interface DeviceClient { ...@@ -53,6 +53,9 @@ public interface DeviceClient {
@PostMapping("/device/info/infoByImei") @PostMapping("/device/info/infoByImei")
DeviceInfoDto queryDeviceByImei(@RequestBody String imei); DeviceInfoDto queryDeviceByImei(@RequestBody String imei);
@PostMapping("/device/info/infoByImeis")
List<DeviceInfoDto> queryDeviceByImeis(@RequestBody List<String> imeis);
@PostMapping("/device/info/infoByIds") @PostMapping("/device/info/infoByIds")
List<DeviceInfoDto> queryDeviceByIds(@RequestBody Set<Long> ids); List<DeviceInfoDto> queryDeviceByIds(@RequestBody Set<Long> ids);
...@@ -119,4 +122,17 @@ public interface DeviceClient { ...@@ -119,4 +122,17 @@ public interface DeviceClient {
@PostMapping("/device/info/queryDeviceChannelSetting") @PostMapping("/device/info/queryDeviceChannelSetting")
List<String> queryDeviceChannelSetting(List<String> imeis); List<String> queryDeviceChannelSetting(List<String> imeis);
@PostMapping("device/info/deviceInfoExportList")
PageDto deviceInfoExportList(@RequestParam Integer page,
@RequestParam("limit") Integer pageSize,
@RequestParam("sidx") String orderField,
@RequestParam("order") String order,
@RequestParam("seriesId") Long seriesId);
@PostMapping("device/info/addDeviceInfoExport")
void addDeviceInfoExport(@RequestBody DeviceInfoExportDto deviceInfoExportDto);
} }
package com.mx.cneeds.server.datashow.web.device; package com.mx.cneeds.server.datashow.web.device;
import com.mx.cneeds.common.constant.FilePath;
import com.mx.cneeds.common.constant.ResultCode; import com.mx.cneeds.common.constant.ResultCode;
import com.mx.cneeds.common.converter.RequestParamterConverter; import com.mx.cneeds.common.converter.RequestParamterConverter;
import com.mx.cneeds.common.dto.*; import com.mx.cneeds.common.dto.*;
import com.mx.cneeds.common.result.R; import com.mx.cneeds.common.result.R;
import com.mx.cneeds.common.utils.ExcelDeviceInfoWriter;
import com.mx.cneeds.common.vo.*; import com.mx.cneeds.common.vo.*;
import com.mx.cneeds.server.datashow.client.DeviceClient; import com.mx.cneeds.server.datashow.client.DeviceClient;
import com.mx.cneeds.server.datashow.client.UserClient; import com.mx.cneeds.server.datashow.client.UserClient;
import com.mx.hbasefile.hadoop.hdfs.api.HdfsTemplate;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.BeanUtils; import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.ClassPathResource;
...@@ -20,13 +25,10 @@ import org.springframework.http.ResponseEntity; ...@@ -20,13 +25,10 @@ import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import java.io.FileNotFoundException; import java.io.*;
import java.io.IOException;
import java.io.InputStream;
import java.text.ParseException; import java.text.ParseException;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.LinkedList; import java.util.*;
import java.util.List;
/** /**
* @ClassName DeviceController * @ClassName DeviceController
...@@ -41,6 +43,9 @@ import java.util.List; ...@@ -41,6 +43,9 @@ import java.util.List;
public class DeviceController { public class DeviceController {
@Autowired @Autowired
private HdfsTemplate hdfsTemplate;
@Autowired
private DeviceClient deviceClient; private DeviceClient deviceClient;
@Autowired @Autowired
...@@ -451,6 +456,107 @@ public class DeviceController { ...@@ -451,6 +456,107 @@ public class DeviceController {
} }
/**
* 查询 设备信息导出表
* @param page
* @param pageSize
* @param sort
* @param seriesId
* @return
*/
// @PreAuthorize("hasRole('deviceManagement:device:list') or hasRole('admin')")
@PostMapping("/deviceInfoExport/list")
public R deviceInfoExportList(@RequestParam(required = false) Integer page,
@RequestParam(required = false) Integer pageSize,
@RequestParam(required = false) String sort,
@RequestParam(required = false) Long seriesId){
String orderField = null;
String order = null;
if (page==null || page==0){
page = new Integer(1);
}
if (pageSize==null || pageSize==0){
pageSize = new Integer(10);
}
if (StringUtils.isNotEmpty(sort) && StringUtils.isNotBlank(sort.trim())){
if (sort.contains("+")){
order = new String("asc");
}
if (sort.contains("-")){
order = new String("desc");
}
orderField = sort.substring(1);
orderField = RequestParamterConverter.toLine(orderField);
}
PageDto pageDto = deviceClient.deviceInfoExportList(page,pageSize,orderField,order,seriesId);
return new R().put("data",pageDto);
}
@PostMapping("/deviceInfoExport/uploadDeviceInfoExport")
public R uploadDeviceInfoExport(@RequestBody UploadDeviceChannelExcelVo vo) throws IOException {
List<String> list = new ArrayList<>();
// 检测参数
if (vo.getInfos() == null || vo.getInfos().size() == 0 ){
return R.error();
}
Iterator<UploadDeviceChannelExcelVo.UploadDeviceChannelExcelInfoVo> iterator = vo.getInfos().iterator();
while (iterator.hasNext()) {
UploadDeviceChannelExcelVo.UploadDeviceChannelExcelInfoVo next = iterator.next();
if (next.getImei() == null || next.getImei().trim().isEmpty()){
return R.error();
}
list.add(next.getImei());
}
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
try {
UploadDeviceChannelExcelDto dto = new UploadDeviceChannelExcelDto();
BeanUtils.copyProperties(vo, dto);
// 查询数据 生成excel
List<DeviceInfoDto> deviceInfoDtos = deviceClient.queryDeviceByImeis(list);
Workbook workbook = ExcelDeviceInfoWriter.exportData(deviceInfoDtos);
// 存到文件系统
ByteArrayOutputStream baos = new ByteArrayOutputStream();
String fileName = simpleDateFormat.format(new Date()) + ".xlsx";
workbook.write(baos);
ByteArrayInputStream swapStream = new ByteArrayInputStream(baos.toByteArray());
StringBuffer dir = new StringBuffer(FilePath.deviceInfoExportFilePath);
hdfsTemplate.saveFile(dir.toString(),fileName,swapStream,baos.toByteArray().length, (short) 2);
// 增加一条设备导出表的数据状态
DeviceInfoExportDto deviceInfoExportDto = new DeviceInfoExportDto();
deviceInfoExportDto.setName(fileName);
deviceInfoExportDto.setUpdateTime(new Date());
deviceInfoExportDto.setStatus(1);
deviceInfoExportDto.setFilepath(FilePath.deviceInfoExportFilePath + baos.toByteArray().length + "/" + fileName);
deviceClient.addDeviceInfoExport(deviceInfoExportDto);
return R.ok();
} catch (Exception e) {
String fileName = simpleDateFormat.format(new Date()) + ".xlsx";
DeviceInfoExportDto deviceInfoExportDto = new DeviceInfoExportDto();
deviceInfoExportDto.setName(fileName);
deviceInfoExportDto.setUpdateTime(new Date());
deviceInfoExportDto.setStatus(-1);
deviceClient.addDeviceInfoExport(deviceInfoExportDto);
log.error("Exception Message: "+ e.getMessage(),e);
return R.error();
}
}
} }
package com.mx.cneeds.server.datashow.web.device;
import com.mx.cneeds.common.constant.FilePath;
import com.mx.hbasefile.hadoop.hdfs.api.HdfsTemplate;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
/**
* @ClassName DeviceFileController
* @Author zzrdark
* @Date 2020-07-28 19:27
* @Description TODO
**/
@Controller
@Slf4j
@RequestMapping("/deviceFile/")
public class DeviceFileController {
@Autowired
private HdfsTemplate hdfsTemplate;
@RequestMapping("/deviceFileDownload/{fileSize}/{fileName}")
public void download(@PathVariable("fileName") String fileName, @PathVariable("fileSize") String fileSize,
HttpServletResponse response) throws IOException {
StringBuffer dirPath = new StringBuffer(FilePath.deviceInfoExportFilePath);
InputStream inputStream = hdfsTemplate.openFile(dirPath.toString(), fileName);
ServletOutputStream outputStream = response.getOutputStream();
String mediaType = fileName.substring(fileName.indexOf("."));
response.setContentType(mediaType);
response.addHeader("Content-Disposition", "attachment; filename" + "testfilename");
byte[] bytes = new byte[1024];
long total = Long.valueOf(fileSize);
while (total>0) {
inputStream.read(bytes);
outputStream.write(bytes,0, total>1024?1024: (int) total);
outputStream.flush();
total-=1024;
}
/*int i = 0 ;
while ((i = inputStream.read(bytes)) != -1){
outputStream.write(bytes,0, i);
outputStream.flush();
}*/
outputStream.close();
}
}
...@@ -97,6 +97,7 @@ public class DeviceInfoController { ...@@ -97,6 +97,7 @@ public class DeviceInfoController {
/** /**
* 信息 * 信息
* 会查出设备信息渠道号信息
*/ */
@RequestMapping("/infoByImei") @RequestMapping("/infoByImei")
public DeviceInfoDto infoByimei(@RequestBody String imei){ public DeviceInfoDto infoByimei(@RequestBody String imei){
...@@ -135,6 +136,25 @@ public class DeviceInfoController { ...@@ -135,6 +136,25 @@ public class DeviceInfoController {
} }
} }
/**
* 批量查询设备信息,
* 但是不查出设备渠道号信息
* @param imeis
* @return
*/
@RequestMapping("/infoByImeis")
public List<DeviceInfoDto> infoByimeis(@RequestBody List<String> imeis){
List<DeviceInfoEntity> deviceInfoEntities = deviceInfoService.queryDeviceByImeis(imeis);
List<DeviceInfoDto> deviceInfoDtos = new ArrayList<>();
for (DeviceInfoEntity deviceInfoEntity : deviceInfoEntities) {
DeviceInfoDto dto = new DeviceInfoDto();
BeanUtils.copyProperties(deviceInfoEntity,dto);
deviceInfoDtos.add(dto);
}
return deviceInfoDtos;
}
@RequestMapping("/infoByIds") @RequestMapping("/infoByIds")
public List<DeviceInfoDto> infoByIds(@RequestBody Set<Long> ids){ public List<DeviceInfoDto> infoByIds(@RequestBody Set<Long> ids){
List<DeviceInfoEntity> deviceInfoEntitys = deviceInfoService.queryDeviceByIds(ids); List<DeviceInfoEntity> deviceInfoEntitys = deviceInfoService.queryDeviceByIds(ids);
...@@ -419,4 +439,26 @@ public class DeviceInfoController { ...@@ -419,4 +439,26 @@ public class DeviceInfoController {
page.setList(list); page.setList(list);
return page; return page;
} }
@RequestMapping("/addDeviceInfoExport")
public void addDeviceInfoExport(@RequestBody DeviceInfoExportDto deviceInfoExportDto){
DeviceInfoExportEntity entity = new DeviceInfoExportEntity();
BeanUtils.copyProperties(deviceInfoExportDto,entity);
deviceInfoExportService.save(entity);
}
@RequestMapping("/deleteDeviceInfoExport")
public void deleteDeviceInfoExport(@RequestBody Long deviceInfoExportId){
deviceInfoExportService.removeById(deviceInfoExportId);
}
@RequestMapping("updateDeviceInfoExport")
public void updateDeviceInfoExport(@RequestBody DeviceInfoExportDto deviceInfoExportDto){
DeviceInfoExportEntity entity = new DeviceInfoExportEntity();
BeanUtils.copyProperties(deviceInfoExportDto,entity);
deviceInfoExportService.saveOrUpdate(entity);
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment