diff --git a/lzbi-admin/src/main/resources/application-dev.yml b/lzbi-admin/src/main/resources/application-dev.yml index bdd9947..0886e9e 100644 --- a/lzbi-admin/src/main/resources/application-dev.yml +++ b/lzbi-admin/src/main/resources/application-dev.yml @@ -201,3 +201,6 @@ patrol-server: # 消息推送 message-send: url: ${goal-server.message-server}/message/send + +# 报表文件路径 +reportFilePath: D:/ruoyi/report \ No newline at end of file diff --git a/lzbi-admin/src/main/resources/application-test.yml b/lzbi-admin/src/main/resources/application-test.yml index 242be73..df5114c 100644 --- a/lzbi-admin/src/main/resources/application-test.yml +++ b/lzbi-admin/src/main/resources/application-test.yml @@ -198,4 +198,7 @@ patrol-server: # 消息推送 message-send: - url: ${goal-server.message-server}/message/send \ No newline at end of file + url: ${goal-server.message-server}/message/send + +# 报表文件路径 +reportFilePath: /data/luenmeilz_bi_backend/report \ No newline at end of file diff --git a/lzbi-admin/src/main/resources/application.yml b/lzbi-admin/src/main/resources/application.yml index 9af1cf0..3466c76 100644 --- a/lzbi-admin/src/main/resources/application.yml +++ b/lzbi-admin/src/main/resources/application.yml @@ -60,7 +60,7 @@ spring: # 国际化资源文件路径 basename: i18n/messages profiles: - active: test + active: dev # 文件上传 servlet: multipart: diff --git a/lzbi-common/src/main/java/com/lzbi/common/constant/BizConstants.java b/lzbi-common/src/main/java/com/lzbi/common/constant/BizConstants.java index 1b9fd5a..4fb96a4 100644 --- a/lzbi-common/src/main/java/com/lzbi/common/constant/BizConstants.java +++ b/lzbi-common/src/main/java/com/lzbi/common/constant/BizConstants.java @@ -5,6 +5,28 @@ package com.lzbi.common.constant; */ public interface BizConstants { + /** + * 报表记录状态 + */ + interface ReportRecordStatus { + /** + * 准备数据 + */ + String PREPARE_DATA = "0"; + /** + * 未下载 + */ + String NOT_DOWNLOAD = "1"; + /** + * 已下载 + */ + String DOWNLOADED = "2"; + /** + * 导出失败 + */ + String ERROR = "3"; + } + /** * 巡检告警类型 */ diff --git a/lzbi-module/src/main/java/com/lzbi/bi/controller/DcBusiHisReportController.java b/lzbi-module/src/main/java/com/lzbi/bi/controller/DcBusiHisReportController.java index 9bb4c0c..e05a5a6 100644 --- a/lzbi-module/src/main/java/com/lzbi/bi/controller/DcBusiHisReportController.java +++ b/lzbi-module/src/main/java/com/lzbi/bi/controller/DcBusiHisReportController.java @@ -13,11 +13,14 @@ import com.lzbi.code.domain.WeatherReportVO; import com.lzbi.code.service.LogTimesacleHistoryThreeService; import com.lzbi.common.core.controller.BaseController; import com.lzbi.common.core.domain.AjaxResult; +import com.lzbi.common.core.domain.model.LoginUser; +import com.lzbi.common.utils.SecurityUtils; import com.lzbi.common.utils.StringUtils; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import lombok.extern.slf4j.Slf4j; import org.springframework.security.access.prepost.PreAuthorize; +import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.*; import javax.annotation.Resource; @@ -51,7 +54,7 @@ public class DcBusiHisReportController extends BaseController { * @return 分页数据 */ @PostMapping("/listReport") - public AjaxResult list(@RequestBody LogTimeThreeQueryParamVo queryVo) { + public AjaxResult list(@Validated @RequestBody LogTimeThreeQueryParamVo queryVo) { String error = validatorator(queryVo); if (error != null) { return AjaxResult.error(error); @@ -181,108 +184,25 @@ public class DcBusiHisReportController extends BaseController { return null; } - ///按时间 设备+参数 形成数据--时间分组 - private List> getMapList(List list, List dcDymicReportHeaderVos) { - Map> collect = list.stream().collect(Collectors.groupingBy(LogTimescaleHistoryThreeVo::getTimeStampString)); - return collect.entrySet().stream().sorted(Map.Entry.comparingByKey()).map(entry -> { - Map map = new LinkedHashMap<>(); - map.put("统计时间", entry.getKey()); - entry.getValue().stream().sorted(Comparator.comparing(LogTimescaleHistoryThreeVo::getDeviceUuid).thenComparing(LogTimescaleHistoryThreeVo::getParamCode)).forEach(vo -> { - String key = vo.getDeviceUuid() + "_" + vo.getParamCode(); - Optional first = dcDymicReportHeaderVos.stream().filter(vos -> vos.getColCode().equals(key)).findFirst(); - if (first.isPresent()) { - //map.put(first.get().getColName1(), first.get().getColName2()); - map.put(first.get().getColName1() + '.' + first.get().getColName2(), vo.getParamValueNum()); - } - }); - return map; - }).collect(Collectors.toList()); - } - - /// 设备+参数 形成数据--设备-参数分组 - private List> getMapListDevice(List list, List dcDymicReportHeaderVos) { - Map> collect = list.stream().collect(Collectors.groupingBy(item -> item.getDeviceUuid() + "_" + item.getParamCode())); - List> collect1 = collect.entrySet().stream().map(entry -> { - Map map = new LinkedHashMap<>(); - entry.getValue().stream().sorted(Comparator.comparing(LogTimescaleHistoryThreeVo::getTimeStampString).thenComparing(LogTimescaleHistoryThreeVo::getParamCode)).forEach(vo -> { - String key = vo.getDeviceUuid() + "_" + vo.getParamCode(); - Optional first = dcDymicReportHeaderVos.stream().filter(vos -> vos.getColCode().equals(key)).findFirst(); - if (first.isPresent()) { - map.put("设备名称", first.get().getColName1()); - map.put("参数名称", first.get().getColName2()); - map.put(vo.getTimeStampString(), vo.getParamValueNum()); - } - }); - return map; - }).collect(Collectors.toList()); - return collect1; - } - /** * "生产设备参数统计明细表导出功能 */ @ApiOperation("生产设备参数统计明细表导出功能--时间纵向") @PreAuthorize("@ss.hasPermi('report:iot:export')") @PostMapping("/export") - public void export(HttpServletResponse response, LogTimeThreeQueryParamVo queryVo) throws IOException { - List> mapList = getExcellData(queryVo, 2); - toExcel(response, mapList); + public AjaxResult export(@Validated @RequestBody LogTimeThreeQueryParamVo queryVo) { + LoginUser loginUser = SecurityUtils.getLoginUser(); + logTimesacleHistoryThreeService.getExcellData(queryVo, 2, loginUser); + return AjaxResult.success(); } @ApiOperation("生产设备参数统计明细表导出功能-时间横向") @PreAuthorize("@ss.hasPermi('report:iot:export')") @PostMapping("/export2") - public void export2(HttpServletResponse response, LogTimeThreeQueryParamVo queryVo) throws IOException { - List> mapList = getExcellData(queryVo, 1); - // 通过工具类创建writer,默认创建xls格式 - toExcel(response, mapList); - - } - - //获取excel数据 1 excel设备参数为列模式 2 excel时间为列模式 3 浏览器设备参数为列模式 - //注意 excel有最大列数显示255 ,超出列数会报错 - private List> getExcellData(LogTimeThreeQueryParamVo queryVo, int type) { - // 设置头部数据 - DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); - LocalDateTime startTime = LocalDateTime.parse(queryVo.getBeginTime(), dateTimeFormatter); - LocalDateTime endTime = LocalDateTime.parse(queryVo.getEndTime(), dateTimeFormatter); - LocalDate startDate = startTime.toLocalDate(); - LocalDate endDate = endTime.toLocalDate(); - if (endDate.isBefore(startDate)) { - throw new RuntimeException("结束日期不能小于开始日期"); - } - if (startDate.plusMonths(1L).isBefore(endDate)) { - throw new RuntimeException("日期间隔不能大于1个月"); - } - DcDymicHeaderQueryVo dcDymicHeaderQueryVo = new DcDymicHeaderQueryVo(); - dcDymicHeaderQueryVo.setParamModels(queryVo.getQueryParamClass()); - dcDymicHeaderQueryVo.setDeviceUuids(queryVo.getDeviceUuids()); - List dcDymicReportHeaderVos = logTimesacleHistoryThreeService.selectHeaderInfo(dcDymicHeaderQueryVo); - List collect = dcDymicReportHeaderVos.stream().map(DcDymicReportHeaderVo::getParamCode).collect(Collectors.toList()); - queryVo.setQueryParamCodes(collect); - List list = logTimesacleHistoryThreeService.selectDetailByQuery(queryVo); - if (type == 1) { - return getMapListDevice(list, dcDymicReportHeaderVos); - } else { - return getMapList(list, dcDymicReportHeaderVos); - } - } - - private void toExcel(HttpServletResponse response, List> mapList) throws IOException { - String fileName = "report" + DateUtil.now() + ".xlsx"; - response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); - response.setCharacterEncoding("utf-8"); - //ExcelWriter writer = ExcelUtil.getWriter(true); - BigExcelWriter writer = ExcelUtil.getBigWriter(); - writer.write(mapList, true); - //test.xls是弹出下载对话框的文件名,不能为中文,中文请自行编码 - //response.setHeader("Content-Disposition", "attachment;filename="+fileName); - ServletOutputStream out = response.getOutputStream(); - writer.flush(out, true); - // 关闭writer,释放内存 - writer.close(); - //此处记得关闭输出Servlet流 - IoUtil.close(out); + public AjaxResult export2(@Validated @RequestBody LogTimeThreeQueryParamVo queryVo) { + LoginUser loginUser = SecurityUtils.getLoginUser(); + logTimesacleHistoryThreeService.getExcellData(queryVo, 1, loginUser); + return AjaxResult.success(); } @ApiOperation("获取生产IOT系统参数模版列表") @@ -306,12 +226,19 @@ public class DcBusiHisReportController extends BaseController { return AjaxResult.success(logTimesacleHistoryThreeService.selectAreaElementCascaderByParentId(parentId)); } + @ApiOperation("天气报表") + @PreAuthorize("@ss.hasPermi('report:iot:export')") + @PostMapping("/queryWeather") + public AjaxResult queryWeather(@Validated @RequestBody ExportWeatherReq exportWeatherReq) { + return AjaxResult.success(logTimesacleHistoryThreeService.getWeatherReport(exportWeatherReq)); + } + @ApiOperation("天气报表") @PreAuthorize("@ss.hasPermi('report:iot:export')") @PostMapping("/exportWeather") - public void exportWeather(HttpServletResponse response, ExportWeatherReq exportWeatherReq) throws IOException { - List weatherReport = logTimesacleHistoryThreeService.getWeatherReport(exportWeatherReq); - com.lzbi.common.utils.poi.ExcelUtil util = new com.lzbi.common.utils.poi.ExcelUtil(WeatherReportVO.class); - util.exportExcel(response, weatherReport, "天气报表"); + public AjaxResult exportWeather(@Validated @RequestBody ExportWeatherReq exportWeatherReq) throws IOException { + LoginUser loginUser = SecurityUtils.getLoginUser(); + logTimesacleHistoryThreeService.exportWeather(exportWeatherReq, loginUser); + return AjaxResult.success(); } } \ No newline at end of file diff --git a/lzbi-module/src/main/java/com/lzbi/bi/domain/ExportWeatherReq.java b/lzbi-module/src/main/java/com/lzbi/bi/domain/ExportWeatherReq.java index 9cf9139..ae9de0a 100644 --- a/lzbi-module/src/main/java/com/lzbi/bi/domain/ExportWeatherReq.java +++ b/lzbi-module/src/main/java/com/lzbi/bi/domain/ExportWeatherReq.java @@ -2,6 +2,8 @@ package com.lzbi.bi.domain; import lombok.Data; +import javax.validation.constraints.NotBlank; + /** * @ClassName ExportWeatherReq * @Description 导出天气报表请求参数 @@ -13,13 +15,16 @@ public class ExportWeatherReq { /** * 省市区 */ + @NotBlank(message = "请选择地区") private String adcode; /** * 开始时间 */ + @NotBlank(message = "请选择开始时间") private String beginTime; /** * 结束时间 */ + @NotBlank(message = "请选择结束时间") private String endTime; } diff --git a/lzbi-module/src/main/java/com/lzbi/bi/domain/LogTimeThreeQueryParamVo.java b/lzbi-module/src/main/java/com/lzbi/bi/domain/LogTimeThreeQueryParamVo.java index de23dd6..7bdccc7 100644 --- a/lzbi-module/src/main/java/com/lzbi/bi/domain/LogTimeThreeQueryParamVo.java +++ b/lzbi-module/src/main/java/com/lzbi/bi/domain/LogTimeThreeQueryParamVo.java @@ -8,6 +8,7 @@ import lombok.NoArgsConstructor; import lombok.experimental.Accessors; import javax.validation.constraints.NotBlank; +import javax.validation.constraints.NotEmpty; import javax.validation.constraints.NotNull; import java.util.List; @@ -31,21 +32,13 @@ public class LogTimeThreeQueryParamVo { private String endTime; /** 中台设备UUID */ @ApiModelProperty(name = "中台设备UUID",notes = "") - @NotNull(message = "请选择设备") + @NotEmpty(message = "请选择设备") private List deviceUuids ; /** 参数列表 */ @ApiModelProperty(name = "参数列表",notes = "") private List queryParamCodes ; /** 参数模版列表 */ @ApiModelProperty(name = "参数模版列表",notes = "") - @NotNull(message = "请选择统计参数") + @NotEmpty(message = "请选择统计参数") private List queryParamClass ; - //private Integer pageNum; - // private Integer pageSize; - public String getBeginTime() { - return this.beginTime+" 00:00:00"; - } - public String getEndTime() { - return this.endTime+" 23:59:59"; - } } diff --git a/lzbi-module/src/main/java/com/lzbi/bi/mapper/LogTimeScaleThreeMapper.java b/lzbi-module/src/main/java/com/lzbi/bi/mapper/LogTimeScaleThreeMapper.java index ab5c6b9..88f4071 100644 --- a/lzbi-module/src/main/java/com/lzbi/bi/mapper/LogTimeScaleThreeMapper.java +++ b/lzbi-module/src/main/java/com/lzbi/bi/mapper/LogTimeScaleThreeMapper.java @@ -1,5 +1,6 @@ package com.lzbi.bi.mapper; +import com.baomidou.dynamic.datasource.annotation.DS; import com.baomidou.mybatisplus.annotation.InterceptorIgnore; import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.lzbi.bi.domain.*; @@ -10,7 +11,7 @@ import org.apache.ibatis.annotations.Param; import java.util.List; import java.util.Map; - +@DS("workDB") @InterceptorIgnore(tenantLine = "true") public interface LogTimeScaleThreeMapper extends BaseMapper { diff --git a/lzbi-module/src/main/java/com/lzbi/code/service/LogTimesacleHistoryThreeService.java b/lzbi-module/src/main/java/com/lzbi/code/service/LogTimesacleHistoryThreeService.java index 79333cd..55d3473 100644 --- a/lzbi-module/src/main/java/com/lzbi/code/service/LogTimesacleHistoryThreeService.java +++ b/lzbi-module/src/main/java/com/lzbi/code/service/LogTimesacleHistoryThreeService.java @@ -1,49 +1,261 @@ package com.lzbi.code.service; +import cn.hutool.core.date.DatePattern; +import cn.hutool.core.date.DateUtil; +import cn.hutool.core.io.IoUtil; +import cn.hutool.poi.excel.BigExcelWriter; +import cn.hutool.poi.excel.ExcelUtil; +import com.alibaba.fastjson2.JSONArray; 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.bi.domain.*; import com.lzbi.bi.mapper.LogTimeScaleThreeMapper; import com.lzbi.code.domain.WeatherReportVO; +import com.lzbi.common.constant.BizConstants; +import com.lzbi.common.core.domain.model.LoginUser; +import com.lzbi.common.utils.SecurityUtils; +import com.lzbi.report.domain.DcBusiReportRecord; +import com.lzbi.report.service.DcBusiReportRecordService; import lombok.extern.slf4j.Slf4j; -import org.apache.ibatis.annotations.MapKey; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.scheduling.annotation.Async; import org.springframework.stereotype.Service; +import org.springframework.util.CollectionUtils; -import java.util.List; -import java.util.Map; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.OutputStream; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.time.LocalDate; +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; +import java.util.*; +import java.util.stream.Collectors; -@DS("workDB") @Slf4j @Service public class LogTimesacleHistoryThreeService extends ServiceImpl implements IService { + @Value("${reportFilePath}") + private String reportFilePath; + + @Autowired + private DcBusiReportRecordService dcBusiReportRecordService; //@DataScopeCommon(deptAlias = "param") //public List selectVo(DeviceHistoryQueryVo queryVo){ - // return baseMapper.selectVo(queryVo); + // return baseMapper.selectVo(queryVo); //} - public List selectDetailByQuery(LogTimeThreeQueryParamVo queryVo){ - return baseMapper.selectDetailByQuery(queryVo); + public List selectDetailByQuery(LogTimeThreeQueryParamVo queryVo) { + queryVo.setDeviceUuids(null); + long time1 = System.currentTimeMillis(); + List logTimescaleHistoryThreeVos = baseMapper.selectDetailByQuery(queryVo); + long time2 = System.currentTimeMillis(); + log.info("查询耗时:{} s", (time2 - time1) / 1000); + return logTimescaleHistoryThreeVos; + } + + /** + * 按月份循环查询 + * + * @param queryVo + * @return + */ + public List selectDetailByQuery2(LogTimeThreeQueryParamVo queryVo) { + long time1 = System.currentTimeMillis(); + queryVo.setDeviceUuids(null); + List list = new ArrayList<>(); + DateTimeFormatter dtf = DateTimeFormatter.ofPattern(DatePattern.NORM_DATE_PATTERN); + String beginTimeStr = queryVo.getBeginTime(); + String endTimeStr = queryVo.getEndTime(); + for (LocalDate beginDate = LocalDate.parse(beginTimeStr, dtf), endDate = LocalDate.parse(endTimeStr, dtf); + beginDate.isBefore(endDate); + beginDate = beginDate.plusMonths(1)) { + queryVo.setBeginTime(beginDate.format(dtf)); + queryVo.setEndTime(beginDate.plusMonths(1).format(dtf)); + List logTimescaleHistoryThreeVos = baseMapper.selectDetailByQuery(queryVo); + if (!CollectionUtils.isEmpty(logTimescaleHistoryThreeVos)) { + list.addAll(logTimescaleHistoryThreeVos); + } + } + long time2 = System.currentTimeMillis(); + log.info("查询耗时:{} s", (time2 - time1) / 1000); + return list; + } + + //获取excel数据 1 excel设备参数为列模式 2 excel时间为列模式 3 浏览器设备参数为列模式 + //注意 excel有最大列数显示255 ,超出列数会报错 + @Async + public void getExcellData(LogTimeThreeQueryParamVo queryVo, int type, LoginUser loginUser) { + String fileName = "生产数据统计报表_"; + if (type == 1) { + fileName += "设备列格式_"; + } else { + fileName += "时间列格式_"; + } + fileName += LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss")); + fileName += ".xlsx"; + // 新增报表记录 + DcBusiReportRecord dcBusiReportRecord = saveReportRecord(fileName, loginUser); + try { + List> excelData; + // 设置头部数据 + DcDymicHeaderQueryVo dcDymicHeaderQueryVo = new DcDymicHeaderQueryVo(); + dcDymicHeaderQueryVo.setParamModels(queryVo.getQueryParamClass()); + dcDymicHeaderQueryVo.setDeviceUuids(queryVo.getDeviceUuids()); + List dcDymicReportHeaderVos = this.selectHeaderInfo(dcDymicHeaderQueryVo); + List collect = dcDymicReportHeaderVos.stream().map(DcDymicReportHeaderVo::getParamCode).collect(Collectors.toList()); + queryVo.setQueryParamCodes(collect); + List list = this.selectDetailByQuery2(queryVo); + if (type == 1) { + excelData = getMapListDevice(list, dcDymicReportHeaderVos); + } else { + excelData = getMapList(list, dcDymicReportHeaderVos); + } + toExcel(dcBusiReportRecord, excelData); + // 更新报表记录 + dcBusiReportRecord.setStatus(BizConstants.ReportRecordStatus.NOT_DOWNLOAD); + dcBusiReportRecordService.updateDcBusiReportRecord(dcBusiReportRecord); + } catch (IOException e) { + log.error("导出数据异常", e); + // 更新报表记录 + dcBusiReportRecord.setStatus(BizConstants.ReportRecordStatus.ERROR); + dcBusiReportRecordService.updateDcBusiReportRecord(dcBusiReportRecord); + } + } + + /** + * 新增报表记录 + * + * @param fileName 文件名称 + * @return + */ + private DcBusiReportRecord saveReportRecord(String fileName, LoginUser loginUser) { + DcBusiReportRecord record = new DcBusiReportRecord(); + record.setReportName(fileName); + record.setStatus(BizConstants.ReportRecordStatus.PREPARE_DATA); + record.setCreatedTime(new Date()); + record.setCreatedBy(loginUser.getUsername()); + record.setDeptId(loginUser.getDeptId()); + dcBusiReportRecordService.insertDcBusiReportRecord(record); + return record; + } + + ///按时间 设备+参数 形成数据--时间分组 + private List> getMapList(List list, List dcDymicReportHeaderVos) { + Map> collect = list.stream().collect(Collectors.groupingBy(LogTimescaleHistoryThreeVo::getTimeStampString)); + return collect.entrySet().stream().sorted(Map.Entry.comparingByKey()).map(entry -> { + Map map = new LinkedHashMap<>(); + map.put("统计时间", entry.getKey()); + entry.getValue().stream().sorted(Comparator.comparing(LogTimescaleHistoryThreeVo::getDeviceUuid).thenComparing(LogTimescaleHistoryThreeVo::getParamCode)).forEach(vo -> { + String key = vo.getDeviceUuid() + "_" + vo.getParamCode(); + Optional first = dcDymicReportHeaderVos.stream().filter(vos -> vos.getColCode().equals(key)).findFirst(); + if (first.isPresent()) { + //map.put(first.get().getColName1(), first.get().getColName2()); + map.put(first.get().getColName1() + '.' + first.get().getColName2(), vo.getParamValueNum()); + } + }); + return map; + }).collect(Collectors.toList()); + } + + /// 设备+参数 形成数据--设备-参数分组 + private List> getMapListDevice(List list, List dcDymicReportHeaderVos) { + Map> collect = list.stream().collect(Collectors.groupingBy(item -> item.getDeviceUuid() + "_" + item.getParamCode())); + List> collect1 = collect.entrySet().stream().map(entry -> { + Map map = new LinkedHashMap<>(); + entry.getValue().stream().sorted(Comparator.comparing(LogTimescaleHistoryThreeVo::getTimeStampString).thenComparing(LogTimescaleHistoryThreeVo::getParamCode)).forEach(vo -> { + String key = vo.getDeviceUuid() + "_" + vo.getParamCode(); + Optional first = dcDymicReportHeaderVos.stream().filter(vos -> vos.getColCode().equals(key)).findFirst(); + if (first.isPresent()) { + map.put("设备名称", first.get().getColName1()); + map.put("参数名称", first.get().getColName2()); + map.put(vo.getTimeStampString(), vo.getParamValueNum()); + } + }); + return map; + }).collect(Collectors.toList()); + log.info("collect1:{}", JSONArray.toJSONString(collect1)); + return collect1; + } + + private void toExcel(DcBusiReportRecord dcBusiReportRecord, List> mapList) throws IOException { + Path folder = Paths.get(reportFilePath); + if (!Files.exists(folder)) { + Files.createDirectories(folder); + } + OutputStream out = Files.newOutputStream(Paths.get(dcBusiReportRecord.getUrl())); + BigExcelWriter writer = ExcelUtil.getBigWriter(); + writer.write(mapList, true); + writer.flush(out, true); + // 关闭writer,释放内存 + writer.close(); + //此处记得关闭输出流 + IoUtil.close(out); } - public List selectHeaderInfo(DcDymicHeaderQueryVo queryVo){ + + public List selectHeaderInfo(DcDymicHeaderQueryVo queryVo) { return baseMapper.selectHeaderInfo(queryVo); } - public List> selectParamModels(){ + + public List> selectParamModels() { return baseMapper.selectParamModels(); } - public List> selectDevices(Long orgCode){ + public List> selectDevices(Long orgCode) { return baseMapper.selectDevices(orgCode); } - public List selectAreaElementCascaderByParentId(Integer parentId){ + public List selectAreaElementCascaderByParentId(Integer parentId) { return baseMapper.selectAreaElementCascaderByParentId(parentId); } - public List getWeatherReport(ExportWeatherReq exportWeatherReq){ + public List getWeatherReport(ExportWeatherReq exportWeatherReq) { return baseMapper.getWeatherReport(exportWeatherReq); } + + @Async + public void exportWeather(ExportWeatherReq exportWeatherReq, LoginUser loginUser) { + String fileName = "天气统计报表_"; + fileName += LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss")); + fileName += ".xlsx"; + // 新增报表记录 + DcBusiReportRecord dcBusiReportRecord = saveReportRecord(fileName, loginUser); + try { + List weatherReportList = baseMapper.getWeatherReport(exportWeatherReq); + List> excelData; + if (CollectionUtils.isEmpty(weatherReportList)) { + excelData = new ArrayList<>(); + Map map = new HashMap<>(); + map.put("时间", null); + map.put("地区", null); + map.put("天气", null); + map.put("温度", null); + excelData.add(map); + } else { + excelData = weatherReportList.stream().map(weatherReportVO -> { + Map map = new HashMap<>(); + map.put("时间", weatherReportVO.getTimestampString()); + map.put("地区", weatherReportVO.getCity()); + map.put("天气", weatherReportVO.getWeather()); + map.put("温度", weatherReportVO.getTemperature()); + return map; + }).collect(Collectors.toList()); + } + toExcel(dcBusiReportRecord, excelData); + // 更新报表记录 + dcBusiReportRecord.setStatus(BizConstants.ReportRecordStatus.NOT_DOWNLOAD); + dcBusiReportRecordService.updateDcBusiReportRecord(dcBusiReportRecord); + } catch (IOException e) { + log.error("导出数据异常", e); + // 更新报表记录 + dcBusiReportRecord.setStatus(BizConstants.ReportRecordStatus.ERROR); + dcBusiReportRecordService.updateDcBusiReportRecord(dcBusiReportRecord); + } + } } diff --git a/lzbi-module/src/main/java/com/lzbi/report/controller/DcBusiReportRecordController.java b/lzbi-module/src/main/java/com/lzbi/report/controller/DcBusiReportRecordController.java new file mode 100644 index 0000000..a3cc152 --- /dev/null +++ b/lzbi-module/src/main/java/com/lzbi/report/controller/DcBusiReportRecordController.java @@ -0,0 +1,143 @@ +package com.lzbi.report.controller; +import com.lzbi.bi.domain.ExportWeatherReq; +import com.lzbi.code.domain.WeatherReportVO; +import io.swagger.annotations.ApiImplicitParam; +import io.swagger.annotations.ApiImplicitParams; +import io.swagger.annotations.ApiOperation; + +import java.io.IOException; +import java.util.List; +import javax.servlet.http.HttpServletResponse; +import org.springframework.security.access.prepost.PreAuthorize; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.validation.annotation.Validated; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.PutMapping; +import org.springframework.web.bind.annotation.DeleteMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; +import com.lzbi.common.annotation.Log; +import com.lzbi.common.core.controller.BaseController; +import com.lzbi.common.core.domain.AjaxResult; +import com.lzbi.common.enums.BusinessType; +import com.lzbi.report.domain. DcBusiReportRecord; +import com.lzbi.report.service.DcBusiReportRecordService; +import com.lzbi.common.utils.poi.ExcelUtil; +import com.lzbi.common.core.page.TableDataInfo; + +/** + * 报表记录Controller + * + * @author enbo.li + * @date 2024-05-21 + */ +@RestController +@RequestMapping("/report/reportRecord") +public class DcBusiReportRecordController extends BaseController +{ + @Autowired + private DcBusiReportRecordService dcBusiReportRecordService; + + /** + * 查询报表记录列表 + */ + @ApiOperation("查询报表记录列表") + @ApiImplicitParams({ + @ApiImplicitParam(name = "DcBusiReportRecord", value = "", dataType = "DcBusiReportRecord", dataTypeClass = DcBusiReportRecord.class), + }) + @PreAuthorize("@ss.hasPermi('report:reportRecord:list')") + @GetMapping("/list") + public TableDataInfo list(DcBusiReportRecord DcBusiReportRecord) + { + startPage(); + List< DcBusiReportRecord> list = dcBusiReportRecordService.selectDcBusiReportRecordList(DcBusiReportRecord); + return getDataTable(list); + } + + /** + * 导出报表记录列表 + */ + @ApiOperation("导出报表记录列表") + @ApiImplicitParams({ + @ApiImplicitParam(name = "DcBusiReportRecord", value = "", dataType = "DcBusiReportRecord", dataTypeClass = DcBusiReportRecord.class), + }) + @PreAuthorize("@ss.hasPermi('report:reportRecord:export')") + @Log(title = "报表记录", businessType = BusinessType.EXPORT) + @PostMapping("/export") + public void export(HttpServletResponse response,DcBusiReportRecord DcBusiReportRecord) + { + List list = dcBusiReportRecordService.selectDcBusiReportRecordList(DcBusiReportRecord); + ExcelUtil util = new ExcelUtil(DcBusiReportRecord.class); + util.exportExcel(response, list, "报表记录数据"); + } + + /** + * 获取报表记录详细信息 + */ + @ApiOperation("获取报表记录详细信息") + @ApiImplicitParams({ + @ApiImplicitParam(name = "id", value = "", dataType = "Long", dataTypeClass = Long.class), + }) + @PreAuthorize("@ss.hasPermi('report:reportRecord:query')") + @GetMapping(value = "/{id}") + public AjaxResult getInfo(@PathVariable("id") Long id) + { + return success(dcBusiReportRecordService.selectDcBusiReportRecordById(id)); + } + + /** + * 新增报表记录 + */ + @ApiOperation("新增报表记录") + @ApiImplicitParams({ + @ApiImplicitParam(name = "DcBusiReportRecord", value = "", dataType = "DcBusiReportRecord", dataTypeClass = DcBusiReportRecord.class), + }) + @PreAuthorize("@ss.hasPermi('report:reportRecord:add')") + @Log(title = "报表记录", businessType = BusinessType.INSERT) + @PostMapping + public AjaxResult add(@RequestBody DcBusiReportRecord DcBusiReportRecord) + { + return toAjax(dcBusiReportRecordService.insertDcBusiReportRecord(DcBusiReportRecord)); + } + + /** + * 修改报表记录 + */ + + @ApiOperation("修改报表记录") + @ApiImplicitParams({ + @ApiImplicitParam(name = "DcBusiReportRecord", value = "", dataType = "DcBusiReportRecord", dataTypeClass = DcBusiReportRecord.class), + }) + @PreAuthorize("@ss.hasPermi('report:reportRecord:edit')") + @Log(title = "报表记录", businessType = BusinessType.UPDATE) + @PutMapping + public AjaxResult edit(@RequestBody DcBusiReportRecord DcBusiReportRecord) + { + return toAjax(dcBusiReportRecordService.updateDcBusiReportRecord(DcBusiReportRecord)); + } + + /** + * 删除报表记录 + */ + @ApiOperation("删除报表记录") + @ApiImplicitParams({ + @ApiImplicitParam(name = "ids", value = "", dataType = "Long", dataTypeClass =Long.class), + }) + @PreAuthorize("@ss.hasPermi('report:reportRecord:remove')") + @Log(title = "报表记录", businessType = BusinessType.DELETE) + @DeleteMapping("/{ids}") + public AjaxResult remove(@PathVariable Long[] ids) + { + return toAjax(dcBusiReportRecordService.deleteDcBusiReportRecordByIds(ids)); + } + + @ApiOperation("天气报表") + @PreAuthorize("@ss.hasPermi('report:reportRecord:download')") + @PostMapping("/download/{id}") + public void downloadFile(HttpServletResponse response, @PathVariable("id") Long id) throws IOException { + dcBusiReportRecordService.downloadFile(response, id); + } +} diff --git a/lzbi-module/src/main/java/com/lzbi/report/domain/DcBusiReportRecord.java b/lzbi-module/src/main/java/com/lzbi/report/domain/DcBusiReportRecord.java new file mode 100644 index 0000000..90d3e4f --- /dev/null +++ b/lzbi-module/src/main/java/com/lzbi/report/domain/DcBusiReportRecord.java @@ -0,0 +1,73 @@ +package com.lzbi.report.domain; + +import com.baomidou.mybatisplus.annotation.IdType; +import com.baomidou.mybatisplus.annotation.TableField; +import com.baomidou.mybatisplus.annotation.TableId; +import com.fasterxml.jackson.annotation.JsonFormat; +import com.lzbi.common.annotation.Excel; +import com.lzbi.module.base.BaseModuleEntity; +import io.swagger.annotations.ApiModelProperty; +import lombok.Data; +import lombok.experimental.Accessors; +import org.springframework.format.annotation.DateTimeFormat; + +import java.util.Date; + +/** + * 报表记录对象 dc_busi_report_record + * + * @author enbo.li + * @date 2024-05-21 + */ +@Data +@Accessors(chain = true) +public class DcBusiReportRecord extends BaseModuleEntity +{ + private static final long serialVersionUID = 1L; + + /** 主键 */ + @TableId(type = IdType.AUTO) + private Long id; + + /** 报表名称 */ + @Excel(name = "报表名称") + @ApiModelProperty(name = "报表名称",notes = "reportName") + private String reportName; + + /** 文件路径 */ + @Excel(name = "文件路径") + @ApiModelProperty(name = "文件路径",notes = "url") + private String url; + + /** 状态 0准备数据 1未下载 2已下载 */ + @Excel(name = "状态 0准备数据 1未下载 2已下载") + @ApiModelProperty(name = "状态 0准备数据 1未下载 2已下载",notes = "status") + private String status; + + /** 部门ID */ + @Excel(name = "部门ID") + @ApiModelProperty(name = "部门ID",notes = "deptId") + private Long deptId; + + /** 备注 */ + @Excel(name = "备注") + @ApiModelProperty(name = "备注",notes = "remark") + private String remark; + + /** 部门名称 */ + @TableField(exist = false) + private String deptName; + + /** 用户姓名 */ + @TableField(exist = false) + private String nickName; + + /** + * 创建时间 + */ + @ApiModelProperty(name = "创建时间", notes = "") + @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") + @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss") + private Date createdTime; + +} diff --git a/lzbi-module/src/main/java/com/lzbi/report/mapper/DcBusiReportRecordMapper.java b/lzbi-module/src/main/java/com/lzbi/report/mapper/DcBusiReportRecordMapper.java new file mode 100644 index 0000000..ecbc419 --- /dev/null +++ b/lzbi-module/src/main/java/com/lzbi/report/mapper/DcBusiReportRecordMapper.java @@ -0,0 +1,63 @@ +package com.lzbi.report.mapper; + +import java.util.List; +import com.lzbi.report.domain.DcBusiReportRecord; +import com.baomidou.mybatisplus.core.mapper.BaseMapper; + +/** + * 报表记录Mapper接口 + * + * @author enbo.li + * @date 2024-05-21 + */ + +public interface DcBusiReportRecordMapper extends BaseMapper +{ + /** + * 查询报表记录 + * + * @param id 报表记录主键 + * @return 报表记录 + */ + public DcBusiReportRecord selectDcBusiReportRecordById(Long id); + + /** + * 查询报表记录列表 + * + * @param dcBusiReportRecord 报表记录 + * @return 报表记录集合 + */ + public List selectDcBusiReportRecordList(DcBusiReportRecord dcBusiReportRecord); + + /** + * 新增报表记录 + * + * @param dcBusiReportRecord 报表记录 + * @return 结果 + */ + public int insertDcBusiReportRecord(DcBusiReportRecord dcBusiReportRecord); + + /** + * 修改报表记录 + * + * @param dcBusiReportRecord 报表记录 + * @return 结果 + */ + public int updateDcBusiReportRecord(DcBusiReportRecord dcBusiReportRecord); + + /** + * 删除报表记录 + * + * @param id 报表记录主键 + * @return 结果 + */ + public int deleteDcBusiReportRecordById(Long id); + + /** + * 批量删除报表记录 + * + * @param ids 需要删除的数据主键集合 + * @return 结果 + */ + public int deleteDcBusiReportRecordByIds(Long[] ids); +} diff --git a/lzbi-module/src/main/java/com/lzbi/report/service/DcBusiReportRecordService.java b/lzbi-module/src/main/java/com/lzbi/report/service/DcBusiReportRecordService.java new file mode 100644 index 0000000..6230443 --- /dev/null +++ b/lzbi-module/src/main/java/com/lzbi/report/service/DcBusiReportRecordService.java @@ -0,0 +1,146 @@ +package com.lzbi.report.service; + +import java.io.*; +import java.nio.file.Files; +import java.util.Arrays; +import java.util.List; + +import cn.hutool.core.lang.Snowflake; +import cn.hutool.core.util.IdUtil; +import com.lzbi.common.constant.BizConstants; +import org.apache.commons.compress.utils.IOUtils; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Service; +import com.lzbi.report.domain.DcBusiReportRecord; +import com.lzbi.report.mapper.DcBusiReportRecordMapper; +import com.baomidou.mybatisplus.extension.service.IService; +import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; +import org.springframework.transaction.annotation.Transactional; +import org.springframework.util.CollectionUtils; + +import javax.servlet.http.HttpServletResponse; + +/** + * 报表记录Service业务层处理 + * + * @author enbo.li + * @date 2024-05-21 + */ +@Service +public class DcBusiReportRecordService extends ServiceImpl implements IService +{ + + // 雪花算法 + private static final Snowflake snowflake = IdUtil.getSnowflake(1, 3); + + @Value("${reportFilePath}") + private String reportFilePath; + + /** + * 查询报表记录 + * + * @param id 报表记录主键 + * @return 报表记录 + */ + public DcBusiReportRecord selectDcBusiReportRecordById(Long id) + { + return baseMapper.selectDcBusiReportRecordById(id); + } + + /** + * 查询报表记录列表 + * + * @param dcBusiReportRecord 报表记录 + * @return 报表记录 + */ + public List selectDcBusiReportRecordList(DcBusiReportRecord dcBusiReportRecord) + { + return baseMapper.selectDcBusiReportRecordList(dcBusiReportRecord); + } + + /** + * 新增报表记录 + * + * @param dcBusiReportRecord 报表记录 + * @return 结果 + */ + + public int insertDcBusiReportRecord(DcBusiReportRecord dcBusiReportRecord) + { + String url = reportFilePath + "/" + snowflake.nextId(); + dcBusiReportRecord.setUrl(url); + return baseMapper.insert(dcBusiReportRecord); + } + + /** + * 修改报表记录 + * + * @param dcBusiReportRecord 报表记录 + * @return 结果 + */ + + public int updateDcBusiReportRecord(DcBusiReportRecord dcBusiReportRecord) + { + return baseMapper.updateDcBusiReportRecord(dcBusiReportRecord); + } + + /** + * 批量删除报表记录 + * + * @param ids 需要删除的报表记录主键 + * @return 结果 + */ + @Transactional(rollbackFor = Exception.class) + public int deleteDcBusiReportRecordByIds(Long[] ids) + { + List dcBusiReportRecords = baseMapper.selectBatchIds(Arrays.asList(ids)); + if (!CollectionUtils.isEmpty(dcBusiReportRecords)) { + dcBusiReportRecords.forEach(reportRecord -> { + String url = reportRecord.getUrl(); + File file = new File(url); + if (file.exists()) { + file.delete(); + } + }); + } + return baseMapper.deleteDcBusiReportRecordByIds(ids); + } + + /** + * 删除报表记录信息 + * + * @param id 报表记录主键 + * @return 结果 + */ + @Transactional(rollbackFor = Exception.class) + public int deleteDcBusiReportRecordById(Long id) + { + DcBusiReportRecord reportRecord = selectDcBusiReportRecordById(id); + String url = reportRecord.getUrl(); + File file = new File(url); + if (file.exists()) { + file.delete(); + } + return baseMapper.deleteDcBusiReportRecordById(id); + } + + public void downloadFile(HttpServletResponse response, Long id) { + DcBusiReportRecord reportRecord = selectDcBusiReportRecordById(id); + String url = reportRecord.getUrl(); + File file = new File(url); + if (!file.exists()) { + return; + } + response.setContentType("application/octet-stream"); + response.setCharacterEncoding("utf-8"); + response.setHeader("Content-Disposition", "attachment;fileName=" + file.getName()); + try (InputStream inputStream = Files.newInputStream(file.toPath())) { + IOUtils.copy(inputStream, response.getOutputStream()); + } catch (IOException e) { + throw new RuntimeException(e); + } + // 更新状态 + reportRecord.setStatus(BizConstants.ReportRecordStatus.DOWNLOADED); + updateDcBusiReportRecord(reportRecord); + } +} diff --git a/lzbi-module/src/main/resources/mapper/LogTimeScaleThreeMapper.xml b/lzbi-module/src/main/resources/mapper/LogTimeScaleThreeMapper.xml index d0a26ad..ea72a3c 100644 --- a/lzbi-module/src/main/resources/mapper/LogTimeScaleThreeMapper.xml +++ b/lzbi-module/src/main/resources/mapper/LogTimeScaleThreeMapper.xml @@ -41,9 +41,8 @@ param_value_str, log_srctag, log_srctype from dc_base_log_history_level3 - where timestamp_key BETWEEN to_timestamp(#{beginTime},'yyyy-MM-dd hh24:mi:ss') - and to_timestamp(#{endTime},'yyyy-MM-dd hh24:mi:ss') - and param_type='double' + where + param_type='double' and param_code in #{code} @@ -55,7 +54,9 @@ #{uuid} - order by timestamp_key desc + and timestamp_key >= to_timestamp(#{beginTime},'yyyy-MM-dd') + and timestamp_key < to_timestamp(#{endTime},'yyyy-MM-dd') + order by timestamp_key asc diff --git a/lzbi-module/src/main/resources/mapper/report/DcBusiReportRecordMapper.xml b/lzbi-module/src/main/resources/mapper/report/DcBusiReportRecordMapper.xml new file mode 100644 index 0000000..229827f --- /dev/null +++ b/lzbi-module/src/main/resources/mapper/report/DcBusiReportRecordMapper.xml @@ -0,0 +1,118 @@ + + + + + + + + + + + + + + + + + + + + + select id, report_name, url, status, dept_id, created_by, created_time, updated_by, updated_time, remark from dc_busi_report_record + + + + + + + + insert into dc_busi_report_record + + id, + report_name, + url, + status, + dept_id, + created_by, + created_time, + updated_by, + updated_time, + remark, + + + #{id}, + #{reportName}, + #{url}, + #{status}, + #{deptId}, + #{createdBy}, + #{createdTime}, + #{updatedBy}, + #{updatedTime}, + #{remark}, + + + + + update dc_busi_report_record + + report_name = #{reportName}, + url = #{url}, + status = #{status}, + dept_id = #{deptId}, + created_by = #{createdBy}, + created_time = #{createdTime}, + updated_by = #{updatedBy}, + updated_time = #{updatedTime}, + remark = #{remark}, + + where id = #{id} + + + + delete from dc_busi_report_record where id = #{id} + + + + delete from dc_busi_report_record where id in + + #{id} + + + \ No newline at end of file diff --git a/lzbi-system/src/main/java/com/lzbi/system/mapper/SysDeptMapper.java b/lzbi-system/src/main/java/com/lzbi/system/mapper/SysDeptMapper.java index de0912a..9281005 100644 --- a/lzbi-system/src/main/java/com/lzbi/system/mapper/SysDeptMapper.java +++ b/lzbi-system/src/main/java/com/lzbi/system/mapper/SysDeptMapper.java @@ -1,7 +1,10 @@ package com.lzbi.system.mapper; +import java.util.Collection; import java.util.HashMap; import java.util.List; +import java.util.Set; + import org.apache.ibatis.annotations.Param; import com.lzbi.common.core.domain.entity.SysDept; @@ -123,5 +126,5 @@ public interface SysDeptMapper List selectDeptListByRoleIds(List ids); - List getDeptListByDeptIdList(@Param("deptIdList") List deptIdList); + List selectBatchIds(@Param("ids") Collection ids); } diff --git a/lzbi-system/src/main/java/com/lzbi/system/service/impl/SysDeptServiceImpl.java b/lzbi-system/src/main/java/com/lzbi/system/service/impl/SysDeptServiceImpl.java index f4f929d..22358cd 100644 --- a/lzbi-system/src/main/java/com/lzbi/system/service/impl/SysDeptServiceImpl.java +++ b/lzbi-system/src/main/java/com/lzbi/system/service/impl/SysDeptServiceImpl.java @@ -23,6 +23,7 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.*; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +import org.springframework.util.CollectionUtils; import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.MultiValueMap; import org.springframework.web.client.RestTemplate; @@ -72,7 +73,7 @@ public class SysDeptServiceImpl implements ISysDeptService { @Override public List getDeptListByDeptIdList(List deptIdList) { - return deptMapper.getDeptListByDeptIdList(deptIdList); + return deptMapper.selectBatchIds(deptIdList); } /** @@ -84,6 +85,20 @@ public class SysDeptServiceImpl implements ISysDeptService { @Override public List selectDeptTreeList(SysDept dept) { List depts = SpringUtils.getAopProxy(this).selectDeptList(dept); + if (CollectionUtils.isEmpty(depts)) { + return new ArrayList<>(); + } + String orgType = dept.getOrgType(); + if (StringUtils.isNotBlank(orgType)) { + List ancestorsList = depts.stream().map(SysDept::getAncestors).collect(Collectors.toList()); + String ancestorsJoinStr = StringUtils.join(ancestorsList, ","); + String[] deptIds = ancestorsJoinStr.split(","); + Set deptIdSet = Arrays.stream(deptIds).map(Long::valueOf).collect(Collectors.toSet()); + List sysDepts = deptMapper.selectBatchIds(deptIdSet); + if (!CollectionUtils.isEmpty(sysDepts)) { + depts.addAll(sysDepts); + } + } return buildDeptTreeSelect(depts); } diff --git a/lzbi-system/src/main/resources/mapper/system/SysDeptMapper.xml b/lzbi-system/src/main/resources/mapper/system/SysDeptMapper.xml index 4380064..8988d27 100644 --- a/lzbi-system/src/main/resources/mapper/system/SysDeptMapper.xml +++ b/lzbi-system/src/main/resources/mapper/system/SysDeptMapper.xml @@ -125,7 +125,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" order by d.parent_id, d.order_num - select dept_id, parent_id, @@ -145,7 +145,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" short_name, org_type from sys_dept where dept_id in - + #{deptId}