刘光辉
昨天 bb638871a7fb692d80f1b7a758f991dc0879002c
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
package jnpf.limsController;
 
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jnpf.exception.DataException;
import jnpf.limsEntity.LimsPrintTemplateEntity;
import jnpf.limsService.LimsPrintTemplateService;
import jnpf.limsService.print.PrintRendererRegistry;
import jnpf.limsService.print.PrintTemplateRenderer;
import jnpf.util.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
 
import java.util.Map;
 
@Slf4j
@Tag(name = "lims打印接口", description = "通用打印模板渲染")
@RestController
@RequestMapping("/lims/biz/print")
public class LimsPrintController {
 
    @Autowired
    private LimsPrintTemplateService limsPrintTemplateService;
 
    @Autowired
    private PrintRendererRegistry printRendererRegistry;
 
    /**
     * 渲染打印模板:按 template_id 命中模板配置 + 路由到 renderer,组装前端
     * 渲染所需的数据。
     *
     * 请求体:{ "template_id": "jianyan_baogao", "id": "<业务实体 f_id>" }
     */
    @Operation(summary = "渲染打印模板")
    @PostMapping("/render")
    public R<Map<String, Object>> render(@RequestBody Map<String, String> params) {
        if (params == null) {
            return R.error("缺少入参");
        }
        String templateId = params.get("template_id");
        String id = params.get("id");
        if (templateId == null || templateId.isEmpty()) {
            return R.error("缺少 template_id");
        }
        if (id == null || id.isEmpty()) {
            return R.error("缺少 id");
        }
 
        LimsPrintTemplateEntity tpl = limsPrintTemplateService.findActiveByTemplateId(templateId);
        if (tpl == null) {
            return R.error("找不到启用的打印模板: " + templateId);
        }
 
        PrintTemplateRenderer renderer;
        try {
            renderer = printRendererRegistry.get(templateId);
        } catch (DataException e) {
            return R.error(e.getMessage());
        }
 
        try {
            Map<String, Object> data = renderer.render(id, tpl);
            return R.success(data);
        } catch (DataException e) {
            return R.error(e.getMessage());
        }
    }
}