燕山石化溯源三维电子沙盘-【后端】-服务
1
13693261870
2024-11-13 2f55cebbad3dea187a5f91d16ec80a9677dab699
src/main/java/com/yssh/service/XlsReportService.java
@@ -10,6 +10,7 @@
import com.yssh.utils.CalculateUtils;
import com.yssh.utils.DateUtils;
import com.yssh.utils.ExcelUtils;
import com.yssh.utils.WebUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
@@ -17,15 +18,11 @@
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
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());
@@ -55,8 +52,11 @@
    private double bjz;
    /**
     * 获取导出路径
     * 报警值
     */
    @Value("${report.templates}")
    private String templates;
    private String getExpPath(String type) {
        String path = reportPath + File.separator + type;
@@ -68,18 +68,13 @@
        return path;
    }
    /**
     * 获取Excel模板
     */
    private String getXslTemplate(String type) throws IOException {
        ClassPathResource resource = new ClassPathResource(String.format("templates/%s.xlsx", type));
        //ClassPathResource resource = new ClassPathResource(String.format("templates/%s.xlsx", type));
        //return resource.exists() ? resource.getFile().getPath() : null;
        return resource.exists() ? resource.getFile().getPath() : null;
        return String.format(templates + File.separator + type + ".xlsx");
    }
    /**
     * 创建Excel
     */
    private <T> void createExcel(String source, String target, List<T> list) {
        Map<String, List<T>> map = new HashMap<>();
        map.put("data", list);
@@ -87,32 +82,48 @@
        ExcelUtils.writeToTemplate(source, target, map);
    }
    /**
     * 创建Excel
     */
    private String getStrDate(String type, Date date) {
        return DateUtils.parseDateToStr("month".equals(type) ? DateUtils.YYYYMM : DateUtils.YYYYMMDD, date);
    }
    private String getTarget(String type, Date date) {
        String strData = getStrDate(type, date);
        return String.format("%s\\%s.xlsx", getExpPath(type), strData);
    }
    private boolean xlsExists(String type, Date date) {
        String target = getTarget(type, date);
        File f = new File(target);
        return f.exists() && !f.isDirectory();
    }
    private <T> String createExcel(String type, Date date, List<T> 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);
        String strData = getStrDate(type, date);
        String target = getTarget(type, date);
        File f = new File(target);
        if (!f.exists() || f.isDirectory()) {
        if (!xlsExists(type, date)) {
            createExcel(source, target, list);
        }
        return String.format("%s\\%s.xlsx", type, strData);
    }
    /**
     * 保存结果
     */
    private <T> void saveResult(String type, Date date, List<T> list) throws Exception {
        date = DateUtils.trimTime(date);
        String filePath = createExcel(type, date, list);
        String name = filePath.replace(type + "\\", "");
    private boolean recordExists(String type, Date date) {
        String name = String.format("%s.xlsx", getStrDate(type, date));
        int rows = mapper.reportExists(type, name);
        if (rows > 0) return;
        return rows > 0;
    }
    private <T> void saveResult(String type, Date date, List<T> list) throws Exception {
        String filePath = createExcel(type, date, list);
        if (recordExists(type, date)) return;
        String name = filePath.replace(type + "\\", "");
        date = DateUtils.trimTime(date);
        XlsReport xls = new XlsReport();
        xls.setName(name);
@@ -123,9 +134,6 @@
        mapper.insertReport(xls);
    }
    /**
     * 获取受影响因素及原因
     */
    public String getYs(List<SuYuan700> suList) {
        if (null == suList || 0 == suList.size()) return "";
@@ -151,10 +159,12 @@
     */
    public void createDayReport(Date date) {
        try {
            String type = "day";
            Date yesterday = DateUtils.getAPeriodOfTime(date, -1, Calendar.DATE); // 2023-07-24
            if (xlsExists(type, yesterday) && recordExists(type, yesterday)) return;
            String yyyy = DateUtils.parseDateToStr(DateUtils.YYYY, yesterday); // 2023
            String yyyymmdd = DateUtils.parseDateToStr(DateUtils.YYYYMMDD, yesterday); // 20230724
            // String yyyy_mm_dd = DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD, yesterday); // 2023-07-24
            Integer yearStart = Integer.parseInt(yyyy + "010100"); // 2023010100
            Integer dayStart = Integer.parseInt(yyyymmdd + "00"); // 2023072400
            Integer end = Integer.parseInt(yyyymmdd + "23"); // 2023072423
@@ -179,7 +189,7 @@
                list.add(new DayExcel("" + rjz, "" + lj, ljtb, ys, "", "", ""));
            }
            saveResult("day", yesterday, list);
            saveResult(type, yesterday, list);
        } catch (Exception ex) {
            logger.error(ex.getMessage(), ex);
        }
@@ -190,8 +200,11 @@
     */
    public void createWeekReport(Date date) {
        try {
            String type = "week";
            Date mon = DateUtils.getAPeriodOfTime(date, -7, Calendar.DATE); // 2023-07-24
            Date sun = DateUtils.getAPeriodOfTime(date, -1, Calendar.DATE); // 2023-07-30
            if (xlsExists(type, sun) && recordExists(type, sun)) return;
            Date lastMon = DateUtils.getAPeriodOfTime(date, -14, Calendar.DATE); // 2023-07-17
            Date lastSun = DateUtils.getAPeriodOfTime(date, -8, Calendar.DATE); // 2023-07-23
            Date lastYear1 = DateUtils.getAPeriodOfTime(mon, -1, Calendar.YEAR); // 2022-07-24
@@ -240,7 +253,7 @@
                list.add(new WeekExcel("" + sz, zhb, ztq, ztb, "" + lj, ljtb, syn, ys, "", "", ""));
            }
            saveResult("week", sun, list);
            saveResult(type, sun, list);
        } catch (Exception ex) {
            logger.error(ex.getMessage(), ex);
        }
@@ -251,9 +264,12 @@
     */
    public void createMonthReport(Date date) {
        try {
            String type = "month";
            Date yesterday = DateUtils.getAPeriodOfTime(date, -1, Calendar.DATE); // 2023-07-31
            Date monthStart = DateUtils.getMonthStart(yesterday); // 2023-07-01
            Date monthEnd = DateUtils.getMonthEnd(yesterday); // 2023-07-31
            if (xlsExists(type, monthEnd) && recordExists(type, monthEnd)) return;
            int intMonthStart = Integer.parseInt(DateUtils.parseDateToStr(DateUtils.YYYYMMDD, monthStart) + "00"); // 2023070100
            int intMonthEnd = Integer.parseInt(DateUtils.parseDateToStr(DateUtils.YYYYMMDD, monthEnd) + "23"); // 2023073123
            Date lastMonth = DateUtils.getAPeriodOfTime(yesterday, -1, Calendar.MONTH); // 2023-06-30
@@ -279,7 +295,7 @@
                double qntq = mapper.selectAccumulate(lastYearMonthStart, lastYearMonthEnd, name); // 去年同期
                String ytq = 0 == qntq ? "--" : CalculateUtils.round2((sy - qntq) / qntq * 100) + "%"; // 月同期
                String ytb = ""; // 月同比
                String ytb = 0 == qntq ? "--" : CalculateUtils.round2((sy - qntq) / qntq * 100) + "%"; // 月同比
                double lj = mapper.selectAccumulate(yearStart, intMonthEnd, name); // 累计
@@ -296,7 +312,22 @@
                list.add(new MonthExcel("" + sy, yhb, ytq, ytb, "" + lj, ljtb, qyn, ys, "", "", ""));
            }
            //saveResult("month", end, list);
            saveResult(type, monthEnd, list);
        } catch (Exception ex) {
            logger.error(ex.getMessage(), ex);
        }
    }
    public void downloadById(Integer id, HttpServletResponse res) {
        XlsReport xlsReport = mapper.selectById(id);
        if (null == xlsReport) return;
        String file = reportPath + File.separator + xlsReport.getPath();
        File f = new File(file);
        if (!f.exists() || f.isDirectory()) return;
        try {
            WebUtils.download(file, xlsReport.getName(), false, res);
        } catch (Exception ex) {
            logger.error(ex.getMessage(), ex);
        }