1
wangxinhui
2025-01-16 5ab6b36c0c4bd80a31dcdd9f15cc835b7352c4e7
´úÂë¹ÜÀí/WCS/WIDESEAWCS_Server/WIDESEAWCS_Server/HostedService/WarehouseHostedService.cs
@@ -1,4 +1,5 @@

using HslCommunication.WebSocket;
using SqlSugar;
using WIDESEAWCS_Core.Helper;
using WIDESEAWCS_Core.Seed;
@@ -12,43 +13,55 @@
    {
        private readonly ICacheService _cacheService;
        private readonly DBContext _dbContext;
        //private readonly WebSocketServer _webSocketServer;
        public WarehouseHostedService(ICacheService cacheService, DBContext dbContext)
        public WarehouseHostedService(ICacheService cacheService, DBContext dbContext/*, WebSocketServer webSocketServer*/)
        {
            _cacheService = cacheService;
            _dbContext = dbContext;
            //_webSocketServer = webSocketServer;
        }
        public Task StartAsync(CancellationToken cancellationToken)
        {
            string connStr = AppSettings.GetValue("WMSConnectionStrings");
            if (string.IsNullOrEmpty(connStr))
            try
            {
                throw new Exception("WMS连接字符串错误");
                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<Dt_Warehouse> warehouses = sugarClient.Queryable<Dt_Warehouse>().ToList();
                _dbContext.Db.Deleteable<Dt_Warehouse>().ExecuteCommand();
                _dbContext.Db.Insertable(warehouses).ExecuteCommand();
                List<Dt_ApiInfo> apiInfos = _dbContext.Db.Queryable<Dt_ApiInfo>().ToList();
                List<Dt_WarehouseDevice> warehouseDevices = _dbContext.Db.Queryable<Dt_WarehouseDevice>().ToList();
                _cacheService.AddOrUpdate(nameof(Dt_WarehouseDevice), warehouseDevices);
                _cacheService.AddOrUpdate(nameof(apiInfos), apiInfos);
            }
            SqlSugarClient sugarClient = new(new ConnectionConfig()
            catch (Exception ex)
            {
                ConnectionString = connStr,
                IsAutoCloseConnection = true,
                DbType = DbType.SqlServer
            });
            List<Dt_Warehouse> warehouses = sugarClient.Queryable<Dt_Warehouse>().ToList();
            _dbContext.Db.Deleteable<Dt_Warehouse>().ExecuteCommand();
            _dbContext.Db.Insertable(warehouses).ExecuteCommand();
            List<Dt_ApiInfo> apiInfos = _dbContext.Db.Queryable<Dt_ApiInfo>().ToList();
            _cacheService.AddOrUpdate(nameof(apiInfos), apiInfos);
                Console.WriteLine(ex.ToString());
            }
            return Task.CompletedTask;
        }
        public Task StopAsync(CancellationToken cancellationToken)
        {
            throw new NotImplementedException();
            return Task.CompletedTask;
        }
    }
}