#
luxiaotao1123
2025-01-08 36dcec12994c82fffe2a86a6acf12ecbd071fffb
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
package com.zy.acs.manager.manager.service.impl;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zy.acs.common.enums.AgvStatusType;
import com.zy.acs.framework.common.Cools;
import com.zy.acs.manager.common.utils.CommonUtil;
import com.zy.acs.manager.manager.entity.*;
import com.zy.acs.manager.manager.enums.FuncStaType;
import com.zy.acs.manager.manager.enums.StatusType;
import com.zy.acs.manager.manager.enums.TaskStsType;
import com.zy.acs.manager.manager.enums.TaskTypeType;
import com.zy.acs.manager.manager.mapper.FuncStaMapper;
import com.zy.acs.manager.manager.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import java.util.*;
import java.util.stream.Collectors;
 
@Service("funcStaService")
public class FuncStaServiceImpl extends ServiceImpl<FuncStaMapper, FuncSta> implements FuncStaService {
 
    @Autowired
    private CodeService codeService;
    @Autowired
    private AgvService agvService;
    @Autowired
    private AgvDetailService agvDetailService;
    @Autowired
    private AgvModelService agvModelService;
    @Autowired
    private TaskService taskService;
 
    @Override
    public FuncSta getByCodeAndType(Long codeId, String type) {
        List<FuncSta> list = this.list(new LambdaQueryWrapper<FuncSta>().eq(FuncSta::getCode, codeId).eq(FuncSta::getType, type));
        if (Cools.isEmpty(list)) {
            return null;
        }
        return list.stream().findFirst().orElse(null);
    }
 
    @Override
    public FuncSta query(Long codeId, String type) {
        List<FuncSta> list = this.list(new LambdaQueryWrapper<FuncSta>()
                .eq(FuncSta::getCode, codeId)
                .eq(FuncSta::getType, type)
        );
        return list.stream().findFirst().orElse(null);
    }
 
    @Override
    public List<FuncSta> findInIdleStatus(FuncStaType type, Long agvId) {
        LambdaQueryWrapper<FuncSta> wrapper = new LambdaQueryWrapper<FuncSta>()
                .eq(FuncSta::getType, type).eq(FuncSta::getStatus, StatusType.ENABLE.val);
        List<FuncSta> funcStaList = this.list(wrapper);
 
        if (Cools.isEmpty(funcStaList)) {
            return new ArrayList<>();
        }
 
        Collections.shuffle(funcStaList);
        // filter idle
        funcStaList = funcStaList.stream().filter(funcSta -> {
 
            Long code = funcSta.getCode();
            if (null == code) {
                return false;
            }
 
            Agv agv = agvService.findByPosition(code);
 
            if (funcSta.getType().equals(FuncStaType.CHARGE.toString())) {
                // if the type of this funSta is charge and the existing agv is in charge status, then that means this funSta is occupied
                if (null != agv && !agv.getId().equals(agvId)) {
                    AgvModel agvModel = agvModelService.getByAgvId(agv.getId());
                    AgvDetail agvDetail = agvDetailService.selectByAgvId(agv.getId());
                    if (agvDetail.getAgvStatus().equals(AgvStatusType.CHARGE)) {
                        if (agvDetail.getVol() < agvModel.getQuaBattery()) {
                            return false;
                        }
                    } else {
                        Task latestTask = taskService.findLatestTask(agv.getId(), null);
                        if (null != latestTask
                                && latestTask.getTaskType().equals(TaskTypeType.TO_CHARGE.val())
                                && latestTask.getDestCode().equals(funcSta.getCode())
                        ) {
                            // avoid the agv already be full battery but there was no task assign to it, so that not in charge status and battery had down
                            if (agvDetailService.isPowerLoss(agv, agvDetail, agvModel)) {
                                return false;
                            }
                        }
                    }
                }
 
                // if there is a running task whose destination is this funSta, then that means this funSta is occupied
                if (0 < taskService.count(new LambdaQueryWrapper<Task>()
                        .in(Task::getTaskType, TaskTypeType.TO_STANDBY.val(), TaskTypeType.TO_CHARGE.val())
                        .in(Task::getTaskSts, TaskStsType.ASSIGN.val(), TaskStsType.PROGRESS.val())
                        .eq(Task::getDestCode, code)
                )) {
                    return false;
                }
 
            }
 
            if (funcSta.getType().equals(FuncStaType.STANDBY.toString())) {
 
                if (null == agv) {
                    // if there is a running task whose destination is this funSta, then that means this funSta is occupied
                    if (0 < taskService.count(new LambdaQueryWrapper<Task>()
                            .in(Task::getTaskType
                                    , TaskTypeType.TO_STANDBY.val()
                                    , TaskTypeType.TO_CHARGE.val()
                                    , TaskTypeType.MOVE.val()
                            )
                            .in(Task::getTaskSts, TaskStsType.ASSIGN.val(), TaskStsType.PROGRESS.val())
                            .eq(Task::getDestCode, code)
                    )) {
                        return false;
                    }
 
                } else {
                    if (!agv.getId().equals(agvId)) {
                        // if there is an agv on the code of this funSta, should we let this agv leave?
                        // we need to judge whether the agv went to this funSta based on a task which in GO_STANDBY type
 
//                    Task latestTaskByAgv = taskService.findLatestTask(existAgv.getId());
                        return false;
                    }
 
                }
            }
 
            return true;
        }).collect(Collectors.toList());
 
        return funcStaList;
    }
 
    @Override
    public FuncSta checkoutClosestFunSta(Long codeId, List<FuncSta> funcStaList) {
        if (Cools.isEmpty(funcStaList)) {
            return null;
        }
 
        if (null != codeId) {
            Code currCode = codeService.getById(codeId);
            Double[] startPos = new Double[]{currCode.getX(), currCode.getY()};
 
            // checkout one funSta which is the closest
 
            // compare => compare返回负数,则排在集合前面 (asc)
            funcStaList.sort(new Comparator<FuncSta>() {
                @Override
                public int compare(FuncSta o1, FuncSta o2) {
                    Code o1Code = codeService.getById(o1.getCode());
                    int o1Distance = CommonUtil.calcDistance(startPos, new Double[]{o1Code.getX(), o1Code.getY()});
 
                    Code o2Code = codeService.getById(o2.getCode());
                    int o2Distance = CommonUtil.calcDistance(startPos, new Double[]{o2Code.getX(), o2Code.getY()});
 
                    return o1Distance - o2Distance;
                }
            });
 
        }
 
        return funcStaList.stream().findFirst().orElse(null);
    }
 
    @Override
    public Boolean isCanBeIdle(FuncSta funcSta) {
        Agv agv;
        switch (Objects.requireNonNull(FuncStaType.query(funcSta.getType()))) {
            case CHARGE:
                if (0 < taskService.count(new LambdaQueryWrapper<Task>()
                        .eq(Task::getTaskType, TaskTypeType.TO_CHARGE.val())
                        .eq(Task::getDestCode, funcSta.getCode())
                        .and(i -> {
                            i.eq(Task::getTaskSts, TaskStsType.WAITING.val()).or()
                                    .eq(Task::getTaskSts, TaskStsType.ASSIGN.val()).or()
                                    .eq(Task::getTaskSts, TaskStsType.PROGRESS.val());
                        })
                )) {
                    return false;
                }
 
                agv = agvService.findByPosition(funcSta.getCode());
                if (null != agv) {
                    AgvModel agvModel = agvModelService.getByAgvId(agv.getId());
                    AgvDetail agvDetail = agvDetailService.selectByAgvId(agv.getId());
                    if (agvDetail.getAgvStatus().equals(AgvStatusType.CHARGE)) {
                        return false;
                    } else {
                        Task latestTask = taskService.findLatestTask(agv.getId(), null);
                        if (null != latestTask
                                && latestTask.getTaskType().equals(TaskTypeType.TO_CHARGE.val())
                                && latestTask.getDestCode().equals(funcSta.getCode())
                        ) {
                            // avoid the agv already be full battery but there was no task assign to it, so that not in charge status and battery had down
                            if (agvDetailService.isPowerLoss(agv, agvDetail, agvModel)) {
                                return false;
                            }
                        }
                    }
                }
                break;
            case STANDBY:
                if (0 < taskService.count(new LambdaQueryWrapper<Task>()
                        .eq(Task::getTaskType, TaskTypeType.TO_STANDBY.val())
                        .eq(Task::getDestCode, funcSta.getCode())
                        .and(i -> {
                            i.eq(Task::getTaskSts, TaskStsType.WAITING.val()).or()
                                    .eq(Task::getTaskSts, TaskStsType.ASSIGN.val()).or()
                                    .eq(Task::getTaskSts, TaskStsType.PROGRESS.val());
                        })
                )) {
                    return false;
                }
 
                agv = agvService.findByPosition(funcSta.getCode());
                if (null != agv) {
                    Task latestTask = taskService.findLatestTask(agv.getId(), null);
                    if (null != latestTask
                            && latestTask.getTaskType().equals(TaskTypeType.TO_STANDBY.val())
                            && latestTask.getDestCode().equals(funcSta.getCode())
                    ) {
                        return false;
                    }
                }
                break;
            default:
                break;
        }
        return true;
    }
 
}