自动化立体仓库 - WMS系统
skyouc
4 天以前 f8014b4fb370266a1b2bd73a0266bb66b79d358a
站点生成入库任务
1个文件已添加
3个文件已修改
2195 ■■■■ 已修改文件
src/main/java/com/zy/asrs/controller/MobileController.java 143 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/zy/asrs/entity/param/CompleteParam.java 21 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/zy/asrs/service/MobileService.java 79 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/zy/asrs/service/impl/MobileServiceImpl.java 1952 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/zy/asrs/controller/MobileController.java
@@ -61,11 +61,6 @@
    private StaDescService staDescService;
    @PostMapping("/agv/callEmptyCar")
    @ManagerAuth
    public R pdaAgvFinishedCall(@RequestBody AgvCallParams params) {
        return mobileService.callEmptyCar(params, getUserId());
    }
    @RequestMapping("/pda/WarehouseOut/v1")
    @ManagerAuth(memo = "并板途中拣料-参考念初")
@@ -162,105 +157,9 @@
        return mobileService.getCacheLocs();
    }
    @PostMapping("/cache/agv/call")
    @ApiOperation("缓冲区入库")
    @ManagerAuth
    public R callAgvMove(@RequestBody AgvCallParams params) {
        if (Objects.isNull(params)) {
            return R.error("参数不能为空!!");
        }
        return mobileService.callAgvMove(params, getUserId());
    }
    @PostMapping("/collection/agv/call")
    @ApiOperation("集货区入库")
    @ManagerAuth
    public R CollectionInCall(@RequestBody AgvCallParams params) {
        if (Objects.isNull(params)) {
            return R.error("参数不能为空!!");
        }
        return mobileService.collectionInCall(params, getUserId());
    }
    /**
     * @author Ryan
     * @date 2025/11/3
     * @description: 呼叫AGV搬运入库缓存区/EO/SO
     * @version 1.0
     */
    @PostMapping("/cache/out/call")
    @ApiOperation("呼叫AGV搬运")
    @ManagerAuth
    public R OutCallAgv(@RequestBody AgvCallParams params) {
        if (Objects.isNull(params)) {
            return R.error("参数不能为空!!");
        }
        return mobileService.OutCallAgv(params, getUserId());
    }
    /**
     * 缓存区出库组托上架
     *
     * @author Ryan
     * @date 2025/12/8 13:45
     * @param params
     * @return com.core.common.R
     */
    @ManagerAuth
    @ApiOperation("缓存出库上架")
    @PostMapping("/commb/task/pub")
    public R combPub(@RequestBody AgvCallParams params) {
        if (Objects.isNull(params)) {
            return R.error("参数不能为空!!");
        }
        return mobileService.combOutPub(params);
    }
    /**
     * 缓存区出库组托上架
     *
     * @author Ryan
     * @date 2025/12/8 13:45
     * @param params
     * @return com.core.common.R
     */
    @ManagerAuth
    @ApiOperation("缓存出库列表")
    @PostMapping("/cache/task/list")
    public R combCacehList(@RequestBody AgvCallParams params) {
        if (Objects.isNull(params)) {
            return R.error("参数不能为空!!");
        }
        return mobileService.combCacehList(params);
    }
    // 组托
    // ----------------------------------------------------------------------------------------------------
    /**
     * 根据单号检索单据数据
     * http://localhost:8081/jkwms/mobile/order/serach/orderNo/auth?orderNo=123123
     */
    // @RequestMapping("/order/search/orderNo/auth")
    // @ManagerAuth
    // public R orderSearchByBarcode(@RequestParam String orderNo){
    // Order order = orderService.selectByNo(orderNo);
    // if (order == null) {
    // return R.ok();
    // }
    // DocType docType = docTypeService.selectById(order.getDocType());
    // if (docType.getPakin() == null || docType.getPakin() != 1) {
    // return R.ok();
    // }
    // if (order.getSettle() > 2) {
    // return R.ok();
    // }
    // List<OrderDetl> orderDetls = orderService.selectWorkingDetls(order.getId());
    // if (Cools.isEmpty(orderDetls)) {
    // return R.ok();
    // }
    // return R.ok().add(orderDetls);
    // }
    @RequestMapping("/order/search/orderNo/auth")
    @ManagerAuth
    public R orderSearchByBarcode(@RequestParam String orderNo) {
@@ -309,40 +208,6 @@
        return R.ok().add(combParams);
    }
    @ManagerAuth(memo = "组托")
    @RequestMapping("/comb/auth")
    public R comb(@RequestBody CombParam combParam) {
        mobileService.comb(combParam, getUserId());
        return R.ok("组托成功");
    }
    @ManagerAuth(memo = "缓冲库入库组托")
    @RequestMapping("/cache/comb/auth")
    public R onlineComb(@RequestBody CombParam combParam) {
        if (Objects.isNull(combParam)) {
            return R.error("参数不能为空!!");
        }
        mobileService.cacheComb(combParam, getUserId());
        return R.ok("组托成功");
    }
    /**
     * @author Ryan
     * @date 2025/9/24
     * @description: 呼叫AGV返回
     * @version 1.0
     */
    @ManagerAuth(memo = "呼叫AGV返回")
    @RequestMapping("/agv/call/back")
    public R callAgvBack(@RequestBody AgvCallParams callAgvBackParam) {
        if (Objects.isNull(callAgvBackParam.getOrgSite())) {
            return R.error("参数不能为空!!");
        }
       return mobileService.agvCallback(callAgvBackParam, getUserId());
    }
    @ManagerAuth(memo = "缓冲库确认上架(模拟物理按钮)")
    @RequestMapping("/cache/comb/pub")
    public R cacheCombPub(@RequestBody CompleteParam combParam) {
@@ -366,12 +231,6 @@
        return R.ok("组托成功");
    }
    @RequestMapping("/pack/comb/auth")
    @ManagerAuth(memo = "下线组托")
    public R packComb(@RequestBody CombParam combParam) {
        mobileService.packComb(combParam, 10031L);
        return R.ok("组托成功");
    }
    // 出库
    // ---------------------------------------------------------------------------------------------------
src/main/java/com/zy/asrs/entity/param/CompleteParam.java
New file
@@ -0,0 +1,21 @@
package com.zy.asrs.entity.param;
import lombok.Data;
import java.io.Serializable;
@Data
public class CompleteParam implements Serializable {
    private String barcode;
    // 库位规格( 0:未知, 1:低库位, 2:高库位)
    private Short locType1;
    private String station;
    private String carBarcode;
    private Integer IoType;
}
src/main/java/com/zy/asrs/service/MobileService.java
@@ -40,11 +40,6 @@
    R pickMats(String matnr, String orderNo);
    /**
     * 组托
     */
    void comb(CombParam param, Long userId);
    /**
     * 上架
     */
    void onSale(CombParam param);
@@ -59,8 +54,6 @@
     */
    void adjust(MobileAdjustParam param, Long userId);
    void packComb(CombParam param, Long userId);
    void pakoutByOrder(JSONObject param, Long userId);
    void stockOut(OrderDetl orderDetl, BasDevp staNo, LocDetl locDetl,
@@ -74,13 +67,6 @@
     */
    R getCacheLocs();
    /**
     * @author Ryan
     * @date 2025/9/22
     * @description: 呼叫AGV搬运
     * @version 1.0
     */
    R callAgvMove(AgvCallParams params, Long userId);
    /**
     * @author Ryan
@@ -90,54 +76,7 @@
     */
    R getMatsByQRcode(PakinMatsByQRParams params);
    /**
     * @author Ryan
     * @date 2025/9/24
     * @description: 呼叫AGV出库搬运
     * @version 1.0
     */
    R OutCallAgv(AgvCallParams params, Long userId);
    /**
     * 生成立库出库任务
     * @author Ryan
     * @date 2025/12/3 8:07
     * @param locCaches
     */
    void generateCRNOutTask(BasStation station, LocCache locCaches, Long userId);
    R callEmptyCar(AgvCallParams params, Long userId);
    R collectionInCall(AgvCallParams params, Long userId);
    /**
     * 缓存出库组托上架
     * @author Ryan
     * @date 2025/12/8 13:46
     * @param params
     * @return com.core.common.R
     */
    R combOutPub(AgvCallParams params);
    /**
     * 缓存出库组托列表
     * @author Ryan
     * @date 2025/12/8 15:36
     * @param params
     * @return com.core.common.R
     */
    R combCacehList(AgvCallParams params);
    /**
     * 缓存区入库组托
     *
     * @param combParam
     * @param userId
     * @author Ryan
     * @date 2025/12/9 13:44
     */
    void cacheComb(CombParam combParam, Long userId);
    /**
     * 立库入库上架
@@ -149,23 +88,5 @@
     */
    void combInPub(CompleteParam combParam, Long userId);
    /**
     * SO/EO 区组托
     * @author Ryan
     * @date 2025/12/18 15:19
     * @param params
     * @return com.core.common.R
     */
    R collectionPakin(CollectionPakinParams params, Long userId);
    R collectionPakinView(CollectionPakinParams params);
    /**
     * 呼叫AGV返回
     * @author Ryan
     * @date 2025/9/24
     * @param callAgvBackParam
     * @return com.core.common.R
     */
    R agvCallback(AgvCallParams callAgvBackParam, Long userId);
}
src/main/java/com/zy/asrs/service/impl/MobileServiceImpl.java
@@ -1,6 +1,5 @@
package com.zy.asrs.service.impl;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
@@ -9,24 +8,15 @@
import com.zy.asrs.entity.*;
import com.zy.asrs.entity.param.*;
import com.zy.asrs.entity.result.*;
import com.zy.asrs.enums.*;
import com.zy.asrs.enums.LocAreaType;
import com.zy.asrs.enums.LocStsType;
import com.zy.asrs.mapper.LocMastMapper;
import com.zy.asrs.mapper.ManLocDetlMapper;
import com.zy.asrs.service.*;
import com.zy.asrs.utils.MatUtils;
import com.zy.asrs.utils.OrderInAndOutUtil;
import com.zy.common.constant.HIKApiConstant;
import com.zy.common.constant.MesConstant;
import com.zy.common.entity.Parameter;
import com.zy.common.model.DetlDto;
import com.zy.common.model.LocTypeDto;
import com.zy.common.model.MesCombParam;
import com.zy.common.model.StartupDto;
import com.zy.common.model.enums.WorkNoType;
import com.zy.common.service.CommonService;
import com.zy.common.utils.HttpHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
@@ -34,12 +24,7 @@
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Collectors;
/**
 * 移动端服务核心类
 * Created by vincent on 2020/6/28
@@ -63,14 +48,6 @@
    @Autowired
    private WrkDetlService wrkDetlService;
    @Autowired
    private PackService packService;
    @Autowired
    private OpenService openService;
    @Autowired
    private SnowflakeIdWorker snowflakeIdWorker;
    @Autowired
    private ApiLogService apiLogService;
    @Autowired
    private BasCrnpService basCrnpService;
    @Autowired
    private LocMastService locMastService;
@@ -86,46 +63,26 @@
    private ManLocDetlService manLocDetlService;
    @Autowired
    private ManLocDetlMapper manLocDetlMapper;
    @Autowired
    private AdjDetlService adjDetlService;
    @Resource
    private LocMastMapper locMastMapper;
    @Resource
    private DocTypeService docTypeService;
    @Resource
    private OrderDetlPakinService orderDetlPakinService;
    @Resource
    private OrderPakinService orderPakinService;
    @Resource
    private OrderPakoutService orderPakoutService;
    @Resource
    private OrderDetlPakoutService orderDetlPakoutService;
    @Autowired
    private LocCacheService locCacheService;
    @Autowired
    private TaskService taskService;
    @Autowired
    private TaskDetlService taskDetlService;
    @Autowired
    private BasStationService basStationService;
    @Autowired
    private BasContainerService basContainerService;
    @Autowired
    private BasAreasService basAreasService;
    @Autowired
    private StationRelaService stationRelaService;
    @Autowired
    private BasStationDetlService basStationDetlService;
    @Override
    @Transactional
@@ -581,208 +538,208 @@
        return R.ok().add(maps);
    }
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void comb(CombParam param, Long userId) {
        if (Cools.isEmpty(param.getBarcode(), param.getCombMats())) {
            throw new CoolException(BaseRes.PARAM);
        }
        if (param.getCombMats().size() < 1) {
            throw new CoolException("请提取一个商品,或者刷新重新组托!");
        }
        // 判断是否有相同条码的数据
        if (waitPakinService.selectCount(new EntityWrapper<WaitPakin>().eq("zpallet", param.getBarcode())
                .eq("io_status", "N")) > 0) {
            throw new CoolException(param.getBarcode() + "数据正在进行入库");
        }
        if (Objects.isNull(param.getBarcode())) {
            throw new CoolException("托盘码不能为空!!");
        }
        BasContainer container = basContainerService
                .selectOne(new EntityWrapper<BasContainer>().eq("barcode", param.getBarcode()));
        if (Objects.isNull(container)) {
            throw new CoolException("数据错误:容器码不存在!!");
        }
        // if (param.getBarcode().length() != 8) {
        // throw new CoolException("条码长度不是8位===>>" + param.getBarcode());
        // }
        if (Objects.isNull(param.getType())) {
            param.setType("0");
        }
        int countLoc = locDetlService.selectCount(new EntityWrapper<LocDetl>().eq("zpallet", param.getBarcode()));
        int countWrk = wrkDetlService.selectCount(new EntityWrapper<WrkDetl>().eq("zpallet", param.getBarcode()));
        int countwait = waitPakinService.selectCount(new EntityWrapper<WaitPakin>()
                .eq(!Objects.isNull(param.getType()), "type", param.getType())
                .eq("zpallet", param.getBarcode()));
        if (countLoc > 0 || countWrk > 0 || countwait > 0) {
            throw new CoolException("组托档/工作档/库存条码数据已存在===>>" + param.getBarcode());
        }
        Date now = new Date();
        // 无单组托
        if (Cools.isEmpty(param.getOrderNo())) {
            // 生成入库通知档
            List<DetlDto> detlDtos = new ArrayList<>();
            param.getCombMats().forEach(elem -> {
                DetlDto detlDto = new DetlDto(elem.getMatnr(), elem.getBatch(), elem.getBrand(), elem.getStandby1(),
                        elem.getStandby2(), elem.getStandby3(),
                        elem.getBoxType1(), elem.getBoxType2(), elem.getBoxType3(), elem.getAnfme(),
                        elem.getThreeCode());
                if (DetlDto.has(detlDtos, detlDto)) {
                    DetlDto one = DetlDto.findDto(detlDtos, detlDto);
                    assert one != null;
                    one.setAnfme(one.getAnfme() + detlDto.getAnfme());
                } else {
                    detlDtos.add(detlDto);
                }
            });
            for (DetlDto detlDto : detlDtos) {
                Mat mat = matService.selectByMatnr(detlDto.getMatnr());
                if (Cools.isEmpty(mat)) {
                    throw new CoolException(detlDto.getMatnr() + "商品档案不存在");
                }
                if (mat.getUpQty().compareTo(detlDto.getAnfme()) < 0) {
                    throw new CoolException("物料:" + detlDto.getMatnr() + "单次最大组托上限为:" + mat.getUpQty());
                }
                WaitPakin waitPakin = new WaitPakin();
                BeanUtils.copyProperties(mat, waitPakin);
                waitPakin.setBatch(detlDto.getBatch());
                waitPakin.setZpallet(param.getBarcode());
                waitPakin.setIoStatus("N");
                waitPakin.setAnfme(detlDto.getAnfme());
                waitPakin.setThreeCode(detlDto.getThreeCode());
                waitPakin.setStatus("Y");
                waitPakin.setSuppCode(detlDto.getStandby1());
                waitPakin.setStandby1(detlDto.getStandby1());
                waitPakin.setStandby2(detlDto.getStandby2());
                waitPakin.setStandby3(detlDto.getStandby3());
                waitPakin.setBoxType1(detlDto.getBoxType1());
                waitPakin.setBoxType2(detlDto.getBoxType2());
                waitPakin.setBoxType3(detlDto.getBoxType3());
                waitPakin.setAppeUser(userId);
                waitPakin.setAppeTime(now);
                waitPakin.setModiUser(userId);
                waitPakin.setModiTime(now);
                if (!waitPakinService.insert(waitPakin)) {
                    throw new CoolException("保存入库通知档失败");
                }
            }
            // 关联组托
        } else {
            // Order order = orderService.selectByNo(param.getOrderNo());
            // 生成入库通知档
            List<DetlDto> detlDtos = new ArrayList<>();
            param.getCombMats().forEach(elem -> {
                Order order = OrderInAndOutUtil.selectByNo(Boolean.TRUE, elem.getOrderNo());
                if (Cools.isEmpty(order) || order.getSettle() > 2) {
                    throw new CoolException("单据编号已过期");
                }
                // 订单明细数量校验
                // OrderDetl orderDetl = OrderInAndOutUtil.selectItem(Boolean.TRUE,
                // order.getId(), elem.getMatnr(), elem.getBatch(), elem.getBrand(),
                // elem.getStandby1(), elem.getStandby2(), elem.getStandby3(),
                // elem.getBoxType1(), elem.getBoxType2(), elem.getBoxType3());
                OrderDetlPakin detls = orderDetlPakinService.selectOne(new EntityWrapper<OrderDetlPakin>()
                        .eq("order_id", order.getId())
                        .eq("matnr", elem.getMatnr()));
                if (Objects.isNull(detls)) {
                    throw new CoolException("数据错误:单据明细不存在!!");
                }
                if (elem.getAnfme() > detls.getEnableQty()) {
                    throw new CoolException(detls.getMatnr() + "入库数量不合规则");
                }
                OrderInAndOutUtil.increaseWorkQty(Boolean.TRUE, order.getId(), elem.getMatnr(), elem.getBatch(),
                        elem.getBrand(), elem.getStandby1(), elem.getStandby2(), elem.getStandby3(),
                        elem.getBoxType1(), elem.getBoxType2(), elem.getBoxType3(), elem.getAnfme());
                DetlDto detlDto = new DetlDto(order.getOrderNo(), elem.getMatnr(), elem.getBatch(), elem.getBrand(), elem.getStandby1(),
                        elem.getStandby2(), elem.getStandby3(),
                        elem.getBoxType1(), elem.getBoxType2(), elem.getBoxType3(), elem.getAnfme(),
                        elem.getThreeCode());
                detlDto.setOrderId(order.getId());
                detlDto.setOrderNo(order.getOrderNo());
                if (DetlDto.hasOrder(detlDtos, detlDto)) {
                    DetlDto one = DetlDto.findDto(detlDtos, detlDto);
                    assert one != null;
                    one.setAnfme(one.getAnfme() + detlDto.getAnfme());
                } else {
                    detlDtos.add(detlDto);
                }
            });
            Set<String> matnrs = detlDtos.stream().map(DetlDto::getMatnr).collect(Collectors.toSet());
            List<Mat> mats = matService.selectList(new EntityWrapper<Mat>().in("matnr", matnrs));
            Set<Long> tagIds = mats.stream().map(Mat::getTagId).collect(Collectors.toSet());
            if (tagIds.size() > 1) {
                throw new CoolException("组托物料类型不一致,只有相同的物料分类才可以组托!!");
            }
            // BasContainer container = basContainerService.selectOne(new
            // EntityWrapper<BasContainer>().eq("barcode", param.getBarcode()));
            // if (Objects.isNull(container)) {
            // throw new CoolException("数据错误:容器码不存在!!");
            // }
            // if (container.getMixMax() < detlDtos.size()) {
            // throw new CoolException("超出容器最大混装数量,当前容器最大数量为:" + container.getMixMax() +
            // "!!");
            // }
            // 还可以放入多少种物料
            // Integer suplus = container.getMixMax();
            for (DetlDto detlDto : detlDtos) {
                Mat mat = matService.selectByMatnr(detlDto.getMatnr());
                if (Cools.isEmpty(mat)) {
                    throw new CoolException(detlDto.getMatnr() + "商品档案不存在");
                }
                // //最多可放数量
                // Double singleMax = mat.getUpQty() * suplus;
                // if (singleMax.compareTo(detlDto.getAnfme()) < 0) {
                // throw new CoolException("物料:" + detlDto.getMatnr() + "单次组托上限为:" +
                // mat.getUpQty() + ",当前总量超出托盘装载上限!!");
                // }
                // BigDecimal decimal = new BigDecimal(detlDto.getAnfme() / mat.getUpQty());
                // //当前物料需要占用料箱格数
                // Integer curr = decimal.setScale(0, RoundingMode.CEILING).intValue();
                // suplus = suplus - curr;
                // if (suplus < 0) {
                // throw new CoolException("物料:" + detlDto.getMatnr() + ", 超出当前托盘装载上限!!");
                // }
                WaitPakin waitPakin = new WaitPakin();
                BeanUtils.copyProperties(mat, waitPakin);
                waitPakin.setOrderNo(detlDto.getOrderNo()); // 单据编号
                waitPakin.setType(param.getType());
                waitPakin.setOrderId(detlDto.getOrderId());
                waitPakin.setBatch(detlDto.getBatch()); // 序列码
                waitPakin.setZpallet(param.getBarcode()); // 托盘码
                waitPakin.setThreeCode(detlDto.getThreeCode());
                waitPakin.setSuppCode(detlDto.getStandby1());
                waitPakin.setStandby1(detlDto.getStandby1());
                waitPakin.setIoStatus("N"); // 入出状态
                waitPakin.setAnfme(detlDto.getAnfme()); // 数量
                waitPakin.setStatus("Y"); // 状态
                waitPakin.setAppeUser(userId);
                waitPakin.setAppeTime(now);
                waitPakin.setModiUser(userId);
                waitPakin.setModiTime(now);
                if (!waitPakinService.insert(waitPakin)) {
                    throw new CoolException("保存入库通知档失败");
                }
            }
            Set<String> stringSet = param.getCombMats().stream().map(CombParam.CombMat::getOrderNo)
                    .collect(Collectors.toSet());
            stringSet.forEach(orderNo -> {
                Order order = OrderInAndOutUtil.selectByNo(Boolean.TRUE, orderNo);
                OrderInAndOutUtil.updateOrder(Boolean.TRUE, order.getId(), 2L, userId);
            });
        }
    }
//    @Override
//    @Transactional(rollbackFor = Exception.class)
//    public void comb(CombParam param, Long userId) {
//        if (Cools.isEmpty(param.getBarcode(), param.getCombMats())) {
//            throw new CoolException(BaseRes.PARAM);
//        }
//        if (param.getCombMats().size() < 1) {
//            throw new CoolException("请提取一个商品,或者刷新重新组托!");
//        }
//        // 判断是否有相同条码的数据
//        if (waitPakinService.selectCount(new EntityWrapper<WaitPakin>().eq("zpallet", param.getBarcode())
//                .eq("io_status", "N")) > 0) {
//            throw new CoolException(param.getBarcode() + "数据正在进行入库");
//        }
//
//        if (Objects.isNull(param.getBarcode())) {
//            throw new CoolException("托盘码不能为空!!");
//        }
//
//        BasContainer container = basContainerService
//                .selectOne(new EntityWrapper<BasContainer>().eq("barcode", param.getBarcode()));
//        if (Objects.isNull(container)) {
//            throw new CoolException("数据错误:容器码不存在!!");
//        }
//        // if (param.getBarcode().length() != 8) {
//        // throw new CoolException("条码长度不是8位===>>" + param.getBarcode());
//        // }
//
//        if (Objects.isNull(param.getType())) {
//            param.setType("0");
//        }
//        int countLoc = locDetlService.selectCount(new EntityWrapper<LocDetl>().eq("zpallet", param.getBarcode()));
//        int countWrk = wrkDetlService.selectCount(new EntityWrapper<WrkDetl>().eq("zpallet", param.getBarcode()));
//        int countwait = waitPakinService.selectCount(new EntityWrapper<WaitPakin>()
//                .eq(!Objects.isNull(param.getType()), "type", param.getType())
//                .eq("zpallet", param.getBarcode()));
//        if (countLoc > 0 || countWrk > 0 || countwait > 0) {
//            throw new CoolException("组托档/工作档/库存条码数据已存在===>>" + param.getBarcode());
//        }
//
//        Date now = new Date();
//        // 无单组托
//        if (Cools.isEmpty(param.getOrderNo())) {
//            // 生成入库通知档
//            List<DetlDto> detlDtos = new ArrayList<>();
//            param.getCombMats().forEach(elem -> {
//                DetlDto detlDto = new DetlDto(elem.getMatnr(), elem.getBatch(), elem.getBrand(), elem.getStandby1(),
//                        elem.getStandby2(), elem.getStandby3(),
//                        elem.getBoxType1(), elem.getBoxType2(), elem.getBoxType3(), elem.getAnfme(),
//                        elem.getThreeCode());
//                if (DetlDto.has(detlDtos, detlDto)) {
//                    DetlDto one = DetlDto.findDto(detlDtos, detlDto);
//                    assert one != null;
//                    one.setAnfme(one.getAnfme() + detlDto.getAnfme());
//                } else {
//                    detlDtos.add(detlDto);
//                }
//            });
//
//            for (DetlDto detlDto : detlDtos) {
//                Mat mat = matService.selectByMatnr(detlDto.getMatnr());
//                if (Cools.isEmpty(mat)) {
//                    throw new CoolException(detlDto.getMatnr() + "商品档案不存在");
//                }
//                if (mat.getUpQty().compareTo(detlDto.getAnfme()) < 0) {
//                    throw new CoolException("物料:" + detlDto.getMatnr() + "单次最大组托上限为:" + mat.getUpQty());
//                }
//                WaitPakin waitPakin = new WaitPakin();
//                BeanUtils.copyProperties(mat, waitPakin);
//                waitPakin.setBatch(detlDto.getBatch());
//                waitPakin.setZpallet(param.getBarcode());
//                waitPakin.setIoStatus("N");
//                waitPakin.setAnfme(detlDto.getAnfme());
//                waitPakin.setThreeCode(detlDto.getThreeCode());
//                waitPakin.setStatus("Y");
//                waitPakin.setSuppCode(detlDto.getStandby1());
//                waitPakin.setStandby1(detlDto.getStandby1());
//                waitPakin.setStandby2(detlDto.getStandby2());
//                waitPakin.setStandby3(detlDto.getStandby3());
//                waitPakin.setBoxType1(detlDto.getBoxType1());
//                waitPakin.setBoxType2(detlDto.getBoxType2());
//                waitPakin.setBoxType3(detlDto.getBoxType3());
//                waitPakin.setAppeUser(userId);
//                waitPakin.setAppeTime(now);
//                waitPakin.setModiUser(userId);
//                waitPakin.setModiTime(now);
//                if (!waitPakinService.insert(waitPakin)) {
//                    throw new CoolException("保存入库通知档失败");
//                }
//            }
//            // 关联组托
//        } else {
//            // Order order = orderService.selectByNo(param.getOrderNo());
//            // 生成入库通知档
//            List<DetlDto> detlDtos = new ArrayList<>();
//            param.getCombMats().forEach(elem -> {
//                Order order = OrderInAndOutUtil.selectByNo(Boolean.TRUE, elem.getOrderNo());
//                if (Cools.isEmpty(order) || order.getSettle() > 2) {
//                    throw new CoolException("单据编号已过期");
//                }
//                // 订单明细数量校验
//                // OrderDetl orderDetl = OrderInAndOutUtil.selectItem(Boolean.TRUE,
//                // order.getId(), elem.getMatnr(), elem.getBatch(), elem.getBrand(),
//                // elem.getStandby1(), elem.getStandby2(), elem.getStandby3(),
//                // elem.getBoxType1(), elem.getBoxType2(), elem.getBoxType3());
//                OrderDetlPakin detls = orderDetlPakinService.selectOne(new EntityWrapper<OrderDetlPakin>()
//                        .eq("order_id", order.getId())
//                        .eq("matnr", elem.getMatnr()));
//
//                if (Objects.isNull(detls)) {
//                    throw new CoolException("数据错误:单据明细不存在!!");
//                }
//
//                if (elem.getAnfme() > detls.getEnableQty()) {
//                    throw new CoolException(detls.getMatnr() + "入库数量不合规则");
//                }
//                OrderInAndOutUtil.increaseWorkQty(Boolean.TRUE, order.getId(), elem.getMatnr(), elem.getBatch(),
//                        elem.getBrand(), elem.getStandby1(), elem.getStandby2(), elem.getStandby3(),
//                        elem.getBoxType1(), elem.getBoxType2(), elem.getBoxType3(), elem.getAnfme());
//                DetlDto detlDto = new DetlDto(order.getOrderNo(), elem.getMatnr(), elem.getBatch(), elem.getBrand(), elem.getStandby1(),
//                        elem.getStandby2(), elem.getStandby3(),
//                        elem.getBoxType1(), elem.getBoxType2(), elem.getBoxType3(), elem.getAnfme(),
//                        elem.getThreeCode());
//                detlDto.setOrderId(order.getId());
//                detlDto.setOrderNo(order.getOrderNo());
//                if (DetlDto.hasOrder(detlDtos, detlDto)) {
//                    DetlDto one = DetlDto.findDto(detlDtos, detlDto);
//                    assert one != null;
//                    one.setAnfme(one.getAnfme() + detlDto.getAnfme());
//                } else {
//                    detlDtos.add(detlDto);
//                }
//            });
//
//            Set<String> matnrs = detlDtos.stream().map(DetlDto::getMatnr).collect(Collectors.toSet());
//            List<Mat> mats = matService.selectList(new EntityWrapper<Mat>().in("matnr", matnrs));
//            Set<Long> tagIds = mats.stream().map(Mat::getTagId).collect(Collectors.toSet());
//            if (tagIds.size() > 1) {
//                throw new CoolException("组托物料类型不一致,只有相同的物料分类才可以组托!!");
//            }
//
//            // BasContainer container = basContainerService.selectOne(new
//            // EntityWrapper<BasContainer>().eq("barcode", param.getBarcode()));
//            // if (Objects.isNull(container)) {
//            // throw new CoolException("数据错误:容器码不存在!!");
//            // }
//            // if (container.getMixMax() < detlDtos.size()) {
//            // throw new CoolException("超出容器最大混装数量,当前容器最大数量为:" + container.getMixMax() +
//            // "!!");
//            // }
//            // 还可以放入多少种物料
//            // Integer suplus = container.getMixMax();
//            for (DetlDto detlDto : detlDtos) {
//                Mat mat = matService.selectByMatnr(detlDto.getMatnr());
//                if (Cools.isEmpty(mat)) {
//                    throw new CoolException(detlDto.getMatnr() + "商品档案不存在");
//                }
//                // //最多可放数量
//                // Double singleMax = mat.getUpQty() * suplus;
//                // if (singleMax.compareTo(detlDto.getAnfme()) < 0) {
//                // throw new CoolException("物料:" + detlDto.getMatnr() + "单次组托上限为:" +
//                // mat.getUpQty() + ",当前总量超出托盘装载上限!!");
//                // }
//                // BigDecimal decimal = new BigDecimal(detlDto.getAnfme() / mat.getUpQty());
//                // //当前物料需要占用料箱格数
//                // Integer curr = decimal.setScale(0, RoundingMode.CEILING).intValue();
//                // suplus = suplus - curr;
//                // if (suplus < 0) {
//                // throw new CoolException("物料:" + detlDto.getMatnr() + ", 超出当前托盘装载上限!!");
//                // }
//
//                WaitPakin waitPakin = new WaitPakin();
//                BeanUtils.copyProperties(mat, waitPakin);
//                waitPakin.setOrderNo(detlDto.getOrderNo()); // 单据编号
//                waitPakin.setType(param.getType());
//                waitPakin.setOrderId(detlDto.getOrderId());
//                waitPakin.setBatch(detlDto.getBatch()); // 序列码
//                waitPakin.setZpallet(param.getBarcode()); // 托盘码
//                waitPakin.setThreeCode(detlDto.getThreeCode());
//                waitPakin.setSuppCode(detlDto.getStandby1());
//                waitPakin.setStandby1(detlDto.getStandby1());
//                waitPakin.setIoStatus("N"); // 入出状态
//                waitPakin.setAnfme(detlDto.getAnfme()); // 数量
//                waitPakin.setStatus("Y"); // 状态
//                waitPakin.setAppeUser(userId);
//                waitPakin.setAppeTime(now);
//                waitPakin.setModiUser(userId);
//                waitPakin.setModiTime(now);
//                if (!waitPakinService.insert(waitPakin)) {
//                    throw new CoolException("保存入库通知档失败");
//                }
//            }
//
//            Set<String> stringSet = param.getCombMats().stream().map(CombParam.CombMat::getOrderNo)
//                    .collect(Collectors.toSet());
//            stringSet.forEach(orderNo -> {
//                Order order = OrderInAndOutUtil.selectByNo(Boolean.TRUE, orderNo);
//                OrderInAndOutUtil.updateOrder(Boolean.TRUE, order.getId(), 2L, userId);
//            });
//        }
//
//    }
    // 商品上架
    @Override
@@ -924,181 +881,6 @@
    }
    @Override
    @Transactional
    public void packComb(CombParam param, Long userId) {
        if (Cools.isEmpty(param.getBarcode(), param.getCombMats())) {
            throw new CoolException(BaseRes.PARAM);
        }
        // 判断是否有相同条码的数据
        if (waitPakinService.selectCount(
                new EntityWrapper<WaitPakin>().eq("zpallet", param.getBarcode()).eq("io_status", "N")) > 0) {
            throw new CoolException(param.getBarcode() + "数据正在进行入库");
        }
        Date now = new Date();
        boolean packDown = Parameter.get().getPackDown().equals("true");
        // 无单组托
        if (Cools.isEmpty(param.getOrderNo())) {
            // 生成入库通知档
            List<DetlDto> detlDtos = new ArrayList<>();
            param.getCombMats().forEach(elem -> {
                // 打包上线数据校验
                if (packDown) {
                    Pack pack = packService.selectByBarcode(elem.getMatnr());
                    if (pack == null) {
                        throw new CoolException(elem.getMatnr() + "条码冗余,请检查!");
                    }
                    if (pack.getSettle() != 1) {
                        throw new CoolException(elem.getMatnr() + "条码已下线,请检查!");
                    }
                }
                Mat analyse = MatUtils.analyseMat(elem.getMatnr());
                // 条码、物料代码、序列号、数量
                DetlDto detlDto = new DetlDto(elem.getMatnr(), elem.getBatch(), analyse.getBrand(), elem.getStandby1(),
                        elem.getStandby2(), elem.getStandby3(),
                        elem.getBoxType1(), elem.getBoxType2(), elem.getBoxType3(), elem.getAnfme(),
                        elem.getThreeCode());
                // DetlDto detlDto = new DetlDto(elem.getMatnr(), analyse.getMatnr(),
                // analyse.getBarcode(), elem.getAnfme());
                // DetlDto detlDto = new DetlDto(elem.getMatnr(), elem.getBatch(),
                // elem.getAnfme());
                if (DetlDto.has(detlDtos, detlDto)) {
                    DetlDto one = DetlDto.findDto(detlDtos, detlDto);
                    assert one != null;
                    one.setAnfme(one.getAnfme() + detlDto.getAnfme());
                } else {
                    detlDtos.add(detlDto);
                }
            });
            if (packDown) {
                MesCombParam mesCombParam = new MesCombParam();
                mesCombParam.setZpallet(param.getBarcode());
                mesCombParam.setPakinTime(DateUtils.convert(now));
                mesCombParam.setLgort("5008");
                mesCombParam.setPlantCode("5000");
                mesCombParam.setFromCode("5012-20");
                mesCombParam.setStationCode("JJQ-PFZPDB-XX");
                for (DetlDto detlDto : detlDtos) {
                    mesCombParam.getList().add(new MesCombParam.Detl(detlDto.getOrderNo(), detlDto.getAnfme()));
                }
                String response = "";
                boolean success = false;
                try {
                    response = new HttpHandler.Builder()
                            .setUri(MesConstant.URL)
                            .setPath(MesConstant.IN_DISPATCH_RESULT)
                            .setJson(JSON.toJSONString(mesCombParam))
                            .build()
                            .doPost();
                    JSONObject jsonObject = JSON.parseObject(response);
                    if (jsonObject.getInteger("code").equals(200)) {
                        success = true;
                    } else if (jsonObject.getInteger("code").equals(500)) {
                        log.error("请求接口失败!!!url:{};request:{};response:{}", MesConstant.URL + MesConstant.IN_DISPATCH_RESULT,
                                JSON.toJSONString(mesCombParam), response);
                        throw new CoolException(jsonObject.getString("msg"));
                    } else {
                        log.error("请求接口失败!!!url:{};request:{};response:{}", MesConstant.URL + MesConstant.IN_DISPATCH_RESULT,
                                JSON.toJSONString(mesCombParam), response);
                        throw new CoolException("上报mes系统失败");
                    }
                } catch (Exception e) {
                    log.error("fail", e);
                    throw new CoolException(e.getMessage());
                } finally {
                    try {
                        // 保存接口日志
                        apiLogService.save(
                                "打包下线帮托上报",
                                MesConstant.URL + MesConstant.IN_DISPATCH_RESULT,
                                null,
                                "127.0.0.1",
                                JSON.toJSONString(mesCombParam),
                                response,
                                success);
                    } catch (Exception e) {
                        log.error("", e);
                    }
                }
            }
            // 生成入库单据
            String orderNo = "PACK" + snowflakeIdWorker.nextId();
            OpenOrderPakinParam openParam = new OpenOrderPakinParam();
            openParam.setOrderNo(orderNo);
            openParam.setOrderTime(DateUtils.convert(now));
            openParam.setOrderType("打包入库单");
            openParam.setOrderDetails(detlDtos);
            openService.pakinOrderCreate(openParam);
            // Order order = orderService.selectByNo(orderNo);
            Order order = OrderInAndOutUtil.selectByNo(Boolean.TRUE, param.getOrderNo());
            if (null == order) {
                throw new CoolException("生成单据失败");
            }
            // if (!orderService.updateSettle(order.getId(), 2L, userId)) {
            // throw new CoolException("修改单据状态失败");
            // }
            OrderInAndOutUtil.updateOrder(Boolean.TRUE, order.getId(), 2L, userId);
            // 生成入库通知档
            for (DetlDto detlDto : detlDtos) {
                // 修改作业数量 ----------------------------------------
                // 订单明细数量校验
                // OrderDetl orderDetl = orderDetlService.selectItem(order.getId(),
                // detlDto.getMatnr(), detlDto.getBatch());
                OrderDetl orderDetl = OrderInAndOutUtil.selectItem(Boolean.TRUE, order.getId(), detlDto.getMatnr(),
                        detlDto.getBatch(), detlDto.getBrand(), detlDto.getStandby1(), detlDto.getStandby2(),
                        detlDto.getStandby3(), detlDto.getBoxType1(), detlDto.getBoxType2(), detlDto.getBoxType3());
                if (detlDto.getAnfme() > orderDetl.getEnableQty()) {
                    throw new CoolException(orderDetl.getMatnr() + "入库数量不合法");
                }
                // 修改订单作业数量
                // if (!orderDetlService.increaseWorkQty(order.getId(), detlDto.getMatnr(),
                // detlDto.getBatch(), detlDto.getAnfme())) {
                // throw new CoolException("修改单据作业数量失败");
                // }
                OrderInAndOutUtil.increaseWorkQty(Boolean.TRUE, order.getId(), detlDto.getMatnr(), detlDto.getBatch(),
                        detlDto.getBrand(), detlDto.getStandby1(), detlDto.getStandby2(), detlDto.getStandby3(),
                        detlDto.getBoxType1(), detlDto.getBoxType2(), detlDto.getBoxType3(), detlDto.getAnfme());
                // 保存入库通知档
                Mat mat = matService.selectByMatnr(detlDto.getMatnr());
                if (Cools.isEmpty(mat)) {
                    throw new CoolException(detlDto.getMatnr() + "商品档案不存在");
                }
                WaitPakin waitPakin = new WaitPakin();
                BeanUtils.copyProperties(mat, waitPakin);
                waitPakin.setOrderNo(orderNo);
                waitPakin.setBatch(detlDto.getBatch());
                waitPakin.setZpallet(param.getBarcode()); // 托盘码
                waitPakin.setIoStatus("N"); // 入出状态
                waitPakin.setAnfme(detlDto.getAnfme()); // 数量
                waitPakin.setStatus("Y"); // 状态
                waitPakin.setAppeUser(userId);
                waitPakin.setAppeTime(now);
                waitPakin.setModiUser(userId);
                waitPakin.setModiTime(now);
                if (!waitPakinService.insert(waitPakin)) {
                    throw new CoolException("保存入库通知档失败");
                }
                // 修改打包数据状态
                Pack pack = packService.selectByBarcode(detlDto.getOrderNo());
                pack.setSettle(2L);
                pack.setUpdateTime(now);
                pack.setUpdateBy(userId);
                if (!packService.updateById(pack)) {
                    throw new CoolException("修改打包数据异常");
                }
            }
        }
    }
    @Override
    @Transactional
@@ -1261,39 +1043,6 @@
                .eq("loc_sts", LocStsType.LOC_STS_TYPE_O.type).orderAsc(Arrays.asList("row1"))));
    }
    /**
     * @author Ryan
     * @date 2025/9/22
     * @description: 呼叫空车
     * @version 1.0
     */
    @Override
    @Transactional
    public synchronized R callAgvMove(AgvCallParams params, Long userId) {
        if (Objects.isNull(params.getOrgSite())) {
            throw new CoolException("源站点不能为空!!");
        }
        BasAreas basAreas = basAreasService
                .selectOne(new EntityWrapper<BasAreas>().eq("whs_type_id", LocAreaType.LOC_AREA_TYPE_IN_CACHE.type));
        LocCache locCache = locCacheService.selectOne(new EntityWrapper<LocCache>()
                .eq("area_id", basAreas.getId())
                .eq("frozen", 0)
                .eq("loc_sts", LocStsType.LOC_STS_TYPE_O.type)
                // .eq("loc_no", params.getTarSite())
                .orderAsc(Arrays.asList("loc_no"))
                .last("OFFSET 0 ROWS FETCH NEXT 1 ROWS ONLY"));
        if (Objects.isNull(locCache)) {
            throw new CoolException("请检查目标库位是否闲置中!!");
        }
        try {
            generateAgvTask("agv", locCache, params.getOrgSite(), params.getBarcode(), userId);
        } catch (Exception e) {
            throw new CoolException(e.getMessage());
        }
        return R.ok();
    }
    /**
     * @author Ryan
@@ -1322,431 +1071,6 @@
        }
        return R.ok().add(detlPakins);
    }
    /**
     * @author Ryan
     * @date 2025/9/24
     * @description: AGV呼叫搬运
     * @version 1.0
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public R OutCallAgv(AgvCallParams params, Long userId) {
        BasAreas basAreas = basAreasService.selectOne(new EntityWrapper<BasAreas>().eq("whs_type_id", LocAreaType.LOC_AREA_TYPE_IN_CACHE.type));
        if (Objects.isNull(basAreas)) {
            throw new CoolException("库区不存在!!");
        }
        /**获取入库最早的一条数据**/
        List<LocCache> locCaches = locCacheService.selectList(new EntityWrapper<LocCache>()
                .eq("loc_sts", LocStsType.LOC_STS_TYPE_F.type)
                .eq("area_id", basAreas.getId())
                .eq("frozen", 0)
                .orderDesc(Arrays.asList("sort", "first_time")));
        if (locCaches.isEmpty()) {
            throw new CoolException("暂无满足需求库位!");
        }
        for (LocCache locCache : locCaches) {
            //获取缓存区信息
//            BasAreas basAreas = basAreasService.selectOne(new EntityWrapper<BasAreas>().eq("whs_type_id", LocAreaType.LOC_AREA_TYPE_IN_CACHE.type));
            List<LocDetl> locDetls = locDetlService.selectList(new EntityWrapper<LocDetl>()
                    .eq("area_id", basAreas.getId())
                    .eq("loc_no", locCache.getLocNo()));
            if (Objects.isNull(locDetls) || locDetls.isEmpty()) {
                throw new CoolException("库存明细不存在!!");
            }
            Set<String> set = locDetls.stream().map(LocDetl::getMatnr).collect(Collectors.toSet());
            List<Mat> matList = matService.selectList(new EntityWrapper<Mat>().in("matnr", set));
            if (Objects.isNull(matList) || matList.isEmpty()) {
                throw new CoolException("物料库不存在该物料!!");
            }
            Set<Long> longs = matList.stream().map(Mat::getTagId).collect(Collectors.toSet());
            if (!longs.isEmpty() && longs.size() > 1) {
                throw new CoolException("物料类型不一致,请检查数据是否正确!!");
            }
            Mat mat = matList.stream().findFirst().get();
            BasStation station = basStationService.selectOne(new EntityWrapper<BasStation>()
                    .eq("loc_sts", LocStsType.LOC_STS_TYPE_O.type)
                    .eq("ctn_type", mat.getTagId())
                    .eq("dev_no", params.getTarSite()));
            if (Objects.isNull(station)) {
                continue;
            }
            /**生成缓存区出库任务*/
            generateCacheOutTask(station, locCache, userId);
            /**生成立库出库任务*/
            generateCRNOutTask(station, locCache, userId);
            return R.ok();
        }
        return R.error("未找到与站点类型匹配的库存");
    }
    @Override
    public R callEmptyCar(AgvCallParams params, Long userId) {
        BasAreas basAreas = basAreasService.selectOne(new EntityWrapper<BasAreas>().eq("name", params.getOrgSite()));
        List<LocCache> locCaches = locCacheService.selectList(new EntityWrapper<LocCache>()
                .eq("loc_sts", LocStsType.LOC_STS_TYPE_D.type)
                .eq("area_id", basAreas.getId()));
        if (locCaches.isEmpty()) {
            throw new CoolException("暂无空板库位");
        }
        BasStation station = basStationService.selectOne(new EntityWrapper<BasStation>()
                .eq("loc_sts", LocStsType.LOC_STS_TYPE_O.type)
                .eq("dev_no", params.getTarSite()));
        if (Objects.isNull(station)) {
            throw new CoolException("站点正在执行任务!!");
        }
        generateCacheOutTask(station, locCaches.get(0), userId);
        // HIKApiDTO hikApiDTO =new HIKApiDTO()
        // .setOrg(locCaches.get(0).getLocNo())
        // .setOrgType("05")
        // .setTar(params.getTarSite())
        // .setTarType("05")
        // .setTaskType("GT5")
        // .setPriority("1")
        // .setCtnrType("2")
        // ;
        // HIKResultDTO hikResultDTO = sendAgvTask(hikApiDTO,
        // HIKApiConstant.AGV_CALL_IN_PATH);
        // if (!hikResultDTO.isSuccess()){
        // return R.error(hikResultDTO.getMessage());
        // }
        return R.ok();
    }
    @Override
    @Transactional(rollbackFor = Exception.class)
    public R collectionInCall(AgvCallParams params, Long userId) {
        if (Objects.isNull(params.getOrgSite())) {
            throw new CoolException("源站点不能为空!!");
        }
        BasAreas basAreas = basAreasService.selectOne(new EntityWrapper<BasAreas>().eq("name", params.getTarSite()));
        List<LocCache> locCaches = locCacheService.selectList(new EntityWrapper<LocCache>()
                .in("loc_sts", LocStsType.LOC_STS_TYPE_O.type)
                .eq("area_id", basAreas.getId()));
        if (locCaches.isEmpty()) {
            throw new CoolException("当前暂无空库位!!");
        }
        BasStation station = basStationService
                .selectOne(new EntityWrapper<BasStation>().eq("dev_no", params.getOrgSite()));
        if (Objects.isNull(station)) {
            throw new CoolException("源站点未检索到数据!!");
        }
        generateSOEOInTask("agv", locCaches.get(0), params.getOrgSite(), station.getBarcode(), userId);
        return R.ok();
    }
    @Transactional(rollbackFor = Exception.class)
    public void generateSOEOInTask(String type, LocCache loc, String orgSite, String barcode, Long userId) {
        List<BasStationDetl> basStationDetls = basStationDetlService
                .selectList(new EntityWrapper<BasStationDetl>().eq("dev_no", orgSite));
        if (Objects.isNull(basStationDetls) || basStationDetls.isEmpty()) {
            throw new CoolException("站点信息不存在!!");
        }
        List<Task> tasks = taskService.selectList(new EntityWrapper<Task>().eq("barcode", barcode));
        if (!tasks.isEmpty()) {
            throw new CoolException("托盘已在任务执行中..");
        }
        // 获取工作号
        int workNo = commonService.getWorkNo(WorkNoType.PICK.type);
        // 保存工作档
        Task task = new Task();
        task.setWrkNo(workNo)
                .setIoTime(new Date())
                .setWrkSts(1L) // 工作状态:11.生成出库ID
                .setIoType(1) // 入出库状态: 11.库格移载
                .setTaskType("agv")
                .setIoPri(10D)
                .setLocNo(loc.getLocNo()) // 目标库位
                .setFullPlt("Y") // 满板:Y
                .setPicking("N") // 拣料
                .setExitMk("N")// 退出
                .setSourceStaNo(orgSite)
                .setEmptyMk(loc.getLocSts().equals("D") ? "Y" : "N")// 空板
                .setBarcode(barcode)// 托盘码
                .setLinkMis("N")
                .setAppeUser(userId)
                .setAppeTime(new Date())
                .setModiUser(userId)
                .setModiTime(new Date());
        if (!taskService.insert(task)) {
            throw new CoolException("保存工作档失败");
        }
        for (BasStationDetl basStationDetl : basStationDetls) {
            TaskDetl wrkDetl = new TaskDetl();
            BeanUtils.copyProperties(basStationDetl, wrkDetl);
            wrkDetl.setWrkNo(workNo)
                    .setIoTime(new Date())
                    .setOrderNo(basStationDetl.getOrderNo())
                    .setAnfme(basStationDetl.getAnfme())
                    .setZpallet(basStationDetl.getBarcode())
                    .setBatch(basStationDetl.getBatch())
                    .setMatnr(basStationDetl.getMatnr())
                    .setMaktx(basStationDetl.getMaktx())
                    .setStandby1(basStationDetl.getStandby1())
                    .setAppeUser(userId)
                    .setUnit(basStationDetl.getUnit())
                    .setModel(basStationDetl.getModel())
                    .setAppeTime(new Date())
                    .setModiUser(userId);
            // 保存工作档明细
            if (!taskDetlService.insert(wrkDetl)) {
                throw new CoolException("保存工作档明细失败");
            }
        }
        // 修改目标库位状态
        if (loc.getLocSts().equals(LocStsType.LOC_STS_TYPE_O.type)) {
            loc.setLocSts(LocStsType.LOC_STS_TYPE_S.type); // S.入库预约
            loc.setModiTime(new Date());
            loc.setModiUser(userId);
            if (!locCacheService.updateById(loc)) {
                throw new CoolException("更新目标库位状态失败");
            }
        } else {
            throw new CoolException("移转失败,目标库位状态:" + loc.getLocSts$());
        }
        // 修改目标站点信息
        BasStation station = basStationService.selectOne(new EntityWrapper<BasStation>().eq("dev_no", orgSite));
        if (Objects.isNull(station)) {
            throw new CoolException("站点不存在!!");
        }
        if (station.getLocSts().equals(LocStsType.LOC_STS_TYPE_F.type)) {
            station.setLocSts("R"); // S.入库预约
            station.setBarcode(barcode);
            station.setModiTime(new Date());
            station.setModiUser(userId);
            if (!basStationService.updateById(station)) {
                throw new CoolException("更新目标站点状态失败");
            }
        } else {
            throw new CoolException("移转失败,目标库位状态:" + station.getLocSts());
        }
    }
    /**
     * 缓存出库组托上架
     *
     * @param params
     * @return com.core.common.R
     * @author Ryan
     * @date 2025/12/8 13:47
     */
    @Override
    public R combOutPub(AgvCallParams params) {
        return R.ok();
    }
    /**
     * 缓存组托数据
     *
     * @param params
     * @return com.core.common.R
     * @author Ryan
     * @date 2025/12/8 15:37
     */
    @Override
    public R combCacehList(AgvCallParams params) {
        if (Objects.isNull(params.getCarBarcode())) {
            throw new CoolException("台车托盘码不能为空!!");
        }
        if (Objects.isNull(params.getBsCode())) {
            throw new CoolException("BS码不能为空!!");
        }
//        Task task = taskService.selectOne(new EntityWrapper<Task>()
//                .eq("io_type", TaskIOType.PICK_OUT.type)
//                .eq("wrk_sts", 14)
//                .eq("barcode", params.getCarBarcode()));
//        if (Objects.isNull(task)) {
//            throw new CoolException("数据错误:任务信息存在!!");
//        }
        List<BasStationDetl> taskDetl = basStationDetlService.selectList(new EntityWrapper<BasStationDetl>()
                .eq("three_code", params.getBsCode())
                .eq("zpallet", params.getCarBarcode()));
        if (Objects.isNull(taskDetl) || taskDetl.isEmpty()) {
            throw new CoolException("当前BS编码不存在!!");
        }
        return R.ok().add(taskDetl);
    }
    /**
     * 缓存区上架组托
     *
     * @param param
     * @param userId
     * @author Ryan
     * @date 2025/12/9 13:45
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void cacheComb(CombParam param, Long userId) {
        if (Objects.isNull(param.getBarcode())) {
            throw new CoolException("托盘码不能为空!!");
        }
        if (Objects.isNull(param.getCombMats()) || param.getCombMats().isEmpty()) {
            throw new CoolException("组托明细不能为空!!");
        }
        // 判断是否有相同条码的数据
        if (waitPakinService.selectCount(new EntityWrapper<WaitPakin>().eq("zpallet", param.getBarcode())
                .eq("io_status", "N")) > 0) {
            throw new CoolException(param.getBarcode() + "数据正在进行入库");
        }
        int countLoc = locDetlService.selectCount(new EntityWrapper<LocDetl>().eq("zpallet", param.getBarcode()));
        int countWrk = wrkDetlService.selectCount(new EntityWrapper<WrkDetl>().eq("zpallet", param.getBarcode()));
        if (countWrk > 0 || countLoc > 0) {
            WrkMast wrkDetl = wrkMastService.selectOne(new EntityWrapper<WrkMast>().eq("barcode", param.getBarcode()));
            if (!wrkDetl.getIoType().equals(TaskIOType.MERGE_OUT.type)) {
                throw new CoolException("组托档/工作档/库存条码数据已存在===>>" + param.getBarcode());
            }
        }
        int countwait = waitPakinService.selectCount(new EntityWrapper<WaitPakin>().eq("zpallet", param.getBarcode()));
        if (countwait > 0) {
            throw new CoolException("组托档/工作档/库存条码数据已存在===>>" + param.getBarcode());
        }
        // 生成入库通知档
        List<DetlDto> detlDtos = new ArrayList<>();
        param.getCombMats().forEach(elem -> {
            DetlDto detlDto = new DetlDto(elem.getMatnr(), elem.getBatch(), elem.getBrand(), elem.getStandby1(),
                    elem.getStandby2(), elem.getStandby3(),
                    elem.getBoxType1(), elem.getBoxType2(), elem.getBoxType3(), elem.getAnfme(), elem.getThreeCode());
            if (DetlDto.has(detlDtos, detlDto)) {
                DetlDto one = DetlDto.findDto(detlDtos, detlDto);
                assert one != null;
                one.setAnfme(one.getAnfme() + detlDto.getAnfme());
            } else {
                detlDtos.add(detlDto);
            }
        });
        BasContainer container = basContainerService
                .selectOne(new EntityWrapper<BasContainer>().eq("barcode", param.getBarcode()));
        if (Objects.isNull(container)) {
            throw new CoolException("数据错误:容器码不存在!!");
        }
        if (container.getMixMax() < detlDtos.size()) {
            throw new CoolException("超出容器最大混装数量,当前容器最大数量为:" + container.getMixMax() + "!!");
        }
        Set<String> matnrs = detlDtos.stream().map(DetlDto::getMatnr).collect(Collectors.toSet());
        List<Mat> mats = matService.selectList(new EntityWrapper<Mat>().in("matnr", matnrs));
        Set<Long> tagIds = mats.stream().map(Mat::getTagId).collect(Collectors.toSet());
        if (tagIds.size() > 1) {
            throw new CoolException("组托物料类型不一致,只有相同的物料分类才可以组托!!");
        }
        // 还可以放入多少种物料
        Double suplus = container.getMixMax();
        Date now = new Date();
        for (DetlDto detlDto : detlDtos) {
            Mat mat = matService.selectByMatnr(detlDto.getMatnr());
            if (Cools.isEmpty(mat)) {
                throw new CoolException(detlDto.getMatnr() + "商品档案不存在");
            }
            // 最多可放数量
            Double singleMax = mat.getUpQty() * suplus;
            if (singleMax.compareTo(detlDto.getAnfme()) < 0) {
                throw new CoolException("物料:" + detlDto.getMatnr() + "单次组托上限为:" + mat.getUpQty() + ",当前总量超出托盘装载上限!!");
            }
            BigDecimal decimal = new BigDecimal(detlDto.getAnfme() / mat.getUpQty());
            // 当前物料需要占用料箱格数
            Integer curr = decimal.setScale(0, RoundingMode.CEILING).intValue();
            suplus = suplus - curr;
            if (suplus < 0) {
                throw new CoolException("物料:" + detlDto.getMatnr() + ", 超出当前托盘装载上限!!");
            }
            BasStationDetl stationDetl = basStationDetlService.selectOne(new EntityWrapper<BasStationDetl>()
                    .eq("zpallet", param.getCarBarcode())
                    .eq("three_code", detlDto.getThreeCode()));
            if (Objects.isNull(stationDetl)) {
                throw new CoolException("明细内容不存在!!");
            }
            Double v = Math.round((stationDetl.getAnfme() - stationDetl.getWorkQty()) * 10000) / 10000.0;
            if (detlDto.getAnfme().compareTo(v) > 0) {
                throw new CoolException("组托上限为:" + stationDetl.getAnfme() + ", 已组托:" + stationDetl.getWorkQty() + ", 还可组托:" + v);
            }
            WaitPakin waitPakin = new WaitPakin();
            BeanUtils.copyProperties(mat, waitPakin);
            waitPakin.setBatch(detlDto.getBatch());
            waitPakin.setType(param.getType());
            waitPakin.setZpallet(param.getBarcode());
            // 设置组托类型
            waitPakin.setType(param.getType());
            waitPakin.setIoStatus("N");
            waitPakin.setAnfme(detlDto.getAnfme());
            waitPakin.setThreeCode(detlDto.getThreeCode());
            waitPakin.setStatus("Y");
            waitPakin.setSuppCode(detlDto.getStandby1());
            waitPakin.setStandby1(detlDto.getStandby1());
            waitPakin.setStandby2(detlDto.getStandby2());
            waitPakin.setStandby3(detlDto.getStandby3());
            waitPakin.setBoxType1(detlDto.getBoxType1());
            waitPakin.setBoxType2(detlDto.getBoxType2());
            waitPakin.setBoxType3(detlDto.getBoxType3());
            waitPakin.setAppeUser(userId);
            waitPakin.setAppeTime(now);
            waitPakin.setModiUser(userId);
            waitPakin.setModiTime(now);
            if (!waitPakinService.insert(waitPakin)) {
                throw new CoolException("保存入库通知档失败");
            }
            Double v1 = Math.round((stationDetl.getWorkQty() + detlDto.getAnfme()) * 10000) / 10000.0;
            stationDetl.setWorkQty(v1);
            if (!basStationDetlService.updateById(stationDetl)) {
                throw new CoolException("站点明细修改失败");
            }
            if (v1.compareTo(stationDetl.getAnfme()) >= 0) {
                if (!basStationDetlService.deleteById(stationDetl)) {
                    throw new CoolException("原始数据移除失败!");
                }
            }
            List<BasStationDetl> stas = basStationDetlService.selectList(new EntityWrapper<BasStationDetl>()
                    .eq("dev_no", stationDetl.getDevNo()));
            if (Objects.isNull(stas) || stas.isEmpty()) {
                BasStation station = basStationService.selectOne(new EntityWrapper<BasStation>().eq("dev_no", stationDetl.getDevNo()));
                if (Objects.isNull(station)) {
                    throw new CoolException("数据错误,站点不存在!!");
                }
                station.setLocSts(LocStsType.LOC_STS_TYPE_O.type);
                station.setBarcode("");
                if (!basStationService.updateById(station)) {
                    throw new CoolException("站点状态更新失败!!");
                }
            }
        }
    }
    /**
@@ -1779,18 +1103,18 @@
//            throw new CoolException("数据错误,任务明细不存在!!");
//        }
        BasStation station = basStationService.selectOne(new EntityWrapper<BasStation>()
                .eq("dev_no", param.getStation()));
        if (Objects.isNull(station)) {
            throw new CoolException("站点不存在!!");
        }
        station.setLocSts(LocStsType.LOC_STS_TYPE_O.type);
        station.setBarcode("");
        if (!basStationService.updateById(station)) {
            throw new CoolException("站点状态修改失败!");
        }
        basStationDetlService.delete(new EntityWrapper<BasStationDetl>().eq("dev_no", station.getDevNo()));
//        BasStation station = basStationService.selectOne(new EntityWrapper<BasStation>()
//                .eq("dev_no", param.getStation()));
//        if (Objects.isNull(station)) {
//            throw new CoolException("站点不存在!!");
//        }
//
//        station.setLocSts(LocStsType.LOC_STS_TYPE_O.type);
//        station.setBarcode("");
//        if (!basStationService.updateById(station)) {
//            throw new CoolException("站点状态修改失败!");
//        }
//        basStationDetlService.delete(new EntityWrapper<BasStationDetl>().eq("dev_no", station.getDevNo()));
//        // 任务完成
//        task.setWrkSts(15L);
@@ -1805,132 +1129,7 @@
            throw new CoolException("数据错误,组拖档已不存在!!");
        }
        generateCrnInTask(waitPakins, station, param.getLocType1(), userId);
    }
    /**
     * SO/EO 区组托
     * @author Ryan
     * @date 2025/12/18 15:19
     * @param params
     * @return com.core.common.R
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public R collectionPakin(CollectionPakinParams params, Long userId) {
        List<BasStation> basStations = basStationService
                .selectList(new EntityWrapper<BasStation>().eq("barcode", params.getTarBarcode()));
        if (basStations.isEmpty() && Cools.isEmpty(params.getDevNo())) {
            throw new CoolException("请输入站点号");
        }
        BasStation basStation = null;
        if (!basStations.isEmpty()) {
            basStation = basStations.get(0);
        } else {
            basStation = basStationService.selectOne(new EntityWrapper<BasStation>().eq("dev_no", params.getDevNo()));
        }
        if (Cools.isEmpty(basStation)) {
            throw new CoolException("请输入正确的站点号");
        }
        for (CollectionPakinParams.CombMat combMat : params.getCombMats()) {
            if (combMat.getWorkQty().compareTo(BigDecimal.ZERO) == 0) {
                continue;
            }
            OrderPakin orderPakin = orderPakinService.selectByNo(combMat.getOrderNo());
            if (Cools.isEmpty(orderPakin)) {
                throw new CoolException("未找到备货入库单");
            }
            UpdateDetailsOfPakin(orderPakin, combMat);
            BasStationDetl basStationDetl = basStationDetlService.selectOne(new EntityWrapper<BasStationDetl>()
                    .eq("dev_no", basStation.getDevNo())
                    .eq("matnr", combMat.getMatnr())
                    .eq("batch", combMat.getBatch())
                    .eq("standby1", combMat.getStandby1()));
            if (Cools.isEmpty(basStationDetl)) {
                basStationDetl = new BasStationDetl();
                Mat mat = matService.selectByMatnr(combMat.getMatnr());
                BeanUtils.copyProperties(mat, basStationDetl);
                basStationDetl
                        .setId(null)
                        .setOrderNo(orderPakin.getOrderNo())
                        .setDevNo(basStation.getDevNo())
                        .setBarcode(params.getTarBarcode())
                        .setZpallet(params.getTarBarcode())
                        .setBatch(combMat.getBatch())
                        .setSuppCode(combMat.getStandby1())
                        .setStandby1(combMat.getStandby1())
                        .setAnfme(combMat.getWorkQty().doubleValue());
                if (!basStationDetlService.insert(basStationDetl)) {
                    throw new CoolException("插入明细失败");
                }
            } else {
                BigDecimal anfme = new BigDecimal(basStationDetl.getAnfme().toString()).add(combMat.getWorkQty());
                basStationDetl.setAnfme(anfme.doubleValue());
                if (!basStationDetlService.updateById(basStationDetl)) {
                    throw new CoolException("更新明细数据失败");
                }
            }
        }
        if (basStation.getLocSts().equals(LocStsType.LOC_STS_TYPE_D.type)
                || basStation.getLocSts().equals(LocStsType.LOC_STS_TYPE_O.type)) {
            basStation.setBarcode(params.getTarBarcode());
            basStation.setLocSts(LocStsType.LOC_STS_TYPE_F.type);
            basStationService.updateById(basStation);
        }
        return R.ok();
    }
    @Transactional(rollbackFor = Exception.class)
    public void UpdateDetailsOfPakin(OrderPakin orderPakin, CollectionPakinParams.CombMat combMat) {
        OrderDetlPakin orderDetlPakin = orderDetlPakinService.selectOne(new EntityWrapper<OrderDetlPakin>()
                .eq("order_id", orderPakin.getId())
                .eq("matnr", combMat.getMatnr())
                .eq("batch", combMat.getBatch())
                .eq("standby1", combMat.getStandby1()));
        if (Cools.isEmpty(orderDetlPakin)) {
            throw new CoolException("检索单据明细数据失败");
        }
        if (orderDetlPakin.getWorkQty() >= orderDetlPakin.getAnfme()) {
            throw new CoolException("请勿超出订单明细数量");
        }
        BigDecimal workQty = new BigDecimal(orderDetlPakin.getWorkQty().toString()).add(combMat.getWorkQty());
        orderDetlPakin.setWorkQty(workQty.doubleValue());
        if (!orderDetlPakinService.updateById(orderDetlPakin)) {
            throw new CoolException("更新单据明细数据失败");
        }
        if (!orderPakin.getSettle().equals(2L)) {
            orderPakin.setSettle(2L);
            if (!orderPakinService.updateById(orderPakin)) {
                throw new CoolException("更新单据状态失败");
            }
        }
    }
    @Override
    public R collectionPakinView(CollectionPakinParams params) {
        WrkMast wrkMast = wrkMastService.selectByBarcode(params.getOrgBarcode());
        if (Cools.isEmpty(wrkMast)) {
            throw new CoolException("未找到任务");
        }
        if (wrkMast.getIoType().equals(TaskIOType.ALL_OUT.type)
                && wrkMast.getIoType().equals(TaskIOType.PICK_OUT.type)) {
            throw new CoolException("人物类型错误");
        }
        List<WrkDetl> wrkDetls = wrkDetlService.selectByWrkNo(wrkMast.getWrkNo());
        if (Cools.isEmpty(wrkDetls)) {
            throw new CoolException("未找到任务明细");
        }
        return R.ok(wrkDetls);
        generateCrnInTask(waitPakins, param.getStation(), param.getLocType1(), userId);
    }
    /**
@@ -1941,18 +1140,17 @@
     * @date 2025/12/9 13:54
     */
    @Transactional(rollbackFor = Exception.class)
    public void generateCrnInTask(List<WaitPakin> waitPakins, BasStation station, Short locType, Long userId) {
    public void generateCrnInTask(List<WaitPakin> waitPakins, String station, Short locType, Long userId) {
        Date now = new Date();
        StationRela rela = stationRelaService.selectOne(new EntityWrapper<StationRela>()
                .eq("agv_sta", station.getDevNo())
                .orderAsc(Arrays.asList("crn_sta"))
                .last("OFFSET 0 ROWS FETCH NEXT 1 ROWS ONLY"));
        if (Objects.isNull(rela)) {
            throw new CoolException("未绑定作业站点!!");
        }
        BasDevp sourceStaNo = basDevpService.checkSiteStatus(Integer.parseInt(rela.getCrnSta()), true);
//        StationRela rela = stationRelaService.selectOne(new EntityWrapper<StationRela>()
//                .eq("agv_sta", station.getDevNo())
//                .orderAsc(Arrays.asList("crn_sta"))
//                .last("OFFSET 0 ROWS FETCH NEXT 1 ROWS ONLY"));
//
//        if (Objects.isNull(rela)) {
//            throw new CoolException("未绑定作业站点!!");
//        }
        BasDevp sourceStaNo = basDevpService.checkSiteStatus(Integer.parseInt(station), true);
        sourceStaNo.setLocType1(locType);
        LocTypeDto locTypeDto = new LocTypeDto(sourceStaNo);
        WaitPakin pakin = waitPakins.stream().findFirst().get();
@@ -1961,7 +1159,7 @@
        WrkMast mast = wrkMastService.selectOne(new EntityWrapper<WrkMast>().eq("barcode", pakin.getZpallet()));
        WrkMast wrkMast = new WrkMast();
        if (!Objects.isNull(mast)) {
            if (mast.getIoType().equals(TaskIOType.MERGE_OUT.type) || mast.getIoType().equals(TaskIOType.PICK_OUT.type) || mast.getIoType().equals(TaskIOType.CHECK_OUT.type)) {
            if (mast.getIoType().equals(110) || mast.getIoType().equals(103) || mast.getIoType().equals(107)) {
                int ioType = mast.getIoType() - 50;
                mast.setIoType(ioType);
                mast.setWrkSts(2L);
@@ -2068,233 +1266,233 @@
//        }
    }
    /**
     * 生成立库出库任务
     * 台车过来默认是同一种物料类型
     *
     * @param locCaches
     * @author Ryan
     * @date 2025/12/3 8:06
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void generateCRNOutTask(BasStation station, LocCache locCaches, Long userId) {
        if (Objects.isNull(locCaches)) {
            throw new CoolException("库位不能为空!!");
        }
        // 获取缓存区信息
        BasAreas basAreas = basAreasService
                .selectOne(new EntityWrapper<BasAreas>().eq("whs_type_id", LocAreaType.LOC_AREA_TYPE_IN_CACHE.type));
        if (Objects.isNull(basAreas)) {
            throw new CoolException("库区不存在!!");
        }
        List<LocDetl> locDetls = locDetlService.selectList(new EntityWrapper<LocDetl>()
                .eq("area_id", basAreas.getId())
                .eq("loc_no", locCaches.getLocNo()));
        if (Objects.isNull(locDetls) || locDetls.isEmpty()) {
            throw new CoolException("库存明细不存在!!");
        }
//    /**
//     * 生成立库出库任务
//     * 台车过来默认是同一种物料类型
//     *
//     * @param locCaches
//     * @author Ryan
//     * @date 2025/12/3 8:06
//     */
//    @Override
//    @Transactional(rollbackFor = Exception.class)
//    public void generateCRNOutTask(BasStation station, LocCache locCaches, Long userId) {
//        if (Objects.isNull(locCaches)) {
//            throw new CoolException("库位不能为空!!");
//        }
//        // 获取缓存区信息
//        BasAreas basAreas = basAreasService
//                .selectOne(new EntityWrapper<BasAreas>().eq("whs_type_id", LocAreaType.LOC_AREA_TYPE_IN_CACHE.type));
//        if (Objects.isNull(basAreas)) {
//            throw new CoolException("库区不存在!!");
//        }
//        List<LocDetl> locDetls = locDetlService.selectList(new EntityWrapper<LocDetl>()
//                .eq("area_id", basAreas.getId())
//                .eq("loc_no", locCaches.getLocNo()));
//        if (Objects.isNull(locDetls) || locDetls.isEmpty()) {
//            throw new CoolException("库存明细不存在!!");
//        }
//
//        // Set<String> set =
//        // locDetls.stream().map(LocDetl::getMatnr).collect(Collectors.toSet());
//        // List<Mat> matList = matService.selectList(new
//        // EntityWrapper<Mat>().in("matnr", set));
//        //
//        // if (Objects.isNull(matList) || matList.isEmpty()) {
//        // throw new CoolException("物料库不存在该物料!!");
//        // }
//        // Set<Long> longs =
//        // matList.stream().map(Mat::getTagId).collect(Collectors.toSet());
//        // if (!longs.isEmpty() && longs.size() > 1) {
//        // throw new CoolException("物料类型不一致,请检查数据是否正确!!");
//        // }
//
//        // 获取立库区信息
//        BasAreas one = basAreasService
//                .selectOne(new EntityWrapper<BasAreas>().eq("whs_type_id", LocAreaType.LOC_AREA_TYPE_CRN.type));
//        if (Objects.isNull(one)) {
//            throw new CoolException("数据错误:库区不存在!!");
//        }
//
//        List<StationRela> relas = stationRelaService
//                .selectList(new EntityWrapper<StationRela>().eq("agv_sta", station.getDevNo()));
//        if (Objects.isNull(relas) || relas.isEmpty()) {
//            throw new CoolException("站点未关联堆垛机作业站点!!");
//        }
//
//        Set<String> crnStas = relas.stream().map(StationRela::getCrnSta).collect(Collectors.toSet());
//
//        List<BasDevp> devps = basDevpService.selectList(
//                new EntityWrapper<BasDevp>().in("dev_no", crnStas).eq("loading", CommonStation.COMMON_STATION_Y.type));
//        if (Objects.isNull(devps) || devps.isEmpty()) {
//            throw new CoolException("无站点可用!");
//        }
//        Collections.shuffle(devps);
//        BasDevp basDevp = devps.stream().findFirst().get();
//        List<CrnTaskDetlDTO> locMasts = new ArrayList<>();
//        Map<String, List<LocDetl>> listMap = locDetls.stream().collect(Collectors.groupingBy(LocDetl::getMatnr));
//        //获取查库存总数量
//        AtomicReference<Double> totalOut = new AtomicReference<>(0.0);
//        listMap.forEach((matnr, detls) -> {
//            Map<String, List<LocDetl>> supIds = detls.stream().collect(Collectors.groupingBy(LocDetl::getStandby1));
//            supIds.forEach((supId, sups) -> {
//                // 根据supId(供应商)分类,得到需要存放的总空库存数量
//                Double sum = sups.stream().mapToDouble(LocDetl::getAnfme).sum();
//                AtomicReference<Double> itemed = new AtomicReference<>(0.0);
//                // 获取当前供应商+ 物料在库
//                List<LocDetl> detlList = locDetlService.selectList(new EntityWrapper<LocDetl>()
//                        .eq("matnr", matnr)
//                        .eq("area_id", one.getId())
//                        .eq("standby1", supId).orderAsc(Arrays.asList("appe_time")));
//                // TODO 判断是否有新库位,没有新库位,再找有空格的位置放 1. 判断当前物料是否有库存 2. 没有余料查询新库位
//                if (!Objects.isNull(detlList) && !detlList.isEmpty()) {
//                    Map<String, List<LocDetl>> locMaps = detlList.stream()
//                            .collect(Collectors.groupingBy(LocDetl::getLocNo));
//                    locMaps.forEach((locNo, adetls) -> {
//                        LocMast locMast = locMastService.selectById(locNo);
//                        if (Objects.isNull(locMast)) {
//                            throw new CoolException("数据错误,库位信息不存在!!");
//                        }
//                        BasContainer container = basContainerService
//                                .selectOne(new EntityWrapper<BasContainer>().eq("barcode", locMast.getBarcode()));
//                        if (Objects.isNull(container)) {
//                            throw new CoolException("数据错误,容器不存在!!");
//                        }
//                        Set<String> sets = adetls.stream().map(LocDetl::getMatnr).collect(Collectors.toSet());
//                        // 判断容器是否还可混放,及当前物料可放多少
//                        if (container.getMixMax() > sets.size()) {
//                            //余下可混放种类
//                            long suplus = Math.round((container.getMixMax() - sets.size()) * 10000) / 10000;
//                            Mat mats = matService.selectOne(new EntityWrapper<Mat>().eq("matnr", matnr));
//                            if (Objects.isNull(mats)) {
//                                throw new CoolException("物料不存在!!");
//                            }
//                            //余下最大可放数量
//                            Double v = Math.round((mats.getUpQty() * suplus) * 10000) / 10000.0;
//                            // 入库数量小于零且入库数量减去已计划出数量大于零()
//                            if (sum.compareTo(v) <= 0 && sum.compareTo(itemed.get()) > 0) {
//                                itemed.set(Math.round((itemed.get() + v) * 10000) / 10000.0);
//                                // 可放下
//                                CrnTaskDetlDTO crnTaskDetlDTO = new CrnTaskDetlDTO().setLocNo(locMast.getLocNo()).setLocDetlList(adetls);
//                                locMasts.add(crnTaskDetlDTO);
//                                totalOut.set(Math.round((totalOut.get() + itemed.get()) * 10000) / 10000.0);
//                            }
//                        }
//                    });
//                }
//            });
//        });
//
//        // 判断根据分拣出库后,还需要出多少托盘或料箱;如果余料大于0, 出新托盘或料箱
//        if (totalOut.get().compareTo(0.0) > 0) {
//            generateTask(locMasts, TaskIOType.MERGE_OUT.type, basDevp, userId, station);
//            //TODO 细化区分,当前出库托盘是满足拣货数量
//        } else {
//            LocMast locMast = locMastService.selectOne(new EntityWrapper<LocMast>()
//                    // todo 根据物料种类,区分库区类型
//                    .eq("loc_sts", LocStsType.LOC_STS_TYPE_D.type)
//                    .orderAsc(Arrays.asList("row1", "bay1", "lev1"))
//                    .last("OFFSET 0 ROWS FETCH NEXT 1 ROWS ONLY"));
//
//            if (Objects.isNull(locMast)) {
//                throw new CoolException("未查询到可用的空板信息!!");
//            }
//
//            CrnTaskDetlDTO crnTaskDetlDTO = new CrnTaskDetlDTO().setLocNo(locMast.getLocNo()).setLocDetlList(locDetls);
//            locMasts.add(crnTaskDetlDTO);
//
//            // 空板出库
//            generateTask(locMasts, TaskIOType.MERGE_OUT.type, basDevp, userId, station);
//        }
//    }
        // Set<String> set =
        // locDetls.stream().map(LocDetl::getMatnr).collect(Collectors.toSet());
        // List<Mat> matList = matService.selectList(new
        // EntityWrapper<Mat>().in("matnr", set));
        //
        // if (Objects.isNull(matList) || matList.isEmpty()) {
        // throw new CoolException("物料库不存在该物料!!");
        // }
        // Set<Long> longs =
        // matList.stream().map(Mat::getTagId).collect(Collectors.toSet());
        // if (!longs.isEmpty() && longs.size() > 1) {
        // throw new CoolException("物料类型不一致,请检查数据是否正确!!");
        // }
        // 获取立库区信息
        BasAreas one = basAreasService
                .selectOne(new EntityWrapper<BasAreas>().eq("whs_type_id", LocAreaType.LOC_AREA_TYPE_CRN.type));
        if (Objects.isNull(one)) {
            throw new CoolException("数据错误:库区不存在!!");
        }
        List<StationRela> relas = stationRelaService
                .selectList(new EntityWrapper<StationRela>().eq("agv_sta", station.getDevNo()));
        if (Objects.isNull(relas) || relas.isEmpty()) {
            throw new CoolException("站点未关联堆垛机作业站点!!");
        }
        Set<String> crnStas = relas.stream().map(StationRela::getCrnSta).collect(Collectors.toSet());
        List<BasDevp> devps = basDevpService.selectList(
                new EntityWrapper<BasDevp>().in("dev_no", crnStas).eq("loading", CommonStation.COMMON_STATION_Y.type));
        if (Objects.isNull(devps) || devps.isEmpty()) {
            throw new CoolException("无站点可用!");
        }
        Collections.shuffle(devps);
        BasDevp basDevp = devps.stream().findFirst().get();
        List<CrnTaskDetlDTO> locMasts = new ArrayList<>();
        Map<String, List<LocDetl>> listMap = locDetls.stream().collect(Collectors.groupingBy(LocDetl::getMatnr));
        //获取查库存总数量
        AtomicReference<Double> totalOut = new AtomicReference<>(0.0);
        listMap.forEach((matnr, detls) -> {
            Map<String, List<LocDetl>> supIds = detls.stream().collect(Collectors.groupingBy(LocDetl::getStandby1));
            supIds.forEach((supId, sups) -> {
                // 根据supId(供应商)分类,得到需要存放的总空库存数量
                Double sum = sups.stream().mapToDouble(LocDetl::getAnfme).sum();
                AtomicReference<Double> itemed = new AtomicReference<>(0.0);
                // 获取当前供应商+ 物料在库
                List<LocDetl> detlList = locDetlService.selectList(new EntityWrapper<LocDetl>()
                        .eq("matnr", matnr)
                        .eq("area_id", one.getId())
                        .eq("standby1", supId).orderAsc(Arrays.asList("appe_time")));
                // TODO 判断是否有新库位,没有新库位,再找有空格的位置放 1. 判断当前物料是否有库存 2. 没有余料查询新库位
                if (!Objects.isNull(detlList) && !detlList.isEmpty()) {
                    Map<String, List<LocDetl>> locMaps = detlList.stream()
                            .collect(Collectors.groupingBy(LocDetl::getLocNo));
                    locMaps.forEach((locNo, adetls) -> {
                        LocMast locMast = locMastService.selectById(locNo);
                        if (Objects.isNull(locMast)) {
                            throw new CoolException("数据错误,库位信息不存在!!");
                        }
                        BasContainer container = basContainerService
                                .selectOne(new EntityWrapper<BasContainer>().eq("barcode", locMast.getBarcode()));
                        if (Objects.isNull(container)) {
                            throw new CoolException("数据错误,容器不存在!!");
                        }
                        Set<String> sets = adetls.stream().map(LocDetl::getMatnr).collect(Collectors.toSet());
                        // 判断容器是否还可混放,及当前物料可放多少
                        if (container.getMixMax() > sets.size()) {
                            //余下可混放种类
                            long suplus = Math.round((container.getMixMax() - sets.size()) * 10000) / 10000;
                            Mat mats = matService.selectOne(new EntityWrapper<Mat>().eq("matnr", matnr));
                            if (Objects.isNull(mats)) {
                                throw new CoolException("物料不存在!!");
                            }
                            //余下最大可放数量
                            Double v = Math.round((mats.getUpQty() * suplus) * 10000) / 10000.0;
                            // 入库数量小于零且入库数量减去已计划出数量大于零()
                            if (sum.compareTo(v) <= 0 && sum.compareTo(itemed.get()) > 0) {
                                itemed.set(Math.round((itemed.get() + v) * 10000) / 10000.0);
                                // 可放下
                                CrnTaskDetlDTO crnTaskDetlDTO = new CrnTaskDetlDTO().setLocNo(locMast.getLocNo()).setLocDetlList(adetls);
                                locMasts.add(crnTaskDetlDTO);
                                totalOut.set(Math.round((totalOut.get() + itemed.get()) * 10000) / 10000.0);
                            }
                        }
                    });
                }
            });
        });
        // 判断根据分拣出库后,还需要出多少托盘或料箱;如果余料大于0, 出新托盘或料箱
        if (totalOut.get().compareTo(0.0) > 0) {
            generateTask(locMasts, TaskIOType.MERGE_OUT.type, basDevp, userId, station);
            //TODO 细化区分,当前出库托盘是满足拣货数量
        } else {
            LocMast locMast = locMastService.selectOne(new EntityWrapper<LocMast>()
                    // todo 根据物料种类,区分库区类型
                    .eq("loc_sts", LocStsType.LOC_STS_TYPE_D.type)
                    .orderAsc(Arrays.asList("row1", "bay1", "lev1"))
                    .last("OFFSET 0 ROWS FETCH NEXT 1 ROWS ONLY"));
            if (Objects.isNull(locMast)) {
                throw new CoolException("未查询到可用的空板信息!!");
            }
            CrnTaskDetlDTO crnTaskDetlDTO = new CrnTaskDetlDTO().setLocNo(locMast.getLocNo()).setLocDetlList(locDetls);
            locMasts.add(crnTaskDetlDTO);
            // 空板出库
            generateTask(locMasts, TaskIOType.MERGE_OUT.type, basDevp, userId, station);
        }
    }
    /**
     * 生成堆垛机出库任务
     *
     * @param locMasts
     * @param station
     * @author Ryan
     * @date 2025/12/6 14:44
     */
    @Transactional(rollbackFor = Exception.class)
    public void generateTask(List<CrnTaskDetlDTO> locMasts, Integer ioType, BasDevp devp, Long userId, BasStation station) {
        Date now = new Date();
        for (CrnTaskDetlDTO crnTaskDetlDTO : locMasts) {
            LocMast locMast = locMastService.selectOne(new EntityWrapper<LocMast>().eq("loc_no", crnTaskDetlDTO.getLocNo()));
            if (Objects.isNull(ioType)) {
                continue;
            }
            WrkMast wrkMast1 = wrkMastService.selectOne(new EntityWrapper<WrkMast>().eq("source_loc_no", locMast.getLocNo()));
            // 生成工作号
            int workNo = commonService.getWorkNo(WorkNoType.getWorkNoType(ioType));
            if (Cools.isEmpty(wrkMast1)){
                Integer outSta = devp.getDevNo();
                // 获取路径
                StaDesc staDesc = staDescService.queryCrnStn(ioType, locMast.getCrnNo(), outSta);
                // 生成工作档
                WrkMast wrkMast = new WrkMast();
                wrkMast.setWrkNo(workNo);
                wrkMast.setIoTime(now);
                wrkMast.setWrkSts(11L); // 工作状态:11.生成出库ID
                wrkMast.setIoType(ioType); // 入出库类型
                wrkMast.setIoPri(13D); // 优先级:13
                wrkMast.setCrnNo(locMast.getCrnNo());
                wrkMast.setSourceStaNo(staDesc.getCrnStn() + ""); // 源站
                wrkMast.setStaNo(staDesc.getStnNo() + ""); // 目标站
                wrkMast.setSourceLocNo(locMast.getLocNo()); // 源库位
                //小松项目,缓存出库AGV站点,用于清空缓存区库存
                wrkMast.setPauseMk(station.getDevNo());
//                wrkMast.setAvgSta(station.getDevNo());;
                wrkMast.setFullPlt("Y"); // 满板:Y
                wrkMast.setPicking("N"); // 拣料
                wrkMast.setExitMk("N"); // 退出
                wrkMast.setEmptyMk("N"); // 空板
                wrkMast.setLinkMis("N");
                wrkMast.setBarcode(locMast.getBarcode());
                wrkMast.setAppeUser(userId); // 操作人员数据
                wrkMast.setAppeTime(now);
                wrkMast.setModiUser(userId);
                wrkMast.setModiTime(now);
                if (!wrkMastService.insert(wrkMast)) {
                    throw new CoolException("保存工作档失败,出库库位号:" + locMast.getLocNo());
                }
            }else {
                workNo = wrkMast1.getWrkNo();
            }
            // 生成工作档明细
            for (LocDetl detlDto : crnTaskDetlDTO.getLocDetlList()) {
                WrkDetl wrkDetl = new WrkDetl();
                BeanUtils.copyProperties(detlDto, wrkDetl);
                wrkDetl.setOrderNo(""); // 手动出库不需要带出库存中的单据编号
                wrkDetl.setWrkNo(workNo);
                wrkDetl.setIoTime(now);
                wrkDetl.setZpallet(null);
                wrkDetl.setBarcode(null);
                wrkDetl.setAppeTime(now);
                wrkDetl.setAppeUser(userId);
                wrkDetl.setModiTime(now);
                wrkDetl.setModiUser(userId);
                if (!wrkDetlService.insert(wrkDetl)) {
                    throw new CoolException("保存工作档明细失败");
                }
            }
            // 修改库位状态: F.在库 ====>>> R.出库预约/P.拣料/盘点/并板出库中
            locMast = locMastService.selectById(locMast.getLocNo());
            if (locMast.getLocSts().equals(LocStsType.LOC_STS_TYPE_F.type)
                    || locMast.getLocSts().equals(LocStsType.LOC_STS_TYPE_D.type)) {
                locMast.setLocSts(ioType == 101 ? "R" : "P");
                locMast.setModiUser(userId);
                locMast.setModiTime(now);
                if (!locMastService.updateById(locMast)) {
                    throw new CoolException("预约库位状态失败,库位号:" + locMast.getLocNo());
                }
            } else {
                throw new CoolException(locMast.getLocNo() + "库位不是在库状态");
            }
        }
    }
//    /**
//     * 生成堆垛机出库任务
//     *
//     * @param locMasts
//     * @param station
//     * @author Ryan
//     * @date 2025/12/6 14:44
//     */
//    @Transactional(rollbackFor = Exception.class)
//    public void generateTask(List<CrnTaskDetlDTO> locMasts, Integer ioType, BasDevp devp, Long userId, BasStation station) {
//        Date now = new Date();
//
//        for (CrnTaskDetlDTO crnTaskDetlDTO : locMasts) {
//            LocMast locMast = locMastService.selectOne(new EntityWrapper<LocMast>().eq("loc_no", crnTaskDetlDTO.getLocNo()));
//            if (Objects.isNull(ioType)) {
//                continue;
//            }
//            WrkMast wrkMast1 = wrkMastService.selectOne(new EntityWrapper<WrkMast>().eq("source_loc_no", locMast.getLocNo()));
//            // 生成工作号
//            int workNo = commonService.getWorkNo(WorkNoType.getWorkNoType(ioType));
//            if (Cools.isEmpty(wrkMast1)){
//                Integer outSta = devp.getDevNo();
//                // 获取路径
//                StaDesc staDesc = staDescService.queryCrnStn(ioType, locMast.getCrnNo(), outSta);
//
//                // 生成工作档
//                WrkMast wrkMast = new WrkMast();
//                wrkMast.setWrkNo(workNo);
//                wrkMast.setIoTime(now);
//                wrkMast.setWrkSts(11L); // 工作状态:11.生成出库ID
//                wrkMast.setIoType(ioType); // 入出库类型
//                wrkMast.setIoPri(13D); // 优先级:13
//                wrkMast.setCrnNo(locMast.getCrnNo());
//                wrkMast.setSourceStaNo(staDesc.getCrnStn() + ""); // 源站
//                wrkMast.setStaNo(staDesc.getStnNo() + ""); // 目标站
//                wrkMast.setSourceLocNo(locMast.getLocNo()); // 源库位
//                //小松项目,缓存出库AGV站点,用于清空缓存区库存
//                wrkMast.setPauseMk(station.getDevNo());
////                wrkMast.setAvgSta(station.getDevNo());;
//                wrkMast.setFullPlt("Y"); // 满板:Y
//                wrkMast.setPicking("N"); // 拣料
//                wrkMast.setExitMk("N"); // 退出
//                wrkMast.setEmptyMk("N"); // 空板
//                wrkMast.setLinkMis("N");
//                wrkMast.setBarcode(locMast.getBarcode());
//                wrkMast.setAppeUser(userId); // 操作人员数据
//                wrkMast.setAppeTime(now);
//                wrkMast.setModiUser(userId);
//                wrkMast.setModiTime(now);
//                if (!wrkMastService.insert(wrkMast)) {
//                    throw new CoolException("保存工作档失败,出库库位号:" + locMast.getLocNo());
//                }
//            }else {
//                workNo = wrkMast1.getWrkNo();
//            }
//            // 生成工作档明细
//            for (LocDetl detlDto : crnTaskDetlDTO.getLocDetlList()) {
//                WrkDetl wrkDetl = new WrkDetl();
//                BeanUtils.copyProperties(detlDto, wrkDetl);
//                wrkDetl.setOrderNo(""); // 手动出库不需要带出库存中的单据编号
//                wrkDetl.setWrkNo(workNo);
//                wrkDetl.setIoTime(now);
//                wrkDetl.setZpallet(null);
//                wrkDetl.setBarcode(null);
//                wrkDetl.setAppeTime(now);
//                wrkDetl.setAppeUser(userId);
//                wrkDetl.setModiTime(now);
//                wrkDetl.setModiUser(userId);
//                if (!wrkDetlService.insert(wrkDetl)) {
//                    throw new CoolException("保存工作档明细失败");
//                }
//            }
//            // 修改库位状态: F.在库 ====>>> R.出库预约/P.拣料/盘点/并板出库中
//            locMast = locMastService.selectById(locMast.getLocNo());
//            if (locMast.getLocSts().equals(LocStsType.LOC_STS_TYPE_F.type)
//                    || locMast.getLocSts().equals(LocStsType.LOC_STS_TYPE_D.type)) {
//                locMast.setLocSts(ioType == 101 ? "R" : "P");
//                locMast.setModiUser(userId);
//                locMast.setModiTime(now);
//                if (!locMastService.updateById(locMast)) {
//                    throw new CoolException("预约库位状态失败,库位号:" + locMast.getLocNo());
//                }
//            } else {
//                throw new CoolException(locMast.getLocNo() + "库位不是在库状态");
//            }
//        }
//    }
    /**
     * @author Ryan
@@ -2484,129 +1682,129 @@
        }
    }
    public HIKResultDTO sendAgvTask(HIKApiDTO haiKangApiDTO, String path) {
        HIKResultDTO result = new HIKResultDTO();
//    public HIKResultDTO sendAgvTask(HIKApiDTO haiKangApiDTO, String path) {
//        HIKResultDTO result = new HIKResultDTO();
//
//        ForwardAGVTaskDTO forwardAGVTaskParam = new ForwardAGVTaskDTO();
//        forwardAGVTaskParam.setReqCode(UUID.randomUUID().toString().replace("-", ""));
//        forwardAGVTaskParam.setClientCode("IWMS");
//        forwardAGVTaskParam.setTaskTyp(haiKangApiDTO.getTaskType());
//        forwardAGVTaskParam.setCtnrTyp(haiKangApiDTO.getCtnrType());
//        forwardAGVTaskParam.setPriority(haiKangApiDTO.getPriority());
//        List<ForwardAGVTaskDTO.PositionCodePaths> positionCodePathsList = new ArrayList<>();
//        positionCodePathsList
//                .add(new ForwardAGVTaskDTO.PositionCodePaths(haiKangApiDTO.getOrg(), haiKangApiDTO.getOrgType()));
//        positionCodePathsList
//                .add(new ForwardAGVTaskDTO.PositionCodePaths(haiKangApiDTO.getTar(), haiKangApiDTO.getTarType()));
//        forwardAGVTaskParam.setPositionCodePath(positionCodePathsList);
//
//        String body = JSON.toJSONString(forwardAGVTaskParam);
//        String response = "";
//        try {
//            response = new HttpHandler.Builder()
//                    .setUri(HIKApiConstant.AGV_IP)
//                    .setPath(path)
//                    .setJson(body)
//                    .build()
//                    .doPost();
//            JSONObject jsonObject = JSON.parseObject(response);
//            if (jsonObject.getInteger("code").equals(0)) {
//                result.setSuccess(true);
//            } else {
//                result.setMessage(jsonObject.getString("message"));
//                log.error("发送agv任务失败!!!url:{};request:{};response:{}", HIKApiConstant.AGV_IP + path, body, response);
//            }
//            // {"code":"1","data":"","interrupt":false,"message":"重复提交","msgErrCode":"0x3a80D012","reqCode":"fa92b49481a44627ae4d80c1400f28f6"}
//        } catch (Exception e) {
//            result.setMessage(e.getMessage());
//            log.error("发送agv任务异常", e);
//        } finally {
//            try {
//                // 保存接口日志
//                apiLogService.save(
//                        "发送agv任务",
//                        HIKApiConstant.AGV_IP + path,
//                        null,
//                        "127.0.0.1",
//                        body,
//                        response,
//                        result.isSuccess());
//            } catch (Exception e) {
//                log.error("", e);
//            }
//        }
//        return result;
//    }
        ForwardAGVTaskDTO forwardAGVTaskParam = new ForwardAGVTaskDTO();
        forwardAGVTaskParam.setReqCode(UUID.randomUUID().toString().replace("-", ""));
        forwardAGVTaskParam.setClientCode("IWMS");
        forwardAGVTaskParam.setTaskTyp(haiKangApiDTO.getTaskType());
        forwardAGVTaskParam.setCtnrTyp(haiKangApiDTO.getCtnrType());
        forwardAGVTaskParam.setPriority(haiKangApiDTO.getPriority());
        List<ForwardAGVTaskDTO.PositionCodePaths> positionCodePathsList = new ArrayList<>();
        positionCodePathsList
                .add(new ForwardAGVTaskDTO.PositionCodePaths(haiKangApiDTO.getOrg(), haiKangApiDTO.getOrgType()));
        positionCodePathsList
                .add(new ForwardAGVTaskDTO.PositionCodePaths(haiKangApiDTO.getTar(), haiKangApiDTO.getTarType()));
        forwardAGVTaskParam.setPositionCodePath(positionCodePathsList);
//    /**
//     * 呼叫AGV空台车返回
//     * @author Ryan
//     * @date 2025/9/24
//     * @param callAgvBackParam
//     * @return com.core.common.R
//     */
//    @Override
//    @Transactional(rollbackFor = Exception.class)
//    public R agvCallback(AgvCallParams callAgvBackParam, Long userId) {
//        if (Objects.isNull(callAgvBackParam.getOrgSite())) {
//            return R.error("参数不能为空!!");
//        }
//
//        // 按当前查找库位顺序,查到一个后,不再往下查询
//        LocCache locCache = findPriorityLocCache();
//
//        if (Objects.isNull(locCache)) {
//            return R.error("未查询到可用库位");
//        }
//
//        // 修改目标站点信息
//        BasStation station = basStationService.selectOne(new EntityWrapper<BasStation>().eq("dev_no", callAgvBackParam.getOrgSite()));
//        if (Objects.isNull(station)) {
//            throw new CoolException("站点不存在!!");
//        }
//
//        try {
//            //空台车回库
//            generateAgvTask("agv_back", locCache, callAgvBackParam.getOrgSite(), station.getBarcode(), userId);
//        } catch (Exception e) {
//            e.printStackTrace();
//            return R.error(e.getMessage());
//        }
//
//        return R.ok(locCache);
//    }
        String body = JSON.toJSONString(forwardAGVTaskParam);
        String response = "";
        try {
            response = new HttpHandler.Builder()
                    .setUri(HIKApiConstant.AGV_IP)
                    .setPath(path)
                    .setJson(body)
                    .build()
                    .doPost();
            JSONObject jsonObject = JSON.parseObject(response);
            if (jsonObject.getInteger("code").equals(0)) {
                result.setSuccess(true);
            } else {
                result.setMessage(jsonObject.getString("message"));
                log.error("发送agv任务失败!!!url:{};request:{};response:{}", HIKApiConstant.AGV_IP + path, body, response);
            }
            // {"code":"1","data":"","interrupt":false,"message":"重复提交","msgErrCode":"0x3a80D012","reqCode":"fa92b49481a44627ae4d80c1400f28f6"}
        } catch (Exception e) {
            result.setMessage(e.getMessage());
            log.error("发送agv任务异常", e);
        } finally {
            try {
                // 保存接口日志
                apiLogService.save(
                        "发送agv任务",
                        HIKApiConstant.AGV_IP + path,
                        null,
                        "127.0.0.1",
                        body,
                        response,
                        result.isSuccess());
            } catch (Exception e) {
                log.error("", e);
            }
        }
        return result;
    }
    /**
     * 呼叫AGV空台车返回
     * @author Ryan
     * @date 2025/9/24
     * @param callAgvBackParam
     * @return com.core.common.R
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public R agvCallback(AgvCallParams callAgvBackParam, Long userId) {
        if (Objects.isNull(callAgvBackParam.getOrgSite())) {
            return R.error("参数不能为空!!");
        }
        // 按当前查找库位顺序,查到一个后,不再往下查询
        LocCache locCache = findPriorityLocCache();
        if (Objects.isNull(locCache)) {
            return R.error("未查询到可用库位");
        }
        // 修改目标站点信息
        BasStation station = basStationService.selectOne(new EntityWrapper<BasStation>().eq("dev_no", callAgvBackParam.getOrgSite()));
        if (Objects.isNull(station)) {
            throw new CoolException("站点不存在!!");
        }
        try {
            //空台车回库
            generateAgvTask("agv_back", locCache, callAgvBackParam.getOrgSite(), station.getBarcode(), userId);
        } catch (Exception e) {
            e.printStackTrace();
            return R.error(e.getMessage());
        }
        return R.ok(locCache);
    }
    /**
     * 按优先级查找可用库位
     * 顺序:入库缓存区 -> SO区 -> EO区
     */
    private LocCache findPriorityLocCache() {
        // 1. Try Inbound Cache Area
        LocCache loc = findAvailableLocByAreaType(LocAreaType.LOC_AREA_TYPE_IN_CACHE);
        if (loc != null) return loc;
        // 2. Try SO Area
        loc = findAvailableLocByAreaType(LocAreaType.LOC_AREA_TYPE_SO);
        if (loc != null) return loc;
        // 3. Try EO Area
        loc = findAvailableLocByAreaType(LocAreaType.LOC_AREA_TYPE_EO);
        return loc;
    }
    /**
     * 根据区域类型查找可用库位
     */
    private LocCache findAvailableLocByAreaType(LocAreaType areaType) {
        BasAreas area = basAreasService.selectOne(new EntityWrapper<BasAreas>().eq("whs_type_id", areaType.type));
        if (area == null) {
            return null;
        }
        return locCacheService.selectOne(new EntityWrapper<LocCache>()
                .eq("area_id", area.getId())
                .eq("frozen", 0)
                .eq("loc_sts", LocStsType.LOC_STS_TYPE_O.type)
                .orderAsc(Arrays.asList("loc_no"))
                .last("OFFSET 0 ROWS FETCH NEXT 1 ROWS ONLY"));
    }
//    /**
//     * 按优先级查找可用库位
//     * 顺序:入库缓存区 -> SO区 -> EO区
//     */
//    private LocCache findPriorityLocCache() {
//        // 1. Try Inbound Cache Area
//        LocCache loc = findAvailableLocByAreaType(LocAreaType.LOC_AREA_TYPE_IN_CACHE);
//        if (loc != null) return loc;
//
//        // 2. Try SO Area
//        loc = findAvailableLocByAreaType(LocAreaType.LOC_AREA_TYPE_SO);
//        if (loc != null) return loc;
//
//        // 3. Try EO Area
//        loc = findAvailableLocByAreaType(LocAreaType.LOC_AREA_TYPE_EO);
//        return loc;
//    }
//
//    /**
//     * 根据区域类型查找可用库位
//     */
//    private LocCache findAvailableLocByAreaType(LocAreaType areaType) {
//        BasAreas area = basAreasService.selectOne(new EntityWrapper<BasAreas>().eq("whs_type_id", areaType.type));
//        if (area == null) {
//            return null;
//        }
//        return locCacheService.selectOne(new EntityWrapper<LocCache>()
//                .eq("area_id", area.getId())
//                .eq("frozen", 0)
//                .eq("loc_sts", LocStsType.LOC_STS_TYPE_O.type)
//                .orderAsc(Arrays.asList("loc_no"))
//                .last("OFFSET 0 ROWS FETCH NEXT 1 ROWS ONLY"));
//    }
}