package com.moon.server.service.all;
|
|
import com.baomidou.mybatisplus.annotation.TableName;
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
import com.moon.server.entity.all.RedisCacheKey;
|
import com.moon.server.entity.all.StaticData;
|
import com.moon.server.entity.ctrl.IdNameEntity;
|
import com.moon.server.entity.ctrl.KeyValueEntity;
|
import com.moon.server.entity.ctrl.TabEntity;
|
import com.moon.server.entity.data.DictEntity;
|
import com.moon.server.entity.data.DomainEntity;
|
import com.moon.server.entity.sys.AttachEntity;
|
import com.moon.server.helper.AesHelper;
|
import com.moon.server.helper.ClassHelper;
|
import com.moon.server.helper.StringHelper;
|
import com.moon.server.mapper.all.BaseQueryMapper;
|
import com.moon.server.mapper.all.BasicMapper;
|
import com.moon.server.mapper.all.GeomBaseMapper;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.stereotype.Service;
|
|
import java.util.*;
|
import java.util.concurrent.TimeUnit;
|
|
/**
|
* 父查询服务类
|
* @author WWW
|
*/
|
@Service
|
public class BaseQueryService implements BaseQueryMapper {
|
@Autowired
|
BaseQueryMapper baseQueryMapper;
|
|
@Autowired
|
RedisService redisService;
|
|
/**
|
* 表名Map
|
*/
|
private static final Map<String, String> TAB_MAP = new HashMap<>(3);
|
|
/**
|
* 添加过滤条件:(?i)and
|
*
|
* @param wrapper QueryWrapper
|
* @param filter 原始过滤条件字符串
|
*/
|
public <T> void addFilterWrapper(QueryWrapper<T> wrapper, String filter) {
|
if (StringHelper.isEmpty(filter)) {
|
return;
|
}
|
|
String[] strs = filter.trim().split(" and | or ");
|
for (String str : strs) {
|
int start = str.indexOf(" ");
|
if (start == -1) {
|
continue;
|
}
|
int end = str.indexOf(" ", start + 1);
|
if (end == -1) {
|
continue;
|
}
|
|
String field = convertFiled(str.substring(0, start).trim());
|
String express = str.substring(start + 1, end).trim().toLowerCase();
|
String value = str.substring(end + 1).trim();
|
|
start = filter.indexOf(str) - StaticData.I4;
|
end = filter.indexOf(" or ", start);
|
if (start == end && start > -1) {
|
wrapper = wrapper.or();
|
}
|
|
addWrapper(wrapper, field, express, getObjectVal(express, value));
|
}
|
}
|
|
/**
|
* 字段转换
|
*/
|
private String convertFiled(String field) {
|
StringBuilder sb = new StringBuilder();
|
for (int i = 0, c = field.length(); i < c; i++) {
|
char ch = field.charAt(i);
|
if (Character.isUpperCase(ch)) {
|
sb.append('_');
|
sb.append(Character.toLowerCase(ch));
|
} else {
|
sb.append(ch);
|
}
|
}
|
|
return sb.toString();
|
}
|
|
/**
|
* 获取值对象
|
*
|
* @param val 值
|
* @return 对象
|
*/
|
private Object getObjectVal(String express, String val) {
|
if (StringHelper.isInteger(val)) {
|
return Long.parseLong(val);
|
}
|
if (StringHelper.isNumeric(val)) {
|
return Double.parseDouble(val);
|
}
|
if (StaticData.IN.equals(express)) {
|
return val;
|
}
|
|
return val.replace("'", "");
|
}
|
|
/**
|
* 添加包装器
|
*
|
* @param wrapper QueryWrapper
|
* @param field 字段
|
* @param express 表达式
|
* @param val 值
|
*/
|
private <T> void addWrapper(QueryWrapper<T> wrapper, String field, String express, Object val) {
|
if (StringHelper.isDate(val.toString())) {
|
wrapper.apply(String.format("%s %s '%s'", field, express, val));
|
return;
|
}
|
|
switch (express) {
|
case "like":
|
wrapper.like("UPPER(" + field + ")", val.toString().toUpperCase());
|
break;
|
case ">":
|
wrapper.gt(field, val);
|
break;
|
case ">=":
|
wrapper.ge(field, val);
|
break;
|
case "<>":
|
case "!=":
|
wrapper.ne(field, val);
|
break;
|
case "=":
|
wrapper.eq(field, val);
|
break;
|
case "<":
|
wrapper.lt(field, val);
|
break;
|
case "<=":
|
wrapper.le(field, val);
|
break;
|
case "in":
|
if (val.toString().contains(StaticData.SINGLE_QUOTES)) {
|
wrapper.in(field, getStringList(val.toString()));
|
} else {
|
wrapper.in(field, getIntegerList(val.toString()));
|
}
|
break;
|
default:
|
break;
|
}
|
}
|
|
/**
|
* 获取字符串列表
|
*/
|
private List<String> getStringList(String val) {
|
return Arrays.asList(val.replace(StaticData.SINGLE_QUOTES, "").split(StaticData.COMMA));
|
}
|
|
/**
|
* 获取整数列表
|
*/
|
private List<Integer> getIntegerList(String val) {
|
List<Integer> list = new ArrayList<>();
|
for (String str : val.split(StaticData.COMMA)) {
|
list.add(Integer.parseInt(str));
|
}
|
|
return list;
|
}
|
|
/**
|
* 添加空间过滤条件
|
*
|
* @param basicMapper 父Mapper
|
* @param wrapper QueryWrapper
|
* @param wkt WKT(著名文本)
|
* @throws Exception 异常
|
*/
|
public void addGeomWrapper(BasicMapper basicMapper, QueryWrapper wrapper, String wkt) throws Exception {
|
if (basicMapper instanceof GeomBaseMapper && !StringHelper.isEmpty(wkt)) {
|
wkt = AesHelper.decrypt(wkt);
|
|
Integer srid = getSrid((GeomBaseMapper) basicMapper);
|
wrapper.apply(String.format("ST_Intersects(ST_PolygonFromText('%s', %d), geom)", wkt, srid));
|
}
|
}
|
|
/**
|
* 获取几何对象的空间参考
|
*
|
* @param basicMapper 空间基础Mapper
|
* @return SRID
|
*/
|
public Integer getSrid(GeomBaseMapper basicMapper) {
|
String tab = getTabName(basicMapper);
|
String key = RedisCacheKey.sridKey(tab);
|
|
Object obj = redisService.get(key);
|
if (obj instanceof Integer) {
|
return (Integer) obj;
|
}
|
|
Integer srid = basicMapper.selectSrid(tab);
|
if (null == srid) {
|
return StaticData.I104903;
|
}
|
redisService.put(key, srid, 5, TimeUnit.MINUTES);
|
|
return srid;
|
}
|
|
/**
|
* 根据Mapper获取表名
|
*
|
* @param basicMapper Mapper
|
* @return 表名
|
*/
|
public static String getTabName(BasicMapper basicMapper) {
|
String className = ClassHelper.getClassName(basicMapper);
|
if (TAB_MAP.containsKey(className)) {
|
return TAB_MAP.get(className);
|
}
|
|
return getTabName(className);
|
}
|
|
/**
|
* 根据Mapper获取表名
|
*
|
* @param className Mapper类名
|
* @return 表名
|
*/
|
private static 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 && !TAB_MAP.containsKey(className)) {
|
TAB_MAP.put(className, tabName);
|
}
|
|
return tabName;
|
}
|
|
/**
|
* 添加缓冲区过滤条件
|
*
|
* @param baseMapper 父Mapper
|
* @param wrapper QueryWrapper
|
* @param wkt WKT(著名文本)
|
*/
|
public void addBufferWrapper(GeomBaseMapper baseMapper, QueryWrapper wrapper, String wkt, double buffer) {
|
Integer srid = getSrid(baseMapper);
|
// buffer = buffer * 0.00000899928
|
buffer = buffer / 1852 / 60;
|
|
wrapper.apply(String.format("ST_Intersects(geom, ST_Buffer(ST_GeomFromText('%s', %d), %f, 'endcap=round join=round'))", wkt, srid, buffer));
|
}
|
|
@Override
|
public List<IdNameEntity> selectUserFuzzy(String name) {
|
name = StringHelper.getLikeUpperStr(name);
|
|
return baseQueryMapper.selectUserFuzzy(name);
|
}
|
|
@Override
|
public List<IdNameEntity> selectDepFuzzy(String name) {
|
name = StringHelper.getLikeUpperStr(name);
|
|
return baseQueryMapper.selectDepFuzzy(name);
|
}
|
|
@Override
|
public Integer selectTabsForCount(String tab, String typesFilter, String field) {
|
tab = StringHelper.getLikeUpperStr(tab);
|
|
return baseQueryMapper.selectTabsForCount(tab, typesFilter, field);
|
}
|
|
@Override
|
public List<TabEntity> selectTabsByPage(String tab, String typesFilter, String field, String filters, Integer limit, Integer offset) {
|
tab = StringHelper.getLikeUpperStr(tab);
|
|
return baseQueryMapper.selectTabsByPage(tab, typesFilter, field, filters, limit, offset);
|
}
|
|
@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);
|
}
|
|
@Override
|
public String selectRoute(double x1, double y1, double x2, double y2) {
|
return baseQueryMapper.selectRoute(x1, y1, x2, y2);
|
}
|
|
@Override
|
public List<KeyValueEntity> selectLocation(String wkt) {
|
return baseQueryMapper.selectLocation(wkt);
|
}
|
|
@Override
|
public List<KeyValueEntity> selectDirTypes(String name) {
|
name = StringHelper.getLikeUpperStr(name);
|
|
return baseQueryMapper.selectDirTypes(name);
|
}
|
|
@Override
|
public List<AttachEntity> selectAnnexByTab(String tab, String gids) {
|
return baseQueryMapper.selectAnnexByTab(tab, gids);
|
}
|
}
|