优化打印

This commit is contained in:
Jack 2024-11-10 02:50:05 +08:00
parent b0645b0f41
commit 3dada36a6f
6 changed files with 244 additions and 14 deletions

View File

@ -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<String> 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<String> 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<String> setLineToList(String str, int wordCnt, int linesCnt) {
List<String> list = new ArrayList<String>();
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;
}
}

View File

@ -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;

View File

@ -8241,11 +8241,17 @@ public class ShopOrderBaseServiceImpl extends BaseServiceImpl<ShopOrderBaseMappe
return null;
}
List<ShopStoreOrderProductPrintVO> orderItems = shopOrderItemService.selectOrderItemPrintInfo(orderId);
if (CollUtil.isEmpty(orderItems)) {
List<ShopStoreOrderProductPrintVO> shopStoreOrderProductPrintVOList = shopOrderItemService.selectOrderItemPrintInfo(orderId);
if (CollUtil.isEmpty(shopStoreOrderProductPrintVOList)) {
return null;
}
// 重构实体类处理标题数量价格适应长度
List<ShopStoreOrderProductPrintVO> 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", "");//卖家留言

View File

@ -258,10 +258,10 @@ public class ShopStorePrinterServiceImpl extends BaseServiceImpl<ShopStorePrinte
String sn = "922441475";
ShopStoreOrderProductPrintVO order1 = new ShopStoreOrderProductPrintVO("6970448170051", "可口可乐CocaCola经典美味汽水1.25L/瓶", 110, new BigDecimal("8100.45"));
ShopStoreOrderProductPrintVO order2 = new ShopStoreOrderProductPrintVO("6970448170053", "排骨约350g默认砍小块", 1, new BigDecimal("150.13"));
ShopStoreOrderProductPrintVO order3 = new ShopStoreOrderProductPrintVO("6970448170054", "新鲜虫草花1包约200g 韭菜1000g 鸡蛋2003克", 11, new BigDecimal("4.01"));
ShopStoreOrderProductPrintVO order4 = new ShopStoreOrderProductPrintVO("6970448170055", "冰红茶风味饮料", 1, new BigDecimal("13.24"));
ShopStoreOrderProductPrintVO order1 = new ShopStoreOrderProductPrintVO("可口可乐CocaCola经典美味汽水1.25L/瓶", 110, new BigDecimal("8100.45"),"6970448170051");
ShopStoreOrderProductPrintVO order2 = new ShopStoreOrderProductPrintVO( "排骨约350g默认砍小块", 1, new BigDecimal("150.13"),"6970448170051");
ShopStoreOrderProductPrintVO order3 = new ShopStoreOrderProductPrintVO("新鲜虫草花1包约200g 韭菜1000g 鸡蛋2003克", 11, new BigDecimal("4.01"),"6970448170051");
ShopStoreOrderProductPrintVO order4 = new ShopStoreOrderProductPrintVO("冰红茶风味饮料", 1, new BigDecimal("13.24"),"6970448170051");
List<ShopStoreOrderProductPrintVO> productList = new ArrayList<>();
productList.add(order1);
productList.add(order2);
@ -366,12 +366,12 @@ public class ShopStorePrinterServiceImpl extends BaseServiceImpl<ShopStorePrinte
}
// 特殊处理订单商品详情由于有逻辑判断无法模版化所以直接生成模版的字符串
String order_items_tmpl = feieUtil.genProductStr((List<ShopStoreOrderProductPrintVO>) 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<ShopStoreOrderProductPrintVO>) 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);