自动化立体仓库 - WMS系统
123
zhang
4 天以前 21d0e7ab9d5c7316225a3e9eecf465cd37c9bd85
src/main/java/com/zy/asrs/service/impl/WorkServiceImpl.java
@@ -1,6 +1,7 @@
package com.zy.asrs.service.impl;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.core.common.BaseRes;
@@ -9,17 +10,16 @@
import com.core.common.SnowflakeIdWorker;
import com.core.exception.CoolException;
import com.zy.asrs.entity.*;
import com.zy.asrs.entity.param.EmptyPlateOutParam;
import com.zy.asrs.entity.param.FullStoreParam;
import com.zy.asrs.entity.param.LocDetlAdjustParam;
import com.zy.asrs.entity.param.StockOutParam;
import com.zy.asrs.entity.param.*;
import com.zy.asrs.service.*;
import com.zy.asrs.utils.VersionUtils;
import com.zy.asrs.utils.Utils;
import com.zy.common.model.*;
import com.zy.common.model.enums.IoWorkType;
import com.zy.common.model.enums.WorkNoType;
import com.zy.common.properties.SlaveProperties;
import com.zy.common.service.CommonService;
import com.zy.common.service.wms.Result;
import com.zy.common.service.wms.WmsService;
import com.zy.common.web.BaseController;
import com.zy.common.web.WcsController;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@@ -42,13 +42,15 @@
    private static final int DEFAULT_ROW_NO_TYPE = 1;
    @Autowired
    private MatService matService;
    @Autowired
    private WrkMastService wrkMastService;
    @Autowired
    private WrkDetlService wrkDetlService;
    @Autowired
    private BasDevpService basDevpService;
    @Autowired
    private CommonService commonService;
    @Autowired
    private WrkDetlService wrkDetlService;
    @Autowired
    private LocMastService locMastService;
    @Autowired
@@ -56,17 +58,25 @@
    @Autowired
    private LocDetlService locDetlService;
    @Autowired
    private MatCodeService matCodeService;
    @Autowired
    private AdjDetlService adjDetlService;
    @Autowired
    private WrkMastLogService wrkMastLogService;
    @Autowired
    private WrkDetlLogService wrkDetlLogService;
    @Autowired
    private WmsService wmsService;
    @Autowired
    private SnowflakeIdWorker snowflakeIdWorker;
    @Autowired
    private OrderService orderService;
    @Autowired
    private OrderDetlService orderDetlService;
    @Autowired
    private WcsController wcsController;
    @Autowired
    private WaitPakinService waitPakinService;
    @Autowired
    private RowLastnoService rowLastnoService;
    @Autowired
    private SlaveProperties slaveProperties;
    @Override
    @Transactional
@@ -75,14 +85,13 @@
        if (Cools.isEmpty(param.getDevpNo(), param.getList())) {
            throw new CoolException(BaseRes.PARAM);
        }
        Date now = new Date();
        // 源站点状态检测
        BasDevp sourceStaNo = basDevpService.checkSiteStatus(param.getDevpNo(), true);
        // 检索库位
        LocTypeDto locTypeDto = new LocTypeDto(sourceStaNo);
        log.info(JSON.toJSONString(locTypeDto));
        log.info("{}入库口尺寸检测:{}", param.getDevpNo(), locTypeDto.getLocType1()==1?"低规格库位":"高规格库位");
        List<String> matNos = param.getList().stream().map(FullStoreParam.MatCodeStore::getMatNo).distinct().collect(Collectors.toList());
        StartupDto dto = commonService.getLocNo(DEFAULT_ROW_NO_TYPE, 1, param.getDevpNo(), matNos, locTypeDto, 0);
        List<String> matnrs = param.getList().stream().map(FullStoreParam.MatCodeStore::getMatnr).distinct().collect(Collectors.toList());
        StartupDto dto = commonService.getLocNo(DEFAULT_ROW_NO_TYPE, 1, param.getDevpNo(), matnrs, locTypeDto, 0);
        // 生成工作号
        int workNo = dto.getWorkNo();
        // 生成工作档
@@ -91,7 +100,9 @@
        wrkMast.setIoTime(new Date());
        wrkMast.setWrkSts(1L); // 工作状态:生成入库ID
        wrkMast.setIoType(1); // 入出库状态:1.入库
        wrkMast.setIoPri(13D); // 优先级:10
        Double ioPri = wrkMastService.getIoPri(1, dto.getLocNo());
        wrkMast.setIoPri(ioPri); // 优先级:13
        wrkMast.setOutMost(locMastService.isOutMost(dto.getLocNo(), true) ? 1 : 0);
        wrkMast.setCrnNo(dto.getCrnNo());
        wrkMast.setSourceStaNo(dto.getSourceStaNo());
        wrkMast.setStaNo(dto.getStaNo());
@@ -103,39 +114,44 @@
        wrkMast.setEmptyMk("N"); // 空板
        wrkMast.setLinkMis("N");
        wrkMast.setCtnType(sourceStaNo.getCtnType()); // 容器类型
        // 操作人员数据
        wrkMast.setAppeUser(userId);
        wrkMast.setAppeTime(new Date());
        wrkMast.setAppeTime(now);
        wrkMast.setModiUser(userId);
        wrkMast.setModiTime(new Date());
        boolean res = wrkMastService.insert(wrkMast);
        if (!res) {
        wrkMast.setModiTime(now);
        if (!wrkMastService.insert(wrkMast)) {
            throw new CoolException("保存工作档失败");
        }
        // 生成工作档明细
        List<MatCodeCountDto> matDtos = new ArrayList<>();
        List<DetlDto> detlDtos = new ArrayList<>();
        param.getList().forEach(elem -> {
            matDtos.add(new MatCodeCountDto(elem.getMatNo(), elem.getCount()));
            DetlDto detlDto = new DetlDto(elem.getMatnr(), elem.getBatch(), elem.getAnfme());
            if (DetlDto.has(detlDtos, detlDto)) {
                DetlDto detlDto1 = DetlDto.find(detlDtos, detlDto.getMatnr(), detlDto.getBatch());
                assert detlDto1 != null;
                detlDto1.setAnfme(detlDto1.getAnfme() + detlDto.getAnfme());
            } else {
                detlDtos.add(detlDto);
            }
        });
        wrkDetlService.createWorkDetail(workNo, matDtos, param.getBarcode(), userId);
        wrkDetlService.createWorkDetail(workNo, detlDtos, param.getBarcode(), userId);
        // 更新源站点信息
        sourceStaNo.setWrkNo(workNo);
        sourceStaNo.setModiUser(userId);
        sourceStaNo.setModiTime(new Date());
        if (!basDevpService.updateById(sourceStaNo)){
        sourceStaNo.setModiTime(now);
        if (!basDevpService.updateById(sourceStaNo)) {
            throw new CoolException("更新源站失败");
        }
        // 更新目标库位状态
        LocMast locMast = locMastService.selectById(dto.getLocNo());
        if (locMast.getLocSts().equals("O")){
        if (locMast.getLocSts().equals("O")) {
            locMast.setLocSts("S"); // S.入库预约
            locMast.setModiUser(userId);
            locMast.setModiTime(new Date());
            if (!locMastService.updateById(locMast)){
            locMast.setModiTime(now);
            if (!locMastService.updateById(locMast)) {
                throw new CoolException("改变库位状态失败");
            }
        } else {
            throw new CoolException(dto.getLocNo()+"目标库位已被占用");
            throw new CoolException(dto.getLocNo() + "目标库位已被占用");
        }
        return dto.getLocNo();
    }
@@ -143,72 +159,643 @@
    @Override
    @Transactional
    public void startupFullTakeStore(StockOutParam param, Long userId) {
        List<StockOutParam.LocDetl> locDetls = new ArrayList<>();
        List<String> locs = new ArrayList<>();
        for(StockOutParam.LocDetl one : param.getLocDetls()){
            locs.add(one.getLocNo());
        }
        for (String loc : locs) {
            List<LocDetl> detls = locDetlService.selectByLocNo(loc);
            for (LocDetl detl : detls) {
                for (StockOutParam.LocDetl paramDetls : param.getLocDetls()) {
                    if (paramDetls.getMatnr().equals(detl.getMatnr()) && paramDetls.getBatch().equals(detl.getBatch()) && paramDetls.getLocNo().equals(detl.getLocNo())) {
                        locDetls.add(new StockOutParam.LocDetl(detl.getLocNo(), detl.getMatnr(), detl.getBatch(), paramDetls.getCount()));
                    }
                }
            }
        }
        // 目标站点状态检测
        BasDevp staNo = basDevpService.checkSiteStatus(param.getOutSite());
        // 获取库位明细
        List<LocDetlDto> locDetlDtos = new ArrayList<>();
        for (StockOutParam.LocDetl paramLocDetl : param.getLocDetls()) {
        for (StockOutParam.LocDetl paramLocDetl : locDetls) {
            if (!Cools.isEmpty(paramLocDetl.getLocNo(), paramLocDetl.getMatnr(), paramLocDetl.getCount())) {
                LocDetl sqlParam = new LocDetl();
                sqlParam.setLocNo(paramLocDetl.getLocNo());
                sqlParam.setMatnr(paramLocDetl.getMatnr());
                LocDetl one = locDetlService.selectOne(new EntityWrapper<>(sqlParam));
                LocDetl one = locDetlService.selectItem(paramLocDetl.getLocNo(), paramLocDetl.getMatnr(), paramLocDetl.getBatch());
                if (null != one) locDetlDtos.add(new LocDetlDto(one, paramLocDetl.getCount()));
            }
        }
        if (!locDetlDtos.isEmpty()) {
            // 启动出库开始 101.出库
            stockOut(staNo, locDetlDtos, null, userId, null, false, null);
        } else {
            throw new CoolException("库位物料不存在");
        for (LocDetlDto locDetlDto:locDetlDtos){
            if (locs.contains(locDetlDto.getLocDetl().getLocNo())){
                locDetlDto.setSign(true);
            }
        }
        if (!locDetlDtos.isEmpty()) {
            LocMast locMast = locMastService.selectOne(new EntityWrapper<LocMast>().eq("loc_no", locDetlDtos.get(0).getLocDetl().getLocNo()));
            if (locMast.getLocSts().equals("F") || locMast.getLocSts().equals("D") ){
                // 启动出库开始 101.出库
                stockOut2(staNo, locDetlDtos, null, userId);
            }else {
                throw new CoolException("所选库位存在状态不为F、D的库位,库位号:"+locMast.getLocNo()+" 、当前状态:"+locMast.getLocSts()+"-"+locMast.getLocSts$());
            }
        } else {
            throw new CoolException("库存不存在");
        }
    }
    // 备份之前 startupFullTakeStore 方法
    @Override
    @Transactional
    public void startupFullTakeStore2(StockOutParam param, Long userId) {
        List<StockOutParam.LocDetl> locDetls = new ArrayList<>();
        List<String> locs = new ArrayList<>();
        for(StockOutParam.LocDetl one : param.getLocDetls()){
            locs.add(one.getLocNo());
        }
        List<StockOutParam.LocDetl> stockOutParamLocDetlSort = Utils.getStockOutParamLocDetlSort(param.getLocDetls());
        boolean sign = true;
        boolean isfull = false; // 是否够数量
        Double isOut = 0.0;
        List<String> locNoWeighting = new ArrayList<>();
        for (StockOutParam.LocDetl locDetl : stockOutParamLocDetlSort) {
            if (Cools.isEmpty(locNoWeighting)){
                locNoWeighting.add(locDetl.getLocNo());
            }else {
                if (!Utils.getLocNoWeighting(locNoWeighting,locDetl.getLocNo())){
                    locNoWeighting.add(locDetl.getLocNo());
                    sign = true;
                }
            }
            if (sign){
                LocMast locMast = locMastService.selectOne(new EntityWrapper<LocMast>().eq("loc_no", locDetl.getLocNo()));
                //获取同一库位组的外侧库位号
                List<String> groupOuterSingleLoc = Utils.getGroupOuterSingleLoc(locDetl.getLocNo());
                if (locMast.getLocType2().equals((short)3)){
                    groupOuterSingleLoc = Utils.getGroupOuterSingleLocLowFrequency(locDetl.getLocNo());
                    sign = false;
                }
                for (String locNo : groupOuterSingleLoc) {
                    //每个库位的库位明细
                    if(locs.contains(locNo)) continue;
                    LocMast mast = locMastService.selectOne(new EntityWrapper<LocMast>().eq("loc_no", locNo));
                    if (mast.getLocSts().equals("P") || mast.getLocSts().equals("Q")){
                        throw new CoolException("当前库位的浅库位状态为" + mast.getLocSts() + "暂不支持出库!");
                    }
                    for (LocDetl detl : locDetlService.selectByLocNo(locNo)) {
                        if (!detl.getMatnr().equals(locDetl.getMatnr())) {
                            //不是相同物料
                            if (!sign){
                                locDetls.add(new StockOutParam.LocDetl(detl.getLocNo(), detl.getMatnr(), detl.getBatch(), detl.getAnfme()));
                            }
                            continue;
                        }
                        if (!param.getOutSite().equals(214) && !param.getOutSite().equals(114)){
                            if (detl.getAnfme() >= locDetl.getCount()) {
                                locDetls.add(new StockOutParam.LocDetl(detl.getLocNo(), detl.getMatnr(), detl.getBatch(), locDetl.getCount()));
                                isOut = locDetl.getCount();
                                isfull = true;
                                break;
                            } else {
                                locDetls.add(new StockOutParam.LocDetl(detl.getLocNo(), detl.getMatnr(), detl.getBatch(), detl.getAnfme()));
                                isOut = detl.getAnfme();
                            }
                        } else {
                            //强制不是相同物料
                            locDetls.add(new StockOutParam.LocDetl(detl.getLocNo(), detl.getMatnr(), detl.getBatch(), detl.getAnfme()));
                        }
                    }
                }
            }
            if (!isfull) {
                locDetls.add(new StockOutParam.LocDetl(locDetl.getLocNo(), locDetl.getMatnr(), locDetl.getBatch(), locDetl.getCount() - isOut));
//                locDetls.add(locDetl);
            }
        }
        //出库时,如果当前库位组有相同的物料,则先出外侧物料 6.30日 注释
        //for (StockOutParam.LocDetl locDetl : param.getLocDetls()) {
        //    List<String> outerSingleLoc = Utils.getGroupOuterSingleLoc(locDetl.getLocNo());
        //    boolean flag = false;
        //    Double useCount = 0D;
        //    for (String locNo : outerSingleLoc) {
        //        if(locs.contains(locNo)) continue;
        //        for (LocDetl locDetl0 : locDetlService.selectByLocNo(locNo)) {
        //            if (!locDetl0.getMatnr().equals(locDetl.getMatnr())) {
        //                //不是相同物料
        //                continue;
        //            }
        //            double lastCount = locDetl.getCount() - useCount;//剩余所需数量
        //            double realCount = 0;//实际提供数量
        //            if (lastCount==0){
        //                continue;
        //            }
        //            if (locDetl0.getAnfme() >= lastCount) {//实际数量大于所需数量,按所需数量提供
        //                realCount = lastCount;
        //            } else {
        //                //实际小于所需数量,按实际数量提供
        //                realCount = locDetl0.getAnfme();
        //            }
        //            useCount += realCount;//已使用的数量
        //
        //            locDetls.add(new StockOutParam.LocDetl(locDetl0.getLocNo(), locDetl.getMatnr(), locDetl.getBatch(), realCount));
        //            flag = true;
        //            if (useCount >= locDetl.getCount()) {
        //                //找到的库存 >= 所需库存,满足条件
        //                break;
        //            }
        //        }
        //    }
        //
        //    if (!flag) {
        //        locDetls.add(locDetl);
        //    }
        //
        //}
        // TODO: 2023/6/28 等李薇确认是否使用
        // ----------------------------------------------
        //for(StockOutParam.LocDetl one : param.getLocDetls()){
        //    List<String> outerSingleLoc = Utils.getGroupOuterSingleLoc(one.getLocNo());
        //    for (String s : outerSingleLoc) {
        //        if (!locs.contains(s)) {
        //            throw new CoolException("浅库位有相同物料不支持出库");
        //        }
        //    }
        //}
        // ----------------------------------------------
//        ArrayList<String> locNos = new ArrayList<>();
//        for (StockOutParam.LocDetl locDetl:param.getLocDetls()){
//            if (!locNos.contains(locDetl.getLocNo())){
//                locNos.add(locDetl.getLocNo());
//            }
//        }
//        List<StockOutParam.LocDetl> locDetls = param.getLocDetls();
//        ArrayList<String> locNos = new ArrayList<>();
//        List<StockOutParam.LocDetl> locDetls = new ArrayList<>();
//        ArrayList<String> matnrs = new ArrayList<>();//目的做数据去重
//        for (StockOutParam.LocDetl locDetl : param.getLocDetls()) {
//            if (!matnrs.contains(locDetl.getMatnr())) {
//                matnrs.add(locDetl.getMatnr());
//
//                //1.根据物料号先搜索高频区域是否有库存
//                List<StockOutParam.LocDetl> locNoStep1 = this.findLocNoStep1(locDetl);
//                if (locNoStep1 != null) {
//                    locDetls.addAll(locNoStep1);
//                    continue;
//                }
//
//                //高频混放区域没有满足的库位,在单品区域进行搜索
//                List<StockOutParam.LocDetl> locNoStep2 = this.findLocNoStep2(locDetl);
//                if (locNoStep2 != null) {
//                    locDetls.addAll(locNoStep2);
//                    continue;
//                }
//
//                //低频混放区域进行搜索
//                List<StockOutParam.LocDetl> locNoStep3 = this.findLocNoStep3(locDetl);
//                if (locNoStep3 != null) {
//                    locDetls.addAll(locNoStep3);
//                    continue;
//                }
//
//            }
//        }
//
//        for (StockOutParam.LocDetl locDetl : locDetls) {
//            if (!locNos.contains(locDetl.getLocNo())) {
//                locNos.add(locDetl.getLocNo());
//            }
//        }
//        //检测提交的出库库位是否合法(浅库位有非空库位)
//        Utils.checkStockOutLocNo(locNos);
        // 目标站点状态检测
        BasDevp staNo = basDevpService.checkSiteStatus(param.getOutSite());
        // 获取库位明细
        List<LocDetlDto> locDetlDtos = new ArrayList<>();
        for (StockOutParam.LocDetl paramLocDetl : locDetls) {
            if (!Cools.isEmpty(paramLocDetl.getLocNo(), paramLocDetl.getMatnr(), paramLocDetl.getCount())) {
                LocDetl one = locDetlService.selectItem(paramLocDetl.getLocNo(), paramLocDetl.getMatnr(), paramLocDetl.getBatch());
                if (null != one) locDetlDtos.add(new LocDetlDto(one, paramLocDetl.getCount()));
            }
        }
        for (LocDetlDto locDetlDto:locDetlDtos){
            if (locs.contains(locDetlDto.getLocDetl().getLocNo())){
                locDetlDto.setSign(true);
            }
        }
        if (!locDetlDtos.isEmpty()) {
            LocMast locMast = locMastService.selectOne(new EntityWrapper<LocMast>().eq("loc_no", locDetlDtos.get(0).getLocDetl().getLocNo()));
            if (locMast.getLocSts().equals("F") || locMast.getLocSts().equals("D") ){
                // 启动出库开始 101.出库
                stockOut2(staNo, locDetlDtos, null, userId);
            }else {
                throw new CoolException("所选库位存在状态不为F、D的库位,库位号:"+locMast.getLocNo()+" 、当前状态:"+locMast.getLocSts()+"-"+locMast.getLocSts$());
            }
        } else {
            throw new CoolException("库存不存在");
        }
    }
    //1.根据物料号先搜索高频区域是否有库存
    private List<StockOutParam.LocDetl> findLocNoStep1(StockOutParam.LocDetl locDetl) {
        List<StockOutParam.LocDetl> locDetlList = new ArrayList<>();
        //1.根据物料号先搜索高频区域是否有库存
        List<LocDetl> locDetls = locDetlService.findHighAreaByMatnr(locDetl.getMatnr());
        if (locDetls.size() > 0) {
            for (LocDetl locDetl0 : locDetls) {
                if (locDetl0.getAnfme() >= locDetl.getCount()) {
                    //找到的库存 >= 所需库存,满足条件
                    locDetlList.add(new StockOutParam.LocDetl(locDetl0.getLocNo(), locDetl.getMatnr(), locDetl.getBatch(), locDetl.getCount()));
                    return locDetlList;
                } else {
                    //不满足库存条件
                    //判断最外侧库位的深处是否有相同物料
                    Double useCount = locDetl0.getAnfme();//已使用的数量
                    List<StockOutParam.LocDetl> tmpLocDetlList = new ArrayList<>();//记录经过的库位
                    tmpLocDetlList.add(new StockOutParam.LocDetl(locDetl0.getLocNo(), locDetl.getMatnr(), locDetl.getBatch(), locDetl0.getAnfme()));
                    List<String> insideLoc = Utils.getGroupInsideLoc(locDetl0.getLocNo());
                    List<LocDetl> locDetls1 = locDetlService.selectByLocNosAndMatnr(insideLoc, locDetl.getMatnr());
                    if (locDetls1.size() == 0) {//内侧库位没有相同的物料,跳过
                        continue;
                    }
                    int locRow = Utils.getRow(locDetl0.getLocNo());
                    switch (locRow) {
                        case 3:
                        case 11:
                        case 18:
                            Collections.reverse(locDetls1);
                            break;
                    }
                    for (LocDetl detl : locDetls1) {
                        double lastCount = locDetl.getCount() - useCount;//剩余所需数量
                        double realCount = 0;//实际提供数量
                        if (detl.getAnfme() >= lastCount) {//实际数量大于所需数量,按所需数量提供
                            realCount = lastCount;
                        } else {
                            //实际小于所需数量,按实际数量提供
                            realCount = detl.getAnfme();
                        }
                        useCount += realCount;
                        tmpLocDetlList.add(new StockOutParam.LocDetl(detl.getLocNo(), locDetl.getMatnr(), locDetl.getBatch(), realCount));
                        if (useCount >= locDetl.getCount()) {
                            //找到的库存 >= 所需库存,满足条件
                            locDetlList.addAll(tmpLocDetlList);
                            return locDetlList;
                        }
                    }
                }
            }
            //最外侧包括内侧都无满足库位,则判断搜索出来的全部最外侧库位加起来是否满足出库数量要求
            Double useCount = 0D;
            for (LocDetl locDetl0 : locDetls) {
                double lastCount = locDetl.getCount() - useCount;//剩余所需数量
                double realCount = 0;//实际提供数量
                if (locDetl0.getAnfme() >= lastCount) {//实际数量大于所需数量,按所需数量提供
                    realCount = lastCount;
                } else {
                    //实际小于所需数量,按实际数量提供
                    realCount = locDetl0.getAnfme();
                }
                useCount += realCount;//已使用的数量
                locDetlList.add(new StockOutParam.LocDetl(locDetl0.getLocNo(), locDetl.getMatnr(), locDetl.getBatch(), realCount));
                if (useCount >= locDetl.getCount()) {
                    //找到的库存 >= 所需库存,满足条件
                    return locDetlList;
                }
            }
        }
        return null;
    }
    //高频混放区域没有满足的库位,在单品区域进行搜索
    private List<StockOutParam.LocDetl> findLocNoStep2(StockOutParam.LocDetl locDetl) {
        List<StockOutParam.LocDetl> locDetlList = new ArrayList<>();
        //在单品区域进行搜索
        List<LocDetl> locDetls = locDetlService.findSingleAreaByMatnr(locDetl.getMatnr());
        if (locDetls.size() > 0) {
            Double useCount = 0D;
            for (LocDetl locDetl0 : locDetls) {
                double lastCount = locDetl.getCount() - useCount;//剩余所需数量
                double realCount = 0;//实际提供数量
                if (locDetl0.getAnfme() >= lastCount) {//实际数量大于所需数量,按所需数量提供
                    realCount = lastCount;
                } else {
                    //实际小于所需数量,按实际数量提供
                    realCount = locDetl0.getAnfme();
                }
                useCount += realCount;//已使用的数量
                locDetlList.add(new StockOutParam.LocDetl(locDetl0.getLocNo(), locDetl.getMatnr(), locDetl.getBatch(), realCount));
                if (useCount >= locDetl.getCount()) {
                    //找到的库存 >= 所需库存,满足条件
                    return locDetlList;
                }
            }
            //库存数量不满足
            throw new CoolException("单品区域只有" + useCount + "个货物");
        }
        return null;
    }
    //低频混放区域进行搜索
    private List<StockOutParam.LocDetl> findLocNoStep3(StockOutParam.LocDetl locDetl) {
        List<StockOutParam.LocDetl> locDetlList = new ArrayList<>();
        //在单品区域进行搜索
        List<LocDetl> locDetls = locDetlService.findLowAreaByMatnr(locDetl.getMatnr());
        if (locDetls.size() > 0) {
            Double useCount = 0D;
            for (LocDetl locDetl0 : locDetls) {
                double lastCount = locDetl.getCount() - useCount;//剩余所需数量
                double realCount = 0;//实际提供数量
                if (locDetl0.getAnfme() >= lastCount) {//实际数量大于所需数量,按所需数量提供
                    realCount = lastCount;
                } else {
                    //实际小于所需数量,按实际数量提供
                    realCount = locDetl0.getAnfme();
                }
                useCount += realCount;//已使用的数量
                locDetlList.add(new StockOutParam.LocDetl(locDetl0.getLocNo(), locDetl.getMatnr(), locDetl.getBatch(), realCount));
                if (useCount >= locDetl.getCount()) {
                    //找到的库存 >= 所需库存,满足条件
                    return locDetlList;
                }
            }
        }
        return null;
    }
    @Override
    @Transactional
    public void stockOut(BasDevp staNo, List<LocDetlDto> locDetlDtos, Integer ioType, Long userId, String allotNo, Boolean sell, Double more) {
    public void stockOut(BasDevp staNo, List<LocDetlDto> locDetlDtos, IoWorkType ioWorkType, Long userId) {
        Integer basDevp = staNo.getDevNo();
        Date now = new Date();
        // 合并同类项
        Set<String> locNos = new HashSet<>();
        locDetlDtos.forEach(dto -> locNos.add(dto.getLocDetl().getLocNo()));
        ArrayList<String> locNos2 = new ArrayList<String>();
        List<OutLocDto> dtos = new ArrayList<>();
        for (String locNo : locNos) {
            List<LocDetlDto> list = new ArrayList<>();
            Iterator<LocDetlDto> iterator = locDetlDtos.iterator();
            while (iterator.hasNext()) {
                LocDetlDto dto = iterator.next();
                if (locNo.equals(dto.getLocDetl().getLocNo())) {
                    list.add(dto);
                    iterator.remove();
                }
            }
            dtos.add(new OutLocDto(locNo, list));
        for (LocDetlDto locDetlDto : locDetlDtos) {
            String locNo = locDetlDto.getLocDetl().getLocNo();
            locNos2.add(locNo);
            //if (locNos.contains(locNo)) {
            //for (OutLocDto dto : dtos) {
            //    if (dto.getLocNo().equals(locNo)) {
            //        dto.getLocDetlDtos().add(locDetlDto);
            //        break;
            //    }
            //}
            //} else {
            //locNos.add(locNo);
            //dtos.add(new OutLocDto(locNo, locDetlDto));
            //}
        }
        List<String> innermostSideLoc = Utils.getInnermostSideLoc(locNos2, true);
        System.out.println(innermostSideLoc);
        for (LocDetlDto locDetlDto : locDetlDtos) {
            String locNo = locDetlDto.getLocDetl().getLocNo();
            if (innermostSideLoc.contains(locNo)) {
                if (locNos.contains(locNo)) {
                    for (OutLocDto dto : dtos) {
                        if (dto.getLocNo().equals(locNo)) {
                            dto.getLocDetlDtos().add(locDetlDto);
                            break;
                        }
                    }
                } else {
                    locNos.add(locNo);
                    dtos.add(new OutLocDto(locNo, locDetlDto));
                }
                //dtos.add(new OutLocDto(locNo,locDetlDto));
            }
        }
        Integer ioType = null;
        List<String> excludeLocNos = dtos.stream().map(OutLocDto::getLocNo).distinct().collect(Collectors.toList());
        // 生成工作档
        for (OutLocDto dto : dtos) {
            // 判断入出库类型:101.全板出库 or 103.拣料出库
            if (ioType == null) {
                ioType = dto.isAll() ? 101 : 103;
            // 如果为深库位,且对应浅库位为X.禁用,则略过此库位
            if (Utils.isDeepLoc(slaveProperties, dto.getLocNo())) {
                String shallowLoc = Utils.getShallowLoc(slaveProperties, dto.getLocNo());
                if (locMastService.selectById(shallowLoc).getLocSts().equals("X")) {
                    continue;
                }
            }
            //-----------------2023.5.31上架区域版本兼容代码-----------------------
            //判断当前库位组是否均为同一种物料,如不是,则认定需要执行兼容代码
            //需要将待出库库位前侧库位(不包含最外侧高频库位)执行101全板出库
            String locNo = dto.getLocNo();
            String matnr = null;
            for (LocDetlDto locDetlDto : dto.getLocDetlDtos()) {
                LocDetl locDetl = locDetlDto.getLocDetl();
                if (locDetl.getLocNo().equals(locNo)) {
                    matnr = locDetl.getMatnr();
                }
            }
            List<String> outerLocs = Utils.getGroupOuterSingleLoc(locNo);
            LocMast locMast1 = locMastService.selectOne(new EntityWrapper<LocMast>().eq("loc_no", dto.getLocNo()));
            if (locMast1.getLocType2().equals((short) 3)) {
                outerLocs = Utils.getGroupOuterSingleLocLowFrequency(locMast1.getLocNo());
            }
            Double wrkPri = 15D;
            for (String outerLoc : outerLocs) {
                LocMast locMast = locMastService.selectById(outerLoc);
                if (locMast == null) {
                    continue;
                }
                if (!locMast.getLocSts().equals("F")) {
                    continue;
                }
                //if (locMast.getLocSts().equals("F")) {
                //    throw new CoolException("浅库位在库不支持此任务");
                //}
                boolean flag = false;
                for (LocDetl locDetl : locDetlService.selectByLocNo(locMast.getLocNo())) {
                    if (!locDetl.getMatnr().equals(matnr)) {
                        flag = true;
                        break;
                    } else if (!Cools.isEmpty(ioWorkType) && ioWorkType.equals(IoWorkType.CHECK_OUT)) {
                        flag = true;
                        break;
                    }
                }
                if (flag) {
                    //当前巷道存在不同规格的物料,需要进行101全板出库
                    // 获取路径
                    StaDesc staDesc = staDescService.queryCrnStn(101, locMast.getCrnNo(), locMast.getCrnNo() == 1 ? 100 : 200, locMast.getRow1());
                    if (staDesc == null) {
                        throw new CoolException("出库路径不存在");
                    }
                    // 生成工作号
                    int workNo = commonService.getWorkNo(WorkNoType.getWorkNoType(101));
                    // 生成工作档
                    WrkMast wrkMast = new WrkMast();
                    wrkMast.setWrkNo(workNo);
                    wrkMast.setIoTime(now);
                    wrkMast.setWrkSts(11L); // 工作状态:11.生成出库ID
                    wrkMast.setIoType(101); // 入出库状态101全板出库
                    wrkPri += 2D;
                    wrkMast.setIoPri(Utils.getIoPri(locMast.getLocNo())); // 优先级
                    wrkMast.setOutMost(0);
                    wrkMast.setCrnNo(locMast.getCrnNo());
                    wrkMast.setSourceStaNo(staDesc.getCrnStn()); // 源站
                    wrkMast.setStaNo(locMast.getCrnNo() == 1 ? 100 : 200); // 目标站
                    wrkMast.setSourceLocNo(locMast.getLocNo()); // 源库位
                    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("保存工作档失败,出库库位号:" + dto.getLocNo());
                    }
                    // 生成工作档明细
                    for (LocDetl locDetl : locDetlService.selectByLocNo(locMast.getLocNo())) {
                        WrkDetl wrkDetl = new WrkDetl();
                        wrkDetl.sync(locDetl);
                        wrkDetl.setOrderNo(""); // 手动出库不需要带出库存中的单据编号
                        wrkDetl.setWrkNo(workNo);
                        wrkDetl.setIoTime(now);
                        wrkDetl.setAnfme(locDetl.getAnfme()); // 数量
                        wrkDetl.setAppeTime(now);
                        wrkDetl.setAppeUser(userId);
                        wrkDetl.setModiTime(now);
                        wrkDetl.setModiUser(userId);
                        wrkDetl.setSpecs(locDetl.getSpecs());
                        wrkDetl.setMatType(locDetl.getMatType());
                        wrkDetl.setZpallet(locMast.getBarcode());
                        if (!wrkDetlService.insert(wrkDetl)) {
                            throw new CoolException("保存工作档明细失败");
                        }
                        Mat mat = matService.selectByMatnr(locDetl.getMatnr());
                        if (Cools.isEmpty(mat)) {
                            throw new CoolException(locDetl.getMatnr() + "商品档案不存在");
                        }
                        int zpallet = waitPakinService.selectCount(new EntityWrapper<WaitPakin>().eq("zpallet", locDetl.getZpallet()).ne("loc_no", locDetl.getLocNo()));
                        if (zpallet > 0) {
                            throw new CoolException(locDetl.getZpallet() + "入库通知档中已存在");
                        }
                        WaitPakin waitPakin = new WaitPakin();
                        waitPakin.sync(mat);
                        waitPakin.setBatch("");
                        waitPakin.setZpallet(locMast.getBarcode());   // 托盘码
                        waitPakin.setIoStatus("N");     // 入出状态
                        waitPakin.setAnfme(locDetl.getAnfme());  // 数量
                        waitPakin.setStatus("Y");    // 状态
                        waitPakin.setAppeUser(userId);
                        waitPakin.setAppeTime(now);
                        waitPakin.setModiUser(userId);
                        waitPakin.setModiTime(now);
                        waitPakin.setMemo(locDetl.getMemo());
                        waitPakin.setFrozen(0); // 是否冻结
                        waitPakin.setManu(locDetl.getManu());
                        waitPakin.setMatType(mat.getMatType());//物料类型
                        if (!waitPakinService.insert(waitPakin)) {
                            throw new CoolException("保存入库通知档失败");
                        }
                    }
                    // 修改库位状态:   F.在库 ====>>> R.出库预约
                    if (locMast.getLocSts().equals("F")) {
                        locMast.setLocSts("R");
                        locMast.setModiUser(userId);
                        locMast.setModiTime(now);
                        if (!locMastService.updateById(locMast)) {
                            throw new CoolException("预约库位状态失败,库位号:" + dto.getLocNo());
                        }
                    } else {
                        throw new CoolException(dto.getLocNo() + "库位不是在库状态");
                    }
                }
            }
            //-----------------2023.5.31上架区域版本兼容代码end-----------------------
            // 判断入出库类型:101.全板出库 or 103.拣料出库
            if (ioWorkType == null) {
                ioType = dto.isAll() ? 101 : 103;
            } else if (ioWorkType.equals(IoWorkType.CHECK_OUT)) {
                ioType = 107;
                // if (wrkMastService.selectCount(new EntityWrapper<WrkMast>().eq("io_type", 107)) > 0) {
                //     throw new CoolException("只能存在一笔盘点任务");
                // }
            }
            assert ioType != null;
            // 获取库位
            LocMast locMast = locMastService.selectById(dto.getLocNo());
            //-----------------2023.6.2出库兼容代码-----------------------
            if (ioType != 107) {
                List<LocDetl> locDetls = locDetlService.selectByLocNo(locMast.getLocNo());
                //if (locMast.getLocType2() == 1 && locDetls.size() > 1) {
                //    //单品区域出库物料大于1种,强制变成101全板出库
                //    ioType = 101;
                //    if (staNo.getDevNo()==300){
                //        staNo.setDevNo(200);
                //    }
                //}
                if (ioType == 101) {
                    if (staNo.getDevNo() == 300) {
                        if (locMast.getCrnNo() == 1) {
                            staNo.setDevNo(100);
                        } else {
                            staNo.setDevNo(200);
                        }
                    }
                } else {
                    staNo.setDevNo(basDevp);
                }
            }
            //-----------------2023.6.2出库兼容代码end-----------------------
            // 获取路径
            Wrapper<StaDesc> wrapper = new EntityWrapper<StaDesc>()
                    .eq("type_no", ioType)
                    .eq("stn_no", staNo.getDevNo())
                    .eq("crn_no", locMast.getCrnNo());
            StaDesc staDesc = staDescService.selectOne(wrapper);
            if (Cools.isEmpty(staDesc)) {
            StaDesc staDesc = staDescService.queryCrnStn(ioType, locMast.getCrnNo(), staNo.getDevNo(), locMast.getRow1());
            if (staDesc == null) {
                throw new CoolException("出库路径不存在");
            }
            // 生成工作号
            int workNo = commonService.getWorkNo(WorkNoType.getWorkNoType(ioType));
            // 生成工作档
            WrkMast wrkMast = new WrkMast();
            wrkMast.setWrkNo(workNo);
            wrkMast.setIoTime(new Date());
            wrkMast.setIoTime(now);
            wrkMast.setWrkSts(11L); // 工作状态:11.生成出库ID
            wrkMast.setIoType(ioType); // 入出库状态
            wrkMast.setIoPri(13D); // 优先级:13
            Double ioPri = wrkMastService.getIoPri(ioType, dto.getLocNo());
            wrkMast.setIoPri(15D); // 优先级:13
            wrkMast.setOutMost(locMastService.isOutMost(dto.getLocNo(), false) ? 1 : 0);
            wrkMast.setCrnNo(locMast.getCrnNo());
            wrkMast.setSourceStaNo(staDesc.getCrnStn()); // 源站
            wrkMast.setStaNo(staDesc.getStnNo()); // 目标站
@@ -218,30 +805,33 @@
            wrkMast.setExitMk("N"); // 退出
            wrkMast.setEmptyMk("N"); // 空板
            wrkMast.setLinkMis("N");
            wrkMast.setPacked(allotNo); // 平仓补仓单号
            wrkMast.setPdcType(sell?"Y":"N"); // ERP销售订单出库任务
            wrkMast.setRefWrkno(more==null?0.0D:more);
            wrkMast.setBarcode(locMast.getBarcode());
            wrkMast.setAppeUser(userId); // 操作人员数据
            wrkMast.setAppeTime(new Date());
            wrkMast.setAppeTime(now);
            wrkMast.setModiUser(userId);
            wrkMast.setModiTime(new Date());
            wrkMast.setModiTime(now);
            if (!wrkMastService.insert(wrkMast)) {
                throw new CoolException("保存工作档失败,出库库位号:"+dto.getLocNo());
                throw new CoolException("保存工作档失败,出库库位号:" + dto.getLocNo());
            }
            // 生成工作档明细
            for (LocDetlDto detlDto : dto.getLocDetlDtos()) {
                // 出库时,数量为0的直接忽略
                if (detlDto.getCount()==null || detlDto.getCount() <= 0.0D) {continue;}
                if (detlDto.getCount() == null || detlDto.getCount() <= 0.0D) {
                    continue;
                }
                WrkDetl wrkDetl = new WrkDetl();
                wrkDetl.sync(detlDto.getLocDetl());
                wrkDetl.setOrderNo(""); // 手动出库不需要带出库存中的单据编号
                wrkDetl.setWrkNo(workNo);
                wrkDetl.setIoTime(new Date());
                Double anfme = ioType==101?detlDto.getLocDetl().getAnfme():detlDto.getCount();
                wrkDetl.setIoTime(now);
                Double anfme = ioType == 101 ? detlDto.getLocDetl().getAnfme() : detlDto.getCount();
                wrkDetl.setAnfme(anfme); // 数量
                VersionUtils.setWrkDetl(wrkDetl, detlDto.getLocDetl()); // 版本控制
                wrkDetl.setAppeTime(new Date());
                wrkDetl.setAppeTime(now);
                wrkDetl.setAppeUser(userId);
                wrkDetl.setModiTime(new Date());
                wrkDetl.setModiTime(now);
                wrkDetl.setModiUser(userId);
                wrkDetl.setSpecs(detlDto.getLocDetl().getSpecs());
                wrkDetl.setMatType(detlDto.getLocDetl().getMatType());
                wrkDetl.setZpallet(locMast.getBarcode());
                if (!wrkDetlService.insert(wrkDetl)) {
                    throw new CoolException("保存工作档明细失败");
                }
@@ -249,16 +839,492 @@
            // 修改库位状态:   F.在库 ====>>> R.出库预约/P.拣料/盘点/并板出库中
            locMast = locMastService.selectById(dto.getLocNo());
            if (locMast.getLocSts().equals("F")) {
                locMast.setLocSts(ioType==101?"R":"P");
                locMast.setLocSts(ioType == 101 ? "R" : "P");
                locMast.setModiUser(userId);
                locMast.setModiTime(new Date());
                locMast.setModiTime(now);
                if (!locMastService.updateById(locMast)) {
                    throw new CoolException("预约库位状态失败,库位号:"+dto.getLocNo());
                    throw new CoolException("预约库位状态失败,库位号:" + dto.getLocNo());
                }
            } else {
                throw new CoolException(dto.getLocNo() + "库位不是在库状态");
            }
//            locMastService.breakUp(dto.getLocNo(), excludeLocNos);
        }
    }
    @Override
    @Transactional
    public void stockOut2(BasDevp staNo, List<LocDetlDto> locDetlDtos, IoWorkType ioWorkType, Long userId) {
        Integer basDevp = staNo.getDevNo();
        Date now = new Date();
        // 合并同类项
        Set<String> locNos = new HashSet<>();
        List<OutLocDto> dtos = new ArrayList<>();
        for (LocDetlDto locDetlDto : locDetlDtos) {
            String locNo = locDetlDto.getLocDetl().getLocNo();
            if (locNos.contains(locNo)) {
                for (OutLocDto dto : dtos) {
                    if (dto.getLocNo().equals(locNo)) {
                        boolean signDto = false;
                        for (LocDetlDto locDetlDto1 : dto.getLocDetlDtos()) {
                            if (locDetlDto1.getLocDetl().getBatch().equals(locDetlDto.getLocDetl().getBatch())
                                    && locDetlDto1.getLocDetl().getMatnr().equals(locDetlDto.getLocDetl().getMatnr())
                                    && locDetlDto1.getLocDetl().getLocNo().equals(locDetlDto.getLocDetl().getLocNo())) {
                                signDto = true;
                            }
                        }
                        if (!signDto) {
                            dto.getLocDetlDtos().add(locDetlDto);
                            break;
                        }
                    }
                }
            } else {
                locNos.add(locNo);
                dtos.add(new OutLocDto(locNo, locDetlDto));
            }
        }
        Integer ioType = null;
        // 生成工作档
        for (OutLocDto dto : dtos) {
            // 如果为深库位,且对应浅库位为X.禁用,则略过此库位
            if (Utils.isDeepLoc(slaveProperties, dto.getLocNo())) {
                String shallowLoc = Utils.getShallowLoc(slaveProperties, dto.getLocNo());
                if (locMastService.selectById(shallowLoc).getLocSts().equals("X")) {
                    continue;
                }
            }
            //-----------------2023.5.31上架区域版本兼容代码-----------------------
            //判断当前库位组是否均为同一种物料,如不是,则认定需要执行兼容代码
            //需要将待出库库位前侧库位(不包含最外侧高频库位)执行101全板出库
            String locNo = dto.getLocNo();
            String matnr = null;
            for (LocDetlDto locDetlDto : dto.getLocDetlDtos()) {
                LocDetl locDetl = locDetlDto.getLocDetl();
                if (locDetl.getLocNo().equals(locNo)) {
                    matnr = locDetl.getMatnr();
                }
            }
            List<String> outerLocs = new ArrayList<>();
            if (!dto.isSign()) {
                outerLocs.add(dto.getLocNo());
            }
            Double wrkPri = 15D;
            for (String outerLoc : outerLocs) {
                if (!dto.isSign()) {
                    // 生成工作档明细
                    for (LocDetl locDetl : locDetlService.selectByLocNo(outerLoc)) {
                        Mat mat = matService.selectByMatnr(locDetl.getMatnr());
                        if (Cools.isEmpty(mat)) {
                            throw new CoolException(locDetl.getMatnr() + "商品档案不存在");
                        }
                        int zpallet = waitPakinService.selectCount(new EntityWrapper<WaitPakin>().eq("zpallet", locDetl.getZpallet()).ne("loc_no", locDetl.getLocNo()));
                        if (zpallet > 0) {
                            throw new CoolException(locDetl.getZpallet() + "入库通知档中已存在");
                        }
                        WaitPakin waitPakin = new WaitPakin();
                        waitPakin.sync(mat);
                        //waitPakin.setBatch("");
                        waitPakin.setZpallet(locDetl.getZpallet());   // 托盘码
                        waitPakin.setIoStatus("N");     // 入出状态
                        waitPakin.setAnfme(locDetl.getAnfme());  // 数量
                        waitPakin.setStatus("Y");    // 状态
                        waitPakin.setAppeUser(userId);
                        waitPakin.setAppeTime(now);
                        waitPakin.setModiUser(userId);
                        waitPakin.setModiTime(now);
                        waitPakin.setMemo(locDetl.getMemo());
                        waitPakin.setFrozen(0); // 是否冻结
                        waitPakin.setManu(locDetl.getManu());
                        waitPakin.setMatType(mat.getMatType());//物料类型
                        if (!waitPakinService.insert(waitPakin)) {
                            throw new CoolException("保存入库通知档失败");
                        }
                    }
                }
            }
            //-----------------2023.5.31上架区域版本兼容代码end-----------------------
            // 判断入出库类型:101.全板出库 or 103.拣料出库
            if (ioWorkType == null) {
                ioType = dto.isAll() ? 101 : 103;
            } else if (ioWorkType.equals(IoWorkType.CHECK_OUT)) {
                ioType = 107;
                // if (wrkMastService.selectCount(new EntityWrapper<WrkMast>().eq("io_type", 107)) > 0) {
                //     throw new CoolException("只能存在一笔盘点任务");
                // }
            }
            if (ioType != 101) {
                if (basDevp == 114 || basDevp == 214) {
                    throw new CoolException("2楼只允许全板出库,请选择全部物料");
                }
            }
            assert ioType != null;
            // 获取库位
            LocMast locMast = locMastService.selectById(dto.getLocNo());
            //-----------------2023.6.2出库兼容代码-----------------------
            if (ioType != 107) {
                List<LocDetl> locDetls = locDetlService.selectByLocNo(locMast.getLocNo());
                //if (locMast.getLocType2() == 1 && locDetls.size() > 1) {
                //    //单品区域出库物料大于1种,强制变成101全板出库
                //    ioType = 101;
                //    if (staNo.getDevNo()==300){
                //        staNo.setDevNo(200);
                //    }
                //}
                if (ioType == 101) {
                    if (basDevp == 300) {
                        if (locMast.getCrnNo() == 1) {
                            staNo.setDevNo(100);
                        } else {
                            staNo.setDevNo(200);
                        }
                    } else if (basDevp == 114) {
                        if (!dto.isSign()) {
                            staNo.setDevNo(100);
                        } else {
                            staNo.setDevNo(basDevp);
                        }
                    } else if (basDevp == 214) {
                        if (!dto.isSign()) {
                            staNo.setDevNo(200);
                        } else {
                            staNo.setDevNo(basDevp);
                        }
                    } else {
                        if (locMast.getCrnNo() == 1) {
                            staNo.setDevNo(100);
                        } else {
                            staNo.setDevNo(200);
                        }
                    }
                } else {
                    staNo.setDevNo(basDevp);
                }
            }
            //-----------------2023.6.2出库兼容代码end-----------------------
            // 获取路径
            StaDesc staDesc = staDescService.queryCrnStn(ioType, locMast.getCrnNo(), staNo.getDevNo(), locMast.getRow1());
            if (staDesc == null) {
                throw new CoolException("出库路径不存在");
            }
            // 生成工作号
            int workNo = commonService.getWorkNo(WorkNoType.getWorkNoType(ioType));
            // 生成工作档
            WrkMast wrkMast = new WrkMast();
            wrkMast.setWrkNo(workNo);
            wrkMast.setIoTime(now);
            wrkMast.setWrkSts(11L); // 工作状态:11.生成出库ID
            wrkMast.setIoType(ioType); // 入出库状态
            Double ioPri = wrkMastService.getIoPri(ioType, dto.getLocNo());
            wrkMast.setIoPri(15D); // 优先级:13
            wrkMast.setOutMost(locMastService.isOutMost(dto.getLocNo(), false) ? 1 : 0);
            wrkMast.setCrnNo(locMast.getCrnNo());
            wrkMast.setSourceStaNo(staDesc.getCrnStn()); // 源站
            wrkMast.setStaNo(staDesc.getStnNo()); // 目标站
            wrkMast.setSourceLocNo(dto.getLocNo()); // 源库位
            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("保存工作档失败,出库库位号:" + dto.getLocNo());
            } else {
                log.info("出库插入工作主档成功===>>" + JSON.toJSONString(wrkMast));
            }
            // 生成工作档明细
            for (LocDetlDto detlDto : dto.getLocDetlDtos()) {
                log.info("开始插入工作明细===>>工作号:" + workNo + "明细:" + JSON.toJSONString(detlDto));
                if (detlDto.getCount() == null || detlDto.getCount() <= 0.0D) {
                    continue;
                }
                WrkDetl wrkDetl = new WrkDetl();
                wrkDetl.sync(detlDto.getLocDetl());
                wrkDetl.setOrderNo(""); // 手动出库不需要带出库存中的单据编号
                wrkDetl.setWrkNo(workNo);
                wrkDetl.setIoTime(now);
                Double anfme = ioType == 101 ? detlDto.getLocDetl().getAnfme() : detlDto.getCount();
                wrkDetl.setAnfme(anfme); // 数量
                wrkDetl.setAppeTime(now);
                wrkDetl.setAppeUser(userId);
                wrkDetl.setModiTime(now);
                wrkDetl.setModiUser(userId);
                wrkDetl.setSpecs(detlDto.getLocDetl().getSpecs());
                wrkDetl.setMatType(detlDto.getLocDetl().getMatType());
                wrkDetl.setZpallet(locMast.getBarcode());
                if (!wrkDetlService.insert(wrkDetl)) {
                    throw new CoolException("stockOut2:保存工作档明细失败===>>" + JSON.toJSONString(wrkDetl));
                } else {
                    log.info("出库插入工作明细成功===>>" + JSON.toJSONString(wrkDetl));
                }
            }
            // 修改库位状态:   F.在库 ====>>> R.出库预约/P.拣料/盘点/并板出库中
            locMast = locMastService.selectById(dto.getLocNo());
            if (locMast.getLocSts().equals("F")) {
                locMast.setLocSts(ioType == 101 ? "R" : "P");
                locMast.setModiUser(userId);
                locMast.setModiTime(now);
                if (!locMastService.updateById(locMast)) {
                    throw new CoolException("预约库位状态失败,库位号:" + dto.getLocNo());
                }
            } else {
                throw new CoolException(dto.getLocNo() + "库位不是在库状态");
            }
//            locMastService.breakUp(dto.getLocNo(), excludeLocNos);
        }
    }
    @Override
    @Transactional
    public void stockOut(BasDevp staNo, TaskDto taskDto, Long userId) {
        Date now = new Date();
        if (Cools.isEmpty(taskDto.getLocDtos()) || taskDto.getLocDtos().size() == 0) {
            // 获取库位
            LocMast locMast = locMastService.selectById(taskDto.getLocNo());
            // 获取路径
            int ioType = locMast.getLocSts().equals("D") ? 110 : 101;
            StaDesc staDesc = staDescService.queryCrnStnAuto(ioType, locMast.getCrnNo(), staNo.getDevNo());
            // 生成工作号
            int workNo = commonService.getWorkNo(WorkNoType.getWorkNoType(ioType));
            // 生成工作档
            WrkMast wrkMast = new WrkMast();
            wrkMast.setWrkNo(workNo);
            wrkMast.setIoTime(now);
            wrkMast.setWrkSts(11L); // 工作状态:11.生成出库ID
            wrkMast.setIoType(ioType); // 入出库状态
            Double ioPri = wrkMastService.getIoPri(ioType, taskDto.getLocNo());
            wrkMast.setIoPri(ioPri); // 优先级:13
            wrkMast.setOutMost(locMastService.isOutMost(taskDto.getLocNo(), false) ? 1 : 0);
            wrkMast.setCrnNo(locMast.getCrnNo());
            wrkMast.setSourceStaNo(staDesc.getCrnStn()); // 源站
            wrkMast.setStaNo(staDesc.getStnNo()); // 目标站
            wrkMast.setSourceLocNo(taskDto.getLocNo()); // 源库位
            wrkMast.setFullPlt(ioType == 110 ? "N" : "Y"); // 满板:Y
            wrkMast.setPicking("N"); // 拣料
            wrkMast.setExitMk("N"); // 退出
            wrkMast.setEmptyMk(ioType == 110 ? "Y" : "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("保存工作档失败,出库库位号:" + taskDto.getLocNo());
            }
            // 修改库位状态:   F、D.在库 ====>>> R.出库预约/P.拣料/盘点/并板出库中
            locMast = locMastService.selectById(taskDto.getLocNo());
            if (locMast.getLocSts().equals("F") || locMast.getLocSts().equals("D")) {
                locMast.setLocSts("R");
                locMast.setModiUser(userId);
                locMast.setModiTime(now);
                if (!locMastService.updateById(locMast)) {
                    throw new CoolException("预约库位状态失败,库位号:" + taskDto.getLocNo());
                }
            } else {
                throw new CoolException(taskDto.getLocNo() + "库位不是在库状态");
            }
            return;
        }
        List<LocDto> locDtos = taskDto.getLocDtos();
        for (LocDto locDto : locDtos) {
            if (!taskDto.getLocNo().equals(locDto.getLocNo()) && !taskDto.getStaNo().equals(locDto.getStaNo())) {
                throw new CoolException("订单出库异常,请联系管理员");
            }
        }
        // 获取库位
        LocMast locMast = locMastService.selectById(taskDto.getLocNo());
        // 获取路径
        int ioType = taskDto.isAll() ? 101 : 103;
        StaDesc staDesc = staDescService.queryCrnStnAuto(ioType, locMast.getCrnNo(), staNo.getDevNo());
        // 生成工作号
        int workNo = commonService.getWorkNo(WorkNoType.getWorkNoType(ioType));
        // 生成工作档
        WrkMast wrkMast = new WrkMast();
        wrkMast.setWrkNo(workNo);
        wrkMast.setIoTime(now);
        wrkMast.setWrkSts(11L); // 工作状态:11.生成出库ID
        wrkMast.setIoType(ioType); // 入出库状态
        Double ioPri = wrkMastService.getIoPri(ioType, taskDto.getLocNo());
        wrkMast.setIoPri(ioPri); // 优先级:13
        wrkMast.setOutMost(locMastService.isOutMost(taskDto.getLocNo(), false) ? 1 : 0);
        wrkMast.setCrnNo(locMast.getCrnNo());
        wrkMast.setSourceStaNo(staDesc.getCrnStn()); // 源站
        wrkMast.setStaNo(staDesc.getStnNo()); // 目标站
        wrkMast.setSourceLocNo(taskDto.getLocNo()); // 源库位
        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("保存工作档失败,出库库位号:" + taskDto.getLocNo());
        }
        // 生成工作档明细
        for (LocDto locDto : taskDto.getLocDtos()) {
            if (locDto.getAnfme() == null || locDto.getAnfme() <= 0.0D) {
                continue;
            }
            LocDetl locDetl = locDetlService.selectItem(locDto.getLocNo(), locDto.getMatnr(), locDto.getBatch());
            if (locDetl == null || locDetl.getAnfme() < locDto.getAnfme()) {
                throw new CoolException(locDto.getLocNo() + "库位中" + locDto.getMatnr() + "商品库存不足!");
            }
            Mat mat = matService.selectByMatnr(locDto.getMatnr());
            assert mat != null;
            WrkDetl wrkDetl = new WrkDetl();
            wrkDetl.sync(mat);
            wrkDetl.setZpallet(wrkMast.getBarcode());
            wrkDetl.setIoTime(now);
            wrkDetl.setWrkNo(workNo);
            wrkDetl.setBatch(locDto.getBatch());
            wrkDetl.setOrderNo(locDto.getOrderNo());
            wrkDetl.setAnfme(locDto.getAnfme()); // 数量
            wrkDetl.setAppeTime(now);
            wrkDetl.setAppeUser(userId);
            wrkDetl.setModiTime(now);
            wrkDetl.setModiUser(userId);
            wrkDetl.setMatType(locDetl.getMatType());
            if (!wrkDetlService.insert(wrkDetl)) {
                throw new CoolException("保存工作档明细失败");
            }
            if (!wrkDetl.getOrderNo().equals("伴生出库")) {
                // 修改订单明细
                if (!BaseController.isJSON(locDto.getOrderNo())) {
                    String[] orderNos = GetOrderNo(locDto.getOrderNo());
                    if (!Cools.isEmpty(orderNos) && orderNos.length != 0) {
                        Double anfme = locDto.getAnfme();
                        for (String orderNo : orderNos) {
                            OrderDetl orderDetl = orderDetlService.selectItem(orderNo, locDto.getMatnr(), locDto.getBatch());
                            if (orderDetl == null) {
                                orderDetl = orderDetlService.selectItem(orderNo, locDto.getMatnr(), null);
                            }
                            if (orderDetl.getAnfme() <= anfme) {
                                if (!orderDetlService.increase(orderDetl.getOrderId(), orderDetl.getMatnr(), orderDetl.getBatch(), orderDetl.getAnfme())) {
                                    throw new CoolException("修改订单明细数量失败");
                                }
                                anfme = anfme - orderDetl.getAnfme();
                            } else {
                                if (!orderDetlService.increase(orderDetl.getOrderId(), orderDetl.getMatnr(), orderDetl.getBatch(), anfme)) {
                                    throw new CoolException("修改订单明细数量失败");
                                }
                            }
                            orderService.updateSettle(orderDetl.getOrderId(), 2L, userId);
                        }
                    } else {
                        OrderDetl orderDetl = orderDetlService.selectItem(locDto.getOrderNo(), locDto.getMatnr(), locDto.getBatch());
                        if (orderDetl == null) {
                            orderDetl = orderDetlService.selectItem(locDto.getOrderNo(), locDto.getMatnr(), null);
                        }
                        if (!orderDetlService.increase(orderDetl.getOrderId(), orderDetl.getMatnr(), orderDetl.getBatch(), locDto.getAnfme())) {
                            throw new CoolException("修改订单明细数量失败");
                        }
                        orderService.updateSettle(orderDetl.getOrderId(), 2L, userId);
                    }
                } else {
                    // 订单合并出库
                    List<OrderDto> orderDtoList = JSON.parseArray(locDto.getOrderNo(), OrderDto.class);
                    for (OrderDto orderDto : orderDtoList) {
                        OrderDetl orderDetl = orderDetlService.selectItem(orderDto.getOrderNo(), locDto.getMatnr(), locDto.getBatch());
                        if (orderDetl == null) {
                            orderDetl = orderDetlService.selectItem(orderDto.getOrderNo(), locDto.getMatnr(), null);
                        }
                        if (!orderDetlService.increase(orderDetl.getOrderId(), orderDetl.getMatnr(), orderDetl.getBatch(), orderDto.getAnfme())) {
                            throw new CoolException("修改订单明细数量失败");
                        }
                        orderService.updateSettle(orderDetl.getOrderId(), 2L, userId);
                    }
                }
            } else {//自动组托
                WaitPakin waitPakin = new WaitPakin();
                waitPakin.sync(mat);
                waitPakin.setBatch(wrkDetl.getBatch());
                waitPakin.setZpallet(wrkDetl.getZpallet());   // 托盘码
                waitPakin.setIoStatus("N");     // 入出状态
                waitPakin.setAnfme(wrkDetl.getAnfme());  // 数量
                waitPakin.setStatus("Y");    // 状态
                waitPakin.setAppeUser(userId);
                waitPakin.setAppeTime(now);
                waitPakin.setModiUser(userId);
                waitPakin.setModiTime(now);
                waitPakin.setMemo(wrkDetl.getMemo());
                waitPakin.setFrozen(wrkDetl.getFrozen()); // 是否冻结
                waitPakin.setManu(wrkDetl.getManu());
                waitPakin.setMatType(mat.getMatType());//物料类型
                if (!waitPakinService.insert(waitPakin)) {
                    throw new CoolException("保存入库通知档失败");
                }
            }
        }
        // 修改库位状态:   F.在库 ====>>> R.出库预约/P.拣料/盘点/并板出库中
        locMast = locMastService.selectById(taskDto.getLocNo());
        if (locMast.getLocSts().equals("F")) {
            locMast.setLocSts(ioType == 101 ? "R" : "P");
            locMast.setModiUser(userId);
            locMast.setModiTime(now);
            if (!locMastService.updateById(locMast)) {
                throw new CoolException("预约库位状态失败,库位号:" + taskDto.getLocNo());
            }
        } else {
            throw new CoolException(taskDto.getLocNo() + "库位不是在库状态");
        }
    }
    private String[] GetOrderNo(String orderNo) {
        String[] s3 = orderNo.split("\"");
        String[] s = new String[(s3.length - 1) / 6];
        if (!Cools.isEmpty(s3)) {
            int i = 0;
            int j = 0;
            for (String ss : s3) {
                if (ss.equals("orderNo")) {
                    s[i] = s3[j + 2];
                    i++;
                }
                j++;
            }
        } else {
            s = null;
        }
        return s;
    }
    private String[] GetAnfme(String orderNo) {
        String[] s3 = orderNo.split("\"");
        String[] s = new String[(s3.length - 1) / 6];
        int i = 0;
        int j = 0;
        for (String ss : s3) {
            if (ss.equals("anfme")) {
                String[] s4 = s3[j + 1].split(":");
                String[] s5 = s4[1].split("\\.");
                s[i] = s5[0];
                i++;
            }
            j++;
        }
        return s;
    }
    @Override
@@ -276,7 +1342,9 @@
        wrkMast.setIoTime(new Date());
        wrkMast.setWrkSts(1L); // 工作状态:生成入库ID
        wrkMast.setIoType(10); // 入出库状态:10.空板入库
        wrkMast.setIoPri(10D); // 优先级:10
        Double ioPri = wrkMastService.getIoPri(10, dto.getLocNo());
        wrkMast.setIoPri(ioPri); // 优先级:10
        wrkMast.setOutMost(locMastService.isOutMost(dto.getLocNo(), true) ? 1 : 0);
        wrkMast.setCrnNo(dto.getCrnNo());
        wrkMast.setSourceStaNo(dto.getSourceStaNo());
        wrkMast.setStaNo(dto.getStaNo());
@@ -300,20 +1368,20 @@
        sourceStaNo.setWrkNo(workNo);
        sourceStaNo.setModiUser(userId);
        sourceStaNo.setModiTime(new Date());
        if (!basDevpService.updateById(sourceStaNo)){
        if (!basDevpService.updateById(sourceStaNo)) {
            throw new CoolException("更新源站失败");
        }
        // 更新目标库位状态
        LocMast locMast = locMastService.selectById(dto.getLocNo());
        if (locMast.getLocSts().equals("O")){
        if (locMast.getLocSts().equals("O")) {
            locMast.setLocSts("S"); // S.入库预约
            locMast.setModiUser(userId);
            locMast.setModiTime(new Date());
            if (!locMastService.updateById(locMast)){
            if (!locMastService.updateById(locMast)) {
                throw new CoolException("改变库位状态失败");
            }
        } else {
            throw new CoolException(dto.getLocNo()+"目标库位已被占用");
            throw new CoolException(dto.getLocNo() + "目标库位已被占用");
        }
        return dto.getLocNo();
    }
@@ -325,12 +1393,22 @@
            throw new CoolException("站点不存在");
        }
        for (String locNo : param.getLocNos()) {
            // 如果为深库位,且对应浅库位为X.禁用,则略过此库位
            if (Utils.isDeepLoc(slaveProperties, locNo)) {
                String shallowLoc = Utils.getShallowLoc(slaveProperties, locNo);
                if (locMastService.selectById(shallowLoc).getLocSts().equals("X")) {
                    continue;
                }
            }
            // 获取工作号
            int workNo = commonService.getWorkNo(WorkNoType.PAKOUT.type);
            // 获取库位
            LocMast locMast = locMastService.selectById(locNo);
            if (Cools.isEmpty(locMast)) {
                throw new CoolException(locNo+"库位不存在");
                throw new CoolException(locNo + "库位不存在");
            }
            if (!locMast.getLocSts().equals("D")) {
                throw new CoolException("所选库位存在状态不为D的库位,库位号:" + locMast.getLocNo() + " 、当前状态:" + locMast.getLocSts() + "-" + locMast.getLocSts$());
            }
            // 获取源站
            Wrapper<StaDesc> wrapper = new EntityWrapper<StaDesc>()
@@ -338,6 +1416,9 @@
                    .eq("stn_no", param.getOutSite())
                    .eq("crn_no", locMast.getCrnNo());
            StaDesc staDesc = staDescService.selectOne(wrapper);
            if (Cools.isEmpty(staDesc)) {
                throw new CoolException("非法路径!");
            }
            Integer sourceStaNo = staDesc.getCrnStn();
            if (Cools.isEmpty(sourceStaNo)) {
                throw new CoolException("检索源站失败");
@@ -348,9 +1429,11 @@
            wrkMast.setIoTime(new Date());
            wrkMast.setWrkSts(11L); // 工作状态:11.生成出库ID
            wrkMast.setIoType(110); // 入出库状态: 110.空板出库
            wrkMast.setIoPri(10D);
            Double ioPri = wrkMastService.getIoPri(110, locNo);
            wrkMast.setIoPri(ioPri);
            wrkMast.setSourceStaNo(sourceStaNo); // 源站
            wrkMast.setStaNo(param.getOutSite()); // 目标站
            wrkMast.setOutMost(locMastService.isOutMost(locNo, false) ? 1 : 0);
            wrkMast.setCrnNo(locMast.getCrnNo());
            wrkMast.setSourceLocNo(locNo); // 源库位
            wrkMast.setFullPlt("N"); // 满板:Y
@@ -367,7 +1450,7 @@
                throw new CoolException("保存工作档失败");
            }
            // 更新库位状态 D.空板 -> R.出库预约
            if (locMast.getLocSts().equals("D")){
            if (locMast.getLocSts().equals("D")) {
                locMast.setLocSts("R");
                locMast.setModiUser(userId);
                locMast.setModiTime(new Date());
@@ -375,29 +1458,34 @@
                    throw new CoolException("更新库位状态失败");
                }
            }
            // todo 更新站点信息(工作号)
        }
    }
    @Override
    @Transactional
    public void locCheckOut(StockOutParam param, Long userId) {
//         if (wrkMastService.selectCount(new EntityWrapper<WrkMast>().eq("io_type", 107)) > 0) {
//             throw new CoolException("只能存在一笔盘点任务");
//         }
        // 目标站点状态检测
        BasDevp staNo = basDevpService.checkSiteStatus(param.getOutSite());
        // 获取库位明细
        List<LocDetlDto> locDetlDtos = new ArrayList<>();
        for (StockOutParam.LocDetl paramLocDetl : param.getLocDetls()) {
            if (!Cools.isEmpty(paramLocDetl.getLocNo(), paramLocDetl.getMatnr())) {
                LocDetl sqlParam = new LocDetl();
                sqlParam.setLocNo(paramLocDetl.getLocNo());
                sqlParam.setMatnr(paramLocDetl.getMatnr());
                LocDetl one = locDetlService.selectOne(new EntityWrapper<>(sqlParam));
                if (null != one) locDetlDtos.add(new LocDetlDto(one, one.getAnfme()));
            if (!Cools.isEmpty(paramLocDetl.getLocNo(), paramLocDetl.getMatnr(), paramLocDetl.getCount())) {
                LocDetl one = locDetlService.selectItem(paramLocDetl.getLocNo(), paramLocDetl.getMatnr(), paramLocDetl.getBatch());
                if (null != one) locDetlDtos.add(new LocDetlDto(one, paramLocDetl.getCount()));
            }
        }
        if (!locDetlDtos.isEmpty()) {
            // 启动出库开始 107.盘点出库
            stockOut(staNo, locDetlDtos, 107, userId, null, false, null);
            LocMast locMast = locMastService.selectOne(new EntityWrapper<LocMast>().eq("loc_no", locDetlDtos.get(0).getLocDetl().getLocNo()));
            if (locMast.getLocSts().equals("F")) {
                // 启动出库开始 107.盘点出库
                stockOut(staNo, locDetlDtos, IoWorkType.CHECK_OUT, userId);
            } else {
                throw new CoolException("所选库位存在状态不为F的库位,库位号:" + locMast.getLocNo() + " 、当前状态:" + locMast.getLocSts() + "-" + locMast.getLocSts$());
            }
        } else {
            throw new CoolException("库位物料不存在");
        }
@@ -407,54 +1495,60 @@
    @Transactional
    public void locMove(String sourceLocNo, String locNo, Long userId) {
        LocMast sourceLoc = locMastService.selectById(sourceLocNo);
        if (Cools.isEmpty(sourceLoc)){
        List<LocDetl> locDetls = locDetlService.selectList(new EntityWrapper<LocDetl>().eq("loc_no", sourceLocNo));
        if (Cools.isEmpty(sourceLoc)) {
            throw new CoolException("未找到库位");
        }
        LocMast loc = locMastService.selectById(locNo);
        if (Cools.isEmpty(loc)){
        if (Cools.isEmpty(loc)) {
            throw new CoolException("未找到库位");
        }
        if (!sourceLoc.getCrnNo().equals(loc.getCrnNo())) {
            throw new CoolException("移转库位属于不同堆垛机");
        if (!loc.getLocSts().equals("O") || (!sourceLoc.getLocSts().equals("F") && !sourceLoc.getLocSts().equals("D"))) {
            throw new CoolException("库位状态已改变");
        }
        if (!sourceLoc.getCrnNo().equals(loc.getCrnNo())) {
//            throw new CoolException("移转库位属于不同堆垛机");   todo:luxiaotao
        }
        Date now = new Date();
        // 获取工作号
        int workNo = commonService.getWorkNo(WorkNoType.PICK.type);
        // 保存工作档
        WrkMast wrkMast = new WrkMast();
        wrkMast.setWrkNo(workNo);
        wrkMast.setIoTime(new Date());
        wrkMast.setIoTime(now);
        wrkMast.setWrkSts(11L); // 工作状态:11.生成出库ID
        wrkMast.setIoType(11); // 入出库状态: 11.库格移载
        wrkMast.setIoPri(10D);
        wrkMast.setOutMost(locMastService.isOutMost(locNo, false) ? 1 : 0);
        wrkMast.setCrnNo(sourceLoc.getCrnNo());
        wrkMast.setSourceLocNo(sourceLocNo); // 源库位
        wrkMast.setLocNo(locNo); // 目标库位
        wrkMast.setFullPlt("N"); // 满板:Y
        wrkMast.setFullPlt(Cools.isEmpty(locDetls) ? "N" : "Y"); // 满板:Y
        wrkMast.setPicking("N"); // 拣料
        wrkMast.setExitMk("N"); // 退出
        wrkMast.setEmptyMk(sourceLoc.getLocSts().equals("D")?"Y":"N"); // 空板
        wrkMast.setEmptyMk(sourceLoc.getLocSts().equals("D") ? "Y" : "N"); // 空板
        wrkMast.setBarcode(sourceLoc.getBarcode()); // 托盘码
        wrkMast.setLinkMis("N");
        wrkMast.setAppeUser(userId);
        wrkMast.setAppeTime(new Date());
        wrkMast.setAppeTime(now);
        wrkMast.setModiUser(userId);
        wrkMast.setModiTime(new Date());
        wrkMast.setModiTime(now);
        boolean res = wrkMastService.insert(wrkMast);
        if (!res) {
            throw new CoolException("保存工作档失败");
        }
        // 工作档明细保存
        List<LocDetl> locDetls = locDetlService.selectList(new EntityWrapper<LocDetl>().eq("loc_no", sourceLocNo));
        for (LocDetl locDetl : locDetls) {
            WrkDetl wrkDetl = new WrkDetl();
            wrkDetl.sync(locDetl);
            wrkDetl.setWrkNo(workNo);
            wrkDetl.setIoTime(new Date());
            wrkDetl.setIoTime(now);
            wrkDetl.setAnfme(locDetl.getAnfme());
            VersionUtils.setWrkDetl(wrkDetl, locDetl); // 版本控制
            wrkDetl.setAppeTime(new Date());
            wrkDetl.setAppeTime(now);
            wrkDetl.setAppeUser(userId);
            wrkDetl.setModiTime(new Date());
            wrkDetl.setModiTime(now);
            wrkDetl.setModiUser(userId);
            wrkDetl.setMatType(locDetl.getMatType());
            if (!wrkDetlService.insert(wrkDetl)) {
                throw new CoolException("保存工作档明细失败");
            }
@@ -463,23 +1557,23 @@
        if (sourceLoc.getLocSts().equals("D") || sourceLoc.getLocSts().equals("F")) {
            sourceLoc.setLocSts("R"); // R.出库预约
            sourceLoc.setModiUser(userId);
            sourceLoc.setModiTime(new Date());
            if (!locMastService.updateById(sourceLoc)){
            sourceLoc.setModiTime(now);
            if (!locMastService.updateById(sourceLoc)) {
                throw new CoolException("更新源库位状态失败");
            }
        } else {
            throw new CoolException("源库位出库失败,状态:"+sourceLoc.getLocSts$());
            throw new CoolException(sourceLoc.getLocNo() + "源库位出库失败,状态:" + sourceLoc.getLocSts$());
        }
        // 修改目标库位状态
        if (loc.getLocSts().equals("O")) {
            loc.setLocSts("S"); // S.入库预约
            loc.setModiTime(new Date());
            loc.setModiTime(now);
            loc.setModiUser(userId);
            if (!locMastService.updateById(loc)) {
                throw new CoolException("更新目标库位状态失败");
            }
        } else {
            throw new CoolException("移转失败,目标库位状态:"+loc.getLocSts$());
            throw new CoolException("移转失败,目标库位状态:" + loc.getLocSts$());
        }
    }
@@ -487,18 +1581,19 @@
    @Transactional
    public void completeWrkMast(String workNo, Long userId) {
        WrkMast wrkMast = wrkMastService.selectById(workNo);
        if (Cools.isEmpty(wrkMast)){
            throw new CoolException(workNo+"工作档不存在");
        if (Cools.isEmpty(wrkMast)) {
            throw new CoolException(workNo + "工作档不存在");
        }
        if (wrkMast.getWrkSts() == 4 || wrkMast.getWrkSts() == 14) {
        if (wrkMast.getWrkSts() == 9 || wrkMast.getWrkSts() == 17) {
            throw new CoolException("当前工作档已完成");
        }
        // 入库 + 库位转移
        if (wrkMast.getWrkSts() < 4 || (wrkMast.getWrkSts() > 10 && wrkMast.getIoType()==11)) {
            wrkMast.setWrkSts(4L);
        // 出库
        if (wrkMast.getWrkSts() < 9 || (wrkMast.getWrkSts() > 10 && wrkMast.getIoType() == 11)) {
            wrkMast.setWrkSts(9L);
            // 出库
        } else if (wrkMast.getWrkSts() > 10) {
            wrkMast.setWrkSts(14L);
            wrkMast.setSteNo(0);
            wrkMast.setWrkSts(17L);
        }
        Date now = new Date();
        wrkMast.setCrnStrTime(DateUtils.calculate(now, 1L, TimeUnit.SECONDS, true));
@@ -515,174 +1610,179 @@
    @Override
    @Transactional
    public void adjustLocDetl(LocDetlAdjustParam param, Long userId) {
        param.integrate();
        LocMast locMast = locMastService.selectById(param.getLocNo());
        if (Cools.isEmpty(locMast)) {
            throw new CoolException("库位不存在");
        }
        for (LocDetlAdjustParam.LocDetlAdjust adjust : param.getList()) {
            if (Cools.isEmpty(adjust.getMatnr())) {
                throw new CoolException(BaseRes.PARAM);
            }
            LocDetl sqlParam = new LocDetl();
            sqlParam.setLocNo(locMast.getLocNo());
            sqlParam.setMatnr(adjust.getMatnr());
            LocDetl one = locDetlService.selectOne(new EntityWrapper<>(sqlParam));
            // 保存新库存明细
            if (Cools.isEmpty(one)) {
                if (adjust.getCount() == 0){
        if (!(locMast.getLocSts().equals("F") || locMast.getLocSts().equals("D") || locMast.getLocSts().equals("O"))) {
            throw new CoolException("当前库位不可调整!库位状态:" + locMast.getLocSts$());
        }
        Date now = new Date();
        List<LocDetl> locDetls = locDetlService.selectList(new EntityWrapper<LocDetl>().eq("loc_no", param.getLocNo()));
        List<LocDetlAdjustParam.LocDetlAdjust> list = param.getList();
        // 修改数量
        Iterator<LocDetl> iterator = locDetls.iterator();
        while (iterator.hasNext()) {
            LocDetl locDetl = iterator.next();
            Iterator<LocDetlAdjustParam.LocDetlAdjust> iterator1 = list.iterator();
            while (iterator1.hasNext()) {
                LocDetlAdjustParam.LocDetlAdjust adjust = iterator1.next();
                if (adjust.getCount() == 0) {
                    continue;
                }
                MatCode matCode = matCodeService.selectById(adjust.getMatnr());
                LocDetl locDetl = new LocDetl();
                locDetl.setLocNo(locMast.getLocNo());
                locDetl.setAnfme(adjust.getCount()); // 数量
                VersionUtils.setLocDetl(locDetl, matCode); // 版本控制
                locDetl.setModiUser(userId); // 操作人员信息
                locDetl.setModiTime(new Date());
                locDetl.setAppeUser(userId);
                locDetl.setAppeTime(new Date());
                if (!locDetlService.insert(locDetl)) {
                    throw new CoolException("保存库存明细失败");
                }
                // 保存调整记录
                AdjDetl adjDetl = new AdjDetl();
                adjDetl.setLocNo(locDetl.getLocNo());
                adjDetl.setMatNo(locDetl.getMatnr());
                adjDetl.setOriQty(0.0D);
                adjDetl.setAdjQty(adjust.getCount());
                adjDetlService.save(adjDetl, userId);
                // 上报erp
                String docNum = "CS-" + String.valueOf(snowflakeIdWorker.nextId()).substring(0, 15);
                Integer docId = 14; // 报溢单
                List<BillDto> dtos = new ArrayList<>();
                BillDto billDto = new BillDto();
                billDto.setMatnr(adjust.getMatnr());
                billDto.setQty(Math.abs(adjust.getCount()));
                dtos.add(billDto);
                Result result = wmsService.erpUpload(dtos, docId, docNum);
                if (result.getCode() != 200) {
                    throw new CoolException("库存调整失败,原因:无法上报至ERP");
                }
            // 修改原库存明细
            } else {
                // 如果数量修改,则更新库存明细
                if (!adjust.getCount().equals(one.getAnfme())) {
                    // 当数量被修改为 0 时,直接清除库存明细
                    if (adjust.getCount() == 0) {
                        // 上报erp
                        String docNum = "CD-" + String.valueOf(snowflakeIdWorker.nextId()).substring(0, 15);
                        Integer docId = 9; // 报损单
                        List<BillDto> dtos = new ArrayList<>();
                        BillDto billDto = new BillDto();
                        billDto.setMatnr(adjust.getMatnr());
                        billDto.setQty(Math.abs(one.getAnfme()));
                        dtos.add(billDto);
                        Result result = wmsService.erpUpload(dtos, docId, docNum);
                        if (result.getCode() != 200) {
                            throw new CoolException("库存调整失败,原因:无法上报至ERP");
                        }
                        // 删除库存
                        if (!locDetlService.delete(new EntityWrapper<>(one))) {
                            throw new CoolException("清除库存明细失败");
                        }
                    } else {
                        LocDetl sqlParam1 = new LocDetl();
                        sqlParam1.setAnfme(adjust.getCount());
                        sqlParam1.setModiTime(new Date());
                        sqlParam1.setModiUser(userId);
                        if (!locDetlService.update(sqlParam1, new EntityWrapper<LocDetl>()
                                .eq("loc_no", locMast.getLocNo())
                                .eq("matnr", adjust.getMatnr()))) {
                            throw new CoolException("修改库存明细失败");
                        }
                        // 上报erp
                        String docNum;
                        Integer docId;
                        if (one.getAnfme() > adjust.getCount()) {
                            // 报损
                            docNum = "CD-" + String.valueOf(snowflakeIdWorker.nextId()).substring(0, 15);
                            docId = 9;
                        } else {
                            // 报溢
                            docNum = "CS-" + String.valueOf(snowflakeIdWorker.nextId()).substring(0, 15);
                            docId = 14;
                        }
                        List<BillDto> dtos = new ArrayList<>();
                        BillDto billDto = new BillDto();
                        billDto.setMatnr(adjust.getMatnr());
                        billDto.setQty(Math.abs(one.getAnfme() - adjust.getCount()));
                        dtos.add(billDto);
                        Result result = wmsService.erpUpload(dtos, docId, docNum);
                        if (result.getCode() != 200) {
                            throw new CoolException("库存调整失败,原因:无法上报至ERP");
                        }
                if (locDetl.getMatnr().equals(adjust.getMatnr()) && Cools.eq(locDetl.getBatch(), adjust.getBatch())) {
                    // todo 盘点记录
                    // 修改库存
                    if (!locDetlService.updateAnfme(adjust.getCount(), locDetl.getLocNo(), locDetl.getMatnr(), locDetl.getBatch(), adjust.getManu(), adjust.getMemo())) {
                        throw new CoolException(locDetl.getLocNo() + "库位," + locDetl.getMatnr() + "商品," + locDetl.getBatch() + "序列码修改数量失败");
                    }
                    // 保存调整记录
                    AdjDetl adjDetl = new AdjDetl();
                    adjDetl.setLocNo(locMast.getLocNo());
                    adjDetl.setMatNo(adjust.getMatnr());
                    adjDetl.setOriQty(one.getAnfme());
                    adjDetl.setLocNo(locDetl.getLocNo());
                    adjDetl.setMatnr(locDetl.getMatnr());
                    adjDetl.setSpecs(locDetl.getSpecs());
                    adjDetl.setBatch(locDetl.getBatch());
                    adjDetl.setOriQty(locDetl.getAnfme());
                    adjDetl.setAdjQty(adjust.getCount());
                    adjDetl.setMemo(adjust.getMemo());
                    adjDetl.setManu(adjust.getManu());
                    adjDetl.setModiTime(now);
                    adjDetl.setModiUser(userId);
                    adjDetl.setAppeTime(now);
                    adjDetl.setAppeUser(userId);
                    adjDetlService.save(adjDetl, userId);
                    iterator.remove();
                    iterator1.remove();
                }
            }
        }
        List<LocDetl> locDetls = locDetlService.selectList(new EntityWrapper<LocDetl>().eq("loc_no", param.getLocNo()));
        if (locDetls.isEmpty()) {
            locMast.setLocSts("D");
            locMast.setModiUser(userId);
            locMast.setModiTime(new Date());
            if (!locMastService.updateById(locMast)) {
                throw new CoolException("更新库位状态失败");
        // 删除库存
        for (LocDetl locDetl : locDetls) {
            // todo 盘点记录
            if (!locDetlService.updateAnfme(-1.0D, locDetl.getLocNo(), locDetl.getMatnr(), locDetl.getBatch())) {
                throw new CoolException("删除" + locDetl.getLocNo() + "库位," + locDetl.getMatnr() + "商品," + locDetl.getBatch() + "序列码库存明细失败");
            }
            // 保存调整记录
            AdjDetl adjDetl = new AdjDetl();
            adjDetl.setLocNo(locDetl.getLocNo());
            adjDetl.setMatnr(locDetl.getMatnr());
            adjDetl.setBatch(locDetl.getBatch());
            adjDetl.setOriQty(locDetl.getAnfme());
            adjDetl.setAdjQty(0.0D);
            adjDetl.setModiTime(now);
            adjDetl.setModiUser(userId);
            adjDetl.setAppeTime(now);
            adjDetl.setAppeUser(userId);
            adjDetlService.save(adjDetl, userId);
        }
        // 添加库存
        for (LocDetlAdjustParam.LocDetlAdjust adjust : list) {
            if (adjust.getCount() == 0.0D) {
                continue;
            }
            Mat mat = matService.selectByMatnr(adjust.getMatnr());
            LocDetl locDetl = new LocDetl();
            locDetl.sync(mat);
            locDetl.setBatch(adjust.getBatch());
            locDetl.setLocNo(locMast.getLocNo());
            locDetl.setAnfme(adjust.getCount()); // 数量
            locDetl.setMemo(adjust.getMemo());
            locDetl.setManu(adjust.getManu());
            locDetl.setModiUser(userId); // 操作人员信息
            locDetl.setModiTime(now);
            locDetl.setAppeUser(userId);
            locDetl.setAppeTime(now);
            if (!locDetlService.insert(locDetl)) {
                throw new CoolException("添加" + locDetl.getLocNo() + "库位," + locDetl.getMatnr() + "商品," + locDetl.getBatch() + "序列码库存明细失败");
            }
            // 保存调整记录
            AdjDetl adjDetl = new AdjDetl();
            adjDetl.setLocNo(locMast.getLocNo());
            adjDetl.setMatnr(adjust.getMatnr());
            adjDetl.setBatch(adjust.getBatch());
            adjDetl.setMemo(adjust.getMemo());
            adjDetl.setManu(adjust.getManu());
            adjDetl.setOriQty(0.0D);
            adjDetl.setAdjQty(adjust.getCount());
            adjDetl.setModiTime(now);
            adjDetl.setModiUser(userId);
            adjDetl.setAppeTime(now);
            adjDetl.setAppeUser(userId);
            adjDetlService.save(adjDetl, userId);
        }
        // 修改库位状态
        int count = locDetlService.selectCount(new EntityWrapper<LocDetl>().eq("loc_no", locMast.getLocNo()));
        if (locMast.getLocSts().equals("F")) {
            if (count == 0) {
                locMast.setLocSts("D");
            }
        }
        if (locMast.getLocSts().equals("D") || locMast.getLocSts().equals("O")) {
            if (count > 0) {
                locMast.setLocSts("F");
            }
        }
        locMast.setModiUser(userId);
        locMast.setModiTime(new Date());
        if (!locMastService.updateById(locMast)) {
            throw new CoolException("更新库位状态失败");
        }
    }
    @Override
    @Transactional
    public void cancelWrkMast(String workNo, Long userId) {
        WrkMast wrkMast = wrkMastService.selectById(workNo);
        if (Cools.isEmpty(wrkMast)){
            throw new CoolException(workNo+"工作档不存在");
        if (Cools.isEmpty(wrkMast)) {
            throw new CoolException(workNo + "工作档不存在");
        }
        String locNo = ""; // 待修改目标库位
        String locSts = ""; // 待修改目标库位状态
        // 入库取消(修改目标库位)
        if (wrkMast.getWrkSts() < 4) {
        if (wrkMast.getWrkSts() < 9) {
            locNo = wrkMast.getLocNo();
            locSts = "O";
            /**
             * 库位转移
             * 取消后 源库位 ==>> F.在库
             *       目标库位 ===>> O.空库位
             **/
            if (wrkMast.getIoType() == 11) {
            // 库位转移
            if (wrkMast.getIoType() == 11 || wrkMast.getIoType() == 111) {
                // 库位转移:源库位
                LocMast locMast = locMastService.selectById(wrkMast.getSourceLocNo());
                if (Cools.isEmpty(locMast)) {
                    throw new CoolException("取消库位转移失败,源库位不存在:"+ wrkMast.getSourceLocNo());
                    throw new CoolException("取消库位转移失败,源库位不存在:" + wrkMast.getSourceLocNo());
                }
                locMast.setLocSts("F");
                locMast.setModiTime(new Date());
                locMast.setModiUser(userId);
                locMastService.updateById(locMast);
            }
        // 出库取消(修改源库位)
        } else if (wrkMast.getWrkSts() > 10 && wrkMast.getWrkSts() != 14) {
            // 出库取消(修改源库位)
        } else if (wrkMast.getWrkSts() > 10 && wrkMast.getWrkSts() != 19) {
            locNo = wrkMast.getSourceLocNo();
            // 出库 ===>> F.在库
            if (wrkMast.getIoType() > 100 && wrkMast.getIoType() != 110) {
                locSts = "F";
            // 空板出库 ===>> D.空桶/空栈板
                // 空板出库 ===>> D.空桶/空栈板
            } else if (wrkMast.getIoType() == 110) {
                locSts = "D";
            // 库位转移 ===>> D.空桶/空栈板
            } else if (wrkMast.getIoType() == 11) {
                locSts = "F";
                // 库位转移 ===>> D.空桶/空栈板
            } else if (wrkMast.getIoType() == 11 || wrkMast.getIoType() == 111) {
                if (wrkMast.getWrkSts() > 11) {
                    throw new CoolException("移库任务已在执行中,不能取消");
                }
                locSts = wrkMast.getFullPlt().equalsIgnoreCase("N") ? "D" : "F";
                // 库位转移:目标库位
                LocMast locMast = locMastService.selectById(wrkMast.getLocNo());
                if (Cools.isEmpty(locMast)) {
                    throw new CoolException("取消库位转移失败,目标库位不存在:"+ wrkMast.getSourceLocNo());
                    throw new CoolException("取消库位转移失败,目标库位不存在:" + wrkMast.getSourceLocNo());
                }
                locMast.setLocSts("O");
                locMast.setModiTime(new Date());
@@ -691,6 +1791,50 @@
            }
        } else {
            throw new CoolException("当前工作状态无法取消");
        }
        // 订单关联
        List<WrkDetl> wrkDetls = wrkDetlService.selectByWrkNo(wrkMast.getWrkNo());
        for (WrkDetl wrkDetl : wrkDetls) {
            if (!Cools.isEmpty(wrkDetl.getOrderNo())) {
                String[] orderNos = GetOrderNo(wrkDetl.getOrderNo());
                if (!Cools.isEmpty(orderNos) && orderNos.length != 0) {
                    String[] anfmes = GetAnfme(wrkDetl.getOrderNo());
                    int i = 0;
                    for (String orderNo : orderNos) {
                        if (!Cools.isEmpty(orderNo)) {
                            Double anfme = Integer.parseInt(anfmes[i]) + 0.0;
                            i++;
                            if (!BaseController.isJSON(wrkDetl.getOrderNo())) {
                                if (!orderDetlService.decrease(orderNo, wrkDetl.getMatnr(), wrkDetl.getBatch(), anfme)) {
                                    throw new CoolException("订单数据回滚失败");
                                }
                            } else {
                                // 订单合并出库
                                List<OrderDto> orderDtoList = JSON.parseArray(wrkDetl.getOrderNo(), OrderDto.class);
                                for (OrderDto orderDto : orderDtoList) {
                                    if (!orderDetlService.decrease(orderDto.getOrderNo(), wrkDetl.getMatnr(), wrkDetl.getBatch(), orderDto.getAnfme())) {
                                        throw new CoolException("订单数据回滚失败");
                                    }
                                }
                            }
                        }
                    }
                } else {
                    if (!BaseController.isJSON(wrkDetl.getOrderNo())) {
                        if (!orderDetlService.decrease(wrkDetl.getOrderNo(), wrkDetl.getMatnr(), wrkDetl.getBatch(), wrkDetl.getAnfme())) {
                            throw new CoolException("订单数据回滚失败");
                        }
                    } else {
                        // 订单合并出库
                        List<OrderDto> orderDtoList = JSON.parseArray(wrkDetl.getOrderNo(), OrderDto.class);
                        for (OrderDto orderDto : orderDtoList) {
                            if (!orderDetlService.decrease(orderDto.getOrderNo(), wrkDetl.getMatnr(), wrkDetl.getBatch(), orderDto.getAnfme())) {
                                throw new CoolException("订单数据回滚失败");
                            }
                        }
                    }
                }
            }
        }
        // 取消操作人员记录
        wrkMast.setManuType("手动取消");
@@ -709,7 +1853,7 @@
        if (wrkMast.getIoType() != 10 && wrkMast.getIoType() != 110) {
            // 保存工作明细档历史档
            if (!wrkDetlLogService.save(wrkMast.getWrkNo())) {
                throw new CoolException("保存工作明细历史档失败, workNo = " + wrkMast.getWrkNo());
//                throw new CoolException("保存工作明细历史档失败, workNo = " + wrkMast.getWrkNo());
            }
            // 删除工作档明细
            boolean wrkDetlRes = wrkDetlService.delete(new EntityWrapper<WrkDetl>().eq("wrk_no", workNo));
@@ -718,7 +1862,7 @@
        // 修改库位状态
        LocMast locMast = locMastService.selectById(locNo);
        if (Cools.isEmpty(locMast)) {
            throw new CoolException("取消工作档失败,库位不存在:"+ locNo);
            throw new CoolException("取消工作档失败,库位不存在:" + locNo);
        }
        locMast.setLocSts(locSts);
        locMast.setModiTime(new Date());
@@ -727,14 +1871,15 @@
        if (!wrkMastRes || !locMastRes) {
            throw new CoolException("保存数据失败");
        }
        waitPakinService.delete(new EntityWrapper<WaitPakin>().eq("zpallet", wrkMast.getBarcode()));
    }
    @Override
    @Transactional
    public void pickWrkMast(String workNo, Long userId) {
        WrkMast wrkMast = wrkMastService.selectById(workNo);
        if (Cools.isEmpty(wrkMast)){
            throw new CoolException(workNo+"工作档不存在");
        if (Cools.isEmpty(wrkMast)) {
            throw new CoolException(workNo + "工作档不存在");
        }
        // 入出库类型判断
        if (wrkMast.getIoType() != 103 && wrkMast.getIoType() != 104 && wrkMast.getIoType() != 107) {
@@ -745,9 +1890,9 @@
            throw new CoolException("当前工作状态无法进行操作");
        }
        // 保存工作明细档历史档
        if (!wrkDetlLogService.save(wrkMast.getWrkNo())) {
            throw new CoolException("保存工作明细档历史档失败");
        }
//        if (!wrkDetlLogService.save(wrkMast.getWrkNo())) {
//            throw new CoolException("保存工作明细档历史档失败");
//        }
        // 保存工作主档历史档
        if (!wrkMastLogService.save(wrkMast.getWrkNo())) {
            throw new CoolException("保存工作主档历史档失败");
@@ -785,4 +1930,148 @@
        }
    }
    @Override
    public StartupDto createWaitPainWrkMastStart(List<WaitPakin> list, Long userId) {
        if (Cools.isEmpty(list)) {
            throw new CoolException("入库通知档不能为空");
        }
        LocTypeDto locTypeDto = new LocTypeDto();
        locTypeDto.setLocType1((short) 1);
        return wcsController.startupFullPutStore(301, list.get(0).getZpallet(), locTypeDto, list);
    }
    @Override
    @Transactional
    public String dealPreHaveStart(Integer wrkNo, Long userId) {
        if (true) {
            throw new CoolException("功能停用");
        }
        return "";
    }
    @Override
    @Transactional
    public void shuttleTransfer(List<String> locNos) {
        if (Cools.isEmpty(locNos)) {
            return;
        }
//        if (true && !Cools.isEmpty(locNos)) {
//            throw new CoolException("暂不支持移库任务");    // todo:luxiaotao 移库开关
//        }
        LocMast one = locMastService.selectById(locNos.get(0));
        List<Integer> rows = Utils.getGroupLoc(Integer.parseInt(locNos.get(0).substring(0, 2)));
        List<LocMast> locMasts = locMastService.queryFreeLocMast(rows, rows.size(), one.getLocType1());
        if (Cools.isEmpty(locMasts)) {
            throw new CoolException("库位移转失败,已无空库位");
        }
        // 入库排序 深库位 ==> 浅库位
        switch (locMasts.get(0).getRow1()) {
            case 4:
            case 5:
            case 6:
            case 7:
            case 12:
            case 13:
            case 14:
            case 19:
            case 20:
            case 21:
                Collections.reverse(locMasts);
                break;
            default:
                break;
        }
        if (locNos.size() > locMasts.size()) {
            throw new CoolException("服务器错误");
        }
        Date now = new Date();
        // 生成移库工作档
        Iterator<LocMast> iterator = locMasts.iterator();
        for (String sourceLocNo : locNos) {
            while (iterator.hasNext()) {
                LocMast sourceLoc = locMastService.selectById(sourceLocNo);
                List<LocDetl> locDetls = locDetlService.selectList(new EntityWrapper<LocDetl>().eq("loc_no", sourceLocNo));
                if (Cools.isEmpty(sourceLoc)) {
                    throw new CoolException("未找到库位");
                }
                LocMast loc = iterator.next();
                if (Cools.isEmpty(loc)) {
                    throw new CoolException("未找到库位");
                }
                if (!sourceLoc.getCrnNo().equals(loc.getCrnNo())) {
//                    throw new CoolException("移转库位属于不同堆垛机");   todo:luxiaotao
                }
                // 获取工作号
                int workNo = commonService.getWorkNo(WorkNoType.PICK.type);
                // 保存工作档
                WrkMast wrkMast = new WrkMast();
                wrkMast.setWrkNo(workNo);
                wrkMast.setIoTime(now);
                wrkMast.setWrkSts(11L); // 工作状态:11.生成出库ID
                wrkMast.setIoType(11); // 入出库状态: 11.库格移载
                wrkMast.setIoPri(15D);
                wrkMast.setOutMost(locMastService.isOutMost(loc.getLocNo(), false) ? 1 : 0);
                wrkMast.setCrnNo(sourceLoc.getCrnNo());
                wrkMast.setSourceLocNo(sourceLocNo); // 源库位
                wrkMast.setLocNo(loc.getLocNo()); // 目标库位
                wrkMast.setFullPlt(Cools.isEmpty(locDetls) ? "N" : "Y"); // 满板:Y
                wrkMast.setPicking("N"); // 拣料
                wrkMast.setExitMk("N"); // 退出
                wrkMast.setEmptyMk(sourceLoc.getLocSts().equals("D") ? "Y" : "N"); // 空板
                wrkMast.setBarcode(sourceLoc.getBarcode()); // 托盘码
                wrkMast.setLinkMis("N");
                wrkMast.setAppeUser(9527L);
                wrkMast.setAppeTime(now);
                wrkMast.setModiUser(9527L);
                wrkMast.setModiTime(now);
                boolean res = wrkMastService.insert(wrkMast);
                if (!res) {
                    throw new CoolException("保存工作档失败");
                }
                // 工作档明细保存
                for (LocDetl locDetl : locDetls) {
                    WrkDetl wrkDetl = new WrkDetl();
                    wrkDetl.sync(locDetl);
                    wrkDetl.setWrkNo(workNo);
                    wrkDetl.setIoTime(now);
                    wrkDetl.setAnfme(locDetl.getAnfme());
                    wrkDetl.setAppeTime(now);
                    wrkDetl.setAppeUser(9527L);
                    wrkDetl.setModiTime(now);
                    wrkDetl.setModiUser(9527L);
                    wrkDetl.setMatType(locDetl.getMatType());
                    if (!wrkDetlService.insert(wrkDetl)) {
                        throw new CoolException("保存工作档明细失败");
                    }
                }
                // 修改源库位状态
                if (sourceLoc.getLocSts().equals("D") || sourceLoc.getLocSts().equals("F")) {
                    sourceLoc.setLocSts("R"); // R.出库预约
                    sourceLoc.setModiUser(9527L);
                    sourceLoc.setModiTime(now);
                    if (!locMastService.updateById(sourceLoc)) {
                        throw new CoolException("更新源库位状态失败");
                    }
                } else {
                    throw new CoolException(sourceLoc.getLocNo() + "源库位出库失败,状态:" + sourceLoc.getLocSts$());
                }
                // 修改目标库位状态
                if (loc.getLocSts().equals("O")) {
                    loc.setLocSts("S"); // S.入库预约
                    loc.setModiTime(now);
                    loc.setModiUser(9527L);
                    if (!locMastService.updateById(loc)) {
                        throw new CoolException("更新目标库位状态失败");
                    }
                } else {
                    throw new CoolException("移转失败,目标库位状态:" + loc.getLocSts$());
                }
                iterator.remove();
                break;
            }
        }
    }
}