diff --git a/lzbi-module/src/main/java/com/lzbi/draft/controller/ReportController.java b/lzbi-module/src/main/java/com/lzbi/draft/controller/ReportController.java index 93b97b5..fd4a938 100644 --- a/lzbi-module/src/main/java/com/lzbi/draft/controller/ReportController.java +++ b/lzbi-module/src/main/java/com/lzbi/draft/controller/ReportController.java @@ -19,8 +19,9 @@ import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; -import java.io.File; -import java.io.IOException; +import javax.servlet.http.HttpServletResponse; +import java.io.*; +import java.net.URLEncoder; import java.util.LinkedList; import java.util.List; import java.util.Map; @@ -56,7 +57,7 @@ public class ReportController extends BaseController { } @GetMapping("/export") - public Map export(ReadQueryReq readQueryReq) { + public void export(ReadQueryReq readQueryReq, HttpServletResponse response) { CountDownLatch countDownLatch = new CountDownLatch(3); // 构造各个sheet页相关信息 Map map = new ConcurrentHashMap(); @@ -71,6 +72,7 @@ public class ReportController extends BaseController { countDownLatch.countDown(); }); try { + // 不知道因为啥,不加上有时候下边sql查不到东西 Thread.sleep(100); } catch (InterruptedException e) { throw new RuntimeException(e); @@ -85,14 +87,15 @@ public class ReportController extends BaseController { } catch (InterruptedException e) { log.error("门闩异常", e); } - String filePath = ruoYiConfig.getProfile() + "/数据报表_" + System.currentTimeMillis() + ".xlsx"; + String fileName = "/数据报表_" + System.currentTimeMillis() + ".xlsx"; + String filePath = ruoYiConfig.getProfile() + fileName; log.info("文件路径:{}", filePath); // 导出文件 File file = new File(filePath); try { file.createNewFile(); } catch (IOException e) { - throw new RuntimeException(e); + log.error("IO异常", e); } try(ExcelWriter excelWriter = EasyExcel.write(file).build()) { WriteSheet writeSheet; @@ -113,7 +116,50 @@ public class ReportController extends BaseController { } catch (Exception e) { log.error("导出excel失败", e); } - return map; + //清空response + response.reset(); + //让浏览器下载文件,fileName是上述默认文件下载名 + response.setContentType("application/vnd.ms-excel;charset=utf-8"); + //设置response的Header + try { + //header仍是乱码??? + response.setHeader("Content-Disposition","attachment;filename=" + URLEncoder.encode(fileName,"UTF8")); + } catch (UnsupportedEncodingException e) { + log.error("设置header异常", e); + } + response.addHeader("Cache-Control","no-cache"); + InputStream inputStream=null; + ByteArrayOutputStream bos=null; + try { + inputStream = new FileInputStream(file); + bos = new ByteArrayOutputStream(1024); + int len = -1; + byte[] b = new byte[1024]; + while((len = inputStream.read(b)) != -1){ + bos.write(b,0,len); + } + response.getOutputStream().write(bos.toByteArray()); + //刷新 + bos.flush(); + } catch (FileNotFoundException e) { + log.error("文件找不到", e); + } catch (IOException e) { + log.error("IO异常", e); + } finally { + //关闭流 + try { + if(inputStream != null) { + inputStream.close(); + } + if(bos != null) { + bos.close(); + } + } catch (IOException e) { + log.error("IO异常", e); + } + } + //删除临时文件 + file.delete(); } /** diff --git a/lzbi-module/src/main/java/com/lzbi/task/domain/DaTangGuanJingDataVO.java b/lzbi-module/src/main/java/com/lzbi/task/domain/DaTangGuanJingDataVO.java index 77e76ca..c2c239c 100644 --- a/lzbi-module/src/main/java/com/lzbi/task/domain/DaTangGuanJingDataVO.java +++ b/lzbi-module/src/main/java/com/lzbi/task/domain/DaTangGuanJingDataVO.java @@ -31,9 +31,8 @@ public class DaTangGuanJingDataVO { /** * 统计时间 */ - @ExcelProperty("统计时间") - @JsonFormat(pattern = "yyyy-MM-dd HH") - private Date thour; + @ExcelProperty(value = "统计时间") + private String thour; /** * 瞬时流量 */ @@ -42,8 +41,8 @@ public class DaTangGuanJingDataVO { /** * 瞬时热量 */ - @ExcelProperty("瞬时热量") - private Double ssrl; + @ExcelProperty("累计热量") + private Double ljrl; /** * 一网供温 */ @@ -97,7 +96,7 @@ public class DaTangGuanJingDataVO { public DaTangGuanJingDataVO() { this.ssll = 0D; - this.ssrl = 0D; + this.ljrl = 0D; this.ywgw = 0D; this.ywhw = 0D; this.ywgy = 0D; diff --git a/lzbi-module/src/main/java/com/lzbi/task/domain/MultipleDeviceDataVO.java b/lzbi-module/src/main/java/com/lzbi/task/domain/MultipleDeviceDataVO.java index 1b8f816..5e41be8 100644 --- a/lzbi-module/src/main/java/com/lzbi/task/domain/MultipleDeviceDataVO.java +++ b/lzbi-module/src/main/java/com/lzbi/task/domain/MultipleDeviceDataVO.java @@ -29,9 +29,8 @@ public class MultipleDeviceDataVO { /** * 统计时间 */ - @ExcelProperty("统计时间") - @JsonFormat(pattern = "yyyy-MM-dd HH") - private Date thour; + @ExcelProperty(value = "统计时间") + private String thour; /** * 瞬时流量 */ @@ -40,8 +39,8 @@ public class MultipleDeviceDataVO { /** * 顺时热量 */ - @ExcelProperty("顺时热量") - private Double ssrl; + @ExcelProperty("累计热量") + private Double ljrl; /** * 一网供温 */ @@ -95,7 +94,7 @@ public class MultipleDeviceDataVO { public MultipleDeviceDataVO() { this.ssll = 0D; - this.ssrl = 0D; + this.ljrl = 0D; this.ywgw = 0D; this.ywhw = 0D; this.ywgy = 0D; diff --git a/lzbi-module/src/main/java/com/lzbi/task/domain/ReadQueryReq.java b/lzbi-module/src/main/java/com/lzbi/task/domain/ReadQueryReq.java index d578ac8..aea0924 100644 --- a/lzbi-module/src/main/java/com/lzbi/task/domain/ReadQueryReq.java +++ b/lzbi-module/src/main/java/com/lzbi/task/domain/ReadQueryReq.java @@ -17,13 +17,13 @@ import java.util.List; public class ReadQueryReq { @NotNull(message = "请输入起始时间") - @JsonFormat(pattern = "yyyy-MM-dd HH") - @DateTimeFormat(pattern = "yyyy-MM-dd HH") + @JsonFormat(pattern = "yyyy-MM-dd") + @DateTimeFormat(pattern = "yyyy-MM-dd") private String beginTime; @NotNull(message = "请输入结束时间") - @JsonFormat(pattern = "yyyy-MM-dd HH") - @DateTimeFormat(pattern = "yyyy-MM-dd HH") + @JsonFormat(pattern = "yyyy-MM-dd") + @DateTimeFormat(pattern = "yyyy-MM-dd") private String endTime; private List deviceCodes; diff --git a/lzbi-module/src/main/java/com/lzbi/task/domain/WorkDataDTO.java b/lzbi-module/src/main/java/com/lzbi/task/domain/WorkDataDTO.java index 22643f4..39bc7a3 100644 --- a/lzbi-module/src/main/java/com/lzbi/task/domain/WorkDataDTO.java +++ b/lzbi-module/src/main/java/com/lzbi/task/domain/WorkDataDTO.java @@ -18,7 +18,7 @@ public class WorkDataDTO { /** 分区 */ private String partion; /** 统计时间 */ - private Date thour ; + private String thour ; /** 和值 */ private Double total ; /** 总条数 */ diff --git a/lzbi-module/src/main/java/com/lzbi/task/mapper/WorkParamReadMapper.java b/lzbi-module/src/main/java/com/lzbi/task/mapper/WorkParamReadMapper.java index c61d016..9792a94 100644 --- a/lzbi-module/src/main/java/com/lzbi/task/mapper/WorkParamReadMapper.java +++ b/lzbi-module/src/main/java/com/lzbi/task/mapper/WorkParamReadMapper.java @@ -16,7 +16,9 @@ public interface WorkParamReadMapper extends BaseMapper { public List readWorkValue(ReadQueryParams readQueryParams); - List readWorkData (ReadQueryReq readQueryReq); + List readWorkData1 (ReadQueryReq readQueryReq); + + List readWorkData2 (ReadQueryReq readQueryReq); List getThourWeather(ReadQueryReq readQueryReq); } diff --git a/lzbi-module/src/main/java/com/lzbi/task/service/MultipDeviceReadService.java b/lzbi-module/src/main/java/com/lzbi/task/service/MultipDeviceReadService.java new file mode 100644 index 0000000..024658c --- /dev/null +++ b/lzbi-module/src/main/java/com/lzbi/task/service/MultipDeviceReadService.java @@ -0,0 +1,53 @@ +package com.lzbi.task.service; + +import com.baomidou.dynamic.datasource.annotation.DS; +import com.baomidou.mybatisplus.extension.service.IService; +import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; +import com.lzbi.common.constant.PartitionConstants; +import com.lzbi.task.domain.*; +import com.lzbi.task.mapper.WorkParamReadMapper; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; +import org.springframework.stereotype.Service; +import org.springframework.util.CollectionUtils; + +import java.lang.reflect.Field; +import java.math.BigDecimal; +import java.math.RoundingMode; +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; +import java.util.*; +import java.util.concurrent.CountDownLatch; +import java.util.stream.Collectors; + + +/** + * 编码规则定义Service业务层处理 + * + * @author win + * @date 2023-11-29 + */ +@DS("workDB") +@Service +@Slf4j +public class MultipDeviceReadService extends ServiceImpl implements IService { + + private List splitByDay(String startDateStr, String endDateStr) { + DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH"); + LocalDateTime startDate = LocalDateTime.parse(startDateStr, dateTimeFormatter); + LocalDateTime endDate = LocalDateTime.parse(endDateStr, dateTimeFormatter); + List dateRange = new ArrayList<>(); + while (!startDate.isAfter(endDate)) { + dateRange.add(dateTimeFormatter.format(startDate)); + startDate = startDate.plusDays(7); + } + dateRange.add(endDateStr); + return dateRange; + } + + public List getMultipleDeviceData(ReadQueryReq readQueryReq) { + return baseMapper.readWorkData2(readQueryReq); + } + +} diff --git a/lzbi-module/src/main/java/com/lzbi/task/service/WorkParamReadService.java b/lzbi-module/src/main/java/com/lzbi/task/service/WorkParamReadService.java index 5beed12..5e211f9 100644 --- a/lzbi-module/src/main/java/com/lzbi/task/service/WorkParamReadService.java +++ b/lzbi-module/src/main/java/com/lzbi/task/service/WorkParamReadService.java @@ -29,6 +29,7 @@ import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.util.*; import java.util.concurrent.CountDownLatch; +import java.util.concurrent.atomic.AtomicInteger; import java.util.stream.Collectors; @@ -46,12 +47,21 @@ public class WorkParamReadService extends ServiceImpl readWorkValue(ReadQueryParams readQueryParams) { return baseMapper.readWorkValue(readQueryParams); } + public static void main(String[] args) { + for (int i = 0; i < 43; i++) { + System.out.println("" + (i % 5)); + } + } + private List splitByDay(String startDateStr, String endDateStr) { DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH"); LocalDateTime startDate = LocalDateTime.parse(startDateStr, dateTimeFormatter); @@ -59,7 +69,7 @@ public class WorkParamReadService extends ServiceImpl dateRange = new ArrayList<>(); while (!startDate.isAfter(endDate)) { dateRange.add(dateTimeFormatter.format(startDate)); - startDate = startDate.plusDays(1); + startDate = startDate.plusDays(7); } dateRange.add(endDateStr); return dateRange; @@ -70,32 +80,48 @@ public class WorkParamReadService extends ServiceImpl clazz = MultipleDeviceDataVO.class; Field[] fields = clazz.getDeclaredFields(); String[] DEVICE_CODE_ARR = new String[]{"00011","00053","00054","00050","00055","00051","00052","00059","00056","00057","00022","00023","00024","00009","00058","00026","00029","00030","00031","00032","00044","00013","00007","00017","00046","00042","00019","00016","00001","00028","00045","00021","00010","00041","00015","00014","00005","00006","00002","00003","00004","00008","00018","00027","00043","00020","00040"}; - String[] PARAM_CODE_ARR = new String[]{"007", "009", "011", "012", "013", "014", "045", "046", "047", "048", "028", "029"}; - List workDataDTOS = new LinkedList<>(); - List dateList = this.splitByDay(readQueryReq.getBeginTime(), readQueryReq.getEndTime()); - int size = dateList.size() - 1; - CountDownLatch countDownLatch = new CountDownLatch(size); - for (int i = 0; i < size; i++) { - ReadQueryReq req = new ReadQueryReq(); - req.setBeginTime(dateList.get(i)); - req.setEndTime(dateList.get(i + 1)); - req.setParamCodes(Arrays.asList(PARAM_CODE_ARR)); - req.setDeviceCodes(Arrays.asList(DEVICE_CODE_ARR)); + String[] PARAM_CODE_ARR = new String[]{"007", "009", "011", "012", "013", "014", "045", "046", "047", "048", "001", "002"}; + Vector workDataDTOS = new Vector(); + int size = DEVICE_CODE_ARR.length; + int SIGN = 10; + CountDownLatch countDownLatch = new CountDownLatch(SIGN); + AtomicInteger index = new AtomicInteger(0); + log.info("查询起始时间:{}", System.currentTimeMillis()); + for (int i = 0, sign = SIGN; i < sign && i < size; i++) { + CountDownLatch finalCountDownLatch = countDownLatch; threadPoolTaskExecutor.execute(() -> { - workDataDTOS.addAll(baseMapper.readWorkData(req)); - log.info("{}~{} 执行成功", req.getBeginTime(), req.getEndTime()); - countDownLatch.countDown(); + int ii = index.getAndAdd(1); + log.info("ii = {}", ii); + ReadQueryReq req = new ReadQueryReq(); + req.setBeginTime(readQueryReq.getBeginTime()); + req.setEndTime(readQueryReq.getEndTime()); + req.setParamCodes(Arrays.asList(PARAM_CODE_ARR)); + req.setDeviceCodes(Arrays.asList(DEVICE_CODE_ARR[ii])); + log.info("{} {} sql执行起始时间 {}", ii, DEVICE_CODE_ARR[ii], System.currentTimeMillis()); + workDataDTOS.addAll(multipDeviceReadService.getMultipleDeviceData(req)); + log.info("{} {} sql执行结束时间 {}", ii, DEVICE_CODE_ARR[ii], System.currentTimeMillis()); + req = null; + finalCountDownLatch.countDown(); }); + if (i == sign - 1 || i == size - 1) { + try { + countDownLatch.await(); + int j = size - 1 - i; + if (j < SIGN) { + SIGN = j; + } + sign += SIGN; + countDownLatch = new CountDownLatch(SIGN); + } catch (InterruptedException e) { + log.error("门闩异常", e); + } + } } - try { - countDownLatch.await(); - } catch (InterruptedException e) { - log.error("门闩异常", e); - } + log.info("查询结束时间:{}", System.currentTimeMillis()); if (!CollectionUtils.isEmpty(workDataDTOS)) { - Map> timeMap = workDataDTOS.stream().collect(Collectors.groupingBy(WorkDataDTO::getThour)); - Set keySet = timeMap.keySet(); - List keyList = keySet.stream().sorted().collect(Collectors.toList()); + Map> timeMap = workDataDTOS.stream().collect(Collectors.groupingBy(WorkDataDTO::getThour)); + Set keySet = timeMap.keySet(); + List keyList = keySet.stream().sorted().collect(Collectors.toList()); keyList.forEach(key -> { List value = timeMap.get(key); Map> deviceMap = value.stream().collect(Collectors.groupingBy(WorkDataDTO::getDeviceCode)); @@ -143,23 +169,23 @@ public class WorkParamReadService extends ServiceImpl getDaTangGuanJingDataVO(ReadQueryReq readQueryReq) { String[] DEVICE_CODE_ARR = new String[]{"00033"}; - String[] PARAM_CODE_ARR = new String[]{"007", "009", "011", "012", "013", "014", "049", "054", "030", "031", "028", "029"}; + String[] PARAM_CODE_ARR = new String[]{"007", "009", "011", "012", "013", "014", "049", "054", "030", "031", "001", "002"}; readQueryReq.setParamCodes(Arrays.asList(PARAM_CODE_ARR)); readQueryReq.setDeviceCodes(Arrays.asList(DEVICE_CODE_ARR)); Class clazz = DaTangGuanJingDataVO.class; Field[] fields = clazz.getDeclaredFields(); Vector list = new Vector<>(); - List workDataDTOS = baseMapper.readWorkData(readQueryReq); + List workDataDTOS = baseMapper.readWorkData2(readQueryReq); if (!CollectionUtils.isEmpty(workDataDTOS)) { - Map> timeMap = workDataDTOS.stream().collect(Collectors.groupingBy(WorkDataDTO::getThour)); - Set keySet = timeMap.keySet(); - List keyList = keySet.stream().sorted().collect(Collectors.toList()); + Map> timeMap = workDataDTOS.stream().collect(Collectors.groupingBy(WorkDataDTO::getThour)); + Set keySet = timeMap.keySet(); + List keyList = keySet.stream().sorted().collect(Collectors.toList()); keyList.forEach(key -> { List value = timeMap.get(key); Map> deviceMap = value.stream().collect(Collectors.groupingBy(WorkDataDTO::getDeviceCode)); @@ -206,13 +232,13 @@ public class WorkParamReadService extends ServiceImpl getThourWeather(ReadQueryReq readQueryReq) { List list = baseMapper.getThourWeather(readQueryReq); - log.info("天气:{}", list); + log.info("天气查询完成"); return list; } diff --git a/lzbi-module/src/main/resources/mapper/asset/WorkParamReadMapper.xml b/lzbi-module/src/main/resources/mapper/asset/WorkParamReadMapper.xml index 4e5f0ce..1dd5413 100644 --- a/lzbi-module/src/main/resources/mapper/asset/WorkParamReadMapper.xml +++ b/lzbi-module/src/main/resources/mapper/asset/WorkParamReadMapper.xml @@ -56,21 +56,55 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" - SELECT t2.param_model_id, t3.param_class_name, t1.device_uuid, t5.device_name, t4.partion, - time_bucket ( '1 HOUR', t1.timestamp_key ) AS thour, + to_char(time_bucket ( '1 HOUR', t1.timestamp_key ), 'yyyy-MM-dd hh24') AS thour, SUM ( CASE WHEN t1.param_value_num > 0.00 THEN t1.param_value_num ELSE 0 END ) AS total, - COUNT ( 1 ) AS countsall, SUM ( CASE WHEN t1.param_value_num > 0.00 THEN 1 ELSE 0 END ) AS countsz, - MAX ( t1.param_value_num ) AS maxVal, - FIRST ( t1.param_value_num, timestamp_key ) AS firstVal, - LAST ( t1.param_value_num, timestamp_key ) AS lastVal, - MIN ( t1.param_value_num ) AS minVal + LAST ( t1.param_value_num, timestamp_key ) AS lastVal + FROM + dc_base_log_history_level1 t1 + INNER JOIN dc_base_device_param t2 ON t1.param_code = t2.param_code + AND t2.param_model_id IN + + #{item} + + LEFT JOIN dc_model_paramclass t3 ON t2.param_model_id = t3.param_class_code + LEFT JOIN dc_base_device_param t4 ON t4.param_code = t1.param_code + AND t4.device_uuid = t4.device_uuid + LEFT JOIN dc_base_deviceinfo t5 ON t5.center_device_code = t1.device_uuid + WHERE + t1.timestamp_key >= to_timestamp( #{beginTime}, 'yyyy-MM-dd hh24:mi:ss' ) + AND t1.timestamp_key < to_timestamp( #{endTime}, 'yyyy-MM-dd hh24:mi:ss' ) + AND t1.device_uuid in + + #{item} + + GROUP BY + thour, + t1.device_uuid, + t2.param_model_id, + t3.param_class_name, + t4.partion, + t5.device_name + + +