bklLiudl
2024-05-25 484e5129e4c9a671c5660a556a24bd306f1fdd9b
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
162
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SqlSugar;
using WMS.Entity;
using WMS.IDAL;
 
namespace WMS.DAL
{
    public class DbHelper<T> where T:BaseEntity,new()
    {
        private readonly SqlSugarScope _db;
 
        public DbHelper(SqlSugarScope db)
        {
            _db = db;
        }
 
        #region sql
 
        public int Cud(string sql, params SugarParameter[] parameters)
        {
            var i = _db.Ado.ExecuteCommand(sql,parameters);
            return i;
        }
        public async Task<int> CudAsync(string sql, params SugarParameter[] parameters)
        {
            var i = await _db.Ado.ExecuteCommandAsync(sql, parameters);
            return i;
        }
        public object GetList(string sql)
        {
            var i = _db.Ado.SqlQuery<object>(sql);
            return i;
        }
        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();
        }
        #endregion
 
        #region sugar 新增
 
        public int Add(T t)
        {
            return _db.Insertable(t).ExecuteCommand();
        }
        public async Task<int> AddAsync(T t)
        {
            return await _db.Insertable(t).ExecuteCommandAsync();
        }
 
        #endregion
 
        #region sugar 编辑
 
        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();
        }
 
        #endregion
 
        #region sugar 删除
 
        public int Remove(int id, int userId)
        {
            var data = _db.Queryable<T>().First(m => m.Id == id && m.IsDel == "0"); ;
            data.IsDel = "1";
            data.UpdateTime = DateTime.Now;
            data.UpdateUser = userId;
            return _db.Updateable(data).UpdateColumns(m => new { m.IsDel, m.UpdateUser, m.UpdateTime }).ExecuteCommand();
        }
        public async Task<int> RemoveAsync(int id, int userId = 0)
        {
            var data = await _db.Queryable<T>().FirstAsync(m => m.Id == id && m.IsDel == "0");
            data.IsDel = "1";
            data.UpdateTime = DateTime.Now;
            data.UpdateUser = userId;
            return await _db.Updateable(data).UpdateColumns(m => new { m.IsDel, m.UpdateUser, m.UpdateTime }).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, m.UpdateUser, m.UpdateTime }).ExecuteCommand();
        }
        public async Task<int> RemoveAsync(T t, int userId)
        {
            t.IsDel = "1";
            t.UpdateTime = DateTime.Now;
            t.UpdateUser = userId;
            return await _db.Updateable(t).UpdateColumns(m => new { m.IsDel, m.UpdateUser, m.UpdateTime }).ExecuteCommandAsync();
        }
        public int RemoveAll(List<T> t, int userId)
        {
            foreach (var item in t)
            {
                item.IsDel = "1";
                item.UpdateTime = DateTime.Now;
                item.UpdateUser = userId;
            }
 
            return _db.Updateable(t).UpdateColumns(m => new { m.IsDel,m.UpdateUser,m.UpdateTime }).ExecuteCommand();
        }
        public async Task<int> RemoveAllAsync(List<T> t, int userId)
        {
            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, m.UpdateUser, m.UpdateTime }).ExecuteCommandAsync();
        }
 
        #endregion
 
        #region sugar 查询
 
        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 ISugarQueryable<T> GetAllAsync()
        {
            var data = _db.Queryable<T>().Where(m => m.IsDel == "0");
            return data;
        }
        public ISugarQueryable<T> GetAllWhereAsync(System.Linq.Expressions.Expression<Func<T, bool>> predicate)
        {
            var data = GetAllAsync().Where(predicate);
            return data;
        } 
        #endregion
 
 
 
    }
}