#
luxiaotao1123
2024-10-25 e3524343be955ba20d10c6e9fd3f46765e6fb9b3
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
package com.zy.acs.manager.core.service;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zy.acs.framework.common.Cools;
import com.zy.acs.manager.common.utils.LocUtils;
import com.zy.acs.manager.manager.entity.Agv;
import com.zy.acs.manager.manager.entity.AgvModel;
import com.zy.acs.manager.manager.entity.Task;
import com.zy.acs.manager.manager.enums.TaskStsType;
import com.zy.acs.manager.manager.service.AgvModelService;
import com.zy.acs.manager.manager.service.AgvService;
import com.zy.acs.manager.manager.service.TaskService;
import com.zy.acs.manager.system.service.ConfigService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
 
/**
 * Created by vincent on 8/12/2024
 */
@Slf4j
@Service
public class AllocateService {
 
    @Autowired
    private AgvService agvService;
    @Autowired
    private AgvModelService agvModelService;
    @Autowired
    private ConfigService configService;
    @Autowired
    private TaskService taskService;
    @Autowired
    private LaneService laneService;
 
    /**
     * get available agv list which is idle
     */
    private List<Agv> getAvailableAgv() {
        List<Agv> result = new ArrayList<>();
        List<Agv> agvList = agvService.list(new LambdaQueryWrapper<Agv>().eq(Agv::getStatus, 1));
        Collections.shuffle(agvList);
        for (Agv agv : agvList) {
 
            // 1. without running tasks
            if (0 < taskService.count(new LambdaQueryWrapper<Task>()
                    .eq(Task::getAgvId, agv.getId())
                    .and(i ->
                            i.eq(Task::getTaskSts, TaskStsType.ASSIGN.val())
                            .or().eq(Task::getTaskSts, TaskStsType.PROGRESS.val())
                    )
            )) {
                continue;
            }
 
            // 2. in idle status
            if (!agvService.judgeEnable(agv.getId(), true)) {
                continue;
            }
 
            result.add(agv);
        }
 
        return result;
    }
 
    public synchronized Agv execute(Task task) {
        List<Agv> availableAgvList = getAvailableAgv();
        if (Cools.isEmpty(availableAgvList)) {
            return null;
        }
 
 
        return null;
    }
 
    public synchronized Agv execute(Task task, Map<String, List<Long>> taskAllot, List<Long> taskIds) {
        String oriLocNo = task.getOriLoc$();
        int oriLocRow = LocUtils.getRow(oriLocNo);
        String destLocNo = task.getDestLoc$();
        int destLocRow = LocUtils.getRow(destLocNo);
 
        Agv hit = null;
 
        List<Agv> agvList = agvService.list(new LambdaQueryWrapper<Agv>().eq(Agv::getStatus, 1));
        Collections.shuffle(agvList);
        for (Agv agv : agvList) {
            AgvModel agvModel = agvModelService.getById(agv.getAgvModel());
            int allotTaskCount = 0;
            List<Long> allotTaskIds = taskAllot.get(agv.getUuid());
            if (!Cools.isEmpty(allotTaskIds)) {
                allotTaskCount = allotTaskIds.size();
            }
            if (allotTaskCount >= agvModel.getBackpack()) {
                continue;
            }
            if (taskService.count(new LambdaQueryWrapper<Task>()
                    .eq(Task::getAgvId, agv.getId())
                    .notIn(Task::getId, taskIds)
                    .and(i -> {
                        i.eq(Task::getTaskSts, TaskStsType.WAITING.val())   // 已经有waiting任务的车不能再分配
                                .or().eq(Task::getTaskSts, TaskStsType.ASSIGN.val())
                                .or().eq(Task::getTaskSts, TaskStsType.PROGRESS.val());
                    })) > 0) {
                log.info(agv.getUuid() + "号AGV不可用,已经存在进行中的任务...");
                continue;
            }
            if (!agvService.judgeEnable(agv.getId(), true)) {
                log.info(agv.getUuid() + "号AGV不可用," + task.getSeqNum() + "任务无法计算...");
                continue;
            }
 
            hit = agv;
            break;
        }
 
        return hit;
    }
 
    public synchronized Agv execute1(Task task, Map<String, List<Long>> taskAllot, List<Long> taskIds) {
        String oriLocNo = task.getOriLoc$();
        int oriLocRow = LocUtils.getRow(oriLocNo);
        String destLocNo = task.getDestLoc$();
        int destLocRow = LocUtils.getRow(destLocNo);
 
        Agv agv = null;
        if (oriLocRow <= 2 && destLocRow <= 2) {
            agv = agvService.selectByUuid(String.valueOf(1));
        }
        if (oriLocRow > 2 && destLocRow > 2) {
            agv = agvService.selectByUuid(String.valueOf(2));
        }
        assert agv != null;
        AgvModel agvModel = agvModelService.getById(agv.getAgvModel());
        int allotTaskCount = 0;
        List<Long> allotTaskIds = taskAllot.get(agv.getUuid());
        if (!Cools.isEmpty(allotTaskIds)) {
            allotTaskCount = allotTaskIds.size();
        }
        if (allotTaskCount >= agvModel.getBackpack()) {
            return null;
        }
        if (taskService.count(new LambdaQueryWrapper<Task>()
                .eq(Task::getAgvId, agv.getId())
                .notIn(Task::getId, taskIds)
                .and(i -> {
                    i.eq(Task::getTaskSts, TaskStsType.WAITING.val())   // 已经有waiting任务的车不能再分配
                            .or().eq(Task::getTaskSts, TaskStsType.ASSIGN.val())
                            .or().eq(Task::getTaskSts, TaskStsType.PROGRESS.val());
                })) > 0) {
            log.info(agv.getUuid() + "号AGV不可用,已经存在进行中的任务...");
            return null;
        }
        if (!agvService.judgeEnable(agv.getId(), true)) {
            log.info(agv.getUuid() + "号AGV不可用," + task.getSeqNum() + "任务无法计算...");
            return null;
        }
 
        return agv;
    }
 
}