package com.vincent.rsf.server.manager.service.impl; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.vincent.rsf.framework.common.R; import com.vincent.rsf.framework.exception.CoolException; import com.vincent.rsf.server.manager.controller.params.PakinItem; import com.vincent.rsf.server.manager.controller.params.WaitPakinParam; import com.vincent.rsf.server.manager.entity.*; import com.vincent.rsf.server.manager.enums.PakinIOStatus; import com.vincent.rsf.server.manager.mapper.WaitPakinMapper; import com.vincent.rsf.server.manager.mapper.MatnrMapper; import com.vincent.rsf.server.manager.service.*; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.vincent.rsf.server.common.utils.QuantityUtils; import com.vincent.rsf.server.system.constant.SerialRuleCode; import com.vincent.rsf.server.system.utils.SerialRuleUtils; 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.*; import java.util.stream.Collectors; @Service("waitPakinService") public class WaitPakinServiceImpl extends ServiceImpl implements WaitPakinService { @Autowired private AsnOrderService asnOrderService; @Autowired private AsnOrderItemService asnOrderItemService; @Autowired private WaitPakinService waitPakinService; @Autowired private WaitPakinItemService waitPakinItemService; @Autowired private WarehouseAreasItemService warehouseAreasItemService; @Autowired private LocService locService; @Autowired private TaskService taskService; @Autowired private TaskItemService taskItemService; @Autowired private MatnrMapper matnrMapper; /** * @param * @param userId * @return * @author Ryan * @description 组托 * @time 2025/3/29 14:42 */ @Override @Transactional(rollbackFor = Exception.class) public synchronized WaitPakin mergeItems(WaitPakinParam waitPakin, Long userId) { if (Objects.isNull(waitPakin.getItems()) || waitPakin.getItems().isEmpty()) { throw new CoolException("参数错误:物料跟踪码为空!"); } if (StringUtils.isBlank(waitPakin.getBarcode())) { throw new CoolException("参数错误:料箱码为空!!"); } WaitPakin pakin = waitPakinService.getOne(new LambdaQueryWrapper() .eq(WaitPakin::getBarcode, waitPakin.getBarcode())); // 如果料箱码已经组托过,提示已组托,请更换料箱码 if (!Objects.isNull(pakin)) { throw new CoolException("已组托,请更换料箱码"); } List tasks = taskService.list(new LambdaQueryWrapper().eq(Task::getBarcode, waitPakin.getBarcode())); if (!tasks.isEmpty()) { throw new CoolException("当前料箱已有任务档在执行,不能再次组托!!"); } // 检查料箱码是否在库存中存在(通过库位表查询) List locs = locService.list(new LambdaQueryWrapper().eq(Loc::getBarcode, waitPakin.getBarcode())); if (!locs.isEmpty()) { throw new CoolException("在库,请更换料箱码"); } Double sum = QuantityUtils.roundToScale(waitPakin.getItems().stream().mapToDouble(PakinItem::getReceiptQty).sum()); WaitPakin waitPakin1 = new WaitPakin(); if (Objects.isNull(pakin)) { String ruleCode = SerialRuleUtils.generateRuleCode(SerialRuleCode.SYS_WAIT_PAKIN_CODE, null); if (StringUtils.isBlank(ruleCode)) { throw new CoolException("编码规则错误: 编码规则「SYS_WAIT_PAKIN_CODE」规则是不存在"); } waitPakin1.setCode(ruleCode) //状态修改为入库中 .setIoStatus(PakinIOStatus.PAKIN_IO_STATUS_DONE.val) .setAnfme(sum) .setUpdateBy(userId) .setCreateBy(userId) .setBarcode(waitPakin.getBarcode()); if (!this.save(waitPakin1)) { throw new CoolException("主单保存失败!!"); } } else { BeanUtils.copyProperties(pakin, waitPakin1); } List items = new ArrayList<>(); for (PakinItem pakinItem1 : waitPakin.getItems()) { WaitPakinItem pakinItem = new WaitPakinItem(); // 如果ASN单号为空,从物料信息表获取物料信息,不查询收货区 if (StringUtils.isBlank(pakinItem1.getAsnCode())) { if (Objects.isNull(pakinItem1.getMatnrId())) { throw new CoolException("物料ID不能为空!!"); } // 从物料信息表获取物料信息 Matnr matnr = matnrMapper.selectById(pakinItem1.getMatnrId()); if (Objects.isNull(matnr)) { throw new CoolException("物料信息不存在,物料ID:" + pakinItem1.getMatnrId()); } // 设置组托明细信息(不设置source,因为不在收货区) pakinItem.setPakinId(waitPakin1.getId()) .setSource(null) // 不在收货区,source设为null .setAsnId(null) .setAsnCode(null) .setAsnItemId(null) .setIsptResult(null) .setPlatItemId(null) .setPlatOrderCode(null) .setPlatWorkCode(null) .setProjectCode(null) .setBatch(null) .setUnit(matnr.getStockUnit()) .setFieldsIndex(matnr.getFieldsIndex()) .setMatnrId(matnr.getId()) .setMaktx(matnr.getName()) .setUpdateBy(userId) .setCreateBy(userId) .setMatnrCode(matnr.getCode()); // 设置组托数量 if (pakinItem1.getReceiptQty() == null || pakinItem1.getReceiptQty().compareTo(0.0) <= 0) { throw new CoolException("组托数量不能小于等于零!!"); } pakinItem.setAnfme(QuantityUtils.roundToScale(pakinItem1.getReceiptQty())) .setTrackCode(pakinItem1.getTrackCode()); } else { // 收货区已停用:有ASN单号时从订单明细获取物料信息 WkOrderItem orderItem = asnOrderItemService.getById(pakinItem1.getId()); if (null == orderItem) { throw new CoolException("订单明细不存在!!"); } WkOrder order = asnOrderService.getById(orderItem.getOrderId()); if (null == order) { throw new CoolException("订单不存在!!"); } pakinItem.setAnfme(QuantityUtils.roundToScale(orderItem.getAnfme())) .setPakinId(waitPakin1.getId()) .setSource(null) // 收货区已停用,不关联收货区 .setAsnId(order.getId()) .setAsnCode(order.getCode()) .setAsnItemId(orderItem.getId()) .setIsptResult(orderItem.getIsptResult()) .setPlatItemId(orderItem.getPlatItemId()) .setPlatOrderCode(orderItem.getPlatOrderCode()) .setPlatWorkCode(orderItem.getPlatWorkCode()) .setProjectCode(orderItem.getProjectCode()) .setBatch(orderItem.getSplrBatch()) .setUnit(orderItem.getStockUnit()) .setFieldsIndex(orderItem.getFieldsIndex()) .setMatnrId(orderItem.getMatnrId()) .setMaktx(orderItem.getMaktx()) .setUpdateBy(userId) .setCreateBy(userId) .setMatnrCode(orderItem.getMatnrCode()); pakinItem.setType(null == order.getType() ? null : order.getType()) .setWkType(StringUtils.isNotBlank(order.getWkType()) ? Short.parseShort(order.getWkType()) : null); for (PakinItem waitPakinItem : waitPakin.getItems()) { if (waitPakinItem.getId().equals(orderItem.getId())) { if (waitPakinItem.getReceiptQty() == null || waitPakinItem.getReceiptQty().compareTo(0.0) <= 0) { throw new CoolException("组托数量不能小于等于零!!"); } if (QuantityUtils.compare(waitPakinItem.getReceiptQty(), orderItem.getAnfme()) > 0) { throw new CoolException("组托数量不能大于计划数量!!"); } pakinItem.setAnfme(QuantityUtils.roundToScale(waitPakinItem.getReceiptQty())) .setTrackCode(waitPakinItem.getTrackCode()); } } } items.add(pakinItem); } if (!waitPakinItemService.saveBatch(items)) { throw new CoolException("组托明细保存失败!!"); } List pakinItems = waitPakinItemService.list(new LambdaQueryWrapper().eq(WaitPakinItem::getPakinId, waitPakin1.getId())); Double waitSum = 0.0; if (!pakinItems.isEmpty()) { waitSum = QuantityUtils.roundToScale(pakinItems.stream().mapToDouble(WaitPakinItem::getAnfme).sum()); } // Double total = Math.round((sum + waitSum) * 100) / 100.0; // 收货区已停用,不再更新收货区 // for (WaitPakinItem pakinItem : items) { // if (Objects.isNull(pakinItem.getSource())) continue; // WarehouseAreasItem one = warehouseAreasItemService.getOne(new LambdaQueryWrapper() // .eq(WarehouseAreasItem::getId, pakinItem.getSource())); // if (Objects.isNull(one)) throw new CoolException("收货区数据错误!!"); // Double workQty = Math.round((one.getWorkQty() + pakinItem.getAnfme()) * 1000000) / 1000000.0; // Double qty = Math.round((workQty + one.getQty()) * 1000000) / 1000000.0; // one.setWorkQty(workQty); // if (qty.compareTo(one.getAnfme()) > 0) throw new CoolException("组托数量不能大于收货数量!!"); // if (!warehouseAreasItemService.saveOrUpdate(one)) throw new CoolException("收货区执行数量修改失败!!"); // } waitPakin1.setAnfme(waitSum); if (!this.updateById(waitPakin1)) { throw new CoolException("组托数量修改失败!!"); } return waitPakin1; } /** * @param * @return * @author Ryan * @description 组托解绑 * @time 2025/3/29 14:42 */ @Override @Transactional(rollbackFor = Exception.class) public synchronized WaitPakin unBind(WaitPakinParam param) { String barcode = param.getBarcode(); if (StringUtils.isNotBlank(barcode)) { WaitPakin waitPakins = waitPakinService.getOne(new LambdaQueryWrapper().eq(WaitPakin::getBarcode, barcode)); if (Objects.isNull(waitPakins)) { throw new CoolException("组托不存在!!"); } List paramItems = param.getItems(); if (Objects.isNull(paramItems) || paramItems.isEmpty()) { throw new CoolException("解绑物料不能为空!!"); } List list = paramItems.stream().map(PakinItem::getId).collect(Collectors.toList()); List pakinItems = waitPakinItemService.list(new LambdaQueryWrapper() .in(WaitPakinItem::getId, list) ); if (pakinItems.isEmpty()) { throw new CoolException("数据错误:组托明细不存在!!"); } // List ids = pakinItems.stream().map(WaitPakinItem::getId).collect(Collectors.toList()); // if (!waitPakinItemService.removeByIds(ids)) { // throw new CoolException("组托明细解绑失败!!"); // } // 收货区已停用:仅保留非空 source 用于兼容旧数据,不再更新收货区 List list2 = pakinItems.stream().map(WaitPakinItem::getSource).filter(Objects::nonNull).collect(Collectors.toList()); List warehouseAreasItems = list2.isEmpty() ? Collections.emptyList() : warehouseAreasItemService.listByIds(list2); for (int i1 = 0; i1 < pakinItems.size(); i1++) { for (PakinItem item : paramItems) { if (item.getId().equals(pakinItems.get(i1).getId())) { if (QuantityUtils.compare(pakinItems.get(i1).getAnfme(), item.getReceiptQty()) > 0) { if (QuantityUtils.compare(item.getReceiptQty(), 0.00) == 0) { throw new CoolException("解绑数量不能为零!!"); } Double reslt = QuantityUtils.subtract(QuantityUtils.subtract(pakinItems.get(i1).getAnfme(), pakinItems.get(i1).getWorkQty()), pakinItems.get(i1).getQty()); if (QuantityUtils.compare(item.getReceiptQty(), reslt) > 0) { throw new CoolException("解绑数量不能大于剩余可执行数!!"); } Double anfme = QuantityUtils.subtract(pakinItems.get(i1).getAnfme(), item.getReceiptQty()); pakinItems.get(i1).setAnfme(anfme); if (!waitPakinItemService.updateById(pakinItems.get(i1))) { throw new CoolException("组托明细数量修改失败!!"); } } else { if (!waitPakinItemService.removeById(pakinItems.get(i1).getId())) { throw new CoolException("组托明细删除失败!!"); } } // 收货区已停用,不再回写收货区 // for (int i = 0; i < warehouseAreasItems.size(); i++) { // if (warehouseAreasItems.get(i).getId().equals(pakinItems.get(i1).getSource())) { // double v = Math.round((warehouseAreasItems.get(i).getWorkQty() - item.getReceiptQty()) * 1000000) / 1000000.0; // warehouseAreasItems.get(i).setWorkQty(v); // if (!warehouseAreasItemService.updateById(warehouseAreasItems.get(i))) { // throw new CoolException("收货区数量修改失败!!"); // } // } // } } } } Double anfmes = QuantityUtils.roundToScale(paramItems.stream().mapToDouble(PakinItem::getReceiptQty).sum()); // double anfmes = warehouseAreasItems.stream().mapToDouble(WarehouseAreasItem::getAnfme).sum(); if (QuantityUtils.compare(waitPakins.getAnfme(), anfmes) <= 0) { if (!waitPakinService.removeById(waitPakins.getId())) { throw new CoolException("组托删除失败!!"); } } else { waitPakins.setAnfme(QuantityUtils.subtract(waitPakins.getAnfme(), anfmes)); if (!waitPakinService.updateById(waitPakins)) { throw new CoolException("组托数据修改失败!!"); } } return waitPakins; } return new WaitPakin(); } /** * @author Ryan * @date 2025/5/7 * @description: 删除组拖信息 * @version 1.0 */ @Override @Transactional(rollbackFor = Exception.class) public R removePakin(List pakinIds) { List pakinItems = waitPakinItemService.list(new LambdaQueryWrapper() .in(WaitPakinItem::getPakinId, pakinIds)); if (!pakinItems.isEmpty()) { List list = pakinItems.stream().map(WaitPakinItem::getId).collect(Collectors.toList()); List taskItems = taskItemService.list(new LambdaQueryWrapper().in(TaskItem::getSource, list)); if (!taskItems.isEmpty()) { return R.error("组拖档有明细任务"); } Set sourceIds = pakinItems.stream().map(WaitPakinItem::getSource).filter(Objects::nonNull).collect(Collectors.toSet()); List areasItems = sourceIds.isEmpty() ? Collections.emptyList() : warehouseAreasItemService.list(new LambdaQueryWrapper().in(WarehouseAreasItem::getId, sourceIds)); // 收货区已停用,仅兼容旧数据时回滚收货区数量 if (!areasItems.isEmpty()) { Map> listMap = pakinItems.stream().collect(Collectors.groupingBy(WaitPakinItem::getSource)); for (WarehouseAreasItem item : areasItems) { List pakin = listMap.get(item.getId()); if (Objects.isNull(pakin)) { continue; } Double sum = QuantityUtils.roundToScale(pakin.stream().mapToDouble(WaitPakinItem::getAnfme).sum()); item.setWorkQty(QuantityUtils.subtract(item.getWorkQty(), sum)); if (!warehouseAreasItemService.updateById(item)) { throw new CoolException("收货区数据回滚失败!!"); } } } Set pakinItemIds = pakinItems.stream().map(WaitPakinItem::getId).collect(Collectors.toSet()); if (!waitPakinItemService.removeByIds(pakinItemIds)) { throw new CoolException("明细删除失败!!"); } } if (!waitPakinService.removeByIds(pakinIds)) { return R.error("Delete Fail"); } return R.ok("Delete Success").add(pakinIds); } }