From f2615eb96ff813e6693ea5a6dc2db419ade5b7d7 Mon Sep 17 00:00:00 2001 From: Jack <46790855@qq.com> Date: Fri, 1 Nov 2024 17:17:54 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8F=91=E7=A5=A8=E6=89=93=E5=8D=B0=E6=A8=A1?= =?UTF-8?q?=E7=89=88=E8=B0=83=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../common/pojo/dto/ProductSimpleInfo.java | 17 ++ .../mall/common/utils/DateTimeUtils.java | 14 + .../admin/ShopStorePrinterController.java | 12 + .../service/ShopStorePrinterService.java | 13 + .../impl/ShopStorePrinterServiceImpl.java | 190 +++++++++--- .../mall/shop/store/utis/FeieUtil.java | 281 ++++++++++++++++-- 6 files changed, 468 insertions(+), 59 deletions(-) create mode 100644 mall-common/src/main/java/com/suisung/mall/common/pojo/dto/ProductSimpleInfo.java create mode 100644 mall-common/src/main/java/com/suisung/mall/common/utils/DateTimeUtils.java diff --git a/mall-common/src/main/java/com/suisung/mall/common/pojo/dto/ProductSimpleInfo.java b/mall-common/src/main/java/com/suisung/mall/common/pojo/dto/ProductSimpleInfo.java new file mode 100644 index 00000000..6581783e --- /dev/null +++ b/mall-common/src/main/java/com/suisung/mall/common/pojo/dto/ProductSimpleInfo.java @@ -0,0 +1,17 @@ +package com.suisung.mall.common.pojo.dto; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +import java.io.Serializable; + +@Data +@NoArgsConstructor +@AllArgsConstructor +public class ProductSimpleInfo implements Serializable { + private String product_sn; + private String title; + private Integer num; + private String amount; +} \ No newline at end of file diff --git a/mall-common/src/main/java/com/suisung/mall/common/utils/DateTimeUtils.java b/mall-common/src/main/java/com/suisung/mall/common/utils/DateTimeUtils.java new file mode 100644 index 00000000..40788577 --- /dev/null +++ b/mall-common/src/main/java/com/suisung/mall/common/utils/DateTimeUtils.java @@ -0,0 +1,14 @@ +package com.suisung.mall.common.utils; + +import lombok.extern.slf4j.Slf4j; + +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; + +@Slf4j +public class DateTimeUtils { + + public static String formatDateTime(LocalDateTime localDateTime, String pattern) { + return localDateTime.format(DateTimeFormatter.ofPattern(pattern)); + } +} diff --git a/mall-shop/src/main/java/com/suisung/mall/shop/store/controller/admin/ShopStorePrinterController.java b/mall-shop/src/main/java/com/suisung/mall/shop/store/controller/admin/ShopStorePrinterController.java index ecf28263..dbd5c93c 100644 --- a/mall-shop/src/main/java/com/suisung/mall/shop/store/controller/admin/ShopStorePrinterController.java +++ b/mall-shop/src/main/java/com/suisung/mall/shop/store/controller/admin/ShopStorePrinterController.java @@ -20,6 +20,12 @@ public class ShopStorePrinterController { @Autowired private ShopStorePrinterService shopStorePrinterService; + @ApiOperation(value = "测试打印模版消息", notes = "测试打印模版消息") + @RequestMapping(value = "/print/order", method = {RequestMethod.POST}) + public CommonResult printOrder() { + return shopStorePrinterService.printOrderInfo(); + } + @ApiOperation(value = "门店打票机分页列表查询", notes = "门店打票机分页列表查询") @RequestMapping(value = "/page", method = {RequestMethod.GET}) public CommonResult shopStorePrinterPageList(@RequestParam(name = "keyword", defaultValue = "") String keyword, @@ -75,4 +81,10 @@ public class ShopStorePrinterController { public CommonResult deleteShopStorePrinter(@RequestParam(name = "printer_id" , required = true) Long printer_id) { return shopStorePrinterService.deleteShopStorePrinter(printer_id); } + + @ApiOperation(value = "清空打票机打印队列", notes = "清空打票机打印队列") + @RequestMapping(value = "/clear/print/queue", method = {RequestMethod.POST}) + public CommonResult clearShopPrinterQueue(@RequestParam(name = "printer_sn" , required = true) String printer_sn) { + return shopStorePrinterService.clearPrinterQueue(printer_sn); + } } \ No newline at end of file diff --git a/mall-shop/src/main/java/com/suisung/mall/shop/store/service/ShopStorePrinterService.java b/mall-shop/src/main/java/com/suisung/mall/shop/store/service/ShopStorePrinterService.java index f703bd4c..73fed204 100644 --- a/mall-shop/src/main/java/com/suisung/mall/shop/store/service/ShopStorePrinterService.java +++ b/mall-shop/src/main/java/com/suisung/mall/shop/store/service/ShopStorePrinterService.java @@ -42,4 +42,17 @@ public interface ShopStorePrinterService extends IBaseService * @return */ CommonResult deleteShopStorePrinter(Long printer_id); + + /** + * 请空打印机打印队列 + * @param printer_sn + * @return + */ + CommonResult clearPrinterQueue(String printer_sn); + + /** + * 测试打印订单 + * @return + */ + CommonResult printOrderInfo(); } diff --git a/mall-shop/src/main/java/com/suisung/mall/shop/store/service/impl/ShopStorePrinterServiceImpl.java b/mall-shop/src/main/java/com/suisung/mall/shop/store/service/impl/ShopStorePrinterServiceImpl.java index 8abe60c4..3ec85653 100644 --- a/mall-shop/src/main/java/com/suisung/mall/shop/store/service/impl/ShopStorePrinterServiceImpl.java +++ b/mall-shop/src/main/java/com/suisung/mall/shop/store/service/impl/ShopStorePrinterServiceImpl.java @@ -7,20 +7,27 @@ import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.suisung.mall.common.api.CommonResult; import com.suisung.mall.common.constant.ConstantError; +import com.suisung.mall.common.domain.UserDto; import com.suisung.mall.common.modules.store.ShopStorePrinter; +import com.suisung.mall.common.pojo.dto.ProductSimpleInfo; +import com.suisung.mall.common.pojo.vo.ShopStorePrinterVO; +import com.suisung.mall.common.utils.DateTimeUtils; +import com.suisung.mall.common.utils.TimeUtil; import com.suisung.mall.core.web.service.impl.BaseServiceImpl; import com.suisung.mall.shop.store.mapper.ShopStorePrinterMapper; import com.suisung.mall.shop.store.service.ShopStorePrinterService; -import com.baomidou.mybatisplus.extension.plugins.pagination.Page; -import com.suisung.mall.common.modules.store.ShopStorePrinter; -import com.suisung.mall.common.pojo.vo.ShopStorePrinterVO; import com.suisung.mall.shop.store.utis.FeieUtil; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; +import java.time.LocalDateTime; +import java.util.ArrayList; import java.util.Date; +import java.util.List; import java.util.Map; +import static com.suisung.mall.common.utils.ContextUtil.getCurrentUser; + @Service public class ShopStorePrinterServiceImpl extends BaseServiceImpl implements ShopStorePrinterService { @@ -35,33 +42,41 @@ public class ShopStorePrinterServiceImpl extends BaseServiceImpl page = new Page(); page.setCurrent(pageNum); page.setSize(pageSize); - return shopStorePrinterMapper.shopStorePrinterPageList(page,keyword); + return shopStorePrinterMapper.shopStorePrinterPageList(page, keyword); } @Override public CommonResult addNewShopStorePrinter(ShopStorePrinter record) { - //TODO 判断有没有权限删除 - record.setStore_id(1); - record.setCreated_by(1); - record.setUpdated_by(1); + //判断有没有权限 + UserDto user = getCurrentUser(); + if (!user.isStore()) { + return CommonResult.failed("没有门店权限!"); + } + + Integer userId = user.getId() == null ? 0 : user.getId(); + record.setStore_id(Integer.parseInt(user.getStore_id())); + record.setCreated_by(userId); + record.setUpdated_by(userId); + + // 判断打票机是否已经存在 QueryWrapper queryWrapper = new QueryWrapper<>(); queryWrapper.eq("store_id", record.getStore_id()); queryWrapper.eq("printer_sn", record.getPrinter_sn()); ShopStorePrinter existRecord = getOne(queryWrapper); - if (existRecord!=null && existRecord.getPrinter_id() > 0) { - if (existRecord.getFlag()== ConstantError.Disable2){ + if (existRecord != null && existRecord.getPrinter_id() > 0) { + if (existRecord.getFlag() == ConstantError.Disable2) { UpdateWrapper updateWrapper = new UpdateWrapper(); updateWrapper.eq("printer_id", existRecord.getPrinter_id()); - updateWrapper.set("updated_by",1); - updateWrapper.set("updated_at",new Date()); + updateWrapper.set("updated_by", userId); + updateWrapper.set("updated_at", new Date()); // 往厂商添加打印机 // "922441475#r6ZXPvHH#核销柜台"; - boolean success = feieUtil.addPrinter(String.format("%s#%s#%s", existRecord.getPrinter_sn(), existRecord.getPrinter_key(),existRecord.getPrinter_name())); - if(success) { - updateWrapper.set("flag",ConstantError.Enable); + boolean success = feieUtil.addPrinter(String.format("%s#%s#%s", existRecord.getPrinter_sn(), existRecord.getPrinter_key(), existRecord.getPrinter_name())); + if (success) { + updateWrapper.set("flag", ConstantError.Enable); } update(updateWrapper); @@ -74,12 +89,12 @@ public class ShopStorePrinterServiceImpl extends BaseServiceImpl queryWrapper2 = new QueryWrapper<>(); queryWrapper2.eq("store_id", record.getStore_id()); queryWrapper2.eq("printer_sn", record.getPrinter_sn()); ShopStorePrinter existRecord2 = getOne(queryWrapper2); - if (existRecord2==null || existRecord2.getPrinter_id()<=0){ + if (existRecord2 == null || existRecord2.getPrinter_id() <= 0) { // 打印机从未加入到厂家到情况 // 往厂商添加打印机 // "922441475#r6ZXPvHH#核销柜台"; - boolean success = feieUtil.addPrinter(String.format("%s#%s#%s", record.getPrinter_sn(), record.getPrinter_key(),record.getPrinter_name())); - if(success) { - updateWrapper.set("flag",ConstantError.Enable); + boolean success = feieUtil.addPrinter(String.format("%s#%s#%s", record.getPrinter_sn(), record.getPrinter_key(), record.getPrinter_name())); + if (success) { + updateWrapper.set("flag", ConstantError.Enable); } updateWrapper.set("printer_sn", record.getPrinter_sn()); } } - boolean success =update(updateWrapper); - if (success){ + boolean success = update(updateWrapper); + if (success) { return CommonResult.success(); } @@ -155,10 +176,14 @@ public class ShopStorePrinterServiceImpl extends BaseServiceImpl productList = new ArrayList<>(); + productList.add(order1); + productList.add(order2); + productList.add(order3); + productList.add(order4); + + String productsStr= feieUtil.genProductStr(productList,18,6,8); + + LocalDateTime now = LocalDateTime.now(); + String currentDateTime = DateTimeUtils.formatDateTime(now,"yyyy-MM-dd HH:mm:ss"); + + StringBuilder cont= new StringBuilder(); + cont.append("小发同城
"); +// cont.append("--------------------------------
"); +// cont.append("#000"+DateTimeUtils.formatDateTime(now,"ddHHmm")+"
"); +// cont.append("买家备注:不用敲门,放在门口旁边的外卖箱,打个电话告知送达就行,谢谢
"); +// cont.append("配送时间:2024-10-25 14:00-14:30
"); +// cont.append("--------------------------------
"); +// cont.append("订单编号:ES20231026111444527685
"); +// cont.append("订单来源:微信小程序
"); +// cont.append("支付方式:微信支付
"); +// cont.append("配送来源:同城配送
"); +// cont.append("付款时间:"+currentDateTime+"
"); +// cont.append("--------------------------------
"); + +// cont.append("商品名称 数量 金额
"); +// cont.append("可口可乐Coca_Cola
经典美味汽水1.25L/瓶
【x1】 5.54
"); +// cont.append("6970448170051
"); +// cont.append("
"); +// cont.append("排骨 约350g (默认砍小块) 【x5】 35.51
"); +// cont.append("69704481700541
"); +// cont.append("
"); +// cont.append("新鲜虫草花 1包 约200g 【x11】 687.52
"); +// cont.append("69704481700512
"); +// cont.append("
"); +// cont.append("冰红茶风味饮料x2;
五香瓜子x1
【x2】 13.26
"); +// cont.append("6970448170051
"); + + // 商品列表 + cont.append(productsStr); + +// cont.append("--------------------------------
"); +// cont.append("商品总件数:3
"); +// cont.append("商品总额:¥18.7
"); +// cont.append("押金:¥500
"); +// cont.append("运费:¥5.54
"); +// cont.append("会员权益:-¥50
"); +// cont.append("秒杀:-¥100
"); +// cont.append("实付金额:¥428.9元
"); +// cont.append("--------------------------------
"); +// cont.append("商家备注:老顾客赠送一箱牛奶;玻璃瓶包装轻拿轻放
"); +// cont.append("--------------------------------
"); +// cont.append("收货人:张三
"); +// cont.append("收货人手机号:13128778765
"); +// cont.append("收货地址:北京市朝阳区朝阳路朝阳人民小区1号楼1栋1101
"); +// cont.append("--------------------------------
"); +// cont.append("门店:岛内价生活超市
"); +// cont.append("门店电话:13665822542
"); +// cont.append("收银员:李小璐
"); + + feieUtil.printContent(sn,cont.toString()); + + return CommonResult.success(); + } + } diff --git a/mall-shop/src/main/java/com/suisung/mall/shop/store/utis/FeieUtil.java b/mall-shop/src/main/java/com/suisung/mall/shop/store/utis/FeieUtil.java index bf3965a3..7042b01d 100644 --- a/mall-shop/src/main/java/com/suisung/mall/shop/store/utis/FeieUtil.java +++ b/mall-shop/src/main/java/com/suisung/mall/shop/store/utis/FeieUtil.java @@ -2,6 +2,7 @@ package com.suisung.mall.shop.store.utis; import cn.hutool.core.collection.CollectionUtil; import cn.hutool.core.util.StrUtil; +import com.suisung.mall.common.pojo.dto.ProductSimpleInfo; import com.suisung.mall.common.pojo.res.FeiePrinterApiDataRes; import com.suisung.mall.common.pojo.res.FeiePrinterApiRes; import com.suisung.mall.common.utils.JsonUtil; @@ -97,7 +98,7 @@ public class FeieUtil { public boolean printContent(String sn, String printContent) { List nvps = new ArrayList(); nvps.add(new BasicNameValuePair("sn", sn)); - nvps.add(new BasicNameValuePair("printContent", printContent)); + nvps.add(new BasicNameValuePair("content", printContent)); FeiePrinterApiRes reps = sendHttpPost("Open_printMsg", nvps); if (reps != null && reps.getRet().equals(0)) { @@ -108,6 +109,25 @@ public class FeieUtil { return false; } + /** + * 清空打印机所有的打印队列 + * + * @param sn + * @return + */ + public boolean clearPrinterQueue(String sn) { + List nvps = new ArrayList(); + nvps.add(new BasicNameValuePair("sn", sn)); + + FeiePrinterApiRes reps = sendHttpPost("Open_delPrinterSqs", nvps); + if (reps != null && reps.getRet().equals(0)) { + return true; + } + + logger.error(reps.getMsg()); + return false; + } + /** * 生成签名字符串 @@ -168,27 +188,248 @@ public class FeieUtil { } catch (Exception e) { e.printStackTrace(); } finally { - try { - if (response != null) { - response.close(); - } - } catch (IOException e) { - e.printStackTrace(); - } - - try { - post.abort(); - } catch (Exception e) { - e.printStackTrace(); - } - - try { - httpClient.close(); - } catch (IOException e) { - e.printStackTrace(); - } + close(response, post, httpClient); } return null; } + + + // 以下是 util 方法 + //飞鹅技术支持 + //######################################################################################################### + //进行订单的多列排版demo,实现商品超出字数的自动换下一行对齐处理,同时保持各列进行对齐 + //排版原理是统计字符串字节数,补空格换行处理 + //58mm的机器,一行打印16个汉字,32个字母;80mm的机器,一行打印24个汉字,48个字母 + //######################################################################################################### + + //orderList为数组 title_len =18 代表名称列占用字节 num_len =8 数量列 amount_len=6金额列-->这里的字节数可按自己需求自由改写, + + /** + * 生成商品的打印字符串 + * + * @param productList 订单商品列表 + * @param titleLen 产品名称字节数,最大18个字节 + * @param numLen 数量字节数,最大6个字节 + * @param amountLen 金额字节数,最大8个字节 + * @return + */ + public String genProductStr(List productList, int titleLen, int numLen, int amountLen) { + String resultStr = ""; + resultStr += "商品名称 数量 金额
"; + resultStr += "--------------------------------
"; + for (int i = 0; i < productList.size(); i++) { + String product_sn = productList.get(i).getProduct_sn(); + String title = productList.get(i).getTitle(); + String num = productList.get(i).getNum().toString(); + if (productList.get(i).getNum() > 1) { + num = "x" + num; + } + String amount = productList.get(i).getAmount(); + + num = addSpace(num, numLen); + amount = addSpace(amount, amountLen); + String otherStr = "" + num + "" + "" + amount + ""; + + int tl = asciiByteLen(title); + int spaceNum = (tl / titleLen + 1) * titleLen - tl; + if (tl < titleLen) { + for (int k = 0; k < spaceNum; k++) { + title += " "; + } + title += otherStr; + } else if (tl == titleLen) { + title += otherStr; + } else { + List list = null; + if (isEn(title)) { + // 英文 + list = getStrList(title, titleLen); + } else { + // 中文 + list = getStrList(title, titleLen / 2); + } + String s0 = titleAddSpace(list.get(0), titleLen); + title = s0 + otherStr + "
";// 添加 单价 数量 总额 + String s = ""; + for (int k = 1; k < list.size(); k++) { + s += list.get(k); + } + + try { + s = getStringByEnter(titleLen, s); + } catch (Exception e) { + e.printStackTrace(); + } + title += "" + s + " "; + } + resultStr += "" + title + " " + "
"; + resultStr += "" + product_sn + "
"; + } + + return resultStr; + } + + /** + * 中英文混合的标题加空格 + * + * @param str + * @param len + * @return + */ + public String titleAddSpace(String str, int len) { + int k = asciiByteLen(str); // 中英文混合的标题 + for (int i = 0; i < len - k; i++) { + str += " "; + } + return str; + } + + /** + * 递归函数,字符串超长,自动换行 + * + * @param length + * @param string + * @return + * @throws Exception + */ + public String getStringByEnter(int length, String string) throws Exception { + for (int i = 1; i <= string.length(); i++) { + if (asciiByteLen(string.substring(0, i)) > length) { + return string.substring(0, i - 1) + "
" + getStringByEnter(length, string.substring(i - 1)); + } + } + return string; + } + + /** + * 给字符串前后补充空格,足够size长度,一般用在 数量和价格 + * + * @param str + * @param size 额定字节数 + * @return + */ + public String addSpace(String str, int size) { + int len = asciiByteLen(str); + if (len >= size) { + return str; + } + int cz = size - len; + if (cz % 2 == 0) { + // 偶数 + for (int i = 1; i <= cz; i++) { + if (i % 2 == 0) { + str = " " + str; + } else { + str = str + " "; + } + } + } else { + // 奇数 + for (int i = 1; i <= cz; i++) { + if (i % 2 == 0) { + str = str + " "; + } else { + str = " " + str; + } + } + } + return str; + } + + /** + * 判断字符串是否为英文 + * + * @param str + * @return + */ + public Boolean isEn(String str) { + return asciiByteLen(str) == str.length(); + } + + /** + * 获取字符串的字节长度,中文 2 个字节,英文数字 1 个字节 + * + * @param str 字符串 + * @return 字符串的字节长度 + */ + public int asciiByteLen(String str) { + if (str == null || str.isEmpty()) { + return 0; + } + + int byteLength = 0; + for (char c : str.toCharArray()) { + if (c > 255) { + byteLength += 2; + } else { + byteLength += 1; + } + } + return byteLength; + } + + public List getStrList(String inputString, int length) { + int size = inputString.length() / length; + if (inputString.length() % length != 0) { + size += 1; + } + return getStrList(inputString, length, size); + } + + public List getStrList(String inputString, int length, int size) { + List list = new ArrayList(); + for (int index = 0; index < size; index++) { + String childStr = subString(inputString, index * length, (index + 1) * length); + list.add(childStr); + } + return list; + } + + /** + * 截取字符串 + * + * @param str + * @param f + * @param t + * @return + */ + public String subString(String str, int f, int t) { + if (f > str.length()) + return ""; + if (t > str.length()) { + return str.substring(f); + } else { + return str.substring(f, t); + } + } + + /** + * 关闭流和客户端 + * + * @param response + * @param post + * @param httpClient + */ + public void close(CloseableHttpResponse response, HttpPost post, CloseableHttpClient httpClient) { + try { + if (response != null) { + response.close(); + } + } catch (IOException e) { + e.printStackTrace(); + } + try { + post.abort(); + } catch (Exception e) { + e.printStackTrace(); + } + try { + httpClient.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + + }