skyouc
2 天以前 0cf1c13ac668cdb689b073c3961592d8ab41316c
rsf-server/src/main/java/com/vincent/rsf/server/manager/service/impl/TaskServiceImpl.java
@@ -3,32 +3,29 @@
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.vincent.rsf.framework.common.Cools;
import com.vincent.rsf.server.api.entity.enums.OrderType;
import com.vincent.rsf.server.api.entity.enums.TaskStsType;
import com.vincent.rsf.server.api.controller.params.TaskInParam;
import com.vincent.rsf.server.manager.enums.*;
import com.vincent.rsf.framework.common.R;
import com.vincent.rsf.framework.exception.CoolException;
import com.vincent.rsf.server.api.entity.enums.TaskType;
import com.vincent.rsf.server.api.utils.LocUtils;
import com.vincent.rsf.server.manager.controller.params.GenerateTaskParams;
import com.vincent.rsf.server.manager.entity.*;
import com.vincent.rsf.server.manager.enums.PakinIOStatus;
import com.vincent.rsf.server.manager.mapper.TaskMapper;
import com.vincent.rsf.server.manager.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.vincent.rsf.server.manager.utils.LocManageUtil;
import com.vincent.rsf.server.system.constant.SerialRuleCode;
import com.vincent.rsf.server.system.enums.LocStsType;
import com.vincent.rsf.server.manager.enums.LocType;
import com.vincent.rsf.server.system.utils.SerialRuleUtils;
import lombok.Synchronized;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.*;
import java.util.stream.Collectors;
@Service("taskService")
@@ -60,6 +57,188 @@
    private DeviceBindService deviceBindService;
    @Autowired
    private WarehouseAreasService warehouseAreasService;
    @Autowired
    private WarehouseAreasItemService warehouseAreasItemService;
    @Autowired
    private WaveItemService waveItemService;
    @Autowired
    private WaveService waveService;
    @Autowired
    private BasStationService basStationService;
    @Autowired
    private TaskService taskService;
    @Autowired
    private LocItemWorkingService locItemWorkingService;
    @Override
    @Transactional(rollbackFor = Exception.class)
    public synchronized R generateAGVTasks(WaitPakin pakins, String locCode, String orgSta, Long loginUserId) {
        if (Cools.isEmpty(pakins) || Cools.isEmpty(locCode)) {
            throw new CoolException("参数不能为空!!");
        }
        /**获取组拖*/
//        List<Long> ids = pakins.stream().map(WaitPakin::getId).collect(Collectors.toList());
        List<WaitPakin> waitPakins = waitPakinService.list(new LambdaQueryWrapper<WaitPakin>()
                .in(WaitPakin::getId, pakins.getId())
                .eq(WaitPakin::getIoStatus, Short.parseShort(PakinIOStatus.PAKIN_IO_STATUS_DONE.val)));
        if (waitPakins.isEmpty()) {
            throw new CoolException("请检查组托状态是否完成!!");
        }
        waitPakins.forEach(pakin -> {
            /**获取库位*/
            String targetLoc = locCode;
            if (Cools.isEmpty(targetLoc)) {
                throw new CoolException("该站点对应库区未找到库位");
            }
            List<TaskItem> taskItems = new ArrayList<>();
            String ruleCode = SerialRuleUtils.generateRuleCode(SerialRuleCode.SYS_TASK_CODE, null);
            if (StringUtils.isBlank(ruleCode)) {
                throw new CoolException("编码错误:请确认编码「SYS_TASK_CODE」是否已生成!!");
            }
            Task task = new Task();
            task.setTaskCode(ruleCode)
                    .setTaskStatus(TaskStsType.GENERATE_IN.id.shortValue())
                    .setTaskType(TaskType.TASK_TYPE_IN.type.shortValue())
                    .setWarehType(WarehType.WAREHOUSE_TYPE_AGV.id)
                    .setTargLoc(targetLoc)
                    .setOrgSite(orgSta)
                    .setBarcode(pakin.getBarcode())
                    .setCreateBy(loginUserId)
                    .setUpdateBy(loginUserId);
            if (!this.save(task)) {
                throw new CoolException("任务保存失败!!");
            }
            if (!locService.update(new LambdaUpdateWrapper<Loc>().eq(Loc::getCode, task.getTargLoc())
                    .set(Loc::getUseStatus, LocStsType.LOC_STS_TYPE_S.type).set(Loc::getBarcode, pakin.getBarcode()))) {
                throw new CoolException("库位预约失败!!");
            }
            /**获取组拖明细**/
            List<WaitPakinItem> waitPakinItems = waitPakinItemService.list(new LambdaQueryWrapper<WaitPakinItem>().eq(WaitPakinItem::getPakinId, pakin.getId()));
            if (waitPakinItems.isEmpty()) {
                throw new CoolException("数据错误:组拖明细不存在");
            }
            waitPakinItems.forEach(item -> {
                TaskItem taskItem = new TaskItem();
                BeanUtils.copyProperties(item, taskItem);
                taskItem.setTaskId(task.getId())
                        .setOrderType(OrderType.ORDER_IN.type)
                        .setSource(item.getId())
                        .setTrackCode(item.getTrackCode())
                        .setCreateBy(loginUserId)
                        .setUpdateBy(loginUserId)
                        .setExtendFields(item.getExtendFields())
                        .setOrderId(item.getAsnId())
                        .setOrderItemId(item.getAsnItemId());
                taskItems.add(taskItem);
            });
            if (!taskItemService.saveBatch(taskItems)) {
                throw new CoolException("任务明细保存失败!!");
            }
            waitPakinItems.forEach(item -> {
                if (!waitPakinItemService.update(new LambdaUpdateWrapper<WaitPakinItem>()
                        .set(WaitPakinItem::getWorkQty, item.getAnfme())
                        .eq(WaitPakinItem::getId, item.getId()))) {
                    throw new CoolException("组托明细修执行数量修改失败!!");
                }
            });
        });
        if (!waitPakinService.update(new LambdaUpdateWrapper<WaitPakin>()
                .in(WaitPakin::getId, pakins.getId())
                .set(WaitPakin::getUpdateBy, loginUserId)
                .set(WaitPakin::getCreateBy, loginUserId)
                .set(WaitPakin::getIoStatus, PakinIOStatus.PAKIN_IO_STATUS_TASK_EXCE.val))) {
            throw new CoolException("组托状态修改失败!!");
        }
        return R.ok("任务生成完毕!");
    }
    @Override
    @Transactional(rollbackFor = Exception.class)
    public synchronized R generateFlatWarehouseTasks(WaitPakin pakins, String locCode, Long loginUserId) {
        if (Cools.isEmpty(pakins) || Cools.isEmpty(locCode)) {
            throw new CoolException("参数不能为空!!");
        }
        /**获取组拖*/
//        List<Long> ids = pakins.stream().map(WaitPakin::getId).collect(Collectors.toList());
        List<WaitPakin> waitPakins = waitPakinService.list(new LambdaQueryWrapper<WaitPakin>()
                .in(WaitPakin::getId, pakins.getId())
                .eq(WaitPakin::getIoStatus, Short.parseShort(PakinIOStatus.PAKIN_IO_STATUS_DONE.val)));
        if (waitPakins.isEmpty()) {
            throw new CoolException("请检查组托状态是否完成!!");
        }
        waitPakins.forEach(pakin -> {
            /**获取库位*/
            String targetLoc = locCode;
            if (Cools.isEmpty(targetLoc)) {
                throw new CoolException("该站点对应库区未找到库位");
            }
            List<TaskItem> taskItems = new ArrayList<>();
            String ruleCode = SerialRuleUtils.generateRuleCode(SerialRuleCode.SYS_TASK_CODE, null);
            if (StringUtils.isBlank(ruleCode)) {
                throw new CoolException("编码错误:请确认编码「SYS_TASK_CODE」是否已生成!!");
            }
            Task task = new Task();
            task.setTaskCode(ruleCode)
                    .setTaskStatus(TaskStsType.COMPLETE_IN.id.shortValue())
                    .setTaskType(TaskType.TASK_TYPE_IN.type.shortValue())
                    .setWarehType(WarehType.WAREHOUSE_TYPE_PLAT.id)
                    .setTargLoc(targetLoc)
                    .setBarcode(pakin.getBarcode())
                    .setCreateBy(loginUserId)
                    .setUpdateBy(loginUserId);
            if (!this.save(task)) {
                throw new CoolException("任务保存失败!!");
            }
            if (!locService.update(new LambdaUpdateWrapper<Loc>().eq(Loc::getCode, task.getTargLoc())
                    .set(Loc::getUseStatus, LocStsType.LOC_STS_TYPE_S.type).set(Loc::getBarcode, pakin.getBarcode()))) {
                throw new CoolException("库位预约失败!!");
            }
            /**获取组拖明细**/
            List<WaitPakinItem> waitPakinItems = waitPakinItemService.list(new LambdaQueryWrapper<WaitPakinItem>().eq(WaitPakinItem::getPakinId, pakin.getId()));
            if (waitPakinItems.isEmpty()) {
                throw new CoolException("数据错误:组拖明细不存在");
            }
            waitPakinItems.forEach(item -> {
                TaskItem taskItem = new TaskItem();
                BeanUtils.copyProperties(item, taskItem);
                taskItem.setTaskId(task.getId())
                        .setOrderType(OrderType.ORDER_IN.type)
                        .setSource(item.getId())
                        .setTrackCode(item.getTrackCode())
                        .setCreateBy(loginUserId)
                        .setUpdateBy(loginUserId)
                        .setExtendFields(item.getExtendFields())
                        .setOrderId(item.getAsnId())
                        .setOrderItemId(item.getAsnItemId());
                taskItems.add(taskItem);
            });
            if (!taskItemService.saveBatch(taskItems)) {
                throw new CoolException("任务明细保存失败!!");
            }
            waitPakinItems.forEach(item -> {
                if (!waitPakinItemService.update(new LambdaUpdateWrapper<WaitPakinItem>()
                        .set(WaitPakinItem::getWorkQty, item.getAnfme())
                        .eq(WaitPakinItem::getId, item.getId()))) {
                    throw new CoolException("组托明细修执行数量修改失败!!");
                }
            });
        });
        if (!waitPakinService.update(new LambdaUpdateWrapper<WaitPakin>()
                .in(WaitPakin::getId, pakins.getId())
                .set(WaitPakin::getUpdateBy, loginUserId)
                .set(WaitPakin::getCreateBy, loginUserId)
                .set(WaitPakin::getIoStatus, PakinIOStatus.PAKIN_IO_STATUS_TASK_EXCE.val))) {
            throw new CoolException("组拖状态修改失败!!");
        }
        return R.ok("任务生成完毕!");
    }
    /**
@@ -90,11 +269,6 @@
            throw new CoolException("未找到所属库区信息");
        }
        /**获取库位*/
        String targetLoc = LocManageUtil.getTargetLoc(warehouseArea.getId());
        if (Cools.isEmpty(targetLoc)) {
            throw new CoolException("该站点对应库区未找到库位");
        }
        /**获取组拖*/
        List<Long> ids = waitPakin.getWaitPakins().stream().map(WaitPakin::getId).collect(Collectors.toList());
        List<WaitPakin> waitPakins = waitPakinService.list(new LambdaQueryWrapper<WaitPakin>()
@@ -104,6 +278,12 @@
            throw new CoolException("请检查组拖状态是否完成!!");
        }
        waitPakins.forEach(pakin -> {
            /**获取库位*/
            String targetLoc = LocManageUtil.getTargetLoc(warehouseArea.getId());
            if (Cools.isEmpty(targetLoc)) {
                throw new CoolException("该站点对应库区未找到库位");
            }
            List<TaskItem> taskItems = new ArrayList<>();
            String ruleCode = SerialRuleUtils.generateRuleCode(SerialRuleCode.SYS_TASK_CODE, null);
            if (StringUtils.isBlank(ruleCode)) {
@@ -156,11 +336,16 @@
//                    throw new CoolException("数据错误: 单据不存在!!");
//                }
                taskItem.setTaskId(task.getId())
                        .setOrderType(OrderType.ORDER_RECEIPT.type)
                        .setOrderType(OrderType.ORDER_IN.type)
                        .setSource(item.getId())
                        .setTrackCode(item.getTrackCode())
                        .setPlatItemId(item.getPlatItemId())
                        .setPlatOrderCode(item.getPlatOrderCode())
                        .setPlatWorkCode(item.getPlatWorkCode())
                        .setProjectCode(item.getProjectCode())
                        .setCreateBy(loginUserId)
                        .setUpdateBy(loginUserId)
                        .setExtendFields(item.getExtendFields())
                        .setOrderId(item.getAsnId())
                        .setOrderItemId(item.getAsnItemId());
                taskItems.add(taskItem);
@@ -168,6 +353,13 @@
            if (!taskItemService.saveBatch(taskItems)) {
                throw new CoolException("任务明细保存失败!!");
            }
            waitPakinItems.forEach(item -> {
                if (!waitPakinItemService.update(new LambdaUpdateWrapper<WaitPakinItem>()
                        .set(WaitPakinItem::getWorkQty, item.getAnfme())
                        .eq(WaitPakinItem::getId, item.getId()))) {
                    throw new CoolException("组托明细修执行数量修改失败!!");
                }
            });
        });
        if (!waitPakinService.update(new LambdaUpdateWrapper<WaitPakin>()
@@ -182,65 +374,451 @@
    /**
     * 完成任务 更新库位明细信息,将单据库存更新到单据库存表
     *
     * @param tasks
     * @throws Exception
     */
    @Synchronized
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void completeTask(List<Task> tasks) throws Exception {
        if (tasks.isEmpty()) {
        for (Task task : tasks) {
            //出库任务
            if (task.getTaskType() < TaskType.TASK_TYPE_OUT.type) {
                if (task.getTaskType().equals(TaskType.TASK_TYPE_IN.type)) {
                    //1.入库
                    complateInstock(task);
                } else if (task.getTaskType().equals(TaskType.TASK_TYPE_PICK_IN.type)) {
                    //53.拣料再入库
                    pickComplateInStock(task);
                }
            } else {
                //出库任务
                if (task.getTaskType().equals(TaskType.TASK_TYPE_OUT.type)) {
                    //全托出库
                    complateOutStock(task);
                } else if (task.getTaskType().equals(TaskType.TASK_TYPE_PICK_AGAIN_IN.type)) {
                    //拣料出库
                    pickTask(task.getId());
                    //移除原始库存
                    complateOutStock(task);
                }
            }
        }
    }
    /**
     * 拣料再入库
     *
     * @param task
     */
    @Transactional(rollbackFor = Exception.class)
    public void pickComplateInStock(Task task) throws Exception {
        if (Objects.isNull(task)) {
            return;
        }
        List<Long> list = tasks.stream().map(Task::getId).collect(Collectors.toList());
        List<TaskItem> taskItems = taskItemService.list(new LambdaQueryWrapper<TaskItem>().in(TaskItem::getTaskId, list));
        Loc loc = locService.getOne(new LambdaQueryWrapper<Loc>().eq(Loc::getCode, task.getTargLoc()));
        if (Objects.isNull(loc)) {
            throw new CoolException("库存不存在!!");
        }
        if (!loc.getUseStatus().equals(LocStsType.LOC_STS_TYPE_S.type)) {
            throw new CoolException("当前库位状态不处于S.入库预约,不可执行入库操作!");
        }
        loc.setUseStatus(LocStsType.LOC_STS_TYPE_F.type).setBarcode(task.getBarcode());
        if (!locService.updateById(loc)) {
            throw new CoolException("库位信息更新失败!!");
        }
        List<TaskItem> taskItems = taskItemService.list(new LambdaQueryWrapper<TaskItem>().eq(TaskItem::getTaskId, task.getId()));
        if (taskItems.isEmpty()) {
            throw new CoolException("任务明细不存在!!");
        }
        List<String> locCodes = tasks.stream().map(Task::getTargLoc).collect(Collectors.toList());
        Map<Long, List<TaskItem>> listMap = taskItems.stream().collect(Collectors.groupingBy(TaskItem::getTaskId));
        /**对任务明细按任务主单进行分组*/
        listMap.keySet().forEach(key -> {
            List<TaskItem> items = listMap.get(key);
            //更新库位明细
            try {
                saveLocItem(items, key);
            } catch (Exception e) {
                throw new CoolException("库位明细更新失败!!");
        List<LocItem> items = new ArrayList<>();
        for (TaskItem taskItem : taskItems) {
            LocItem locItem = new LocItem();
            LocItemWorking locWorking = locItemWorkingService.getOne(new LambdaQueryWrapper<LocItemWorking>()
                    .eq(LocItemWorking::getTaskId, taskItem.getTaskId())
                    .eq(LocItemWorking::getLocCode, task.getTargLoc())
                    .eq(LocItemWorking::getMatnrId, taskItem.getMatnrId())
                    .eq(StringUtils.isNoneBlank(taskItem.getFieldsIndex()), LocItemWorking::getFieldsIndex, taskItem.getFieldsIndex())
                    .eq(StringUtils.isNotBlank(taskItem.getBatch()), LocItemWorking::getBatch, taskItem.getBatch()));
            if (Objects.isNull(locWorking)) {
                throw new CoolException("数据错误,作业中库存数据丢失!!");
            }
        });
            if (locWorking.getAnfme().compareTo(taskItem.getAnfme()) >= 0) {
                locWorking.setAnfme(Math.round((locWorking.getAnfme() - taskItem.getAnfme()) * 10000) / 10000.0);
            } else {
                continue;
            }
        /**对任务明细按订单进行分组*/
        Map<Long, List<TaskItem>> orderMap = taskItems.stream().collect(Collectors.groupingBy(TaskItem::getSource));
        orderMap.keySet().forEach(key -> {
            WaitPakinItem pakinItem = waitPakinItemService.getById(key);
//            AsnOrder order = asnOrderService.getOne(new LambdaQueryWrapper<AsnOrder>()
//                    .eq(AsnOrder::getId, key)
//                    .select(AsnOrder::getId, AsnOrder::getPoCode, AsnOrder::getCode));
            if (null == pakinItem) {
                throw new CoolException("数据错误:组拖数据不存在,请联系管理员!!");
            BeanUtils.copyProperties(locWorking, locItem);
            locItem.setLocCode(loc.getCode()).setLocId(loc.getId()).setId(null);
            items.add(locItem);
        }
        if (!locItemService.saveBatch(items)) {
            throw new CoolException("作业库存回写失败!!");
        }
        if (!locItemWorkingService.remove(new LambdaQueryWrapper<LocItemWorking>().eq(LocItemWorking::getTaskId, task.getId()))) {
            throw new CoolException("作业中库存删除失败!!");
        }
    }
    /**
     * 任务取消
     *
     * @param ids
     * @return
     */
    @Override
    public R removeTask(Long[] ids) {
        List<Short> longs = Arrays.asList(TaskStsType.GENERATE_IN.id, TaskStsType.GENERATE_OUT.id);
        List<Task> tasks = this.list(new LambdaQueryWrapper<Task>().in(Task::getId, ids).in(Task::getTaskStatus, longs));
        if (tasks.isEmpty()) {
            throw new CoolException("任务已处执行状态不可取消!!");
        }
        for (Task task : tasks) {
            //恢复组托状态
            WaitPakin waitPakin = waitPakinService.getOne(new LambdaQueryWrapper<WaitPakin>()
                    .eq(WaitPakin::getBarcode, task.getBarcode())
            );
            if (null != waitPakin) {
                waitPakin.setIoStatus(Short.valueOf(PakinIOStatus.PAKIN_IO_STATUS_DONE.val));
                if (!waitPakinService.updateById(waitPakin)) {
                    throw new CoolException("更新组托状态失败!!");
                }
            }
            List<TaskItem> items = orderMap.get(key);
            //保存库存明细
            Loc loc = locService.getOne(new LambdaQueryWrapper<Loc>().eq(Loc::getCode, task.getTaskType() < TaskStsType.UPDATED_IN.id ? task.getTargLoc() : task.getOrgLoc()));
            if (null != loc
                    && (loc.getUseStatus().equals(LocStsType.LOC_STS_TYPE_S.type)
                    || loc.getUseStatus().equals(LocStsType.LOC_STS_TYPE_R.type))) {
                loc.setUseStatus(LocStsType.LOC_STS_TYPE_O.type);
                if (!locService.updateById(loc)) {
                    throw new CoolException("更新库位状态失败!!");
                }
            }
            if (!Objects.isNull(task.getWarehType()) && task.getWarehType().equals(WarehType.WAREHOUSE_TYPE_AGV.id)) {
                BasStation basStation = null;
                if (task.getTaskType().equals(TaskType.TASK_TYPE_IN.type) || task.getTaskType().equals(TaskType.TASK_TYPE_EMPITY_IN.type)) {
                    basStation = basStationService.getOne(new LambdaQueryWrapper<BasStation>()
                            .eq(BasStation::getStationName, task.getOrgSite())
                            .eq(BasStation::getUseStatus, StaUseStatusType.TYPE_R.type)
                    );
                } else if (task.getTaskType().equals(TaskType.TASK_TYPE_OUT.type)
                        || task.getTaskType().equals(TaskType.TASK_TYPE_PICK_AGAIN_IN.type)
                        || task.getTaskType().equals(TaskType.TASK_TYPE_MERGE_OUT.type)
                        || task.getTaskType().equals(TaskType.TASK_TYPE_CHECK_OUT.type)
                        || task.getTaskType().equals(TaskType.TASK_TYPE_EMPITY_OUT.type)
                ) {
                    basStation = basStationService.getOne(new LambdaQueryWrapper<BasStation>()
                            .eq(BasStation::getStationName, task.getTargLoc())
                            .eq(BasStation::getUseStatus, StaUseStatusType.TYPE_R.type)
                    );
                }
                if (null == basStation) {
                    throw new CoolException("站点状态错误!!");
                }
                basStation.setUseStatus(StaUseStatusType.TYPE_F.type);
                if (!basStationService.updateById(basStation)) {
                    throw new CoolException("更新站点状态失败!!");
                }
            }
        }
        if (!this.removeByIds(Arrays.asList(ids))) {
            throw new CoolException("Delete Fail");
        }
        return R.ok("取消完成");
    }
    /**
     * 拣料出库
     *
     * @param id
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public R pickTask(Long id) throws Exception {
        Task task = this.getById(id);
        if (Objects.isNull(task)) {
            throw new CoolException("当前任务不存在!!");
        }
        if (!task.getTaskType().equals(TaskType.TASK_TYPE_PICK_AGAIN_IN.type)) {
            throw new CoolException("非拣料出库 ,不可执行此操作!!");
        }
        Loc loc = locService.getOne(new LambdaQueryWrapper<Loc>()
                .eq(Loc::getCode, task.getOrgLoc()));
        if (Objects.isNull(loc)) {
            throw new CoolException("没有空库位!!");
        }
        List<LocItem> locItems = locItemService.list(new LambdaQueryWrapper<LocItem>().eq(LocItem::getLocId, loc.getId()));
        if (locItems.isEmpty()) {
            throw new CoolException("库位明细不存在!!");
        }
        String ruleCode = SerialRuleUtils.generateRuleCode(SerialRuleCode.SYS_TASK_CODE, task);
        //更新任务主单
        task.setTaskCode(ruleCode)
                .setTaskType(TaskType.TASK_TYPE_PICK_IN.type)
                .setBarcode(task.getBarcode())
                .setTaskStatus(TaskStsType.GENERATE_IN.id);
        //TODO 后续需根据仓库类型查找新库位,原始库位置空闲状态 {
        // TaskInParam param = new TaskInParam();
        //        param.setSourceStaNo(Integer.parseInt(task.getOrgSite()))
        //                .setIoType(Integer.parseInt(TaskType.TASK_TYPE_IN.type + ""));
        ////                .setLocType1(LocType.LOC_TYPE_LOW.type);}
        if (!this.updateById(task)) {
            throw new CoolException("任务状态更新失败!!");
        }
        List<LocItemWorking> workings = new ArrayList<>();
        for (LocItem item : locItems) {
            LocItemWorking working = new LocItemWorking();
            BeanUtils.copyProperties(item, working);
            working.setId(null).setTaskId(task.getId());
            workings.add(working);
        }
        if (!locItemWorkingService.saveBatch(workings)) {
            throw new CoolException("临时库存保存失败!!");
        }
        return R.ok("拣货成功!!");
    }
    /**
     * @author Ryan
     * @date 2025/5/20
     * @description: 完成出库任务,更新出库库存信息
     * @version 1.0
     */
    @Synchronized
    @Transactional(rollbackFor = Exception.class)
    public void complateOutStock(Task task) throws Exception {
        if (Objects.isNull(task)) {
            throw new CoolException("参数不能为空!!");
        }
        Loc loc = locService.getOne(new LambdaQueryWrapper<Loc>().eq(Loc::getCode, task.getOrgLoc()));
        if (Objects.isNull(loc)) {
            throw new CoolException("库位不存在!!");
        }
        if (!loc.getUseStatus().equals(LocStsType.LOC_STS_TYPE_R.type)) {
            throw new CoolException("库位状态不处理于R.出库预约!!");
        }
        List<TaskItem> taskItems = taskItemService.list(new LambdaQueryWrapper<TaskItem>().eq(TaskItem::getTaskId, task.getId()));
        if (taskItems.isEmpty()) {
            throw new CoolException("任务明细不存在!!");
        }
        try {
            //更新库位明细
            subtractLocItem(loc);
        } catch (Exception e) {
            throw new CoolException(e.getMessage());
        }
        //添加出入库记录信息
        Map<Short, List<TaskItem>> listMap = taskItems.stream().collect(Collectors.groupingBy(TaskItem::getWkType));
        /***获取库存出库值,如果为空表示正常单据出库,非空表明是库存出库
         * 1. 库存出库没有单据信息,单据信息默认为空
         * 2. 单据库存需通过波次查询原始单据信息,将单据信息填入stock中
         * */
        List<TaskItem> list = listMap.get(Short.parseShort(OrderWorkType.ORDER_WORK_TYPE_STOCK_OUT.type));
        if (Objects.isNull(list) || list.isEmpty()) {
            Map<Long, List<TaskItem>> maps = taskItems.stream().collect(Collectors.groupingBy(TaskItem::getSource));
            maps.keySet().forEach(key -> {
                WaveItem waveItem = waveItemService.getById(key);
                if (Objects.isNull(waveItem)) {
                    throw new CoolException("波次明细不存在!!");
                }
                try {
                    saveOutStockItem(maps.get(key), waveItem);
                } catch (Exception e) {
                    throw new CoolException(e.getMessage());
                }
            });
        } else {
            try {
                saveStockItems(items, pakinItem);
                saveOutStockItem(taskItems, null);
            } catch (Exception e) {
                throw new CoolException("库存管理保存更新失败!!");
                throw new CoolException(e.getMessage());
            }
        });
        /**修改库位状态为F.在库*/
        if (!locService.update(new LambdaUpdateWrapper<Loc>().set(Loc::getUseStatus, LocStsType.LOC_STS_TYPE_F.type).in(Loc::getCode, locCodes))) {
        }
        /**修改为库位状态为O.空库*/
        if (!locService.update(new LambdaUpdateWrapper<Loc>().set(Loc::getUseStatus, LocStsType.LOC_STS_TYPE_O.type).eq(Loc::getCode, loc.getId()))) {
            throw new CoolException("库位状态修改失败!!");
        }
        if (!this.update(new LambdaUpdateWrapper<Task>().in(Task::getId, list).set(Task::getTaskStatus, TaskStsType.UPDATED_IN.id))) {
        if (!this.update(new LambdaUpdateWrapper<Task>().eq(Task::getId, task.getId()).set(Task::getTaskStatus, TaskStsType.UPDATED_OUT.id))) {
            throw new CoolException("任务状态修改失败!!");
        }
    }
    /**
     * @author Ryan
     * @description 更新库位明细
     * @date 2025/5/20
     * @description: 出库信息保存至库存明细表
     * @version 1.0
     */
    @Transactional(rollbackFor = Exception.class)
    public void saveOutStockItem(List<TaskItem> taskItems, WaveItem waveItem) throws Exception {
        Stock stock = new Stock();
        String ruleCode = SerialRuleUtils.generateRuleCode(SerialRuleCode.SYS_STOCK_CODE, null);
        if (StringUtils.isBlank(ruleCode)) {
            throw new CoolException("当前业务:" + SerialRuleCode.SYS_STOCK_CODE + ",编码规则不存在!!");
        }
        Double sum = taskItems.stream().mapToDouble(TaskItem::getAnfme).sum();
        stock.setCode(ruleCode)
                .setAnfme(sum);
        if (Objects.isNull(waveItem)) {
            stock.setWkType(Short.parseShort(OrderWorkType.ORDER_WORK_TYPE_STOCK_OUT.type))
                    .setMemo("出库单出库,无单据信息!!")
                    .setType(OrderType.ORDER_OUT.type);
        } else {
            //TODO 生成波次时需要将波次号写入单据,通过物料,批次,动态字段等唯一值反查单据信息
            stock.setSourceId(waveItem.getId()).setType(OrderType.ORDER_OUT.type);
        }
        if (!stockService.save(stock)) {
            throw new CoolException("库存保存失败!!");
        }
        List<StockItem> stockItems = new ArrayList<>();
        for (TaskItem item : taskItems) {
            /**通过任务明细中的taskId查询,获取TASK的目标库位信息*/
            StockItem stockItem = new StockItem();
            BeanUtils.copyProperties(item, stockItem);
            stockItem.setSourceItemId(item.getOrderItemId())
                    .setStockCode(stock.getCode())
                    .setId(null)
                    .setStockId(stock.getId());
            stockItems.add(stockItem);
        }
        if (!stockItemService.saveBatch(stockItems)) {
            throw new CoolException("库存修改架失败!!");
        }
    }
    /**
     * @author Ryan
     * @date 2025/5/20
     * @description: 扣减库存明细
     * @version 1.0
     */
    @Transactional(rollbackFor = Exception.class)
    public void subtractLocItem(Loc loc) throws Exception {
        if (!locItemService.remove(new LambdaQueryWrapper<LocItem>().eq(LocItem::getLocId, loc.getId()))) {
            throw new CoolException("库存明细删除失败!!");
        }
    }
    /**
     * @author Ryan
     * @date 2025/5/20
     * @description: 完成入库任务
     * @version 1.0
     */
    @Transactional(rollbackFor = Exception.class)
    public void complateInstock(Task task) {
        if (Objects.isNull(task)) {
            return;
        }
        Loc loc = locService.getOne(new LambdaQueryWrapper<Loc>().eq(Loc::getCode, task.getTargLoc()));
        if (Objects.isNull(loc)) {
            throw new CoolException("目标库位不存在!");
        }
        if (!loc.getUseStatus().equals(LocStsType.LOC_STS_TYPE_S.type)) {
            throw new CoolException("当前库位状态不处于S.入库预约,不可执行入库操作!");
        }
        List<TaskItem> taskItems = taskItemService.list(new LambdaQueryWrapper<TaskItem>().eq(TaskItem::getTaskId, task.getId()));
        if (taskItems.isEmpty()) {
            throw new CoolException("任务明细不存在!!");
        }
        try {
            //更新库位明细
            saveLocItem(taskItems, task.getId());
        } catch (Exception e) {
            throw new CoolException("库位明细更新失败!!");
        }
        /**对任务明细按组拖明细进行分组*/
        Map<Long, List<TaskItem>> orderMap = taskItems.stream().collect(Collectors.groupingBy(TaskItem::getSource));
        orderMap.keySet().forEach(key -> {
            WaitPakinItem pakinItem = waitPakinItemService.getById(key);
            if (Objects.isNull(pakinItem)) {
                throw new CoolException("数据错误:组拖数据不存在,请联系管理员!!");
            }
            List<TaskItem> items = orderMap.get(key);
            try {
                //保存库存明细
                saveStockItems(items, pakinItem);
                //移出收货区库存, 修改组托状态
                removeReceiptStock(pakinItem);
            } catch (Exception e) {
                throw new CoolException(e.getMessage());
            }
        });
        /**修改库位状态为F.在库*/
        if (!locService.update(new LambdaUpdateWrapper<Loc>().set(Loc::getUseStatus, LocStsType.LOC_STS_TYPE_F.type).eq(Loc::getCode, task.getTargLoc()))) {
            throw new CoolException("库位状态修改失败!!");
        }
        if (!this.update(new LambdaUpdateWrapper<Task>().eq(Task::getId, task.getId()).set(Task::getTaskStatus, TaskStsType.UPDATED_IN.id))) {
            throw new CoolException("任务状态修改失败!!");
        }
    }
    /**
     * @param
     * @return
     * @author Ryan
     * @description 移除收货区库存
     * @time 2025/4/30 16:32
     */
    @Transactional(rollbackFor = Exception.class)
    public void removeReceiptStock(WaitPakinItem pakinItem) {
        WarehouseAreasItem itemServiceOne = warehouseAreasItemService.getOne(new LambdaQueryWrapper<WarehouseAreasItem>()
                .eq(WarehouseAreasItem::getId, pakinItem.getSource()));
        if (Objects.isNull(itemServiceOne)) {
            throw new CoolException("数据错误:请查看请货区库存是否存在!!");
        }
        Double workQty = itemServiceOne.getWorkQty() - pakinItem.getAnfme();
        Double qty = itemServiceOne.getQty() + pakinItem.getAnfme();
        itemServiceOne.setWorkQty(workQty).setQty(qty);
        if (!waitPakinService.update(new LambdaUpdateWrapper<WaitPakin>()
                .set(WaitPakin::getIoStatus, PakinIOStatus.PAKIN_IO_STATUS_TASK_DONE.val)
                .eq(WaitPakin::getId, pakinItem.getPakinId()))) {
            throw new CoolException("组拖状态修改失败!!");
        }
        if (qty.compareTo(itemServiceOne.getAnfme()) == 0.00) {
            if (!warehouseAreasItemService.removeById(itemServiceOne.getId())) {
                throw new CoolException("收货区物料删除失败!!");
            }
        } else {
            if (!warehouseAreasItemService.updateById(itemServiceOne)) {
                throw new CoolException("收货区库完成数量修改失败!!");
            }
        }
    }
    /**
     * @param
     * @return
     * @author Ryan
     * @description 更新库位明细
     * @time 2025/4/15 15:28
     */
    @Transactional(rollbackFor = Exception.class)
@@ -254,7 +832,10 @@
            Loc loc = locService.getOne(new LambdaQueryWrapper<Loc>().eq(Loc::getCode, task.getTargLoc()), false);
            LocItem item = new LocItem();
            BeanUtils.copyProperties(taskItem, item);
            item.setLocCode(loc.getCode()).setId(null).setLocId(loc.getId()).setType(taskItem.getOrderType());
            item.setLocCode(loc.getCode()).setId(null)
                    .setLocId(loc.getId())
                    .setWkType(taskItem.getWkType())
                    .setType(taskItem.getOrderType());
            locItems.add(item);
        });
        if (!locItemService.saveBatch(locItems)) {
@@ -263,19 +844,14 @@
    }
    /**
     * 生成库存明细
     * 生成入库库存明细
     *
     * @param items
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public void saveStockItems(List<TaskItem> items, WaitPakinItem order) throws Exception {
        Stock stock = new Stock();
//        if (!Objects.isNull(order.getPoCode()) && StringUtils.isNotBlank(order.getPoCode())) {
//            Purchase purchase = purchaseService.getOne(new LambdaQueryWrapper<Purchase>().eq(Purchase::getCode, order.getPoCode()));
//            if (!Objects.isNull(purchase)) {
//                stock.setPlatOrderNo(purchase.getPlatCode()).setPlatToken(purchase.getPlatId());
//            }
//        }
        String ruleCode = SerialRuleUtils.generateRuleCode(SerialRuleCode.SYS_STOCK_CODE, null);
        if (StringUtils.isBlank(ruleCode)) {
            throw new CoolException("当前业务:" + SerialRuleCode.SYS_STOCK_CODE + ",编码规则不存在!!");
@@ -294,10 +870,6 @@
        List<StockItem> stockItems = new ArrayList<>();
        for (TaskItem item : items) {
            /**通过任务明细中的taskId查询,获取TASK的目标库位信息*/
//            AsnOrderItem orderItem = asnOrderItemService.getOne(new LambdaQueryWrapper<AsnOrderItem>().eq(AsnOrderItem::getId, item.getOrderItemId()));
//            if (Objects.isNull(orderItem)) {
//                throw new CoolException("单据明细不存在!!");
//            }
            StockItem stockItem = new StockItem();
            BeanUtils.copyProperties(item, stockItem);
            stockItem.setSourceItemId(item.getOrderItemId())