Accomplishing the download of Terminal Two Dimension Code And Its Data By pileId or stationId.

This commit is contained in:
little-cat-sweet 2021-10-11 11:28:39 +08:00
parent 994db7ff8f
commit 3be03ebded
7 changed files with 585 additions and 0 deletions

View File

@ -128,6 +128,13 @@
<version>3.10.2</version>
<scope>compile</scope>
</dependency>
<!-- Operating the excel file.-->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.1.2</version>
</dependency>
</dependencies>
<build>

View File

@ -0,0 +1,49 @@
package com.xhpc.charging.station.controller;
import com.xhpc.charging.station.service.IXhpcDownCodeService;
import com.xhpc.common.core.web.controller.BaseController;
import com.xhpc.common.core.web.page.TableDataInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
/**
* program: ruoyi
* User: HongYun
* Date:2021-09-30 15
*/
@RestController
@RequestMapping(value = "/code")
public class XhpcDownCodeController extends BaseController {
@Autowired
private IXhpcDownCodeService iXhpcDownCodeService;
@GetMapping(value = "/listStations")
public TableDataInfo listStations(String stationName){
startPage();
return getDataTable(iXhpcDownCodeService.listStations(stationName));
}
@GetMapping(value = "/listPiles")
public TableDataInfo listPiles(@RequestParam Long stationId){
startPage();
return getDataTable(iXhpcDownCodeService.listPiles(stationId));
}
@GetMapping(value = "/downMaterialByPileId")
public void downMaterialBy(@RequestParam Long pileId) throws Exception {
iXhpcDownCodeService.downMaterialBy(pileId);
}
@GetMapping(value = "/downMaterialByStationId")
public void downMaterialByStationId(@RequestParam Long stationId) throws Exception{
iXhpcDownCodeService.downMaterialByStationId(stationId);
}
}

View File

@ -0,0 +1,26 @@
package com.xhpc.charging.station.mapper;
import org.apache.ibatis.annotations.Param;
import java.util.List;
import java.util.Map;
/**
* program: ruoyi
* User: HongYun
* Date:2021-09-30 15
*/
public interface XhpcDownCodeMapper {
List<Map<String, Object>> selectStationsBy(@Param(value = "stationName") String stationName);
List<Map<String, Object>> selectPilesBy(@Param(value = "stationId") Long stationId);
List<Map<String,Object>> selectExcelDataByPileId(@Param("pileId") Long pileId );
List<String> selectUrlsByPile(@Param(value = "pileId") Long pileId);
List<String> selectUrlsByStation(@Param(value = "stationId") Long stationId);
List<Map<String, Object>> selectExcelDataByStationId(@Param(value = "stationId") Long stationId);
}

View File

@ -0,0 +1,20 @@
package com.xhpc.charging.station.service;
import java.util.List;
import java.util.Map;
/**
* program: ruoyi
* User: HongYun
* Date:2021-09-30 15
*/
public interface IXhpcDownCodeService {
List<Map<String, Object>> listStations(String stationName);
List<Map<String, Object>> listPiles(Long stationId);
void downMaterialBy(Long pileId) throws Exception;
void downMaterialByStationId(Long stationId) throws Exception;
}

View File

@ -0,0 +1,188 @@
package com.xhpc.charging.station.service;
import com.xhpc.charging.station.mapper.XhpcDownCodeMapper;
import com.xhpc.charging.station.utils.img.DownMaterialUtil;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.List;
import java.util.Map;
/**
* program: ruoyi
* User: HongYun
* Date:2021-09-30 15
*/
@Service
public class XhpcDownCodeServiceImpl implements IXhpcDownCodeService {
@Autowired
private XhpcDownCodeMapper xhpcDownCodeMapper;
@Override
public List<Map<String, Object>> listStations(String stationName) {
return xhpcDownCodeMapper.selectStationsBy(stationName);
}
@Override
public List<Map<String, Object>> listPiles(Long stationId) {
return xhpcDownCodeMapper.selectPilesBy(stationId);
}
@Override
public void downMaterialBy(Long pileId) throws Exception {
List<Map<String,Object>> list = xhpcDownCodeMapper.selectExcelDataByPileId(pileId);
System.out.println(list);
File file1 = new File("Material");
File file2 = new File(file1, "pictures");
if(file1.exists()){
deleteFiles(file1);
}
boolean flag = file1.mkdir();
if(! flag) {
System.out.println(file1.getName()+"文件夹创建失败!");
return;
}
boolean flag1 = file2.mkdir();
if(! flag1){
System.out.println(file2.getName() + "文件夹创建失败");
}
//1创建excel文档对象
HSSFWorkbook workbook = new HSSFWorkbook();
//2创建sheet页
HSSFSheet sheet = workbook.createSheet("第一页");
//3创建头部第一行
HSSFRow headRow = sheet.createRow(0);
HSSFCell cell;
String[] header = new String[] {"终端名称","归属电站","终端编码","电桩编码","终端类型(1直流 2交流快)","电桩功率(KM)"};
for (int i = 0; i < header.length; i++) {
cell = headRow.createCell(i);
cell.setCellValue(header[i]);
}
//4填写数据
HSSFRow dataRow;
HSSFCell dataCell;
String[] values = {"tName", "sName", "tSN", "psn", "type", "power"};
int valuesLength = values.length;
for(int i = 1; i <= list.size(); i ++){
Map<String,Object> map = list.get(i - 1);
if(null != map) {
dataRow = sheet.createRow(i);
for(int j = 0; j < valuesLength; j ++){
if(null != map.get(values[j])){
dataCell = dataRow.createCell(j);
String s = String.valueOf(map.get(values[j]));
dataCell.setCellValue(s);
}
}
}
}
System.out.println(list);
//5写入
OutputStream fos = new FileOutputStream(file1.getAbsoluteFile() + "\\data.xls");
workbook.write(fos);
workbook.close();
fos.close();
String left = "https://xhpc-bucket1.oss-cn-hangzhou.aliyuncs.com/";
List<String> imgUrls = xhpcDownCodeMapper.selectUrlsByPile(pileId);
System.out.println(imgUrls);
List<String> list1 = DownMaterialUtil.getDownLoadUrls(left,imgUrls);
DownMaterialUtil.saveImageToDisk(list1,file2,imgUrls);
File file = new File(file1.getAbsolutePath());
DownMaterialUtil.fileToZip(file);
System.out.println("success");
}
@Override
public void downMaterialByStationId(Long stationId) throws Exception {
List<Map<String, Object>> list = xhpcDownCodeMapper.selectExcelDataByStationId(stationId);
System.out.println(list);
File file1 = new File("Material");
File file2 = new File(file1, "pictures");
if (file1.exists()) {
deleteFiles(file1);
}
boolean flag = file1.mkdir();
if (!flag) {
System.out.println(file1.getName() + "文件夹创建失败!");
return;
}
boolean flag1 = file2.mkdir();
if (!flag1) {
System.out.println(file2.getName() + "文件夹创建失败");
}
//1创建excel文档对象
HSSFWorkbook workbook = new HSSFWorkbook();
//2创建sheet页
HSSFSheet sheet = workbook.createSheet("第一页");
//3创建头部第一行
HSSFRow headRow = sheet.createRow(0);
HSSFCell cell;
String[] header = new String[]{"终端名称", "归属电站", "终端编码", "电桩编码", "终端类型(1直流 2交流快)", "电桩功率(KM)"};
for (int i = 0; i < header.length; i++) {
cell = headRow.createCell(i);
cell.setCellValue(header[i]);
}
//4填写数据
HSSFRow dataRow;
HSSFCell dataCell;
String[] values = {"tName", "sName", "tSN", "psn", "type", "power"};
int valuesLength = values.length;
for (int i = 1; i <= list.size(); i++) {
Map<String, Object> map = list.get(i - 1);
if (null != map) {
dataRow = sheet.createRow(i);
for (int j = 0; j < valuesLength; j++) {
if (null != map.get(values[j])) {
dataCell = dataRow.createCell(j);
String s = String.valueOf(map.get(values[j]));
dataCell.setCellValue(s);
}
}
}
}
System.out.println(list);
//5写入
OutputStream fos = new FileOutputStream(file1.getAbsoluteFile() + "\\data.xls");
workbook.write(fos);
workbook.close();
fos.close();
String left = "https://xhpc-bucket1.oss-cn-hangzhou.aliyuncs.com/";
List<String> imgUrls = xhpcDownCodeMapper.selectUrlsByStation(stationId);
System.out.println(imgUrls);
List<String> list1 = DownMaterialUtil.getDownLoadUrls(left, imgUrls);
DownMaterialUtil.saveImageToDisk(list1, file2, imgUrls);
File file = new File(file1.getAbsolutePath());
DownMaterialUtil.fileToZip(file);
System.out.println("success");
}
public void deleteFiles(File file){
if (file.isDirectory()) {
File[] files=file.listFiles();
if (null != files) {
for (File value : files) {
if (value.isDirectory()) {
deleteFiles(value);
} else {
System.out.println("里层文件:" + value.getName() + "--------" + value.delete());
}
}
}
}
System.out.println("外层文件:"+file.getName()+"--------"+file.delete());
}
}

View File

@ -0,0 +1,218 @@
package com.xhpc.charging.station.utils.img;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
/**
* program: ruoyi
* User: HongYun
* Date:2021-10-09 10
*/
public class DownMaterialUtil {
public static InputStream getInputStream(String urlImg) {
InputStream inputStream = null;
HttpURLConnection httpURLConnection = null;
try {
URL url = new URL(urlImg);
httpURLConnection = (HttpURLConnection) url.openConnection();
// 设置网络连接超时时间
httpURLConnection.setConnectTimeout(3000);
// 设置应用程序要从网络连接读取数据
httpURLConnection.setDoInput(true);
httpURLConnection.setRequestMethod("GET");
int responseCode = httpURLConnection.getResponseCode();
if (responseCode == 200) {
// 从服务器返回一个输入流
inputStream = httpURLConnection.getInputStream();
}
} catch (IOException e) {
e.printStackTrace();
}
return inputStream;
}
public static void saveImageToDisk(List<String> urls, File file, List<String> imgNames) {
for(int i = 0; i < urls.size(); i ++) {
InputStream inputStream = getInputStream(urls.get(i));
byte[] data = new byte[1024];
int len = 0;
FileOutputStream fileOutputStream = null;
try {
String str = imgNames.get(i);
int poke = 0;
for(int j = str.length() - 1; j >= 0; j --){
if(str.charAt(j) == 47){
poke = j;
break;
}
}
String real = str.substring(poke + 1);
System.out.println(real);
System.out.println(file.getName() + "\\" + real);
fileOutputStream = new FileOutputStream(file.getAbsoluteFile() + "\\" + real);
while ((len = inputStream.read(data)) != -1) {
fileOutputStream.write(data, 0, len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fileOutputStream != null) {
try {
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
System.out.println("success");
}
}
}
public static void fileToZip(File sourceFile) throws Exception {
if (!sourceFile.exists()) {
throw new RuntimeException("不存在");
}
if (!sourceFile.isDirectory()) {
throw new RuntimeException("不是文件夹");
}
//zip文件生成位置
File zipFile = new File(sourceFile.getAbsolutePath() + ".zip");
FileOutputStream fos = new FileOutputStream(zipFile);
ZipOutputStream zos = new ZipOutputStream(new BufferedOutputStream(fos));
fileToZip(zos, sourceFile, "");
zos.close();
fos.close();
}
private static void fileToZip(ZipOutputStream zos, File sourceFile, String path) throws Exception {
//如果是文件夹只创建zip实体即可如果是文件创建zip实体后还要读取文件内容并写入
if (sourceFile.isDirectory()) {
path = path + sourceFile.getName() + "/";
ZipEntry zipEntry = new ZipEntry(path);
zos.putNextEntry(zipEntry);
for (File file : Objects.requireNonNull(sourceFile.listFiles())) {
fileToZip(zos, file, path);
}
} else {
//创建ZIP实体并添加进压缩包
ZipEntry zipEntry = new ZipEntry(path + sourceFile.getName());
zos.putNextEntry(zipEntry);
byte[] bufs = new byte[1024 * 10];
//读取待压缩的文件并写进压缩包里
FileInputStream fis = new FileInputStream(sourceFile);
BufferedInputStream bis = new BufferedInputStream(fis, 1024 * 10);
int read = 0;
while ((read = bis.read(bufs, 0, 1024 * 10)) != -1) {
zos.write(bufs, 0, read);
}
bis.close();
fis.close();
}
}
public static void saveToFile(String targetUrl,String destUrl) {
FileOutputStream fos = null;
BufferedInputStream bis = null;
HttpURLConnection httpUrl = null;
URL url = null;
int BUFFER_SIZE = 1024;
byte[] buf = new byte[BUFFER_SIZE];
int size = 0;
try {
url = new URL(targetUrl);
httpUrl = (HttpURLConnection) url.openConnection();
httpUrl.connect();
bis = new BufferedInputStream(httpUrl.getInputStream());
fos = new FileOutputStream(destUrl);
while ((size = bis.read(buf)) != -1) {
fos.write(buf, 0, size);
}
fos.flush();
} catch (IOException | ClassCastException ignored) {
} finally {
try {
if(null != fos) {
fos.close();
}
if(null != bis) {
bis.close();
}
if(null != httpUrl) {
httpUrl.disconnect();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static List<String> getDownLoadUrls(String left, List<String> imgUrl){
List<String> res = new ArrayList<>();
for (String s : imgUrl) {
res.add(left + s);
}
return res;
}
/**
* 以流的形式下载文件
*
* @param file
* @param response
* @return
*/
public static HttpServletResponse downloadZip(File file, HttpServletResponse response) {
try {
// 以流的形式下载文件
InputStream fis = new BufferedInputStream(new FileInputStream(file.getPath()));
byte[] buffer = new byte[fis.available()];
fis.read(buffer);
fis.close();
// 清空response
response.reset();
OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
response.setContentType("application/octet-stream");
//如果输出的是中文名的文件在此处就要用URLEncoder.encode方法进行处理
response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(file.getName(), "UTF-8"));
toClient.write(buffer);
toClient.flush();
toClient.close();
} catch (IOException ex) {
ex.printStackTrace();
} finally {
try {
File f = new File(file.getPath());
// f.delete();
} catch (Exception e) {
e.printStackTrace();
}
}
return response;
}
}

View File

@ -0,0 +1,77 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.xhpc.charging.station.mapper.XhpcDownCodeMapper">
<select id="selectStationsBy" resultType="map">
select xcs.charging_station_id as chargingStationId, xcs.name as chargingStationName,
xo.name as operatorName, count(xt.terminal_id) as number
from xhpc_charging_station as xcs left join xhpc_operator as xo
on xcs.operator_id = xo.operator_id
left join xhpc_terminal as xt
on xcs.charging_station_id = xt.charging_station_id
where xo.status = 0 and xo.del_flag = 0
<if test="null != stationName and '' != stationName">
and xcs.name like concat('%',#{stationName},'%')
</if>
group by xcs.charging_station_id, xcs.name, xo.name
</select>
<select id="selectPilesBy" resultType="map">
select xcp.charging_pile_id as pileId, xcp.name as pileName, count(xt.terminal_id) as number
from xhpc_charging_pile as xcp left join xhpc_terminal as xt
on xcp.charging_pile_id = xt.charging_pile_id
where xcp.charging_station_id = #{stationId} and xt.del_flag = 0
group by pileId, pileName
</select>
<select id="selectExcelDataByPileId" resultType="map">
select t.name as tName, s.name as sName, t.serial_number as tSN, p.serial_number as psn,
p.type, p.power
from xhpc_terminal as t left join xhpc_charging_station as s
on t.charging_station_id = s.charging_station_id
left join xhpc_charging_pile as p
on p.charging_pile_id = t.charging_pile_id
where p.charging_pile_id = #{pileId}
</select>
<select id="selectExcelDataByStationId" resultType="map">
select t.name as tName, s.name as sName, t.serial_number as tSN, p.serial_number as psn,
p.type, p.power
from xhpc_terminal as t left join xhpc_charging_station as s
on t.charging_station_id = s.charging_station_id
left join xhpc_charging_pile as p
on p.charging_pile_id = t.charging_pile_id
where s.charging_station_id = #{stationId}
</select>
<select id="selectUrlsByPile" resultType="java.lang.String">
select i.url
from xhpc_img as i left join xhpc_terminal as t
on i.terminal_id = t.terminal_id
where t.terminal_id in (
select terminal_id
from xhpc_terminal
where charging_pile_id = #{pileId}
)
</select>
<select id="selectUrlsByStation" resultType="java.lang.String">
select i.url
from xhpc_img as i left join xhpc_terminal as t
on i.terminal_id = t.terminal_id
where t.terminal_id in (
select terminal_id
from xhpc_terminal
where charging_station_id = #{stationId}
)
</select>
</mapper>