z8018
2026-02-11 b8fb68b44c29e4667f6ea5746119413809a60a9e
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
using SqlSugar;
using Microsoft.Extensions.Logging;
using KH.WMS.Core.DependencyInjection.ServiceLifetimes;
 
namespace KH.WMS.Core.Database.Repositories;
 
/// <summary>
/// 仓储基类实现
/// </summary>
/// <typeparam name="T">实体类型</typeparam>
public class RepositoryBase<T, TKey> : IRepository<T, TKey>
    where T : class, new()
    where TKey : struct
{
    protected readonly ISqlSugarClient _db;
    protected readonly ILogger? _logger;
 
    public RepositoryBase(ISqlSugarClient db, ILogger? logger = null)
    {
        _db = db;
        _logger = logger;
    }
 
    public async Task<T?> GetByIdAsync(TKey id)
    {
        return await _db.Queryable<T>().In(id).FirstAsync();
    }
 
    public async Task<List<T>> GetAllAsync()
    {
        return await _db.Queryable<T>().ToListAsync();
    }
 
    public async Task<List<T>> GetListAsync(System.Linq.Expressions.Expression<Func<T, bool>> expression)
    {
        return await _db.Queryable<T>().Where(expression).ToListAsync();
    }
 
    public async Task<T?> GetFirstOrDefaultAsync(System.Linq.Expressions.Expression<Func<T, bool>> expression)
    {
        return await _db.Queryable<T>().FirstAsync(expression);
    }
 
    public async Task<(List<T> Items, int Total)> GetPagedListAsync(int pageIndex, int pageSize, System.Linq.Expressions.Expression<Func<T, bool>>? expression = null)
    {
        var query = _db.Queryable<T>();
 
        if (expression != null)
        {
            query = query.Where(expression);
        }
 
        var total = await query.CountAsync();
        var items = await query.ToPageListAsync(pageIndex, pageSize);
 
        return (items, total);
    }
 
    public async Task<TKey> InsertAsync(T entity)
    {
        return (await _db.Insertable(entity).ExecuteReturnPkListAsync<TKey>()).First();
    }
 
    public async Task<List<TKey>> InsertAsync(List<T> entities)
    {
        return await _db.Insertable(entities).ExecuteReturnPkListAsync<TKey>();
    }
 
    public async Task<T> InsertReturnEntityAsync(T entity)
    {
        return await _db.Insertable(entity).ExecuteReturnEntityAsync();
    }
 
    public async Task<List<T>> InsertReturnEntityAsync(List<T> entities)
    {
        List<TKey> keys = await _db.Insertable(entities).ExecuteReturnPkListAsync<TKey>();
        return await _db.Queryable<T>().In(keys).ToListAsync();
    }
 
    public async Task<bool> UpdateAsync(T entity)
    {
        return await _db.Updateable(entity).ExecuteCommandAsync() > 0;
    }
 
    public async Task<bool> UpdateAsync(List<T> entities)
    {
        return await _db.Updateable(entities).ExecuteCommandAsync() > 0;
    }
 
    public async Task<bool> DeleteAsync(TKey id)
    {
        return await _db.Deleteable<T>().In(id).ExecuteCommandAsync() > 0;
    }
 
    public async Task<bool> DeleteAsync(List<TKey> ids)
    {
        return await _db.Deleteable<T>().In(ids).ExecuteCommandAsync() > 0;
    }
 
    public async Task<bool> DeleteAsync(System.Linq.Expressions.Expression<Func<T, bool>> expression)
    {
        return await _db.Deleteable<T>().Where(expression).ExecuteCommandAsync() > 0;
    }
 
    public async Task<bool> ExistsAsync(System.Linq.Expressions.Expression<Func<T, bool>> expression)
    {
        return await _db.Queryable<T>().Where(expression).AnyAsync();
    }
 
    public async Task<int> CountAsync(System.Linq.Expressions.Expression<Func<T, bool>>? expression = null)
    {
        var query = _db.Queryable<T>();
 
        if (expression != null)
        {
            query = query.Where(expression);
        }
 
        return await query.CountAsync();
    }
}