Tjiny
2025-05-20 74938606dd1dd8d7d77f053492b987f96a867338
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
163
164
165
166
167
168
169
170
171
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
 
namespace HH.WCS.Mobox3.Template.Util.Helper
{
    /// <summary>
    /// 数据库连接帮助类
    /// </summary>
    public static class AdoSqlHelper<T> where T : class, new()
    {
        /// <summary>
        /// 根据条件查询一条数据
        /// </summary>
        /// <param name="condition">条件</param>
        /// <returns></returns>
        public static T QueryFirst(Expression<Func<T,bool>> condition)
        {
            var db = new SqlHelper<object>().GetInstance();
            
            // 返回数据
            return db.Queryable<T>().First(condition);
        }
        
        /// <summary>
        /// 根据条件查询多条数据
        /// </summary>
        /// <param name="condition"></param>
        public static List<T> QueryList(Expression<Func<T, bool>> condition)
        {
            var db = new SqlHelper<object>().GetInstance();
            
            // 返回数据
            return db.Queryable<T>().Where(condition).ToList();
        }
 
        /// <summary>
        /// 根据条件查询对应数据数量
        /// </summary>
        /// <param name="condition">条件</param>
        /// <returns></returns>
        public static int QueryCount(Expression<Func<T,bool>> condition)
        {
            var db = new SqlHelper<object>().GetInstance();
            // 返回数据
            return db.Queryable<T>().Count(condition);
        }
 
        /// <summary>
        /// 根据条件修改一条数据
        /// </summary>
        /// <param name="model">需要进行修改的对象</param>
        /// <param name="condition">修改条件</param>
        /// <returns></returns>
        public static bool UpdateFirst(T model, Expression<Func<T, object>> condition)
        {
            var db = new SqlHelper<object>().GetInstance();
            
            // 修改数据
            var executeCommand = db.Updateable(model).UpdateColumns(condition).ExecuteCommand();
 
            if (executeCommand > 0)
            {
                return true;
            }
            else
                return false;
        }
 
        /// <summary>
        /// 根据条件修改多条数据
        /// </summary>
        /// <param name="models"></param>
        /// <param name="condition"></param>
        /// <returns></returns>
        public static int UpdateList(List<T> models, Expression<Func<T, object>> condition)
        {
            var db = new SqlHelper<object>().GetInstance();
            
            // 修改数据
            return db.Updateable(models).UpdateColumns(condition).ExecuteCommand();
        }
 
        /// <summary>
        /// 新增一条数据
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public static bool AddFirst(T model)
        {
            var db = new SqlHelper<object>().GetInstance();
 
            var executeCommand = db.Insertable(model).ExecuteCommand();
            
            if (executeCommand > 0)
                return true;
            else
                return false;
        }
        
        /// <summary>
        /// 新增多条数据
        /// </summary>
        /// <param name="models"></param>
        /// <returns></returns>
        public static int AddList(List<T> models)
        {
            var db = new SqlHelper<object>().GetInstance();
 
            var executeCommand = db.Insertable(models).ExecuteCommand();
        
            return executeCommand;
        }
        
        /// <summary>
        /// 删除一条数据
        /// </summary>
        /// <param name="model">需要删除的对象</param>
        /// <returns></returns>
        public static bool DeleteFirst(T model)
        {
            var db = new SqlHelper<object>().GetInstance();
 
            var executeCommand = db.Deleteable(model).ExecuteCommand();
 
            if (executeCommand > 0)
                return true;
            else
                return false;
        }
        
        /// <summary>
        /// 删除多条数据
        /// </summary>
        /// <param name="models">需要删除的对象集合</param>
        /// <returns></returns>
        public static int DeleteList(List<T> models)
        {
            var db = new SqlHelper<object>().GetInstance();
 
            return db.Deleteable(models).ExecuteCommand();
        }
        
        /// <summary>
        /// 根据条件删除数据
        /// </summary>
        /// <param name="condition">条件</param>
        /// <returns></returns>
        public static int DeleteList(Expression<Func<T, bool>> condition)
        {
            var db = new SqlHelper<object>().GetInstance();
 
            return db.Deleteable<T>().Where(condition).ExecuteCommand();
        }
 
 
        /// <summary>
        /// 按照时间倒序查询
        /// </summary>
        /// <param name="condition"></param>
        /// <param name="orderBy"></param>
        /// <returns></returns>
        public static T QueryFirstByDecs(Expression<Func<T, bool>> condition, Expression<Func<T, object>> orderBy)
        {
            var db = new SqlHelper<object>().GetInstance();
            
            // 返回数据
            return db.Queryable<T>().OrderByDescending(orderBy).First(condition);
        }
    }
}