private async Task <StepResultOutput> createJob_NC(SapMOrderProcessDispatchPrepare prepareInfo, DmesDispatchedIdOutput idGet, IList <DmesWorkcenterMapOutput> mappers) { StepResultOutput result = StepResultOutput.Failure; var machine = mappers.SingleOrDefault(m => m.WORKID == idGet.ActualWorkID); DateTime jobCreate = DateTime.Now; result = await tryDispatchPrepareStep(async() => { WinToolResult nJob = await _winToolRepository.WintoolCreateNCJob(new Domain.External.Entities.Winchill.CreateJobInput() { JobType = "NC", PrepareInfoId = prepareInfo.Id, ItemNumber = idGet.MaterialNumber, MachineType = (machine != null && machine.ID > 0) ? machine.WinTool : "" }); return(await Task.FromResult(new StepResultOutput(nJob))); }, SapMOrderProcessDispatchPrepareStepTransTypes.NC, SapMOrderProcessDispatchPrepareStepTransTypes.NC_Start, prepareInfo); prepareInfo.NC_IsPreparedFinished = (short)SapMOrderProcessDispatchPrepareStepStatus.未准备; if (result.IsSuccess) { prepareInfo.NC_IsPreparedFinished = (short)SapMOrderProcessDispatchPrepareStepStatus.准备中; prepareInfo.NC_RequiredDate = jobCreate.AddDays(1); } await _sapMOrderProcessDispatchPrepareRepository.UpdateAsync(prepareInfo); return(result); }
//执行一个过程方法,(无论成功与否)记录一条日志 private async Task <StepResultOutput> tryDispatchPrepareStep(Func <Task <StepResultOutput> > tryFunc, string stepTransactionType, string stepName, SapMOrderProcessDispatchPrepare prepareInfo) { //执行该步骤 StepResultOutput result = StepResultOutput.Failure; try { result = await tryFunc(); return(result); } catch (Exception ex) { result = new StepResultOutput(false, ex.Message); return(result); } finally { var step = await _sapMOrderProcessDispatchPrepareStepRepository.FirstOrDefaultAsync(s => s.SapMOrderProcessDispatchPrepareId == prepareInfo.Id && s.StepTransactionType == stepTransactionType && s.StepName == stepName); if (step == null || step.Id <= 0) { step = new SapMOrderProcessDispatchPrepareStep() { } } ; step.IsStepSuccess = result.IsSuccess; step.SapMOrderProcessDispatchPrepareId = prepareInfo.Id; step.StepTransactionType = stepTransactionType; step.StepName = stepName; step.StepResultMessage = result.ResultMessage; //StepStatus = (short)SapMOrderProcessDispatchPrepareStepStatus.准备中, //步骤记录 await _sapMOrderProcessDispatchPrepareStepRepository.InsertOrUpdateAsync(step); } }
public async Task <DispatchedWorkerOutput> DoWorkForDispatched(DispatchedWorkerInput input) { //1、获取DMESDispatchedWorker最新数据 //2、根据最新的同步完成时间,去获取(最新的同步完成时间-当前)的所有realeased派工单 //3、根据获取的派工单,获取该派工单下所有与工单对应的唯一编号和工单的物料编码 //3、以唯一编号为主键(派工单和工单1对1关系表的那个主键),进行本地齐备性流程状态创建 //4、如果唯一编号已经有状态信息,不重复创建 var workerlog = new DMESDispatchedWorker() { WorkerType = input.WorkerType, WorkerStartDate = DateTime.Now, WorkerResultMessage = "" }; try { int workId = this._dMESDispatchedWorkerRepository.InsertAndGetId(workerlog); workerlog.Id = workId; var latestWorker = await this._dMESDispatchedWorkerRepository.GetAll() .Where(b => b.IsWorkerSuccess) .OrderByDescending(b => b.CreationTime) .FirstOrDefaultAsync(); List <AsyncResult> totalResults = new List <AsyncResult>(); if (latestWorker != null && latestWorker.Id > 0) { var newIds = await this._dmesWorkTicketRepository.GetDispatchedWorkTicketIDsLatest( new DmesGetDispatchedIdsInput() { lastWorkerDate = latestWorker.WorkerStartDate }); //Task < IList < DmesWorkcenterMapOutput >> var mappers = await _dmesWorkCenterRepository.GetWorkCenterWinToolMaps(); foreach (var idGet in newIds.Distinct()) { AsyncResult asyncResult = new AsyncResult(); asyncResult.DispatchedWorkTicketId = idGet.DispatchWorKTicketID.ToString(); var existId = await this._sapMOrderProcessDispatchPrepareRepository.GetAll() .Where(p => p.DispatchWorKTicketID == idGet.DispatchWorKTicketID) .FirstOrDefaultAsync(); if (existId == null || existId.Id <= 0) { //新建 var newPrepareInfo = new SapMOrderProcessDispatchPrepare() { DispatchWorKTicketID = idGet.DispatchWorKTicketID }; int newid = await this._sapMOrderProcessDispatchPrepareRepository.InsertAndGetIdAsync(newPrepareInfo); existId = newPrepareInfo; } //MES下达新派工单时,发起流程: //1) 刀具配刀流程 //2) NC程序准备流程 StepResultOutput resultTL = StepResultOutput.Failure; if (!existId.Tooling_IsPreparedFinished.HasValue || existId.Tooling_IsPreparedFinished <= (short)SapMOrderProcessDispatchPrepareStepStatus.未准备) { resultTL = await createJob_Tooling(existId, idGet, mappers); asyncResult.Tooling_IsSuccess = resultTL.IsSuccess; asyncResult.Tooling_ResultMessage = resultTL.ResultMessage; } else { asyncResult.Tooling_IsSuccess = true; asyncResult.Tooling_ResultMessage = "已存在"; } StepResultOutput resultNC = StepResultOutput.Failure; if (!existId.NC_IsPreparedFinished.HasValue || existId.NC_IsPreparedFinished <= (short)SapMOrderProcessDispatchPrepareStepStatus.未准备) { resultNC = await createJob_NC(existId, idGet, mappers); asyncResult.NC_IsSuccess = resultNC.IsSuccess; asyncResult.NC_ResultMessage = resultNC.ResultMessage; } else { asyncResult.NC_IsSuccess = true; asyncResult.NC_ResultMessage = "已存在"; } totalResults.Add(asyncResult); } } int failcount = totalResults.Count(t => !(t.NC_IsSuccess && t.Tooling_IsSuccess)); //var totalResultsStr = totalResults.Select(buildAsyncResult); var totalResultsStr = buildAsyncResult(totalResults); workerlog.WorkerFinishDate = DateTime.Now; workerlog.IsWorkerSuccess = failcount > 0 ? false : true; workerlog.WorkerResultMessage = totalResultsStr; await this._dMESDispatchedWorkerRepository.InsertOrUpdateAsync(workerlog); return(await Task.FromResult(workerlog.MapTo <DispatchedWorkerOutput>())); } catch (Exception e) { workerlog.WorkerFinishDate = DateTime.Now; workerlog.IsWorkerSuccess = false; workerlog.WorkerResultMessage = e.Message; await this._dMESDispatchedWorkerRepository.InsertOrUpdateAsync(workerlog); return(await Task.FromResult(workerlog.MapTo <DispatchedWorkerOutput>())); } }