之前写过一个打印机的一段代码,由于之前的代码存在缺陷,现在我来做下补充:
直接上干货:
@Scheduled(cron = "0/5 * * * * ?")public void shopCancelOrder(){logger.info("开始打印商家端订单小票");QueryWrapper<ShopOrder> queryWrapper = new QueryWrapper<ShopOrder>();queryWrapper.eq("status", 2);//待发货queryWrapper.eq("is_del",2);//未删除的List<ShopOrder> shopOrderList = shopOrderService.list(queryWrapper); //得到订单表for (ShopOrder shopOrder : shopOrderList) {// 遍历订单表String content ="";List<OrderInfo> orderInfoList = orderGoodsService.selectShopIdAndOther(shopOrder.getId()); //只搜索打印状态为0的 ,可以得到商品id的for (int i =0 ; i< orderInfoList.size();) {int num = 1;List<OrderInfo> orListChange = new ArrayList<>();BigDecimal totalPrice = new BigDecimal(0);BigDecimal totalPrice2 = new BigDecimal(0);BigDecimal totalPriceTotal = new BigDecimal(0);OrderInfo orderInfo = orderInfoList.get(i);Goods goods = goodsService.getById(orderInfo.getGoodsId());Long shopId = goods.getShopId();int returnCount = orderGoodsService.selectByShopIdReturnCount(shopOrder.getId(), shopId);SysUser sysUser = userService.selectByPrimaryKey(shopId);int flag = 0;if(flag == 0){if(sysUser!=null){content = "<CB>"+sysUser.getShopName()+"</CB><BR>";content += "序号 名称 单价 数量 ";content += "--------------------------------<BR>";flag++;}else{logger.error("商家端打印失败:无此商家" );}}for (int j =i+1 ; j< orderInfoList.size(); j++){OrderInfo orderInfo1 = orderInfoList.get(j);Goods goods1 = goodsService.getById(orderInfo1.getGoodsId());Long shopId1 = goods1.getShopId();if(shopId1.equals(shopId) && goods1 != null && orderInfo1.getShopPrint()==0 ){content += num + " " + cutString(goods1.getName()+"/"+goods.getStardardName()) + " " + orderInfo1.getUnitPrice()+ " " + orderInfo1.getBuyNum() + "<BR>";num++;i++;orListChange.add(orderInfo1);//System.out.println("--------------加入orListChange的orderInfo为shopid->"+orderInfo1.getShopId()+"商品id为"+orderInfo1.getGoodsId()+"----------------");totalPrice = totalPrice.add(orderInfo1.getUnitPrice().multiply(new BigDecimal(orderInfo1.getBuyNum())));}else{continue; }}if(goods != null ){if( orderInfo.getShopPrint()==0){content += num + " " + cutString(goods.getName()+"/"+goods.getStardardName()) + " " + orderInfo.getUnitPrice()+ " " + orderInfo.getBuyNum() + "<BR>";num++;i++;orListChange.add(orderInfo);//System.out.println("--------------加入orListChange的orderInfo为shopid->"+orderInfo.getShopId()+"商品id为"+orderInfo.getGoodsId()+"----------------");totalPrice2 = totalPrice2.add(orderInfo.getUnitPrice().multiply(new BigDecimal(orderInfo.getBuyNum())));}}else{clearAllCont(totalPrice,totalPrice2,totalPriceTotal,orListChange);logger.error("商家端打印失败:无此商品 -> id: " + goods.getId() );}content += "备注:" + shopOrder.getBak() + "<BR>";content += "--------------------------------<BR>";totalPriceTotal= totalPrice.add(totalPrice2);content += " 合计: " + totalPriceTotal + "元<BR>";UserProp prop = userPropService.getById(shopOrder.getAddressId());content += "订单编号:" + shopOrder.getOrderNo() + "<BR>";content += "送货地点:" + prop.getAddress() + "<BR>";content += "收货人: " + prop.getUserName() + "<BR>";content += "联系电话:" + prop.getPhone() + "<BR>";content += "下单时间:" + shopOrder.getCreatetime() + "<BR>";QueryWrapper<ShopDevices> queryShopDevicesWrapper = new QueryWrapper<ShopDevices>();queryShopDevicesWrapper.eq("status", 1);queryShopDevicesWrapper.eq("shop_id", shopId);List<ShopDevices> shopDevicesList = shopDevicesService.list(queryShopDevicesWrapper);for (int k = 0; k<shopDevicesList.size(); k++) {ShopDevices shopDevices = shopDevicesList.get(k);if (shopDevices.getStatus() == 1) {try {JSONObject jsonObject = PrintDeviceUtil.print(url, shopDevices.getDeviceNo(),shopDevices.getMkey(), content, shopDevices.getTimes() + "");String responseCode = jsonObject.getString("responseCode");if ("0".equals(responseCode)) {//获取打印设备的订单索引String orderindex = jsonObject.getString("orderindex");JSONObject jsonObject1 = PrintDeviceUtil.printStatus(url, shopDevices.getDeviceNo(),shopDevices.getMkey(), orderindex);String responseCode1 = jsonObject1.getString("responseCode");if ("0".equals(responseCode1)) {if(orListChange != null){//System.out.println("-----------------------"+orListChange.size()+"------------------------------------");if( returnCount == orListChange.size() ){///int count=0;for(OrderInfo orderInfo2:orListChange){if(orderInfo2.getShopPrint() == 0 ){count += orderGoodsService.updateByInfoId(orderInfo2.getOrderId(), orderInfo2.getGoodsId());}else{clearAllCont(totalPrice,totalPrice2,totalPriceTotal,orListChange);logger.error("商家端打印失败:订单中商品部分已经打印!" );}}if(count == orListChange.size()){/* System.out.println("====打印完之后的小票打印状态====" + orderGoodsService.getById(orderInfo.getId()).getShopPrint());System.out.println("====控制台输出订单打印内容===="+content);*/clearAllCont(totalPrice,totalPrice2,totalPriceTotal,orListChange);logger.info(content);logger.info("商家端打印成功");}else{if(k==shopDevicesList.size()){clearAllCont(totalPrice,totalPrice2,totalPriceTotal,orListChange);}logger.error("商家端打印失败:订单中商品未全部成功修改打印状态!" );}}else{if(k==shopDevicesList.size()){clearAllCont(totalPrice,totalPrice2,totalPriceTotal,orListChange);}logger.error("商家端打印失败:订单中商品未全部录入待打印状态!" );}}else{if(k==shopDevicesList.size()){clearAllCont(totalPrice,totalPrice2,totalPriceTotal,orListChange);}logger.error("商家端打印失败:无符合订单!" );}} else {if(k==shopDevicesList.size()){clearAllCont(totalPrice,totalPrice2,totalPriceTotal,orListChange);}logger.error("商家端打印失败:" + jsonObject1);}} else {if(k==shopDevicesList.size()){clearAllCont(totalPrice,totalPrice2,totalPriceTotal,orListChange);}logger.error("商家端打印失败:" + jsonObject);}} catch (IOException e) {if(k==shopDevicesList.size()){clearAllCont(totalPrice,totalPrice2,totalPriceTotal,orListChange);}logger.error("商家端打印失败:" + e.getMessage());}} else {if(k==shopDevicesList.size()){clearAllCont(totalPrice,totalPrice2,totalPriceTotal,orListChange);}logger.error("商家端打印失败,打印机未开启!");}}}}logger.info("商家端打印结束");}public static void clearAllCont(BigDecimal totalPrice,BigDecimal totalPrice2,BigDecimal totalPriceTotal,List<OrderInfo> orListChange){orListChange.clear();totalPrice = BigDecimal.ZERO;totalPrice2 = BigDecimal.ZERO;totalPriceTotal = BigDecimal.ZERO;}public static String cutString(String str) {String result = str_split(str, 10, "<BR>");// System.out.println(result);return result;}public static String[] str_split(String str, int length) {int len = str.length();String[] arr = new String[(len + length - 1) / length];for (int i = 0; i < len; i += length) {int n = len - i;if (n > length) {n = length;}if(i > 0){arr[i / length] = " " + str.substring(i, i + n);}else{arr[i / length] = str.substring(i, i + n);}}while (arr[arr.length - 1].length() < 15){arr[arr.length - 1] += " ";}return arr;}public static String str_split(String str, int length, CharSequence delimiter) {return String.join(delimiter, str_split(str, length));}