using HslCommunication.WebSocket; using SqlSugar; using WIDESEAWCS_Core.Helper; using WIDESEAWCS_Core.Seed; using WIDESEAWCS_DTO.BasicInfo; using WIDESEAWCS_Model.Models; using ICacheService = WIDESEAWCS_Core.Caches.ICacheService; namespace WIDESEAWCS_Server.HostedService { public class WarehouseHostedService : IHostedService { private readonly ICacheService _cacheService; private readonly DBContext _dbContext; //private readonly WebSocketServer _webSocketServer; public WarehouseHostedService(ICacheService cacheService, DBContext dbContext/*, WebSocketServer webSocketServer*/) { _cacheService = cacheService; _dbContext = dbContext; //_webSocketServer = webSocketServer; } public Task StartAsync(CancellationToken cancellationToken) { try { string connStr = AppSettings.GetValue("WMSConnectionStrings"); if (string.IsNullOrEmpty(connStr)) { throw new Exception("WMS连接字符串错误"); } SqlSugarClient sugarClient = new(new ConnectionConfig() { ConnectionString = connStr, IsAutoCloseConnection = true, DbType = DbType.SqlServer }); List warehouses = sugarClient.Queryable().ToList(); _dbContext.Db.Deleteable().ExecuteCommand(); _dbContext.Db.Insertable(warehouses).ExecuteCommand(); List apiInfos = _dbContext.Db.Queryable().ToList(); List warehouseDevices = _dbContext.Db.Queryable().ToList(); _cacheService.AddOrUpdate(nameof(Dt_WarehouseDevice), warehouseDevices); _cacheService.AddOrUpdate(nameof(apiInfos), apiInfos); } catch (Exception ex) { Console.WriteLine(ex.ToString()); } return Task.CompletedTask; } public Task StopAsync(CancellationToken cancellationToken) { return Task.CompletedTask; } } }