| | |
| | | package com.lf.server.service.all; |
| | | |
| | | import com.baomidou.mybatisplus.annotation.TableName; |
| | | import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; |
| | | import com.baomidou.mybatisplus.core.mapper.BaseMapper; |
| | | import com.lf.server.entity.all.RedisCacheKey; |
| | | 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.data.redis.cache.RedisCache; |
| | | import org.springframework.stereotype.Service; |
| | | |
| | | import java.util.HashMap; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.util.concurrent.TimeUnit; |
| | | |
| | | /** |
| | | * 父查询服务类 |
| | | * @author WWW |
| | | */ |
| | | @Service |
| | | public class BaseQueryService { |
| | | public class BaseQueryService implements BaseQueryMapper { |
| | | @Autowired |
| | | BaseQueryMapper baseQueryMapper; |
| | | |
| | | @Autowired |
| | | RedisService redisService; |
| | | |
| | | /** |
| | | * 表名Map |
| | | */ |
| | | private static Map<String, String> tabMap = new HashMap<String, String>(3); |
| | | |
| | | /** |
| | | * 获取父Mapper |
| | | * |
| | | * @param name Mapper名 |
| | | * @return BaseMapper |
| | | */ |
| | | public BaseMapper getBaseMapper(String name) { |
| | | public BasicMapper getBasicMapper(String name) { |
| | | if (StringHelper.isEmpty(name)) { |
| | | return null; |
| | | } |
| | | |
| | | Object obj = ClassHelper.getBean(name.trim() + "Mapper"); |
| | | if (!(obj instanceof BaseMapper)) { |
| | | if (!(obj instanceof BasicMapper)) { |
| | | return null; |
| | | } |
| | | |
| | | return (BaseMapper) obj; |
| | | return (BasicMapper) obj; |
| | | } |
| | | |
| | | /** |
| | |
| | | } |
| | | |
| | | /** |
| | | * @param wrapper |
| | | * @param filter |
| | | * 添加过滤条件 |
| | | * |
| | | * @param wrapper QueryWrapper |
| | | * @param filter 原始过滤条件字符串 |
| | | */ |
| | | public <T> void addFilterWrapper(QueryWrapper<T> wrapper, String filter) { |
| | | public void addFilterWrapper(QueryWrapper wrapper, String filter) { |
| | | if (StringHelper.isEmpty(filter)) { |
| | | return; |
| | | } |
| | |
| | | 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, value); |
| | | |
| | | addWrapper(wrapper, field, express, getObjectVal(value)); |
| | | } |
| | | } |
| | | |
| | | private <T> void addWrapper(QueryWrapper<T> wrapper, String field, String express, String 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); |
| | |
| | | } |
| | | |
| | | /** |
| | | * 添加空间查询条件 |
| | | * 添加空间过滤条件 |
| | | * |
| | | * @param baseMapper 父Mapper |
| | | * @param wrapper QueryWrapper |
| | | * @param wkt WKT(著名文本) |
| | | * @param srid 空间引用标识符 |
| | | * @param basicMapper 父Mapper |
| | | * @param wrapper QueryWrapper |
| | | * @param wkt WKT(著名文本) |
| | | * @throws Exception 异常 |
| | | */ |
| | | public void addGeomWrapper(BaseMapper baseMapper, QueryWrapper<Object> wrapper, String wkt, Integer srid) throws Exception { |
| | | if (baseMapper instanceof GeomBaseMapper && !StringHelper.isEmpty(wkt) && srid != null) { |
| | | public void addGeomWrapper(BasicMapper basicMapper, QueryWrapper wrapper, String wkt) throws Exception { |
| | | if (basicMapper instanceof GeomBaseMapper && !StringHelper.isEmpty(wkt)) { |
| | | wkt = AesHelper.decrypt(wkt); |
| | | wrapper.apply(String.format("ST_Intersects(geom, ST_PolygonFromText('%s', %d))", wkt, srid)); |
| | | |
| | | Integer srid = getSrid((GeomBaseMapper) basicMapper); |
| | | wrapper.apply(String.format("ST_Intersects(ST_PolygonFromText('%s', %d), geom)", wkt, srid)); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 获取几何对象的空间参考 |
| | | * |
| | | * @param basicMapper 空间基础Mapper |
| | | * @return SRID |
| | | */ |
| | | private 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 (srid != null) { |
| | | redisService.put(key, srid, 5, TimeUnit.MINUTES); |
| | | } |
| | | |
| | | return 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); |
| | | } |
| | | } |