From bfac8bfa522289b8d2c0b66c50c4bf8b8d75c0e1 Mon Sep 17 00:00:00 2001
From: luxiaotao1123 <t1341870251@163.com>
Date: 星期二, 29 十月 2024 14:27:02 +0800
Subject: [PATCH] #

---
 zy-acs-manager/src/main/java/com/zy/acs/manager/manager/controller/DigitalController.java    |   11 -
 zy-acs-manager/src/main/java/com/zy/acs/manager/manager/service/TaskService.java             |    2 
 zy-acs-manager/src/main/java/com/zy/acs/manager/core/service/MainService.java                |   22 ++-
 zy-acs-manager/src/main/java/com/zy/acs/manager/manager/service/FuncStaService.java          |    7 +
 zy-acs-manager/src/main/java/com/zy/acs/manager/manager/service/impl/FuncStaServiceImpl.java |  145 ++++++++++++++++++++++++++++
 zy-acs-manager/src/main/java/com/zy/acs/manager/core/scheduler/MaintainScheduler.java        |   32 ++++--
 zy-acs-manager/src/main/java/com/zy/acs/manager/manager/service/impl/TaskServiceImpl.java    |   11 ++
 zy-acs-manager/src/main/java/com/zy/acs/manager/manager/service/AgvService.java              |    2 
 zy-acs-manager/src/main/java/com/zy/acs/manager/core/service/AllocateService.java            |    5 
 zy-acs-manager/src/main/java/com/zy/acs/manager/manager/service/impl/AgvServiceImpl.java     |   16 +++
 10 files changed, 223 insertions(+), 30 deletions(-)

diff --git a/zy-acs-manager/src/main/java/com/zy/acs/manager/core/scheduler/MaintainScheduler.java b/zy-acs-manager/src/main/java/com/zy/acs/manager/core/scheduler/MaintainScheduler.java
index 95f7210..a3df759 100644
--- a/zy-acs-manager/src/main/java/com/zy/acs/manager/core/scheduler/MaintainScheduler.java
+++ b/zy-acs-manager/src/main/java/com/zy/acs/manager/core/scheduler/MaintainScheduler.java
@@ -4,10 +4,7 @@
 import com.zy.acs.common.enums.AgvStatusType;
 import com.zy.acs.manager.core.service.MainService;
 import com.zy.acs.manager.manager.entity.*;
-import com.zy.acs.manager.manager.enums.FuncStaStateType;
-import com.zy.acs.manager.manager.enums.FuncStaType;
-import com.zy.acs.manager.manager.enums.TaskStsType;
-import com.zy.acs.manager.manager.enums.TaskTypeType;
+import com.zy.acs.manager.manager.enums.*;
 import com.zy.acs.manager.manager.service.*;
 import com.zy.acs.manager.system.service.ConfigService;
 import lombok.extern.slf4j.Slf4j;
@@ -41,24 +38,39 @@
     private ConfigService configService;
     @Autowired
     private AgvModelService agvModelService;
+    @Autowired
+    private SegmentService segmentService;
 
     @Scheduled(cron = "0/5 * * * * ? ")
     private synchronized void autoCharge(){
-        List<Agv> agvList = agvService.list(new LambdaQueryWrapper<Agv>().eq(Agv::getStatus, 1));
+        List<Agv> agvList = agvService.list(new LambdaQueryWrapper<Agv>().eq(Agv::getStatus, StatusType.ENABLE.val));
         for (Agv agv : agvList) {
             AgvDetail agvDetail = agvDetailService.selectByAgvId(agv.getId());
+            AgvModel agvModel = agvModelService.getById(agv.getAgvModel());
             if (null == agvDetail || null == agvDetail.getVol() || null == agvDetail.getAgvStatus()) {
                 continue;
             }
-            if (agvDetail.getVol() <= agv.getChargeLine() && !agvDetail.getAgvStatus().equals(AgvStatusType.CHARGE)) {
+            if (agvDetail.getAgvStatus().equals(AgvStatusType.CHARGE)) {
+                continue;
+            }
+            if (agvDetail.getVol() < agv.getChargeLine() || agvDetail.getVol() < agvModel.getLowBattery()) {
                 if (taskService.count(new LambdaQueryWrapper<Task>()
                         .eq(Task::getAgvId, agv.getId())
                         .and(i -> {
 //                            i.eq(Task::getTaskSts, TaskStsType.WAITING.val())
-                            i.eq(Task::getTaskSts, TaskStsType.ASSIGN.val())
-                                    .or().eq(Task::getTaskSts, TaskStsType.PROGRESS.val());
+                                i.eq(Task::getTaskSts, TaskStsType.ASSIGN.val())
+                                .or().eq(Task::getTaskSts, TaskStsType.PROGRESS.val());
                         })) > 0) {
-                    return;
+                    continue;
+                }
+                if (segmentService.count(new LambdaQueryWrapper<Segment>()
+                        .eq(Segment::getAgvId, agv.getId())
+                        .and( i -> {
+//                                i.eq(Segment::getState, SegmentStateType.WAITING.toString()).or()
+                                i.eq(Segment::getState, SegmentStateType.RUNNING.toString());
+                        })
+                ) > 0) {
+                    continue;
                 }
                 mainService.buildMinorTask(agv, agvDetail, TaskTypeType.TO_CHARGE, null);
             }
@@ -71,7 +83,7 @@
     private void autoStandby(){
         // 1.agv鍏呯數鍒颁竴瀹氱數鍘嬪悗锛屽洖鍒板緟鏈轰綅
         // 2.agv娌℃湁浠诲姟鏃讹紝鍥炲埌寰呮満浣�
-        List<Agv> agvList = agvService.list(new LambdaQueryWrapper<Agv>().eq(Agv::getStatus, 1));
+        List<Agv> agvList = agvService.list(new LambdaQueryWrapper<Agv>().eq(Agv::getStatus, StatusType.ENABLE.val));
         label: for (Agv agv : agvList) {
             AgvDetail agvDetail = agvDetailService.selectByAgvId(agv.getId());
             if (null == agvDetail || null == agvDetail.getVol() || null == agvDetail.getAgvStatus() || null == agvDetail.getCode()) {
diff --git a/zy-acs-manager/src/main/java/com/zy/acs/manager/core/service/AllocateService.java b/zy-acs-manager/src/main/java/com/zy/acs/manager/core/service/AllocateService.java
index 279170e..c62fe74 100644
--- a/zy-acs-manager/src/main/java/com/zy/acs/manager/core/service/AllocateService.java
+++ b/zy-acs-manager/src/main/java/com/zy/acs/manager/core/service/AllocateService.java
@@ -5,6 +5,7 @@
 import com.zy.acs.manager.common.utils.LocUtils;
 import com.zy.acs.manager.core.domain.Lane;
 import com.zy.acs.manager.manager.entity.*;
+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.service.*;
@@ -47,7 +48,7 @@
      */
     private List<Agv> getAvailableAgv() {
         List<Agv> result = new ArrayList<>();
-        List<Agv> agvList = agvService.list(new LambdaQueryWrapper<Agv>().eq(Agv::getStatus, 1));
+        List<Agv> agvList = agvService.list(new LambdaQueryWrapper<Agv>().eq(Agv::getStatus, StatusType.ENABLE.val));
         Collections.shuffle(agvList);
         for (Agv agv : agvList) {
 
@@ -241,7 +242,7 @@
 
         Agv hit = null;
 
-        List<Agv> agvList = agvService.list(new LambdaQueryWrapper<Agv>().eq(Agv::getStatus, 1));
+        List<Agv> agvList = agvService.list(new LambdaQueryWrapper<Agv>().eq(Agv::getStatus, StatusType.ENABLE.val));
         Collections.shuffle(agvList);
         for (Agv agv : agvList) {
             AgvModel agvModel = agvModelService.getById(agv.getAgvModel());
diff --git a/zy-acs-manager/src/main/java/com/zy/acs/manager/core/service/MainService.java b/zy-acs-manager/src/main/java/com/zy/acs/manager/core/service/MainService.java
index c35cd65..1e752b6 100644
--- a/zy-acs-manager/src/main/java/com/zy/acs/manager/core/service/MainService.java
+++ b/zy-acs-manager/src/main/java/com/zy/acs/manager/core/service/MainService.java
@@ -499,16 +499,22 @@
             switch (taskType) {
                 case TO_CHARGE:
                 case TO_STANDBY:
-                    List<FuncSta> funcStaList = funcStaService.list(new LambdaQueryWrapper<FuncSta>()
-                                    .eq(FuncSta::getType, FuncStaType.query(taskType).toString())
-                                    .eq(FuncSta::getAgvId, agv.getId())
-                                    .eq(FuncSta::getState, FuncStaStateType.IDLE.toString())
-                                    .eq(FuncSta::getStatus, StatusType.ENABLE.val)
-                    );
-                    if (!Cools.isEmpty(funcStaList)) {
-                        FuncSta funcSta = funcStaList.get(0);
+                    List<FuncSta> idleFunStaList = funcStaService.findInIdleStatus(FuncStaType.query(taskType), agv.getId());
+                    if (!Cools.isEmpty(idleFunStaList)) {
+                        FuncSta funcSta = funcStaService.checkoutClosestFunSta(agvDetail.getRecentCode(), idleFunStaList);
                         endCode = codeService.getById(funcSta.getCode());
                     }
+
+//                    List<FuncSta> funcStaList = funcStaService.list(new LambdaQueryWrapper<FuncSta>()
+//                                    .eq(FuncSta::getType, FuncStaType.query(taskType).toString())
+//                                    .eq(FuncSta::getAgvId, agv.getId())
+//                                    .eq(FuncSta::getState, FuncStaStateType.IDLE.toString())
+//                                    .eq(FuncSta::getStatus, StatusType.ENABLE.val)
+//                    );
+//                    if (!Cools.isEmpty(funcStaList)) {
+//                        FuncSta funcSta = funcStaList.get(0);
+//                        endCode = codeService.getById(funcSta.getCode());
+//                    }
                     break;
                 case MOVE:
                     endCode = codeService.selectByData(destination);
diff --git a/zy-acs-manager/src/main/java/com/zy/acs/manager/manager/controller/DigitalController.java b/zy-acs-manager/src/main/java/com/zy/acs/manager/manager/controller/DigitalController.java
index a5a6429..7159bb3 100644
--- a/zy-acs-manager/src/main/java/com/zy/acs/manager/manager/controller/DigitalController.java
+++ b/zy-acs-manager/src/main/java/com/zy/acs/manager/manager/controller/DigitalController.java
@@ -7,16 +7,13 @@
 import com.zy.acs.common.domain.protocol.IMessageBody;
 import com.zy.acs.common.enums.AgvInOutFlagType;
 import com.zy.acs.common.utils.RedisSupport;
+import com.zy.acs.framework.common.Cools;
+import com.zy.acs.framework.common.R;
 import com.zy.acs.manager.manager.controller.result.DigitalAgvVo;
 import com.zy.acs.manager.manager.controller.result.DigitalStockVo;
 import com.zy.acs.manager.manager.entity.*;
-import com.zy.acs.manager.manager.enums.ActionStsType;
-import com.zy.acs.manager.manager.enums.ActionTypeType;
-import com.zy.acs.manager.manager.enums.LocStsType;
-import com.zy.acs.manager.manager.enums.TaskStsType;
+import com.zy.acs.manager.manager.enums.*;
 import com.zy.acs.manager.manager.service.*;
-import com.zy.acs.framework.common.Cools;
-import com.zy.acs.framework.common.R;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.web.bind.annotation.GetMapping;
 import org.springframework.web.bind.annotation.RequestMapping;
@@ -51,7 +48,7 @@
     public R getAgvInfo() {
         List<DigitalAgvVo> voList = new ArrayList<>();
 
-        List<Agv> agvList = agvService.list(new LambdaQueryWrapper<Agv>().eq(Agv::getStatus, 1));
+        List<Agv> agvList = agvService.list(new LambdaQueryWrapper<Agv>().eq(Agv::getStatus, StatusType.ENABLE.val));
         for (Agv agv : agvList) {
             DigitalAgvVo vo = new DigitalAgvVo();
             vo.setAgvNo(agv.getUuid());
diff --git a/zy-acs-manager/src/main/java/com/zy/acs/manager/manager/service/AgvService.java b/zy-acs-manager/src/main/java/com/zy/acs/manager/manager/service/AgvService.java
index 638bd43..82a11cc 100644
--- a/zy-acs-manager/src/main/java/com/zy/acs/manager/manager/service/AgvService.java
+++ b/zy-acs-manager/src/main/java/com/zy/acs/manager/manager/service/AgvService.java
@@ -35,4 +35,6 @@
 
     Integer getBackpack(Agv agv);
 
+    Agv findByPosition(Long codeId);
+
 }
diff --git a/zy-acs-manager/src/main/java/com/zy/acs/manager/manager/service/FuncStaService.java b/zy-acs-manager/src/main/java/com/zy/acs/manager/manager/service/FuncStaService.java
index 2499426..4c98b4b 100644
--- a/zy-acs-manager/src/main/java/com/zy/acs/manager/manager/service/FuncStaService.java
+++ b/zy-acs-manager/src/main/java/com/zy/acs/manager/manager/service/FuncStaService.java
@@ -2,6 +2,9 @@
 
 import com.baomidou.mybatisplus.extension.service.IService;
 import com.zy.acs.manager.manager.entity.FuncSta;
+import com.zy.acs.manager.manager.enums.FuncStaType;
+
+import java.util.List;
 
 public interface FuncStaService extends IService<FuncSta> {
 
@@ -9,4 +12,8 @@
 
     FuncSta query(Long agvId, Long codeId, String type);
 
+    List<FuncSta> findInIdleStatus(FuncStaType type, Long agvId);
+
+    FuncSta checkoutClosestFunSta(Long codeId, List<FuncSta> funcStaList);
+
 }
diff --git a/zy-acs-manager/src/main/java/com/zy/acs/manager/manager/service/TaskService.java b/zy-acs-manager/src/main/java/com/zy/acs/manager/manager/service/TaskService.java
index 84a32f1..364104b 100644
--- a/zy-acs-manager/src/main/java/com/zy/acs/manager/manager/service/TaskService.java
+++ b/zy-acs-manager/src/main/java/com/zy/acs/manager/manager/service/TaskService.java
@@ -34,4 +34,6 @@
 
     List<Task> findRunningTasksByAgv(Long agvId);
 
+    Task findLatestTask(Long agvId);
+
 }
diff --git a/zy-acs-manager/src/main/java/com/zy/acs/manager/manager/service/impl/AgvServiceImpl.java b/zy-acs-manager/src/main/java/com/zy/acs/manager/manager/service/impl/AgvServiceImpl.java
index 336317f..844be39 100644
--- a/zy-acs-manager/src/main/java/com/zy/acs/manager/manager/service/impl/AgvServiceImpl.java
+++ b/zy-acs-manager/src/main/java/com/zy/acs/manager/manager/service/impl/AgvServiceImpl.java
@@ -13,6 +13,7 @@
 import com.zy.acs.manager.manager.controller.result.AgvResult;
 import com.zy.acs.manager.manager.entity.*;
 import com.zy.acs.manager.manager.enums.SegmentStateType;
+import com.zy.acs.manager.manager.enums.StatusType;
 import com.zy.acs.manager.manager.mapper.AgvMapper;
 import com.zy.acs.manager.manager.service.*;
 import com.zy.acs.manager.system.service.ConfigService;
@@ -160,4 +161,19 @@
         return agvModel.getBackpack();
     }
 
+    @Override
+    public Agv findByPosition(Long codeId) {
+        List<Agv> agvList = this.list(new LambdaQueryWrapper<Agv>().eq(Agv::getStatus, StatusType.ENABLE.val));
+        for (Agv agv : agvList) {
+            AgvDetail agvDetail = agvDetailService.selectByAgvId(agv.getId());
+            if (null == agvDetail) {
+                continue;
+            }
+            if (agvDetail.getRecentCode().equals(codeId)) {
+                return agv;
+            }
+        }
+        return null;
+    }
+
 }
diff --git a/zy-acs-manager/src/main/java/com/zy/acs/manager/manager/service/impl/FuncStaServiceImpl.java b/zy-acs-manager/src/main/java/com/zy/acs/manager/manager/service/impl/FuncStaServiceImpl.java
index 680bfde..ae89023 100644
--- a/zy-acs-manager/src/main/java/com/zy/acs/manager/manager/service/impl/FuncStaServiceImpl.java
+++ b/zy-acs-manager/src/main/java/com/zy/acs/manager/manager/service/impl/FuncStaServiceImpl.java
@@ -2,16 +2,37 @@
 
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
-import com.zy.acs.manager.manager.entity.FuncSta;
-import com.zy.acs.manager.manager.mapper.FuncStaMapper;
-import com.zy.acs.manager.manager.service.FuncStaService;
+import com.zy.acs.common.enums.AgvStatusType;
 import com.zy.acs.framework.common.Cools;
+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.ArrayList;
+import java.util.Collections;
+import java.util.Comparator;
 import java.util.List;
+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) {
@@ -32,4 +53,122 @@
         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);
+        if (null != agvId) {
+            wrapper.eq(FuncSta::getAgvId, agvId);
+        }
+        List<FuncSta> funcStaList = this.list(wrapper);
+        funcStaList = funcStaList.stream().filter(funcSta -> {
+            if (null != funcSta.getAgvId()) {
+                if (!funcSta.getAgvId().equals(agvId)) {
+                    return false;
+                }
+            }
+
+            return true;
+        }).collect(Collectors.toList());
+
+        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 existAgv = 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 != existAgv) {
+                    AgvDetail agvDetail = agvDetailService.selectByAgvId(existAgv.getId());
+                    AgvModel agvModel = agvModelService.getById(existAgv.getAgvModel());
+                    if (agvDetail.getAgvStatus().equals(AgvStatusType.CHARGE)) {
+                        if (agvDetail.getVol() < agvModel.getQuaBattery()) {
+                            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>()
+                        .eq(Task::getTaskType, 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 == existAgv) {
+                    // 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>()
+                            .eq(Task::getTaskType, TaskTypeType.TO_STANDBY.val())
+                            .in(Task::getTaskSts, TaskStsType.ASSIGN.val(), TaskStsType.PROGRESS.val())
+                            .eq(Task::getDestCode, code)
+                    )) {
+                        return false;
+                    }
+
+                } else {
+                    // 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 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杩斿洖璐熸暟锛屽垯鎺掑湪闆嗗悎鍓嶉潰 锛坅sc锛�
+            funcStaList.sort(new Comparator<FuncSta>() {
+                @Override
+                public int compare(FuncSta o1, FuncSta o2) {
+                    Code o1Code = codeService.getById(o1.getCode());
+                    int o1Distance = calcPositionDistance(startPos, new Double[]{o1Code.getX(), o1Code.getY()});
+
+                    Code o2Code = codeService.getById(o2.getCode());
+                    int o2Distance = calcPositionDistance(startPos, new Double[]{o2Code.getX(), o2Code.getY()});
+
+                    return o1Distance - o2Distance;
+                }
+            });
+
+        }
+
+        return funcStaList.stream().findFirst().orElse(null);
+    }
+
+    private int calcPositionDistance(Double[] from, Double[] to) {
+        return (int) (Math.abs(to[0] - from[0]) + Math.abs(to[1] - from[1]));
+    }
+
 }
diff --git a/zy-acs-manager/src/main/java/com/zy/acs/manager/manager/service/impl/TaskServiceImpl.java b/zy-acs-manager/src/main/java/com/zy/acs/manager/manager/service/impl/TaskServiceImpl.java
index f37814e..88dc08e 100644
--- a/zy-acs-manager/src/main/java/com/zy/acs/manager/manager/service/impl/TaskServiceImpl.java
+++ b/zy-acs-manager/src/main/java/com/zy/acs/manager/manager/service/impl/TaskServiceImpl.java
@@ -186,6 +186,17 @@
         return this.list(wrapper);
     }
 
+    @Override
+    public Task findLatestTask(Long agvId) {
+        LambdaQueryWrapper<Task> wrapper = new LambdaQueryWrapper<Task>()
+                .orderByDesc(Task::getCreateTime)
+                .last("limit 0, 1");
+        if (null != agvId) {
+            wrapper.eq(Task::getAgvId, agvId);
+        }
+        return this.list(wrapper).stream().findFirst().orElse(null);
+    }
+
     @Transactional
     public void maintainLocSts(Task task, Boolean complete) {
         Loc oriLoc = null; Loc destLoc = null;

--
Gitblit v1.9.1