diff --git a/mall-common/src/main/java/com/suisung/mall/common/pojo/vo/ShopStoreOrderProductPrintVO.java b/mall-common/src/main/java/com/suisung/mall/common/pojo/vo/ShopStoreOrderProductPrintVO.java index e15a2aa1..b93065a8 100644 --- a/mall-common/src/main/java/com/suisung/mall/common/pojo/vo/ShopStoreOrderProductPrintVO.java +++ b/mall-common/src/main/java/com/suisung/mall/common/pojo/vo/ShopStoreOrderProductPrintVO.java @@ -1,18 +1,242 @@ package com.suisung.mall.common.pojo.vo; +import cn.hutool.core.collection.CollUtil; +import cn.hutool.core.util.StrUtil; +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; import lombok.AllArgsConstructor; import lombok.Data; +import lombok.EqualsAndHashCode; import lombok.NoArgsConstructor; +import lombok.experimental.Accessors; import java.io.Serializable; import java.math.BigDecimal; +import java.text.DecimalFormat; +import java.util.ArrayList; +import java.util.List; @Data +@EqualsAndHashCode(callSuper = false) +@Accessors(chain = true) @NoArgsConstructor @AllArgsConstructor +@ApiModel(value = "订单商品打印对象", description = "订单商品打印对象") public class ShopStoreOrderProductPrintVO implements Serializable { - private String product_sn; + private static final long serialVersionUID = 1L; + + public ShopStoreOrderProductPrintVO(String itemName, Integer orderItemQuantity,BigDecimal orderItemAmount,String productSn) { + new ShopStoreOrderProductPrintVO(itemName,orderItemQuantity,orderItemAmount,productSn,18,6,8); + } + + public ShopStoreOrderProductPrintVO(String itemName, Integer orderItemQuantity,BigDecimal orderItemAmount,String productSn,int titleBlen,int quantityBlen,int amountBlen) { + this.item_name = itemName; + this.order_item_quantity = orderItemQuantity; + this.order_item_amount = orderItemAmount; + this.product_sn = productSn; + this.title_blen = titleBlen; + this.quantity_blen = quantityBlen; + this.amount_blen = amountBlen; + + // 处理显示标题字段 + int tbl = asciiByteLen(this.item_name); + if (tbl < this.title_blen) { + // 标题小于指定的长度(字节数) + //int spaceNum = (tbl / this.title_blen + 1) * this.title_blen - tbl; + this.s_name = titleAddSpace(this.item_name, this.title_blen); //this.item_name + StrUtil.repeat(" ", spaceNum); // 增加spaceNum个空字符 + } else if (tbl == this.title_blen) { + // 标题刚刚等于指定的长度(字节数) + this.s_name = this.item_name; + } else { + this.s_name_segs = containEn(this.item_name) ? getStrList(this.item_name, this.title_blen) : getStrList(this.item_name, this.title_blen / 2); + if (CollUtil.isNotEmpty(this.s_name_segs) && this.s_name_segs.size() > 1) { + this.s_name = titleAddSpace(this.s_name_segs.get(0), this.title_blen); + this.s_name_segs.remove(0); + } else { + this.s_name = this.item_name; + } + } + + // 处理显示数量字段 + this.s_quantity = addSpaceBetween(order_item_quantity > 1 ? "x" + orderItemQuantity.toString() : orderItemQuantity.toString(), this.quantity_blen); + // 处理显示金额字段 + this.s_amount = addSpaceLeft(new DecimalFormat("#.00").format(order_item_amount), this.amount_blen); + } + + /** + * 重构实体类,处理标题,数量,价格适应长度 + * @return + */ + public ShopStoreOrderProductPrintVO rebuild(){ + return new ShopStoreOrderProductPrintVO(this.item_name,this.order_item_quantity,this.order_item_amount,this.product_sn,18,6,8); + } + + @ApiModelProperty(value = "商品名") private String item_name; + + @ApiModelProperty(value = "数量") private Integer order_item_quantity; + + @ApiModelProperty(value = "金额") private BigDecimal order_item_amount; + + @ApiModelProperty(value = "显示的商品名") + private String s_name; + + @ApiModelProperty(value = "显示的数量") + private String s_quantity; + + @ApiModelProperty(value = "显示的金额") + private String s_amount; + + @ApiModelProperty(value = "显示的商品条码") + private String product_sn; + + @ApiModelProperty(value = "商品名称分段") + private List s_name_segs; + + @ApiModelProperty(value = "标题额定字节长度") + private Integer title_blen; + + @ApiModelProperty(value = "数量额定字节长度") + private Integer quantity_blen; + + @ApiModelProperty(value = "金额额定字节长度") + private Integer amount_blen; + + + /** + * 判断字符串是否包含英文? + * + * @param str + * @return + */ + public Boolean containEn(String str) { + return asciiByteLen(str) == str.length(); + } + + /** + * 获取字符串的字节长度,中文 2 个字节,英文数字 1 个字节 + * + * @param str 字符串 + * @return 字符串的字节长度 + */ + public int asciiByteLen(String str) { + if (StrUtil.isEmpty(str)) { + return 0; + } + + int byteLength = 0; + for (char c : str.toCharArray()) { + byteLength = c > 255 ? byteLength + 2 : byteLength + 1; + } + return byteLength; + } + + /** + * 分析标题能分几行显示, + * + * @param str + * @param wordCnt 英文数字或中文的字数,中文2个字节,英文数字1个字节 + * @return + */ + public List getStrList(String str, int wordCnt) { + // 计算标题 wordCnt个字能分多少行? + int len = str.length(); + int linesCnt = len / wordCnt; + if (len % wordCnt != 0) { + linesCnt += 1; + } + + return setLineToList(str, wordCnt, linesCnt); + } + + /** + * 把一行行标题,分别放入列表里 + * + * @param str + * @param wordCnt + * @param linesCnt + * @return + */ + public List setLineToList(String str, int wordCnt, int linesCnt) { + List list = new ArrayList(); + for (int i = 0; i < linesCnt; i++) { + String childStr = subString(str, i * wordCnt, (i + 1) * wordCnt); + list.add(childStr); + } + return list; + } + + /** + * 截取字符串 + * + * @param str + * @param bidx + * @param eidx + * @return + */ + public String subString(String str, int bidx, int eidx) { + if (bidx > str.length()) + return ""; + if (eidx > str.length()) { + return str.substring(bidx); + } else { + return str.substring(bidx, eidx); + } + } + + /** + * 给商品标题(中英文混合的标题)后面补充空格 + * + * @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; + } + + /** + * 给字符串左右两边补充空格,指定到size长度,一般用在数量和价格 + * + * @param str + * @param size 额定字节数 + * @return + */ + public String addSpaceBetween(String str, int size) { + int len = asciiByteLen(str); + if (len >= size) { + return str; + } + + int cz = size - len; + if (cz == 1) { + return str + " "; + } else { + if (cz % 2 == 0) { + // 偶数 + return StrUtil.repeat(" ", cz / 2) + str + StrUtil.repeat(" ", cz / 2); + } else { + // 奇数 + return StrUtil.repeat(" ", cz / 2) + str + StrUtil.repeat(" ", cz / 2 + 1); + } + } + } + + // 字符串左边补空格 + public String addSpaceLeft(String str, int size) { + int len = asciiByteLen(str); + if (len >= size) { + return str; + } + + int cz = size - len; + return StrUtil.repeat(" ",cz) + str; + } + } \ No newline at end of file diff --git a/mall-common/src/main/java/com/suisung/mall/common/pojo/vo/ShopStorePrinterVO.java b/mall-common/src/main/java/com/suisung/mall/common/pojo/vo/ShopStorePrinterVO.java index bf5c7ffb..55e2dd31 100644 --- a/mall-common/src/main/java/com/suisung/mall/common/pojo/vo/ShopStorePrinterVO.java +++ b/mall-common/src/main/java/com/suisung/mall/common/pojo/vo/ShopStorePrinterVO.java @@ -28,7 +28,7 @@ import java.util.Date; @Accessors(chain = true) @NoArgsConstructor @AllArgsConstructor -@ApiModel(value = "ShopStorePrinter显示对象", description = "门店打票机表") +@ApiModel(value = "下单订单打印对象", description = "下单订单打印对象") public class ShopStorePrinterVO implements Serializable { private static final long serialVersionUID = 1L; diff --git a/mall-shop/src/main/java/com/suisung/mall/shop/order/service/impl/ShopOrderBaseServiceImpl.java b/mall-shop/src/main/java/com/suisung/mall/shop/order/service/impl/ShopOrderBaseServiceImpl.java index e1a29cef..94b55144 100644 --- a/mall-shop/src/main/java/com/suisung/mall/shop/order/service/impl/ShopOrderBaseServiceImpl.java +++ b/mall-shop/src/main/java/com/suisung/mall/shop/order/service/impl/ShopOrderBaseServiceImpl.java @@ -8241,11 +8241,17 @@ public class ShopOrderBaseServiceImpl extends BaseServiceImpl orderItems = shopOrderItemService.selectOrderItemPrintInfo(orderId); - if (CollUtil.isEmpty(orderItems)) { + List shopStoreOrderProductPrintVOList = shopOrderItemService.selectOrderItemPrintInfo(orderId); + if (CollUtil.isEmpty(shopStoreOrderProductPrintVOList)) { return null; } + // 重构实体类,处理标题,数量,价格适应长度 + List orderItems = new ArrayList<>(); + for(ShopStoreOrderProductPrintVO ent:shopStoreOrderProductPrintVOList) { + orderItems.add(ent.rebuild()); + } + // 7位数取单号,位数不够向左补0 m.put("order_pickup_num_str", String.format("%07d", m.get("order_pickup_num"))); m.put("seller_message", "");//卖家留言 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 385326cb..48f5601f 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 @@ -258,10 +258,10 @@ public class ShopStorePrinterServiceImpl extends BaseServiceImpl productList = new ArrayList<>(); productList.add(order1); productList.add(order2); @@ -366,12 +366,12 @@ public class ShopStorePrinterServiceImpl extends BaseServiceImpl) binding.get("order_items"), 18, 6, 8); - if (StrUtil.isBlank(order_items_tmpl)) { - logger.info("订单{}详情列表模版渲染异常,无法打印小票。", orderId); - return false; - } - binding.put("order_items_tmpl", order_items_tmpl); +// String order_items_tmpl = feieUtil.genProductStr((List) binding.get("order_items"), 18, 6, 8); +// if (StrUtil.isBlank(order_items_tmpl)) { +// logger.info("订单{}详情列表模版渲染异常,无法打印小票。", orderId); +// return false; +// } +// binding.put("order_items_tmpl", order_items_tmpl); // 生成打印内容(暂时飞鹅打票机的内容) String printContent = freeMakerUtils.processTemplate(template.getTemplate_name(), template.getTemplate_value(), binding); diff --git a/mall-shop/target/classes/com/suisung/mall/shop/order/service/impl/ShopOrderBaseServiceImpl.class b/mall-shop/target/classes/com/suisung/mall/shop/order/service/impl/ShopOrderBaseServiceImpl.class index 38cba706..f6bebd96 100644 Binary files a/mall-shop/target/classes/com/suisung/mall/shop/order/service/impl/ShopOrderBaseServiceImpl.class and b/mall-shop/target/classes/com/suisung/mall/shop/order/service/impl/ShopOrderBaseServiceImpl.class differ diff --git a/mall-shop/target/classes/com/suisung/mall/shop/order/service/impl/ShopOrderItemServiceImpl.class b/mall-shop/target/classes/com/suisung/mall/shop/order/service/impl/ShopOrderItemServiceImpl.class index 9e75bf2b..d01b82b0 100644 Binary files a/mall-shop/target/classes/com/suisung/mall/shop/order/service/impl/ShopOrderItemServiceImpl.class and b/mall-shop/target/classes/com/suisung/mall/shop/order/service/impl/ShopOrderItemServiceImpl.class differ