wangxinhui
2025-01-04 ca08d1f380987254814242e2fe3ea9de28228132
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
 
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;
 
        public WarehouseHostedService(ICacheService cacheService, DBContext dbContext)
        {
            _cacheService = cacheService;
            _dbContext = dbContext;
        }
 
        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<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);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
            return Task.CompletedTask;
        }
 
        public Task StopAsync(CancellationToken cancellationToken)
        {
            return Task.CompletedTask;
        }
    }
}