diff --git a/module_base/src/main/java/com/lukouguoji/module_base/util/PrinterUtils.kt b/module_base/src/main/java/com/lukouguoji/module_base/util/PrinterUtils.kt index 3f34557..b6ff1e8 100644 --- a/module_base/src/main/java/com/lukouguoji/module_base/util/PrinterUtils.kt +++ b/module_base/src/main/java/com/lukouguoji/module_base/util/PrinterUtils.kt @@ -27,7 +27,12 @@ import com.lukouguoji.module_base.ktx.permission import com.lukouguoji.module_base.ktx.showToast import com.lukouguoji.module_base.model.LoadingModel import dev.DevUtils +import dev.utils.app.HandlerUtils +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.delay +import kotlinx.coroutines.withContext import okio.internal.commonToUtf8String +import java.io.IOException object PrinterUtils { @@ -315,6 +320,50 @@ object PrinterUtils { * 打印国际出港板箱过磅挂签(2列布局,支持合并单元格) */ fun printGjcBoxWeighing(bean: GjcUldUseBean) { + printMergedGrid(buildGjcBoxWeighingRows(bean)) + } + + /** + * 批量打印国际出港板箱过磅挂签 + * 逐张串行发送,张间节流,任一张发送失败即中止并提示已完成张数 + */ + suspend fun printGjcBoxWeighingBatch(beans: List) = withContext(Dispatchers.IO) { + var printed = 0 + loading.showLoading() + try { + for ((index, bean) in beans.withIndex()) { + // 连接断开(含上一张写入异常后 SDK 置为断开)则不再继续发送 + if (portManager?.connectStatus != true) break + HandlerUtils.postRunnable { + loading.setMessage("正在打印 ${index + 1}/${beans.size}") + } + val bytes = buildMergedGridBytes(buildGjcBoxWeighingRows(bean)) + val ok = try { + portManager?.writeDataImmediately(bytes) == true + } catch (e: IOException) { + showLog("批量打印第 ${index + 1} 张写入异常 : ${e.message}") + false + } + if (!ok) break + printed++ + // 张间留出出纸时间,避免连续灌入导致打印机缓冲溢出 + if (index < beans.lastIndex) delay(800) + } + } finally { + loading.dismissLoading() + } + if (printed == beans.size) { + showToast("${beans.size} 张挂签打印完成") + } else { + val failBean = beans[printed] + showToast("已打印 $printed 张,第 ${printed + 1} 张(ULD ${failBean.uld})发送失败,请检查打印机后重试") + } + } + + /** + * 组装板箱过磅挂签的表格行数据 + */ + private fun buildGjcBoxWeighingRows(bean: GjcUldUseBean): ArrayList { val rows = arrayListOf() // 第1行:日期 | 航班号 @@ -345,7 +394,7 @@ object PrinterUtils { merged = CellData("备注:", "NOTES:", bean.remark) )) - printMergedGrid(rows) + return rows } private fun printCommonGrid(rowItem: ArrayList>) { @@ -456,6 +505,13 @@ object PrinterUtils { * 打印支持合并单元格的表格 */ private fun printMergedGrid(rows: ArrayList) { + portManager?.writeDataImmediately(buildMergedGridBytes(rows)) + } + + /** + * 生成支持合并单元格的表格标签字节流(自包含一张完整标签的 TSPL 指令) + */ + private fun buildMergedGridBytes(rows: ArrayList): ByteArray { val gridStartX = 30 val titleTopMargin = 50 // 标题顶部间距 val titleFontSize = 130 // 标题字体大小 @@ -536,7 +592,7 @@ object PrinterUtils { addPrint(1) }.bytes - portManager?.writeDataImmediately(bytes) + return bytes } /** diff --git a/module_gjc/src/main/java/com/lukouguoji/gjc/viewModel/GjcBoxWeighingViewModel.kt b/module_gjc/src/main/java/com/lukouguoji/gjc/viewModel/GjcBoxWeighingViewModel.kt index b3ae37e..f06218e 100644 --- a/module_gjc/src/main/java/com/lukouguoji/gjc/viewModel/GjcBoxWeighingViewModel.kt +++ b/module_gjc/src/main/java/com/lukouguoji/gjc/viewModel/GjcBoxWeighingViewModel.kt @@ -3,6 +3,7 @@ package com.lukouguoji.gjc.viewModel import android.app.Activity import android.content.Intent import androidx.lifecycle.MutableLiveData +import androidx.lifecycle.viewModelScope import com.lukouguoji.gjc.R import com.lukouguoji.gjc.holder.GjcBoxWeighingViewHolder import com.lukouguoji.module_base.base.BasePageViewModel @@ -20,6 +21,7 @@ import com.lukouguoji.module_base.util.PrinterUtils import dev.utils.app.info.KeyValue import dev.utils.common.DateUtils import com.lukouguoji.module_base.ktx.formatDate +import kotlinx.coroutines.launch /** * 国际出港板箱过磅 ViewModel @@ -96,16 +98,19 @@ class GjcBoxWeighingViewModel : BasePageViewModel() { return } - // 校验多选 - if (selectedItems.size > 1) { - showToast("只能选择一条记录进行打印") - return - } - - // 执行打印 - val bean = selectedItems.first() - BluetoothDialogModel().showCallBack { - PrinterUtils.printGjcBoxWeighing(bean) + if (selectedItems.size == 1) { + // 单条:保持原有打印路径 + val bean = selectedItems.first() + BluetoothDialogModel().showCallBack { + PrinterUtils.printGjcBoxWeighing(bean) + } + } else { + // 多条:逐张串行批量打印 + BluetoothDialogModel().showCallBack { + viewModelScope.launch { + PrinterUtils.printGjcBoxWeighingBatch(selectedItems) + } + } } }