高章伟
2022-02-18 8b5f4c6c281cfa548f92de52c8021e37aa81901e
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
public without sharing class DataBasePlus {
 
    public DataBasePlus() {
        
    }
 
    public static String Mysql;
    public static String MysqlWhere;
 
    // 根据 Wrapper,查询一条记录。结果集,如果是多个会抛出异常,随机取一条加上限制条件 wrapper.last("LIMIT 1")
//    static SObject getOne() {
//         QueryWrapper queryWrapper = new QueryWrapper(Test01__mdt.getSObjectType());
//         return getOne( queryWrapper, true);
        
//     }
    // 根据 Wrapper,查询一条记录
    public static SObject getOne(QueryWrapper queryWrapper, boolean throwEx) {
        
        List<SObject> sObjectList = getRes(queryWrapper.getType(), queryWrapper.getSObjectFieldList(),queryWrapper.build(),'');
        if(sObjectList != null && sObjectList.size() > 1){
        //异常
        }else{
            return sObjectList == null || sObjectList.size() == 0 ? null : sObjectList.get(0);
        }   
        return null; 
    }
 
    // 查询列表
    public static List<SObject> listPlus(QueryWrapper queryWrapper) {
        return getRes(queryWrapper.getType(), queryWrapper.getSObjectFieldList(),queryWrapper.build(),'');
    }
 
    // 条件分页查询
    public static PagePlus page(PagePlus page, QueryWrapper queryWrapper) {
        page.setRecords(getRes(queryWrapper.getType(), queryWrapper.getSObjectFieldList(),queryWrapper.build(),page.getPage()));
        page.setTotal(count(queryWrapper));
        return page;
    }
    // 条数
    public static Integer count(QueryWrapper queryWrapper) {
        return getCount(queryWrapper.getType(), queryWrapper.build());
    }
    
 
    // 获取查询数据
    private static List<SObject> getRes(Schema.SObjectType type, List<SObjectField> sObjectFieldList, String bulid, String limits){
        //获取传过来对象的列的值
        List<SObject> sobjList = null;
        String sql = '';
        // Make the describe call
        try {
            // Make the describe call  
            Schema.DescribeSObjectResult results = type.getDescribe();
           
            //获取表中所有列的字段
            Map<String, Schema.SObjectField> fieldMap = results.fields.getMap();
            //字段拼接成字符串
            for(String keyset : fieldMap.keySet()) {
                Schema.DescribeFieldResult checksObjectField=fieldMap.get(keyset).getDescribe();
                if(checksObjectField.isAccessible()==true){
                    if(checksObjectField.isAccessible()){
                        if(sql == ''){
                            sql = keyset;
                        }else{
                            sql = sql + ',' + keyset;
                        }
                    }
                }
            }
            if(sObjectFieldList != null && sObjectFieldList.size() > 0){
 
                for(Schema.SObjectField sObjectField : sObjectFieldList) {
                    Schema.DescribeFieldResult dfr =sObjectField.getDescribe();
                    Integer num=dfr.getRelationshipOrder(); //0
                    if(num !=null){
                      List<Schema.sObjectType> list1 = dfr.getReferenceTo(); //表名
                      String a=dfr.getRelationshipName();  //字段名
                      for (Schema.sObjectType st : list1) {
                          // Make the describe call
                            Schema.DescribeSObjectResult res = st.getDescribe();
                        
                            //获取表中所有列的字段
                            Map<String, Object> fieldMaps = res.fields.getMap();
                            //字段拼接成字符串
                            for(String keyset : fieldMaps.keySet()) {
                                if(sql == ''){
                                    sql = a + '.' + keyset;
                                }else{
                                    sql = sql + ',' + a + '.' +  keyset;
                                }
                            }
                        
                      }
                    }
                }
 
            }
            //拼接查询语句
            if(!''.equals(sql)){
                sql = 'select ' + sql +'  from ' + results.Name;
            }
            //条件
            if(!''.equals(bulid)){
                sql += bulid; 
            }
            //分页
            if(!''.equals(limits)){
                sql += limits; 
            }
            System.debug(sql);
            sobjList = Database.query(sql);
            MysqlWhere = bulid;
            Mysql = sql;
 
        } catch(Exception e) {
            // 捕捉 DML 相关的异常
          
        }       
        
        return  sobjList;
    }
 
    // 获取查询数据条数
    public static Integer getCount(Schema.SObjectType type, String bulid){
        //获取传过来对象的列的值
        Integer cnt = 0;
        String sql = '';
        // Make the describe call
        try {
            // Make the describe call
            Schema.DescribeSObjectResult results = type.getDescribe();
            
            //拼接查询语句
            sql = 'select count()  from ' + results.Name;
            //条件
            if(!''.equals(bulid)){
                sql += bulid; 
            }
            //sql
            System.debug(sql);
            cnt = Database.countQuery(sql);
        } catch(Exception e) {
            // 捕捉 DML 相关的异常
            
        }       
        
        return  cnt;
    }
    
 
}