package com.yssh.service; import com.yssh.entity.xls.DayExcel; import com.yssh.entity.xls.MonthExcel; import com.yssh.entity.xls.WeekExcel; import com.yssh.mapper.XlsReportMapper; import com.yssh.utils.DateUtils; import com.yssh.utils.ExcelUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Value; import org.springframework.core.io.ClassPathResource; import org.springframework.stereotype.Service; import javax.annotation.Resource; import java.io.File; import java.io.IOException; import java.util.*; /** * Excel导出服务类 * @author WWW * @author 2023-08-05 */ @Service public class XlsReportService { protected final Logger logger = LoggerFactory.getLogger(this.getClass()); @Resource XlsReportMapper mapper; /** * 报告路径 */ @Value("${report.path}") private String reportPath; /** * 预警值 */ @Value("${report.yjz}") private double yjz; /** * 报警值 */ @Value("${report.bjz}") private double bjz; /** * Excel是否已存在 */ private boolean xlsExists(String type, String name) { int rows = mapper.reportExists(type, name); return rows > 0; } /** * 获取导出路径 */ private String getExpPath(String type) { String path = reportPath + File.separator + type; File f = new File(path); if (!f.exists() || !f.isDirectory()) { f.mkdirs(); } return path; } /** * 获取Excel模板 */ private String getXslTemplate(String type) throws IOException { ClassPathResource resource = new ClassPathResource(String.format("templates/%s.xlsx", type)); return resource.exists() ? resource.getFile().getPath() : null; } /** * 创建Excel */ private void createExcel(String source, String target, List list) { Map> map = new HashMap<>(); map.put("data", list); ExcelUtils.writeToTemplate(source, target, map); } /** * 创建Excel */ private String createExcel(String type, Date date, List list) throws Exception { String source = getXslTemplate(type); String strData = DateUtils.parseDateToStr("month".equals(type) ? DateUtils.YYYYMM : DateUtils.YYYYMMDD, date); String target = String.format("%s\\%s.xlsx", getExpPath(type), strData); createExcel(source, target, list); return String.format("%s\\%s.xlsx", type, strData); } /** * 创建日报 */ public void createDayReport(Date date) { try { Date yesterday = DateUtils.getAPeriodOfTime(date, -1, Calendar.DATE); List list = new ArrayList<>(); for (int i = 1; i < 47; i++) { DayExcel day = new DayExcel("AI-" + (i < 10 ? "0" : "") + i, "lj", "ljtb", "ys", "3m/s", "东南", "°c"); list.add(day); } String filePath = createExcel("day", yesterday, list); } catch (Exception ex) { logger.error(ex.getMessage(), ex); } } /** * 创建周报 */ public void createWeekReport(Date date) { try { Date start = DateUtils.getAPeriodOfTime(date, -7, Calendar.DATE); Date end = DateUtils.getAPeriodOfTime(date, -1, Calendar.DATE); List list = new ArrayList<>(); for (int i = 1; i < 47; i++) { WeekExcel day = new WeekExcel("AI-" + (i < 10 ? "0" : "") + i, "zhb", "ztq", "ztb", "zhb2", "lj", "ljtb", "syn", "ys", "fs", "fx", "°c"); list.add(day); } String filePath = createExcel("week", end, list); } catch (Exception ex) { logger.error(ex.getMessage(), ex); } } /** * 创建月报 */ public void createMonthReport(Date date) { try { Date yesterday = DateUtils.getAPeriodOfTime(date, -1, Calendar.DATE); Date start = DateUtils.getMonthStart(yesterday); Date end = DateUtils.getMonthEnd(yesterday); List list = new ArrayList<>(); for (int i = 1; i < 47; i++) { MonthExcel day = new MonthExcel("AI-" + (i < 10 ? "0" : "") + i, "yhb", "ytq", "ytb", "yhb2", "lj", "ljtb", "qyn", "ys", "fs", "fx", "°c"); list.add(day); } String filePath = createExcel("month", end, list); } catch (Exception ex) { logger.error(ex.getMessage(), ex); } } }