wxw
2025-05-12 c7c2f7aa20427204944ba80a2704232b2f281582
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using SqlSugar;
using WMS.Entity;
using WMS.Entity.Context;
using WMS.IDAL;
 
namespace WMS.DAL
{
    public class BaseRepository<T> : IBaseRepository<T> where T : BaseEntity, new()
    {
        private readonly SqlSugarScope _db;
 
        public BaseRepository(SqlSugarScope db)
        {
            _db = db;
        }
        public int Add(T t)
        {
            return _db.Insertable(t).ExecuteCommand();
        }
        public async Task<int> AddAsync(T t)
        {
            return await _db.Insertable(t).ExecuteCommandAsync();
        }
 
        public int Edit(T t)
        {
            return _db.Updateable(t).IgnoreColumns(m => new { m.CreateTime,m.CreateUser}).ExecuteCommand();
        }
        public async Task<int> EditAsync(T t)
        {
            return await _db.Updateable(t).IgnoreColumns(m => new { m.CreateTime, m.CreateUser }).ExecuteCommandAsync();
        }
 
        public int Remove(int id,int userId = 0)
        {
            var data = GetOneById(id);
            data.IsDel = "1";
            data.UpdateTime = DateTime.Now;
            data.UpdateUser = userId;
            return _db.Updateable(data).UpdateColumns(m => new { m.IsDel }).ExecuteCommand();
        }
        public async Task<int> RemoveAsync(int id,int userId = 0)
        {
            var data = await GetOneByIdAsync(id);
            data.IsDel = "1";
            data.UpdateTime = DateTime.Now;
            data.UpdateUser = userId;
            return await _db.Updateable(data).UpdateColumns(m => new { m.IsDel }).ExecuteCommandAsync();
        }
 
        public int Remove(T t, int userId = 0)
        {
            t.IsDel = "1";
            t.UpdateTime = DateTime.Now;
            t.UpdateUser = userId;
            return  _db.Updateable(t).UpdateColumns(m => new { m.IsDel }).ExecuteCommand();
        }
        public async Task<int> RemoveAsync(T t,int userId = 0)
        {
            t.IsDel = "1";
            t.UpdateTime = DateTime.Now;
            t.UpdateUser = userId;
            return await _db.Updateable(t).UpdateColumns(m => new { m.IsDel }).ExecuteCommandAsync();
        }
        public int RemoveAll(List<T> t,int userId = 0)
        {
            foreach (var item in t)
            {
                item.IsDel = "1";
                item.UpdateTime = DateTime.Now;
                item.UpdateUser = userId;
            }
            
            return _db.Updateable(t).UpdateColumns(m => new { m.IsDel }).ExecuteCommand();
        }
        public async Task<int> RemoveAllAsync(List<T> t,int userId = 0)
        {
            foreach (var item in t)
            {
                item.IsDel = "1";
                item.UpdateTime = DateTime.Now;
                item.UpdateUser = userId;
            }
 
            return await _db.Updateable(t).UpdateColumns(m => new { m.IsDel }).ExecuteCommandAsync();
        }
        public void Save()
        {
            _db.SaveQueues();
        }
        public async Task SaveAsync()
        {
            await _db.SaveQueuesAsync();
        }
 
        public T GetOneById(int id)
        {
            var data = _db.Queryable<T>().First(m => m.Id == id && m.IsDel == "0");
            return data;
        }
        public async Task<T> GetOneByIdAsync(int id)
        {
            var data = await _db.Queryable<T>().FirstAsync(m => m.Id == id && m.IsDel == "0");
            return data;
        }
 
        public List<T> GetModels(string sqlString) 
        {
            var models = _db.Ado.SqlQuery<T>(sqlString);
            return models;
        }
 
        public T GetModel(string sqlString)
        {
            var model = _db.Ado.SqlQuery<T>(sqlString);
            return model.FirstOrDefault();
        }
        
        public SqlSugar.ISugarQueryable<T> GetAllAsync()
        {
            var data =_db.Queryable<T>().Where(m=>m.IsDel =="0");
            return data;
        }
        public SqlSugar.ISugarQueryable<T> GetAllWhereAsync(System.Linq.Expressions.Expression<Func<T, bool>> predicate)
        {
            var data = GetAllAsync().Where(predicate);
            return data;
        }
        public SqlSugar.ISugarQueryable<T> GetAllByOrderAsync(System.Linq.Expressions.Expression<Func<T, bool>> predicate, bool asc = true)
        {
            var type = OrderByType.Asc;
            if (!asc)
            {
                type = OrderByType.Desc;
            }
            var data = GetAllWhereAsync(predicate).OrderBy(m => m.CreateTime, type);
            return data;
        }
        public SqlSugar.ISugarQueryable<T> GeTAllByPageAsync(System.Linq.Expressions.Expression<Func<T, bool>> predicate, int pageSize, int pageIndex,out int count )
        {
            var list = GetAllWhereAsync(predicate);
            count = list.Count();
            var data = list.Skip(pageSize * (pageIndex - 1)).Take(pageSize);
            return data;
        }
 
        public SqlSugar.ISugarQueryable<T> GetAllByOrderPageAsync(System.Linq.Expressions.Expression<Func<T, bool>> predicate, int pageSize, int pageIndex, out int count , bool asc = true)
        {
            var list = GetAllByOrderAsync(predicate, asc);
            count = list.Count();
            var data = list.Skip(pageSize * (pageIndex-1)).Take(pageSize);
 
            return data;
        }
        
    }
}