| | |
| | | using WIDESEA_IStockService; |
| | | using WIDESEA_ITaskInfoService; |
| | | using WIDESEA_Model.Models; |
| | | using WIDESEA_Model.Models.Basic; |
| | | using WIDESEA_Model.Models.Check; |
| | | using WIDESEA_Model.Models.Outbound; |
| | | using static HslCommunication.Profinet.Knx.KnxCode; |
| | |
| | | private readonly HttpClientHelper _httpClientHelper; |
| | | private readonly IBasicService _basicService; |
| | | private readonly IRepository<Dt_TakeStockOrder> _takeStockOrder; |
| | | public readonly IRepository<Dt_LocationType> _locationTypeRepository; |
| | | public readonly IRepository<Dt_WarehouseArea> _warehouseAreaRepository; |
| | | public IRepository<Dt_Task> Repository => BaseDal; |
| | | |
| | | private Dictionary<string, SqlSugar.OrderByType> _taskOrderBy = new() |
| | |
| | | |
| | | public List<int> TaskOutboundTypes => typeof(TaskTypeEnum).GetEnumIndexList(); |
| | | |
| | | public TaskService(IRepository<Dt_Task> BaseDal, IMapper mapper, IUnitOfWorkManage unitOfWorkManage, IRepository<Dt_StockInfo> stockRepository, ILocationInfoService locationInfoService, IInboundOrderService inboundOrderService, ILocationStatusChangeRecordService locationStatusChangeRecordService, IESSApiService eSSApiService, ILogger<TaskService> logger, IStockService stockService, IRecordService recordService, IInboundOrderDetailService inboundOrderDetailService, IOutboundOrderService outboundOrderService, IOutboundOrderDetailService outboundOrderDetailService, IInvokeMESService invokeMESService, IOutStockLockInfoService outStockLockInfoService, IAllocateService allocateService, IRepository<Dt_OutboundBatch> outboundBatchRepository, IRepository<Dt_ReCheckOrder> reCheckOrderRepository, IRepository<Dt_AllocateOrderDetail> allocateOrderDetailRepository, IRepository<Dt_AllocateOrder> allocateOrderRepository, IMaterialUnitService materialUnitService, ITask_HtyService task_HtyService, IRepository<Dt_AllocateMaterialInfo> allocateMaterialInfo, IRepository<Dt_AllocateMaterialInfo_Hty> allocateMaterialInfo_Hty, HttpClientHelper httpClientHelper, IBasicService basicService,IRepository<Dt_TakeStockOrder> takeStockOrder) : base(BaseDal) |
| | | public TaskService(IRepository<Dt_Task> BaseDal, IMapper mapper, IUnitOfWorkManage unitOfWorkManage, IRepository<Dt_StockInfo> stockRepository, ILocationInfoService locationInfoService, IInboundOrderService inboundOrderService, ILocationStatusChangeRecordService locationStatusChangeRecordService, IESSApiService eSSApiService, ILogger<TaskService> logger, IStockService stockService, IRecordService recordService, IInboundOrderDetailService inboundOrderDetailService, IOutboundOrderService outboundOrderService, IOutboundOrderDetailService outboundOrderDetailService, IInvokeMESService invokeMESService, IOutStockLockInfoService outStockLockInfoService, IAllocateService allocateService, IRepository<Dt_OutboundBatch> outboundBatchRepository, IRepository<Dt_ReCheckOrder> reCheckOrderRepository, IRepository<Dt_AllocateOrderDetail> allocateOrderDetailRepository, IRepository<Dt_AllocateOrder> allocateOrderRepository, IMaterialUnitService materialUnitService, ITask_HtyService task_HtyService, IRepository<Dt_AllocateMaterialInfo> allocateMaterialInfo, IRepository<Dt_AllocateMaterialInfo_Hty> allocateMaterialInfo_Hty, HttpClientHelper httpClientHelper, IBasicService basicService,IRepository<Dt_TakeStockOrder> takeStockOrder, IRepository<Dt_LocationType> locationTypeRepository, IRepository<Dt_WarehouseArea> warehouseAreaRepository) : base(BaseDal) |
| | | { |
| | | _mapper = mapper; |
| | | _unitOfWorkManage = unitOfWorkManage; |
| | |
| | | _httpClientHelper = httpClientHelper; |
| | | _basicService = basicService; |
| | | _takeStockOrder = takeStockOrder; |
| | | _locationTypeRepository = locationTypeRepository; |
| | | _warehouseAreaRepository = warehouseAreaRepository; |
| | | } |
| | | |
| | | public async Task TaskStatusChange(string taskNum, TaskStatusEnum taskStatusEnum) |
| | |
| | | return WebResponseContent.Instance.OK(); |
| | | } |
| | | |
| | | public async Task<WebResponseContent> RelocationTaskCompleted(Dt_Task task) |
| | | { |
| | | WebResponseContent content = new WebResponseContent(); |
| | | try |
| | | { |
| | | if (task == null || string.IsNullOrEmpty(task.PalletCode) || string.IsNullOrEmpty(task.TargetAddress)) |
| | | { |
| | | return WebResponseContent.Instance.Error("ç§»åºä»»å¡ä¿¡æ¯ä¸å®æ´ï¼æçå·/ç®æ è´§ä½ä¸ºç©ºï¼"); |
| | | } |
| | | |
| | | // 2. æ¥è¯¢æçåºåä¿¡æ¯ |
| | | Dt_StockInfo stockInfo = await _stockRepository.Db.Queryable<Dt_StockInfo>() |
| | | .Includes(x => x.Details) |
| | | .Where(x => x.PalletCode == task.PalletCode) |
| | | .FirstAsync(); |
| | | |
| | | if (stockInfo == null) |
| | | { |
| | | return WebResponseContent.Instance.Error($"æªæ¾å°æç[{task.PalletCode}]对åºçç»çä¿¡æ¯"); |
| | | } |
| | | |
| | | // é空æçå¿
é¡»ææç» |
| | | if (stockInfo.Details.Count == 0 && stockInfo.PalletType != PalletTypeEnum.Empty.ObjToInt()) |
| | | { |
| | | _logger.LogInformation($"TaskService RelocationTaskCompleted: æªæ¾å°è¯¥æçåºåæç»ä¿¡æ¯.{task.TaskNum}"); |
| | | return WebResponseContent.Instance.Error($"æªæ¾å°è¯¥æç[{task.PalletCode}]åºåæç»ä¿¡æ¯"); |
| | | } |
| | | |
| | | // 3. æ¥è¯¢ç®æ è´§ä½+åè´§ä½ä¿¡æ¯ |
| | | Dt_LocationInfo targetLocationInfo = _locationInfoService.Repository.QueryFirst(x => x.LocationCode == task.TargetAddress); |
| | | if (targetLocationInfo == null) |
| | | { |
| | | return content.Error($"æªæ¾å°å¯¹åºçç»ç¹è´§ä½[{task.TargetAddress}]ä¿¡æ¯"); |
| | | } |
| | | |
| | | // åè´§ä½ä¿¡æ¯ |
| | | Dt_LocationInfo oldLocationInfo = null; |
| | | if (!string.IsNullOrEmpty(stockInfo.LocationCode)) |
| | | { |
| | | oldLocationInfo = _locationInfoService.Repository.QueryFirst(x => x.LocationCode == stockInfo.LocationCode); |
| | | if (oldLocationInfo == null) |
| | | { |
| | | return content.Error($"æªæ¾å°åè´§ä½[{stockInfo.LocationCode}]ä¿¡æ¯"); |
| | | } |
| | | } |
| | | |
| | | // 4. è´§ä½ç¶ææ ¡éª |
| | | if (targetLocationInfo.LocationStatus == LocationStatusEnum.InStock.ObjToInt()) |
| | | { |
| | | return WebResponseContent.Instance.Error($"ç®æ è´§ä½[{task.TargetAddress}]ç¶æä¸æ£ç¡®ï¼å½å为已å ç¨ï¼"); |
| | | } |
| | | |
| | | // 5. å¼å¯äºå¡å¤çæ ¸å¿é»è¾ |
| | | _unitOfWorkManage.BeginTran(); |
| | | |
| | | // 5.1 è®°å½ç®æ è´§ä½åç¶æï¼æ´æ°ä¸ºå ç¨ |
| | | var beforeTargetLocationStatus = targetLocationInfo.LocationStatus; |
| | | targetLocationInfo.LocationStatus = LocationStatusEnum.InStock.ObjToInt(); |
| | | _locationInfoService.Repository.UpdateData(targetLocationInfo); |
| | | |
| | | // 5.2 éæ¾åè´§ä½ |
| | | int beforeOldLocationStatus = 0; |
| | | if (oldLocationInfo != null) |
| | | { |
| | | beforeOldLocationStatus = oldLocationInfo.LocationStatus; |
| | | // åè´§ä½æ¢å¤ä¸ºç©ºé² |
| | | oldLocationInfo.LocationStatus = stockInfo.PalletType == PalletTypeEnum.Empty.ObjToInt() |
| | | ? LocationStatusEnum.Pallet.ObjToInt() |
| | | : LocationStatusEnum.Free.ObjToInt(); |
| | | _locationInfoService.Repository.UpdateData(oldLocationInfo); |
| | | } |
| | | var stockLockInfo = _outStockLockInfoService.Db.Queryable<Dt_OutboundLockInfo_Hty>().Where(x => x.PalletCode == stockInfo.PalletCode && x.TaskNum == task.TaskNum).First(); |
| | | var allocateOrderToWarehouse = _allocateOrderRepository.Db.Queryable<Dt_AllocateOrder>().Where(x => x.OrderNo == stockLockInfo.OrderNo).First(); |
| | | // 5.3 æ´æ°åºåæç»ç¶æï¼ç§»åºå®æï¼ |
| | | stockInfo.Details.ForEach(x => |
| | | { |
| | | x.Status = StockStatusEmun.å
¥åºå®æ.ObjToInt(); |
| | | x.WarehouseCode = allocateOrderToWarehouse.ToWarehouse; |
| | | }); |
| | | _stockService.StockInfoDetailService.Repository.UpdateData(stockInfo.Details); |
| | | |
| | | // 5.4 æ´æ°åºå主信æ¯ï¼ç»å®æ°è´§ä½ï¼ |
| | | string oldLocationCode = stockInfo.LocationCode; // è®°å½åè´§ä½ |
| | | stockInfo.LocationCode = targetLocationInfo.LocationCode; // ç»å®ç®æ è´§ä½ |
| | | stockInfo.PalletCode = task.PalletCode; |
| | | stockInfo.StockStatus = StockStatusEmun.å
¥åºå®æ.ObjToInt(); |
| | | |
| | | var name =_warehouseAreaRepository.Db.Queryable<Dt_WarehouseArea>().Where(x => x.Code == allocateOrderToWarehouse.ToWarehouse).First(); |
| | | var locationType =_locationTypeRepository.QueryFirst(x => x.LocationTypeDesc.Equals(name.Name)); |
| | | if(locationType != null) |
| | | { |
| | | stockInfo.LocationType = locationType.LocationType; |
| | | } |
| | | _stockRepository.UpdateData(stockInfo); |
| | | |
| | | // 5.5 æ´æ°ä»»å¡ç¶æä¸ºå®æ |
| | | task.TaskStatus = TaskStatusEnum.Finish.ObjToInt(); |
| | | var result = _task_HtyService.DeleteAndMoveIntoHty(task, OperateTypeEnum.èªå¨å®æ); |
| | | |
| | | // æäº¤äºå¡ |
| | | _unitOfWorkManage.CommitTran(); |
| | | |
| | | // ä»»å¡å½æ¡£å¤±è´¥åç´æ¥å é¤ |
| | | if (!result) |
| | | { |
| | | await Db.Deleteable(task).ExecuteCommandAsync(); |
| | | } |
| | | |
| | | // 6. å¤çåºåºå+è°æ¨ç©æä¿¡æ¯ |
| | | Dt_OutboundOrder outboundOrder = null; |
| | | //var firstDetail = stockInfo.Details.FirstOrDefault(); |
| | | if (stockLockInfo != null && !string.IsNullOrEmpty(stockLockInfo.OrderNo)) |
| | | { |
| | | outboundOrder = _outboundOrderService.Db.Queryable<Dt_OutboundOrder>() |
| | | .Where(x => x.OrderNo == stockLockInfo.OrderNo) |
| | | .Includes(x => x.Details) |
| | | .First(); |
| | | |
| | | if (outboundOrder != null) |
| | | { |
| | | var allocatInfo =_allocateMaterialInfo.Db.Queryable<Dt_AllocateMaterialInfo>().Where(x => x.OrderNo == outboundOrder.OrderNo).ToList(); |
| | | // å é¤è°æ¨ç©æä¿¡æ¯ |
| | | foreach (var item in allocatInfo) |
| | | { |
| | | var inbounddetail = _allocateMaterialInfo.QueryFirst(x => x.OrderNo == item.OrderNo && x.Barcode == item.Barcode); |
| | | if (inbounddetail != null) |
| | | { |
| | | var alldelete = _allocateMaterialInfo.DeleteAndMoveIntoHty(inbounddetail, OperateTypeEnum.èªå¨å é¤); |
| | | if (!alldelete) |
| | | { |
| | | await Db.Deleteable(task).ExecuteCommandAsync(); |
| | | } |
| | | } |
| | | } |
| | | |
| | | // 7. åè°MES |
| | | string Operator = outboundOrder.Modifier; |
| | | 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>(); |
| | | |
| | | Dt_AllocateMaterialInfo allocateMaterialInfo = _allocateMaterialInfo.QueryFirst(x => x.OrderNo == outboundOrder.OrderNo); |
| | | // ç§»åºåºæ¯ï¼åºåºå®æä¸æªåä¼ MESãæ è°æ¨ç©æä¿¡æ¯æ¶åè° |
| | | if (outboundOrder.OrderStatus == OutOrderStatusEnum.åºåºå®æ.ObjToInt() && outboundOrder.ReturnToMESStatus == 0 && allocateMaterialInfo == null) |
| | | { |
| | | Dt_AllocateOrder allocateOrder = _allocateOrderRepository.QueryFirst(x => x.OrderNo == outboundOrder.OrderNo); |
| | | if (allocateOrder == null) |
| | | { |
| | | return WebResponseContent.Instance.Error($"æªæ¾å°å¯¹åºçè°æ¨å[{outboundOrder.OrderNo}]"); |
| | | } |
| | | |
| | | // æå»ºç§»åºåè°æ°æ® |
| | | AllocationReturnDTO? returnDTO = BuildAllocationFeedbackData( |
| | | outboundOrder, |
| | | allocateOrder.FromWarehouse, |
| | | allocateOrder.ToWarehouse, |
| | | Operator); |
| | | |
| | | if (returnDTO == null) |
| | | { |
| | | return 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; |
| | | } |
| | | |
| | | // 8. å¤çMESåè°ç»æ |
| | | 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}"; |
| | | } |
| | | } |
| | | |
| | | // 9. è®°å½MESåä¼ è®°å½ |
| | | Dt_MesReturnRecord mesReturnRecord = new Dt_MesReturnRecord() |
| | | { |
| | | ApiUrl = httpResponseResult.ApiUrl, |
| | | InterfaceType = outboundOrder.OrderType == 0 ? 1 : 3, // ç§»åºæ¥å£ç±»åï¼å¤ç¨è°æ¨ç±»å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 |
| | | }; |
| | | |
| | | // å¼å¯äºå¡ä¿åMESè®°å½+æ´æ°åºåºåç¶æ |
| | | _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; |
| | | } |
| | | |
| | | _outboundOrderService.Db.Updateable(outboundOrderDetails).ExecuteCommand(); |
| | | _outboundOrderService.UpdateData(outboundOrder); |
| | | |
| | | _unitOfWorkManage.CommitTran(); |
| | | } |
| | | } |
| | | |
| | | // 10. è®°å½è´§ä½ç¶æåæ´ï¼ç®æ è´§ä½+åè´§ä½ï¼ |
| | | try |
| | | { |
| | | // è®°å½ç®æ è´§ä½ç¶æåæ´ |
| | | _locationStatusChangeRecordService.AddLocationStatusChangeRecord( |
| | | targetLocationInfo, |
| | | beforeTargetLocationStatus, |
| | | StockChangeType.Inbound.ObjToInt(), |
| | | $"ç§»åºå
¥åºï¼åè´§ä½ï¼{oldLocationCode}ï¼", |
| | | task.TaskNum); |
| | | |
| | | // è®°å½åè´§ä½ç¶æåæ´ï¼è¥æï¼ |
| | | if (oldLocationInfo != null) |
| | | { |
| | | _locationStatusChangeRecordService.AddLocationStatusChangeRecord( |
| | | oldLocationInfo, |
| | | beforeOldLocationStatus, |
| | | StockChangeType.Outbound.ObjToInt(), |
| | | $"ç§»åºåºåºï¼ç®æ è´§ä½ï¼{targetLocationInfo.LocationCode}ï¼", |
| | | task.TaskNum); |
| | | } |
| | | } |
| | | catch (Exception ex) |
| | | { |
| | | _logger.LogInformation($"RelocationTaskCompleted AddLocationStatusChangeRecord : {ex.Message} "); |
| | | } |
| | | |
| | | return content; |
| | | } |
| | | catch (Exception ex) |
| | | { |
| | | // äºå¡åæ» |
| | | _unitOfWorkManage.RollbackTran(); |
| | | _logger.LogError($"RelocationTaskCompleted å¤ç失败ï¼{ex.Message}", ex); |
| | | return await Task.FromResult(WebResponseContent.Instance.Error($"ç§»åºä»»å¡å¤ç失败ï¼{ex.Message}")); |
| | | } |
| | | } |
| | | |
| | | public HttpResponseResult<MesResponseDTO> responseModel(Dt_InboundOrder order, int InterfaceType, FeedbackInboundRequestModel model = null, AllocateDto allocateDto = null) |
| | | { |
| | | HttpResponseResult<MesResponseDTO> httpResponseResult = new HttpResponseResult<MesResponseDTO>(); |
| | |
| | | ResponseData = httpResponseResult.Content, |
| | | ReturnType = 0, |
| | | ReturnCount = 1, |
| | | ReturnStatus = httpResponseResult.IsSuccess ? 1 : 2, |
| | | ReturnStatus = isSuccess ? 1 : 2, |
| | | SuccessTime = httpResponseResult.IsSuccess ? DateTime.Now : null |
| | | }; |
| | | _unitOfWorkManage.Db.Insertable(mesReturnRecord).ExecuteCommand(); |