From aff6f184e7a38a1674f3067e3f6c3acc92a4fed4 Mon Sep 17 00:00:00 2001
From: zhanzhiqin <895896009@qq.com>
Date: 星期二, 17 五月 2022 11:44:52 +0800
Subject: [PATCH] 日志
---
ximon-admin/src/main/java/com/sandu/ximon/admin/service/LightReportDataService.java | 166 ++++++++++++++++++++++++++++++++++++++++++++++---------
1 files changed, 138 insertions(+), 28 deletions(-)
diff --git a/ximon-admin/src/main/java/com/sandu/ximon/admin/service/LightReportDataService.java b/ximon-admin/src/main/java/com/sandu/ximon/admin/service/LightReportDataService.java
index 54b4d4e..e8aaca2 100644
--- a/ximon-admin/src/main/java/com/sandu/ximon/admin/service/LightReportDataService.java
+++ b/ximon-admin/src/main/java/com/sandu/ximon/admin/service/LightReportDataService.java
@@ -1,28 +1,39 @@
package com.sandu.ximon.admin.service;
-import cn.hutool.core.io.FileUtil;
+import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.RandomUtil;
import com.alibaba.excel.EasyExcel;
-import com.alibaba.excel.ExcelWriter;
-import com.alibaba.excel.write.metadata.WriteSheet;
-import com.github.pagehelper.PageHelper;
-import com.sandu.common.file.FileUploadDto;
+import com.alibaba.fastjson.JSON;
+import com.baomidou.mybatisplus.core.toolkit.Wrappers;
+import com.sandu.common.domain.CommonPage;
+import com.sandu.common.execption.BusinessException;
import com.sandu.common.file.config.FileProperties;
import com.sandu.common.file.impl.AliOssFileServiceImpl;
import com.sandu.common.service.impl.BaseServiceImpl;
-import com.sandu.common.util.ResUtils;
+import com.sandu.common.util.SpringContextHolder;
import com.sandu.ximon.admin.manager.iot.frame.inner.report.A5LightHeartbeatReportInnerFrame;
+import com.sandu.ximon.admin.manager.iot.rrpc.enums.A5LightDataEnum;
+import com.sandu.ximon.admin.security.SecurityUtils;
+import com.sandu.ximon.admin.utils.RedisUtils;
import com.sandu.ximon.dao.bo.LightReportDataBo;
import com.sandu.ximon.dao.domain.LightReportData;
+import com.sandu.ximon.dao.domain.Pole;
+import com.sandu.ximon.dao.domain.PoleBinding;
import com.sandu.ximon.dao.mapper.LightReportDataMapper;
import lombok.AllArgsConstructor;
import lombok.SneakyThrows;
import org.springframework.beans.BeanUtils;
-import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Service;
-import org.springframework.web.multipart.MultipartFile;
+import javax.servlet.ServletOutputStream;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.time.LocalDateTime;
+import java.time.format.DateTimeFormatter;
+import java.util.ArrayList;
import java.util.List;
/**
@@ -36,6 +47,7 @@
private final FileProperties properties;
private final AliOssFileServiceImpl fileService;
+ private final LightReportDataMapper lightReportDataMapper;
/**
* 淇濆瓨涓婃姤鐨勭伅蹇冭烦鏁版嵁
@@ -43,9 +55,24 @@
* @return 鏄惁鎴愬姛
*/
public boolean saveReportData(String deviceName, A5LightHeartbeatReportInnerFrame.HeartBeatDataPackage heartBeatDataPackage) {
+// LightReportData lightReportData = RedisUtils.getBean().get(A5LightDataEnum.LIGHT_HEART_BEAT.getCode() + deviceName, LightReportData.class);
+
+
+ String format = LocalDateTime.now().format(DateTimeFormatter.ofPattern("YYYY-MM-dd HH:mm:ss"));
LightReportData lightReportData = new LightReportData();
+
+ if (lightReportData == null) {
+ lightReportData = new LightReportData();
+ lightReportData.setCreateTime(format);
+ }
+ //todo
+ lightReportData.setCreateTime(format);
BeanUtils.copyProperties(heartBeatDataPackage, lightReportData);
lightReportData.setDeviceCode(deviceName);
+
+ lightReportData.setUpdateTime(format);
+ //鍗曠伅鏁版嵁淇濆瓨鍒扮紦瀛橀噷
+ RedisUtils.getBean().set(A5LightDataEnum.LIGHT_HEART_BEAT.getCode() + deviceName, lightReportData);
return save(lightReportData);
}
@@ -66,34 +93,117 @@
* @param keyword 鍏抽敭璇�
* @param deviceCode 璁惧鐮�
*/
- public List<LightReportDataBo> listReportData(int pageNo, int pageSize, String keyword, String deviceCode) {
- PageHelper.startPage(pageNo, pageSize);
- return baseMapper.listReportData(keyword, deviceCode);
+ public CommonPage listReportData(int pageNo, int pageSize, String keyword, String deviceCode) {
+
+ List<LightReportDataBo> lightReportDataBos = new ArrayList<>(pageSize);
+
+ CommonPage<String> stringCommonPage = SpringContextHolder.getBean(LightService.class).listDeviceCode(pageNo, pageSize, keyword, deviceCode);
+ List<String> macList = stringCommonPage.getList();
+ if (CollUtil.isEmpty(macList)) {
+ return new CommonPage();
+ }
+
+ for (String macCode : macList) {
+ try {
+ LightReportData lightReportData
+ = JSON.parseObject(RedisUtils.getBean().get(A5LightDataEnum.LIGHT_HEART_BEAT.getCode() + macCode), LightReportData.class);
+
+ LightReportDataBo lightReportDataBo = new LightReportDataBo();
+ if (lightReportData != null) {
+ BeanUtils.copyProperties(lightReportData, lightReportDataBo);
+ }
+ lightReportDataBo.setDeviceCode(macCode);
+ Pole pole = SpringContextHolder.getBean(PoleService.class).getOne(Wrappers.lambdaQuery(Pole.class).eq(Pole::getDeviceCode, macCode));
+ if (pole != null) {
+ lightReportDataBo.setPoleName(pole.getPoleName());
+ }
+ lightReportDataBos.add(lightReportDataBo);
+
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ }
+
+ CommonPage commonPage = CommonPage.restPage(lightReportDataBos);
+ commonPage.setTotal(stringCommonPage.getTotal());
+ commonPage.setTotalPage(stringCommonPage.getTotalPage());
+ commonPage.setPageNum(pageNo);
+
+ return commonPage;
}
@SneakyThrows
- public String exportList(int pageNo, int pageSize, String keyword, String deviceCode) {
- PageHelper.startPage(pageNo, pageSize);
- List<LightReportDataBo> list = baseMapper.listReportData(keyword, deviceCode);
+ public void exportList(HttpServletRequest request, HttpServletResponse response, String deviceCode) {
+
+ if (SecurityUtils.getClientId() != null) {
+ PoleBinding one = SpringContextHolder.getBean(PoleBindingService.class).getOne(Wrappers.lambdaQuery(PoleBinding.class).eq(PoleBinding::getDeviceCode, deviceCode)
+ .eq(PoleBinding::getDeviceType, 0));
+ if (one == null) {
+ throw new BusinessException("鏈壘鍒扮粦瀹氬叧绯伙紒");
+ } else {
+ Pole pole = SpringContextHolder.getBean(PoleService.class).getOne(Wrappers.lambdaQuery(Pole.class).eq(Pole::getUserId, SecurityUtils.getUserId()).or(w -> {
+ w.eq(Pole::getClientId, SecurityUtils.getUserId());
+ }));
+ if (pole == null) {
+ throw new BusinessException("缁戝畾鍏崇郴涓嶆纭紒");
+ }
+ }
+ }
+
+ List<LightReportDataBo> list = lightReportDataMapper.listReportData(null, deviceCode);
File file = new File("./" + RandomUtil.randomString(12) + ".xlsx");
-// File file = new File(properties.getUploadRootPath() + "export" + File.separator + RandomUtil.randomString(12) + ".xlsx");
-// ClassPathResource cpr = new ClassPathResource("public/exportOrder.xlsx");
-// FileUtil.mkParentDirs(file);
-//
-// ExcelWriter excelWriter = EasyExcel.write(file).withTemplate(cpr.getInputStream()).build();
-// WriteSheet writeSheet = EasyExcel.writerSheet().build();
-// excelWriter.fill(list, writeSheet);
-// excelWriter.finish();
EasyExcel.write(file, LightReportDataBo.class).sheet("妯$増").doWrite(list);
- FileUploadDto fileUploadDto = fileService.uploadFile(file);
-// String url = file.getPath().replace(properties.getUploadRootPath(), "");
- String url = fileUploadDto.getFileUrl();
- file.delete();
- return url;
+ //鏂囦欢娴佸寲杩斿洖缁欏墠绔�
+ findfile(request, response, file);
+ //鍒犻櫎鏂囦欢
+ // file.delete();
+ }
- // return list;
+ /**
+ * 鏂囦欢娴佸寲
+ *
+ * @param request
+ * @param response
+ * @param file
+ * @throws IOException
+ */
+ public void findfile(HttpServletRequest request, HttpServletResponse response, File file) throws IOException {
+ ServletOutputStream out = null;
+ FileInputStream ips = null;
+ try {
+ //鑾峰彇鍒版枃瀛� 鏁版嵁搴撻噷瀵瑰簲鐨勯檮浠跺悕瀛楀姞涓婅�佺殑鏂囦欢鍚嶅瓧锛歠ilename 鎴彇鍒板悗闈㈢殑鏂囦欢绫诲瀷 渚嬶細txt 缁勬垚涓�涓柊鐨勬枃浠跺悕瀛楋細newFileName
+ //鐢熸垚3涓殢鏈烘暟锛岀敤鏉ョ敓鎴愭柊鐨勬枃浠跺悕瀛�
+ String newFileName = "鍗曠伅鏁版嵁" + RandomUtil.randomString(3) + ".xlsx";
+ if (!file.exists()) {
+ //濡傛灉鏂囦欢涓嶅瓨鍦ㄥ氨璺冲嚭
+ return;
+ }
+ ips = new FileInputStream(file);
+ response.setContentType("application/json;charset=utf-8");
+ //涓烘枃浠堕噸鏂拌缃悕瀛楋紝閲囩敤鏁版嵁搴撳唴瀛樺偍鐨勬枃浠跺悕绉�
+ response.addHeader("Content-Disposition", "attachment; filename=\"" + new String(newFileName.getBytes("UTF-8"), "ISO8859-1") + "\"");
+ out = response.getOutputStream();
+ //璇诲彇鏂囦欢娴�
+ int len = 0;
+ byte[] buffer = new byte[1024 * 10];
+ while ((len = ips.read(buffer)) != -1) {
+ out.write(buffer, 0, len);
+ }
+ out.flush();
+ } catch (Exception e) {
+ e.printStackTrace();
+ } finally {
+ try {
+ out.close();
+ ips.close();
+ file.delete();
+ } catch (IOException e) {
+ System.out.println("鍏抽棴娴佸嚭鐜板紓甯�");
+ e.printStackTrace();
+ }
+ }
}
}
--
Gitblit v1.9.3