Commit 71d2b2796c5337228d77955e98361ba1c768a15a
1 parent
fd237e9c
day09-智能调度之作业范围
Showing
1 changed file
with
148 additions
and
0 deletions
sl-express-ms-service-scope-service/src/main/java/com/sl/ms/scope/service/impl/ScopeServiceImpl.java
0 → 100644
| 1 | +package com.sl.ms.scope.service.impl; | |
| 2 | + | |
| 3 | +import cn.hutool.core.util.ObjectUtil; | |
| 4 | +import com.itheima.em.sdk.EagleMapTemplate; | |
| 5 | +import com.itheima.em.sdk.enums.ProviderEnum; | |
| 6 | +import com.itheima.em.sdk.vo.Coordinate; | |
| 7 | +import com.itheima.em.sdk.vo.GeoResult; | |
| 8 | +import com.sl.ms.scope.entity.ServiceScopeEntity; | |
| 9 | +import com.sl.ms.scope.enums.ServiceTypeEnum; | |
| 10 | +import com.sl.ms.scope.service.ScopeService; | |
| 11 | +import lombok.extern.slf4j.Slf4j; | |
| 12 | +import org.bson.types.ObjectId; | |
| 13 | +import org.springframework.beans.factory.annotation.Autowired; | |
| 14 | +import org.springframework.data.mongodb.core.MongoTemplate; | |
| 15 | +import org.springframework.data.mongodb.core.geo.GeoJsonPoint; | |
| 16 | +import org.springframework.data.mongodb.core.geo.GeoJsonPolygon; | |
| 17 | +import org.springframework.data.mongodb.core.query.Criteria; | |
| 18 | +import org.springframework.data.mongodb.core.query.Query; | |
| 19 | +import org.springframework.stereotype.Service; | |
| 20 | + | |
| 21 | +import java.util.List; | |
| 22 | + | |
| 23 | +@Slf4j | |
| 24 | +@Service | |
| 25 | +public class ScopeServiceImpl implements ScopeService { | |
| 26 | + | |
| 27 | + @Autowired | |
| 28 | + private MongoTemplate mongoTemplate; | |
| 29 | + | |
| 30 | + @Autowired | |
| 31 | + private EagleMapTemplate eagleMapTemplate; | |
| 32 | + | |
| 33 | + /** | |
| 34 | + * 新增或更新服务范围 | |
| 35 | + * | |
| 36 | + * @param bid 业务id | |
| 37 | + * @param type 类型 | |
| 38 | + * @param polygon 多边形坐标点 | |
| 39 | + * @return 是否成功 | |
| 40 | + */ | |
| 41 | + @Override | |
| 42 | + public Boolean saveOrUpdate(Long bid, ServiceTypeEnum type, GeoJsonPolygon polygon) { | |
| 43 | +// 1.构建Mogo查询条件 bid type | |
| 44 | + Query query = Query.query(Criteria.where("bid").is(bid).and("type").is(type.getCode())); | |
| 45 | + ServiceScopeEntity serviceScopeEntity = mongoTemplate.findOne(query, ServiceScopeEntity.class); | |
| 46 | + | |
| 47 | +// 2.根据查询条件查询对于作业范围 | |
| 48 | + if (ObjectUtil.isEmpty(serviceScopeEntity)) { | |
| 49 | + serviceScopeEntity = new ServiceScopeEntity(); | |
| 50 | + serviceScopeEntity.setId(new ObjectId()); | |
| 51 | + serviceScopeEntity.setBid(bid); | |
| 52 | + serviceScopeEntity.setType(type.getCode()); | |
| 53 | + serviceScopeEntity.setPolygon(polygon); | |
| 54 | + serviceScopeEntity.setCreated(System.currentTimeMillis()); | |
| 55 | + serviceScopeEntity.setUpdated(System.currentTimeMillis()); | |
| 56 | + } else { | |
| 57 | + serviceScopeEntity.setPolygon(polygon); | |
| 58 | + serviceScopeEntity.setUpdated(System.currentTimeMillis()); | |
| 59 | + } | |
| 60 | + | |
| 61 | +// 3.如果作业范围为空==》新建作业范围保存 | |
| 62 | +// 4.如果作业范围存在 ==> 修改多边形字段 和 updated时间 | |
| 63 | + try { | |
| 64 | + mongoTemplate.save(serviceScopeEntity); | |
| 65 | + return true; | |
| 66 | + } catch (Exception e) { | |
| 67 | + log.error("新增/更新服务范围数据失败! bid = {}, type = {}, points = {}", bid, type, polygon.getPoints(), e); | |
| 68 | + } | |
| 69 | + return false; | |
| 70 | + } | |
| 71 | + | |
| 72 | + /** | |
| 73 | + * 根据主键id删除数据 | |
| 74 | + * | |
| 75 | + * @param id 主键 | |
| 76 | + * @return 是否成功 | |
| 77 | + */ | |
| 78 | + @Override | |
| 79 | + public Boolean delete(String id) { | |
| 80 | + Query query = Query.query(Criteria.where("id").is(new ObjectId(id))); | |
| 81 | + return mongoTemplate.remove(query, ServiceScopeEntity.class).getDeletedCount() > 0; | |
| 82 | + } | |
| 83 | + | |
| 84 | + /** | |
| 85 | + * 根据业务id和类型删除数据 | |
| 86 | + * | |
| 87 | + * @param bid 业务id | |
| 88 | + * @param type 类型 | |
| 89 | + * @return 是否成功 | |
| 90 | + */ | |
| 91 | + @Override | |
| 92 | + public Boolean delete(Long bid, ServiceTypeEnum type) { | |
| 93 | + Query query = Query.query(Criteria.where("bid").is(bid).and("type").is(type.getCode())); | |
| 94 | + return mongoTemplate.remove(query, ServiceScopeEntity.class).getDeletedCount()>0; | |
| 95 | + } | |
| 96 | + | |
| 97 | + /** | |
| 98 | + * 根据主键查询数据 | |
| 99 | + * | |
| 100 | + * @param id 主键 | |
| 101 | + * @return 服务范围数据 | |
| 102 | + */ | |
| 103 | + @Override | |
| 104 | + public ServiceScopeEntity queryById(String id) { | |
| 105 | + return mongoTemplate.findById(new ObjectId(id), ServiceScopeEntity.class); | |
| 106 | + } | |
| 107 | + | |
| 108 | + /** | |
| 109 | + * 根据业务id和类型查询数据 | |
| 110 | + * | |
| 111 | + * @param bid 业务id | |
| 112 | + * @param type 类型 | |
| 113 | + * @return 服务范围数据 | |
| 114 | + */ | |
| 115 | + @Override | |
| 116 | + public ServiceScopeEntity queryByBidAndType(Long bid, ServiceTypeEnum type) { | |
| 117 | + Query query = Query.query(Criteria.where("bid").is(bid).and("type").is(type.getCode())); | |
| 118 | + return mongoTemplate.findOne(query, ServiceScopeEntity.class); | |
| 119 | + } | |
| 120 | + | |
| 121 | + /** | |
| 122 | + * 根据坐标点查询所属的服务对象 | |
| 123 | + * | |
| 124 | + * @param type 类型 | |
| 125 | + * @param point 坐标点 | |
| 126 | + * @return 服务范围数据 | |
| 127 | + */ | |
| 128 | + @Override | |
| 129 | + public List<ServiceScopeEntity> queryListByPoint(ServiceTypeEnum type, GeoJsonPoint point) { | |
| 130 | +// 根据类型和坐标点查询有交集的作业范围 tips:intersects 查询传入坐标点和mongo库中多边形相交的数据 | |
| 131 | + Query query = Query.query(Criteria.where("type").is(type.getCode()).and("polygon").intersects(point)); | |
| 132 | + return mongoTemplate.find(query, ServiceScopeEntity.class); | |
| 133 | + } | |
| 134 | + | |
| 135 | + /** | |
| 136 | + * 根据详细地址查询所属的服务对象 | |
| 137 | + * | |
| 138 | + * @param type 类型 | |
| 139 | + * @param address 详细地址,如:北京市昌平区金燕龙办公楼传智教育总部 | |
| 140 | + * @return 服务范围数据 | |
| 141 | + */ | |
| 142 | + @Override | |
| 143 | + public List<ServiceScopeEntity> queryListByPoint(ServiceTypeEnum type, String address) { | |
| 144 | + GeoResult geoResult = eagleMapTemplate.opsForBase().geoCode(ProviderEnum.AMAP, address, null); | |
| 145 | + Coordinate coordinate = geoResult.getLocation(); | |
| 146 | + return queryListByPoint(type, new GeoJsonPoint(coordinate.getLongitude(), coordinate.getLatitude())); | |
| 147 | + } | |
| 148 | +} | ... | ... |