cl
4 天以前 225f9914090016cbe0836a06fbb852da05333504
rsf-server/src/main/java/com/vincent/rsf/server/manager/service/impl/LocItemServiceImpl.java
@@ -8,6 +8,7 @@
import com.vincent.rsf.server.api.service.WcsService;
import com.vincent.rsf.server.api.utils.LocUtils;
import com.vincent.rsf.server.common.constant.Constants;
import com.vincent.rsf.server.common.utils.QuantityUtils;
import com.vincent.rsf.server.manager.controller.params.CheckLocQueryParams;
import com.vincent.rsf.server.manager.controller.params.LocToTaskParams;
import com.vincent.rsf.server.manager.entity.*;
@@ -15,7 +16,9 @@
import com.vincent.rsf.server.manager.mapper.LocItemMapper;
import com.vincent.rsf.server.manager.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.vincent.rsf.server.system.constant.GlobalConfigCode;
import com.vincent.rsf.server.system.constant.SerialRuleCode;
import com.vincent.rsf.server.system.service.ConfigService;
import com.vincent.rsf.server.system.utils.SerialRuleUtils;
import lombok.Synchronized;
import org.apache.commons.lang3.StringUtils;
@@ -35,6 +38,14 @@
public class LocItemServiceImpl extends ServiceImpl<LocItemMapper, LocItem> implements LocItemService {
    private static final BigDecimal FULL_OUT_QTY_TOLERANCE = new BigDecimal("0.000001");
    /** 可与 {@link TaskStsType#GENERATE_OUT}~{@link TaskStsType#WAVE_SEED} 衔接追加明细的出库任务类型 */
    private static final List<Integer> OUTBOUND_TASK_TYPES_FOR_APPEND = Arrays.asList(
            TaskType.TASK_TYPE_OUT.type,
            TaskType.TASK_TYPE_PICK_AGAIN_OUT.type,
            TaskType.TASK_TYPE_MERGE_OUT.type,
            TaskType.TASK_TYPE_CHECK_OUT.type
    );
    Logger logger = LoggerFactory.getLogger(LocItemServiceImpl.class);
@@ -58,7 +69,10 @@
    private BasStationService basStationService;
    @Autowired
    private MatnrService matnrService;
    @Autowired
    private AsnOrderItemService asnOrderItemService;
    @Autowired
    private ConfigService configService;
    /** 入库/出库保存前:若规格或型号为空则从物料带出 */
    private void fillSpecModelFromMatnr(LocItem item) {
        if (item == null || item.getMatnrId() == null) {
@@ -169,45 +183,73 @@
            order = new WkOrder();
        }
        final boolean appendSameLocEnabled = isOutAppendSameLocTaskEnabled();
        listMap.keySet().forEach(key -> {
            Task task = new Task();
            Loc loc = locService.getById(key);
            logger.info("库位:>{}", loc.getCode());
            if (Objects.isNull(loc)) {
                throw new CoolException("数据错误:所选库存信息不存在!!");
            }
            // 支持 F.在库 或 R.出库预约/拣货中 状态下分配(拣货中可追加同库位订单,分配量不超过已分配剩余)
            if (!loc.getUseStatus().equals(LocStsType.LOC_STS_TYPE_F.type)
                    && !loc.getUseStatus().equals(LocStsType.LOC_STS_TYPE_R.type)) {
                throw new CoolException("库位:" + loc.getCode() + ",不处于F.在库或R.出库预约状态,不可执行出库分配!!");
            if (appendSameLocEnabled) {
                if (LocStsType.LOC_STS_TYPE_S.type.equals(loc.getUseStatus())) {
                    throw new CoolException("库位:" + loc.getCode() + " 处于S.入库预约,须待拣料/盘点入库回库至F.在库后再下发出库任务!!");
                }
                if (!LocStsType.LOC_STS_TYPE_F.type.equals(loc.getUseStatus())
                        && !LocStsType.LOC_STS_TYPE_R.type.equals(loc.getUseStatus())) {
                    throw new CoolException("库位:" + loc.getCode() + ",不处于F.在库或R.出库预约状态,不可执行出库分配!!");
                }
            } else {
                if (!LocStsType.LOC_STS_TYPE_F.type.equals(loc.getUseStatus())) {
                    throw new CoolException("库位:" + loc.getCode() + " 不处于F.在库状态,不可执行出库分配!!");
                }
            }
            if (loc.getUseStatus().equals(LocStsType.LOC_STS_TYPE_F.type)) {
            final boolean startedAsReserved = LocStsType.LOC_STS_TYPE_R.type.equals(loc.getUseStatus());
            if (LocStsType.LOC_STS_TYPE_F.type.equals(loc.getUseStatus())) {
                loc.setUseStatus(LocStsType.LOC_STS_TYPE_R.type);
                if (!locService.updateById(loc)) {
                    throw new CoolException("库位状态更新失败!!");
                }
            }
            // 库位已为 R 时:计算该库位当前任务已分配量,新分配不能超过 (库位数量 - 已分配)
            Map<String, Double> allocatedByKey = new HashMap<>();
            List<Task> existTasks = new ArrayList<>();
            if (loc.getUseStatus().equals(LocStsType.LOC_STS_TYPE_R.type)) {
                existTasks = taskService.list(new LambdaQueryWrapper<Task>()
                        .eq(Task::getOrgLoc, loc.getCode())
                        .in(Task::getTaskStatus, Arrays.asList(TaskStsType.GENERATE_OUT.id, TaskStsType.WAVE_SEED.id)));
                for (Task t : existTasks) {
                    List<TaskItem> existItems = taskItemService.list(new LambdaQueryWrapper<TaskItem>().eq(TaskItem::getTaskId, t.getId()));
                    for (TaskItem ti : existItems) {
                        String k = buildAllocKey(ti.getMatnrId(), ti.getBatch(), ti.getFieldsIndex());
                        allocatedByKey.put(k, allocatedByKey.getOrDefault(k, 0.0) + (ti.getAnfme() != null ? ti.getAnfme() : 0.0));
                    }
            List<Task> openOutboundOnLoc = taskService.list(new LambdaQueryWrapper<Task>()
                    .eq(Task::getOrgLoc, loc.getCode())
                    .in(Task::getTaskType, OUTBOUND_TASK_TYPES_FOR_APPEND)
                    .ge(Task::getTaskStatus, TaskStsType.GENERATE_OUT.id)
                    .le(Task::getTaskStatus, TaskStsType.WAVE_SEED.id));
            boolean reuseExistingTask = false;
            Task task = null;
            if (appendSameLocEnabled && startedAsReserved) {
                if (openOutboundOnLoc.size() > 1) {
                    throw new CoolException("库位:" + loc.getCode() + " 出库预约下存在多笔未完结出库任务,不允许再生成任务,请待拣料回库至F后再分配!!");
                }
                if (openOutboundOnLoc.isEmpty()) {
                    throw new CoolException("库位:" + loc.getCode() + " 为R.出库预约但未找到未完结出库任务,库位数据异常,不可下发出库或波次!!");
                }
                task = openOutboundOnLoc.get(0);
                reuseExistingTask = true;
                if (StringUtils.isNotBlank(task.getTargSite())
                        && !StringUtils.equals(StringUtils.trimToEmpty(siteNo), StringUtils.trimToEmpty(task.getTargSite()))) {
                    throw new CoolException("库位:" + loc.getCode() + " 已有出库任务目标站点为" + task.getTargSite() + ",与本次站点不一致,不可追加明细!!");
                }
            }
            // 料箱码:优先用库位;预约出库(R) 时若库位未绑定则从同库位已有任务带出并回写库位;再否则从本次分配明细带出
            Map<String, Double> allocatedByKey = new HashMap<>();
            for (Task t : openOutboundOnLoc) {
                List<TaskItem> existItems = taskItemService.list(new LambdaQueryWrapper<TaskItem>().eq(TaskItem::getTaskId, t.getId()));
                for (TaskItem ti : existItems) {
                    String k = buildAllocKey(ti.getMatnrId(), ti.getBatch(), ti.getFieldsIndex());
                    allocatedByKey.put(k, allocatedByKey.getOrDefault(k, 0.0) + (ti.getAnfme() != null ? ti.getAnfme() : 0.0));
                }
            }
            String barcodeToUse = StringUtils.isNotBlank(loc.getBarcode()) ? loc.getBarcode() : null;
            if (barcodeToUse == null && !existTasks.isEmpty()) {
                barcodeToUse = existTasks.get(0).getBarcode();
            if (barcodeToUse == null && !openOutboundOnLoc.isEmpty()) {
                barcodeToUse = openOutboundOnLoc.get(0).getBarcode();
                if (StringUtils.isNotBlank(barcodeToUse)) {
                    Task refTask = existTasks.get(0);
                    Task refTask = openOutboundOnLoc.get(0);
                    LambdaUpdateWrapper<Loc> locUw = new LambdaUpdateWrapper<Loc>().eq(Loc::getId, loc.getId())
                            .set(Loc::getBarcode, barcodeToUse)
                            .set(Loc::getUpdateBy, loginUserId)
@@ -231,71 +273,87 @@
            if (barcodeToUse == null) {
                barcodeToUse = loc.getBarcode();
            }
            Task moveTask = new Task();
            String ruleCode = SerialRuleUtils.generateRuleCode(SerialRuleCode.SYS_TASK_CODE, null);
            task.setOrgLoc(loc.getCode())
                    .setTaskCode(ruleCode)
                    .setResource(resouce)
                    .setTargSite(siteNo)
                    .setSort(Constants.TASK_SORT_DEFAULT_VALUE)
                    .setUpdateBy(loginUserId)
                    .setCreateBy(loginUserId)
                    .setCreateTime(new Date())
                    .setUpdateTime(new Date())
                    .setTaskStatus(TaskStsType.GENERATE_OUT.id)
                    .setBarcode(barcodeToUse)
                    .setMemo(map.getMemo());
            if (!reuseExistingTask) {
                task = new Task();
                String ruleCode = SerialRuleUtils.generateRuleCode(SerialRuleCode.SYS_TASK_CODE, null);
                task.setOrgLoc(loc.getCode())
                        .setTaskCode(ruleCode)
                        .setResource(resouce)
                        .setTargSite(siteNo)
                        .setSort(Constants.TASK_SORT_DEFAULT_VALUE)
                        .setUpdateBy(loginUserId)
                        .setCreateBy(loginUserId)
                        .setCreateTime(new Date())
                        .setUpdateTime(new Date())
                        .setTaskStatus(TaskStsType.GENERATE_OUT.id)
                        .setBarcode(barcodeToUse)
                        .setMemo(map.getMemo());
            List<LocItem> locItems = this.list(new LambdaQueryWrapper<LocItem>().eq(LocItem::getLocId, key));
            if (locItems.isEmpty()) {
                throw new CoolException("数据错误:所选库存明细不存在!!");
            }
                List<LocItem> locItems = this.list(new LambdaQueryWrapper<LocItem>().eq(LocItem::getLocId, key));
                if (locItems.isEmpty()) {
                    throw new CoolException("数据错误:所选库存明细不存在!!");
                }
            Double orgQty = locItems.stream().mapToDouble(LocItem::getAnfme).sum();
            List<LocItem> locItemList = listMap.get(key);
            Double outQty = locItemList.stream().mapToDouble(LocItem::getOutQty).sum();
            BigDecimal orgQtyBd = BigDecimal.valueOf(orgQty).setScale(6, RoundingMode.HALF_UP);
            BigDecimal outQtyBd = BigDecimal.valueOf(outQty).setScale(6, RoundingMode.HALF_UP);
                Double orgQty = locItems.stream().mapToDouble(LocItem::getAnfme).sum();
                List<LocItem> locItemList = listMap.get(key);
                Double outQty = locItemList.stream().mapToDouble(LocItem::getOutQty).sum();
                BigDecimal orgQtyBd = BigDecimal.valueOf(orgQty).setScale(6, RoundingMode.HALF_UP);
                BigDecimal outQtyBd = BigDecimal.valueOf(outQty).setScale(6, RoundingMode.HALF_UP);
            if (map.getType().equals(Constants.TASK_TYPE_OUT_STOCK)
                    || map.getType().equals(Constants.TASK_TYPE_ORDER_OUT_STOCK)
                    || map.getType().equals(Constants.TASK_TYPE_WAVE_OUT_STOCK)) {
                // 出库量达到库位库存(含容差)视为全板出库,避免浮点误差导致误判为拣料/部分出库
                if (orgQtyBd.subtract(outQtyBd).compareTo(FULL_OUT_QTY_TOLERANCE) > 0) {
                    // 拣料出库(部分出库)
                    DeviceSite deviceSite = deviceSiteService.getOne(new LambdaQueryWrapper<DeviceSite>()
                            .eq(DeviceSite::getSite, siteNo)
                            .eq(!Objects.isNull(loc.getChannel()),DeviceSite::getChannel, loc.getChannel())
                            .eq(DeviceSite::getType, TaskType.TASK_TYPE_PICK_AGAIN_OUT.type));
                    if (Objects.isNull(deviceSite)) {
                        throw new CoolException("站点不支持拣料出库!!");
                if (map.getType().equals(Constants.TASK_TYPE_OUT_STOCK)
                        || map.getType().equals(Constants.TASK_TYPE_ORDER_OUT_STOCK)
                        || map.getType().equals(Constants.TASK_TYPE_WAVE_OUT_STOCK)) {
                    if (orgQtyBd.subtract(outQtyBd).compareTo(FULL_OUT_QTY_TOLERANCE) > 0) {
                        DeviceSite deviceSite = deviceSiteService.getOne(new LambdaQueryWrapper<DeviceSite>()
                                .eq(DeviceSite::getSite, siteNo)
                                .eq(!Objects.isNull(loc.getChannel()), DeviceSite::getChannel, loc.getChannel())
                                .eq(DeviceSite::getType, TaskType.TASK_TYPE_PICK_AGAIN_OUT.type));
                        if (Objects.isNull(deviceSite)) {
                            throw new CoolException("站点不支持拣料出库!!");
                        }
                        task.setTaskType(TaskType.TASK_TYPE_PICK_AGAIN_OUT.type).setWarehType(deviceSite.getDevice());
                    } else {
                        DeviceSite deviceSite = deviceSiteService.getOne(new LambdaQueryWrapper<DeviceSite>()
                                .eq(!Objects.isNull(loc.getChannel()), DeviceSite::getChannel, loc.getChannel())
                                .eq(DeviceSite::getSite, siteNo).eq(DeviceSite::getType, TaskType.TASK_TYPE_OUT.type));
                        if (Objects.isNull(deviceSite)) {
                            throw new CoolException("站点不支持全板出库!!");
                        }
                        task.setTaskType(TaskType.TASK_TYPE_OUT.type).setWarehType(deviceSite.getDevice());
                    }
                    task.setTaskType(TaskType.TASK_TYPE_PICK_AGAIN_OUT.type).setWarehType(deviceSite.getDevice());
                } else {
                    //全板出库
                } else if (map.getType().equals(Constants.TASK_TYPE_OUT_CHECK)) {
                    DeviceSite deviceSite = deviceSiteService.getOne(new LambdaQueryWrapper<DeviceSite>()
                            .eq(!Objects.isNull(loc.getChannel()), DeviceSite::getChannel, loc.getChannel())
                            .eq(DeviceSite::getSite, siteNo).eq(DeviceSite::getType, TaskType.TASK_TYPE_OUT.type));
                            .eq(DeviceSite::getSite, siteNo)
                            .eq(DeviceSite::getType, TaskType.TASK_TYPE_CHECK_OUT.type));
                    if (Objects.isNull(deviceSite)) {
                        throw new CoolException("站点不支持全板出库!!");
                        throw new CoolException("当前站点不支持盘点出库!!");
                    }
                    task.setTaskType(TaskType.TASK_TYPE_OUT.type).setWarehType(deviceSite.getDevice());
                    task.setTaskType(TaskType.TASK_TYPE_CHECK_OUT.type).setWarehType(deviceSite.getDevice());
                }
            } else if (map.getType().equals(Constants.TASK_TYPE_OUT_CHECK)) {
                //盘点出库
                DeviceSite deviceSite = deviceSiteService.getOne(new LambdaQueryWrapper<DeviceSite>()
                        .eq(!Objects.isNull(loc.getChannel()), DeviceSite::getChannel, loc.getChannel())
                        .eq(DeviceSite::getSite, siteNo)
                        .eq(DeviceSite::getType, TaskType.TASK_TYPE_CHECK_OUT.type));
                if (Objects.isNull(deviceSite)) {
                    throw new CoolException("当前站点不支持盘点出库!!");
                }
                task.setTaskType(TaskType.TASK_TYPE_CHECK_OUT.type).setWarehType(deviceSite.getDevice());
            }
            if (!taskService.save(task)) {
                throw new CoolException("任务创建失败!!");
                if (!taskService.save(task)) {
                    throw new CoolException("任务创建失败!!");
                }
            } else {
                if (StringUtils.isNotBlank(task.getBarcode()) && StringUtils.isBlank(barcodeToUse)) {
                    barcodeToUse = task.getBarcode();
                    LambdaUpdateWrapper<Loc> locUw = new LambdaUpdateWrapper<Loc>().eq(Loc::getId, loc.getId())
                            .set(Loc::getBarcode, barcodeToUse)
                            .set(Loc::getUpdateBy, loginUserId)
                            .set(Loc::getUpdateTime, new Date());
                    if (task.getWeight() != null) {
                        locUw.set(Loc::getWeight, task.getWeight());
                    }
                    locService.update(locUw);
                }
                List<LocItem> locItemsVerify = this.list(new LambdaQueryWrapper<LocItem>().eq(LocItem::getLocId, key));
                if (locItemsVerify.isEmpty()) {
                    throw new CoolException("数据错误:所选库存明细不存在!!");
                }
            }
//            if (!LocUtils.isShallowLoc(loc.getCode())) {
@@ -331,6 +389,11 @@
                }
            }
            final Task outTask = task;
            if (outTask == null) {
                throw new CoolException("任务数据异常!!");
            }
            List<TaskItem> taskItems = new ArrayList<>();
            listMap.get(key).forEach(item -> {
                LocItem locItem = locItemService.getById(item.getId());
@@ -340,25 +403,22 @@
                if (item.getOutQty().compareTo(0.0) < 0) {
                    throw new CoolException("出库数里不能小于0!!");
                }
                // 预约出库/拣货中追加:新分配量不能超过 (库位数量 - 该物料已分配量)
                Double allocQty = item.getOutQty();
                if (!allocatedByKey.isEmpty()) {
                    String allocKey = buildAllocKey(locItem.getMatnrId(), locItem.getBatch(), locItem.getFieldsIndex());
                    Double already = allocatedByKey.getOrDefault(allocKey, 0.0);
                    Double available = Math.round((locItem.getAnfme() - already) * 1000000) / 1000000.0;
                    if (available.compareTo(0.0) <= 0) {
                        throw new CoolException("库位:" + loc.getCode() + " 该物料已无剩余可分配数量,不可再追加订单!");
                    }
                    if (allocQty.compareTo(available) > 0) {
                        allocQty = available;
                        item.setOutQty(allocQty);
                    }
                    allocatedByKey.put(allocKey, already + allocQty);
                String allocKey = buildAllocKey(locItem.getMatnrId(), locItem.getBatch(), locItem.getFieldsIndex());
                Double already = allocatedByKey.getOrDefault(allocKey, 0.0);
                Double available = Math.round((locItem.getAnfme() - already) * 1000000) / 1000000.0;
                if (available.compareTo(0.0) <= 0) {
                    throw new CoolException("库位:" + loc.getCode() + " 该物料已无剩余可分配数量,不可再追加订单!");
                }
                if (allocQty.compareTo(available) > 0) {
                    allocQty = available;
                    item.setOutQty(allocQty);
                }
                allocatedByKey.put(allocKey, already + allocQty);
                TaskItem taskItem = new TaskItem();
                BeanUtils.copyProperties(item, taskItem);
                taskItem.setTaskId(task.getId())
                taskItem.setTaskId(outTask.getId())
                        .setAnfme(allocQty)
                        .setBatch(item.getBatch())
                        .setUpdateBy(loginUserId)
@@ -370,7 +430,29 @@
                    taskItem.setWkType(order.getWkType())
                            .setSourceCode(order.getCode())
                            .setSourceId(order.getId())
                            .setOrderId(order.getId())
                            .setOrderItemId(item.getOrderItemId());
                    String poOrCode = StringUtils.isNotBlank(order.getPoCode()) ? order.getPoCode() : order.getCode();
                    if (StringUtils.isBlank(taskItem.getPlatOrderCode()) && StringUtils.isNotBlank(poOrCode)) {
                        taskItem.setPlatOrderCode(poOrCode);
                    }
                    if (item.getOrderItemId() != null) {
                        WkOrderItem oi = asnOrderItemService.getById(item.getOrderItemId());
                        if (oi != null) {
                            if (StringUtils.isBlank(taskItem.getPlatItemId()) && StringUtils.isNotBlank(oi.getPlatItemId())) {
                                taskItem.setPlatItemId(oi.getPlatItemId());
                            }
                            if (StringUtils.isBlank(taskItem.getPlatWorkCode()) && StringUtils.isNotBlank(oi.getPlatWorkCode())) {
                                taskItem.setPlatWorkCode(oi.getPlatWorkCode());
                            }
                            if (StringUtils.isBlank(taskItem.getPlatOrderCode())) {
                                String p = StringUtils.isNotBlank(oi.getPlatOrderCode()) ? oi.getPlatOrderCode() : poOrCode;
                                if (StringUtils.isNotBlank(p)) {
                                    taskItem.setPlatOrderCode(p);
                                }
                            }
                        }
                    }
                } else if (map.getType().equals(Constants.TASK_TYPE_WAVE_OUT_STOCK)) {
                    taskItem.setSourceId(wave.getId())
                            .setWkType(OrderWorkType.ORDER_WORK_TYPE_OTHER.type)
@@ -400,6 +482,12 @@
            if (!taskItemService.saveBatch(taskItems)) {
                throw new CoolException("任务明细生成失败!!");
            }
            for (TaskItem ti : taskItems) {
                if (ti.getAnfme() == null) {
                    throw new CoolException("任务明细数量异常");
                }
                taskService.enqueueCloudWmsOutNotifyLogEarly(outTask, ti, QuantityUtils.toScaledBigDecimal(ti.getAnfme()));
            }
        });
    }
@@ -591,4 +679,9 @@
    private static String buildAllocKey(Long matnrId, String batch, String fieldsIndex) {
        return (matnrId != null ? matnrId : "") + "|" + (batch != null ? batch : "") + "|" + (fieldsIndex != null ? fieldsIndex : "");
    }
    private boolean isOutAppendSameLocTaskEnabled() {
        Boolean v = configService.getVal(GlobalConfigCode.OUT_APPEND_SAME_LOC_TASK_ENABLED, Boolean.class);
        return Boolean.TRUE.equals(v);
    }
}