|
@@ -1,28 +1,38 @@
|
|
|
package com.hjy.module.service.template.impl;
|
|
|
|
|
|
import cn.hutool.core.util.NumberUtil;
|
|
|
+import cn.hutool.core.util.ReflectUtil;
|
|
|
import cn.hutool.core.util.StrUtil;
|
|
|
+import cn.hutool.json.JSONUtil;
|
|
|
import cn.hutool.poi.excel.ExcelReader;
|
|
|
import cn.hutool.poi.excel.ExcelUtil;
|
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
+import com.hjy.common.config.ZhbsqConfig;
|
|
|
import com.hjy.common.core.page.TableDataInfo;
|
|
|
import com.hjy.common.utils.PageUtils;
|
|
|
+import com.hjy.common.utils.file.FileUploadUtils;
|
|
|
+import com.hjy.common.utils.file.FileUtils;
|
|
|
+import com.hjy.module.domain.report.ReportDTO;
|
|
|
import com.hjy.module.domain.report.ReportRecord;
|
|
|
+import com.hjy.module.domain.report.ReportWord;
|
|
|
import com.hjy.module.domain.reportsetting.ReportSetting;
|
|
|
import com.hjy.module.domain.template.ExcelOutInTotal;
|
|
|
import com.hjy.module.mapper.template.ExcelOutInTotalMapper;
|
|
|
import com.hjy.module.service.report.IReportRecordService;
|
|
|
+import com.hjy.module.service.report.IReportWordService;
|
|
|
import com.hjy.module.service.reportsetting.IReportSettingService;
|
|
|
import com.hjy.module.service.template.IExcelOutInTotalService;
|
|
|
import com.hjy.module.vo.report.ReportRecordVo;
|
|
|
import com.hjy.module.vo.template.ExcelOutInTotalVo;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.apache.poi.xwpf.usermodel.*;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
-import java.io.IOException;
|
|
|
-import java.io.InputStream;
|
|
|
+import java.io.*;
|
|
|
+import java.lang.reflect.Field;
|
|
|
import java.math.BigDecimal;
|
|
|
import java.math.RoundingMode;
|
|
|
import java.time.LocalDate;
|
|
@@ -45,6 +55,12 @@ public class ExcelOutInTotalServiceImpl extends ServiceImpl<ExcelOutInTotalMappe
|
|
|
@Resource
|
|
|
private IReportSettingService reportSettingService;
|
|
|
|
|
|
+ @Resource
|
|
|
+ private ZhbsqConfig zhbsqConfig;
|
|
|
+ @Resource
|
|
|
+ private IReportWordService reportWordService;
|
|
|
+
|
|
|
+
|
|
|
|
|
|
@Override
|
|
|
public TableDataInfo<ExcelOutInTotalVo> queryList(ExcelOutInTotalVo entity) {
|
|
@@ -62,12 +78,15 @@ public class ExcelOutInTotalServiceImpl extends ServiceImpl<ExcelOutInTotalMappe
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public boolean analyze(MultipartFile file) {
|
|
|
+ public boolean analyze(MultipartFile file,String gxTradeTotalMoney) {
|
|
|
final String nn = "南宁综";
|
|
|
final String wz = "梧州综";
|
|
|
final String qz = "钦州综";
|
|
|
final String bh = "北海综";
|
|
|
final String px = "凭祥综";
|
|
|
+ BigDecimal tenThousand = BigDecimal.valueOf(10000);
|
|
|
+ ReportDTO reportDTO = new ReportDTO();
|
|
|
+ reportDTO.setGxTradeTotalMoney(gxTradeTotalMoney+"");
|
|
|
try (InputStream inputStream = file.getInputStream()) {
|
|
|
ExcelReader reader = ExcelUtil.getReader(inputStream);
|
|
|
String text = reader.readAsText(false);
|
|
@@ -80,6 +99,7 @@ public class ExcelOutInTotalServiceImpl extends ServiceImpl<ExcelOutInTotalMappe
|
|
|
String time = StrUtil.subBefore(StrUtil.subAfter(title, "20", true), "特定地区进出口总值", true);
|
|
|
String year = "20" + StrUtil.subBefore(time, "年", true);
|
|
|
String month = StrUtil.subBetween(time, "年", "月");
|
|
|
+ reportDTO.setYear(year).setMonth(month);
|
|
|
ReportRecord reportRecord = reportRecordService.findReport(year, month);
|
|
|
if (reportRecord == null) {
|
|
|
reportRecord = new ReportRecord();
|
|
@@ -101,9 +121,29 @@ public class ExcelOutInTotalServiceImpl extends ServiceImpl<ExcelOutInTotalMappe
|
|
|
String name = str.getName();
|
|
|
return name.contains(nn) || name.contains(wz) || name.contains(qz) || name.contains(bh) || name.contains(px);
|
|
|
}).collect(Collectors.toList());
|
|
|
- gxList.forEach(excelOutInTotal -> excelOutInTotal.setReportId(reportId));
|
|
|
+ gxList.forEach(excelOutInTotal -> {
|
|
|
+ excelOutInTotal.setReportId(reportId);
|
|
|
+ String name = excelOutInTotal.getName();
|
|
|
+ int index = excelOutInTotal.getIndex();
|
|
|
+ String totalInc = excelOutInTotal.getTotalInc()+"%";
|
|
|
+ totalInc=Double.parseDouble(excelOutInTotal.getTotalInc())==0?"-":totalInc;
|
|
|
+ String totalMoney = new BigDecimal(excelOutInTotal.getTotalCumulativeMonth()).divide(tenThousand, 1, RoundingMode.HALF_UP).toString();
|
|
|
+ if (name.contains(nn)) {
|
|
|
+ reportDTO.setNnIndex(index)
|
|
|
+ .setNnInc(totalInc)
|
|
|
+ .setNnTotalMoney(totalMoney);
|
|
|
+ } else if (name.contains(px)) {
|
|
|
+ reportDTO.setPxIndex(index).setPxInc(totalInc).setPxTotalMoney(totalMoney);
|
|
|
+ } else if (name.contains(wz)) {
|
|
|
+ reportDTO.setWzIndex(index).setWzInc(totalInc).setWzTotalMoney(totalMoney);
|
|
|
+ } else if (name.contains(bh)) {
|
|
|
+ reportDTO.setBhIndex(index).setBhInc(totalInc).setBhTotalMoney(totalMoney);
|
|
|
+ } else if (name.contains(qz)) {
|
|
|
+ reportDTO.setQzIndex(index).setQzInc(totalInc).setQzTotalMoney(totalMoney);
|
|
|
+ }
|
|
|
+ });
|
|
|
this.saveBatch(gxList);
|
|
|
- generateReport(reportRecord, gxList, excelDTOList.size());
|
|
|
+ generateReport(reportRecord, gxList, excelDTOList.size(), reportDTO);
|
|
|
reportRecordService.updateById(reportRecord);
|
|
|
} catch (IOException e) {
|
|
|
e.printStackTrace();
|
|
@@ -111,38 +151,166 @@ public class ExcelOutInTotalServiceImpl extends ServiceImpl<ExcelOutInTotalMappe
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
- @Override
|
|
|
- public void removeByReportId(String reportId) {
|
|
|
- this.baseMapper.removeByReportId(reportId);
|
|
|
- }
|
|
|
|
|
|
- private void generateReport(ReportRecord reportRecord, List<ExcelOutInTotal> gxList, int total) {
|
|
|
+ private void generateReport(ReportRecord reportRecord, List<ExcelOutInTotal> gxList, int total, ReportDTO reportDTO) {
|
|
|
Double beforeTotalCumulativeMonth = 0D;
|
|
|
BigDecimal hundred = BigDecimal.valueOf(100);
|
|
|
+ BigDecimal tenThousand = BigDecimal.valueOf(10000);
|
|
|
+ gxList.sort(Comparator.comparing(excelOutInTotal -> Double.parseDouble(excelOutInTotal.getTotalInc())));
|
|
|
+ List<ExcelOutInTotal> incList = new ArrayList<>();
|
|
|
+ List<ExcelOutInTotal> descList = new ArrayList<>();
|
|
|
+ StringBuilder tradeTotalMoneyIndexDesc = new StringBuilder();
|
|
|
for (ExcelOutInTotal excelOutInTotal : gxList) {
|
|
|
Double totalCumulativeMonth = Double.parseDouble(excelOutInTotal.getTotalCumulativeMonth());
|
|
|
- Double totalInc = (new BigDecimal(excelOutInTotal.getTotalInc()).add(hundred))
|
|
|
+ BigDecimal totalIncD = new BigDecimal(excelOutInTotal.getTotalInc());
|
|
|
+ Double totalInc = totalIncD.add(hundred)
|
|
|
.divide(hundred, 5, RoundingMode.HALF_UP).doubleValue();
|
|
|
+ if (totalIncD.doubleValue() > 0) {
|
|
|
+ incList.add(excelOutInTotal);
|
|
|
+ } else if (totalIncD.doubleValue() < 0) {
|
|
|
+ descList.add(excelOutInTotal);
|
|
|
+ }
|
|
|
Double t = NumberUtil.div(totalCumulativeMonth, totalInc, 1, RoundingMode.HALF_UP);
|
|
|
- log.info("t==============:{},{},{}", totalCumulativeMonth, totalInc, t);
|
|
|
+ //log.info("t==============:{},{},{}", totalCumulativeMonth, totalInc, t);
|
|
|
beforeTotalCumulativeMonth = NumberUtil.add(t, beforeTotalCumulativeMonth);
|
|
|
+ tradeTotalMoneyIndexDesc.append(excelOutInTotal.getName()).append("排名").append(excelOutInTotal.getIndex()).append("、");
|
|
|
}
|
|
|
-
|
|
|
- long gxTotal = gxList.stream().collect(Collectors.summarizingLong(e -> Long.parseLong(e.getTotalCumulativeMonth()))).getSum();
|
|
|
- double foreignTradeInc = (BigDecimal.valueOf(gxTotal).subtract(BigDecimal.valueOf(beforeTotalCumulativeMonth)))
|
|
|
- .divide(BigDecimal.valueOf(gxTotal), 4, RoundingMode.HALF_UP)
|
|
|
+ reportDTO.setTradeTotalMoneyIndexDesc(tradeTotalMoneyIndexDesc.toString());
|
|
|
+ String gxTradeTotalMoney=reportDTO.getGxTradeTotalMoney();
|
|
|
+ long tradeTotalMoney = gxList.stream().collect(Collectors.summarizingLong(e -> Long.parseLong(e.getTotalCumulativeMonth()))).getSum();
|
|
|
+ if (StrUtil.isNotEmpty(gxTradeTotalMoney)&&!StrUtil.equals(gxTradeTotalMoney,"undefined")){
|
|
|
+ BigDecimal tradeTotalMoneyPercent= new BigDecimal(tradeTotalMoney)
|
|
|
+ .divide(tenThousand,1,RoundingMode.UP).divide(new BigDecimal(gxTradeTotalMoney),3,RoundingMode.UP);
|
|
|
+ reportDTO.setTradeTotalMoneyPercent(tradeTotalMoneyPercent.multiply(hundred).toString());
|
|
|
+ }
|
|
|
+ //同比增长
|
|
|
+ double tradeTotalMoneyInc = (BigDecimal.valueOf(tradeTotalMoney).subtract(BigDecimal.valueOf(beforeTotalCumulativeMonth)))
|
|
|
+ .divide(BigDecimal.valueOf(beforeTotalCumulativeMonth), 3, RoundingMode.UP)
|
|
|
.multiply(hundred).doubleValue();
|
|
|
- reportRecord.setForeignTrade(gxTotal + "").setForeignTradeInc(foreignTradeInc + "");
|
|
|
- log.info("total:{},{},{}", beforeTotalCumulativeMonth, gxTotal, foreignTradeInc);
|
|
|
+ String tradeTotalMoneyIncDirection = tradeTotalMoneyInc > 0 ? "增长" : "下降";
|
|
|
+ reportDTO.setTradeTotalMoney(new BigDecimal(tradeTotalMoney).divide(tenThousand, 1, RoundingMode.HALF_UP).toString())
|
|
|
+ .setTradeTotalMoneyInc(Math.abs(tradeTotalMoneyInc) + "%")
|
|
|
+ .setTradeTotalMoneyIncDirection(tradeTotalMoneyIncDirection);
|
|
|
+ reportRecord.setForeignTrade(tradeTotalMoney + "").setForeignTradeInc(tradeTotalMoneyInc + "%");
|
|
|
+ //log.info("total:{},{},{}", beforeTotalCumulativeMonth, tradeTotalMoney, tradeTotalMoneyInc);
|
|
|
List<ReportSetting> reportSettings = reportSettingService.list();
|
|
|
- String content = "<h3>(一)外贸进出口";
|
|
|
- Optional<ReportSetting> optionalReportSetting = reportSettings.stream()
|
|
|
- .filter(reportSetting -> Double.parseDouble(reportSetting.getMinNum()) <= foreignTradeInc
|
|
|
- && Double.parseDouble(reportSetting.getMaxNum()) > foreignTradeInc)
|
|
|
- .findFirst();
|
|
|
- String titleSuff = optionalReportSetting.isPresent() ? optionalReportSetting.get().getDescription() : "{"+foreignTradeInc+"%}";
|
|
|
- content=content+titleSuff+":</h3>";
|
|
|
- reportRecord.setResult(content);
|
|
|
+ reportSettings.stream()
|
|
|
+ .filter(reportSetting -> Double.parseDouble(reportSetting.getMinNum()) <= tradeTotalMoneyInc
|
|
|
+ && Double.parseDouble(reportSetting.getMaxNum()) > tradeTotalMoneyInc)
|
|
|
+ .findFirst().ifPresent(reportSetting -> reportDTO.setTradeSituation(reportSetting.getDescription()));
|
|
|
+ String incName = incList.stream().map(ExcelOutInTotal::getName).collect(Collectors.joining("、"));
|
|
|
+ String incPercent = incList.stream().map(excelOutInTotal -> excelOutInTotal.getTotalInc() + "%").collect(Collectors.joining("、"));
|
|
|
+ String descName = descList.stream().map(ExcelOutInTotal::getName).collect(Collectors.joining("、"));
|
|
|
+ String descPercent = descList.stream().map(excelOutInTotal -> Math.abs(Double.parseDouble(excelOutInTotal.getTotalInc())) + "%").collect(Collectors.joining("、"));
|
|
|
+ StringBuilder bondedIncAnalyse = new StringBuilder("其中,");
|
|
|
+ if (incList.size() > 0) {
|
|
|
+ bondedIncAnalyse.append(incName).append("同比增长了").append(incPercent).append(";");
|
|
|
+ }
|
|
|
+ if (descList.size() > 0) {
|
|
|
+ bondedIncAnalyse.append(descName).append("同比下降了").append(descPercent);
|
|
|
+ }
|
|
|
+
|
|
|
+ reportDTO.setBondedIncAnalyse(bondedIncAnalyse.toString());
|
|
|
+ log.info("report:{}", JSONUtil.toJsonStr(reportDTO));
|
|
|
+ createWord(reportDTO,reportRecord);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 生成word文档
|
|
|
+ * @param reportDTO
|
|
|
+ */
|
|
|
+ private void createWord(ReportDTO reportDTO,ReportRecord reportRecord) {
|
|
|
+ // 上传文件路径
|
|
|
+ String filePath = ZhbsqConfig.getWordPath();
|
|
|
+ File file=new File(filePath);
|
|
|
+ if (!file.exists()) {
|
|
|
+ file.mkdirs();
|
|
|
+ }
|
|
|
+ Field[] fields = ReflectUtil.getFields(reportDTO.getClass());
|
|
|
+ String reportPath= zhbsqConfig.getReportPath();
|
|
|
+ try (FileInputStream fileInputStream = new FileInputStream(reportPath)) {
|
|
|
+ XWPFDocument document = new XWPFDocument(fileInputStream);
|
|
|
+ List<XWPFParagraph> paragraphs = document.getParagraphs();
|
|
|
+ paragraphs.forEach(paragraph -> {
|
|
|
+ List<XWPFRun> runs = paragraph.getRuns();
|
|
|
+ StringBuilder sb = new StringBuilder();
|
|
|
+ for (XWPFRun r : runs) {
|
|
|
+ sb.append(r.getText(0));
|
|
|
+ }
|
|
|
+
|
|
|
+ String text = sb.toString();
|
|
|
+ for (Field field : fields) {
|
|
|
+ String fieldName = field.getName();
|
|
|
+ if (text.contains("${" + fieldName + "}")) {
|
|
|
+ Object fieldValue = ReflectUtil.getFieldValue(reportDTO, fieldName);
|
|
|
+ if (fieldValue != null) {
|
|
|
+ text = StrUtil.replace(text, "${" + fieldName + "}", fieldValue.toString());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (StrUtil.isNotEmpty(text)) {
|
|
|
+ // 清除所有的runs
|
|
|
+ for (int i = runs.size() - 1; i >= 0; i--) {
|
|
|
+ paragraph.removeRun(i);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 添加新的run
|
|
|
+ XWPFRun newRun = paragraph.createRun();
|
|
|
+ newRun.setText(text);
|
|
|
+ if (text.contains("广西综合保税区经济运行分析") || text.contains("月经济运行指标情况")) {
|
|
|
+ newRun.setFontSize(22);
|
|
|
+ newRun.setFontFamily("方正小标宋_GBK");
|
|
|
+ } else if (text.contains("(一)")){
|
|
|
+ newRun.setFontSize(16);
|
|
|
+ newRun.setFontFamily("方正仿宋_GBK");
|
|
|
+ }else {
|
|
|
+ newRun.setFontSize(16);
|
|
|
+ newRun.setFontFamily("方正仿宋_GBK");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ });
|
|
|
+ List<XWPFTable> tables = document.getTables();
|
|
|
+ for (XWPFTable table : tables) {
|
|
|
+ List<XWPFTableRow> rows = table.getRows();
|
|
|
+ for (XWPFTableRow row : rows) {
|
|
|
+ for (XWPFTableCell cell : row.getTableCells()) {
|
|
|
+ String text = cell.getText();
|
|
|
+ for (Field field : fields) {
|
|
|
+ String fieldName = field.getName();
|
|
|
+ if (text.contains("${"+fieldName+ "}")) {
|
|
|
+ Object fieldValue = ReflectUtil.getFieldValue(reportDTO, fieldName);
|
|
|
+ if (fieldValue != null) {
|
|
|
+ // 清空单元格
|
|
|
+ cell.removeParagraph(0);
|
|
|
+ XWPFParagraph newPara = cell.addParagraph();
|
|
|
+ //居中
|
|
|
+ newPara.setAlignment(ParagraphAlignment.CENTER);
|
|
|
+ XWPFRun run= newPara.createRun();
|
|
|
+ if (text.contains("tradeTotalMoney")) {
|
|
|
+ run.setBold(true);
|
|
|
+ }
|
|
|
+ text = StrUtil.replace(text, "${" + fieldName + "}", fieldValue.toString());
|
|
|
+ run.setText(text);
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ fileInputStream.close();
|
|
|
+ String filename = reportDTO.getYear() + "年-" + reportDTO.getMonth() + "月分析报告-" + System.currentTimeMillis() + ".docx";
|
|
|
+ FileOutputStream out = new FileOutputStream(filePath +File.separator+ filename);
|
|
|
+ document.write(out);
|
|
|
+ out.close();
|
|
|
+ document.close();
|
|
|
+ ReportWord reportWord=new ReportWord();
|
|
|
+ reportWord.setWordName(filename).setWordPath("/profile/word/"+filename).setRecordId(reportRecord.getId());
|
|
|
+ reportWordService.save(reportWord);
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
private List<ExcelOutInTotal> buildDTO(List<String> list, String currentMonth, String cumulativeMonth) {
|
|
@@ -169,9 +337,16 @@ public class ExcelOutInTotalServiceImpl extends ServiceImpl<ExcelOutInTotalMappe
|
|
|
Collections.reverse(excelDTOList);
|
|
|
int index = 0;
|
|
|
for (ExcelOutInTotal e : excelDTOList) {
|
|
|
+ e.setIndex(index);
|
|
|
|
|
|
index++;
|
|
|
}
|
|
|
return excelDTOList;
|
|
|
}
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void removeByReportId(String reportId) {
|
|
|
+ this.baseMapper.removeByReportId(reportId);
|
|
|
+ }
|
|
|
+
|
|
|
}
|