| | |
| | | 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 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 javax.servlet.ServletOutputStream; |
| | | import javax.servlet.http.HttpServletRequest; |
| | | import javax.servlet.http.HttpServletResponse; |
| | | import java.io.File; |
| | | import java.sql.Wrapper; |
| | | import java.time.LocalDate; |
| | | import java.io.FileInputStream; |
| | | import java.io.IOException; |
| | | import java.time.LocalDateTime; |
| | | import java.time.format.DateTimeFormatter; |
| | | import java.util.*; |
| | | import java.util.ArrayList; |
| | | import java.util.List; |
| | | |
| | | /** |
| | | * @author chenjiantian |
| | |
| | | } |
| | | |
| | | @SneakyThrows |
| | | public String exportList(int pageNo, int pageSize, String keyword, String deviceCode) { |
| | | public String exportList(HttpServletRequest request, HttpServletResponse response, String deviceCode) { |
| | | |
| | | if (SecurityUtils.getClientId() != null) { |
| | | PoleBinding one = SpringContextHolder.getBean(PoleBindingService.class).getOne(Wrappers.lambdaQuery(PoleBinding.class).eq(PoleBinding::getDeviceCode, deviceCode) |
| | |
| | | } |
| | | } |
| | | |
| | | PageHelper.startPage(pageNo, pageSize); |
| | | List<LightReportDataBo> list = baseMapper.listReportData(keyword, deviceCode); |
| | | List<LightReportDataBo> list = baseMapper.listReportData(null, deviceCode); |
| | | |
| | | 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); |
| | | // |
| | | // ExcelWriter excelWriter = EasyExcel.write(file).withTemplate(cpr.getInputStream()).build(); |
| | | // WriteSheet writeSheet = EasyExcel.writerSheet().build(); |
| | | // excelWriter.fill(list, writeSheet); |
| | | // excelWriter.finish(); |
| | | |
| | | EasyExcel.write(file, LightReportDataBo.class).sheet("模版").doWrite(list); |
| | | |
| | | FileUploadDto fileUploadDto = fileService.uploadFile(file); |
| | | // String url = file.getPath().replace(properties.getUploadRootPath(), ""); |
| | | String url = fileUploadDto.getFileUrl(); |
| | | //文件流化返回给前端 |
| | | findfile(request, response, file); |
| | | //删除文件 |
| | | file.delete(); |
| | | return url; |
| | | |
| | | // return list; |
| | | return null; |
| | | } |
| | | |
| | | public List<LightReportDataBo> reportDataList(List<String> deviceCodeList, DateTime startDate, DateTime endDate) { |
| | | return baseMapper.reportDataList(deviceCodeList, startDate, endDate); |
| | | /** |
| | | * 文件流化 |
| | | * |
| | | * @param request |
| | | * @param response |
| | | * @param file |
| | | * @throws IOException |
| | | */ |
| | | public void findfile(HttpServletRequest request, HttpServletResponse response, File file) throws IOException { |
| | | ServletOutputStream out = null; |
| | | FileInputStream ips = null; |
| | | try { |
| | | //获取文件存放的路径 |
| | | String fileName = file.getName(); |
| | | //获取到文字 数据库里对应的附件名字加上老的文件名字:filename 截取到后面的文件类型 例:txt 组成一个新的文件名字:newFileName |
| | | //生成3个随机数,用来生成新的文件名字 |
| | | String newFileName = "单灯数据" + RandomUtil.randomString(3) + ".xlsx"; |
| | | if (!file.exists()) { |
| | | //如果文件不存在就跳出 |
| | | return; |
| | | } |
| | | ips = new FileInputStream(file); |
| | | response.setContentType("multipart/form-data"); |
| | | //为文件重新设置名字,采用数据库内存储的文件名称 |
| | | response.addHeader("Content-Disposition", "attachment; filename=\"" + new String(newFileName.getBytes("UTF-8"), "ISO8859-1") + "\""); |
| | | out = response.getOutputStream(); |
| | | //读取文件流 |
| | | int len = 0; |
| | | byte[] buffer = new byte[1024 * 10]; |
| | | while ((len = ips.read(buffer)) != -1) { |
| | | out.write(buffer, 0, len); |
| | | } |
| | | out.flush(); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } finally { |
| | | try { |
| | | out.close(); |
| | | ips.close(); |
| | | } catch (IOException e) { |
| | | System.out.println("关闭流出现异常"); |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | } |
| | | } |