EntityUtils.java
2.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package com.sl.ms.scope.util;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.sl.ms.scope.dto.Coordinate;
import com.sl.ms.scope.dto.ServiceScopeDTO;
import com.sl.ms.scope.entity.ServiceScopeEntity;
import org.springframework.data.geo.Point;
import org.springframework.data.mongodb.core.geo.GeoJsonPolygon;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
/**
* 实体工具类
*/
public class EntityUtils {
public static ServiceScopeDTO toDTO(ServiceScopeEntity entity) {
if (ObjectUtil.isEmpty(entity)) {
return null;
}
ServiceScopeDTO serviceScopeDTO = BeanUtil.toBeanIgnoreError(entity, ServiceScopeDTO.class);
//设置范围
List<Coordinate> coordinateList = entity.getPolygon().getPoints()
.stream().map(point -> new Coordinate(point.getX(), point.getY()))
.collect(Collectors.toList());
serviceScopeDTO.setPolygon(coordinateList);
return serviceScopeDTO;
}
public static List<ServiceScopeDTO> toDTOList(List<ServiceScopeEntity> entityList) {
if (CollUtil.isEmpty(entityList)) {
return Collections.emptyList();
}
List<ServiceScopeDTO> list = new ArrayList<>();
entityList.forEach(entity -> list.add(toDTO(entity)));
return list;
}
public static ServiceScopeEntity toEntity(ServiceScopeDTO dto) {
if (ObjectUtil.isEmpty(dto)) {
return null;
}
ServiceScopeEntity serviceScopeEntity = BeanUtil.toBeanIgnoreError(dto, ServiceScopeEntity.class);
List<Point> pointList = dto.getPolygon().stream()
.map(coordinate -> new Point(coordinate.getLongitude(), coordinate.getLatitude()))
.collect(Collectors.toList());
serviceScopeEntity.setPolygon(new GeoJsonPolygon(pointList));
return serviceScopeEntity;
}
public static List<ServiceScopeEntity> toEntity(List<ServiceScopeDTO> dtoList) {
if (CollUtil.isEmpty(dtoList)) {
return Collections.emptyList();
}
List<ServiceScopeEntity> list = new ArrayList<>();
dtoList.forEach(dto -> list.add(toEntity(dto)));
return list;
}
}