Browse Source

导出陈哥要的表格

develop
bobol 11 months ago
parent
commit
1401f3dbda
  1. 58
      lzbi-module/src/main/java/com/lzbi/draft/controller/ReportController.java
  2. 11
      lzbi-module/src/main/java/com/lzbi/task/domain/DaTangGuanJingDataVO.java
  3. 11
      lzbi-module/src/main/java/com/lzbi/task/domain/MultipleDeviceDataVO.java
  4. 8
      lzbi-module/src/main/java/com/lzbi/task/domain/ReadQueryReq.java
  5. 2
      lzbi-module/src/main/java/com/lzbi/task/domain/WorkDataDTO.java
  6. 4
      lzbi-module/src/main/java/com/lzbi/task/mapper/WorkParamReadMapper.java
  7. 53
      lzbi-module/src/main/java/com/lzbi/task/service/MultipDeviceReadService.java
  8. 78
      lzbi-module/src/main/java/com/lzbi/task/service/WorkParamReadService.java
  9. 48
      lzbi-module/src/main/resources/mapper/asset/WorkParamReadMapper.xml

58
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<String, SheetInfoBean> export(ReadQueryReq readQueryReq) {
public void export(ReadQueryReq readQueryReq, HttpServletResponse response) {
CountDownLatch countDownLatch = new CountDownLatch(3);
// 构造各个sheet页相关信息
Map<String, SheetInfoBean> 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();
}
/**

11
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;

11
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;

8
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<String> deviceCodes;

2
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 ;
/** 总条数 */

4
lzbi-module/src/main/java/com/lzbi/task/mapper/WorkParamReadMapper.java

@ -16,7 +16,9 @@ public interface WorkParamReadMapper extends BaseMapper<WorkParamInfo>
{
public List<DcBusiWorkReadLog> readWorkValue(ReadQueryParams readQueryParams);
List<WorkDataDTO> readWorkData (ReadQueryReq readQueryReq);
List<WorkDataDTO> readWorkData1 (ReadQueryReq readQueryReq);
List<WorkDataDTO> readWorkData2 (ReadQueryReq readQueryReq);
List<ThourWeatherVO> getThourWeather(ReadQueryReq readQueryReq);
}

53
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<WorkParamReadMapper, WorkParamInfo> implements IService<WorkParamInfo> {
private List<String> 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<String> dateRange = new ArrayList<>();
while (!startDate.isAfter(endDate)) {
dateRange.add(dateTimeFormatter.format(startDate));
startDate = startDate.plusDays(7);
}
dateRange.add(endDateStr);
return dateRange;
}
public List<WorkDataDTO> getMultipleDeviceData(ReadQueryReq readQueryReq) {
return baseMapper.readWorkData2(readQueryReq);
}
}

78
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<WorkParamReadMapper, WorkP
@Autowired
private ThreadPoolTaskExecutor threadPoolTaskExecutor;
@Autowired
private MultipDeviceReadService multipDeviceReadService;
/*获取生产单元的参数读取配置
* */
public List<DcBusiWorkReadLog> 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<String> 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<WorkParamReadMapper, WorkP
List<String> 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<WorkParamReadMapper, WorkP
Class<MultipleDeviceDataVO> 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<WorkDataDTO> workDataDTOS = new LinkedList<>();
List<String> dateList = this.splitByDay(readQueryReq.getBeginTime(), readQueryReq.getEndTime());
int size = dateList.size() - 1;
CountDownLatch countDownLatch = new CountDownLatch(size);
for (int i = 0; i < size; i++) {
String[] PARAM_CODE_ARR = new String[]{"007", "009", "011", "012", "013", "014", "045", "046", "047", "048", "001", "002"};
Vector<WorkDataDTO> 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(() -> {
int ii = index.getAndAdd(1);
log.info("ii = {}", ii);
ReadQueryReq req = new ReadQueryReq();
req.setBeginTime(dateList.get(i));
req.setEndTime(dateList.get(i + 1));
req.setBeginTime(readQueryReq.getBeginTime());
req.setEndTime(readQueryReq.getEndTime());
req.setParamCodes(Arrays.asList(PARAM_CODE_ARR));
req.setDeviceCodes(Arrays.asList(DEVICE_CODE_ARR));
threadPoolTaskExecutor.execute(() -> {
workDataDTOS.addAll(baseMapper.readWorkData(req));
log.info("{}~{} 执行成功", req.getBeginTime(), req.getEndTime());
countDownLatch.countDown();
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);
}
}
}
log.info("查询结束时间:{}", System.currentTimeMillis());
if (!CollectionUtils.isEmpty(workDataDTOS)) {
Map<Date, List<WorkDataDTO>> timeMap = workDataDTOS.stream().collect(Collectors.groupingBy(WorkDataDTO::getThour));
Set<Date> keySet = timeMap.keySet();
List<Date> keyList = keySet.stream().sorted().collect(Collectors.toList());
Map<String, List<WorkDataDTO>> timeMap = workDataDTOS.stream().collect(Collectors.groupingBy(WorkDataDTO::getThour));
Set<String> keySet = timeMap.keySet();
List<String> keyList = keySet.stream().sorted().collect(Collectors.toList());
keyList.forEach(key -> {
List<WorkDataDTO> value = timeMap.get(key);
Map<String, List<WorkDataDTO>> deviceMap = value.stream().collect(Collectors.groupingBy(WorkDataDTO::getDeviceCode));
@ -143,23 +169,23 @@ public class WorkParamReadService extends ServiceImpl<WorkParamReadMapper, WorkP
}
});
}
log.info("多设备:{}", list);
log.info("多设备查询完成");
return list;
}
public List<DaTangGuanJingDataVO> 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<DaTangGuanJingDataVO> clazz = DaTangGuanJingDataVO.class;
Field[] fields = clazz.getDeclaredFields();
Vector<DaTangGuanJingDataVO> list = new Vector<>();
List<WorkDataDTO> workDataDTOS = baseMapper.readWorkData(readQueryReq);
List<WorkDataDTO> workDataDTOS = baseMapper.readWorkData2(readQueryReq);
if (!CollectionUtils.isEmpty(workDataDTOS)) {
Map<Date, List<WorkDataDTO>> timeMap = workDataDTOS.stream().collect(Collectors.groupingBy(WorkDataDTO::getThour));
Set<Date> keySet = timeMap.keySet();
List<Date> keyList = keySet.stream().sorted().collect(Collectors.toList());
Map<String, List<WorkDataDTO>> timeMap = workDataDTOS.stream().collect(Collectors.groupingBy(WorkDataDTO::getThour));
Set<String> keySet = timeMap.keySet();
List<String> keyList = keySet.stream().sorted().collect(Collectors.toList());
keyList.forEach(key -> {
List<WorkDataDTO> value = timeMap.get(key);
Map<String, List<WorkDataDTO>> deviceMap = value.stream().collect(Collectors.groupingBy(WorkDataDTO::getDeviceCode));
@ -206,13 +232,13 @@ public class WorkParamReadService extends ServiceImpl<WorkParamReadMapper, WorkP
}
});
}
log.info("大唐管井:{}", list);
log.info("大唐管井查询完成");
return list;
}
public List<ThourWeatherVO> getThourWeather(ReadQueryReq readQueryReq) {
List<ThourWeatherVO> list = baseMapper.getThourWeather(readQueryReq);
log.info("天气:{}", list);
log.info("天气查询完成");
return list;
}

48
lzbi-module/src/main/resources/mapper/asset/WorkParamReadMapper.xml

@ -56,21 +56,55 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
<result property="lastVal" column="lastVal" />
</resultMap>
<select id="readWorkData" resultMap="WorkDataVoResult" parameterType="com.lzbi.task.domain.ReadQueryParams">
<select id="readWorkData1" resultMap="WorkDataVoResult" parameterType="com.lzbi.task.domain.ReadQueryParams">
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
<foreach item="item" index="index" collection="paramCodes" open="(" close=")" separator=",">
#{item}
</foreach>
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 &gt;= to_timestamp( #{beginTime}, 'yyyy-MM-dd hh24:mi:ss' )
AND t1.timestamp_key &lt; to_timestamp( #{endTime}, 'yyyy-MM-dd hh24:mi:ss' )
AND t1.device_uuid in
<foreach item="item" index="index" collection="deviceCodes" open="(" close=")" separator=",">
#{item}
</foreach>
GROUP BY
thour,
t1.device_uuid,
t2.param_model_id,
t3.param_class_name,
t4.partion,
t5.device_name
</select>
<select id="readWorkData2" resultMap="WorkDataVoResult" parameterType="com.lzbi.task.domain.ReadQueryParams">
SELECT
t2.param_model_id,
t3.param_class_name,
t1.device_uuid,
t5.device_name,
t4.partion,
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,
SUM ( CASE WHEN t1.param_value_num > 0.00 THEN 1 ELSE 0 END ) AS countsz,
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

Loading…
Cancel
Save