JRT报告多列多页打印

最近进入打磨阶段,会写一些demo和借助demo实现测试bug。本次实现基于JRT的报告多列多页的模板控制打印。随着慢慢的写demo打磨,渐入佳境的感觉。

模板设计器表格元素支持报告模式的边框,报告元素的边框表现为标题只画下边框,数据列只画多列分割线
JRT报告多列多页打印_第1张图片

打印效果
JRT报告多列多页打印_第2张图片

JRT报告多列多页打印_第3张图片

设置正常边框
JRT报告多列多页打印_第4张图片

正常边框打印效果
JRT报告多列多页打印_第5张图片
JRT报告多列多页打印_第6张图片

设置单列打印
JRT报告多列多页打印_第7张图片

单列打印效果
JRT报告多列多页打印_第8张图片

JRT报告多列多页打印_第9张图片

JRT报告多列多页打印_第10张图片

打印代码的微调

import JRT.Core.DataGrid.GridDto;
import JRT.Core.DataGrid.IGridChangePage;
import JRT.Core.Dto.HashParam;
import JRT.Core.Dto.OutValue;
import JRT.Core.Dto.PrintElement;
import JRT.Core.Util.Convert;
import JRT.Core.Util.PrintDrawProtocol;
import JRT.Model.Entity.JRTPrintTemplate;
import JRT.Model.Entity.JRTPrintTemplateEle;
import JRTBLLBase.BaseHttpHandlerNoSession;
import JRTBLLBase.Helper;

import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;

/**
 * 调用模板数据来打印,这是一个模拟打印的demo,调用模板数据组装打印数据
 */
public class PrintTemplateTest extends BaseHttpHandlerNoSession implements IGridChangePage {

    /**
     * 打印元素
     */
    private List<PrintElement> retList = new ArrayList<>();

    /**
     * 打印模板数据
     */
    JRTPrintTemplateDto template =null;

    /**
     * 存打印的Data数据
     */
    Hashtable<String, String> printDataMap =null;

    /**
     * 表格换页之前要执行的逻辑
     * @param gridKey
     * @param maxY
     */
    public void PreChangePageDo(String gridKey,int maxY)
    {

    }

    /**
     * 表格换页之后要执行的逻辑
     * @param gridKey
     * @param maxY
     */
    public void AfterChangePageDo(String gridKey,int maxY)
    {
        System.out.println("换页后执行逻辑");
        //画数据元素
        PrintData(template, retList, printDataMap);
        //画标签元素
        PrintLabel(template, retList);
        //画线元素
        PrintLine(template, retList);
    }

    /**
     * 按传入的RowID输出符合打印元素绘制协议的数据来实现打印控制
     *
     * @param RowID   业务主键
     * @param P1
     * @param P2
     * @param P3
     * @param P4
     * @param P5
     * @param P6
     * @param P7
     * @param P8
     * @param P9
     * @param P10
     * @param P11
     * @param P12
     * @param P13
     * @param Session
     * @param Output
     * @return
     */
    public String GetData(String RowID, String P1, String P2, String P3, String P4, String P5, String P6, String P7, String P8, String P9, String P10, String P11, String P12, String P13, OutValue Session, OutValue Output) throws Exception {
        //查询模板数据
        template = GetOneTemplate(null, "JRTReport");
        //存打印的Data数据
        printDataMap = new Hashtable<>();
        printDataMap.put("姓名", "小乌鱼");
        printDataMap.put("性别", "男");
        printDataMap.put("年龄", "32岁");
        printDataMap.put("登记号", "0947809");
        printDataMap.put("检验号", "24011901");
        printDataMap.put("科室", "体检科");
        printDataMap.put("标本", "全血");
        printDataMap.put("床号", "");
        printDataMap.put("诊断", "体检");
        printDataMap.put("采集日期", "2024-01-19");
        printDataMap.put("采集时间", "16:00");
        printDataMap.put("医嘱备注", "体检");
        printDataMap.put("医嘱名称", "血细胞分析五分类");
        printDataMap.put("申请医生", "乌鱼");
        printDataMap.put("申请日期", "2024-01-19");
        printDataMap.put("操作者", "李四");
        printDataMap.put("审核日期", "2024-01-19");
        printDataMap.put("审核时间", "17:00");
        printDataMap.put("审核者", "张三");

        //存打印的图数据
        Hashtable<String, String> printImgMap = new Hashtable<>();
        printImgMap.put("RPC", "http://localhost:8080/JRTWeb/FileService/zlz/logo.png");

        //存项目结果数据
        List<Object> repResList = new ArrayList<>();
        RepResPrintDto oneRes = new RepResPrintDto();
        oneRes.AbFlag = "*";
        oneRes.RefRange = "12-34um/ml";
        oneRes.Result = "50";
        oneRes.Seq = "1";
        oneRes.TestCodeName = "白细胞计数";
        repResList.add(oneRes);
        oneRes = new RepResPrintDto();
        oneRes.AbFlag = "*";
        oneRes.RefRange = "12-34um/ml";
        oneRes.Result = "50";
        oneRes.Seq = "2";
        oneRes.TestCodeName = "红细胞计数";
        repResList.add(oneRes);
        oneRes = new RepResPrintDto();
        oneRes.AbFlag = "";
        oneRes.RefRange = "12-34um/ml";
        oneRes.Result = "33";
        oneRes.Seq = "3";
        oneRes.TestCodeName = "淋巴细胞计数";
        repResList.add(oneRes);
        oneRes = new RepResPrintDto();
        oneRes.AbFlag = "↓";
        oneRes.RefRange = "22-34um/ml";
        oneRes.Result = "5";
        oneRes.Seq = "4";
        oneRes.TestCodeName = "单核细胞计数";
        repResList.add(oneRes);
        oneRes = new RepResPrintDto();
        oneRes.AbFlag = "↓";
        oneRes.RefRange = "22-34um/ml";
        oneRes.Result = "5";
        oneRes.Seq = "5";
        oneRes.TestCodeName = "单核细胞计数";
        repResList.add(oneRes);
        oneRes = new RepResPrintDto();
        oneRes.AbFlag = "↓";
        oneRes.RefRange = "22-34um/ml";
        oneRes.Result = "5";
        oneRes.Seq = "6";
        oneRes.TestCodeName = "单核细胞计数";
        repResList.add(oneRes);
        oneRes = new RepResPrintDto();
        oneRes.AbFlag = "↓";
        oneRes.RefRange = "22-34um/ml";
        oneRes.Result = "5";
        oneRes.Seq = "7";
        oneRes.TestCodeName = "单核细胞计数";
        repResList.add(oneRes);
        oneRes = new RepResPrintDto();
        oneRes.AbFlag = "↓";
        oneRes.RefRange = "22-34um/ml";
        oneRes.Result = "5";
        oneRes.Seq = "8";
        oneRes.TestCodeName = "单核细胞计数";
        repResList.add(oneRes);
        for(int k=9;k<40;k++)
        {
            oneRes = new RepResPrintDto();
            oneRes.AbFlag = "↓";
            oneRes.RefRange = "22-34um/ml";
            oneRes.Result = "5";
            oneRes.Seq = String.valueOf(k);
            oneRes.TestCodeName = "项目"+k;
            repResList.add(oneRes);
        }

        //画图片元素
        PrintImage(template, retList, printImgMap);
        //画数据元素
        PrintData(template, retList, printDataMap);
        //画标签元素
        PrintLabel(template, retList);
        //画线元素
        PrintLine(template, retList);
        //画表格元素
        PrintDataGrid(template, retList, repResList);
        return Helper.Object2Json(retList);
    }

    /**
     * 输出报告结果
     *
     * @param template
     * @param template   模板
     * @param retList    元素列表
     * @param repResList 报告结果列表
     */
    private void PrintDataGrid(JRTPrintTemplateDto template, List<PrintElement> retList, List<Object> repResList) throws Exception {
        for (JRTPrintTemplateEle ele : template.EleList) {
            //输出表格
            if (ele.PrintType.equals("DataGrid")) {
                //得到表格定义
                GridDto defGrid = (GridDto) Helper.Json2Object(ele.PrintText, GridDto.class);
                int lastY = PrintDrawProtocol.DrawGrid(retList, defGrid, null, repResList, "ReportRes", this);
            }
        }
    }

    /**
     * 输出图片
     *
     * @param template
     * @param template     模板
     * @param retList      元素列表
     * @param printDataMap 数据哈希
     */
    private void PrintImage(JRTPrintTemplateDto template, List<PrintElement> retList, Hashtable<String, String> printDataMap) {
        for (JRTPrintTemplateEle ele : template.EleList) {
            //输出图片
            if (ele.PrintType.equals("Graph")) {
                boolean isSign = false;
                if (ele.PrintFlag.equals("D")) {
                    isSign = true;
                }
                String dataField = ele.DataField;
                if (!ele.DataField.isEmpty() && printDataMap.containsKey(ele.DataField)) {
                    dataField = printDataMap.get(ele.DataField);
                }
                PrintElement printEle = PrintDrawProtocol.DrawGraph(ele.PrintX, ele.PrintY, ele.PrintWidth, ele.PrintHeight, dataField, isSign, false);
                retList.add(printEle);
            }
        }
    }

    /**
     * 输出数据
     *
     * @param template     模板
     * @param retList      元素列表
     * @param printDataMap 数据哈希
     */
    private void PrintData(JRTPrintTemplateDto template, List<PrintElement> retList, Hashtable<String, String> printDataMap) {
        for (JRTPrintTemplateEle ele : template.EleList) {
            //输出标签
            if (ele.PrintType.equals("Data")) {
                String dataFiled = ele.DataField;
                if (!dataFiled.isEmpty() && printDataMap.containsKey(dataFiled)) {
                    //带字的条码
                    if (ele.PrintFlag.contains("BarCode")) {
                        String BarType = ele.PrintFlag.replace("BarCode", "");
                        PrintElement printEle = PrintDrawProtocol.DrawBarCode(ele.PrintX, ele.PrintY, printDataMap.get(dataFiled), true, ele.PrintWidth, ele.PrintHeight, BarType, ele.PrintFont, Convert.ToDouble(ele.PrintFontSize).intValue(), ele.PrintFontStyle, PrintDrawProtocol.GetPrintAlignment(ele.PrintAlignment), ele.Color, ele.Angle);
                        retList.add(printEle);
                    }
                    //不带字的条码
                    else if (ele.PrintFlag.contains("BarCodeN")) {
                        String BarType = ele.PrintFlag.replace("BarCodeN", "");
                        PrintElement printEle = PrintDrawProtocol.DrawBarCode(ele.PrintX, ele.PrintY, printDataMap.get(dataFiled), false, ele.PrintWidth, ele.PrintHeight, BarType, ele.PrintFont, Convert.ToDouble(ele.PrintFontSize).intValue(), ele.PrintFontStyle, PrintDrawProtocol.GetPrintAlignment(ele.PrintAlignment), ele.Color, ele.Angle);
                        retList.add(printEle);
                    }
                    //画普通数据
                    else {
                        PrintElement printEle = PrintDrawProtocol.DrawLabel(ele.PrintX, ele.PrintY, printDataMap.get(dataFiled), ele.PrintFont, Convert.ToDouble(ele.PrintFontSize).intValue(), ele.PrintFontStyle, PrintDrawProtocol.GetPrintAlignment(ele.PrintAlignment), ele.Color, "", ele.Angle);
                        retList.add(printEle);
                    }
                }
            }
        }
    }

    /**
     * 输出标签
     *
     * @param template
     * @param retList
     */
    private void PrintLabel(JRTPrintTemplateDto template, List<PrintElement> retList) {
        for (JRTPrintTemplateEle ele : template.EleList) {
            //输出标签
            if (ele.PrintType.equals("Label")) {
                PrintElement printEle = PrintDrawProtocol.DrawLabel(ele.PrintX, ele.PrintY, ele.PrintText, ele.PrintFont, Convert.ToDouble(ele.PrintFontSize).intValue(), ele.PrintFontStyle, PrintDrawProtocol.GetPrintAlignment(ele.PrintAlignment), ele.Color, "", ele.Angle);
                retList.add(printEle);
            }
        }
    }

    /**
     * 输出线
     *
     * @param template
     * @param retList
     */
    private void PrintLine(JRTPrintTemplateDto template, List<PrintElement> retList) {
        for (JRTPrintTemplateEle ele : template.EleList) {
            //输出先
            if (ele.PrintType.equals("ILineN")) {
                int width = ele.PrintWidth;
                int height = ele.PrintHeight;
                int endX = ele.PrintX + width;
                int endY = ele.PrintY;
                int lineWidth = height;
                if (height > width) {
                    endX = ele.PrintX;
                    endY = ele.PrintY;
                    lineWidth = width;
                }
                PrintElement printEle = PrintDrawProtocol.DrawLine(ele.PrintX, ele.PrintY, endX, endY, lineWidth, ele.PrintFlag, ele.Color);
                retList.add(printEle);
            }
        }
    }

    /**
     * 通过模板主键或者代码得到模板数据供打印使用
     *
     * @param rowID
     * @param code
     * @return
     */
    private JRTPrintTemplateDto GetOneTemplate(Integer rowID, String code) throws Exception {
        JRTPrintTemplateDto retDto = null;
        //按主键取
        if (rowID != null) {
            retDto = EntityManager().GetById(JRTPrintTemplateDto.class, rowID);
        }
        //按代码取
        else if (code != null && !code.isEmpty()) {
            retDto = EntityManager().GetByColVal(JRTPrintTemplateDto.class, "Code", code);
        }
        if (retDto != null) {
            HashParam hs = new HashParam();
            hs.Add("PrintTemplateDR", retDto.RowID);
            retDto.EleList = EntityManager().FindAllSimple(JRTPrintTemplateEle.class, hs);
        }
        return retDto;
    }

    /**
     * 存获得的一个模板数据
     */
    public static class JRTPrintTemplateDto extends JRTPrintTemplate {
        /**
         * 模板元素
         */
        public List<JRTPrintTemplateEle> EleList;
    }

    /**
     * 打印的报告结果实体
     */
    public static class RepResPrintDto {
        /**
         * 序号
         */
        public String Seq;

        /**
         * 项目名称
         */
        public String TestCodeName;

        /**
         * 结果
         */
        public String Result;

        /**
         * 标志
         */
        public String AbFlag;

        /**
         * 参考范围
         */
        public String RefRange;
    }
}

这里表格元素设计了表格换页接口,换页时候会回调接口实现方法,来达到表格换页前后执行一些画笔操作。

这种逻辑简化量在M时代是不可想象的

你可能感兴趣的:(java)