using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Newtonsoft.Json; using WIDESEA_Core; using WIDESEA_Core.BaseRepository; using WIDESEA_Core.BaseServices; using WIDESEA_Core.Helper; using WIDESEA_Core.HttpContextUser; using WIDESEA_Core.Util; using WIDESEA_DTO.Base; using WIDESEA_DTO.ReturnMES; using WIDESEA_IBasicService; using WIDESEA_Model.Models; using static HslCommunication.Profinet.Knx.KnxCode; namespace WIDESEA_BasicService.MESOperation { public class FeedbackMesService : ServiceBase>, IFeedbackMesService { private readonly IUnitOfWorkManage _unitOfWorkManage; private readonly HttpClientHelper _httpClientHelper; private readonly IRepository _outboundOrderRepository; private readonly IBasicService _basicService; private readonly IRepository _allocateRepository; public FeedbackMesService(IRepository BaseDal, IUnitOfWorkManage unitOfWorkManage, HttpClientHelper httpClientHelper, IRepository outboundOrderRepository, IBasicService basicService, IRepository allocateRepository) : base(BaseDal) { _unitOfWorkManage = unitOfWorkManage; _httpClientHelper = httpClientHelper; _outboundOrderRepository = outboundOrderRepository; _basicService = basicService; _allocateRepository = allocateRepository; } public WebResponseContent OutboundFeedback(string orderNo) { try { Dt_OutboundOrder outboundOrder = _outboundOrderRepository.Db.Queryable().Where(x => x.OrderNo == orderNo).Includes(x => x.Details).First(); if (outboundOrder == null) { return WebResponseContent.Instance.Error($"未找到对应的出库单信息"); } HttpResponseResult httpResponseResult = new HttpResponseResult(); string reqCode = Guid.NewGuid().ToString(); string reqTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); string requestData = string.Empty; List lineNos = new List(); if (outboundOrder.OrderType == 0) { MaterialOutboundReturnDTO? returnDTO = BuildOutboundFeedbackData(outboundOrder); if (returnDTO == null) { return WebResponseContent.Instance.Error($"构建回调对象失败"); } string apiUrl = ""; returnDTO.ReqCode = reqCode; returnDTO.ReqTime = reqTime; requestData = returnDTO.Serialize(); lineNos = returnDTO.Details.Select(x => x.LineNo).ToList(); httpResponseResult = _httpClientHelper.Post(apiUrl, requestData); httpResponseResult.ApiUrl = apiUrl; } else { Dt_AllocateOrder allocateOrder = _allocateRepository.QueryFirst(x => x.OrderNo == outboundOrder.OrderNo); if (allocateOrder == null) { return WebResponseContent.Instance.Error($"未找到对应的调拨单"); } AllocationReturnDTO? returnDTO = BuildAllocationFeedbackData(outboundOrder, allocateOrder.FromWarehouse, allocateOrder.ToWarehouse); if (returnDTO == null) { return WebResponseContent.Instance.Error($"构建回调对象失败"); } string apiUrl = ""; returnDTO.ReqCode = reqCode; returnDTO.ReqTime = reqTime; requestData = returnDTO.Serialize(); lineNos = returnDTO.Details.Select(x => x.LineNo).ToList(); httpResponseResult = _httpClientHelper.Post(apiUrl, requestData); httpResponseResult.ApiUrl = apiUrl; } bool isSuccess = httpResponseResult.IsSuccess && httpResponseResult.Data != null && httpResponseResult.Data.Code == "200"; string message = "成功"; if (!isSuccess) { if (!httpResponseResult.IsSuccess) { message = $"MES接口返回错误,HTTP代码:{httpResponseResult.StatusCode},信息:{httpResponseResult.ErrorMessage}"; } else if (httpResponseResult.Data.Code != "200") { message = $"调用MES接口失败,代码:{httpResponseResult.Data.Code},信息:{httpResponseResult.Data.Message}"; } } Dt_MesReturnRecord mesReturnRecord = new Dt_MesReturnRecord() { ApiUrl = httpResponseResult.ApiUrl, InterfaceType = 1, OrderId = outboundOrder.Id, OrderNo = outboundOrder.OrderNo, OrderType = outboundOrder.OrderType, RequestCode = reqCode, RequestData = requestData, FailureReason = message, LastReturnTime = DateTime.Now, HttpStatusCode = httpResponseResult.StatusCode.ObjToInt(), ResponseData = httpResponseResult.Content, ReturnType = 0, ReturnCount = 1, ReturnStatus = httpResponseResult.IsSuccess ? 1 : 2, SuccessTime = httpResponseResult.IsSuccess ? DateTime.Now : null }; _unitOfWorkManage.BeginTran(); _unitOfWorkManage.Db.Insertable(mesReturnRecord).ExecuteCommand(); if (isSuccess) { List outboundOrderDetails = outboundOrder.Details.Where(x => lineNos.Contains(x.lineNo)).ToList(); outboundOrderDetails.ForEach(x => { if (x.OverOutQuantity == x.OrderQuantity - x.MoveQty) { x.ReturnToMESStatus = isSuccess ? 1 : 2; } else { x.ReturnToMESStatus = isSuccess ? 3 : 4; } x.CurrentDeliveryQty = 0; x.ReturnJsonData = ""; }); _outboundOrderRepository.Db.Updateable(outboundOrderDetails).ExecuteCommand(); } _unitOfWorkManage.CommitTran(); WebResponseContent responseContent = new WebResponseContent(); responseContent.Status = isSuccess; responseContent.Message = message; return responseContent; } catch(Exception ex) { return WebResponseContent.Instance.Error(ex.Message); } } public AllocationReturnDTO? BuildAllocationFeedbackData(Dt_OutboundOrder outboundOrder, string fromWarehouse, string toWarehouse) { try { List details = outboundOrder.Details; List returnDetails = new List(); foreach (var detail in details) { List? barcodes = JsonConvert.DeserializeObject>(detail.ReturnJsonData); if (barcodes != null && barcodes.Any()) { UnitConvertResultDTO currentResult = _basicService.UnitQuantityConvert(detail.MaterielCode, detail.Unit, detail.BarcodeUnit, detail.CurrentDeliveryQty); UnitConvertResultDTO totalResult = _basicService.UnitQuantityConvert(detail.MaterielCode, detail.Unit, detail.BarcodeUnit, detail.OrderQuantity); returnDetails.Add(new AllocationDetail { Barcodes = barcodes, BatchNo = detail.BatchNo, LineNo = detail.lineNo, MaterialCode = detail.MaterielCode, Qty = totalResult.ToQuantity, WarehouseCode = detail.WarehouseCode, Unit = detail.BarcodeUnit }); } } AllocationReturnDTO outboundReturnDTO = new AllocationReturnDTO() { Business_type = outboundOrder.BusinessType, Details = returnDetails, FactoryArea = outboundOrder.FactoryArea, OperationType = 1, OrderNo = outboundOrder.OrderNo, FromWarehouse = fromWarehouse, ToWarehouse = toWarehouse }; return outboundReturnDTO; } catch (Exception ex) { return null; } } public void MaterialOutboundFeedback(Dt_OutboundOrder outboundOrder) { try { MaterialOutboundReturnDTO? returnDTO = BuildOutboundFeedbackData(outboundOrder); if (returnDTO != null) { string apiUrl = ""; HttpResponseResult httpResponseResult = _httpClientHelper.Post(apiUrl, returnDTO.Serialize()); bool isSuccess = httpResponseResult.IsSuccess && httpResponseResult.Data != null && httpResponseResult.Data.Code == "200"; string message = ""; if (!isSuccess) { if (httpResponseResult.IsSuccess) { message = $"MES接口返回错误,HTTP代码:{httpResponseResult.StatusCode},信息:{httpResponseResult.ErrorMessage}"; } else if (httpResponseResult.Data.Code != "200") { message = $"调用MES接口失败,代码:{httpResponseResult.Data.Code},信息:{httpResponseResult.Data.Message}"; } } Dt_MesReturnRecord mesReturnRecord = new Dt_MesReturnRecord() { ApiUrl = apiUrl, InterfaceType = 1, OrderId = outboundOrder.Id, OrderNo = outboundOrder.OrderNo, OrderType = outboundOrder.OrderType, RequestCode = returnDTO.ReqCode, RequestData = returnDTO.Serialize(), FailureReason = message, LastReturnTime = DateTime.Now, HttpStatusCode = httpResponseResult.StatusCode.ObjToInt(), ResponseData = httpResponseResult.Content, ReturnType = 0, ReturnCount = 1, ReturnStatus = httpResponseResult.IsSuccess ? 1 : 2, SuccessTime = httpResponseResult.IsSuccess ? DateTime.Now : null }; _unitOfWorkManage.BeginTran(); _unitOfWorkManage.Db.Insertable(mesReturnRecord).ExecuteCommand(); List lineNos = returnDTO.Details.Select(x => x.LineNo).ToList(); List outboundOrderDetails = outboundOrder.Details.Where(x => lineNos.Contains(x.lineNo)).ToList(); outboundOrderDetails.ForEach(x => { if (x.OverOutQuantity == x.OrderQuantity - x.MoveQty) { x.ReturnToMESStatus = isSuccess ? 1 : 2; } else { x.ReturnToMESStatus = isSuccess ? 3 : 4; } x.CurrentDeliveryQty = 0; x.ReturnJsonData = ""; }); _outboundOrderRepository.Db.Updateable(outboundOrderDetails).ExecuteCommand(); _unitOfWorkManage.CommitTran(); } } catch (Exception ex) { throw new Exception(ex.Message); } } public MaterialOutboundReturnDTO? BuildOutboundFeedbackData(Dt_OutboundOrder outboundOrder) { try { List details = outboundOrder.Details; List returnDetails = new List(); foreach (var detail in details) { if (!string.IsNullOrWhiteSpace(detail.ReturnJsonData)) { List? barcodes = JsonConvert.DeserializeObject>(detail.ReturnJsonData); if (barcodes != null && barcodes.Any()) { UnitConvertResultDTO currentResult = _basicService.UnitQuantityConvert(detail.MaterielCode, detail.Unit, detail.BarcodeUnit, detail.CurrentDeliveryQty); UnitConvertResultDTO totalResult = _basicService.UnitQuantityConvert(detail.MaterielCode, detail.Unit, detail.BarcodeUnit, detail.OrderQuantity); returnDetails.Add(new MaterialOutboundDetail { Barcodes = barcodes, CurrentDeliveryQty = currentResult.ToQuantity, LineNo = detail.lineNo, MaterialCode = detail.MaterielCode, Qty = totalResult.ToQuantity, WarehouseCode = detail.WarehouseCode, Unit = detail.BarcodeUnit }); } } } MaterialOutboundReturnDTO outboundReturnDTO = new MaterialOutboundReturnDTO() { Business_type = outboundOrder.BusinessType, Details = returnDetails, DocumentsNO = "", FactoryArea = outboundOrder.FactoryArea, OperationType = 1, Operator = App.User.UserName, OrderNo = outboundOrder.OrderNo, Status = 1 }; return outboundReturnDTO; } catch (Exception ex) { return null; } } } }