Sfoglia il codice sorgente

解决导出相关问题

wukai 1 anno fa
parent
commit
4ac16e6f54

+ 11 - 13
ruoyi-admin/src/main/java/com/ruoyi/biz/controller/ApiController.java

@@ -378,14 +378,12 @@ public class ApiController extends BaseController {
     @ApiOperation("导出产量数据")
     @GetMapping("/export/production")
     @CrossOrigin(origins = "*")
-    public void productionExport(HttpServletResponse response) {
-        LocalDateTime ldt = LocalDateTime.now();
-        if (ldt.getHour() < 7) {
-            ldt = ldt.minusDays(1);
-        }
-        LocalDate localDate = ldt.toLocalDate().minusDays(7);
-        Date date = Date.from(localDate.atStartOfDay(ZoneOffset.of("+8")).toInstant());
-        List<TwinCalcDay> list = twinCalcDayService.selectTwinCalcDayListByTime(date);
+    public void productionExport(String start, String end, HttpServletResponse response) {
+        LocalDate localDate = LocalDate.parse(start);
+        Date sd = Date.from(localDate.atStartOfDay(ZoneOffset.of("+8")).toInstant());
+        localDate = LocalDate.parse(end);
+        Date ed = Date.from(localDate.atStartOfDay(ZoneOffset.of("+8")).toInstant());
+        List<TwinCalcDay> list = twinCalcDayService.selectTwinCalcDayListByTime(sd,ed);
         try (FileInputStream inputStream = new FileInputStream(totalExcelTemplate); Workbook wb = new XSSFWorkbook(inputStream); OutputStream outputStream = new BufferedOutputStream(response.getOutputStream())) {
             CreationHelper creationHelper = wb.getCreationHelper();
             CellStyle percentStyle = wb.createCellStyle();
@@ -497,7 +495,7 @@ public class ApiController extends BaseController {
                     cells[j] = row.createCell(j);
                 }
 
-                cells[0].setCellValue(stop.getDeviceId());
+                cells[0].setCellValue(deviceService.deviceName(stop.getDeviceId()));
                 cells[1].setCellValue(stopStr[stop.getStopType() - 1]);
                 cells[2].setCellValue(stop.getStartTime());
                 cells[2].setCellStyle(timeStyle);
@@ -597,7 +595,7 @@ public class ApiController extends BaseController {
                     cs[0].setCellValue(entry.getKey());
                     cs[0].setCellStyle(dateStyle);
                     cs[1].setCellValue(st + "-" + et);
-                    cs[2].setCellValue(stop.getDeviceId());
+                    cs[2].setCellValue(deviceService.deviceName(stop.getDeviceId()));
                     cs[3].setCellValue(stopStr[stop.getStopType() - 1]);
                     cs[4].setCellValue(stop.getStartTime());
                     cs[4].setCellStyle(timeStyle);
@@ -621,7 +619,7 @@ public class ApiController extends BaseController {
             //Content-Disposition的作用:告知浏览器以何种方式显示响应返回的文件,用浏览器打开还是以附件的形式下载到本地保存
             //attachment表示以附件方式下载 inline表示在线打开 "Content-Disposition: inline; filename=文件名.mp3"
             // filename表示文件的默认名称,因为网络传输只支持URL编码的相关支付,因此需要将文件名URL编码后进行传输,前端收到后需要反编码才能获取到真正的名称
-            response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(stopStr[type-1] + "分析" + System.currentTimeMillis() + ".xlsx", "UTF-8"));
+            response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(stopStr[type - 1] + "分析" + System.currentTimeMillis() + ".xlsx", "UTF-8"));
             response.setContentType("application/octet-stream");
             wb.write(outputStream);
             outputStream.flush();
@@ -655,7 +653,7 @@ public class ApiController extends BaseController {
                 for (int j = 0; j < cells.length; j++) {
                     cells[j] = row.createCell(j);
                 }
-                cells[0].setCellValue(entry.getKey());
+                cells[0].setCellValue(deviceService.deviceName(entry.getKey()));
                 cells[1].setCellValue(alarms.size());
                 Map<Integer, Long> temp = alarms.stream().collect(Collectors.groupingBy(TwinCalcAlarms::getAlarmType, Collectors.counting()));
                 for (Integer v : temp.keySet()) {
@@ -673,7 +671,7 @@ public class ApiController extends BaseController {
                     cells[j] = row.createCell(j);
                 }
 
-                cells[0].setCellValue(alarms.getDeviceId());
+                cells[0].setCellValue(deviceService.deviceName(alarms.getDeviceId()));
                 cells[1].setCellValue(alarmStr[alarms.getAlarmType() - 1]);
                 cells[2].setCellValue(alarms.getStartTime());
                 cells[2].setCellStyle(timeStyle);

+ 5 - 2
ruoyi-admin/src/main/java/com/ruoyi/biz/mapper/TwinCalcDayMapper.java

@@ -1,6 +1,7 @@
 package com.ruoyi.biz.mapper;
 
 import com.ruoyi.biz.domain.TwinCalcDay;
+import org.apache.ibatis.annotations.Param;
 
 import java.util.Date;
 import java.util.List;
@@ -63,10 +64,12 @@ public interface TwinCalcDayMapper {
     /**
      * 查询指定日期及之后的数据
      *
-     * @param date 指定日期
+     * @param sd 开始日期
+     * @param ed 结束日期
      * @return 列表
      */
-    List<TwinCalcDay> selectTwinCalcDayListByTime(Date date);
+    List<TwinCalcDay> selectTwinCalcDayListByTime(@Param("sd") Date sd, @Param("ed") Date ed);
+
 
     /**
      * 按日期删除数据

+ 10 - 2
ruoyi-admin/src/main/java/com/ruoyi/biz/service/ITwinCalcDayService.java

@@ -64,10 +64,18 @@ public interface ITwinCalcDayService {
     /**
      * 查询指定日期及之后的数据
      *
-     * @param date 指定日期
+     * @param sd 开始日期
+     * @param ed 结束日期
      * @return 列表
      */
-    List<TwinCalcDay> selectTwinCalcDayListByTime(Date date);
+    List<TwinCalcDay> selectTwinCalcDayListByTime(Date sd,Date ed);
+    /**
+     * 查询指定日期及之后的数据
+     *
+     * @param sd 开始日期
+     * @return 列表
+     */
+    List<TwinCalcDay> selectTwinCalcDayListByTime(Date sd);
 
     /**
      * 统计昨日数据入库

+ 7 - 0
ruoyi-admin/src/main/java/com/ruoyi/biz/service/ITwinDeviceService.java

@@ -18,6 +18,13 @@ public interface ITwinDeviceService {
      * @return 设备管理
      */
     public TwinDevice selectTwinDeviceById(Long id);
+    /**
+     * 根据设备ID查询名称
+     *
+     * @param id 设备管理主键
+     * @return 设备名称
+     */
+    String deviceName(Long id);
 
     /**
      * 查询设备管理列表

+ 15 - 3
ruoyi-admin/src/main/java/com/ruoyi/biz/service/impl/TwinCalcDayServiceImpl.java

@@ -117,12 +117,24 @@ public class TwinCalcDayServiceImpl implements ITwinCalcDayService {
     /**
      * 查询指定日期及之后的数据
      *
-     * @param date 指定日期
+     * @param sd 开始日期
+     * @param ed 结束日期
      * @return 列表
      */
     @Override
-    public List<TwinCalcDay> selectTwinCalcDayListByTime(Date date) {
-        return twinCalcDayMapper.selectTwinCalcDayListByTime(date);
+    public List<TwinCalcDay> selectTwinCalcDayListByTime(Date sd, Date ed) {
+        return twinCalcDayMapper.selectTwinCalcDayListByTime(sd, ed);
+    }
+
+    /**
+     * 查询指定日期及之后的数据
+     *
+     * @param sd 开始日期
+     * @return 列表
+     */
+    @Override
+    public List<TwinCalcDay> selectTwinCalcDayListByTime(Date sd) {
+        return twinCalcDayMapper.selectTwinCalcDayListByTime(sd, null);
     }
 
     /**

+ 25 - 0
ruoyi-admin/src/main/java/com/ruoyi/biz/service/impl/TwinDeviceServiceImpl.java

@@ -3,11 +3,16 @@ package com.ruoyi.biz.service.impl;
 import com.ruoyi.biz.domain.TwinDevice;
 import com.ruoyi.biz.mapper.TwinDeviceMapper;
 import com.ruoyi.biz.service.ITwinDeviceService;
+import com.ruoyi.common.constant.Constants;
 import com.ruoyi.common.core.text.Convert;
+import com.ruoyi.common.utils.CacheUtils;
 import org.springframework.stereotype.Service;
 
 import javax.annotation.Resource;
+import java.util.HashMap;
 import java.util.List;
+import java.util.Map;
+import java.util.stream.Collectors;
 
 /**
  * 设备管理Service业务层处理
@@ -32,6 +37,26 @@ public class TwinDeviceServiceImpl implements ITwinDeviceService {
     }
 
     /**
+     * 根据设备ID查询名称
+     *
+     * @param id 设备管理主键
+     * @return 设备名称
+     */
+    @Override
+    public String deviceName(Long id) {
+        Object d = CacheUtils.get(Constants.IOT_TOKEN, Constants.DEVICE_ALL);
+        Map<Long, TwinDevice> deviceMap = new HashMap<>(16);
+        if (d == null) {
+            List<TwinDevice> list = selectTwinDeviceList(new TwinDevice());
+            deviceMap = list.stream().collect(Collectors.toMap(TwinDevice::getDeviceId, o -> o));
+            CacheUtils.put(Constants.IOT_TOKEN, Constants.DEVICE_ALL, deviceMap);
+        } else {
+            deviceMap = (Map<Long, TwinDevice>) d;
+        }
+        return deviceMap.get(id).getDeviceName();
+    }
+
+    /**
      * 查询设备管理列表
      *
      * @param twinDevice 设备管理

+ 37 - 35
ruoyi-admin/src/main/resources/mapper/biz/TwinCalcDayMapper.xml

@@ -39,40 +39,41 @@
     </resultMap>
 
     <sql id="selectTwinCalcDayVo">
-        select * from (select A.ID,
-                       A.TIME,
-                       A.DEVICE_ID,
-                       A.LENGTH,
-                       A.WEIGHT,
-                       A.EFFICIENCY,
-                       A.KWH,
-                       A.ALARM,
-                       A.LENGTH_A,
-                       A.WEIGHT_A,
-                       A.EFFICIENCY_A,
-                       A.OPEN_TIME_A,
-                       A.CLOSE_TIME_A,
-                       A.KWH_A,
-                       A.STOP1_A,
-                       A.STOP2_A,
-                       A.STOP3_A,
-                       A.LENGTH_B,
-                       A.WEIGHT_B,
-                       A.EFFICIENCY_B,
-                       A.OPEN_TIME_B,
-                       A.CLOSE_TIME_B,
-                       A.KWH_B,
-                       A.STOP1_B,
-                       A.STOP2_B,
-                       A.STOP3_B,
-                       A.CREATED_BY,
-                       A.CREATED_TIME,
-                       A.UPDATED_BY,
-                       A.UPDATED_TIME,
-                       B.device_name REMARK
-                from twin_calc_day a,
-                     twin_device b
-                where a.device_id = b.device_id) a
+        select *
+        from (select A.ID,
+                     A.TIME,
+                     A.DEVICE_ID,
+                     A.LENGTH,
+                     A.WEIGHT,
+                     A.EFFICIENCY,
+                     A.KWH,
+                     A.ALARM,
+                     A.LENGTH_A,
+                     A.WEIGHT_A,
+                     A.EFFICIENCY_A,
+                     A.OPEN_TIME_A,
+                     A.CLOSE_TIME_A,
+                     A.KWH_A,
+                     A.STOP1_A,
+                     A.STOP2_A,
+                     A.STOP3_A,
+                     A.LENGTH_B,
+                     A.WEIGHT_B,
+                     A.EFFICIENCY_B,
+                     A.OPEN_TIME_B,
+                     A.CLOSE_TIME_B,
+                     A.KWH_B,
+                     A.STOP1_B,
+                     A.STOP2_B,
+                     A.STOP3_B,
+                     A.CREATED_BY,
+                     A.CREATED_TIME,
+                     A.UPDATED_BY,
+                     A.UPDATED_TIME,
+                     B.device_name REMARK
+              from twin_calc_day a,
+                   twin_device b
+              where a.device_id = b.device_id) a
     </sql>
 
     <select id="selectTwinCalcDayList" parameterType="TwinCalcDay" resultMap="TwinCalcDayResult">
@@ -117,7 +118,8 @@
     </select>
     <select id="selectTwinCalcDayListByTime" resultMap="TwinCalcDayResult">
         <include refid="selectTwinCalcDayVo"/>
-        where time>=#{date}
+        where time>=#{sd}
+        <if test="ed != null ">and time&lt;=#{ed}</if>
         order by time
     </select>
 

+ 4 - 0
ruoyi-common/src/main/java/com/ruoyi/common/constant/Constants.java

@@ -150,4 +150,8 @@ public class Constants {
      */
     public static final String[] JOB_ERROR_STR = {"java.net.URL", "javax.naming.InitialContext", "org.yaml.snakeyaml",
             "org.springframework", "org.apache", "com.ruoyi.common.utils.file", "com.ruoyi.common.config", "com.ruoyi.generator"};
+    /**
+     * 设备缓存
+     */
+    public static final String DEVICE_ALL = "deviceAll";
 }