using Kean.Infrastructure.Database.Repository.Record; using Kean.Infrastructure.Database.Repository.Record.Entities; using Kean.Infrastructure.Soap.MES.Models; using Microsoft.Extensions.Logging; using Newtonsoft.Json; using SMesService; using System; using System.Threading.Tasks; namespace Kean.Infrastructure.Soap.MES { public sealed class MesService { private readonly ILogger _logger; // 日志 //private readonly IDefaultDb _defaultDb; // 默认数据库 private readonly IRecordDb _recordDb; /// /// 依赖注入 /// public MesService(ILogger logger, //IDefaultDb defaultDb IRecordDb recordDb ) { _logger = logger; //_defaultDb = defaultDb; _recordDb = recordDb; } public async Task Invoke(string function, string paramIn, string barcode, string requestNo) { _logger.LogInformation($"WMS调用MES接口[{function}]开始:参数 requestNo-{requestNo} barcode-{barcode} paramIn-{paramIn}"); T_INTERFACE_RECORD interfaceRecord = new T_INTERFACE_RECORD(); interfaceRecord.DIRECTION = "WMS->MES"; interfaceRecord.METHOD = function; interfaceRecord.BARCODE = barcode; interfaceRecord.REQUEST_NO = requestNo; interfaceRecord.START_TIME = DateTime.Now; MesServiceClient mesServiceClient = new MesServiceClient(); //mesServiceClient.ClientCredentials.UserName.UserName = "user"; //mesServiceClient.ClientCredentials.UserName.Password = "123"; string result = String.Empty; bool bResult = true; try { switch (function) { case "ProductionInfo": result = await mesServiceClient.ProductionInfoAsync(paramIn); break; case "WarehouseInventoryTransfer": result = await mesServiceClient.WarehouseInventoryTransferAsync(paramIn); break; } Production production = JsonConvert.DeserializeObject(result); if (!production.status_code.Equals("0")) { bResult = false; production.message = $"调用MES接口({function})返回失败,返回信息:{production.message})"; } result = JsonConvert.SerializeObject(production); } catch (Exception e) { bResult = false; _logger.LogWarning($"WMS调用MES接口[{function}]异常:参数 requestNo-{requestNo} barcode-{barcode} 原因-{e.Message}"); result = JsonConvert.SerializeObject(new { code = "1", message = $"调用MES接口({function})错误,原因:{e.Message}" }); } try { interfaceRecord.END_TIME = DateTime.Now; interfaceRecord.CREATE_TIME = DateTime.Now; interfaceRecord.PARAM_IN = paramIn; interfaceRecord.PARAM_OUT = result; interfaceRecord.RESULT = bResult?"0":"1"; var temp = await _recordDb.From().Add(interfaceRecord); _recordDb.Save(); } catch(Exception ex) { _logger.LogWarning($"创建WMS调用MES接口[{function}]记录异常。参数 requestNo-{requestNo} barcode-{barcode} 原因-{ex.Message}"); } _logger.LogInformation($"WMS调用MES接口[{function}]结束:参数 requestNo-{requestNo} barcode-{barcode} result-{result}"); return result; } } }