本項目專欄:
物流項目_Auc23的博客-CSDN博客
建議先看這期:
MongoDB入門之Java的使用-CSDN博客
需求分析?
在項目中,會有兩個作業范圍,分別是機構作業范圍和快遞員作業范圍,這兩個作業范圍的邏輯是一致的,就是在地圖中進行畫出范圍,就是其作業范圍。
實現分析?
對于作業范圍是一個由多個坐標點組成的多邊形,并且必須是閉合的多邊形,這個就比較適合用MongoDB來存儲。
現在想一個實際需求,用戶小王下了訂單,如何找到屬于該服務范圍內的快遞員呢?這個就需要使用MongoDB的$geoIntersects
查詢操作,其原理就是查找小王的位置坐標點與哪個多邊形有交叉,這個就是為其服務的快遞員。
ServiceScopeEntity
/*** 服務范圍實體*/
@Data
@Document("sl_service_scope")
public class ServiceScopeEntity {@Id@JsonIgnoreprivate ObjectId id;/*** 業務id,可以是機構或快遞員*/@Indexedprivate Long bid;/*** 類型 {@link com.sl.ms.scope.enums.ServiceTypeEnum}*/@Indexedprivate Integer type;/*** 多邊形范圍,是閉合的范圍,開始經緯度與結束經緯度必須一樣* x: 經度,y:緯度*/@GeoSpatialIndexed(type = GeoSpatialIndexType.GEO_2DSPHERE)private GeoJsonPolygon polygon;private Long created; //創建時間private Long updated; //更新時間
}
/*** 服務類型枚舉*/
public enum ServiceTypeEnum {ORGAN(1, "機構"),COURIER(2, "快遞員");/*** 類型編碼*/private final Integer code;/*** 類型值*/private final String value;ServiceTypeEnum(Integer code, String value) {this.code = code;this.value = value;}public Integer getCode() {return code;}public String getValue() {return value;}public static ServiceTypeEnum codeOf(Integer code) {return EnumUtil.getBy(ServiceTypeEnum::getCode, code);}
}
ScopeService
在ScopeService中主要定義了如下方法:
- 新增或更新服務范圍
- 根據主鍵id刪除數據
- 根據業務id和類型刪除數據
- 根據主鍵查詢數據
- 根據業務id和類型查詢數據
- 根據坐標點查詢所屬的服務對象
- 根據詳細地址查詢所屬的服務對象
/*** 服務范圍Service*/
public interface ScopeService {/*** 新增或更新服務范圍** @param bid 業務id* @param type 類型* @param polygon 多邊形坐標點* @return 是否成功*/Boolean saveOrUpdate(Long bid, ServiceTypeEnum type, GeoJsonPolygon polygon);/*** 根據主鍵id刪除數據** @param id 主鍵* @return 是否成功*/Boolean delete(String id);/*** 根據業務id和類型刪除數據** @param bid 業務id* @param type 類型* @return 是否成功*/Boolean delete(Long bid, ServiceTypeEnum type);/*** 根據主鍵查詢數據** @param id 主鍵* @return 服務范圍數據*/ServiceScopeEntity queryById(String id);/*** 根據業務id和類型查詢數據** @param bid 業務id* @param type 類型* @return 服務范圍數據*/ServiceScopeEntity queryByBidAndType(Long bid, ServiceTypeEnum type);/*** 根據坐標點查詢所屬的服務對象** @param type 類型* @param point 坐標點* @return 服務范圍數據*/List<ServiceScopeEntity> queryListByPoint(ServiceTypeEnum type, GeoJsonPoint point);/*** 根據詳細地址查詢所屬的服務對象** @param type 類型* @param address 詳細地址,如:北京市昌平區金燕龍辦公樓傳智教育總部* @return 服務范圍數據*/List<ServiceScopeEntity> queryListByPoint(ServiceTypeEnum type, String address);
}
?ScopeController
/*** 服務范圍*/
@Api(tags = "服務范圍")
@RestController
@RequestMapping("scopes")
@Validated
public class ScopeController {@Resourceprivate ScopeService scopeService;/*** 新增或更新服務服務范圍** @return REST標準響應*/@ApiOperation(value = "新增/更新", notes = "新增或更新服務服務范圍")@PostMappingpublic ResponseEntity<Void> saveScope(@RequestBody ServiceScopeDTO serviceScopeDTO) {ServiceScopeEntity serviceScopeEntity = EntityUtils.toEntity(serviceScopeDTO);Long bid = serviceScopeEntity.getBid();ServiceTypeEnum type = ServiceTypeEnum.codeOf(serviceScopeEntity.getType());Boolean result = this.scopeService.saveOrUpdate(bid, type, serviceScopeEntity.getPolygon());if (result) {return ResponseEntityUtils.ok();}return ResponseEntityUtils.error();}/*** 刪除服務范圍** @param bid 業務id* @param type 類型* @return REST標準響應*/@ApiImplicitParams({@ApiImplicitParam(name = "bid", value = "業務id,可以是機構或快遞員", dataTypeClass = Long.class),@ApiImplicitParam(name = "type", value = "類型,1-機構,2-快遞員", dataTypeClass = Integer.class)})@ApiOperation(value = "刪除", notes = "刪除服務范圍")@DeleteMapping("{bid}/{type}")public ResponseEntity<Void> delete(@NotNull(message = "bid不能為空") @PathVariable("bid") Long bid,@NotNull(message = "type不能為空") @PathVariable("type") Integer type) {Boolean result = this.scopeService.delete(bid, ServiceTypeEnum.codeOf(type));if (result) {return ResponseEntityUtils.ok();}return ResponseEntityUtils.error();}/*** 查詢服務范圍** @param bid 業務id* @param type 類型* @return 服務范圍數據*/@ApiImplicitParams({@ApiImplicitParam(name = "bid", value = "業務id,可以是機構或快遞員", dataTypeClass = Long.class),@ApiImplicitParam(name = "type", value = "類型,1-機構,2-快遞員", dataTypeClass = Integer.class)})@ApiOperation(value = "查詢", notes = "查詢服務范圍")@GetMapping("{bid}/{type}")public ResponseEntity<ServiceScopeDTO> queryServiceScope(@NotNull(message = "bid不能為空") @PathVariable("bid") Long bid,@NotNull(message = "type不能為空") @PathVariable("type") Integer type) {ServiceScopeEntity serviceScopeEntity = this.scopeService.queryByBidAndType(bid, ServiceTypeEnum.codeOf(type));return ResponseEntityUtils.ok(EntityUtils.toDTO(serviceScopeEntity));}/*** 地址查詢服務范圍** @param type 類型,1-機構,2-快遞員* @param address 詳細地址,如:北京市昌平區金燕龍辦公樓傳智教育總部* @return 服務范圍數據列表*/@ApiImplicitParams({@ApiImplicitParam(name = "type", value = "類型,1-機構,2-快遞員", dataTypeClass = Integer.class),@ApiImplicitParam(name = "address", value = "詳細地址,如:北京市昌平區金燕龍辦公樓傳智教育總部", dataTypeClass = String.class)})@ApiOperation(value = "地址查詢服務范圍", notes = "地址查詢服務范圍")@GetMapping("address")public ResponseEntity<List<ServiceScopeDTO>> queryListByAddress(@NotNull(message = "type不能為空") @RequestParam("type") Integer type,@NotNull(message = "address不能為空") @RequestParam("address") String address) {List<ServiceScopeEntity> serviceScopeEntityList = this.scopeService.queryListByPoint(ServiceTypeEnum.codeOf(type), address);return ResponseEntityUtils.ok(EntityUtils.toDTOList(serviceScopeEntityList));}/*** 位置查詢服務范圍** @param type 類型,1-機構,2-快遞員* @param longitude 經度* @param latitude 緯度* @return 服務范圍數據列表*/@ApiImplicitParams({@ApiImplicitParam(name = "type", value = "類型,1-機構,2-快遞員", dataTypeClass = Integer.class),@ApiImplicitParam(name = "longitude", value = "經度", dataTypeClass = Double.class),@ApiImplicitParam(name = "latitude", value = "緯度", dataTypeClass = Double.class)})@ApiOperation(value = "位置查詢服務范圍", notes = "位置查詢服務范圍")@GetMapping("location")public ResponseEntity<List<ServiceScopeDTO>> queryListByAddress(@NotNull(message = "type不能為空") @RequestParam("type") Integer type,@NotNull(message = "longitude不能為空") @RequestParam("longitude") Double longitude,@NotNull(message = "latitude不能為空") @RequestParam("latitude") Double latitude) {List<ServiceScopeEntity> serviceScopeEntityList = this.scopeService.queryListByPoint(ServiceTypeEnum.codeOf(type), new GeoJsonPoint(longitude, latitude));return ResponseEntityUtils.ok(EntityUtils.toDTOList(serviceScopeEntityList));}
}
實現接口
@Slf4j
@Service
public class ScopeServiceImpl implements ScopeService {@Resourceprivate MongoTemplate mongoTemplate;@Resourceprivate EagleMapTemplate eagleMapTemplate;@Overridepublic Boolean saveOrUpdate(Long bid, ServiceTypeEnum type, GeoJsonPolygon polygon) {Query query = Query.query(Criteria.where("bid").is(bid).and("type").is(type.getCode())); //構造查詢條件ServiceScopeEntity serviceScopeEntity = this.mongoTemplate.findOne(query, ServiceScopeEntity.class);if (ObjectUtil.isEmpty(serviceScopeEntity)) {//新增serviceScopeEntity = new ServiceScopeEntity();serviceScopeEntity.setBid(bid);serviceScopeEntity.setType(type.getCode());serviceScopeEntity.setPolygon(polygon);serviceScopeEntity.setCreated(System.currentTimeMillis());serviceScopeEntity.setUpdated(serviceScopeEntity.getCreated());} else {//更新serviceScopeEntity.setPolygon(polygon);serviceScopeEntity.setUpdated(System.currentTimeMillis());}try {this.mongoTemplate.save(serviceScopeEntity);return true;} catch (Exception e) {log.error("新增/更新服務范圍數據失敗! bid = {}, type = {}, points = {}", bid, type, polygon.getPoints(), e);}return false;}@Overridepublic Boolean delete(String id) {Query query = Query.query(Criteria.where("id").is(new ObjectId(id))); //構造查詢條件return this.mongoTemplate.remove(query, ServiceScopeEntity.class).getDeletedCount() > 0;}@Overridepublic Boolean delete(Long bid, ServiceTypeEnum type) {Query query = Query.query(Criteria.where("bid").is(bid).and("type").is(type.getCode())); //構造查詢條件return this.mongoTemplate.remove(query, ServiceScopeEntity.class).getDeletedCount() > 0;}@Overridepublic ServiceScopeEntity queryById(String id) {return this.mongoTemplate.findById(new ObjectId(id), ServiceScopeEntity.class);}@Overridepublic ServiceScopeEntity queryByBidAndType(Long bid, ServiceTypeEnum type) {Query query = Query.query(Criteria.where("bid").is(bid).and("type").is(type.getCode())); //構造查詢條件return this.mongoTemplate.findOne(query, ServiceScopeEntity.class);}@Overridepublic List<ServiceScopeEntity> queryListByPoint(ServiceTypeEnum type, GeoJsonPoint point) {Query query = Query.query(Criteria.where("polygon").intersects(point).and("type").is(type.getCode()));return this.mongoTemplate.find(query, ServiceScopeEntity.class);}@Overridepublic List<ServiceScopeEntity> queryListByPoint(ServiceTypeEnum type, String address) {//根據詳細地址查詢坐標GeoResult geoResult = this.eagleMapTemplate.opsForBase().geoCode(ProviderEnum.AMAP, address, null);Coordinate coordinate = geoResult.getLocation();return this.queryListByPoint(type, new GeoJsonPoint(coordinate.getLongitude(), coordinate.getLatitude()));}
}
測試
package com.sl.ms.scope.service;import com.sl.ms.scope.entity.ServiceScopeEntity;
import com.sl.ms.scope.enums.ServiceTypeEnum;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.geo.Point;
import org.springframework.data.mongodb.core.geo.GeoJsonPoint;
import org.springframework.data.mongodb.core.geo.GeoJsonPolygon;import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;@SpringBootTest
public class ScopeServiceTest {@Resourceprivate ScopeService scopeService;@Testvoid saveOrUpdate() {List<Point> pointList = Arrays.asList(new Point(116.340064,40.061245),new Point(116.347081,40.061836),new Point(116.34751,40.05842),new Point(116.342446,40.058092),new Point(116.340064,40.061245));Boolean result = this.scopeService.saveOrUpdate(2L, ServiceTypeEnum.ORGAN, new GeoJsonPolygon(pointList));System.out.println(result);}@Testvoid testQueryListByPoint() {GeoJsonPoint point = new GeoJsonPoint(116.344828,40.05911);List<ServiceScopeEntity> serviceScopeEntities = this.scopeService.queryListByPoint(ServiceTypeEnum.ORGAN, point);serviceScopeEntities.forEach(serviceScopeEntity -> System.out.println(serviceScopeEntity));}@Testvoid testQueryListByPoint2() {String address = "北京市昌平區金燕龍辦公樓";List<ServiceScopeEntity> serviceScopeEntities = this.scopeService.queryListByPoint(ServiceTypeEnum.ORGAN, address);serviceScopeEntities.forEach(serviceScopeEntity -> System.out.println(serviceScopeEntity));}
}