Browse Source

RFID标签打印的通用方法

master
han\hanst 7 months ago
parent
commit
0c62652e32
  1. 166
      src/main/java/com/gaotao/modules/base/controller/ReportFamilyListController.java
  2. 35
      src/main/java/com/gaotao/modules/base/dao/ReportFamilyListMapper.java
  3. 60
      src/main/java/com/gaotao/modules/base/entity/ReportFamilyList.java
  4. 238
      src/main/java/com/gaotao/modules/base/service/Impl/ReportFamilyListServiceImpl.java
  5. 216
      src/main/java/com/gaotao/modules/base/service/Impl/ReportLabelListServiceImpl.java
  6. 72
      src/main/java/com/gaotao/modules/base/service/ReportFamilyListService.java
  7. 2
      src/main/java/com/gaotao/modules/base/service/ReportLabelListService.java

166
src/main/java/com/gaotao/modules/base/controller/ReportFamilyListController.java

@ -0,0 +1,166 @@
package com.gaotao.modules.base.controller;
import com.gaotao.common.utils.PageUtils;
import com.gaotao.common.utils.R;
import com.gaotao.modules.base.entity.ReportFamilyList;
import com.gaotao.modules.base.service.ReportFamilyListService;
import com.gaotao.modules.sys.controller.AbstractController;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Map;
/**
* 标签类型管理控制器
*
* <p><b>主要功能</b></p>
* <ul>
* <li>标签类型的增删改查</li>
* <li>标签类型列表查询</li>
* <li>标签类型与视图的映射管理</li>
* </ul>
*
* @author System
* @since 2025-10-13
*/
@Slf4j
@RestController
@RequestMapping("/label/labelType")
public class ReportFamilyListController extends AbstractController {
@Autowired
private ReportFamilyListService reportFamilyListService;
/**
* @Author System
* @Description 分页查询标签类型列表
* @Date 2025/10/13
* @Param [params]
* @return com.gaotao.common.utils.R
**/
@PostMapping("getList")
public R getList(@RequestBody Map<String, Object> params) {
try {
log.info("查询标签类型列表 - params: {}", params);
// 转换分页参数为String类型Query工具类需要String类型
if (params.get("page") != null) {
params.put("page", String.valueOf(params.get("page")));
}
if (params.get("limit") != null) {
params.put("limit", String.valueOf(params.get("limit")));
}
PageUtils page = reportFamilyListService.queryPage(params);
return R.ok()
.put("code", 200)
.put("msg", "查询成功")
.put("page", page);
} catch (Exception e) {
log.error("查询标签类型列表失败: " + e.getMessage(), e);
return R.error("查询失败: " + e.getMessage());
}
}
/**
* @Author System
* @Description 获取所有标签类型用于下拉选择
* @Date 2025/10/13
* @Param [params]
* @return com.gaotao.common.utils.R
**/
@PostMapping("getAllList")
public R getAllList(@RequestBody Map<String, Object> params) {
try {
String site = (String) params.get("site");
log.info("获取所有标签类型 - site: {}", site);
List<ReportFamilyList> list = reportFamilyListService.getLabelTypeList(site);
return R.ok()
.put("code", 200)
.put("msg", "查询成功")
.put("data", list);
} catch (Exception e) {
log.error("获取标签类型失败: " + e.getMessage(), e);
return R.error("查询失败: " + e.getMessage());
}
}
/**
* @Author System
* @Description 保存标签类型
* @Date 2025/10/13
* @Param [reportFamilyList]
* @return com.gaotao.common.utils.R
**/
@PostMapping("save")
public R save(@RequestBody ReportFamilyList reportFamilyList) {
try {
log.info("保存标签类型 - name: {}", reportFamilyList.getName());
// 设置创建人
reportFamilyList.setCreatedBy(getUser().getUsername());
reportFamilyList.setUpdatedBy(getUser().getUsername());
reportFamilyListService.saveLabelType(reportFamilyList);
return R.ok()
.put("code", 200)
.put("msg", "保存成功");
} catch (Exception e) {
log.error("保存标签类型失败: " + e.getMessage(), e);
return R.error("保存失败: " + e.getMessage());
}
}
/**
* @Author System
* @Description 更新标签类型
* @Date 2025/10/13
* @Param [reportFamilyList]
* @return com.gaotao.common.utils.R
**/
@PostMapping("update")
public R update(@RequestBody ReportFamilyList reportFamilyList) {
try {
log.info("更新标签类型 - name: {}", reportFamilyList.getName());
// 设置更新人
reportFamilyList.setUpdatedBy(getUser().getUsername());
reportFamilyListService.updateLabelType(reportFamilyList);
return R.ok()
.put("code", 200)
.put("msg", "更新成功");
} catch (Exception e) {
log.error("更新标签类型失败: " + e.getMessage(), e);
return R.error("更新失败: " + e.getMessage());
}
}
/**
* @Author System
* @Description 删除标签类型
* @Date 2025/10/13
* @Param [params]
* @return com.gaotao.common.utils.R
**/
@PostMapping("delete")
public R delete(@RequestBody Map<String, Object> params) {
try {
String name = (String) params.get("name");
String site = (String) params.get("site");
log.info("删除标签类型 - name: {}, site: {}", name, site);
reportFamilyListService.deleteLabelType(name, site);
return R.ok()
.put("code", 200)
.put("msg", "删除成功");
} catch (Exception e) {
log.error("删除标签类型失败: " + e.getMessage(), e);
return R.error("删除失败: " + e.getMessage());
}
}
}

35
src/main/java/com/gaotao/modules/base/dao/ReportFamilyListMapper.java

@ -0,0 +1,35 @@
package com.gaotao.modules.base.dao;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.gaotao.modules.base.entity.ReportFamilyList;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
/**
* 标签类型管理Mapper接口
*
* <p><b>主要功能</b></p>
* <ul>
* <li>标签类型的增删改查操作</li>
* <li>标签类型与视图的映射管理</li>
* </ul>
*
* @author System
* @since 2025-10-13
*/
@Mapper
public interface ReportFamilyListMapper extends BaseMapper<ReportFamilyList> {
/**
* 检查标签类型是否被使用
*
* @param reportFamily 标签类型名称
* @param site 站点编码
* @return 使用该标签类型的数量
*/
@Select("SELECT COUNT(1) FROM ReportFileList WHERE ReportFamily = #{reportFamily}")
Integer countLabelTypeUsage(@Param("reportFamily") String reportFamily, @Param("site") String site);
}

60
src/main/java/com/gaotao/modules/base/entity/ReportFamilyList.java

@ -1,24 +1,72 @@
package com.gaotao.modules.base.entity;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import java.io.Serializable;
import java.util.Date;
/**
* report_family_list实体类
* 标签类型信息实体类
*
* <p><b>核心字段说明</b></p>
* <ul>
* <li><b>name</b>标签类型名称唯一标识</li>
* <li><b>site</b>站点编码</li>
* <li><b>view_sql</b>WMS视图名称</li>
* <li><b>ifs_view_sql</b>IFS视图名称</li>
* </ul>
*/
@Data
@TableName("report_family_list")
public class ReportFamilyList implements Serializable {
private String name; // 标签类型
/**
* 标签类型名称 - 必须字段
*/
@TableField("name")
private String name;
private String site; // 工厂
/**
* 站点编码
*/
@TableField("site")
private String site;
private String viewSql; // 视图名称
/**
* 创建人
*/
@TableField("created_by")
private String createdBy;
private String ifsViewSql; // IFS视图名称
/**
* 创建时间
*/
@TableField("created_time")
private Date createdTime;
// 其他字段根据实际表结构添加
/**
* 更新人
*/
@TableField("updated_by")
private String updatedBy;
/**
* 更新时间
*/
@TableField("updated_time")
private Date updatedTime;
/**
* WMS视图名称
*/
@TableField("view_sql")
private String viewSql;
/**
* IFS视图名称
*/
@TableField("ifs_view_sql")
private String ifsViewSql;
}

238
src/main/java/com/gaotao/modules/base/service/Impl/ReportFamilyListServiceImpl.java

@ -0,0 +1,238 @@
package com.gaotao.modules.base.service.Impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gaotao.common.utils.PageUtils;
import com.gaotao.common.utils.Query;
import com.gaotao.modules.base.dao.ReportFamilyListMapper;
import com.gaotao.modules.base.entity.ReportFamilyList;
import com.gaotao.modules.base.service.ReportFamilyListService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.Date;
import java.util.List;
import java.util.Map;
/**
* 标签类型管理服务实现类
*
* <p><b>主要功能</b></p>
* <ul>
* <li>标签类型的增删改查</li>
* <li>标签类型与视图的映射管理</li>
* </ul>
*
* <p><b>关键业务规则</b></p>
* <ul>
* <li>标签类型名称在同一站点下唯一</li>
* <li>删除标签类型前需要检查是否被标签引用</li>
* </ul>
*
* @author System
* @since 2025-10-13
*/
@Slf4j
@Service
@Transactional
public class ReportFamilyListServiceImpl extends ServiceImpl<ReportFamilyListMapper, ReportFamilyList>
implements ReportFamilyListService {
/**
* 分页查询标签类型列表
*
* @param params 查询参数
* @return 分页结果
*/
@Override
public PageUtils queryPage(Map<String, Object> params) {
log.info("=== 开始查询标签类型列表 ===");
String site = (String) params.get("site");
String name = (String) params.get("name");
log.info("查询条件 - site: {}, name: {}", site, name);
LambdaQueryWrapper<ReportFamilyList> wrapper = new LambdaQueryWrapper<>();
if (StringUtils.isNotBlank(site)) {
wrapper.eq(ReportFamilyList::getSite, site);
}
if (StringUtils.isNotBlank(name)) {
wrapper.like(ReportFamilyList::getName, name);
}
wrapper.orderByDesc(ReportFamilyList::getCreatedTime);
IPage<ReportFamilyList> page = this.page(
new Query<ReportFamilyList>().getPage(params),
wrapper
);
log.info("查询完成,共 {} 条记录", page.getTotal());
return new PageUtils(page);
}
/**
* 获取所有标签类型列表用于下拉选择
*
* @param site 工厂编码
* @return 标签类型列表
*/
@Override
public List<ReportFamilyList> getLabelTypeList(String site) {
log.info("获取标签类型列表 - site: {}", site);
LambdaQueryWrapper<ReportFamilyList> wrapper = new LambdaQueryWrapper<>();
if (StringUtils.isNotBlank(site)) {
wrapper.eq(ReportFamilyList::getSite, site);
}
wrapper.orderByAsc(ReportFamilyList::getName);
return this.list(wrapper);
}
/**
* 保存标签类型
*
* @param reportFamilyList 标签类型信息
*/
@Override
@Transactional
public void saveLabelType(ReportFamilyList reportFamilyList) {
log.info("=== 开始保存标签类型 ===");
log.info("标签类型: {}, 站点: {}", reportFamilyList.getName(), reportFamilyList.getSite());
// 验证标签类型名称是否已存在
LambdaQueryWrapper<ReportFamilyList> wrapper = new LambdaQueryWrapper<>();
wrapper.eq(ReportFamilyList::getName, reportFamilyList.getName())
.eq(ReportFamilyList::getSite, reportFamilyList.getSite());
ReportFamilyList existing = this.getOne(wrapper);
if (existing != null) {
log.error("标签类型已存在: {}", reportFamilyList.getName());
throw new RuntimeException("标签类型名称已存在: " + reportFamilyList.getName());
}
// 设置创建时间
reportFamilyList.setCreatedTime(new Date());
reportFamilyList.setUpdatedTime(new Date());
boolean result = this.save(reportFamilyList);
if (!result) {
log.error("保存标签类型失败");
throw new RuntimeException("保存标签类型失败");
}
log.info("=== 标签类型保存成功 ===");
}
/**
* 更新标签类型
*
* @param reportFamilyList 标签类型信息
*/
@Override
@Transactional
public void updateLabelType(ReportFamilyList reportFamilyList) {
log.info("=== 开始更新标签类型 ===");
log.info("标签类型: {}, 站点: {}", reportFamilyList.getName(), reportFamilyList.getSite());
// 检查标签类型是否正在被使用
if (isLabelTypeInUse(reportFamilyList.getName(), reportFamilyList.getSite())) {
log.error("标签类型正在被使用,不允许修改: {}", reportFamilyList.getName());
throw new RuntimeException("该标签类型正在被标签使用,不允许修改");
}
// 设置更新时间
reportFamilyList.setUpdatedTime(new Date());
LambdaQueryWrapper<ReportFamilyList> wrapper = new LambdaQueryWrapper<>();
wrapper.eq(ReportFamilyList::getName, reportFamilyList.getName())
.eq(ReportFamilyList::getSite, reportFamilyList.getSite());
boolean result = this.update(reportFamilyList, wrapper);
if (!result) {
log.error("更新标签类型失败");
throw new RuntimeException("更新标签类型失败");
}
log.info("=== 标签类型更新成功 ===");
}
/**
* 删除标签类型
*
* @param name 标签类型名称
* @param site 工厂编码
*/
@Override
@Transactional
public void deleteLabelType(String name, String site) {
log.info("=== 开始删除标签类型 ===");
log.info("标签类型: {}, 站点: {}", name, site);
// 验证标签类型是否存在
LambdaQueryWrapper<ReportFamilyList> wrapper = new LambdaQueryWrapper<>();
wrapper.eq(ReportFamilyList::getName, name)
.eq(ReportFamilyList::getSite, site);
ReportFamilyList existing = this.getOne(wrapper);
if (existing == null) {
log.error("标签类型不存在: {}", name);
throw new RuntimeException("标签类型不存在: " + name);
}
// 检查标签类型是否正在被使用
if (isLabelTypeInUse(name, site)) {
log.error("标签类型正在被使用,不允许删除: {}", name);
throw new RuntimeException("该标签类型正在被标签使用,不允许删除");
}
boolean result = this.remove(wrapper);
if (!result) {
log.error("删除标签类型失败");
throw new RuntimeException("删除标签类型失败");
}
log.info("=== 标签类型删除成功 ===");
}
/**
* 检查标签类型是否正在被使用
*
* @param name 标签类型名称
* @param site 工厂编码
* @return true=正在使用false=未使用
*/
@Override
public boolean isLabelTypeInUse(String name, String site) {
log.info("检查标签类型是否被使用 - name: {}, site: {}", name, site);
try {
// 使用Mapper接口的@Select注解方法查询
Integer count = baseMapper.countLabelTypeUsage(name, site);
boolean inUse = count != null && count > 0;
log.info("标签类型 {} 的使用情况: {} (使用该类型的标签数量: {})",
name, inUse ? "正在使用" : "未使用", count);
return inUse;
} catch (Exception e) {
log.error("检查标签类型使用情况时发生异常: " + e.getMessage(), e);
// 出现异常时为了安全起见返回true假设正在使用不允许删除
return true;
}
}
}

216
src/main/java/com/gaotao/modules/base/service/Impl/ReportLabelListServiceImpl.java

@ -240,19 +240,9 @@ public class ReportLabelListServiceImpl extends ServiceImpl<ReportLabelListMappe
// 为每个ZPL代码设置到printRequest中用于日志记录
printRequest.setZplCode(zplCode);
Map<String, Object> labelData = i < labelDataList.size() ? labelDataList.get(i) : new HashMap<>();
String epc = getStringValue(labelData, "unit_id", "");
// 补齐到24位不足的在前面加0
epc = String.format("%24s", epc).replace(' ', '0');
log.info("epc24位: {}", epc);
// 发送ZPL到打印机
RfidProcessResult printResult = sendZplToPrinterAndGetResult(printerIP, zplCode, requestedCopies, rfidFlag,labelData);
// 打印成功后保存标签记录
/*if (printResult.isSuccess()) {
saveWmsLabelRecord(printRequest, labelSettingDataList.getFirst().getLabelType(), printResult, defaultLabelSettingData.getLabelNo(), i + 1, labelData);
}*/
sendZplToPrinterAndGetResult(printerIP, zplCode, requestedCopies, rfidFlag,labelData);
}
// 6. 记录打印日志
log.info("所有标签打印任务执行成功,共打印 {} 张标签,每张 {} 份", totalLabels, requestedCopies);
} catch (Exception e) {
@ -264,7 +254,7 @@ public class ReportLabelListServiceImpl extends ServiceImpl<ReportLabelListMappe
/**
* 发送ZPL代码到打印机并返回结果支持RFID标签处理和多份打印
*/
private RfidProcessResult sendZplToPrinterAndGetResult(String printerIP, String zplCode,
private void sendZplToPrinterAndGetResult(String printerIP, String zplCode,
Integer copies, String rfidFlag, Map<String, Object> labelData) {
try {
log.info("开始处理标签打印: {}, 请求份数: {}, RFID标志: {}", printerIP, copies, rfidFlag);
@ -286,7 +276,6 @@ public class ReportLabelListServiceImpl extends ServiceImpl<ReportLabelListMappe
}
log.info("标签打印任务完成,总份数: {}", copies);
return result;
} catch (Exception e) {
throw new RuntimeException("无法连接到打印机 " + printerIP + ": " + e.getMessage());
@ -317,84 +306,6 @@ public class ReportLabelListServiceImpl extends ServiceImpl<ReportLabelListMappe
}
}
/**
* 保存WMS标签记录
*/
private void saveWmsLabelRecord(PrintLabelRequest printRequest, String labelType, RfidProcessResult printResult, String labelNo,
int labelIndex, java.util.Map<String, Object> labelData) {
try {
log.info("开始保存WMS标签记录,标签索引: {}", labelIndex);
// 1. 获取流水号 - 从数据库中查询已保存的流水号
/*String serialNo = getSerialNumberFromDatabase(labelNo, labelIndex);
// 2. 从查询参数中获取其他信息
WmsLabel wmsLabel = new WmsLabel();
wmsLabel.setSite(printRequest.getSite());
wmsLabel.setSerialNo(serialNo);
// 3. 从labelData中获取实际的字段值
wmsLabel.setPartNo(getStringValue(labelData, "part_no", "UNKNOWN_PART"));
wmsLabel.setQty(getBigDecimalValue(labelData, "qty_received", null));
wmsLabel.setInStockFlag("Y".equals(printRequest.getNeedCheck())?"N":"Y"); // 根据needCheck决定是否直接入库
wmsLabel.setLabelType(labelType); // 标签类型
wmsLabel.setBatchNo(getStringValue(labelData, "batch_no", null));
wmsLabel.setWarehouseId(getStringValue(labelData, "warehouse_id", null));
wmsLabel.setLocationId(getStringValue(labelData, "location_id", null));
wmsLabel.setRemark("采购接收"+printRequest.getReceiptNo()+",打印时间: " + java.time.LocalDateTime.now());
// 4. 如果是RFID标签设置TID和EPC
if (printResult.getTid() != null && !printResult.getTid().trim().isEmpty()) {
wmsLabel.setTid(printResult.getTid());
wmsLabel.setEpc(printResult.getEpcData());
log.info("RFID标签信息 - TID: {}, EPC: {}", printResult.getTid(), printResult.getEpcData());
}
// 5. 保存到数据库
baseService.saveWmsLabel(wmsLabel);*/
// 根据site+unitId获取HU更新HU的tid和epc
HandlingUnit hUnit = handlingUnitService.lambdaQuery()
.eq(HandlingUnit::getSite, printRequest.getSite())
.eq(HandlingUnit::getUnitId, getStringValue(labelData, "unit_id", null))
.one();
hUnit.setTid(printResult.getTid());
hUnit.setEpc(printResult.getEpcData());
handlingUnitService.lambdaUpdate()
.set(HandlingUnit::getTid, printResult.getTid())
.set(HandlingUnit::getEpc, printResult.getEpcData())
.eq(HandlingUnit::getSite, printRequest.getSite())
.eq(HandlingUnit::getUnitId, getStringValue(labelData, "unit_id", null))
.update();
log.info("WMS标签记录保存成功: {}", hUnit);
} catch (Exception e) {
log.error("保存WMS标签记录失败: {}", e.getMessage(), e);
// 不抛出异常避免影响打印流程
}
}
/**
* 从数据库中获取流水号
*/
private String getSerialNumberFromDatabase(String labelNo, int labelIndex) {
try {
// 查询该标签的serialNumber元素
List<ReportLabelList> serialElement = baseService.getReportLabelListByReportId(labelNo);
for (ReportLabelList element : serialElement) {
if (element != null && "serialNumber".equals(element.getType())) {
String fullSerialNumber = element.getFullSerialNumber();
if (fullSerialNumber != null && !fullSerialNumber.trim().isEmpty()) {
return fullSerialNumber;
}
}
}
return "";
} catch (Exception e) {
log.warn("获取流水号失败,使用临时流水号: {}", e.getMessage());
return "";
}
}
/**
* 从Map中安全地获取字符串值
*/
@ -406,30 +317,6 @@ public class ReportLabelListServiceImpl extends ServiceImpl<ReportLabelListMappe
return value != null ? value.toString() : defaultValue;
}
/**
* 从Map中安全地获取BigDecimal值
*/
private java.math.BigDecimal getBigDecimalValue(Map<String, Object> data, String key, java.math.BigDecimal defaultValue) {
if (data == null || !data.containsKey(key)) {
return defaultValue;
}
Object value = data.get(key);
if (value == null) {
return defaultValue;
}
try {
if (value instanceof java.math.BigDecimal) {
return (java.math.BigDecimal) value;
} else if (value instanceof Number) {
return new java.math.BigDecimal(value.toString());
} else {
return new java.math.BigDecimal(value.toString());
}
} catch (Exception e) {
log.warn("转换BigDecimal失败,使用默认值: key={}, value={}, error={}", key, value, e.getMessage());
return defaultValue;
}
}
@Override
public void printLabelTest(PrintLabelRequest printRequest) {
@ -556,23 +443,6 @@ public class ReportLabelListServiceImpl extends ServiceImpl<ReportLabelListMappe
return responseStr;
}
/**
* 生成有效的EPC数据
* EPC必须是12个十六进制字符6字节
*/
private String generateValidEpc() {
// 生成基于时间戳的EPC确保唯一性和有效性
long timestamp = System.currentTimeMillis();
String epc = String.format("%012X", timestamp & 0xFFFFFFFFFFFFL);
// 确保EPC长度正确12个字符
if (epc.length() > 12) {
epc = epc.substring(epc.length() - 12);
} else if (epc.length() < 12) {
epc = String.format("%012s", epc).replace(' ', '0');
}
log.info("生成EPC: {}", epc);
return epc;
}
/**
* 清理ZPL代码移除可能导致多张打印的指令
@ -704,4 +574,86 @@ public class ReportLabelListServiceImpl extends ServiceImpl<ReportLabelListMappe
throw new RuntimeException("标签预览失败: " + e.getMessage());
}
}
/**
* RFID标签打印的通用方法
* 需传入标签打印请求对象包含必要的打印参数
* reportId 标签编号
* userId 用户名用于获取打印机配置
* unitId 标签数据的唯一码
* site 工厂编号
*/
@Override
public void printLabelCommon(PrintLabelRequest printRequest) {
// 校验必要参数
if (printRequest.getReportId() == null || printRequest.getReportId().trim().isEmpty()) {
throw new RuntimeException("标签编号不能为空");
}
if (printRequest.getUserId() == null || printRequest.getUserId().trim().isEmpty()) {
throw new RuntimeException("用户名不能为空");
}
if (printRequest.getUnitId() == null || printRequest.getUnitId().trim().isEmpty()) {
throw new RuntimeException("HU标签唯一码unitID不能为空");
}
if (printRequest.getSite() == null || printRequest.getSite().trim().isEmpty()) {
throw new RuntimeException("工厂编号不能为空");
}
String rfidFlag;
LabelSettingData labelSettingData = new LabelSettingData();
labelSettingData.setLabelNo(printRequest.getReportId());
List<LabelSettingData> labelSettingDataList = baseService.getLabelSettingList(labelSettingData);
LabelSettingData labelSetting;
if (labelSettingDataList.isEmpty()) {
throw new RuntimeException("未找到对应的标签");
} else {
labelSetting = labelSettingDataList.getFirst();
rfidFlag = labelSetting.getRfidFlag();
}
try {
// 1. 构建查询参数Map
Map<String, Object> queryParams = new HashMap<>();
queryParams.put("site", printRequest.getSite());
List<String> unitIds = Collections.singletonList(printRequest.getUnitId());
queryParams.put("unitIds", unitIds);
// 可以根据需要添加更多参数
// 生成带真实数据的ZPL代码和对应的数据
List<String> zplCodeList = previewLabelWithRealData(labelSetting.getLabelNo(), true, queryParams);
// 同时获取原始数据用于保存WMS标签记录
List<java.util.Map<String, Object>> labelDataList = getLabelDataForSaving(labelSetting.getLabelNo(), queryParams);
// 2. 验证ZPL代码
if (zplCodeList == null || zplCodeList.isEmpty()) {
throw new RuntimeException("ZPL代码获取失败");
}
log.info("生成了 {} 个ZPL代码,准备打印", zplCodeList.size());
// 3. 获取用户的打印机配置
UserLabelPrinterData printerQuery = new UserLabelPrinterData();
printerQuery.setUserId(printRequest.getUserId());
printerQuery.setLabelNo(labelSetting.getLabelNo());
List<UserLabelPrinterData> printers = baseService.getUserLabelPrinters(printerQuery);
if (printers.isEmpty()) {
throw new RuntimeException("未找到指定的打印机配置");
}
UserLabelPrinterData printer = printers.getFirst();
String printerIP = printer.getIpAddress();
if (printerIP == null || printerIP.trim().isEmpty()) {
throw new RuntimeException("打印机IP地址未配置");
}
// 4. 处理打印份数
int requestedCopies = 1;
// 5. 循环发送每个ZPL到打印机并保存标签记录
int totalLabels = zplCodeList.size();
for (int i = 0; i < totalLabels; i++) {
String zplCode = zplCodeList.get(i);
log.info("正在打印第 {} / {} 张标签", i + 1, totalLabels);
Map<String, Object> labelData = i < labelDataList.size() ? labelDataList.get(i) : new HashMap<>();
// 发送ZPL到打印机
sendZplToPrinterAndGetResult(printerIP, zplCode, requestedCopies, rfidFlag,labelData);
}
// 6. 记录打印日志
log.info("所有标签打印任务执行成功,共打印 {} 张标签,每张 {} 份", totalLabels, requestedCopies);
} catch (Exception e) {
System.err.println("RFID标签打印失败: " + e.getMessage());
throw new RuntimeException("RFID标签打印失败: " + e.getMessage());
}
}
}

72
src/main/java/com/gaotao/modules/base/service/ReportFamilyListService.java

@ -0,0 +1,72 @@
package com.gaotao.modules.base.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.gaotao.common.utils.PageUtils;
import com.gaotao.modules.base.entity.ReportFamilyList;
import java.util.List;
import java.util.Map;
/**
* 标签类型管理服务接口
*
* <p><b>主要功能</b></p>
* <ul>
* <li>标签类型的增删改查</li>
* <li>标签类型列表查询</li>
* <li>标签类型与视图的映射管理</li>
* </ul>
*
* @author System
* @since 2025-10-13
*/
public interface ReportFamilyListService extends IService<ReportFamilyList> {
/**
* 分页查询标签类型列表
*
* @param params 查询参数包含sitename等
* @return 分页结果
*/
PageUtils queryPage(Map<String, Object> params);
/**
* 获取所有标签类型列表用于下拉选择
*
* @param site 工厂编码
* @return 标签类型列表
*/
List<ReportFamilyList> getLabelTypeList(String site);
/**
* 保存标签类型
*
* @param reportFamilyList 标签类型信息
*/
void saveLabelType(ReportFamilyList reportFamilyList);
/**
* 更新标签类型
*
* @param reportFamilyList 标签类型信息
*/
void updateLabelType(ReportFamilyList reportFamilyList);
/**
* 删除标签类型
*
* @param name 标签类型名称
* @param site 工厂编码
*/
void deleteLabelType(String name, String site);
/**
* 检查标签类型是否正在被使用
*
* @param name 标签类型名称
* @param site 工厂编码
* @return true=正在使用false=未使用
*/
boolean isLabelTypeInUse(String name, String site);
}

2
src/main/java/com/gaotao/modules/base/service/ReportLabelListService.java

@ -18,6 +18,8 @@ public interface ReportLabelListService extends IService<ReportLabelList> {
void printLabelTest(PrintLabelRequest printRequest);
void printLabelCommon(PrintLabelRequest printRequest);
/**
* 获取处理后的标签元素用于前端预览
* @param reportId 标签ID

Loading…
Cancel
Save