schangxiang@126.com
2025-11-04 f5ed29dc26c7cd952d56ec5721a2efc43cd25992
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
using DataEntity.Device;
using DataEntity.Page;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using XCommon.SqlSugar;
using DbType = SqlSugar.DbType;
 
namespace DataRWDAL.Base
{
    public class BaseDB
    {
        /// <summary>
        /// 数据库连接信息
        /// </summary>
        private static readonly string m_connString = ConfigurationManager.AppSettings["mysqlstring"];
 
        /// <summary>
        /// DB实例化
        /// </summary>
        /// <returns></returns>
        public static SqlSugarClient GetInstance()
        {
            SqlSugarClient db = new SqlSugarClient(new ConnectionConfig()
            {
                ConnectionString = m_connString,
                DbType = DbType.MySql,
                IsAutoCloseConnection = true,
                ConfigureExternalServices = new ConfigureExternalServices()
                {
                    SqlFuncServices = ExtMethods.GetExpMethods,
                }
            });
 
            db.Ado.IsEnableLogEvent = true;
            return db;
        }
 
        /// <summary>
        /// 获取分页数据
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="pagination"></param>
        /// <param name="whereExpression"></param>
        /// <param name="orderExpression"></param>
        /// <param name="orderType"></param>
        /// <returns></returns>
        public static Tuple<List<T>, int> GetPage<T>(Pagination pagination, Expression<Func<T, bool>> whereExpression,
            Expression<Func<T, object>> orderExpression, OrderByType orderType)
        {
            using (var db = GetInstance())
            {
                var totalCount = 0;
                List<T> lstResult = null;
                if (orderExpression == null)
                {
                    lstResult = db.Queryable<T>().Where(whereExpression).ToPageList(pagination.Current, pagination.PageSize, ref totalCount).ToList();
                }
                else
                {
                    lstResult = db.Queryable<T>().Where(whereExpression).OrderBy(orderExpression, orderType).
                        ToPageList(pagination.Current, pagination.PageSize, ref totalCount).ToList();
                }
 
                return new Tuple<List<T>, int>(lstResult, totalCount);
            }
        }
 
        public static List<T> GetList<T>(int topNum, Expression<Func<T, bool>> whereExpression, Expression<Func<T, object>> orderExpression, OrderByType orderType)
        {
            using (var db = GetInstance())
            {
                var _list = new List<T>();
                if (orderExpression == null)
                {
                    if (topNum == -1)
                    {
                        _list = db.Queryable<T>().Where(whereExpression).ToList();
                    }
                    else
                    {
                        _list = db.Queryable<T>().Where(whereExpression).Take(topNum).ToList();
                    }
                }
                else
                {
                    if (topNum == -1)
                    {
                        _list = db.Queryable<T>().Where(whereExpression).OrderBy(orderExpression, orderType).ToList();
                    }
                    else
                    {
                        _list = db.Queryable<T>().Where(whereExpression).OrderBy(orderExpression, orderType).Take(topNum).ToList();
                    }
                }
                return _list;
            }
        }
 
        public static void CreateClassFile()
        {
            using (var db = GetInstance())
            {
                //db.DbFirst.CreateClassFile(@"D:\Model\bak", "FlowManage.Model.Model");
 
                db.DbFirst
                    .SettingClassTemplate(old =>
                    {
                        return "{using}\r\n\r\nnamespace {Namespace}\r\n{\r\n\t{ClassDescription}{SugarTable}\r\n\tpublic partial class {ClassName} : BaseModel\r\n\t    {\r\n\t{PropertyName}\r\n\t    }\r\n}\r\n\r\n";
                    })
                    .SettingNamespaceTemplate(old =>
                    {
                        //修改using命名空间
                        return "using SqlSugar;\r\nusing System;\r\nusing System.ComponentModel;\r\nusing System.Linq;\r\nusing System.Text;";
                    })
                    .SettingPropertyDescriptionTemplate(old =>
                    {
                        return @"           /// <summary>
           /// {PropertyDescription}
           /// </summary>";
                    })
                    .SettingPropertyTemplate(old =>
                    {
                        return old;
                    })
                    //.SettingConstructorTemplate(old =>
                    //    {
                    //        return old;//修改构造函数
                    //    })
                    .CreateClassFile(@"D:\Model\bak", "FlowManage.Model.Model");
            }
        }
    }
}