skyouc
2 天以前 093c43638d3f9aa64fc3b396a0a2452cc28abbe3
rsf-server/src/main/java/com/vincent/rsf/server/manager/service/impl/TaskServiceImpl.java
@@ -3,19 +3,22 @@
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.controller.params.TaskInParam;
import com.vincent.rsf.server.api.entity.dto.InTaskMsgDto;
import com.vincent.rsf.server.api.service.WcsService;
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.utils.LocUtils;
import com.vincent.rsf.server.manager.controller.params.GenerateTaskParams;
import com.vincent.rsf.server.manager.entity.*;
import com.vincent.rsf.server.manager.mapper.TaskItemMapper;
import com.vincent.rsf.server.manager.enums.LocType;
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.LocStsType;
import com.vincent.rsf.server.system.utils.SerialRuleUtils;
import lombok.Synchronized;
import org.apache.commons.lang3.StringUtils;
@@ -68,6 +71,8 @@
    private TaskService taskService;
    @Autowired
    private LocItemWorkingService locItemWorkingService;
    @Autowired
    private WcsService wcsService;
    @Override
    @Transactional(rollbackFor = Exception.class)
@@ -97,8 +102,8 @@
            }
            Task task = new Task();
            task.setTaskCode(ruleCode)
                    .setTaskStatus(TaskStsType.GENERATE_IN.id.shortValue())
                    .setTaskType(TaskType.TASK_TYPE_IN.type.shortValue())
                    .setTaskStatus(TaskStsType.GENERATE_IN.id)
                    .setTaskType(TaskType.TASK_TYPE_IN.type)
                    .setWarehType(WarehType.WAREHOUSE_TYPE_AGV.id)
                    .setTargLoc(targetLoc)
                    .setOrgSite(orgSta)
@@ -183,8 +188,8 @@
            }
            Task task = new Task();
            task.setTaskCode(ruleCode)
                    .setTaskStatus(TaskStsType.COMPLETE_IN.id.shortValue())
                    .setTaskType(TaskType.TASK_TYPE_IN.type.shortValue())
                    .setTaskStatus(TaskStsType.COMPLETE_IN.id)
                    .setTaskType(TaskType.TASK_TYPE_IN.type)
                    .setWarehType(WarehType.WAREHOUSE_TYPE_PLAT.id)
                    .setTargLoc(targetLoc)
                    .setBarcode(pakin.getBarcode())
@@ -290,8 +295,8 @@
            }
            Task task = new Task();
            task.setTaskCode(ruleCode)
                    .setTaskStatus(TaskStsType.GENERATE_IN.id.shortValue())
                    .setTaskType(TaskType.TASK_TYPE_IN.type.shortValue())
                    .setTaskStatus(TaskStsType.GENERATE_IN.id)
                    .setTaskType(TaskType.TASK_TYPE_IN.type)
                    .setTargLoc(targetLoc)
                    .setBarcode(pakin.getBarcode())
                    .setOrgSite(deviceSite.getSite())
@@ -372,6 +377,31 @@
    }
    /**
     * 入库任务
     *
     * @param tasks
     * @throws Exception
     */
    @Synchronized
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void complateInTask(List<Task> tasks) throws Exception {
        for (Task task : tasks) {
            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_CHECK_IN.type)) {
                //57.盘点再入库
                pickComplateInStock(task);
            }
        }
    }
    /**
     * 完成任务 更新库位明细信息,将单据库存更新到单据库存表
     *
     * @param tasks
@@ -383,26 +413,15 @@
    public void completeTask(List<Task> tasks) throws Exception {
        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);
                }
            if (task.getTaskType().equals(TaskType.TASK_TYPE_OUT.type)) {
                //全托出库
                complateOutStock(task);
            } else if (task.getTaskType().equals(TaskType.TASK_TYPE_PICK_AGAIN_OUT.type)) {
                //拣料出库
                complateOutStock(task);
            } else if (task.getTaskType().equals(TaskType.TASK_TYPE_CHECK_OUT.type)) {
                //盘点出库
                complateOutStock(task);
            }
        }
    }
@@ -440,31 +459,46 @@
            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()));
                    .eq(StringUtils.isNotBlank(taskItem.getBatch()), LocItemWorking::getBatch, taskItem.getBatch())
                    .eq(StringUtils.isNotBlank(taskItem.getFieldsIndex()), LocItemWorking::getFieldsIndex, taskItem.getFieldsIndex())
                    .eq(LocItemWorking::getMatnrId, taskItem.getMatnrId()));
            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);
            if (task.getTaskType().equals(TaskType.TASK_TYPE_CHECK_IN.type)) {
                locWorking.setWorkQty(0.0);
                locWorking.setAnfme(taskItem.getAnfme());
            } else {
                continue;
                if (locWorking.getAnfme().compareTo(taskItem.getAnfme()) >= 0) {
                    locWorking.setWorkQty(0.0);
                    locWorking.setAnfme(Math.round((locWorking.getAnfme() - taskItem.getAnfme()) * 10000) / 10000.0);
                } else {
                    continue;
                }
            }
            BeanUtils.copyProperties(locWorking, locItem);
            locItem.setLocCode(loc.getCode()).setLocId(loc.getId()).setId(null);
            items.add(locItem);
        }
       if (!locItemService.saveBatch(items)) {
           throw new CoolException("作业库存回写失败!!");
       }
        if (!locItemService.saveBatch(items)) {
            throw new CoolException("作业库存回写失败!!");
        }
       if (!locItemWorkingService.remove(new LambdaQueryWrapper<LocItemWorking>().eq(LocItemWorking::getTaskId, task.getId()))) {
           throw new CoolException("作业中库存删除失败!!");
       }
        TaskItem taskItem = taskItems.stream().findFirst().get();
        //保存入出库流水
        saveStockItems(taskItems, null, null, taskItem.getWkType(), taskItem.getOrderType());
        if (!locItemWorkingService.remove(new LambdaQueryWrapper<LocItemWorking>().eq(LocItemWorking::getTaskId, task.getId()))) {
            throw new CoolException("作业中库存删除失败!!");
        }
        task.setTaskStatus(TaskStsType.UPDATED_IN.id);
        if (!taskService.updateById(task)) {
            throw new CoolException("任务状态修改失败!!");
        }
    }
@@ -476,7 +510,7 @@
     */
    @Override
    public R removeTask(Long[] ids) {
        List<Short> longs = Arrays.asList(TaskStsType.GENERATE_IN.id, TaskStsType.GENERATE_OUT.id);
        List<Integer> 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("任务已处执行状态不可取消!!");
@@ -507,23 +541,23 @@
                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)
                            .eq(BasStation::getUseStatus, LocStsType.LOC_STS_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_PICK_AGAIN_OUT.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)
                            .eq(BasStation::getUseStatus, LocStsType.LOC_STS_TYPE_R.type)
                    );
                }
                if (null == basStation) {
                    throw new CoolException("站点状态错误!!");
                }
                basStation.setUseStatus(StaUseStatusType.TYPE_F.type);
                basStation.setUseStatus(LocStsType.LOC_STS_TYPE_F.type);
                if (!basStationService.updateById(basStation)) {
                    throw new CoolException("更新站点状态失败!!");
                }
@@ -544,13 +578,26 @@
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public R pickTask(Long id) throws Exception {
    public Task pickOrCheckTask(Long id, String oType) 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("非拣料出库 ,不可执行此操作!!");
        Integer type;
        if (oType.equals("check")) {
            //盘点入库
            type = TaskType.TASK_TYPE_CHECK_IN.type;
            if (!task.getTaskType().equals(TaskType.TASK_TYPE_CHECK_OUT.type)) {
                throw new CoolException("非盘点出库 ,不可执行此操作!!");
            }
        } else {
            //拣料入库
            type = TaskType.TASK_TYPE_PICK_IN.type;
            if (!task.getTaskType().equals(TaskType.TASK_TYPE_PICK_AGAIN_OUT.type)) {
                throw new CoolException("非拣料出库 ,不可执行此操作!!");
            }
        }
        Loc loc = locService.getOne(new LambdaQueryWrapper<Loc>()
@@ -559,35 +606,39 @@
            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)
                .setTaskType(type)
                .setBarcode(task.getBarcode())
                .setTaskStatus(TaskStsType.GENERATE_IN.id);
        //TODO 根据物料的库位类型生成新的库位
        TaskInParam param = new TaskInParam();
        param.setSourceStaNo(Integer.parseInt(task.getTargSite()))
                .setIoType(type)
                .setLocType1(Integer.parseInt(loc.getType()));
        InTaskMsgDto locInfo = wcsService.getLocNo(param);
        if (Objects.isNull(locInfo)) {
            throw new CoolException("获取库位失败!!");
        }
        task.setTargLoc(locInfo.getLocNo());
        if (!this.updateById(task)) {
            throw new CoolException("任务状态更新失败!!");
        }
        //TODO 后续需根据仓库类型查找新库位,原始库位置空闲状态
        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);
        Loc one = locService.getOne(new LambdaQueryWrapper<Loc>().eq(Loc::getCode, task.getTargLoc()));
        if (Objects.isNull(one)) {
            throw new CoolException("目标库位不存在!!");
        }
        one.setUseStatus(LocStsType.LOC_STS_TYPE_S.type);
        if (!locItemWorkingService.saveBatch(workings)) {
            throw new CoolException("临时库存保存失败!!");
        if (!locService.updateById(one)) {
            throw new CoolException("库位预约入库失败!!");
        }
        return R.ok("拣货成功!!");
        return task;
    }
    /**
@@ -614,10 +665,29 @@
        if (taskItems.isEmpty()) {
            throw new CoolException("任务明细不存在!!");
        }
        List<LocItem> locItems = locItemService.list(new LambdaQueryWrapper<LocItem>().eq(LocItem::getLocId, loc.getId()));
        if (locItems.isEmpty()) {
            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()).setLocItemId(item.getId());
            workings.add(working);
        }
        if (!locItemWorkingService.saveBatch(workings)) {
            throw new CoolException("临时库存保存失败!!");
        }
        try {
            //更新库位明细
            subtractLocItem(loc);
        } catch (Exception e) {
            log.error("<UNK>", e);
            throw new CoolException(e.getMessage());
        }
@@ -650,12 +720,27 @@
        }
        /**修改为库位状态为O.空库*/
        if (!locService.update(new LambdaUpdateWrapper<Loc>().set(Loc::getUseStatus, LocStsType.LOC_STS_TYPE_O.type).eq(Loc::getCode, loc.getId()))) {
        if (!locService.update(new LambdaUpdateWrapper<Loc>()
                .set(Loc::getUseStatus, LocStsType.LOC_STS_TYPE_O.type)
                .eq(Loc::getId, loc.getId()))) {
            throw new CoolException("库位状态修改失败!!");
        }
        if (!this.update(new LambdaUpdateWrapper<Task>().eq(Task::getId, task.getId()).set(Task::getTaskStatus, TaskStsType.UPDATED_OUT.id))) {
            throw new CoolException("任务状态修改失败!!");
        if (task.getTaskType().equals(TaskType.TASK_TYPE_PICK_AGAIN_OUT.type) || task.getTaskType().equals(TaskType.TASK_TYPE_CHECK_OUT.type)) {
            if (!this.update(new LambdaUpdateWrapper<Task>()
                    .eq(Task::getId, task.getId())
                    .set(Task::getTaskStatus, TaskStsType.WAVE_SEED.id))) {
                throw new CoolException("库存状态更新失败!!");
            }
        } else {
            if (!this.update(new LambdaUpdateWrapper<Task>()
                    .eq(Task::getId, task.getId())
                    .set(Task::getTaskStatus, TaskStsType.UPDATED_OUT.id))) {
                throw new CoolException("库存状态更新失败!!");
            }
        }
    }
    /**
@@ -757,7 +842,7 @@
            List<TaskItem> items = orderMap.get(key);
            try {
                //保存库存明细
                saveStockItems(items, pakinItem);
                saveStockItems(items, pakinItem.getId(), pakinItem.getAsnCode(), pakinItem.getWkType(), pakinItem.getType());
                //移出收货区库存, 修改组托状态
                removeReceiptStock(pakinItem);
            } catch (Exception e) {
@@ -844,19 +929,20 @@
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public void saveStockItems(List<TaskItem> items, WaitPakinItem order) throws Exception {
    public void saveStockItems(List<TaskItem> items, Long id, String code, Short wkType, String type) 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 = items.stream().mapToDouble(TaskItem::getAnfme).sum();
        stock.setSourceId(order.getId())
        stock.setSourceId(id)
                .setCode(ruleCode)
                .setSourceCode(order.getAsnCode())
                .setSourceCode(code)
                .setAnfme(sum)
                .setWkType(order.getWkType())
                .setType(order.getType());
                .setWkType(wkType)
                .setType(type);
        if (!stockService.save(stock)) {
            throw new CoolException("库存保存失败!!");
        }