liuyn
2024-03-11 a87f1c3df03078814ee97ad0c8ac200a232419e9
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
public abstract class BaseController {
    public BaseController() {
        
    }
     /**
     * 编辑查询条件
     * @param  search             HttpServletRequest
     * @return Map<String, Object> 检索条件Map
     */
    public QueryWrapper getSearchMap(String search, String sortPlus, String sortOrder, String[] conditionArray,Schema.SObjectType type) {
        return getSearchMap(search, sortPlus, sortOrder, conditionArray,type, null);
    }
     /**
     * 编辑查询条件
     * @param  search             HttpServletRequest
     * @return Map<String, Object> 检索条件Map
     */
    public QueryWrapper getSearchMap(String search, String sortPlus, String sortOrder, String[] conditionArray,Schema.SObjectType type, List<SObjectField> sObjectFieldList) {
      
        // 对搜索条件进行过滤
        Map<String, Object> conditionMap = new Map<String, Object>();
        if(search != null && !search.equals('')){
            String param = EncodingUtil.urlDecode(search, 'utf-8');
            // 前一次检索条件
            Map<String, Object> pageCond = (Map<String, Object>)JSON.deserializeUntyped(param);
            if (pageCond != null && !pageCond.equals('')) {
                for(String key : pageCond.keySet()) {
                    if(conditionArray.contains(key)) {
                        Integer i = 1;
                        conditionMap.put(key.trim(), pageCond == null || pageCond.equals('') 
                                || pageCond.get(key) == null || (pageCond.get(key) instanceof String && ((String)pageCond.get(key)).equals(''))
                                ? null : pageCond.get(key));
                    }
                }
            }
            
        }
 
        QueryWrapper queryWrapper = new QueryWrapper(type, sObjectFieldList);
        if (sortPlus != null && !sortPlus.equals('') && sortOrder != null && !sortOrder.equals('')) {
            if ( sortOrder.equals('asc')) {
                // 判断列名称的合法性,防止SQL注入。只能是【字母,数字,下划线】
                // if (!sortPlus.matches('[A-Za-z0-9_]+')) {
                    // throw new IllegalArgumentException("非法的排序字段名称:" + sort);
                // }
                queryWrapper.orderByAsc(sortPlus);
            } else if ( sortOrder.equals('desc')) {
                // 判断列名称的合法性,防止SQL注入。只能是【字母,数字,下划线】
                // if (!sortPlus.matches('[A-Za-z0-9_]+')) {
                    // throw new IllegalArgumentException("非法的排序字段名称:" + sort);
                // }
                queryWrapper.orderByDesc(sortPlus);
            } else {
                // throw new IllegalArgumentException("非法的排序策略:" + sortOrder);
            }
        }
 
        for(String mEntry : conditionMap.keySet()) {
            if(conditionMap.get(mEntry) != null && !conditionMap.get(mEntry).equals('')) {
                if (mEntry.endsWith('Equal')) {
                    if (mEntry.endsWith('NotEqual')) {
                        queryWrapper.ne(mEntry.substring(0, mEntry.length() - 8), conditionMap.get(mEntry));
                    } else {
                        queryWrapper.eq(mEntry.substring(0, mEntry.length() - 5), conditionMap.get(mEntry));
                    }
                } else if (mEntry.endsWith('Begin')) {
                    queryWrapper.gt(mEntry.substring(0,mEntry.length() - 5),conditionMap.get(mEntry));
                } else if (mEntry.endsWith('BeginEqual')) {
                    queryWrapper.ge(mEntry.substring(0,mEntry.length() - 10),conditionMap.get(mEntry));
                } else if (mEntry.endsWith('End')) {
                    queryWrapper.lt(mEntry.substring(0,mEntry.length() - 3),conditionMap.get(mEntry));
                } else if (mEntry.endsWith('EndEqual')) {
                    queryWrapper.le(mEntry.substring(0,mEntry.length() - 8),conditionMap.get(mEntry));
                } else if (mEntry.endsWith('Like')) {
                    queryWrapper.likePlus(mEntry.substring(0,mEntry.length() - 4),conditionMap.get(mEntry));
                } else if (mEntry.endsWith('LikeLeft')) {
                    queryWrapper.likeLeft(mEntry.substring(0,mEntry.length() - 8),conditionMap.get(mEntry));
                } else if (mEntry.endsWith('LikeRight')) {
                    queryWrapper.likeRight(mEntry.substring(0,mEntry.length() - 9),conditionMap.get(mEntry));
                } else if (mEntry.endsWith('IsNull')) {
                    queryWrapper.isNull(mEntry.substring(0,mEntry.length() - 6));
                } else if (mEntry.endsWith('IsNotNull')) {
                    queryWrapper.isNotNull(mEntry.substring(0,mEntry.length() - 9));
                } else if (mEntry.endsWith('In')) {
                    if (mEntry.endsWith('NotIn')) {
                        queryWrapper.notIn(mEntry.substring(0,mEntry.length() - 5), ((String)conditionMap.get(mEntry)).split(','));
                    } else {
                        queryWrapper.inPlus(mEntry.substring(0,mEntry.length() - 2), ((String)conditionMap.get(mEntry)).split(','));
                    }
                } else if (mEntry.endsWith('EqualDate')) {
                    if (mEntry.endsWith('NotEqualDate')) {
                        queryWrapper.ne(mEntry.substring(0, mEntry.length() - 12), DateTime.newInstance(Long.valueOf(String.valueOf(conditionMap.get(mEntry)))));
                    } else {
                        queryWrapper.eq(mEntry.substring(0, mEntry.length() - 9), DateTime.newInstance(Long.valueOf(String.valueOf(conditionMap.get(mEntry)))));
                    }
                } else if (mEntry.endsWith('BeginDate')) {
                    queryWrapper.gt(mEntry.substring(0,mEntry.length() - 9),DateTime.newInstance(Long.valueOf(String.valueOf(conditionMap.get(mEntry)))));
                } else if (mEntry.endsWith('BeginEqualDate')) {
                    queryWrapper.ge(mEntry.substring(0,mEntry.length() - 14),DateTime.newInstance(Long.valueOf(String.valueOf(conditionMap.get(mEntry)))));
                } else if (mEntry.endsWith('EndDate')) {
                    queryWrapper.lt(mEntry.substring(0,mEntry.length() - 7),DateTime.newInstance(Long.valueOf(String.valueOf(conditionMap.get(mEntry)))));
                } else if (mEntry.endsWith('EndEqualDate')) {
                    queryWrapper.le(mEntry.substring(0,mEntry.length() - 12),DateTime.newInstance(Long.valueOf(String.valueOf(conditionMap.get(mEntry)))));
                } else if (mEntry.endsWith('InDate')) {
                    String s = (String)conditionMap.get(mEntry);
                    List<String> sas = s.split(',');
                    for(String sa : sas) {
                        if (mEntry.endsWith('NotInDate')) {
                           // queryWrapper.notIn(mEntry.substring(0,mEntry.length() - 9), DateTime.newInstance(Long.valueOf(String.valueOf(sa))));
                        } else {
                           // queryWrapper.inPlus(mEntry.substring(0,mEntry.length() - 6), DateTime.newInstance(Long.valueOf(String.valueOf(sa))));
                        }
                    }
                }
            }
 
        }
        return queryWrapper;
    }
 
        /**
     * 编辑查询条件
     * @param  page             HttpServletRequest
     * @return Map<String, Object> 检索条件Map
     */
    public PagePlus getPage(Integer page, Integer limits) {
        return page != null && limits != null ? new PagePlus(page, limits) : null;
    }
}