refactor: 优化标签打印功能并调整MQTT主题

- 将ProFinishedProductReceiptService中的_receiptLogService改为局部变量
- 统一GP12和后道标签打印的MQTT主题格式
- 调整标签打印名称以更准确反映业务场景
- 在后道服务中新增标签打印MQTT消息发送功能
This commit is contained in:
赵正易 2026-01-23 11:48:19 +08:00
parent 28cd87467d
commit d4613ab893
3 changed files with 73 additions and 6 deletions

View File

@ -18,7 +18,6 @@ namespace ZR.Service.Business
[AppService(ServiceType = typeof(IProFinishedProductReceiptService), ServiceLifetime = LifeTime.Transient)]
public class ProFinishedProductReceiptService : BaseService<ProFinishedProductReceipt>, IProFinishedProductReceiptService
{
private readonly IProFinishedProductReceiptLogService _receiptLogService;
/// <summary>
/// 查询MES成品入库单主表含产品信息及标签打印状态列表
@ -61,7 +60,7 @@ namespace ZR.Service.Business
/// <returns></returns>
public ProFinishedProductReceipt AddProFinishedProductReceipt(ProFinishedProductReceipt model)
{
ProFinishedProductReceiptLogService _receiptLogService = new ();
try
{

View File

@ -9,6 +9,7 @@ using System.Threading.Tasks;
using ZR.Common.MqttHelper;
using ZR.Model.Business;
using ZR.Model.Dto;
using ZR.Model.MES.qc.DTO;
using ZR.Model.MES.wms;
using ZR.Model.MES.wms.Dto;
using ZR.Service.Business.IBusinessService;
@ -25,11 +26,13 @@ namespace ZR.Service.Business
{
private readonly MqttService _mqttService; // 注入MqttService
private readonly ILogger<QcBackEndService> _logger;
private readonly IProFinishedProductReceiptService _proFinishedProductReceiptService;
public QcBackEndService(MqttService mqttService, ILogger<QcBackEndService> logger)
public QcBackEndService(MqttService mqttService, ILogger<QcBackEndService> logger, IProFinishedProductReceiptService proFinishedProductReceiptService)
{
_mqttService = mqttService;
_logger = logger;
_proFinishedProductReceiptService = proFinishedProductReceiptService;
}
public QcBackEndLabelAnalysisDto AnalyzeLabelToDto(string label, int type)
@ -1237,6 +1240,19 @@ namespace ZR.Service.Business
Context.Insertable(qcBackEndLog).ExecuteCommand();
// 提交事务
Context.Ado.CommitTran();
// 发送mqtt消息通知打印抛光/打磨标签
// 发送抛光标签打印消息
if (qcBackEndWorkorder.PolishNumber > 0)
{
SendLabelPrintMqttMessage(qcBackEndWorkorder, 1, qcBackEndWorkorder.PolishNumber.Value);
}
// 发送打磨标签打印消息
if (qcBackEndWorkorder.DamoNumber > 0)
{
SendLabelPrintMqttMessage(qcBackEndWorkorder, 2, qcBackEndWorkorder.DamoNumber.Value);
}
// 插入成品入库单
_ = Task.Run(() =>
{
@ -1519,6 +1535,58 @@ namespace ZR.Service.Business
return Guid.NewGuid().ToString("N").Substring(0, 10); // Generate a 10-character unique ID
}
/// <summary>
/// 发送标签打印MQTT消息
/// </summary>
/// <param name="workorder">工单信息</param>
/// <param name="labelType">标签类型1-抛光2-打磨</param>
/// <param name="quantity">数量</param>
private void SendLabelPrintMqttMessage(QcBackEndServiceWorkorder workorder, int labelType, int quantity)
{
try
{
if (quantity <= 0)
{
return;
}
string mqttTopic = "shgg_mes/backEnd/label_print/print/PGDM";
string labelCode = labelType == 1 ? "Type=DeNoPG" : "Type=DeNoDM";
string path = labelType == 1 ? "D:\\RIZO\\label\\抛光送货单.btw" : "D:\\RIZO\\label\\打磨送货单.btw";
string name = labelType == 1 ? "后道抛光送货单标签打印" : "后道打磨送货单标签打印";
var printDto = new PrintDeliveryNoteDto
{
Path = path,
SiteNo = workorder.SiteNo,
Name = name,
PartNumber = workorder.PartNumber,
Description = workorder.Description,
Color = workorder.Color,
Specification = workorder.Specification,
WorkOrder = workorder.WorkOrder,
PackageCode = workorder.WorkOrder,
Team = workorder.Team,
Sort = 1,
ProductionTime = workorder.WorkOrder,
BatchCode = workorder.WorkOrder,
PackageNum = quantity,
LabelCode = $"{labelCode}^ItemNumber={workorder.PartNumber}^Order={workorder.WorkOrder}^Qty={quantity}",
LabelType = 1,
CreatedTime = DateTime.Now.ToString()
};
string message = JsonSerializer.Serialize(printDto);
_mqttService.PublishAsync(mqttTopic, message, MqttQualityOfServiceLevel.AtLeastOnce);
_logger.LogInformation($"发送后道标签打印MQTT消息成功: {mqttTopic}");
}
catch (Exception ex)
{
// 记录异常但不影响主流程
_logger.LogError(ex, "发送后道标签打印MQTT消息失败");
}
}
/// <summary>
/// 打印特殊包装标签
/// </summary>

View File

@ -1039,10 +1039,10 @@ namespace ZR.Service.Business
{
return;
}
string mqttTopic = $"shgg_mes/gp12/label_print/print/PGDM";
string labelCode = labelType == 1 ? "Type=DeNoPG" : "Type=DeNoDM";
string path = labelType == 1 ? "D:\\RIZO\\label\\抛光送货单.btw" : "D:\\RIZO\\label\\打磨送货单.btw";
string name = labelType == 1 ? "包装抛光送货单标签打印" : "包装打磨送货单标签打印";
string name = labelType == 1 ? "GP12抛光送货单标签打印" : "GP12打磨送货单标签打印";
var printDto = new PrintDeliveryNoteDto
{
@ -1066,7 +1066,7 @@ namespace ZR.Service.Business
};
string message = JsonSerializer.Serialize(printDto);
_mqttService.PublishAsync("Print/Label", message, MQTTnet.Protocol.MqttQualityOfServiceLevel.AtLeastOnce);
_mqttService.PublishAsync(mqttTopic, message, MQTTnet.Protocol.MqttQualityOfServiceLevel.AtLeastOnce);
}
catch (Exception ex)
{