springBoot项目--平台控制商品订单中各商家打印机打印小票!

@Configuration
public class GoodsOrderPrintScheduling {
    private static final Logger logger = LoggerFactory.getLogger(GoodsOrderPrintScheduling.class);

    @Value("${device.print.url}")
    public String url = "";

    @Autowired
    private IShopOrderService shopOrderService;
    @Autowired
    private IOrderGoodsService orderGoodsService;
    @Autowired
    private IGoodsService goodsService;
    @Autowired
    private IUserPropService userPropService;
    @Autowired
    private IPlatformDevicesService platformDevicesService;
    @Autowired
    private IShopDevicesService shopDevicesService;
    @Autowired
    private IUserService  userService;
/**
     * 待付款订单,打印给商家端
     */
    @Scheduled(cron = "0/5 * * * * ?")
    public  void shopCancelOrder(){
        logger.info("开始打印商家端订单小票");
        QueryWrapper<ShopOrder> queryWrapper = new QueryWrapper<ShopOrder>();
        queryWrapper.eq("status", 2);             //待发货
        queryWrapper.eq("print_status", 0);       //打印状态0:未打印
        List<ShopOrder> shopOrderList = shopOrderService.list(queryWrapper);  //得到订单表


        for (ShopOrder shopOrder : shopOrderList) {// 订单表
            String content ="";
            QueryWrapper<OrderGoods> queryOrderGoodsWrapper = new QueryWrapper<OrderGoods>();
            queryOrderGoodsWrapper.eq("order_id", shopOrder.getId());                   //订单id
            List<OrderInfo> orderInfoList = orderGoodsService.selectShopIdAndOther(shopOrder.getId());  //可以得到商品id  的


            for (int i =0 ; i< orderInfoList.size();) { //货物
                //只打印该商家的订单信息
                //拿到商家id   查看 商家打印机
                int num = 1;
                OrderInfo orderInfo = orderInfoList.get(i);
                Goods goods = goodsService.getById(orderInfo.getGoodsId());
                Long shopId = goods.getShopId(); //取到  订单商店id
                SysUser sysUser = userService.selectByPrimaryKey(shopId);
                int flag = 0;
                if(flag == 0){
                    content = ""+sysUser.getShopName()+"
"
; content += "序号 名称      单价 数量 "; content += "--------------------------------
"
; flag++; } 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)){ content += num + " " + cutString(goods1.getName()) + " " + goods1.getPrice() + " " + orderInfo1.getBuyNum() + "
"
; num++; i++; }else{ continue; } } content += num + " " + cutString(goods.getName()) + " " + goods.getPrice() + " " + orderInfo.getBuyNum() + "
"
; num++; i++; content += "备注:" + shopOrder.getBak() + "
"
; content += "--------------------------------
"
; content += " 合计: " + shopOrder.getPrice() + "元
"
; UserProp prop = userPropService.getById(shopOrder.getAddressId()); content += "订单编号:" + shopOrder.getOrderNo() + "
"
; content += "送货地点:" + prop.getAddress() + "
"
; content += "收货人: " + prop.getUserName() + "
"
; content += "联系电话:" + prop.getPhone() + "
"
; content += "下单时间:" + shopOrder.getCreatetime() + "
"
; QueryWrapper<ShopDevices> queryShopDevicesWrapper = new QueryWrapper<ShopDevices>(); queryShopDevicesWrapper.eq("status", 1); queryShopDevicesWrapper.eq("shop_id", shopId); List<ShopDevices> shopDevicesList = shopDevicesService.list(queryShopDevicesWrapper); for (ShopDevices shopDevices : shopDevicesList) { 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)) { shopOrder.setPrintStatus(1); shopOrderService.updateById(shopOrder); logger.info(content); logger.info("商家端打印成功"); } else { logger.error("商家端打印失败:" + jsonObject1); } } else { logger.error("商家端打印失败:" + jsonObject); } } catch (IOException e) { logger.error("商家端打印失败:" + e.getMessage()); } } else { logger.error("商家端打印失败,打印机未开启!"); } } } } logger.info("商家端打印结束"); } /** * 切割字符串 * * @param str * @return */ public static String cutString(String str) { String result = str_split(str, 10, "
"
); // 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)); }

效果如图

springBoot项目--平台控制商品订单中各商家打印机打印小票!_第1张图片


你可能感兴趣的:(Spring,boot)