CrmQuotePdfServiceImpl.java 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  1. package org.dromara.crm.service.impl;
  2. import cn.hutool.core.io.FileUtil;
  3. import cn.hutool.core.util.StrUtil;
  4. import lombok.extern.slf4j.Slf4j;
  5. import org.dromara.common.core.exception.ServiceException;
  6. import org.dromara.crm.domain.vo.CrmQuoteProductVo;
  7. import org.dromara.crm.domain.vo.CrmQuoteVo;
  8. import org.dromara.crm.service.ICrmQuotePdfService;
  9. import org.dromara.crm.service.ICrmQuoteService;
  10. import org.springframework.beans.factory.annotation.Value;
  11. import org.springframework.context.annotation.Lazy;
  12. import org.springframework.stereotype.Service;
  13. import java.io.File;
  14. import java.io.FileWriter;
  15. import java.math.BigDecimal;
  16. import java.nio.charset.StandardCharsets;
  17. import java.time.LocalDate;
  18. import java.time.format.DateTimeFormatter;
  19. import java.util.List;
  20. /**
  21. * 报价单PDF生成Service实现
  22. *
  23. * 说明: 当前采用HTML模板方式生成,可打印为PDF
  24. * 后续可集成专业PDF库(如iText、Apache PDFBox等)
  25. *
  26. * @author 系统管理员
  27. * @date 2025-10-05
  28. */
  29. @Slf4j
  30. @Service
  31. public class CrmQuotePdfServiceImpl implements ICrmQuotePdfService {
  32. private final ICrmQuoteService quoteService;
  33. /**
  34. * 构造函数 - 使用@Lazy注解打破循环依赖
  35. * CrmQuoteServiceImpl依赖ICrmQuotePdfService
  36. * CrmQuotePdfServiceImpl依赖ICrmQuoteService
  37. * 通过延迟加载ICrmQuoteService来解决循环依赖
  38. */
  39. public CrmQuotePdfServiceImpl(@Lazy ICrmQuoteService quoteService) {
  40. this.quoteService = quoteService;
  41. }
  42. @Value("${ruoyi.profile:/Users/wuzilong/work/00code/shanxia/backend/ruoyi-admin/uploadPath}")
  43. private String uploadPath;
  44. @Override
  45. public String generateQuotePdf(Long quoteId) {
  46. // 1. 查询报价单信息
  47. CrmQuoteVo quote = quoteService.queryById(quoteId);
  48. if (quote == null) {
  49. throw new ServiceException("报价单不存在");
  50. }
  51. // 2. 生成HTML内容
  52. String htmlContent = buildQuoteHtml(quote);
  53. // 3. 保存HTML文件(可打印为PDF)
  54. // 将报价单编号中的特殊字符替换为横线,避免文件路径错误
  55. String safeQuoteNo = quote.getQuoteNo().replaceAll("[/\\\\:*?\"<>|]", "-");
  56. String fileName = "报价单-" + safeQuoteNo + ".pdf";
  57. String dateStr = LocalDate.now().format(java.time.format.DateTimeFormatter.ofPattern("yyyyMMdd"));
  58. String relativePath = "/quote/" + dateStr + "/";
  59. String fullPath = uploadPath + relativePath;
  60. // 确保目录存在
  61. FileUtil.mkdir(fullPath);
  62. // 保存文件
  63. String filePath = fullPath + fileName;
  64. try (FileWriter writer = new FileWriter(filePath, StandardCharsets.UTF_8)) {
  65. writer.write(htmlContent);
  66. log.info("生成报价单HTML文件成功: quoteId={}, filePath={}", quoteId, filePath);
  67. } catch (Exception e) {
  68. log.error("生成报价单HTML文件失败: quoteId={}", quoteId, e);
  69. throw new ServiceException("生成PDF文件失败: " + e.getMessage());
  70. }
  71. // 4. 返回相对路径(供前端访问)
  72. String pdfUrl = relativePath + fileName;
  73. log.info("生成报价单PDF成功: quoteId={}, pdfUrl={}", quoteId, pdfUrl);
  74. return pdfUrl;
  75. }
  76. @Override
  77. public Boolean deleteQuotePdf(Long quoteId) {
  78. CrmQuoteVo quote = quoteService.queryById(quoteId);
  79. if (quote == null || StrUtil.isBlank(quote.getPdfUrl())) {
  80. return true;
  81. }
  82. String filePath = uploadPath + quote.getPdfUrl();
  83. File file = new File(filePath);
  84. if (file.exists()) {
  85. boolean deleted = FileUtil.del(file);
  86. log.info("删除报价单PDF: quoteId={}, filePath={}, success={}", quoteId, filePath, deleted);
  87. return deleted;
  88. }
  89. return true;
  90. }
  91. /**
  92. * 构建报价单HTML内容
  93. */
  94. private String buildQuoteHtml(CrmQuoteVo quote) {
  95. StringBuilder html = new StringBuilder();
  96. html.append("<!DOCTYPE html>\n");
  97. html.append("<html lang=\"zh-CN\">\n");
  98. html.append("<head>\n");
  99. html.append(" <meta charset=\"UTF-8\">\n");
  100. html.append(" <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">\n");
  101. html.append(" <title>报价单 - ").append(quote.getQuoteNo()).append("</title>\n");
  102. html.append(" <style>\n");
  103. html.append(getPdfStyles());
  104. html.append(" </style>\n");
  105. html.append("</head>\n");
  106. html.append("<body>\n");
  107. // 页眉
  108. html.append(" <div class=\"header\">\n");
  109. html.append(" <h1>产品报价单</h1>\n");
  110. html.append(" <div class=\"company-name\">杭州第一技师学院智慧校园管理系统</div>\n");
  111. html.append(" </div>\n");
  112. // 报价单信息
  113. html.append(" <div class=\"quote-info\">\n");
  114. html.append(" <table class=\"info-table\">\n");
  115. html.append(" <tr>\n");
  116. html.append(" <td><strong>报价单号:</strong></td>\n");
  117. html.append(" <td>").append(quote.getQuoteNo()).append("</td>\n");
  118. html.append(" <td><strong>版本号:</strong></td>\n");
  119. html.append(" <td>").append(quote.getVersion()).append("</td>\n");
  120. html.append(" </tr>\n");
  121. html.append(" <tr>\n");
  122. html.append(" <td><strong>报价日期:</strong></td>\n");
  123. html.append(" <td>").append(formatDate(quote.getQuoteDate())).append("</td>\n");
  124. html.append(" <td><strong>有效期至:</strong></td>\n");
  125. html.append(" <td>").append(formatDate(quote.getExpireDate())).append("</td>\n");
  126. html.append(" </tr>\n");
  127. html.append(" </table>\n");
  128. html.append(" </div>\n");
  129. // 客户信息
  130. html.append(" <div class=\"customer-info\">\n");
  131. html.append(" <h3>客户信息</h3>\n");
  132. html.append(" <table class=\"info-table\">\n");
  133. html.append(" <tr>\n");
  134. html.append(" <td><strong>客户名称:</strong></td>\n");
  135. html.append(" <td colspan=\"3\">").append(quote.getCustomerName()).append("</td>\n");
  136. html.append(" </tr>\n");
  137. html.append(" <tr>\n");
  138. html.append(" <td><strong>联系人:</strong></td>\n");
  139. html.append(" <td>").append(defaultValue(quote.getContactName())).append("</td>\n");
  140. html.append(" <td><strong>联系电话:</strong></td>\n");
  141. html.append(" <td>").append(defaultValue(quote.getContactMobile())).append("</td>\n");
  142. html.append(" </tr>\n");
  143. html.append(" <tr>\n");
  144. html.append(" <td><strong>电子邮箱:</strong></td>\n");
  145. html.append(" <td colspan=\"3\">").append(defaultValue(quote.getContactEmail()))
  146. .append("</td>\n");
  147. html.append(" </tr>\n");
  148. html.append(" </table>\n");
  149. html.append(" </div>\n");
  150. // 产品明细
  151. html.append(" <div class=\"products\">\n");
  152. html.append(" <h3>产品明细</h3>\n");
  153. html.append(" <table class=\"product-table\">\n");
  154. html.append(" <thead>\n");
  155. html.append(" <tr>\n");
  156. html.append(" <th width=\"50\">序号</th>\n");
  157. html.append(" <th>产品名称</th>\n");
  158. html.append(" <th width=\"100\">规格型号</th>\n");
  159. html.append(" <th width=\"60\">单位</th>\n");
  160. html.append(" <th width=\"80\">数量</th>\n");
  161. html.append(" <th width=\"100\">单价(元)</th>\n");
  162. html.append(" <th width=\"80\">折扣</th>\n");
  163. html.append(" <th width=\"120\">小计(元)</th>\n");
  164. html.append(" </tr>\n");
  165. html.append(" </thead>\n");
  166. html.append(" <tbody>\n");
  167. List<CrmQuoteProductVo> products = quote.getProductList();
  168. if (products != null && !products.isEmpty()) {
  169. int index = 1;
  170. for (CrmQuoteProductVo product : products) {
  171. html.append(" <tr>\n");
  172. html.append(" <td class=\"center\">").append(index++).append("</td>\n");
  173. html.append(" <td>").append(product.getProductName()).append("</td>\n");
  174. html.append(" <td class=\"center\">").append(defaultValue(product.getModel()))
  175. .append("</td>\n");
  176. html.append(" <td class=\"center\">").append(product.getUnit()).append("</td>\n");
  177. html.append(" <td class=\"right\">").append(product.getQuantity()).append("</td>\n");
  178. html.append(" <td class=\"right\">").append(formatMoney(product.getPrice()))
  179. .append("</td>\n");
  180. html.append(" <td class=\"center\">").append(formatDiscount(product.getDiscount()))
  181. .append("</td>\n");
  182. html.append(" <td class=\"right\">").append(formatMoney(product.getSubtotal()))
  183. .append("</td>\n");
  184. html.append(" </tr>\n");
  185. }
  186. }
  187. html.append(" </tbody>\n");
  188. html.append(" </table>\n");
  189. html.append(" </div>\n");
  190. // 价格汇总
  191. html.append(" <div class=\"price-summary\">\n");
  192. html.append(" <table class=\"summary-table\">\n");
  193. html.append(" <tr>\n");
  194. html.append(" <td class=\"label\">产品总价:</td>\n");
  195. html.append(" <td class=\"value\">¥ ").append(formatMoney(quote.getTotalAmount()))
  196. .append("</td>\n");
  197. html.append(" </tr>\n");
  198. if (quote.getDiscountRate() != null && quote.getDiscountRate().compareTo(BigDecimal.ZERO) > 0) {
  199. html.append(" <tr>\n");
  200. html.append(" <td class=\"label\">整单折扣(").append(quote.getDiscountRate())
  201. .append("%):</td>\n");
  202. html.append(" <td class=\"value\">- ¥ ").append(formatMoney(quote.getDiscountAmount()))
  203. .append("</td>\n");
  204. html.append(" </tr>\n");
  205. }
  206. html.append(" <tr class=\"total\">\n");
  207. html.append(" <td class=\"label\"><strong>最终金额:</strong></td>\n");
  208. html.append(" <td class=\"value\"><strong>¥ ").append(formatMoney(quote.getFinalAmount()))
  209. .append("</strong></td>\n");
  210. html.append(" </tr>\n");
  211. html.append(" </table>\n");
  212. html.append(" </div>\n");
  213. // 条款说明
  214. html.append(" <div class=\"terms\">\n");
  215. if (StrUtil.isNotBlank(quote.getPaymentTerms())) {
  216. html.append(" <h3>付款条款</h3>\n");
  217. html.append(" <p>").append(quote.getPaymentTerms()).append("</p>\n");
  218. }
  219. if (StrUtil.isNotBlank(quote.getDeliveryTerms())) {
  220. html.append(" <h3>交付条款</h3>\n");
  221. html.append(" <p>").append(quote.getDeliveryTerms()).append("</p>\n");
  222. }
  223. if (StrUtil.isNotBlank(quote.getRemark())) {
  224. html.append(" <h3>备注说明</h3>\n");
  225. html.append(" <p>").append(quote.getRemark()).append("</p>\n");
  226. }
  227. html.append(" </div>\n");
  228. // 页脚
  229. html.append(" <div class=\"footer\">\n");
  230. html.append(" <p><strong>销售负责人:</strong> ").append(defaultValue(quote.getOwnerUserName()))
  231. .append("</p>\n");
  232. html.append(" <p><strong>联系电话:</strong> 请联系您的专属销售</p>\n");
  233. html.append(" <p class=\"notice\">本报价单有效期至 ").append(formatDate(quote.getExpireDate()))
  234. .append(",过期需重新确认价格。</p>\n");
  235. html.append(" </div>\n");
  236. html.append("</body>\n");
  237. html.append("</html>");
  238. return html.toString();
  239. }
  240. /**
  241. * PDF样式
  242. */
  243. private String getPdfStyles() {
  244. return """
  245. * {
  246. margin: 0;
  247. padding: 0;
  248. box-sizing: border-box;
  249. }
  250. body {
  251. font-family: "Microsoft YaHei", "微软雅黑", Arial, sans-serif;
  252. line-height: 1.6;
  253. color: #333;
  254. padding: 40px;
  255. background: #fff;
  256. position: relative;
  257. }
  258. /* 水印层 */
  259. body::before {
  260. content: '';
  261. position: fixed;
  262. top: 0;
  263. left: 0;
  264. width: 100%;
  265. height: 100%;
  266. background-image: url('/watermask.jpg');
  267. background-repeat: no-repeat;
  268. background-position: center center;
  269. background-size: 50%;
  270. opacity: 0.15;
  271. z-index: 0;
  272. pointer-events: none;
  273. }
  274. /* 确保所有内容在水印上方 */
  275. body > * {
  276. position: relative;
  277. z-index: 1;
  278. }
  279. .header {
  280. text-align: center;
  281. border-bottom: 3px solid #1890ff;
  282. padding-bottom: 20px;
  283. margin-bottom: 30px;
  284. }
  285. .header h1 {
  286. font-size: 32px;
  287. color: #1890ff;
  288. margin-bottom: 10px;
  289. }
  290. .company-name {
  291. font-size: 16px;
  292. color: #666;
  293. }
  294. .quote-info, .customer-info {
  295. margin-bottom: 30px;
  296. }
  297. .info-table {
  298. width: 100%;
  299. border-collapse: collapse;
  300. margin-top: 10px;
  301. }
  302. .info-table td {
  303. padding: 8px 12px;
  304. border: 1px solid #e8e8e8;
  305. }
  306. .info-table td:nth-child(odd) {
  307. background: #fafafa;
  308. width: 120px;
  309. }
  310. .products {
  311. margin-bottom: 30px;
  312. }
  313. h3 {
  314. font-size: 18px;
  315. color: #1890ff;
  316. margin-bottom: 15px;
  317. border-left: 4px solid #1890ff;
  318. padding-left: 10px;
  319. }
  320. .product-table {
  321. width: 100%;
  322. border-collapse: collapse;
  323. margin-top: 10px;
  324. }
  325. .product-table th {
  326. background: #1890ff;
  327. color: #fff;
  328. padding: 12px 8px;
  329. text-align: center;
  330. font-weight: normal;
  331. }
  332. .product-table td {
  333. padding: 10px 8px;
  334. border: 1px solid #e8e8e8;
  335. }
  336. .product-table tbody tr:hover {
  337. background: #f5f5f5;
  338. }
  339. .center {
  340. text-align: center;
  341. }
  342. .right {
  343. text-align: right;
  344. }
  345. .price-summary {
  346. margin: 30px 0;
  347. float: right;
  348. width: 350px;
  349. }
  350. .summary-table {
  351. width: 100%;
  352. border-collapse: collapse;
  353. }
  354. .summary-table td {
  355. padding: 10px 15px;
  356. border: 1px solid #e8e8e8;
  357. }
  358. .summary-table .label {
  359. background: #fafafa;
  360. width: 150px;
  361. }
  362. .summary-table .value {
  363. text-align: right;
  364. font-size: 16px;
  365. }
  366. .summary-table .total {
  367. background: #fff7e6;
  368. }
  369. .summary-table .total .value {
  370. color: #ff4d4f;
  371. font-size: 20px;
  372. }
  373. .terms {
  374. clear: both;
  375. margin-top: 50px;
  376. }
  377. .terms p {
  378. padding: 10px 15px;
  379. background: #f5f5f5;
  380. border-radius: 4px;
  381. line-height: 1.8;
  382. margin-bottom: 10px;
  383. }
  384. .footer {
  385. margin-top: 50px;
  386. padding-top: 20px;
  387. border-top: 2px solid #e8e8e8;
  388. text-align: center;
  389. color: #666;
  390. }
  391. .footer p {
  392. margin: 8px 0;
  393. }
  394. .footer .notice {
  395. color: #ff4d4f;
  396. font-size: 14px;
  397. margin-top: 20px;
  398. }
  399. @media print {
  400. body {
  401. padding: 20px;
  402. }
  403. /* 打印时也显示水印 */
  404. body::before {
  405. position: absolute;
  406. print-color-adjust: exact;
  407. -webkit-print-color-adjust: exact;
  408. }
  409. .header {
  410. page-break-after: avoid;
  411. }
  412. .product-table {
  413. page-break-inside: avoid;
  414. }
  415. }
  416. """;
  417. }
  418. /**
  419. * 格式化日期
  420. */
  421. private String formatDate(Object date) {
  422. if (date == null) {
  423. return "";
  424. }
  425. if (date instanceof LocalDate) {
  426. return ((LocalDate) date).format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
  427. }
  428. return date.toString();
  429. }
  430. /**
  431. * 格式化金额
  432. */
  433. private String formatMoney(BigDecimal amount) {
  434. if (amount == null) {
  435. return "0.00";
  436. }
  437. return String.format("%,.2f", amount);
  438. }
  439. /**
  440. * 格式化折扣
  441. */
  442. private String formatDiscount(BigDecimal discount) {
  443. if (discount == null || discount.compareTo(BigDecimal.ZERO) == 0) {
  444. return "无折扣";
  445. }
  446. return discount.stripTrailingZeros().toPlainString() + "%";
  447. }
  448. /**
  449. * 默认值处理
  450. */
  451. private String defaultValue(String value) {
  452. return StrUtil.isNotBlank(value) ? value : "-";
  453. }
  454. }