zhangback
7 天以前 b054362aaf616bfe0be0b50ae5dc2137091dbd7d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package com.ruoyi.tms.service.impl;
 
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.PdfTemplateUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.uuid.Seq;
import com.ruoyi.tms.domain.TmsDispatchOrder;
import com.ruoyi.tms.domain.TmsDriver;
import com.ruoyi.tms.domain.TmsTrip;
import com.ruoyi.tms.mapper.TmsTripMapper;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
 
import javax.annotation.Resource;
import java.io.File;
import java.util.HashMap;
import java.util.Map;
 
@Service
public class AsyncPdfService {
    @Resource
    private TmsTripMapper tmsTripMapper;
    @Value("${custom.driver.template1}")
    private String template1;
 
    @Value("${custom.driver.template2}")
    private String template2;
    @Value("${custom.driver.font}")
    private String fontPath;
    @Value("${custom.upload.network-path}")
    private String networkPath;
    @Async
    public void generateTripPdfAsync(TmsTrip tmsTrip, TmsDispatchOrder order, TmsDriver driver) {
 
        try {
            Map<String,String> data = new HashMap<>();
            data.put("name", driver.getDriverName());
            data.put("date", DateUtils.getDate());
            data.put("signImage", tmsTrip.getSignImg());
 
            String uploadPath = RuoYiConfig.getUploadPath();
 
            // 1. 生成 PDF A
            String format1 = StringUtils.format("{}/{}_{}_{}.pdf",
                    DateUtils.datePath(), "chqjkcns", order.getDispatchNo(), Seq.getId(Seq.uploadSeqType));
 
            File absoluteFile1 = FileUploadUtils.getAbsoluteFile(uploadPath, format1);
            PdfTemplateUtil.fillPdf(template1, absoluteFile1.getAbsolutePath(), data, fontPath);
            String url1 = networkPath + FileUploadUtils.getPathFileName(uploadPath, format1);
 
            // 2. 生成 PDF B
            String format2 = StringUtils.format("{}/{}_{}_{}.pdf",
                    DateUtils.datePath(), "zjsf", order.getDispatchNo(), Seq.getId(Seq.uploadSeqType));
 
            File absoluteFile2 = FileUploadUtils.getAbsoluteFile(uploadPath, format2);
            PdfTemplateUtil.fillPdf(template2, absoluteFile2.getAbsolutePath(), data, fontPath);
            String url2 = networkPath + FileUploadUtils.getPathFileName(uploadPath, format2);
 
            // 3. 更新数据库 PDF URL
            TmsTrip update = new TmsTrip();
            update.setId(tmsTrip.getId());
            update.setChqjkcnsUrl(url1);
            update.setZjsfUrl(url2);
 
            tmsTripMapper.updateTmsTrip(update);
 
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}