wanshenmean
8 天以前 eb399b544b4055c1b58a1746f8c453ba41e4010b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using System;
using System.Threading;
using System.Threading.Tasks;
using WIDESEA_Core.Core;
using WIDESEA_ITaskInfoService;
 
namespace WIDESEA_WMSServer.BackgroundServices
{
    /// <summary>
    /// 自动出库任务后台服务
    /// 定期检查到期库存并创建出库任务
    /// </summary>
    public class AutoOutboundTaskBackgroundService : BackgroundService
    {
        private readonly ILogger<AutoOutboundTaskBackgroundService> _logger;
        private readonly ITaskService _taskService;
        private readonly AutoOutboundTaskOptions _options;
 
        public AutoOutboundTaskBackgroundService(
            ILogger<AutoOutboundTaskBackgroundService> logger,
            ITaskService taskService,
            IOptions<AutoOutboundTaskOptions> options)
        {
            _logger = logger;
            _taskService = taskService;
            _options = options.Value;
        }
 
        protected override async Task ExecuteAsync(CancellationToken stoppingToken)
        {
            _logger.LogInformation("自动出库任务后台服务已启动");
 
            if (!_options.Enable)
            {
                _logger.LogInformation("自动出库任务功能已禁用,服务退出");
                return;
            }
 
            _logger.LogInformation("自动出库任务检查间隔: {Seconds} 秒", _options.CheckIntervalSeconds);
 
            int index = 0;
 
            while (!stoppingToken.IsCancellationRequested)
            {
                try
                {
                    _logger.LogInformation("开始检查到期库存...");
                    if (index == 0)
                    {
                        await Task.Delay(5000, stoppingToken);
                        index++;
                    }
                    var result = await _taskService.CreateAutoOutboundTasksAsync();
 
                    if (result.Status)
                    {
                        _logger.LogInformation("到期库存检查完成: {Message}", result.Message);
                    }
                    else
                    {
                        _logger.LogWarning("到期库存检查失败: {Message}", result.Message);
                    }
                }
                catch (Exception ex)
                {
                    _logger.LogError(ex, "自动出库任务创建过程中发生异常");
                }
 
                var delay = TimeSpan.FromSeconds(_options.CheckIntervalSeconds);
                _logger.LogDebug("等待 {Seconds} 秒后进行下次检查", delay.TotalSeconds);
                await Task.Delay(delay, stoppingToken);
            }
 
            _logger.LogInformation("自动出库任务后台服务已停止");
        }
 
        public override async Task StopAsync(CancellationToken cancellationToken)
        {
            _logger.LogInformation("正在停止自动出库任务后台服务...");
            await base.StopAsync(cancellationToken);
        }
    }
}