rain
2024-07-30 c56112796e4b0a642cf187a2fa16ee5a271383b0
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package com.dji.sample.map.service.impl;
 
import com.dji.sample.common.model.ResponseResult;
import com.dji.sample.map.model.dto.ElementCreateDTO;
import com.dji.sample.map.model.dto.ElementUpdateDTO;
import com.dji.sample.map.model.dto.GroupDTO;
import com.dji.sample.map.model.dto.GroupElementDTO;
import com.dji.sample.map.service.IElementCoordinateService;
import com.dji.sample.map.service.IGroupElementService;
import com.dji.sample.map.service.IGroupService;
import com.dji.sample.map.service.IWorkspaceElementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.util.List;
import java.util.Optional;
 
/**
 * @author sean
 * @version 0.2
 * @date 2021/11/30
 */
@Transactional
@Service
public class WorkspaceElementServiceImpl implements IWorkspaceElementService {
 
    @Autowired
    private IGroupService groupService;
 
    @Autowired
    private IGroupElementService groupElementService;
 
    @Autowired
    private IElementCoordinateService elementCoordinateService;
 
    @Override
    public List<GroupDTO> getAllGroupsByWorkspaceId(String workspaceId, String groupId, Boolean isDistributed) {
        List<GroupDTO> groupList = groupService.getAllGroupsByWorkspaceId(workspaceId, groupId, isDistributed);
        groupList.forEach(group -> group.setElements(
                groupElementService.getElementsByGroupId(group.getId())
        ));
        return groupList;
    }
 
    @Override
    public ResponseResult saveElement(String groupId, ElementCreateDTO elementCreate) {
        boolean saveElement = groupElementService.saveElement(groupId, elementCreate);
        if (!saveElement) {
            return ResponseResult.error("保存元素失败");
        }
 
        // save coordinate
        boolean saveCoordinate = elementCoordinateService.saveCoordinate(
                        elementCreate.getResource().getContent().getGeometry().convertToList(), elementCreate.getId());
 
        return saveCoordinate ?
                ResponseResult.success() : ResponseResult.error("保存坐标失败");
    }
 
 
    @Override
    public ResponseResult updateElement(String elementId, ElementUpdateDTO elementUpdate, String username) {
        boolean updElement = groupElementService.updateElement(elementId, elementUpdate, username);
        if (!updElement) {
            return ResponseResult.error("更新元素失败");
        }
 
        // delete all coordinates according to element id.
        boolean delCoordinate = elementCoordinateService.deleteCoordinateByElementId(elementId);
        // save coordinate
        boolean saveCoordinate = elementCoordinateService.saveCoordinate(
                elementUpdate.getContent().getGeometry().convertToList(), elementId);
 
        return delCoordinate && saveCoordinate ?
                ResponseResult.success() : ResponseResult.error("更新坐标失败");
    }
 
    @Override
    public ResponseResult deleteElement(String elementId) {
        boolean delElement = groupElementService.deleteElement(elementId);
        if (!delElement) {
            return ResponseResult.error("删除元素失败");
        }
 
        // delete all coordinates according to element id.
        boolean delCoordinate = elementCoordinateService.deleteCoordinateByElementId(elementId);
 
        return delCoordinate ?
                ResponseResult.success() : ResponseResult.error("删除坐标失败");
    }
 
    @Override
    public Optional<GroupElementDTO> getElementByElementId(String elementId) {
        return groupElementService.getElementByElementId(elementId);
    }
 
    @Override
    public ResponseResult deleteAllElementByGroupId(String groupId) {
        List<GroupElementDTO> groupElementList = groupElementService.getElementsByGroupId(groupId);
        for (GroupElementDTO groupElement : groupElementList) {
            ResponseResult response = this.deleteElement(groupElement.getElementId());
            if (ResponseResult.CODE_SUCCESS != response.getCode()) {
                return response;
            }
        }
        return ResponseResult.success();
    }
}