Browse Source

mongodbinit

feature/dangjian
zhangyuan 3 years ago
parent
commit
0ff5f2f7a0
  1. 4
      esua-epdc/epdc-admin/epdc-admin-client/pom.xml
  2. 26
      esua-epdc/epdc-admin/epdc-admin-client/src/main/java/com/elink/esua/epdc/dto/PolyDTO.java
  3. 5
      esua-epdc/epdc-admin/epdc-admin-server/pom.xml
  4. 1
      esua-epdc/epdc-admin/epdc-admin-server/src/main/java/com/elink/esua/epdc/controller/SysDeptController.java
  5. 59
      esua-epdc/epdc-admin/epdc-admin-server/src/main/java/com/elink/esua/epdc/service/impl/SysDeptServiceImpl.java
  6. 8
      esua-epdc/epdc-admin/epdc-admin-server/src/main/resources/application-prod.yml
  7. 11
      esua-epdc/epdc-module/epdc-events/epdc-events-server/src/main/resources/application.yml

4
esua-epdc/epdc-admin/epdc-admin-client/pom.xml

@ -33,6 +33,10 @@
<artifactId>springfox-swagger-ui</artifactId>
<version>${swagger.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb</artifactId>
</dependency>
</dependencies>
<build>

26
esua-epdc/epdc-admin/epdc-admin-client/src/main/java/com/elink/esua/epdc/dto/PolyDTO.java

@ -0,0 +1,26 @@
package com.elink.esua.epdc.dto;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.data.mongodb.core.geo.GeoJsonPolygon;
import org.springframework.data.mongodb.core.index.GeoSpatialIndexType;
import org.springframework.data.mongodb.core.index.GeoSpatialIndexed;
/**
* @author zhy
* @date 2022/12/12 16:30
*/
@NoArgsConstructor
@Data
public class PolyDTO {
private String gridName;
private String gridId;
/**
* mongodb中search的名称就是这个
*/
@JsonProperty("polygons")
@GeoSpatialIndexed(type = GeoSpatialIndexType.GEO_2DSPHERE)
private GeoJsonPolygon polygons;
}

5
esua-epdc/epdc-admin/epdc-admin-server/pom.xml

@ -97,6 +97,11 @@
<version>1.0.0</version>
<scope>compile</scope>
</dependency>
<!--mongodb-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
</dependencies>
<build>

1
esua-epdc/epdc-admin/epdc-admin-server/src/main/java/com/elink/esua/epdc/controller/SysDeptController.java

@ -66,7 +66,6 @@ public class SysDeptController {
@Resource
private CustomerGridDao customerGridDao;
@GetMapping("list")
@ApiOperation("列表")
public Result<List<SysDeptDTO>> list() {

59
esua-epdc/epdc-admin/epdc-admin-server/src/main/java/com/elink/esua/epdc/service/impl/SysDeptServiceImpl.java

@ -56,6 +56,12 @@ import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.geo.Point;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.geo.GeoJsonPoint;
import org.springframework.data.mongodb.core.geo.GeoJsonPolygon;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@ -112,6 +118,9 @@ public class SysDeptServiceImpl extends BaseServiceImpl<SysDeptDao, SysDeptEntit
@Autowired
private HttpUtil httpUtil;
@Autowired
private MongoTemplate mongoTemplate;
private static final Logger logger = LoggerFactory.getLogger(SysDeptServiceImpl.class);
@Override
@ -1405,7 +1414,7 @@ public class SysDeptServiceImpl extends BaseServiceImpl<SysDeptDao, SysDeptEntit
JSONArray jsonArray = JSON.parseArray(toResult.get("features").toString());
jsonArray.forEach(item -> {
List<Point> points = new ArrayList<>();
JSONObject feature = JSON.parseObject(item.toString());
JSONObject attributes = JSON.parseObject(feature.get("attributes").toString());
String gridName = attributes.get("网格名称").toString();
@ -1426,7 +1435,10 @@ public class SysDeptServiceImpl extends BaseServiceImpl<SysDeptDao, SysDeptEntit
entity.setGridId(gridId);
list.add(entity);
Point p = new Point(Double.parseDouble(longitude), Double.parseDouble(latitude));
points.add(p);
});
saveCoordinateCache(gridName, gridId, points);
});
if (!list.isEmpty()) {
@ -1436,25 +1448,46 @@ public class SysDeptServiceImpl extends BaseServiceImpl<SysDeptDao, SysDeptEntit
}
/**
* 地理坐标放到mongodb
*
* @param gridName
* @param gridId
* @param points
* @return void
* @author zhy
* @date 2022/12/16 17:31
*/
private void saveCoordinateCache(String gridName, String gridId, List<Point> points) {
PolyDTO polyDTO = new PolyDTO();
polyDTO.setGridId(gridId);
polyDTO.setGridName(gridName);
GeoJsonPolygon geoJsonPolygon = new GeoJsonPolygon(points);
polyDTO.setPolygons(geoJsonPolygon);
// 我们将多个对象放到同一个容器polygons中,每个对象会生成自己的id,需要注意不要重复保存,否则会产生覆盖的区域
// 如果包含了主键,insert会提示主键冲突,而save会进行更新(设置主键需要使用@Id注解,否则自动生成)
// insert可以批量处理,效率高,save只能逐个处理
mongoTemplate.save(polyDTO, "polygons");
}
@Override
public GisGridDTO getCoordinate(GisFormDTO formDTO) {
Map<String, String> urlParams = new HashMap<>();
String geometryStr = "point(" + formDTO.getLongitude() + "," + formDTO.getLatitude() + ")";
urlParams.put("geometry", geometryStr);
String resultStr = httpUtil.getGisStr(urlParams);
JSONObject toResult = JSON.parseObject(resultStr);
JSONArray jsonArray = JSON.parseArray(toResult.get("features").toString());
Query query = new Query(Criteria.where("polygons").
intersects(new GeoJsonPoint(Double.parseDouble(formDTO.getLongitude()), Double.parseDouble(formDTO.getLatitude()))));
// 如果带了collectionName,MongoTemplate就查询collectionName集合中的记录;
// 如果没带collectionName,MongoTemplate就会到缓存上下文mappingContext中根据classname获取集合名,
// 如果没有获取到,就会新建一个根据classname生成的集合名(首字母小写),这样就可能和mongo中的集合名不一致,导致查询不到结果。
List<PolyDTO> dbList = mongoTemplate.find(query, PolyDTO.class, "polygons");
if (jsonArray.size() == NumConstant.ZERO) {
if (dbList.size() == NumConstant.ZERO) {
throw new RenException("该坐标不在任何网格内,请重新选择");
} else if (jsonArray.size() > NumConstant.ONE) {
} else if (dbList.size() > NumConstant.ONE) {
throw new RenException("该坐标处于多个网格交界处,请重新选择");
} else {
JSONObject feature = JSON.parseObject(jsonArray.get(0).toString());
JSONObject attributes = JSON.parseObject(feature.get("attributes").toString());
String gridName = attributes.get("网格名称").toString();
String gridId = attributes.get("网格编码").toString();
PolyDTO polyDTO = dbList.get(0);
String gridName = polyDTO.getGridName();
String gridId = polyDTO.getGridId();
GisGridDTO gisGridDTO = gisGridService.getByGisGridId(gridId);
if (gisGridDTO == null) {

8
esua-epdc/epdc-admin/epdc-admin-server/src/main/resources/application-prod.yml

@ -34,3 +34,11 @@ spring:
log-slow-sql: true
slow-sql-millis: 1000
merge-sql: false
data:
mongodb:
host: dds-m5e31db1fcc742341.mongodb.rds.aliyuncs.com
port: 3717
username: root
password: 'Elink833066'
database: gis
authentication-database: admin

11
esua-epdc/epdc-module/epdc-events/epdc-events-server/src/main/resources/application.yml

@ -61,11 +61,12 @@ spring:
password: @spring.datasource.druid.password@
data:
mongodb:
host: localhost
port: 27017
username: test
password: '123'
database: test
host: dds-m5e31db1fcc742341.mongodb.rds.aliyuncs.com
port: 3717
username: root
password: 'Elink833066'
database: gis
authentication-database: admin
feign:
hystrix:

Loading…
Cancel
Save