From f6e826c2a71fb2567db7d1f8e616870ded8d9aee Mon Sep 17 00:00:00 2001 From: quowingwang Date: Mon, 26 Jan 2026 14:46:56 +0800 Subject: [PATCH] =?UTF-8?q?PLC=E8=BF=87=E7=AB=99=E8=AE=B0=E5=BD=95?= =?UTF-8?q?=E8=A1=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../PlcOperationResultController.cs | 99 +++++++++++++++++++ .../PLC/Model/Dto/PlcOperationResultDto.cs | 41 ++++++++ .../PLC/Model/PlcOperationResult.cs | 62 ++++++++++++ .../IService/IPlcOperationResultService.cs | 23 +++++ .../PLC/Service/PlcOperationResultService.cs | 82 +++++++++++++++ RIZO.Admin.WebApi/PLC/Service/PlcService.cs | 32 +++--- 6 files changed, 328 insertions(+), 11 deletions(-) create mode 100644 RIZO.Admin.WebApi/PLC/Controllers/PlcOperationResultController.cs create mode 100644 RIZO.Admin.WebApi/PLC/Model/Dto/PlcOperationResultDto.cs create mode 100644 RIZO.Admin.WebApi/PLC/Model/PlcOperationResult.cs create mode 100644 RIZO.Admin.WebApi/PLC/Service/IService/IPlcOperationResultService.cs create mode 100644 RIZO.Admin.WebApi/PLC/Service/PlcOperationResultService.cs diff --git a/RIZO.Admin.WebApi/PLC/Controllers/PlcOperationResultController.cs b/RIZO.Admin.WebApi/PLC/Controllers/PlcOperationResultController.cs new file mode 100644 index 0000000..67b18be --- /dev/null +++ b/RIZO.Admin.WebApi/PLC/Controllers/PlcOperationResultController.cs @@ -0,0 +1,99 @@ +using Microsoft.AspNetCore.Mvc; +using RIZO.Admin.WebApi.PLC.Model; +using RIZO.Admin.WebApi.PLC.Model.Dto; +using RIZO.Admin.WebApi.PLC.Service.IService; +//创建时间:2026-01-17 +namespace RIZO.Admin.WebApi.PLC.Controllers +{ + /// + /// PLC返回的工位工序结果 + /// + [Route("mes/PlcOperationResult")] + public class PlcOperationResultController : BaseController + { + /// + /// PLC返回的工位工序结果接口 + /// + private readonly IPlcOperationResultService _PlcOperationResultService; + + public PlcOperationResultController(IPlcOperationResultService PlcOperationResultService) + { + _PlcOperationResultService = PlcOperationResultService; + } + + /// + /// 查询PLC返回的工位工序结果列表 + /// + /// + /// + [HttpGet("list")] + [ActionPermissionFilter(Permission = "plcoperationresult:list")] + public IActionResult QueryPlcOperationResult([FromQuery] PlcOperationResultQueryDto parm) + { + var response = _PlcOperationResultService.GetList(parm); + return SUCCESS(response); + } + + + /// + /// 查询PLC返回的工位工序结果详情 + /// + /// + /// + [HttpGet("{Id}")] + [ActionPermissionFilter(Permission = "plcoperationresult:query")] + public IActionResult GetPlcOperationResult(int Id) + { + var response = _PlcOperationResultService.GetInfo(Id); + + var info = response.Adapt(); + return SUCCESS(info); + } + + /// + /// 添加PLC返回的工位工序结果 + /// + /// + [HttpPost] + [ActionPermissionFilter(Permission = "plcoperationresult:add")] + [Log(Title = "PLC返回的工位工序结果", BusinessType = BusinessType.INSERT)] + public IActionResult AddPlcOperationResult([FromBody] PlcOperationResultDto parm) + { + var modal = parm.Adapt().ToCreate(HttpContext); + + var response = _PlcOperationResultService.AddPlcOperationResult(modal); + + return SUCCESS(response); + } + + /// + /// 更新PLC返回的工位工序结果 + /// + /// + [HttpPut] + [ActionPermissionFilter(Permission = "plcoperationresult:edit")] + [Log(Title = "PLC返回的工位工序结果", BusinessType = BusinessType.UPDATE)] + public IActionResult UpdatePlcOperationResult([FromBody] PlcOperationResultDto parm) + { + var modal = parm.Adapt().ToUpdate(HttpContext); + var response = _PlcOperationResultService.UpdatePlcOperationResult(modal); + + return ToResponse(response); + } + + /// + /// 删除PLC返回的工位工序结果 + /// + /// + [HttpPost("delete/{ids}")] + [ActionPermissionFilter(Permission = "plcoperationresult:delete")] + [Log(Title = "PLC返回的工位工序结果", BusinessType = BusinessType.DELETE)] + public IActionResult DeletePlcOperationResult([FromRoute]string ids) + { + var idArr = Tools.SplitAndConvert(ids); + + return ToResponse(_PlcOperationResultService.Delete(idArr)); + } + + } +} \ No newline at end of file diff --git a/RIZO.Admin.WebApi/PLC/Model/Dto/PlcOperationResultDto.cs b/RIZO.Admin.WebApi/PLC/Model/Dto/PlcOperationResultDto.cs new file mode 100644 index 0000000..344e170 --- /dev/null +++ b/RIZO.Admin.WebApi/PLC/Model/Dto/PlcOperationResultDto.cs @@ -0,0 +1,41 @@ +using MiniExcelLibs.Attributes; +using RIZO.Model; +using System.ComponentModel.DataAnnotations; + +namespace RIZO.Admin.WebApi.PLC.Model.Dto +{ + /// + /// PLC返回的工位工序结果查询对象 + /// + public class PlcOperationResultQueryDto : PagerInfo + { + } + + /// + /// PLC返回的工位工序结果输入输出对象 + /// + public class PlcOperationResultDto + { + [Required(ErrorMessage = "ID不能为空")] + public int Id { get; set; } + + public string? Sn { get; set; } + + public string? Workstationcode { get; set; } + + public int? Result { get; set; } + + public int? Qty { get; set; } + + public string? CreatedBy { get; set; } + + public DateTime? CreatedTime { get; set; } + + public string? UpdatedBy { get; set; } + + public DateTime? UpdatedTime { get; set; } + + + + } +} \ No newline at end of file diff --git a/RIZO.Admin.WebApi/PLC/Model/PlcOperationResult.cs b/RIZO.Admin.WebApi/PLC/Model/PlcOperationResult.cs new file mode 100644 index 0000000..c5f5dc6 --- /dev/null +++ b/RIZO.Admin.WebApi/PLC/Model/PlcOperationResult.cs @@ -0,0 +1,62 @@ +using SqlSugar; + +namespace RIZO.Admin.WebApi.PLC.Model +{ + /// + /// PLC返回的工位工序结果 + /// + [SugarTable("plc_operation_result")] + public class PlcOperationResult + { + /// + /// ID + /// + [SugarColumn(IsPrimaryKey = true, IsIdentity = true)] + public int Id { get; set; } + + /// + /// 条码 + /// + public string Sn { get; set; } + + /// + /// 工站编码 + /// + public string Workstationcode { get; set; } + + /// + /// 工站结果;1OK,2NG + /// + public int? Result { get; set; } + + /// + /// 数量 + /// + public int? Qty { get; set; } + + /// + /// 创建人 + /// + [SugarColumn(ColumnName = "created_by")] + public string CreatedBy { get; set; } + + /// + /// 创建时间 + /// + [SugarColumn(ColumnName = "created_time")] + public DateTime? CreatedTime { get; set; } + + /// + /// 更新人 + /// + [SugarColumn(ColumnName = "updated_by")] + public string UpdatedBy { get; set; } + + /// + /// 更新时间 + /// + [SugarColumn(ColumnName = "updated_time")] + public DateTime? UpdatedTime { get; set; } + + } +} \ No newline at end of file diff --git a/RIZO.Admin.WebApi/PLC/Service/IService/IPlcOperationResultService.cs b/RIZO.Admin.WebApi/PLC/Service/IService/IPlcOperationResultService.cs new file mode 100644 index 0000000..e16db04 --- /dev/null +++ b/RIZO.Admin.WebApi/PLC/Service/IService/IPlcOperationResultService.cs @@ -0,0 +1,23 @@ +using RIZO.Admin.WebApi.PLC.Model; +using RIZO.Admin.WebApi.PLC.Model.Dto; +using RIZO.Model; +using RIZO.ServiceCore; + +namespace RIZO.Admin.WebApi.PLC.Service.IService +{ + /// + /// PLC返回的工位工序结果service接口 + /// + public interface IPlcOperationResultService : IBaseService + { + PagedInfo GetList(PlcOperationResultQueryDto parm); + + PlcOperationResult GetInfo(int Id); + + + PlcOperationResult AddPlcOperationResult(PlcOperationResult parm); + int UpdatePlcOperationResult(PlcOperationResult parm); + + + } +} diff --git a/RIZO.Admin.WebApi/PLC/Service/PlcOperationResultService.cs b/RIZO.Admin.WebApi/PLC/Service/PlcOperationResultService.cs new file mode 100644 index 0000000..9799253 --- /dev/null +++ b/RIZO.Admin.WebApi/PLC/Service/PlcOperationResultService.cs @@ -0,0 +1,82 @@ +using Infrastructure.Attribute; +using Infrastructure.Extensions; +using RIZO.Admin.WebApi.PLC.Model; +using RIZO.Admin.WebApi.PLC.Model.Dto; +using RIZO.Admin.WebApi.PLC.Service.IService; +using RIZO.Model; +using RIZO.Repository; +using RIZO.ServiceCore; +using SqlSugar; + +namespace RIZO.Admin.WebApi.PLC.Service +{ + /// + /// PLC返回的工位工序结果Service业务层处理 + /// + [AppService(ServiceType = typeof(IPlcOperationResultService), ServiceLifetime = LifeTime.Transient)] + public class PlcOperationResultService : BaseService, IPlcOperationResultService + { + /// + /// 查询PLC返回的工位工序结果列表 + /// + /// + /// + public PagedInfo GetList(PlcOperationResultQueryDto parm) + { + var predicate = QueryExp(parm); + + var response = Queryable() + .Where(predicate.ToExpression()) + .ToPage(parm); + + return response; + } + + + /// + /// 获取详情 + /// + /// + /// + public PlcOperationResult GetInfo(int Id) + { + var response = Queryable() + .Where(x => x.Id == Id) + .First(); + + return response; + } + + /// + /// 添加PLC返回的工位工序结果 + /// + /// + /// + public PlcOperationResult AddPlcOperationResult(PlcOperationResult model) + { + return Insertable(model).ExecuteReturnEntity(); + } + + /// + /// 修改PLC返回的工位工序结果 + /// + /// + /// + public int UpdatePlcOperationResult(PlcOperationResult model) + { + return Update(model, true); + } + + /// + /// 查询导出表达式 + /// + /// + /// + private static Expressionable QueryExp(PlcOperationResultQueryDto parm) + { + var predicate = Expressionable.Create(); + + return predicate; + } + } +} \ No newline at end of file diff --git a/RIZO.Admin.WebApi/PLC/Service/PlcService.cs b/RIZO.Admin.WebApi/PLC/Service/PlcService.cs index dc0495b..dc5ad46 100644 --- a/RIZO.Admin.WebApi/PLC/Service/PlcService.cs +++ b/RIZO.Admin.WebApi/PLC/Service/PlcService.cs @@ -38,10 +38,18 @@ namespace RIZO.Admin.WebApi.PLC.Service private readonly ConcurrentDictionary _plcConnPool = new(); // 连接池 #region PLC地址块儿映射 - //保存请求返回结果 - private readonly Dictionary _saveRequestReturnMap = new() - { - { "保存请求返回", "DB1050.DBW2002" }, // Int + //MES返回PLC请求映射 + private readonly Dictionary _mesIntReturnMap = new() + { + { "设备使能", "DB1050.DBW0" }, // Int + { "工位开始查询结果", "DB1050.DBW2000" }, // Int + { "保存结果", "DB1050.DBW2002" }, // Int + }; + + private readonly Dictionary _mesStringReturnMap = new() + { + { "产品型号", ("DB1001.DBB1016", 14) }, // String[14] + { "订单下发", ("DB1001.DBB1032", 50) }, // String[50] }; // OP020-2 专属地址映射(合盖工位,DB1001) @@ -488,7 +496,7 @@ namespace RIZO.Admin.WebApi.PLC.Service { if (t.IsFaulted && plc is { IsConnected: true }) { - WritePlcSaveRequestResult(plc, ip, plcName, "6"); + WritePlcSaveRequestResult(plc, ip, plcName, prodData, "6"); Console.WriteLine($"{plcName}({ip})数据保存失败:{t.Exception?.InnerException?.Message ?? t.Exception?.Message ?? "未知错误"}"); } }, TaskContinuationOptions.OnlyOnFaulted); @@ -499,7 +507,7 @@ namespace RIZO.Admin.WebApi.PLC.Service var successMsg = isConnReused ? $"{plcName}生产数据读取成功(复用连接)" : $"{plcName}生产数据读取成功(新建连接)"; - WritePlcSaveRequestResult(plc, ip, plcName, "1"); + WritePlcSaveRequestResult(plc, ip, plcName, prodData, "1"); return (true, prodData, successMsg); } @@ -2127,16 +2135,18 @@ namespace RIZO.Admin.WebApi.PLC.Service } // 提取写PLC返回值的通用方法,统一处理逻辑和日志 - private void WritePlcSaveRequestResult(Plc plc, string ip, string plcName, string returnValue) + private void WritePlcSaveRequestResult(Plc plc, string ip, string plcName, PlcProductionData prodData, string saveResult) { try { - WritePlcValue(plc, _saveRequestReturnMap["保存请求返回"], returnValue); + WritePlcValue(plc, _mesIntReturnMap["设备使能"], "1"); + //WritePlcValue(plc, _mesIntReturnMap["工位开始查询结果"], "1"); + WritePlcValue(plc, _mesIntReturnMap["保存结果"], saveResult); + WritePlcValue(plc, "产品型号", prodData.ProductModel); + WritePlcValue(plc, "订单下发", "");//?? } catch (Exception ex) - { - // 标准化日志格式,包含更多上下文信息 - Console.WriteLine($"[{DateTime.Now}]{plcName}({ip}) 写保存请求返回值失败[目标值:{returnValue}]:{ex.Message}"); + {; } } ///