管道基础大数据平台系统开发-【后端】-Server
1
13693261870
2022-11-02 e54ef5e185c46a35641dce47b792c4f20b98879a
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
package com.lf.server.service.all;
 
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lf.server.entity.ctrl.IdNameEntity;
import com.lf.server.entity.ctrl.TabEntity;
import com.lf.server.entity.data.DictEntity;
import com.lf.server.entity.data.DomainEntity;
import com.lf.server.helper.AesHelper;
import com.lf.server.helper.ClassHelper;
import com.lf.server.helper.StringHelper;
import com.lf.server.mapper.all.BaseQueryMapper;
import com.lf.server.mapper.all.BasicMapper;
import com.lf.server.mapper.all.GeomBaseMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
/**
 * 父查询服务类
 * @author WWW
 */
@Service
public class BaseQueryService implements BaseQueryMapper {
    @Autowired
    BaseQueryMapper baseQueryMapper;
 
    /**
     * 表名Map
     */
    private static Map<String, String> tabMap = new HashMap<String, String>(3);
 
    /**
     * 获取父Mapper
     *
     * @param name Mapper名
     * @return BaseMapper
     */
    public BasicMapper getBasicMapper(String name) {
        if (StringHelper.isEmpty(name)) {
            return null;
        }
 
        Object obj = ClassHelper.getBean(name.trim() + "Mapper");
        if (!(obj instanceof BasicMapper)) {
            return null;
        }
 
        return (BasicMapper) obj;
    }
 
    /**
     * 获取空间父Mapper
     *
     * @param name Mapper名
     * @return GeomBaseMapper
     */
    public GeomBaseMapper getGeoBaseMapper(String name) {
        if (StringHelper.isEmpty(name)) {
            return null;
        }
 
        Object obj = ClassHelper.getBean(name.trim() + "Mapper");
        if (!(obj instanceof GeomBaseMapper)) {
            return null;
        }
 
        return (GeomBaseMapper) obj;
    }
 
    /**
     * 添加过滤条件
     *
     * @param wrapper QueryWrapper
     * @param filter  原始过滤条件字符串
     */
    public void addFilterWrapper(QueryWrapper wrapper, String filter) {
        if (StringHelper.isEmpty(filter)) {
            return;
        }
 
        String[] strs = filter.trim().split(" (?i)and ");
        for (String str : strs) {
            int start = str.indexOf(" ");
            if (start == -1) {
                continue;
            }
            int end = str.indexOf(" ", start + 1);
            if (end == -1) {
                continue;
            }
 
            String field = str.substring(0, start).trim();
            String express = str.substring(start + 1, end).trim().toLowerCase();
            String value = str.substring(end + 1).trim();
 
            addWrapper(wrapper, field, express, getObjectVal(value));
        }
    }
 
    /**
     * 获取值对象
     *
     * @param value 值
     * @return 对象
     */
    private Object getObjectVal(String value) {
        if (StringHelper.isInteger(value)) {
            return Long.parseLong(value);
        }
 
        if (StringHelper.isNumeric(value)) {
            return Double.parseDouble(value);
        }
 
        return value.replace("'", "");
    }
 
    /**
     * 添加包装器
     *
     * @param wrapper QueryWrapper
     * @param field   字段
     * @param express 表达式
     * @param value   值
     */
    private void addWrapper(QueryWrapper wrapper, String field, String express, Object value) {
        switch (express) {
            case "like":
                wrapper.like(field, value);
                break;
            case ">":
                wrapper.gt(field, value);
                break;
            case ">=":
                wrapper.ge(field, value);
                break;
            case "<>":
                wrapper.ne(field, value);
                break;
            case "=":
                wrapper.eq(field, value);
                break;
            case "<":
                wrapper.lt(field, value);
                break;
            case "<=":
                wrapper.le(field, value);
                break;
            default:
                break;
        }
    }
 
    /**
     * 添加空间过滤条件
     *
     * @param basicMapper 父Mapper
     * @param wrapper     QueryWrapper
     * @param wkt         WKT(著名文本)
     * @param srid        空间引用标识符
     * @throws Exception 异常
     */
    public void addGeomWrapper(BasicMapper basicMapper, QueryWrapper wrapper, String wkt, Integer srid) throws Exception {
        if (basicMapper instanceof GeomBaseMapper && !StringHelper.isEmpty(wkt) && srid != null) {
            wkt = AesHelper.decrypt(wkt);
            wrapper.apply(String.format("ST_Intersects(geom, ST_PolygonFromText('%s', %d))", wkt, srid));
        }
    }
 
    /**
     * 根据Mapper获取表名
     *
     * @param basicMapper Mapper
     * @return 表名
     */
    public String getTabName(BasicMapper basicMapper) {
        String className = ClassHelper.getClassName(basicMapper);
        if (tabMap.containsKey(className)) {
            return tabMap.get(className);
        }
 
        return getTabName(className);
    }
 
    /**
     * 根据Mapper获取表名
     *
     * @param className Mapper类名
     * @return 表名
     */
    private String getTabName(String className) {
        Class clazz = ClassHelper.getEntityClass(className);
        if (clazz == null) {
            return null;
        }
 
        TableName annotation = (TableName) clazz.getAnnotation(TableName.class);
 
        String tabName = annotation.value();
        if (tabName != null && !tabMap.containsKey(className)) {
            tabMap.put(className, tabName);
        }
 
        return tabName;
    }
 
    @Override
    public List<IdNameEntity> selectUserFuzzy(String name) {
        name = StringHelper.getLikeStr(name);
 
        return baseQueryMapper.selectUserFuzzy(name);
    }
 
    @Override
    public List<IdNameEntity> selectDepFuzzy(String name) {
        name = StringHelper.getLikeStr(name);
 
        return baseQueryMapper.selectDepFuzzy(name);
    }
 
    @Override
    public List<TabEntity> selectTabs() {
        return baseQueryMapper.selectTabs();
    }
 
    @Override
    public List<DictEntity> selectFields(String ns, String tab) {
        return baseQueryMapper.selectFields(ns, tab);
    }
 
    @Override
    public List<DomainEntity> selectDomains(String ns, String tab) {
        return baseQueryMapper.selectDomains(ns, tab);
    }
}