zhang
2025-05-20 1313906bb1eb983d3beece810035e7fc28d6a92f
zy-acs-manager/src/main/java/com/zy/acs/manager/manager/service/impl/FuncStaServiceImpl.java
@@ -4,6 +4,8 @@
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.core.constant.AgvAreaDispatcher;
import com.zy.acs.manager.manager.entity.*;
import com.zy.acs.manager.manager.enums.FuncStaType;
import com.zy.acs.manager.manager.enums.StatusType;
@@ -14,10 +16,7 @@
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.*;
import java.util.stream.Collectors;
@Service("funcStaService")
@@ -33,6 +32,8 @@
    private AgvModelService agvModelService;
    @Autowired
    private TaskService taskService;
    @Autowired
    private AgvAreaDispatcher agvAreaDispatcher;
    @Override
    public FuncSta getByCodeAndType(Long codeId, String type) {
@@ -44,9 +45,8 @@
    }
    @Override
    public FuncSta query(Long agvId, Long codeId, String type) {
    public FuncSta query(Long codeId, String type) {
        List<FuncSta> list = this.list(new LambdaQueryWrapper<FuncSta>()
                .eq(FuncSta::getAgvId, agvId)
                .eq(FuncSta::getCode, codeId)
                .eq(FuncSta::getType, type)
        );
@@ -57,25 +57,28 @@
    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);
        // area limit
        List<String> areaCodeList = agvAreaDispatcher.getAreaCodeListByAgvNo(agvService.getAgvNo(agvId));
        if (Cools.isEmpty(areaCodeList)) {
            funcStaList.clear();
        } else {
            funcStaList.removeIf(funcSta -> {
                Code code = codeService.getCacheById(funcSta.getCode());
                if (Cools.isEmpty(code, code.getData())) { return true; }
                return !areaCodeList.contains(code.getData());
            });
        }
        if (!Cools.isEmpty(funcStaList)) {
            Collections.shuffle(funcStaList);
        }
        // filter idle
        funcStaList = funcStaList.stream().filter(funcSta -> {
@@ -84,23 +87,34 @@
                return false;
            }
            Agv existAgv = agvService.findByPosition(code);
            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 != existAgv) {
                    AgvDetail agvDetail = agvDetailService.selectByAgvId(existAgv.getId());
                    AgvModel agvModel = agvModelService.getById(existAgv.getAgvModel());
                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>()
                        .eq(Task::getTaskType, TaskTypeType.TO_CHARGE.val())
                        .in(Task::getTaskType, TaskTypeType.TO_STANDBY.val(), TaskTypeType.TO_CHARGE.val())
                        .in(Task::getTaskSts, TaskStsType.ASSIGN.val(), TaskStsType.PROGRESS.val())
                        .eq(Task::getDestCode, code)
                )) {
@@ -111,10 +125,14 @@
            if (funcSta.getType().equals(FuncStaType.STANDBY.toString())) {
                if (null == existAgv) {
                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>()
                            .eq(Task::getTaskType, TaskTypeType.TO_STANDBY.val())
                            .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)
                    )) {
@@ -122,10 +140,13 @@
                    }
                } 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
                    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;
                    }
                }
            }
@@ -143,7 +164,7 @@
        }
        if (null != codeId) {
            Code currCode = codeService.getById(codeId);
            Code currCode = codeService.getCacheById(codeId);
            Double[] startPos = new Double[]{currCode.getX(), currCode.getY()};
            // checkout one funSta which is the closest
@@ -152,11 +173,11 @@
            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 o1Code = codeService.getCacheById(o1.getCode());
                    int o1Distance = CommonUtil.calcDistance(startPos, new Double[]{o1Code.getX(), o1Code.getY()});
                    Code o2Code = codeService.getById(o2.getCode());
                    int o2Distance = calcPositionDistance(startPos, new Double[]{o2Code.getX(), o2Code.getY()});
                    Code o2Code = codeService.getCacheById(o2.getCode());
                    int o2Distance = CommonUtil.calcDistance(startPos, new Double[]{o2Code.getX(), o2Code.getY()});
                    return o1Distance - o2Distance;
                }
@@ -167,8 +188,102 @@
        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]));
    @Override
    public FuncSta checkoutFurthestFunSta(Long codeId, List<FuncSta> funcStaList) {
        if (Cools.isEmpty(funcStaList)) {
            return null;
        }
        if (null != codeId) {
            Code currCode = codeService.getCacheById(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.getCacheById(o1.getCode());
                    int o1Distance = CommonUtil.calcDistance(startPos, new Double[]{o1Code.getX(), o1Code.getY()});
                    Code o2Code = codeService.getCacheById(o2.getCode());
                    int o2Distance = CommonUtil.calcDistance(startPos, new Double[]{o2Code.getX(), o2Code.getY()});
                    return o2Distance - o1Distance;
                }
            });
        }
        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;
    }
}