222
schangxiang@126.com
2024-12-05 2225959bc5c3588407e90a6285ab2c32b3481d90
CC/iWareCC_ASRS/ThreadService/02_BZ39¹¤Î»£¨²¹°åºó¹¤Î»£©/Outbound/2¡¢DataProcess_RobotBuffer_IssueOutboundTask.cs
@@ -24,99 +24,132 @@
    /// </summary>
    public static class DataProcess_RobotBuffer_IssueOutboundTask
    {
        public static async void Handler()
        {
            var alertMsg = "";
            LogType logType = LogType.DataProcess_BZ39;
            LogType logType = LogType.DataProcess_RobotBuffer_IssueOutboundTask;
            while (true)
            {
                Thread.Sleep(2000);//休眠2秒,将休眠写到前面,是为了下面的continue方法执行后不显示错误信息的提示!!!【EditBy shaocx,2022-05-24】
                SystemWarningMsg._lbl_alert_DataProcess_BZ39 = string.Empty;
                Thread.Sleep(1000);//休眠2秒,将休眠写到前面,是为了下面的continue方法执行后不显示错误信息的提示!!!【EditBy shaocx,2022-05-24】
                SystemWarningMsg._lbl_Alert_DataProcess_RobotBuffer_IssueOutboundTask = string.Empty;
                try
                {
                    if (SystemValue.isAllowRuning_DataProcess_BZ39 && SystemValue.isStartedModel)
                {//SystemValue.isAllowRuning_DataProcess_RobotBuffer_IssueOutboundTask &&
                    if (SystemValue.isStartedModel)
                    {
                        var rgvLocation = StationLocationEnum.BZ39.ToString();
                        /*
                         * 1、从数据库中判断是否齐套
                         * 2、根据齐套结果,推送给PLC
                         */
                        var obj = FormCC.stationView.R_StationForReadCommList.Where(x => x.StationCode == rgvLocation.ToString()).FirstOrDefault();
                        if (obj.R_ReqParseData)
                        using (WmsDBModel wmsDB = new WmsDBModel())
                        {
                            var result = obj.R_ReadCodeResult;
                            if (string.IsNullOrEmpty(result))
                            {
                                SystemWarningMsg._lbl_alert_DataProcess_BZ39 = $"{rgvLocation.ToString()}-请求了解码,但是结果是空的,此次循环结束";
                                continue;
                            }
                            var upiCode = result;
                            var qitaoReault = false;
                            using (WmsDBModel wmsDB = new WmsDBModel())
                            {
                                //要是有已下发的任务,就不要执行
                                var isValidate = TaskHandler.ValidateIssueTaskForRobotBuffer(wmsDB);
                                if (!isValidate)
                                {
                                    SystemWarningMsg._lbl_alert_DataProcess_BZ39 = $"{rgvLocation.ToString()}-有‘已下发’的任务,不能处理,此次循环结束";
                                    continue;
                                }
                                var taskList = wmsDB.wms_rbline_task.Where(x => x.PlaceCode == obj.R_OutboundNumber
                              && x.RbTaskType == (int)RbTaskTypeEnum.出库任务
                              && x.TaskStatus == (int)TaskStatusEnum.新建).OrderBy(x => x.PackageCode).ToList();
                                if (taskList == null)
                                {
                                    SystemWarningMsg._lbl_Alert_DataProcess_RobotBuffer_FinishTaskForOutbound = $"{rgvLocation.ToString()},根据 R_OutboundNumber:{obj.R_OutboundNumber}没有找到已下发的任务 ";
                                    continue;
                                }
                                var doTask = taskList.FirstOrDefault();
                                var upiObj = wmsDB.mes_batchOrderUPI_new.Where(x => x.UPI == upiCode).FirstOrDefault();
                                if (upiObj == null)
                                {
                                    SystemWarningMsg._lbl_alert_DataProcess_BZ39 = $"{rgvLocation.ToString()},根据UPI:{upiCode}没有找到对象 ";
                                    continue;
                                }
                                //这里改为 ä¸‹å‘ å‡ºåº“任务
                            var noFinishOutTask = TaskHandler.ValidateIssueTaskForNoFinishTaskByOutStore(wmsDB);
                            if (noFinishOutTask == true)
                            {//表示没有未结束的出库任务,那么就 åˆå§‹åŒ–请求出库指令
                                using (StationServiceClient client = new StationServiceClient())
                                {
                                    //首先要清理
                                    var res = await client.OutboundFinishConfirmAsync((int)EDevice.Station, true, rgvLocation);
                                    var res = await client.WriteOutStoreTaskInfoAsync(true, (int)EDevice.Station, rgvLocation, "", 0);
                                    if (!res.result)
                                    {
                                        SystemWarningMsg._lbl_Alert_DataProcess_RobotBuffer_FinishTaskForOutbound = $"{rgvLocation.ToString()}-下发出库任务 å¤±è´¥,OutboundFinishConfirmAsync è¿”回:{res.resMsg}";
                                        SystemWarningMsg._lbl_Alert_DataProcess_RobotBuffer_IssueOutboundTask = $"{rgvLocation.ToString()}-初始化请求出库指令 å¤±è´¥,WriteOutStoreTaskInfoAsync è¿”回:{res.resMsg}";
                                        continue;
                                    }
                                    else
                                    {
                                        Log4NetHelper.WriteInfoLog(logType, $"{rgvLocation.ToString()}-下发出库任务 æˆåŠŸ,OutboundFinishConfirmAsync,参数: ç«™ç‚¹{rgvLocation}");
                                    }
                                    res = await client.WriteOutStoreTaskInfoAsync((int)EDevice.Station, rgvLocation, doTask.PlaceCode, Convert.ToInt32(doTask.TaskNo));
                                    if (!res.result)
                                    {
                                        SystemWarningMsg._lbl_alert_DataProcess_BZ39 = $"{rgvLocation.ToString()}-下发出库任务 å¤±è´¥,WriteOutStoreTaskInfoAsync è¿”回:{res.resMsg}";
                                        continue;
                                    }
                                    else
                                    {
                                        Log4NetHelper.WriteInfoLog(logType, $"{rgvLocation.ToString()}-下发出库任务 æˆåŠŸ,WriteOutStoreTaskInfoAsync,参数: ç«™ç‚¹{rgvLocation}");
                                        //Log4NetHelper.WriteInfoLog(logType, $"{rgvLocation.ToString()}-初始化请求出库指令 æˆåŠŸ,WriteOutStoreTaskInfoAsync,参数: ç«™ç‚¹{rgvLocation}");
                                    }
                                }
                                wmsDB.SaveChanges();
                            }
                            //要是有已下发的任务,就不要执行
                            var isValidate = TaskHandler.ValidateIssueOutTaskForRobotBuffer(wmsDB);
                            if (!isValidate)
                            {
                                SystemWarningMsg._lbl_Alert_DataProcess_RobotBuffer_IssueOutboundTask = $"{rgvLocation.ToString()}-有‘已下发’的任务,不能处理,此次循环结束";
                                continue;
                            }
                            //取创建时间最早的那个要出库的包
                            if (string.IsNullOrEmpty(FormCC.currHandlerPackageCodeForIssueOutboundTask))
                            {
                                var firstTask = wmsDB.wms_rbline_task.Where(x =>
                           (x.RbTaskType == (int)RbTaskTypeEnum.齐包出库任务 || x.RbTaskType == (int)RbTaskTypeEnum.强制出库任务)
                          && x.TaskStatus == (int)TaskStatusEnum.新建).OrderBy(x => x.CreateTime).FirstOrDefault();
                                if (firstTask == null)
                                {
                                    SystemWarningMsg._lbl_Alert_DataProcess_RobotBuffer_IssueOutboundTask = $"{rgvLocation.ToString()},根据 æ²¡æœ‰æ‰¾åˆ° æ–°å»º çš„任务 ";
                                    FormCC.currHandlerPackageCodeForIssueOutboundTask = "";
                                    continue;
                                }
                                FormCC.currHandlerPackageCodeForIssueOutboundTask = firstTask.PackageCode;
                            }
                            else
                            {
                                var firstTask = wmsDB.wms_rbline_task.Where(x =>
                           (x.RbTaskType == (int)RbTaskTypeEnum.齐包出库任务 || x.RbTaskType == (int)RbTaskTypeEnum.强制出库任务)
                          && x.TaskStatus == (int)TaskStatusEnum.新建
                          && x.PackageCode == FormCC.currHandlerPackageCodeForIssueOutboundTask
                          ).OrderBy(x => x.CreateTime).FirstOrDefault();
                                if (firstTask == null)
                                {
                                    SystemWarningMsg._lbl_Alert_DataProcess_RobotBuffer_IssueOutboundTask = $"{rgvLocation.ToString()},根据 æ²¡æœ‰æ‰¾åˆ° æ–°å»º çš„任务 ";
                                    FormCC.currHandlerPackageCodeForIssueOutboundTask = "";
                                    continue;
                                }
                            }
                            var taskList = wmsDB.wms_rbline_task.Where(x =>
                          (x.RbTaskType == (int)RbTaskTypeEnum.齐包出库任务 || x.RbTaskType == (int)RbTaskTypeEnum.强制出库任务)
                          && x.TaskStatus == (int)TaskStatusEnum.新建).Where(x => x.PackageCode == FormCC.currHandlerPackageCodeForIssueOutboundTask).OrderBy(x => x.CreateTime).ToList();
                            if (taskList == null)
                            {
                                SystemWarningMsg._lbl_Alert_DataProcess_RobotBuffer_IssueOutboundTask = $"{rgvLocation.ToString()},根据 æ²¡æœ‰æ‰¾åˆ° æ–°å»º çš„任务 ";
                                continue;
                            }
                            var doTask = taskList.FirstOrDefault();
                            //这里下发 å‡ºåº“任务
                            using (StationServiceClient client = new StationServiceClient())
                            {
                                //首先要清理
                                //var res = await client.OutboundFinishConfirmAsync((int)EDevice.Station, false, rgvLocation);
                                //if (!res.result)
                                //{
                                //    SystemWarningMsg._lbl_Alert_DataProcess_RobotBuffer_FinishTaskForOutbound = $"{rgvLocation.ToString()}-下发出库任务 å¤±è´¥,OutboundFinishConfirmAsync è¿”回:{res.resMsg}";
                                //    continue;
                                //}
                                //else
                                //{
                                //    Log4NetHelper.WriteInfoLog(logType, $"{rgvLocation.ToString()}-下发出库任务 æˆåŠŸ,OutboundFinishConfirmAsync,参数: ç«™ç‚¹{rgvLocation}");
                                //}
                                var res = await client.WriteOutStoreTaskInfoAsync(false, (int)EDevice.Station, rgvLocation, doTask.PlaceCode, Convert.ToInt32(doTask.TaskNo));
                                if (!res.result)
                                {
                                    SystemWarningMsg._lbl_Alert_DataProcess_RobotBuffer_IssueOutboundTask = $"{rgvLocation.ToString()}-下发出库任务 å¤±è´¥,WriteOutStoreTaskInfoAsync è¿”回:{res.resMsg}";
                                    continue;
                                }
                                else
                                {
                                    Log4NetHelper.WriteInfoLog(logType, $"{rgvLocation.ToString()}-下发出库任务 æˆåŠŸ,WriteOutStoreTaskInfoAsync,参数: ç«™ç‚¹{rgvLocation}");
                                }
                            }
                            doTask.TaskStatus = (int)TaskStatusEnum.已下发;
                            doTask.TaskStatusName = TaskStatusEnum.已下发.ToString();
                            wmsDB.SaveChanges();
                        }
                    }
                }
                catch (Exception ex)
                {
                    SystemWarningMsg._lbl_alert_DataProcess_BZ39 += " å‡ºçް异叏:" + ex.Message + SysGloble.SPLIT_STR;
                    SystemWarningMsg._lbl_Alert_DataProcess_RobotBuffer_IssueOutboundTask += " å‡ºçް异叏:" + ex.Message + SysGloble.SPLIT_STR;
                    Log4NetHelper.WriteErrorLog(logType, " å‡ºçŽ°å¼‚å¸¸ï¼š" + ex.Message, ex);
                }
            }