| using Microsoft.Extensions.Hosting; | 
| using Microsoft.Extensions.Options; | 
| using System; | 
| using System.Collections.Generic; | 
| using System.Linq; | 
| using System.Threading; | 
| using System.Threading.Tasks; | 
| using Utility.Tools; | 
| using Wms.Tools; | 
| using WMS.BLL.BllSoServer; | 
| using WMS.BLL.DataServer; | 
| using WMS.IBLL.IDataServer; | 
|   | 
| namespace Wms | 
| { | 
|     public class DailyTaskService : IHostedService, IDisposable | 
|     { | 
|         private Timer _timer; | 
|         private Timer _timer2; | 
|         private Timer _timer3; | 
|         private string _wcsUrl; | 
|         private string _agvUrl; | 
|         private Timer _periodicReport; | 
|         public DailyTaskService(string wcsUrl,string agvUrl)  | 
|         { | 
|             _wcsUrl = wcsUrl; | 
|             _agvUrl = agvUrl; | 
|         } | 
|   | 
|         //创建定时任务 | 
|         public Task StartAsync(CancellationToken cancellationToken) | 
|         { | 
|             #region 每天0点执行 | 
|             //计算距离下一个 0 点的时间间隔 | 
|             DateTime now = DateTime.Now; | 
|             DateTime nextZeroHour = now.AddDays(1).Date; | 
|             TimeSpan delay = nextZeroHour - now; | 
|   | 
|             //创建定时器,并设置回调函数 | 
|             _timer = new Timer(ExecuteDailyTask, null, delay, TimeSpan.FromDays(1)); | 
|             _periodicReport = new Timer(PeriodicReport, null, delay, TimeSpan.FromDays(1)); | 
|             //TimeSpan delay2 = TimeSpan.FromMinutes(2); | 
|   | 
|             // 创建定时器,并设置回调函数 | 
|             // _timer2 = new Timer(ExecuteDailyTask2, null, delay2, TimeSpan.FromMinutes(3)); | 
|   | 
|             //TimeSpan delay3 = TimeSpan.FromMinutes(2); | 
|   | 
|             // 创建定时器,并设置回调函数 | 
|             //_timer3 = new Timer(ExecuteDailyTask3, null, delay3, TimeSpan.FromMinutes(3)); | 
|   | 
|             return Task.CompletedTask; | 
|             #endregion | 
|   | 
|         } | 
|   | 
|         /// <summary> | 
|         /// 在应用程序关闭时被调用,用于执行清理操作和释放资源。在 ASP.NET Core 应用程序中, | 
|         /// 当主机关闭时,StopAsync 方法会被调用来停止后台服务,以确保服务能够正确地停止并释放它所占用的资源,比如关闭定时器、释放数据库连接等。 | 
|         /// </summary> | 
|         /// <param name="cancellationToken"></param> | 
|         /// <returns></returns> | 
|         public Task StopAsync(CancellationToken cancellationToken) | 
|         { | 
|             // 停止定时器 | 
|             _timer?.Change(Timeout.Infinite, 0); | 
|             _timer2?.Change(Timeout.Infinite, 0); | 
|             _timer3?.Change(Timeout.Infinite, 0); | 
|             _periodicReport?.Change(Timeout.Infinite, 0); | 
|   | 
|             return Task.CompletedTask; | 
|         } | 
|         //执行任务逻辑 | 
|         private void ExecuteDailyTask(object state) | 
|         { | 
|             try | 
|             { | 
|                 StockDetailServer _stockDetail = new StockDetailServer(); | 
|                 //任务逻辑 | 
|                 _stockDetail.StockCheck(); | 
|             } | 
|             catch (Exception ex) | 
|             { | 
|                 var logStr = $@".\log\WMS\WMS定时任务" + DateTime.Now.ToString("yyyyMMdd") + ".txt"; | 
|                 LogFile.SaveLogToFile($"WMS定时任务异常:( {ex.Message} ),", logStr); | 
|             } | 
|         } | 
|   | 
|         //执行任务逻辑 | 
|         private void ExecuteDailyTask2(object state) | 
|         { | 
|             try | 
|             { | 
|                 ExportNoticeServer _exNotice = new ExportNoticeServer(); | 
|                 //任务逻辑 | 
|                 var list = _exNotice.BeiLiaoIssueOutHouse(_wcsUrl); | 
|             } | 
|             catch (Exception ex) | 
|             { | 
|                 var logStr = $@".\log\WMS\WMS定时备料任务" + DateTime.Now.ToString("yyyyMMdd") + ".txt"; | 
|                 LogFile.SaveLogToFile($"WMS定时备料任务异常:( {ex.Message} ),", logStr); | 
|             } | 
|         } | 
|   | 
|   | 
|         //执行任务逻辑 | 
|         private void ExecuteDailyTask3(object state) | 
|         { | 
|             try | 
|             { | 
|                 ExportNoticeServer _exNotice = new ExportNoticeServer(); | 
|                 //任务逻辑 | 
|                 _exNotice.ProCallIssueOutHouse(_agvUrl); | 
|             } | 
|             catch (Exception ex) | 
|             { | 
|                 var logStr = $@".\log\WMS\WMS定时备料任务" + DateTime.Now.ToString("yyyyMMdd") + ".txt"; | 
|                 LogFile.SaveLogToFile($"WMS定时备料任务异常:( {ex.Message} ),", logStr); | 
|             } | 
|         } | 
|   | 
|         //定时执行周期报表 | 
|         private void PeriodicReport(object state) | 
|         { | 
|             try | 
|             { | 
|                 StockInfoServer _stock = new StockInfoServer(); | 
|                 var arr = _stock.ByDayInsertStock(); | 
|   | 
|                 var logStr = $@".\log\WMS\WMS定时任务" + DateTime.Now.ToString("yyyyMMdd") + ".txt"; | 
|                 LogFile.SaveLogToFile($"WMS定时任务执行异常:{arr},", logStr); | 
|                 //释放资源 | 
|                 _periodicReport?.Dispose(); | 
|             } | 
|             catch (Exception ex) | 
|             { | 
|                 var logStr = $@".\log\WMS\WMS定时任务" + DateTime.Now.ToString("yyyyMMdd") + ".txt"; | 
|                 LogFile.SaveLogToFile($"WMS定时任务执行异常:{DateTime.Now}:( {ex.Message} ),", logStr); | 
|             } | 
|         } | 
|   | 
|         /// <summary> | 
|         /// 在服务结束运行时,定时器得到正确地清理和释放 | 
|         /// </summary> | 
|         public void Dispose() | 
|         { | 
|             //释放资源 | 
|             _timer?.Dispose(); | 
|             _timer2?.Dispose(); | 
|             _timer3?.Dispose(); | 
|             _periodicReport?.Dispose(); | 
|         } | 
|     } | 
| } |