Просмотр исходного кода

调整企业名称为升序排序

liwh 1 год назад
Родитель
Сommit
3f1990bc90

+ 13 - 0
cql-service/src/main/java/cn/com/taiji/cql/service/ISqlService.java

@@ -0,0 +1,13 @@
+package cn.com.taiji.cql.service;
+
+
+import cn.com.taiji.common.domain.PageResult;
+import org.geotools.api.feature.FeatureVisitor;
+
+import java.util.Map;
+
+public interface ISqlService {
+
+
+    PageResult sqlQuery4Mysql(Map<String, Object> mapParams, String sql, Object[] bindArgs);
+}

+ 159 - 0
cql-service/src/main/java/cn/com/taiji/cql/service/impl/SqlServiceImpl.java

@@ -0,0 +1,159 @@
+package cn.com.taiji.cql.service.impl;
+import cn.com.taiji.common.domain.PageResult;
+import cn.com.taiji.common.enums.CommonConstant;
+import cn.com.taiji.cql.service.ISqlService;
+import lombok.extern.slf4j.Slf4j;
+import org.geotools.data.jdbc.datasource.DataSourceFinder;
+import org.geotools.jdbc.JDBCDataStoreFactory;
+import org.springframework.stereotype.Service;
+
+import javax.sql.DataSource;
+import java.sql.*;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+@Slf4j
+@Service
+public class SqlServiceImpl implements ISqlService {
+
+    @Override
+    public PageResult sqlQuery4Mysql(Map<String, Object> mapParams, String sql, Object[] bindArgs) {
+
+
+        //查询记录总数
+        Long count =0l;
+
+        //当前页数
+        Long pageNumber= CommonConstant.PAGE_NUMBER;
+
+        //一页显示多少条记录
+        Long pageSize=CommonConstant.PAGE_SIZE;
+
+        //总页数
+        Long pageCount =0l;
+
+        List<Map<String, Object>> datas = new ArrayList<>();
+
+        String host = mapParams.get(JDBCDataStoreFactory.HOST.key).toString();
+
+        String database = mapParams.get(JDBCDataStoreFactory.DATABASE.key).toString();
+
+        DataSource dataSource = null;
+
+        Connection connection = null;
+        PreparedStatement preparedStatement = null;
+        ResultSet resultSet = null;
+
+        try {
+
+            dataSource = DataSourceFinder.getDataSource(mapParams);
+
+            if (dataSource != null) {
+
+                log.info("系统连接到位于:" + host + "的空间数据库" + database + "成功!");
+
+                connection = dataSource.getConnection();
+
+                preparedStatement = connection.prepareStatement(sql);
+                if (bindArgs != null) {
+                    /**设置sql占位符中的值**/
+                    for (int i = 0; i < bindArgs.length; i++) {
+                        preparedStatement.setObject(i + 1, bindArgs[i]);
+                    }
+                }
+
+                System.out.println(getExecSQL(sql, bindArgs));
+
+                /**执行sql语句,获取结果集**/
+
+                resultSet = preparedStatement.executeQuery();
+
+                datas = getDatas(resultSet);
+
+            }
+        }catch (Exception exception){
+
+            exception.printStackTrace();
+            log.error("系统连接到位于:" + mapParams.get(JDBCDataStoreFactory.HOST.key) + "的空间数据库" + mapParams.get(JDBCDataStoreFactory.DATABASE.key) + "失败!请检查相关参数");
+
+        }finally {
+
+            if (resultSet != null){
+                try{
+                    resultSet.close();
+                }catch (Exception ex){
+                    ex.printStackTrace();
+                }
+            }
+
+            if (preparedStatement != null){
+                try{
+                    preparedStatement.close();
+                }catch (Exception ex){
+                    ex.printStackTrace();
+                }
+            }
+
+            if (connection != null){
+                try{
+                    connection.close();
+                }catch (Exception ex){
+                    ex.printStackTrace();
+                }
+            }
+        }
+
+        int respCode = 0;
+
+        String respMsg = "操作成功";
+
+        if(datas.size() == 0){
+
+            respCode = 1;
+            respMsg = "操作成功,但没有查找到记录!";
+
+        }
+
+        return PageResult.<List<Map<String, Object>>>builder().datas(datas).resp_code(respCode).resp_msg(respMsg).pageSize(pageSize).pageNumber(pageNumber).pageCount(pageCount).recordCount(count).build();
+    }
+
+    private String getExecSQL(String sql, Object[] bindArgs) {
+        StringBuilder sb = new StringBuilder(sql);
+        if (bindArgs != null && bindArgs.length > 0) {
+            int index = 0;
+            for (int i = 0; i < bindArgs.length; i++) {
+                index = sb.indexOf("?", index);
+                sb.replace(index, index + 1, String.valueOf(bindArgs[i]));
+            }
+        }
+        return sb.toString();
+    }
+
+    /**
+     * 将结果集对象封装成List<Map<String, Object>> 对象
+     *
+     * @param resultSet 结果多想
+     * @return 结果的封装
+     * @throws SQLException
+     */
+    private List<Map<String, Object>> getDatas(ResultSet resultSet) throws SQLException {
+        List<Map<String, Object>> datas = new ArrayList<>();
+        /**获取结果集的数据结构对象**/
+        ResultSetMetaData metaData = resultSet.getMetaData();
+        while (resultSet.next()) {
+            Map<String, Object> rowMap = new HashMap<>();
+            for (int i = 1; i <= metaData.getColumnCount(); i++) {
+                rowMap.put(metaData.getColumnName(i).replaceAll("(\\[|\\]|`)",""), resultSet.getObject(i));
+            }
+            datas.add(rowMap);
+        }
+//        System.out.println("成功查询到了" + datas.size() + "行数据");
+        for (int i = 0; i < datas.size(); i++) {
+            Map<String, Object> map = datas.get(i);
+//            System.out.println("第" + (i + 1) + "行:" + map);
+        }
+        return datas;
+    }
+}

+ 50 - 0
tile-web/src/main/java/cn/com/taiji/web/service/impl/EnterpriseServiceImpl.java

@@ -4,6 +4,7 @@ import cn.com.taiji.common.domain.PageResult;
 import cn.com.taiji.common.domain.Result;
 import cn.com.taiji.cql.model.GeoDataSource;
 import cn.com.taiji.cql.service.IECqlService;
+import cn.com.taiji.cql.service.ISqlService;
 import cn.com.taiji.web.config.EnterpriseConfig;
 import cn.com.taiji.web.model.DeviceInfo;
 import cn.com.taiji.web.model.EnterpriseInfo;
@@ -14,6 +15,7 @@ import cn.com.taiji.web.vo.EnterpriseVo;
 import com.alibaba.fastjson.JSONObject;
 import lombok.extern.slf4j.Slf4j;
 import org.geotools.api.referencing.crs.CoordinateReferenceSystem;
+import org.geotools.data.jdbc.datasource.DBCPDataSourceFactory;
 import org.geotools.feature.visitor.CountVisitor;
 import org.geotools.geometry.jts.JTS;
 import org.geotools.jdbc.JDBCDataStoreFactory;
@@ -36,6 +38,9 @@ public class EnterpriseServiceImpl implements IEnterpriseService {
     @Autowired
     private IECqlService ecqlService;
 
+    @Autowired
+    private ISqlService sqlService;
+
     @Override
     public PageResult enterpriseQuery(EnterpriseVo enterpriseVo) {
 
@@ -103,6 +108,51 @@ public class EnterpriseServiceImpl implements IEnterpriseService {
     @Override
     public Result deviceQuery(DeviceVo deviceVo) {
 
+        //构建mysql数据库连接数据源--DataSource
+        Map<String, Object> mapParams = new HashMap<String, Object>();
+        mapParams.put(DBCPDataSourceFactory.DSTYPE.key, "DBCP");
+        mapParams.put(DBCPDataSourceFactory.DRIVERCLASS.key, "com.mysql.cj.jdbc.Driver");
+        mapParams.put(DBCPDataSourceFactory.JDBC_URL.key, "jdbc:mysql://"+enterpriseConfig.getDsIp()+":"+enterpriseConfig.getDsPort()+"/"+enterpriseConfig.getDsBaseName()+"?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false&zeroDateTimeBehavior=convertToNull&serverTimezone=Asia/Shanghai");
+        mapParams.put(DBCPDataSourceFactory.USERNAME.key, enterpriseConfig.getDsUserName());
+        mapParams.put(DBCPDataSourceFactory.PASSWORD.key, enterpriseConfig.getDsPassword());
+        mapParams.put(DBCPDataSourceFactory.MAXACTIVE.key, Integer.valueOf(10));
+        mapParams.put(DBCPDataSourceFactory.MAXIDLE.key, Integer.valueOf(0));
+        mapParams.put(JDBCDataStoreFactory.HOST.key, enterpriseConfig.getDsIp());
+        mapParams.put(JDBCDataStoreFactory.DATABASE.key, enterpriseConfig.getDsBaseName());
+
+
+        String sql = "SELECT *,(st_distance(point(longitude, latitude), point("+deviceVo.getLon()+","+deviceVo.getLat()+"))*111195) AS distance FROM "+enterpriseConfig.getDevice()+" having distance < "+deviceVo.getRadius()*1000+" order by distance asc";
+
+        PageResult pageResult = sqlService.sqlQuery4Mysql(mapParams,sql,null);
+
+        List<DeviceInfo> deviceInfoList = new ArrayList<DeviceInfo>();
+
+        if(pageResult.getResp_code() ==0) {
+
+            List<Map<String, Object>> dataList = (List<Map<String, Object>>) pageResult.getDatas();
+
+            for (Map<String, Object> dataMap : dataList) {
+
+                if (dataMap.containsKey("location")) {
+
+                    dataMap.remove("location");
+
+                }
+
+                DeviceInfo deviceInfo = JSONObject.parseObject(JSONObject.toJSONString(dataMap), DeviceInfo.class);
+
+                deviceInfoList.add(deviceInfo);
+            }
+        }
+
+        return Result.of(deviceInfoList,pageResult.getResp_code(),pageResult.getResp_msg());
+
+
+    }
+
+
+    public Result deviceQuery2(DeviceVo deviceVo) {
+
         //数据源
         Map<String, Object> entDsParams = new HashMap<String, Object>();
         entDsParams.put(JDBCDataStoreFactory.DATABASE.key, enterpriseConfig.getDsBaseName());