| | |
| | | using System; |
| | | using Newtonsoft.Json; |
| | | using Newtonsoft.Json.Serialization; |
| | | using Org.BouncyCastle.Asn1.Ocsp; |
| | | using SqlSugar; |
| | | 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.LogHelper; |
| | | 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 : IFeedbackMesService |
| | | public class FeedbackMesService : ServiceBase<Dt_MesReturnRecord, IRepository<Dt_MesReturnRecord>>, IFeedbackMesService |
| | | { |
| | | private readonly IUnitOfWorkManage _unitOfWorkManage; |
| | | private readonly HttpClientHelper _httpClientHelper; |
| | | private readonly IRepository<Dt_OutboundOrder> _outboundOrderRepository; |
| | | private readonly IBasicService _basicService; |
| | | private readonly IRepository<Dt_AllocateOrder> _allocateRepository; |
| | | private readonly IRepository<Dt_MaterialCodeInfo> _materialCodeInfoRepository; |
| | | |
| | | public FeedbackMesService(IUnitOfWorkManage unitOfWorkManage, HttpClientHelper httpClientHelper, IRepository<Dt_OutboundOrder> outboundOrderRepository, IBasicService basicService) |
| | | public FeedbackMesService(IRepository<Dt_MesReturnRecord> BaseDal, IUnitOfWorkManage unitOfWorkManage, HttpClientHelper httpClientHelper, IRepository<Dt_OutboundOrder> outboundOrderRepository, IBasicService basicService, IRepository<Dt_AllocateOrder> allocateRepository, IRepository<Dt_MaterialCodeInfo> materialCodeInfoRepository) : base(BaseDal) |
| | | { |
| | | _unitOfWorkManage = unitOfWorkManage; |
| | | _httpClientHelper = httpClientHelper; |
| | | _outboundOrderRepository = outboundOrderRepository; |
| | | _basicService = basicService; |
| | | _allocateRepository = allocateRepository; |
| | | _materialCodeInfoRepository = materialCodeInfoRepository; |
| | | } |
| | | |
| | | public void MaterialOutboundFeedback(string orderNo) |
| | | public WebResponseContent OutboundFeedback(string orderNo) |
| | | { |
| | | WebResponseContent webResponse = new WebResponseContent(); |
| | | try |
| | | { |
| | | Dt_OutboundOrder outboundOrder = _outboundOrderRepository.Db.Queryable<Dt_OutboundOrder>().Where(x => x.OrderNo == orderNo).Includes(x => x.Details).First(); |
| | | if (outboundOrder == null) |
| | | { |
| | | // todo è®°å½æ¥å¿ï¼æªæ¾å°å¯¹åºçåºåºå |
| | | |
| | | return; |
| | | return webResponse = WebResponseContent.Instance.Error($"æªæ¾å°å¯¹åºçåºåºåä¿¡æ¯"); |
| | | } |
| | | |
| | | List<Dt_MesReturnRecord> returnRecords = BaseDal.QueryData(x => x.OrderNo == orderNo && x.OrderId == outboundOrder.Id && x.ReturnStatus == 2); |
| | | |
| | | foreach (var item in returnRecords) |
| | | { |
| | | HttpResponseResult<MesResponseDTO> httpResponse = _httpClientHelper.Post<MesResponseDTO>(item.ApiUrl, item.RequestData); |
| | | |
| | | string rMsg = "æå"; |
| | | bool success = httpResponse.IsSuccess && httpResponse.Data.Code == "200"; |
| | | if (!success) |
| | | { |
| | | if (!httpResponse.IsSuccess) |
| | | { |
| | | rMsg = $"MESæ¥å£è¿åé误ï¼HTTP代ç ï¼{httpResponse.StatusCode}ï¼ä¿¡æ¯ï¼{httpResponse.ErrorMessage}"; |
| | | } |
| | | else if (httpResponse?.Data?.Code != "200") |
| | | { |
| | | rMsg = $"è°ç¨MESæ¥å£å¤±è´¥ï¼ä»£ç ï¼{httpResponse?.Data?.Code}ï¼ä¿¡æ¯ï¼{httpResponse?.Data?.Message}"; |
| | | } |
| | | } |
| | | |
| | | item.ReturnCount += 1; |
| | | |
| | | item.ReturnStatus = success ? 1 : 2; |
| | | item.HttpStatusCode = httpResponse.StatusCode.ObjToInt(); |
| | | item.LastReturnTime = DateTime.Now; |
| | | item.ResponseData = httpResponse.Content; |
| | | item.SuccessTime = success ? DateTime.Now : null; |
| | | } |
| | | |
| | | BaseDal.UpdateData(returnRecords); |
| | | |
| | | HttpResponseResult<MesResponseDTO> httpResponseResult = new HttpResponseResult<MesResponseDTO>(); |
| | | string reqCode = Guid.NewGuid().ToString(); |
| | | string reqTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); |
| | | string requestData = string.Empty; |
| | | List<string> lineNos = new List<string>(); |
| | | if (outboundOrder.OrderType == 0) |
| | | { |
| | | MaterialOutboundReturnDTO? returnDTO = BuildOutboundFeedbackData(outboundOrder); |
| | | if (returnDTO == null) |
| | | { |
| | | return webResponse = WebResponseContent.Instance.Error($"æå»ºåè°å¯¹è±¡å¤±è´¥"); |
| | | } |
| | | |
| | | if (returnDTO.Details.Count <= 0) |
| | | { |
| | | return webResponse = WebResponseContent.Instance.Error($"è¯¥åæ®æ æç»å¯åä¼ "); |
| | | } |
| | | |
| | | string apiUrl = AppSettings.GetValue("MaterialOutboundFeedbackUrl"); |
| | | returnDTO.ReqCode = reqCode; |
| | | returnDTO.ReqTime = reqTime; |
| | | JsonSerializerSettings settings = new JsonSerializerSettings |
| | | { |
| | | ContractResolver = new CamelCasePropertyNamesContractResolver() |
| | | }; |
| | | requestData = JsonConvert.SerializeObject(returnDTO, settings); |
| | | lineNos = returnDTO.Details.Select(x => x.LineNo).ToList(); |
| | | httpResponseResult = _httpClientHelper.Post<MesResponseDTO>(apiUrl, requestData); |
| | | httpResponseResult.ApiUrl = apiUrl; |
| | | } |
| | | else |
| | | { |
| | | Dt_AllocateOrder allocateOrder = _allocateRepository.QueryFirst(x => x.OrderNo == outboundOrder.OrderNo); |
| | | if (allocateOrder == null) |
| | | { |
| | | return webResponse = WebResponseContent.Instance.Error($"æªæ¾å°å¯¹åºçè°æ¨å"); |
| | | } |
| | | |
| | | AllocationReturnDTO? returnDTO = BuildAllocationFeedbackData(outboundOrder, allocateOrder.FromWarehouse, allocateOrder.ToWarehouse); |
| | | if (returnDTO == null) |
| | | { |
| | | return webResponse = WebResponseContent.Instance.Error($"æå»ºåè°å¯¹è±¡å¤±è´¥"); |
| | | } |
| | | |
| | | if (returnDTO.Details.Count <= 0) |
| | | { |
| | | return webResponse = WebResponseContent.Instance.Error($"è¯¥åæ®æ æç»å¯åä¼ "); |
| | | } |
| | | |
| | | string apiUrl = AppSettings.GetValue("AllocationFeedbackUrl"); ; |
| | | returnDTO.ReqCode = reqCode; |
| | | returnDTO.ReqTime = reqTime; |
| | | JsonSerializerSettings settings = new JsonSerializerSettings |
| | | { |
| | | ContractResolver = new CamelCasePropertyNamesContractResolver() |
| | | }; |
| | | requestData = JsonConvert.SerializeObject(returnDTO, settings); |
| | | lineNos = returnDTO.Details.Select(x => x.LineNo).ToList(); |
| | | httpResponseResult = _httpClientHelper.Post<MesResponseDTO>(apiUrl, requestData); |
| | | httpResponseResult.ApiUrl = apiUrl; |
| | | |
| | | Logger.Add(requestData, httpResponseResult.Serialize()); |
| | | } |
| | | |
| | | bool isSuccess = httpResponseResult.IsSuccess && 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 = outboundOrder.OrderType == 0 ? 1 : 3, |
| | | OrderId = outboundOrder.Id, |
| | | OrderNo = outboundOrder.OrderNo, |
| | | RequestCode = reqCode, |
| | | RequestData = requestData, |
| | | FailureReason = message, |
| | | LastReturnTime = DateTime.Now, |
| | | HttpStatusCode = httpResponseResult.StatusCode.ObjToInt(), |
| | | ResponseData = httpResponseResult.Content, |
| | | ReturnType = 0, |
| | | ReturnCount = 1, |
| | | ReturnStatus = isSuccess ? 1 : 2, |
| | | SuccessTime = isSuccess ? DateTime.Now : null |
| | | }; |
| | | |
| | | _unitOfWorkManage.BeginTran(); |
| | | _unitOfWorkManage.Db.Insertable(mesReturnRecord).ExecuteCommand(); |
| | | |
| | | List<Dt_OutboundOrderDetail> 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 = ""; |
| | | }); |
| | | |
| | | mesReturnRecord.ReturnType = outboundOrder.Details.Count == outboundOrderDetails.Count ? 1 : 2; |
| | | |
| | | if (outboundOrder.Details.Count == outboundOrderDetails.Count && outboundOrderDetails.All(x => x.ReturnToMESStatus == 1 || x.ReturnToMESStatus == 2)) |
| | | { |
| | | outboundOrder.ReturnToMESStatus = isSuccess ? 1 : 2; |
| | | } |
| | | else |
| | | { |
| | | outboundOrder.ReturnToMESStatus = isSuccess ? 3 : 4; |
| | | } |
| | | |
| | | _outboundOrderRepository.Db.Updateable(outboundOrderDetails).ExecuteCommand(); |
| | | _outboundOrderRepository.UpdateData(outboundOrder); |
| | | |
| | | _unitOfWorkManage.CommitTran(); |
| | | int successCount = returnRecords.Where(x => x.ReturnStatus == 1).Count() + (isSuccess ? 1 : 0); |
| | | int failCount = returnRecords.Where(x => x.ReturnStatus == 2).Count() + (isSuccess ? 1 : 0); |
| | | |
| | | webResponse.Status = true; |
| | | webResponse.Message = $"åè°æåæ¡æ°ï¼{successCount}ï¼åè°å¤±è´¥æ¡æ°ï¼{failCount}"; |
| | | |
| | | return webResponse; |
| | | } |
| | | catch (Exception ex) |
| | | { |
| | | return webResponse = WebResponseContent.Instance.Error(ex.Message); |
| | | } |
| | | finally |
| | | { |
| | | Logger.Add(orderNo, webResponse.Serialize()); |
| | | } |
| | | } |
| | | |
| | | public AllocationReturnDTO? BuildAllocationFeedbackData(Dt_OutboundOrder outboundOrder, string fromWarehouse, string toWarehouse) |
| | | { |
| | | try |
| | | { |
| | | List<Dt_OutboundOrderDetail> details = outboundOrder.Details; |
| | | |
| | | List<AllocationDetail> returnDetails = new List<AllocationDetail>(); |
| | | |
| | | foreach (var detail in details) |
| | | { |
| | | List<Barcodes>? barcodes = JsonConvert.DeserializeObject<List<Barcodes>>(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.UpperOrderNo, |
| | | FromWarehouse = fromWarehouse, |
| | | ToWarehouse = toWarehouse, |
| | | Operator = App.User.UserName |
| | | }; |
| | | |
| | | return outboundReturnDTO; |
| | | |
| | | } |
| | | catch (Exception ex) |
| | | { |
| | | return null; |
| | | } |
| | | } |
| | | |
| | | public void MaterialOutboundFeedback(Dt_OutboundOrder outboundOrder) |
| | | { |
| | | try |
| | | { |
| | | MaterialOutboundReturnDTO? returnDTO = BuildOutboundFeedbackData(outboundOrder); |
| | | if (returnDTO != null) |
| | | { |
| | |
| | | InterfaceType = 1, |
| | | OrderId = outboundOrder.Id, |
| | | OrderNo = outboundOrder.OrderNo, |
| | | OrderType = outboundOrder.OrderType, |
| | | RequestCode = returnDTO.ReqCode, |
| | | RequestData = returnDTO.Serialize(), |
| | | FailureReason = message, |
| | |
| | | { |
| | | throw new Exception(ex.Message); |
| | | } |
| | | |
| | | } |
| | | |
| | | public MaterialOutboundReturnDTO? BuildOutboundFeedbackData(Dt_OutboundOrder outboundOrder) |
| | |
| | | |
| | | foreach (var detail in details) |
| | | { |
| | | List<Barcodes>? barcodes = JsonConvert.DeserializeObject<List<Barcodes>>(detail.ReturnJsonData); |
| | | if (barcodes != null && barcodes.Any()) |
| | | if (!string.IsNullOrWhiteSpace(detail.ReturnJsonData)) |
| | | { |
| | | 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 |
| | | List<Barcodes>? barcodes = JsonConvert.DeserializeObject<List<Barcodes>>(detail.ReturnJsonData); |
| | | if (barcodes != null && barcodes.Any()) |
| | | { |
| | | Barcodes = barcodes, |
| | | CurrentDeliveryQty = currentResult.ToQuantity, |
| | | LineNo = detail.lineNo, |
| | | MaterialCode = detail.MaterielCode, |
| | | Qty = totalResult.ToQuantity, |
| | | WarehouseCode = detail.WarehouseCode, |
| | | Unit = detail.BarcodeUnit |
| | | }); |
| | | 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 |
| | | }); |
| | | } |
| | | } |
| | | } |
| | | |
| | |
| | | { |
| | | Business_type = outboundOrder.BusinessType, |
| | | Details = returnDetails, |
| | | DocumentsNO = "", |
| | | DocumentsNO = _basicService.CreateCodeByRule("OutboundOrderRule"), |
| | | FactoryArea = outboundOrder.FactoryArea, |
| | | OperationType = 1, |
| | | Operator = App.User.UserName, |
| | | OrderNo = outboundOrder.OrderNo, |
| | | OrderNo = outboundOrder.UpperOrderNo, |
| | | Status = 1 |
| | | }; |
| | | |
| | |
| | | return null; |
| | | } |
| | | } |
| | | |
| | | public WebResponseContent BarcodeFeedback(string newBarcode) |
| | | { |
| | | try |
| | | { |
| | | Dt_MaterialCodeInfo materialCodeInfo = _materialCodeInfoRepository.QueryFirst(x => x.NewBarcode == newBarcode, new Dictionary<string, OrderByType> { { nameof(Dt_MaterialCodeInfo.Id), OrderByType.Desc } }); |
| | | if (materialCodeInfo == null) |
| | | { |
| | | return WebResponseContent.Instance.Error($"æªæ¾å°ç©ææ¡ç ä¿¡æ¯{newBarcode}"); |
| | | } |
| | | |
| | | string apiUrl = AppSettings.GetValue("BarcodeFeedbackUrl"); |
| | | |
| | | BarcodeReturnDTO returnDTO = new BarcodeReturnDTO() |
| | | { |
| | | BatchNo = materialCodeInfo.BatchNo, |
| | | FactoryArea = materialCodeInfo.FactoryArea, |
| | | OperationType = 1, |
| | | MaterialCode = materialCodeInfo.MaterialCode, |
| | | NewmaterialCode = materialCodeInfo.NewBarcode, |
| | | OldmaterialCode = materialCodeInfo.OldBarcode, |
| | | Operator = materialCodeInfo.Creater, |
| | | Qty = materialCodeInfo.AfterQuantity, |
| | | SupplyCode = materialCodeInfo.SuplierCode, |
| | | Unit = materialCodeInfo.Unit, |
| | | WarehouseCode = materialCodeInfo.WarehouseCode, |
| | | OldQty = materialCodeInfo.OriginalQuantity |
| | | }; |
| | | |
| | | string reqCode = Guid.NewGuid().ToString(); |
| | | string reqTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); |
| | | JsonSerializerSettings settings = new JsonSerializerSettings |
| | | { |
| | | ContractResolver = new CamelCasePropertyNamesContractResolver() |
| | | }; |
| | | |
| | | string requestData = JsonConvert.SerializeObject(returnDTO, settings); |
| | | HttpResponseResult<MesResponseDTO> httpResponseResult = _httpClientHelper.Post<MesResponseDTO>(apiUrl, requestData); |
| | | httpResponseResult.ApiUrl = apiUrl; |
| | | |
| | | bool isSuccess = httpResponseResult.IsSuccess && 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 = materialCodeInfo.OrderId ?? 0, |
| | | OrderNo = materialCodeInfo.OrderNo ?? "", |
| | | 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(); |
| | | materialCodeInfo.ReturnStatus = httpResponseResult.IsSuccess ? 1 : 2; |
| | | _materialCodeInfoRepository.UpdateData(materialCodeInfo); |
| | | |
| | | _unitOfWorkManage.CommitTran(); |
| | | |
| | | WebResponseContent responseContent = new WebResponseContent(); |
| | | responseContent.Status = isSuccess; |
| | | responseContent.Message = message; |
| | | return responseContent; |
| | | } |
| | | catch (Exception ex) |
| | | { |
| | | return WebResponseContent.Instance.Error(ex.Message); |
| | | } |
| | | } |
| | | } |
| | | } |