|
@@ -0,0 +1,202 @@
|
|
|
|
+package com.rongwei.bscommon.sys.service.impl;
|
|
|
|
+
|
|
|
|
+import cn.hutool.core.date.DateUtil;
|
|
|
|
+import cn.hutool.core.util.ArrayUtil;
|
|
|
|
+import cn.hutool.core.util.ObjectUtil;
|
|
|
|
+import cn.hutool.core.util.StrUtil;
|
|
|
|
+import com.aspose.words.Document;
|
|
|
|
+import com.aspose.words.MailMergeCleanupOptions;
|
|
|
|
+import com.aspose.words.net.System.Data.DataRow;
|
|
|
|
+import com.aspose.words.net.System.Data.DataTable;
|
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
|
+import com.bsentity.domin.ComCustomerDo;
|
|
|
|
+import com.bsentity.dto.ComExportMasterDto;
|
|
|
|
+import com.bsentity.dto.ComProductExportDto;
|
|
|
|
+import com.bsentity.vo.ComProTempWordVo;
|
|
|
|
+import com.rongwei.bscommon.sys.service.ComCustomerService;
|
|
|
|
+import com.rongwei.bscommon.sys.service.ExportService;
|
|
|
|
+import com.rongwei.bscommon.sys.utils.AsposeWordsUtils;
|
|
|
|
+import com.rongwei.rwcommon.base.exception.CustomException;
|
|
|
|
+import org.slf4j.Logger;
|
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
+import org.springframework.core.io.ClassPathResource;
|
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
|
+
|
|
|
|
+import javax.servlet.http.HttpServletResponse;
|
|
|
|
+import java.math.BigDecimal;
|
|
|
|
+import java.util.*;
|
|
|
|
+import java.util.function.Function;
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * @author Trj
|
|
|
|
+ */
|
|
|
|
+@Service("export")
|
|
|
|
+public class ExportServiceImpl implements ExportService {
|
|
|
|
+ private final Logger log = LoggerFactory.getLogger(this.getClass().getName());
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ @Autowired
|
|
|
|
+ private ComCustomerService comCustomerService;
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 利用 aspose 框架实现 word 模板导出
|
|
|
|
+ *
|
|
|
|
+ * @param ids
|
|
|
|
+ * @param response
|
|
|
|
+ */
|
|
|
|
+ @Override
|
|
|
|
+ public void exportComProductListTempWord(List<String> ids, HttpServletResponse response) {
|
|
|
|
+ boolean wordLicense = AsposeWordsUtils.getWordLicense();
|
|
|
|
+ ClassPathResource classPathResource = new ClassPathResource("template/comProductlistTemp.docx");
|
|
|
|
+ try {
|
|
|
|
+ // 获取导出数据
|
|
|
|
+ ComProTempWordVo problemTrackingAndResolutionWordDataVo = getComProTempWordVo(ids);
|
|
|
|
+
|
|
|
|
+ Document doc = new Document(classPathResource.getInputStream());
|
|
|
|
+ doc.getMailMerge().setCleanupOptions(MailMergeCleanupOptions.REMOVE_UNUSED_FIELDS);
|
|
|
|
+
|
|
|
|
+ // 子表数据填充
|
|
|
|
+ problemTrackingAndResolutionWordDataVo.getListData().forEach(info -> {
|
|
|
|
+ try {
|
|
|
|
+ doc.getMailMerge().executeWithRegions(info);
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ throw new CustomException("客诉单word导出异常");
|
|
|
|
+ }
|
|
|
|
+ });
|
|
|
|
+ // 单独数据合并
|
|
|
|
+ doc.getMailMerge().execute(problemTrackingAndResolutionWordDataVo.getSingleDataMap().keySet().stream().toArray(String[]::new),
|
|
|
|
+ problemTrackingAndResolutionWordDataVo.getSingleDataMap().values()
|
|
|
|
+ .stream().map((Function<Object, String>) Object::toString).toArray(String[]::new));
|
|
|
|
+
|
|
|
|
+ // 设置打印编号
|
|
|
|
+ comCustomerService.updatePrintNoByIds(ids,problemTrackingAndResolutionWordDataVo.getSingleDataMap().get("PRINTNO"));
|
|
|
|
+
|
|
|
|
+ response.setContentType("application/octet-stream;charset=ISO8859-1");
|
|
|
|
+ response.setHeader("Content-Disposition", "attachment;filename=comProductlistTemp.docx");
|
|
|
|
+ // 返回给前端 输出流文件
|
|
|
|
+ doc.save(response.getOutputStream(), com.aspose.words.SaveFormat.DOCX);
|
|
|
|
+
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ throw new CustomException("客诉单word导出异常");
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 数据准备
|
|
|
|
+ *
|
|
|
|
+ * @param ids 主表id集合
|
|
|
|
+ * @return 模板数据
|
|
|
|
+ */
|
|
|
|
+ private ComProTempWordVo getComProTempWordVo(List<String> ids) {
|
|
|
|
+ // 表单数据
|
|
|
|
+ Map<String, String> wordMap;
|
|
|
|
+
|
|
|
|
+ ComProTempWordVo vo = new ComProTempWordVo();
|
|
|
|
+ // 子表数据
|
|
|
|
+ List<DataTable> listData = new ArrayList<>();
|
|
|
|
+
|
|
|
|
+ if (ArrayUtil.isEmpty(ids)) {
|
|
|
|
+ return vo;
|
|
|
|
+ }
|
|
|
|
+ // 查询主表数据
|
|
|
|
+ ComExportMasterDto comExportMasterDto = comCustomerService.getCustomerByIds(ids);
|
|
|
|
+ if (ObjectUtil.isEmpty(comExportMasterDto)) {
|
|
|
|
+ return vo;
|
|
|
|
+ }
|
|
|
|
+ // 数据清洗
|
|
|
|
+ wordMap = comDtosToMap(comExportMasterDto);
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ /*****************************客诉产品信息*************************************/
|
|
|
|
+ // 根据id 查询 客诉单信息
|
|
|
|
+ List<ComProductExportDto> comProductExportDtos = comCustomerService.getProductByIds(ids);
|
|
|
|
+ if (ArrayUtil.isEmpty(comProductExportDtos)) {
|
|
|
|
+ return vo;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ //子表模板
|
|
|
|
+ DataTable productDataTable = new DataTable("productlist");
|
|
|
|
+ productDataTable.getColumns().add("NO");
|
|
|
|
+ productDataTable.getColumns().add("ORIORDERNO");
|
|
|
|
+ productDataTable.getColumns().add("ALLOY");
|
|
|
|
+ productDataTable.getColumns().add("ISSTATUS");
|
|
|
|
+ productDataTable.getColumns().add("SPECIFICATIONS");
|
|
|
|
+ productDataTable.getColumns().add("MRAWEIGHT");
|
|
|
|
+ productDataTable.getColumns().add("DEFECT");
|
|
|
|
+ productDataTable.getColumns().add("TOTALMARWEIGHT");
|
|
|
|
+
|
|
|
|
+ // 计算 MRA重量总和
|
|
|
|
+ BigDecimal totalMraWeight = comProductExportDtos.stream().map(ComProductExportDto::getMraWeight).reduce(BigDecimal.ONE, BigDecimal::add);
|
|
|
|
+ wordMap.put("TOTALMARWEIGHT",totalMraWeight.toString());
|
|
|
|
+ vo.setSingleDataMap(wordMap);
|
|
|
|
+ // 填充数据
|
|
|
|
+ comProductExportDtos.forEach(comProductExportDto -> {
|
|
|
|
+ DataRow dataRow = productDataTable.newRow();
|
|
|
|
+ dataRow.set("NO", comProductExportDto.getNo());
|
|
|
|
+ dataRow.set("ORIORDERNO", comProductExportDto.getOriOrderNo());
|
|
|
|
+ dataRow.set("ALLOY", comProductExportDto.getAlloy());
|
|
|
|
+ dataRow.set("ISSTATUS", comProductExportDto.getIsStatus());
|
|
|
|
+ dataRow.set("SPECIFICATIONS", comProductExportDto.getSpecifications());
|
|
|
|
+ dataRow.set("MRAWEIGHT", comProductExportDto.getMraWeight());
|
|
|
|
+ dataRow.set("DEFECT", comProductExportDto.getDefect());
|
|
|
|
+ dataRow.set("TOTALMARWEIGHT", totalMraWeight);
|
|
|
|
+ productDataTable.getRows().add(dataRow);
|
|
|
|
+ });
|
|
|
|
+ listData.add(productDataTable);
|
|
|
|
+ vo.setListData(listData);
|
|
|
|
+ return vo;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 将查询的表单数据,转换成MAP
|
|
|
|
+ *
|
|
|
|
+ * @param comExportMasterDto 表单数据
|
|
|
|
+ * @return Map<String, String>
|
|
|
|
+ */
|
|
|
|
+ private Map<String, String> comDtosToMap(ComExportMasterDto comExportMasterDto) {
|
|
|
|
+ Map<String, String> wordMap = new HashMap<>();
|
|
|
|
+ String printNo = null;
|
|
|
|
+ // 最大 打印号
|
|
|
|
+ ComCustomerDo comCustomerDo = comCustomerService.getBaseMapper()
|
|
|
|
+ .selectOne(
|
|
|
|
+ new LambdaQueryWrapper<ComCustomerDo>()
|
|
|
|
+ .select(ComCustomerDo::getPrintNo)
|
|
|
|
+ .orderByDesc(ComCustomerDo::getPrintNo)
|
|
|
|
+ .last("LIMIT 1")
|
|
|
|
+
|
|
|
|
+ );
|
|
|
|
+
|
|
|
|
+ // 编号:规则(MRA+年份+序号)。例如,当年导出的第一个MRA就是MRA23001,导出的第二个就是MRA23002,按照年份以此递增
|
|
|
|
+ String year = String.valueOf(DateUtil.year(new Date())).substring(2);
|
|
|
|
+ if (ObjectUtil.isEmpty(comCustomerDo)) {
|
|
|
|
+ printNo = "MRA" + year + "001";
|
|
|
|
+ }else {
|
|
|
|
+ printNo = comCustomerDo.getPrintNo();
|
|
|
|
+ // 不为空
|
|
|
|
+ String number = printNo.substring(3);
|
|
|
|
+ Integer no = Integer.parseInt(number) + 1;
|
|
|
|
+ String y = String.valueOf(no).substring(0, 2);
|
|
|
|
+ // 跨年了。年份加一,从001开始计算
|
|
|
|
+ if (y.equals(year)) {
|
|
|
|
+ // 未跨年
|
|
|
|
+ printNo = "MRA" + no;
|
|
|
|
+ } else {
|
|
|
|
+ printNo = "MRA" + year + "001";
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ wordMap.put("PRINTNO", printNo);
|
|
|
|
+ wordMap.put("COMNO", comExportMasterDto.getComNo());
|
|
|
|
+ wordMap.put("CUSTOMERNAME", comExportMasterDto.getCustomerName());
|
|
|
|
+ wordMap.put("CUSTOMERUSERNAME", comExportMasterDto.getCustomerUserName());
|
|
|
|
+ wordMap.put("PRODUCTENGINEERNAME", comExportMasterDto.getProductEngineerName());
|
|
|
|
+ wordMap.put("CREATEUSERNAME", comExportMasterDto.getCreateUserName());
|
|
|
|
+ wordMap.put("DATENOW", DateUtil.formatDate(comExportMasterDto.getDateNow()));
|
|
|
|
+ return wordMap;
|
|
|
|
+ }
|
|
|
|
+}
|