|
@@ -1,10 +1,13 @@
|
|
|
package com.fjhx.purchase.service.impl;
|
|
|
|
|
|
+import cn.hutool.core.bean.BeanUtil;
|
|
|
+import cn.hutool.core.util.ObjectUtil;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
import com.fjhx.common.attachment.IAttachmentApi;
|
|
|
import com.fjhx.common.service.ICommonService;
|
|
|
import com.fjhx.company.entity.Company;
|
|
|
import com.fjhx.company.service.ICompanyService;
|
|
|
+import com.fjhx.purchase.dto.PdfPreviewDto;
|
|
|
import com.fjhx.purchase.entity.PurchaseContract;
|
|
|
import com.fjhx.purchase.entity.PurchaseContractProduct;
|
|
|
import com.fjhx.purchase.entity.PurchaseContractProject;
|
|
@@ -16,6 +19,7 @@ import com.fjhx.shipmentdetailed.entity.ShipmentDetailed;
|
|
|
import com.fjhx.shipmentdetailed.service.IShipmentDetailedService;
|
|
|
import com.fjhx.supply.entity.Supply;
|
|
|
import com.fjhx.supply.service.ISupplyService;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
import org.apache.commons.collections4.CollectionUtils;
|
|
|
import org.apache.commons.collections4.MapUtils;
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
@@ -38,6 +42,7 @@ import org.springblade.system.attachment.entity.Attachment;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
+import javax.servlet.ServletOutputStream;
|
|
|
import java.math.BigDecimal;
|
|
|
import java.text.SimpleDateFormat;
|
|
|
import java.util.*;
|
|
@@ -47,19 +52,16 @@ import java.util.stream.Collectors;
|
|
|
* 生成外销合同pdf
|
|
|
*/
|
|
|
@Service
|
|
|
+@Slf4j
|
|
|
public class PurchaseContractPdfServiceImpl implements IPurchaseContractPdfService {
|
|
|
|
|
|
//富文本每行多少个文字
|
|
|
private static final int text_row_count = 45;
|
|
|
//富文本每行多少个文字追加的符号
|
|
|
private static final String text_row_symbol = "<br/>";
|
|
|
-
|
|
|
//模板文件名称
|
|
|
private final String template_name = "purchase_contract";
|
|
|
|
|
|
- //临时保存文件夹
|
|
|
- private final String temp_folder = "/temp/purchase/contract/pdf";
|
|
|
-
|
|
|
@Autowired
|
|
|
private ICommonService commonService;
|
|
|
|
|
@@ -95,7 +97,7 @@ public class PurchaseContractPdfServiceImpl implements IPurchaseContractPdfServi
|
|
|
@Override
|
|
|
public Attachment generate(String id) {
|
|
|
try {
|
|
|
- //获取参数
|
|
|
+ // 获取参数
|
|
|
Map variablesMap = getVariablesMap(id);
|
|
|
//采购id
|
|
|
String purchaseId = (String) variablesMap.get("purchaseId");
|
|
@@ -124,11 +126,11 @@ public class PurchaseContractPdfServiceImpl implements IPurchaseContractPdfServi
|
|
|
Attachment attachment = attachmentJson.toJavaObject(Attachment.class);
|
|
|
//业务id=报价单id
|
|
|
attachment.setBusiId(purchaseId);
|
|
|
- //附件类型=pdf
|
|
|
+ // 附件类型=pdf
|
|
|
attachment.setBusiType(AttachmentConstant.BusiType.PDF);
|
|
|
- //删除pdf附件
|
|
|
+ // 删除pdf附件
|
|
|
attachmentApi.deleteByBusiIdAndType(purchaseId, AttachmentConstant.BusiType.PDF);
|
|
|
- //保存附件
|
|
|
+ // 保存附件
|
|
|
attachmentApi.batchInsert(Collections.singletonList(attachment));
|
|
|
return attachment;
|
|
|
} catch (Exception e) {
|
|
@@ -137,13 +139,34 @@ public class PurchaseContractPdfServiceImpl implements IPurchaseContractPdfServi
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
+ * 地址长度过长,默认截取的长度
|
|
|
+ */
|
|
|
+ private static final int defaultLength = 22;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void pdfPreview(PdfPreviewDto pdfPreviewDto, ServletOutputStream outputStream) throws Exception {
|
|
|
+
|
|
|
+ // 数据处理
|
|
|
+ handlePdfPreviewData(pdfPreviewDto);
|
|
|
+
|
|
|
+ // 生成pdf
|
|
|
+ String temp = template_name + "_" + AuthUtil.getTenantId();
|
|
|
+ // 判断租户定制模板是否存在,如果不存在使用默认模板
|
|
|
+ if (!ThymeleafPdfUtil.existTemplate(temp)) {
|
|
|
+ temp = template_name;
|
|
|
+ }
|
|
|
+
|
|
|
+ ThymeleafPdfUtil.generate(temp, BeanUtil.beanToMap(pdfPreviewDto), outputStream);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
* 获取参数
|
|
|
*
|
|
|
* @param id
|
|
|
* @return
|
|
|
*/
|
|
|
private Map getVariablesMap(String id) {
|
|
|
- //采购合同信息
|
|
|
+ // 采购合同信息
|
|
|
PurchaseContract purchaseContract = purchaseContractService.getById(id);
|
|
|
if (purchaseContract == null) {
|
|
|
throw new ServiceException("生成采购合同PDF文件失败:采购合同不存在");
|
|
@@ -159,7 +182,9 @@ public class PurchaseContractPdfServiceImpl implements IPurchaseContractPdfServi
|
|
|
// 标签过滤
|
|
|
Document document = Jsoup.parse(remark);
|
|
|
remark = document.text();
|
|
|
+
|
|
|
String[] remarkArr = remark.split(purchaseContract.getCode());
|
|
|
+
|
|
|
StringBuilder sb = new StringBuilder();
|
|
|
for (String s : remarkArr) {
|
|
|
if (s.length() > text_row_count + 7) {
|
|
@@ -168,7 +193,6 @@ public class PurchaseContractPdfServiceImpl implements IPurchaseContractPdfServi
|
|
|
sb.append(s).append(text_row_symbol);
|
|
|
}
|
|
|
remark = sb.toString();
|
|
|
-// remark = remark.replaceAll("font-size:", "");
|
|
|
purchaseContract.setRemark(remark);
|
|
|
}
|
|
|
|
|
@@ -282,8 +306,6 @@ public class PurchaseContractPdfServiceImpl implements IPurchaseContractPdfServi
|
|
|
payTypeName = PayTypeEnum.getNameByKey(Integer.valueOf(purchaseContract.getPayType()));
|
|
|
}
|
|
|
|
|
|
- //地址长度过长,默认截取的长度
|
|
|
- Integer defaultLength = 22;
|
|
|
|
|
|
//公司详细地址
|
|
|
String companyAddress = company.getProvinceChinese() + company.getCityChinese() + company.getAddressChinese();
|
|
@@ -328,4 +350,86 @@ public class PurchaseContractPdfServiceImpl implements IPurchaseContractPdfServi
|
|
|
return variablesMap;
|
|
|
}
|
|
|
|
|
|
+ private void handlePdfPreviewData(PdfPreviewDto pdfPreviewDto) {
|
|
|
+ pdfPreviewDto.setHuaWeiUrl(commonService.getHuaWeiUrl());
|
|
|
+ pdfPreviewDto.setApprovedDate(DateUtils.parseDateToStr(DateUtils.DD_MMM_YYYY, new Date(), Locale.ENGLISH));
|
|
|
+ pdfPreviewDto.setUnitTotalAmount(MoneyUtil.number2CNMontrayUnit(ObjectUtil.defaultIfNull(pdfPreviewDto.getTotalAmount(), BigDecimal.ZERO)));
|
|
|
+
|
|
|
+ // 赋值交货日期
|
|
|
+ if (pdfPreviewDto.getPurchaseContract() != null && pdfPreviewDto.getPurchaseContract().getDeliveryTime() != null) {
|
|
|
+ pdfPreviewDto.setDeliveryTime(cn.hutool.core.date.DateUtil.formatDateTime(pdfPreviewDto.getPurchaseContract().getDeliveryTime()));
|
|
|
+ }
|
|
|
+
|
|
|
+ // 企业信息
|
|
|
+ Company company = ObjectUtil.defaultIfNull(companyService.getById(ObjectUtil.defaultIfNull(pdfPreviewDto.getCompanyId(), -99999)), new Company());
|
|
|
+ pdfPreviewDto.setCompany(company);
|
|
|
+ // 企业详细地址
|
|
|
+ String companyAddress = company.getProvinceChinese() + company.getCityChinese() + company.getAddressChinese();
|
|
|
+ if (StringUtils.isNotBlank(companyAddress) && companyAddress.length() > defaultLength) {
|
|
|
+ pdfPreviewDto.setCompanyAddress1(StringUtils.substring(companyAddress, 0, defaultLength));
|
|
|
+ pdfPreviewDto.setCompanyAddress2(StringUtils.substring(companyAddress, defaultLength));
|
|
|
+ } else {
|
|
|
+ pdfPreviewDto.setCompanyAddress1("");
|
|
|
+ pdfPreviewDto.setCompanyAddress2("");
|
|
|
+ }
|
|
|
+
|
|
|
+ // 供应商信息
|
|
|
+ Supply supply = ObjectUtil.defaultIfNull(supplyService.getById(pdfPreviewDto.getSupplyId()), new Supply());
|
|
|
+ pdfPreviewDto.setSupply(supply);
|
|
|
+ // 供应商详细地址
|
|
|
+ String supplyAddress = supply.getProvinceName() + supply.getCityName() + supply.getAddress();
|
|
|
+ if (StringUtils.isNotBlank(supplyAddress) && supplyAddress.length() > defaultLength) {
|
|
|
+ pdfPreviewDto.setSupplyAddress1(StringUtils.substring(supplyAddress, 0, defaultLength));
|
|
|
+ pdfPreviewDto.setSupplyAddress2(StringUtils.substring(supplyAddress, defaultLength));
|
|
|
+ } else {
|
|
|
+ pdfPreviewDto.setSupplyAddress1("");
|
|
|
+ pdfPreviewDto.setSupplyAddress2("");
|
|
|
+ }
|
|
|
+
|
|
|
+ List<PdfPreviewDto.ProductDto> products = pdfPreviewDto.getProducts();
|
|
|
+ if (CollectionUtils.isNotEmpty(products)) {
|
|
|
+ for (PdfPreviewDto.ProductDto product : products) {
|
|
|
+ if (StringUtils.isNotBlank(product.getRemark())) {
|
|
|
+ // 富文本内容每个n位追加符号
|
|
|
+ String remark = CommonUtil.nodeTextAppendBR(product.getRemark(), text_row_symbol, text_row_count)
|
|
|
+ .replaceAll(" ", "")
|
|
|
+ .replaceAll("style=", "aaa=");
|
|
|
+ product.setRemark(remark);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ PdfPreviewDto.PurchaseContractDto purchaseContract = pdfPreviewDto.getPurchaseContract();
|
|
|
+ if (purchaseContract == null) {
|
|
|
+ purchaseContract = new PdfPreviewDto.PurchaseContractDto();
|
|
|
+ pdfPreviewDto.setPurchaseContract(purchaseContract);
|
|
|
+ }
|
|
|
+ if (StringUtils.isNotBlank(purchaseContract.getRemark())) {
|
|
|
+ // 内容特殊符号转义
|
|
|
+ String remark = purchaseContract.getRemark();
|
|
|
+ remark = remark.replaceAll("<", "<")
|
|
|
+ .replaceAll(">", ">")
|
|
|
+ .replaceAll(" ", "")
|
|
|
+ .replaceAll(""", "'");
|
|
|
+ // 富文本内容每隔n位追加符号
|
|
|
+ remark = CommonUtil.nodeTextAppendBR(remark, text_row_symbol, text_row_count);
|
|
|
+ remark = remark.replaceAll("</", purchaseContract.getCode() + "</");
|
|
|
+ // 标签过滤
|
|
|
+ Document document = Jsoup.parse(remark);
|
|
|
+ remark = document.text();
|
|
|
+
|
|
|
+ String[] remarkArr = remark.split(purchaseContract.getCode());
|
|
|
+
|
|
|
+ StringBuilder sb = new StringBuilder();
|
|
|
+ for (String s : remarkArr) {
|
|
|
+ if (s.length() > text_row_count + 7) {
|
|
|
+ s = CommonUtil.displayWithComma(s, text_row_symbol, text_row_count + 7);
|
|
|
+ }
|
|
|
+ sb.append(s).append(text_row_symbol);
|
|
|
+ }
|
|
|
+ remark = sb.toString();
|
|
|
+ purchaseContract.setRemark(remark);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
}
|