wbc
2025-05-30 1167bba23716eeda7ad6342b8d6ca736adca16c6
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
using MES.Service.util;
using SqlSugar;
 
namespace MES.Service.DB;
 
public class Repository<T> : SimpleClient<T> where T : class, new()
{
    protected static SqlSugarScope Db = new(new ConnectionConfig
        {
            DbType = DbType.Oracle,
            ConnectionString = AppsettingsUtility.Settings.DataBaseConn,
            IsAutoCloseConnection = true
        },
        db =>
        {
            db.Aop.OnLogExecuting = (sql, pars) =>
            {
                Console.WriteLine(
                    UtilMethods.GetSqlString(DbType.Oracle, sql, pars));
            };
        });
 
    public Repository(ISqlSugarClient context = null) : base(context)
    {
        if (context == null) Context = Db;
    }
 
    protected int UseTransaction(Func<SqlSugarScope, int> action)
    {
        try
        {
            Db.Ado.BeginTran(); // 开始事务
            var affectedRows = action.Invoke(Db); // 执行传入的操作并获取受影响的行数
            Db.Ado.CommitTran(); // 提交事务
            return affectedRows; // 返回受影响的行数
        }
        catch (Exception)
        {
            Db.Ado.RollbackTran(); // 回滚事务
            throw; // 重新抛出异常
        }
    }
 
    public object CommonPage(QueryParameters pars, int pageIndex, int pagesize)
    {
        var tolCount = 0;
        var sugarParamters = pars.Parameters.Select(it =>
            (IConditionalModel)new ConditionalModel
            {
                ConditionalType = it.ConditionalType,
                FieldName = it.FieldName,
                FieldValue = it.FieldValue
            }).ToList();
        var query = Db.Queryable<T>();
        if (pars.OrderBys != null)
            foreach (var item in pars.OrderBys)
                query.OrderBy(item.ToSqlFilter());
        var result = query.Where(sugarParamters)
            .ToPageList(pageIndex, pagesize, ref tolCount);
        return new
        {
            count = tolCount,
            data = result
        };
    }
 
    public object CommonPage<ViewModel>(ISugarQueryable<ViewModel> query,
        QueryParameters pars, int pageIndex, int pagesize)
    {
        var tolCount = 0;
        var sugarParamters = pars.Parameters.Select(it =>
            (IConditionalModel)new ConditionalModel
            {
                ConditionalType = it.ConditionalType,
                FieldName = it.FieldName,
                FieldValue = it.FieldValue
            }).ToList();
        if (pars.OrderBys != null)
            foreach (var item in pars.OrderBys)
                query.OrderBy(item.ToSqlFilter());
        var result = query.Where(sugarParamters)
            .ToPageList(pageIndex, pagesize, ref tolCount);
        return new
        {
            count = tolCount,
            data = result
        };
    }
}
 
public class QueryParameters
{
    public List<QueryParameter> Parameters { get; set; }
    public List<string> OrderBys { get; set; }
}
 
public class QueryParameter
{
    public string FieldName { get; set; }
    public string FieldValue { get; set; }
    public ConditionalType ConditionalType { get; set; }
}