• 04 后端开发总结


    04 后端开发总结

    调用小票打印机

    首先需要在本地完成打印机的驱动安装,建议到相应官网进行。

    1. 创建要打印的文本内容类,我这里是小票类

    可以继承某个你原本的数据类,然后需要实现Printable接口,在里面自行编写打印函数print()

    @Data
    public class SalesTicket extends PreCheckOutVo implements Printable {
        private String shuliang;
        private String heji;
    
        public SalesTicket(PreCheckOutVo preCheckOutVo){
            Long num = 0l;
            List<OrderDishesBo> orderDishesBoList = preCheckOutVo.getDishes();
    //        System.out.println("getDishes"+orderDishesBoList);
            for (OrderDishesBo ob: orderDishesBoList
            ) {
                num += ob.getNum();
            }
            Double price = 0.0;
            for(OrderDishesBo ob:orderDishesBoList){
                price += ob.getPrice();
            }
    
    
            this.setShuliang(num.toString());
            this.setHeji(price.toString());
            this.setTotalPrice(preCheckOutVo.getTotalPrice());
            this.setCreateTime(preCheckOutVo.getCreateTime());
            this.setDiscount(preCheckOutVo.getDiscount());
            this.setDishes(preCheckOutVo.getDishes());
            this.setOrderId(preCheckOutVo.getOrderId());
            this.setPeopleNum(preCheckOutVo.getPeopleNum());
            this.setTableNo(preCheckOutVo.getTableNo());
            this.setTime(preCheckOutVo.getTime());
        }
    
    
        /**
         * 打印方法
         * graphics - 用来绘制页面的上下文,即打印的图形
         * pageFormat - 将绘制页面的大小和方向,即设置打印格式,如页面大小一点为计量单位(以1/72 英寸为单位,1英寸为25.4毫米。A4纸大致为595 × 842点)
         * 小票纸宽度一般为58mm,大概为165点
         * pageIndex - 要绘制的页面从 0 开始的索引 ,即页号
         */
        @Override
        public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException {
            //此 Graphics2D 类扩展 Graphics 类,以提供对几何形状、坐标转换、颜色管理和文本布局更为复杂的控制。
            //它是用于在 Java(tm) 平台上呈现二维形状、文本和图像的基础类。
            Graphics2D g2 = (Graphics2D) graphics;
    
            g2.setColor(Color.black);//设置打印颜色为黑色
    
            //打印起点坐标
            double x= pageFormat.getImageableX();  //返回与此 PageFormat相关的 Paper对象的可成像区域左上方点的 x坐标。
            double y= pageFormat.getImageableY();  //返回与此 PageFormat相关的 Paper对象的可成像区域左上方点的 y坐标。
    
    
            //Font.PLAIN: 普通样式常量   Font.ITALIC 斜体样式常量  Font.BOLD 粗体样式常量。
            Font font = new Font("宋体",Font.BOLD,10); //根据指定名称、样式和磅值大小,创建一个新 Font。
    
            g2.setFont(font);//设置标题打印字体
    
            float heigth = font.getSize2D();//获取字体的高度
    
            //设置小票的标题标题
            g2.drawString("食尚年华石锅土鲫鱼",(float)x+25,(float)y+heigth);
            float line = 2*heigth; //下一行开始打印的高度
            g2.drawString("预结单",(float)x+50,(float)y+line+10);
    
            g2.setFont(new Font("宋体", Font.PLAIN,8));//设置正文字体
            heigth = font.getSize2D();// 字体高度
    
            line+=15;
            //设置订单号
            g2.drawString("订单号:"+this.getOrderId(),(float)x+10,(float)y+line);
            line+=heigth;
    
            //设置桌号
            g2.drawString("桌号:"+this.getTableNo(), (float)x+10,(float)y+line);
            line+=heigth;
    
            //设置人数
            g2.drawString("就餐人数:"+this.getPeopleNum(),(float)x+10,(float)y+line);
            line+=heigth;
    
            //设置下单时间
            g2.drawString("下单时间:"+this.getCreateTime(),(float)x+10,(float)y+line);
            line+=heigth+2;
    
    //设置标题
    
            /*
             * 虚线绘制设置    setStroke(Stroke):为 Graphics2D 上下文设置 Stroke
             * 由 BasicStroke定义的呈现属性描述了用画笔沿 Shape 的轮廓绘制的某个标记的形状,以及应用在 Shape 路径线段的末端和连接处的装饰。
             * 这些呈现属性包括:
             * width:画笔的宽度,是垂直于画笔轨迹的测量值。  此宽度必须大于或等于 0.0f,0.0f为最细线条。
             * end caps:在未封闭子路径和虚线线段的末端应用的一些装饰。如果子路径没有 CLOSE 段,则在同一点上开始和结束的子路径仍被认为是未封闭的。
             * 关于 CLOSE 段的更多信息,请参阅 SEG_CLOSE。三个不同的装饰是:
             * CAP_BUTT:无装饰地结束未封闭的子路径和虚线线段。
             * CAP_ROUND:使用半径等于画笔宽度一半的圆形装饰结束未封闭的子路径和虚线线段。
             * CAP_SQUARE:使用正方形结束未封闭的子路径和虚线线段,正方形越过线段端点,并延长等于线条宽度一半的距离。
             * line joins:在两个路径线段的交汇处,以及使用 SEG_CLOSE 封闭的子路径端点的交汇处应用的装饰。
             * 三个不同的装饰是:
             * JOIN_BEVEL:通过直线连接宽体轮廓的外角,将路径线段连接在一起。
             * JOIN_MITER:扩展路径线段的外边缘,直到它们连接在一起。
             * JOIN_ROUND:通过舍去半径为线长的一半的圆角,将路径线段连接在一起。
             * miter limit:对剪裁具有 JOIN_MITER 装饰的线接合点的限制。当斜接长度与笔划宽度的比大于 miterlimit 值时,需要剪裁线接合点。
             * 斜接长度是斜接的对角线长度,即交汇处的内棱角和外棱角之间的距离。两条线段形成的角度越小,斜接长度就越长,交汇处的角度就越尖锐。
             * 默认 miterlimit 值为 10.0f,它会使所有小于 11 度的角都被剪裁。剪裁斜接会使线接合点的装饰变成斜角。 必须大于或等于 1.0f。
             * dash attributes:关于如何通过在不透明和透明部分之间交替形成一个虚线模式的定义。 表示虚线模式的数组
             * dash phase - 开始虚线模式的偏移量
             */
    //虚线设置
            g2.setStroke(new BasicStroke(1f,BasicStroke.CAP_BUTT,BasicStroke.JOIN_MITER,4.0f,new float[]{4.0f},0.0f));
    //在此图形上下文的坐标系中使用当前颜色在点 (x1, y1) 和 (x2, y2) 之间画一条线。 即绘制虚线
            g2.drawLine((int) x, (int)(y+line), (int) x + 158, (int)(y+line));
            line += heigth+2;
            g2.drawString("菜品",(float)x+10, (float)y+line);
            g2.drawString("单价",(float)x+50, (float)y+line);
            g2.drawString("数量",(float)x+80, (float)y+line);
            g2.drawString("小计",(float)x+110, (float)y+line);
            line+=heigth;
    
            g2.drawLine((int)x,(int)(y+line),(int)x+158,(int)(y+line));
            line+=heigth;
    //设置商品清单
            List<OrderDishesBo> orderDishesBoList = this.getDishes();
            if(orderDishesBoList!=null && orderDishesBoList.size()>0){
                for(OrderDishesBo gdf:orderDishesBoList){
                    g2.drawString(gdf.getDishes().getDisName(),(float)x+5, (float)y+line);
                    g2.drawString(String.valueOf(gdf.getUnitPrice()),(float)x+50, (float)y+line);
                    g2.drawString(String.valueOf(gdf.getNum()),(float)x+85,(float)y+line);
                    g2.drawString(String.valueOf(gdf.getPrice()),(float)x+110,(float)y+line);
                    line += heigth;
                }
            }
            g2.drawLine((int) x, (int)(y+line), (int) x + 158, (int)(y+line));
            line += heigth+2;
    
            g2.drawString("商品数量:"+this.shuliang+"件",(float)x+10,(float)y+line);
            g2.drawString("合计:"+this.heji+" 元", (float)x+80, (float)y+line);
    
            line += heigth;
            g2.drawLine((int) x, (int)(y+line), (int) x + 158, (int)(y+line));
            line += heigth+2;
    
            g2.drawString("优惠信息:"+this.getDiscount().getDiscountName(),(float)x+10,(float)y+line);
            line+=heigth;
            g2.drawString("优惠金额:"+this.getDiscount().getDiscountPrice().toString()+" 元", (float)x+10, (float)y+line);
            line += heigth;
    //        line += heigth;
            g2.drawLine((int) x, (int)(y+line), (int) x + 158, (int)(y+line));
            line += heigth+2;
    
            g2.drawString("应收:"+this.getTotalPrice().toString()+"元",(float)x+10,(float)y+line);
            line += heigth;
    
    
            g2.drawString("时间:"+this.getTime(),(float)x+10,(float)y+line);
            line += heigth;
    //        line += heigth;
            g2.drawLine((int) x, (int)(y+line), (int) x + 158, (int)(y+line));
            line += heigth+2;
    
            g2.drawString("食尚年华石锅土鲫鱼欢迎您的光临!",(float)x+5,(float)y+line);
            line += heigth+2;
            g2.drawString("联系电话:13368330888",(float)x+15,(float)y+line);
            line += heigth+2;
            g2.drawString("地址:重庆市沙坪坝区渝磅路街道",(float)x+5,(float)y+line);
            line += heigth+2;
            g2.drawString("站西路88号附84、85、86号",(float)x+10,(float)y+line);
    
            switch (pageIndex) {
                case 0:
                    return PAGE_EXISTS;  //0
                default:
                    return NO_SUCH_PAGE;   //1
            }
        }
    }
    
    • 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
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175

    2. 构造打印机类,在其中包含打印内容类

    需要写一下打印机类内容,设定纸张文档格式等。

    package group.mngzs.orderingsystemback.commonutils;
    
    import java.awt.print.*;
    
    public class MngzsPrint {
        private SalesTicket salesTicket;
    
        public MngzsPrint(SalesTicket salesTicket){
            this.salesTicket=salesTicket;
        }
    
        public void printer() {
            try {
                //Book 类提供文档的表示形式,该文档的页面可以使用不同的页面格式和页面 painter
                Book book = new Book(); //要打印的文档
    
                //PageFormat类描述要打印的页面大小和方向
                PageFormat pf = new PageFormat();  //初始化一个页面打印对象
                pf.setOrientation(PageFormat.PORTRAIT); //设置页面打印方向,从上往下,从左往右
    
                //设置打印纸页面信息。通过Paper设置页面的空白边距和可打印区域。必须与实际打印纸张大小相符。
                Paper paper = new Paper();
                paper.setSize(158,30000);// 纸张大小
                paper.setImageableArea(0,0,158,30000);// A4(595 X 842)设置打印区域,其实0,0应该是72,72,因为A4纸的默认X,Y边距是72
                pf.setPaper(paper);
    
                book.append(salesTicket,pf);
    
                PrinterJob job = PrinterJob.getPrinterJob();   //获取打印服务对象
    
                job.setPageable(book);  //设置打印类
    
                job.print(); //开始打印
            } catch (PrinterException e) {
                e.printStackTrace();
            }
        }
    }
    
    • 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

    3. 调用方法传递数据即打印

    image-20220706090748248

    定时任务

    注意使用@component注解

    @Component
    public class ScheduledService {
        //在一个特定的时间执行这个方法~  Timer
    
        //Cron表达式
        //秒  分  时  日  月  周几~
        /*
            * /5 * * * * ? 每隔五秒
            30 15 10 * * ?   每天10点15分30秒执行一次
            30 0/5 10,18 * * ?    每天10点到18点,每个五分钟执行一次
            0 15 10 ? * 1-6    每个月的周一到周六 10点15分执行一次
        */
        @Scheduled(cron = "0 45 11 * * ?")
        public void hello() {
            System.out.println("十一点四十五啦~");
        }
    
        // 每天凌晨1点自动新建当天账单
        @Scheduled(cron = "0 0 1 * * ?")
        public void createTodayBill(){
    
        }
    
        //  每天凌晨2点补充材料计算价格
        @Scheduled(cron = "0 0 2 * * ?")
        public void calcMatPrice(){
            
        }
    
    }
    
    • 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

    时间类处理java.sql.timestamp等

    获取过去七天日期

    package group.mngzs.orderingsystemback.commonutils;
    
    import java.sql.Timestamp;
    import java.text.SimpleDateFormat;
    import java.util.Calendar;
    import java.util.Date;
    
    public class TimeUtils {
        public static Boolean validDate(Timestamp nowTime, Timestamp startTime, Timestamp endTime) {
            return nowTime.compareTo(startTime) >= 0 && nowTime.compareTo(endTime) <= 0;
        }
        public static String getPastDate(int past){
            Calendar calendar = Calendar.getInstance();
            calendar.set(Calendar.DAY_OF_YEAR,calendar.get(Calendar.DAY_OF_YEAR)-past);
            Date today = calendar.getTime();
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            String result = format.format(today);
            return result;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    一系列时间函数

        /**
         * 得到当前时间string
         */
        public static String getNowTimeYMD(){
            //修改时间
            Date date = new Date();
            SimpleDateFormat dateString = new SimpleDateFormat("yyyy-MM-dd");
            String stringDate = dateString.format(date);
            return stringDate;
        }
    
    
        public static String toString(Timestamp t){
            //修改时间
            Date date = t;
            SimpleDateFormat dateString = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String stringDate = dateString.format(date);
            return stringDate;
        }
    
        public static String getNowTime(){
            //修改时间
            Date date = new Date();
            SimpleDateFormat dateString = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String stringDate = dateString.format(date);
            return stringDate;
        }
    
        public static Timestamp getNowTimeStamp(){
            Date date = new Date();
            Timestamp t = new Timestamp(date.getTime());
            return t;
        }
    
    • 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
     SimpleDateFormat dateString = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String stringDate = dateString.format(date);
        return stringDate;
    }
    
    public static Timestamp getNowTimeStamp(){
        Date date = new Date();
        Timestamp t = new Timestamp(date.getTime());
        return t;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    
    
    • 1
  • 相关阅读:
    S2B2C平台协同管理业务详解 | 数商云S2B2C系统赋能新能源汽车行业高价值增长
    【获奖论文】2023年数学建模国赛优秀获奖论文
    Java反序列化和JNDI注入
    并发编程之 ThreadLocal
    flutter Package 打包上传插件
    关于Flask_自定义路由转换器的方法和使用
    动态规划:区间动态规划
    《 Python List 列表全实例详解系列(七)》__索引和切片
    比特币通用API服务
    Vue_Bug error0308010Cdigital envelope routinesunsupported
  • 原文地址:https://blog.csdn.net/Tgmmmmmmmm/article/details/125632570