From 0064fa7e640994b361d34fef55e13a9d0157f8bd Mon Sep 17 00:00:00 2001
From: zhanzhiqin <895896009@qq.com>
Date: 星期三, 27 四月 2022 11:11:02 +0800
Subject: [PATCH] fix

---
 ximon-admin/src/main/java/com/sandu/ximon/admin/service/LightReportDataService.java |  110 ++++++++++++++++++++++++++++++++++++++++++++++++++----
 1 files changed, 101 insertions(+), 9 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 3239d23..5f0f272 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,26 +1,45 @@
 package com.sandu.ximon.admin.service;
 
+import cn.hutool.core.collection.CollUtil;
+import cn.hutool.core.date.DateTime;
 import cn.hutool.core.io.FileUtil;
 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.alibaba.fastjson.JSON;
+import com.baomidou.mybatisplus.core.toolkit.Wrappers;
 import com.github.pagehelper.PageHelper;
+import com.sandu.common.domain.CommonPage;
+import com.sandu.common.execption.BusinessException;
+import com.sandu.common.file.FileUploadDto;
 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 java.io.File;
-import java.util.List;
+import java.sql.Wrapper;
+import java.time.LocalDate;
+import java.time.LocalDateTime;
+import java.time.format.DateTimeFormatter;
+import java.util.*;
 
 /**
  * @author chenjiantian
@@ -32,6 +51,7 @@
 public class LightReportDataService extends BaseServiceImpl<LightReportDataMapper, LightReportData> {
 
     private final FileProperties properties;
+    private final AliOssFileServiceImpl fileService;
 
     /**
      * 淇濆瓨涓婃姤鐨勭伅蹇冭烦鏁版嵁
@@ -39,9 +59,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);
     }
@@ -62,17 +97,66 @@
      * @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());
+
+        return commonPage;
     }
 
     @SneakyThrows
     public String exportList(int pageNo, int pageSize, String keyword, 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("缁戝畾鍏崇郴涓嶆纭紒");
+                }
+            }
+        }
+
         PageHelper.startPage(pageNo, pageSize);
         List<LightReportDataBo> list = baseMapper.listReportData(keyword, deviceCode);
 
-        File file = new File(properties.getUploadRootPath() + "export" + File.separator + RandomUtil.randomString(12) + ".xlsx");
+        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);
@@ -82,10 +166,18 @@
 //        excelWriter.fill(list, writeSheet);
 //        excelWriter.finish();
 
-        EasyExcel.write(file, LightReportDataBo.class).sheet("鍗曠伅鏁版嵁").doWrite(list);
-        String url = file.getPath().replace(properties.getUploadRootPath(), "");
-       return ResUtils.addPrefix(url);
+        EasyExcel.write(file, LightReportDataBo.class).sheet("妯$増").doWrite(list);
 
-       // return list;
+        FileUploadDto fileUploadDto = fileService.uploadFile(file);
+//        String url = file.getPath().replace(properties.getUploadRootPath(), "");
+        String url = fileUploadDto.getFileUrl();
+        file.delete();
+        return url;
+
+        // return list;
+    }
+
+    public List<LightReportDataBo> reportDataList(List<String> deviceCodeList, DateTime startDate, DateTime endDate) {
+        return baseMapper.reportDataList(deviceCodeList, startDate, endDate);
     }
 }

--
Gitblit v1.9.3