当前位置: 首页 > news >正文

《神领物流》day08-作业范围微服务_完整代码【简单易懂注释版】

目录

四十七、Spring Data MongoDB

1.interface PersonService

2.PersonServiceImpl.java

3.PersonServiceTest.java

四十八、作业范围功能实现

1.ScopeController.java

2.interface ScopeService

3.ScopeServiceImpl.java

4.ScopeServiceTest.java


四十七、Spring Data MongoDB

这是一个练习MongoDB的demo

1.interface PersonService

package com.sl.mongo.service;public interface PersonService {/*** 新增数据** @param person 数据*/void savePerson(Person person);/*** 更新数据** @param person 数据*/void update(Person person);/*** 根据名字查询用户列表** @param name 用户名字* @return 用户列表*/List<Person> queryPersonListByName(String name);/*** 分页查询用户列表** @param page     页数* @param pageSize 页面大小* @return 用户列表*/List<Person> queryPersonPageList(int page, int pageSize);/*** 根据id删除用户** @param id 主键*/void deleteById(String id);
}

2.PersonServiceImpl.java

package com.sl.mongo.service.impl;@Service
public class PersonServiceImpl implements PersonService {@Resourceprivate MongoTemplate mongoTemplate;/*** 新增数据** @param person 数据*/@Overridepublic void savePerson(Person person) {// 保存用户信息到MongoDB数据库中this.mongoTemplate.save(person);}/*** 更新数据** @param person 数据*/@Overridepublic void update(Person person) {// 1.构造查询条件,根据用户ID进行查询Query query = Query.query(Criteria.where("id").is(person.getId()));// 2.构造更新内容,设置需要更新的字段Update update = Update.update("name", person.getName()).set("age", person.getAge()).set("location", person.getLocation()).set("address", person.getAddress());// 3.执行更新操作,更新匹配查询条件的所有记录this.mongoTemplate.updateMulti(query, update, Person.class);}/*** 根据名字查询用户列表** @param name 用户名字* @return 用户列表*/@Overridepublic List<Person> queryPersonListByName(String name) {// 构造查询条件,根据用户姓名进行精确匹配查询Query query = Query.query(Criteria.where("name").is(name));// 执行查询操作,返回符合条件的用户列表return this.mongoTemplate.find(query, Person.class);}/*** 分页查询用户列表** @param page     页数* @param pageSize 页面大小* @return 用户列表*/@Overridepublic List<Person> queryPersonPageList(int page, int pageSize) {// 构造分页请求对象,页码从0开始,按年龄降序排序PageRequest pageRequest = PageRequest.of(page - 1, pageSize, Sort.by(Sort.Order.desc("age")));// 创建查询对象并应用分页条件Query query = new Query().with(pageRequest);// 执行分页查询,返回当前页的用户列表return this.mongoTemplate.find(query, Person.class);}/*** 根据id删除用户** @param id 主键*/@Overridepublic void deleteById(String id) {// 构造查询条件,根据用户ID进行精确匹配查询Query query = Query.query(Criteria.where("id").is(new ObjectId(id)));// 执行删除操作,删除匹配查询条件的记录DeleteResult result = this.mongoTemplate.remove(query, Person.class);System.out.println(result);}
}

3.PersonServiceTest.java

package com.sl.mongo.service;@SpringBootTest
class PersonServiceTest {@ResourcePersonService personService;// 测试保存用户数据的功能@Testvoid savePerson() {for (int i = 1; i <= 20; i++) {Person person = Person.builder().id(ObjectId.get()).name("张三_" + i).age(20 + i).location(new GeoJsonPoint(116.343847, 40.060539)).address(new Address("人民路", "上海市", "666666")).build();this.personService.savePerson(person);}}// 测试更新用户数据的功能@Testvoid update() {Person person = Person.builder().id(new ObjectId("68c37a48ba7eb3231f93756a"))  //id要对应表的id.name("张三").age(22) //修改数据.location(new GeoJsonPoint(116.343847, 40.060539)).address(new Address("人民路", "上海市", "666666")).build();this.personService.update(person);}// 测试根据用户名查询用户列表的功能@Testvoid queryPersonListByName() {List<Person> personList = this.personService.queryPersonListByName("张三");personList.forEach(System.out::println);}// 测试分页查询用户列表的功能@Testvoid queryPersonPageList() {List<Person> personList = this.personService.queryPersonPageList(1, 10);personList.forEach(System.out::println);System.out.println("-----------------------------");personList = this.personService.queryPersonPageList(2, 10);personList.forEach(System.out::println);}// 测试根据id删除用户的功能@Testvoid deleteById() {this.personService.deleteById("68c37cb3008ed50660522b2f");}}

四十八、作业范围功能实现

项目用的经纬度坐标体系是这个

1.ScopeController.java

package com.sl.ms.scope.controller;/*** 服务范围*/
@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));}
}

2.interface ScopeService

package com.sl.ms.scope.service;/*** 服务范围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);
}

3.ScopeServiceImpl.java

package com.sl.ms.scope.service.impl;@Service
public class ScopeServiceImpl implements ScopeService {@Resourceprivate MongoTemplate mongoTemplate;@Resourceprivate EagleMapTemplate eagleMapTemplate;/*** 新增或更新服务范围** @param bid     业务id* @param type    类型* @param polygon 多边形坐标点* @return 是否成功*/@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.setUpdated(System.currentTimeMillis());serviceScopeEntity.setPolygon(polygon);  //作业范围多边形坐标}// 新增操作ServiceScopeEntity saveServiceScopeEntity = this.mongoTemplate.save(serviceScopeEntity);return ObjectUtil.isNotEmpty(saveServiceScopeEntity);}/*** 根据主键id删除数据** @param id 主键* @return 是否成功*/@Overridepublic Boolean delete(String id) {// 构造查询条件,根据主键id删除数据Query query = Query.query(Criteria.where("id").is(new ObjectId(id)));// 执行删除操作DeleteResult deleteResult = this.mongoTemplate.remove(query, ServiceScopeEntity.class);// 返回删除结果,大于0表示删除成功return deleteResult.getDeletedCount() > 0;}/*** 根据业务id和类型删除数据** @param bid  业务id* @param type 类型* @return 是否成功*/@Overridepublic Boolean delete(Long bid, ServiceTypeEnum type) {// 构造查询条件,根据业务id和类型删除数据Query query = Query.query(Criteria.where("bid").is(bid).and("type").is(type.getCode()));// 执行删除操作DeleteResult deleteResult = this.mongoTemplate.remove(query, ServiceScopeEntity.class);// 返回删除结果,大于0表示删除成功return deleteResult.getDeletedCount() > 0;}/*** 根据主键查询数据** @param id 主键* @return 服务范围数据*/@Overridepublic ServiceScopeEntity queryById(String id) {// 根据主键id查询服务范围数据return this.mongoTemplate.findById(new ObjectId(id), ServiceScopeEntity.class);}/*** 根据业务id和类型查询数据** @param bid  业务id* @param type 类型* @return 服务范围数据*/@Overridepublic ServiceScopeEntity queryByBidAndType(Long bid, ServiceTypeEnum type) {// 构造查询条件,根据业务id和类型查询数据Query query = Query.query(Criteria.where("bid").is(bid).and("type").is(type.getCode()));// 执行查询并返回结果return this.mongoTemplate.findOne(query, ServiceScopeEntity.class);}/*** 根据坐标点查询所属的服务对象** @param type  类型* @param point 坐标点* @return 服务范围数据*/@Overridepublic List<ServiceScopeEntity> queryListByPoint(ServiceTypeEnum type, GeoJsonPoint point) {// 构造查询条件,根据服务类型和坐标点查找相交的多边形范围Query query = Query.query(Criteria.where("type").is(type.getCode()).and("polygon").intersects(point));// 执行查询并返回结果列表return this.mongoTemplate.find(query, ServiceScopeEntity.class);}/*** 根据详细地址查询所属的服务对象** @param type    类型* @param address 详细地址,如:北京市昌平区金燕龙办公楼传智教育总部* @return 服务范围数据*/@Overridepublic List<ServiceScopeEntity> queryListByPoint(ServiceTypeEnum type, String address) {// 将中文地址转换为经纬度坐标点(调用EagleMap)GeoResult geoResult = this.eagleMapTemplate.opsForBase().geoCode(address);Coordinate location = geoResult.getLocation();GeoJsonPoint point = new GeoJsonPoint(location.getLongitude(),location.getLatitude());return this.queryListByPoint(type,point);}}

4.ScopeServiceTest.java

这是测试类

package com.sl.ms.scope.service;@SpringBootTest
public class ScopeServiceTest {@Resourceprivate ScopeService scopeService;@Testvoid saveOrUpdate() {List<Point> pointList = Arrays.asList(new Point(121.595289,31.040958),new Point(121.628591,31.041987),new Point(121.628591,31.028161),new Point(121.599066,31.026984),new Point(121.595289,31.040958));Boolean result = this.scopeService.saveOrUpdate(9999L, ServiceTypeEnum.COURIER, new GeoJsonPolygon(pointList));System.out.println(result);}@Testvoid delete() {}@Testvoid testDelete() {}@Testvoid queryById() {}@Testvoid queryByBidAndType() {}@Testvoid queryListByPoint() {}@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 = "上海市浦东新区航都路18号";List<ServiceScopeEntity> serviceScopeEntities = this.scopeService.queryListByPoint(ServiceTypeEnum.ORGAN, address);serviceScopeEntities.forEach(serviceScopeEntity -> System.out.println(serviceScopeEntity));System.out.println("-----------------------------------");serviceScopeEntities = this.scopeService.queryListByPoint(ServiceTypeEnum.COURIER, address);serviceScopeEntities.forEach(serviceScopeEntity -> System.out.println(serviceScopeEntity));}}
http://www.dtcms.com/a/532444.html

相关文章:

  • 西安网站定制开发h5课件制作软件
  • ABAP 调用接口传输文件(multipart form-data)
  • 联邦学习:现状与展望!
  • Linux IPC 为什么要这么架构
  • 实验室PRCV 2025论文分享|如何利用大模型自动生成高质量英语阅读理解练习题
  • C 转 C++:高效上手的核心容器与函数指南
  • 网站建设人员岗位设置网站建设职业兴趣要求
  • MyBatis操作数据库入门
  • 美团网站开发北京网站建设外包公司
  • [nanoGPT] 编排训练 | `get_batch` | AdamW | `get_lr` | 分布式训练(DDP)
  • 2.2.1.1 大数据方法论与实践指南-公司产品功能命名管理
  • Spring Boot3零基础教程,@SpringBootApplication 注解详细说明,笔记63
  • Flutter 响应式 + Clean Architecture / MVU 模式 实战指南
  • 免费注册二级域名的网站网站制作哪些公司好
  • 【Go】--time包的使用
  • VR 工业组装案例
  • 网络运维管理
  • 使用STM32H723VGT6芯片驱动达妙电机
  • 【计算机通识】进程、线程、协程对比讲解--特点、优点、缺点
  • 专业做俄语网站建设上海建设公司注册
  • 南京营销型网站制作建设一个网站需要什么手续
  • POPAI全球启动仪式成功举办|AI×Web3全球算力革命启航
  • PCB笔记
  • C++ 类的学习(六) 虚函数
  • leetcode 2043 简易银行系统
  • 网站插件代码怎么用哪个网站上做自媒体最好
  • 【LeetCode100】--- 97.多数元素【思维导图+复习回顾】
  • Wasserstein 距离简介
  • 南宁网站建设外包vs做的网站如何
  • 【C++】前缀和算法习题