|  |  |  | 
|---|
|  |  |  | 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.controller.erp.params.TaskInParam; | 
|---|
|  |  |  | import com.vincent.rsf.server.api.entity.dto.InTaskMsgDto; | 
|---|
|  |  |  | import com.vincent.rsf.server.api.service.WcsService; | 
|---|
|  |  |  | import com.vincent.rsf.server.common.constant.Constants; | 
|---|
|  |  |  | 
|---|
|  |  |  | import com.vincent.rsf.server.system.utils.SerialRuleUtils; | 
|---|
|  |  |  | import com.vincent.rsf.server.system.utils.SystemAuthUtils; | 
|---|
|  |  |  | import lombok.Synchronized; | 
|---|
|  |  |  | import lombok.extern.slf4j.Slf4j; | 
|---|
|  |  |  | import org.apache.commons.lang3.StringUtils; | 
|---|
|  |  |  | import org.slf4j.Logger; | 
|---|
|  |  |  | import org.slf4j.LoggerFactory; | 
|---|
|  |  |  | 
|---|
|  |  |  | import java.util.*; | 
|---|
|  |  |  | import java.util.stream.Collectors; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Slf4j | 
|---|
|  |  |  | @Service("taskService") | 
|---|
|  |  |  | public class TaskServiceImpl extends ServiceImpl<TaskMapper, Task> implements TaskService { | 
|---|
|  |  |  |  | 
|---|
|  |  |  | 
|---|
|  |  |  | private OutStockService outStockService; | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | private OutStockItemService outStockItemService; | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | private CheckOrderItemService checkOrderItemService; | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | private CheckOrderService checkOrderService; | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | private CheckDiffService checkDiffService; | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | private CheckDiffItemService checkDiffItemService; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | @Transactional(rollbackFor = Exception.class) | 
|---|
|  |  |  | 
|---|
|  |  |  | if (Objects.isNull(deviceSite)) { | 
|---|
|  |  |  | throw new CoolException("站点不存在!!"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | DeviceBind deviceBind = deviceBindService.getById(LocUtils.getAreaType(Integer.valueOf(deviceSite.getSite()))); | 
|---|
|  |  |  | DeviceBind deviceBind = deviceBindService.getById(LocUtils.getAreaType(deviceSite.getSite())); | 
|---|
|  |  |  | if (Cools.isEmpty(deviceBind)) { | 
|---|
|  |  |  | throw new CoolException("库位规则未知"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | 
|---|
|  |  |  | if (!this.save(task)) { | 
|---|
|  |  |  | throw new CoolException("任务保存失败!!"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | BasStation station = basStationService.getOne(new LambdaQueryWrapper<BasStation>().eq(BasStation::getStationName, deviceSite.getSite())); | 
|---|
|  |  |  | if (Objects.isNull(station) || station.getUseStatus() != LocStsType.LOC_STS_TYPE_O.type) { | 
|---|
|  |  |  | throw new CoolException("站点不存在或站点不处于空库状态!!"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | station.setUseStatus(LocStsType.LOC_STS_TYPE_R.type); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | if (basStationService.updateById(station)) { | 
|---|
|  |  |  | 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("库位预约失败!!"); | 
|---|
|  |  |  | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | @Synchronized | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | @Transactional(rollbackFor = Exception.class) | 
|---|
|  |  |  | public void complateInTask(List<Task> tasks) throws Exception { | 
|---|
|  |  |  | Long loginUserId = SystemAuthUtils.getLoginUserId(); | 
|---|
|  |  |  | for (Task task : tasks) { | 
|---|
|  |  |  | if (task.getTaskType().equals(TaskType.TASK_TYPE_IN.type)) { | 
|---|
|  |  |  | //1.入库 | 
|---|
|  |  |  | complateInstock(task, loginUserId); | 
|---|
|  |  |  | } else if (task.getTaskType().equals(TaskType.TASK_TYPE_PICK_IN.type) || task.getTaskType().equals(TaskType.TASK_TYPE_CHECK_IN.type)) { | 
|---|
|  |  |  | //53.拣料再入库 | 
|---|
|  |  |  | //57.盘点再入库 | 
|---|
|  |  |  | pickComplateInStock(task, loginUserId); | 
|---|
|  |  |  | } else if (task.getTaskType().equals(TaskType.TASK_TYPE_LOC_MOVE.type)) { | 
|---|
|  |  |  | //移库 | 
|---|
|  |  |  | moveInStock(task, loginUserId); | 
|---|
|  |  |  | try { | 
|---|
|  |  |  | if (task.getTaskType().equals(TaskType.TASK_TYPE_IN.type)) { | 
|---|
|  |  |  | //1.入库 | 
|---|
|  |  |  | complateInstock(task, loginUserId); | 
|---|
|  |  |  | } else if (task.getTaskType().equals(TaskType.TASK_TYPE_PICK_IN.type) || task.getTaskType().equals(TaskType.TASK_TYPE_CHECK_IN.type)) { | 
|---|
|  |  |  | //53.拣料再入库 | 
|---|
|  |  |  | //57.盘点再入库 | 
|---|
|  |  |  | pickComplateInStock(task, loginUserId); | 
|---|
|  |  |  | } else if (task.getTaskType().equals(TaskType.TASK_TYPE_LOC_MOVE.type)) { | 
|---|
|  |  |  | //移库 | 
|---|
|  |  |  | moveInStock(task, loginUserId); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } catch (Exception ex) { | 
|---|
|  |  |  | log.error(ex.getMessage(), ex); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | 
|---|
|  |  |  | //更新库位明细 | 
|---|
|  |  |  | saveLocItem(taskItems, task.getId(), loginUserId); | 
|---|
|  |  |  | } catch (Exception e) { | 
|---|
|  |  |  | throw new CoolException("库位明细更新失败!!"); | 
|---|
|  |  |  | throw new CoolException("任务号:" + task.getTaskCode() +  "库位明细更新失败!!"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | if (!locItemService.remove(new LambdaQueryWrapper<LocItem>().eq(LocItem::getLocCode, task.getOrgLoc()))) { | 
|---|
|  |  |  | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | @Synchronized | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | @Transactional(rollbackFor = Exception.class) | 
|---|
|  |  |  | public void completeTask(List<Task> tasks) throws Exception { | 
|---|
|  |  |  | public void completeTask(List<Task> tasks) { | 
|---|
|  |  |  | Long loginUserId = SystemAuthUtils.getLoginUserId(); | 
|---|
|  |  |  | for (Task task : tasks) { | 
|---|
|  |  |  | //出库任务 | 
|---|
|  |  |  | if (task.getTaskType().equals(TaskType.TASK_TYPE_OUT.type)) { | 
|---|
|  |  |  | //全托出库 | 
|---|
|  |  |  | try { | 
|---|
|  |  |  | complateOutStock(task, loginUserId); | 
|---|
|  |  |  | } else if (task.getTaskType().equals(TaskType.TASK_TYPE_PICK_AGAIN_OUT.type)) { | 
|---|
|  |  |  | //拣料出库 | 
|---|
|  |  |  | complateOutStock(task, loginUserId); | 
|---|
|  |  |  | } else if (task.getTaskType().equals(TaskType.TASK_TYPE_CHECK_OUT.type)) { | 
|---|
|  |  |  | //盘点出库 | 
|---|
|  |  |  | complateOutStock(task, loginUserId); | 
|---|
|  |  |  | } catch (Exception e) { | 
|---|
|  |  |  | log.error(e.getMessage(), e); | 
|---|
|  |  |  | //                throw new CoolException(e.getMessage()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | 
|---|
|  |  |  | if (taskItems.isEmpty()) { | 
|---|
|  |  |  | 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(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("数据错误,作业中库存数据丢失!!"); | 
|---|
|  |  |  | 
|---|
|  |  |  | //保存入出库流水 | 
|---|
|  |  |  | saveStockItems(taskItems, task, null, null, taskItem.getWkType(), taskItem.getOrderType(), loginUserId); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | if (!locItemWorkingService.remove(new LambdaQueryWrapper<LocItemWorking>().eq(LocItemWorking::getTaskId, task.getId()))) { | 
|---|
|  |  |  | throw new CoolException("作业中库存删除失败!!"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | locItemWorkingService.remove(new LambdaQueryWrapper<LocItemWorking>().eq(LocItemWorking::getTaskId, task.getId())); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | task.setTaskStatus(TaskStsType.UPDATED_IN.id).setUpdateTime(new Date()).setUpdateBy(loginUserId); | 
|---|
|  |  |  | if (!taskService.updateById(task)) { | 
|---|
|  |  |  | 
|---|
|  |  |  | .set(Loc::getUseStatus, LocStsType.LOC_STS_TYPE_F.type))) { | 
|---|
|  |  |  | throw new CoolException("源库位状态修改失败!!"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if (!locService.update(new LambdaUpdateWrapper<Loc>().eq(Loc::getCode, task.getTargLoc()).set(Loc::getUseStatus, LocStsType.LOC_STS_TYPE_O.type))) { | 
|---|
|  |  |  | if (!locService.update(new LambdaUpdateWrapper<Loc>() | 
|---|
|  |  |  | .eq(Loc::getCode, task.getTargLoc()) | 
|---|
|  |  |  | .set(Loc::getUseStatus, LocStsType.LOC_STS_TYPE_O.type))) { | 
|---|
|  |  |  | throw new CoolException("移库目标库位状态修改失败!!"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } else if (task.getTaskType().equals(TaskType.TASK_TYPE_CHECK_OUT.type)) { | 
|---|
|  |  |  |  | 
|---|
|  |  |  | List<TaskItem> taskItems = taskItemService.list(new LambdaQueryWrapper<TaskItem>().in(TaskItem::getTaskId, Arrays.asList(ids))); | 
|---|
|  |  |  | if (!taskItems.isEmpty()) { | 
|---|
|  |  |  |  | 
|---|
|  |  |  | taskItems.forEach(taskItem -> { | 
|---|
|  |  |  | if (!checkOrderItemService.update(new LambdaUpdateWrapper<WkOrderItem>() | 
|---|
|  |  |  | .eq(WkOrderItem::getId, taskItem.getOrderItemId()) | 
|---|
|  |  |  | .setSql("work_qty = work_qty - " + taskItem.getAnfme()))) { | 
|---|
|  |  |  | throw new CoolException("明细数量修改失败!!"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | if (!checkOrderService.update(new LambdaUpdateWrapper<WkOrder>() | 
|---|
|  |  |  | .eq(WkOrder::getId, taskItem.getOrderId()) | 
|---|
|  |  |  | .setSql("work_qty = work_qty - " + taskItem.getAnfme()))) { | 
|---|
|  |  |  | throw new CoolException("盘点单执行数量修改失败!!"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | CheckDiffItem serviceOne = checkDiffItemService.getOne(new LambdaQueryWrapper<CheckDiffItem>().eq(CheckDiffItem::getTaskItemId, taskItem.getId())); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | checkDiffItemService.remove(new LambdaQueryWrapper<CheckDiffItem>().eq(CheckDiffItem::getTaskItemId, taskItem.getId())); | 
|---|
|  |  |  | if (!Objects.isNull(serviceOne)) { | 
|---|
|  |  |  | CheckDiff checkDiff = checkDiffService.getById(serviceOne.getCheckId()); | 
|---|
|  |  |  | if (!Objects.isNull(checkDiff)) { | 
|---|
|  |  |  | checkDiffService.removeById(checkDiff.getId()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | }); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | if (!locService.update(new LambdaUpdateWrapper<Loc>() | 
|---|
|  |  |  | .eq(Loc::getCode, task.getOrgLoc()) | 
|---|
|  |  |  | .set(Loc::getUseStatus, LocStsType.LOC_STS_TYPE_F.type))) { | 
|---|
|  |  |  | throw new CoolException("源库位状态修改失败!!"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | if (!Objects.isNull(task.getWarehType()) && task.getWarehType().equals(WarehType.WAREHOUSE_TYPE_AGV.val)) { | 
|---|
|  |  |  | 
|---|
|  |  |  | if (Objects.isNull(loc)) { | 
|---|
|  |  |  | throw new CoolException("数据错误:库位信息不存在!!"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | loc.setUseStatus(LocStsType.LOC_STS_TYPE_F.type).setBarcode(task.getBarcode()).setUpdateBy(loginUserId).setUpdateTime(new Date()); | 
|---|
|  |  |  | loc.setUseStatus(LocStsType.LOC_STS_TYPE_F.type) | 
|---|
|  |  |  | .setBarcode(task.getBarcode()) | 
|---|
|  |  |  | .setUpdateBy(loginUserId) | 
|---|
|  |  |  | .setUpdateTime(new Date()); | 
|---|
|  |  |  | if (!locService.updateById(loc)) { | 
|---|
|  |  |  | throw new CoolException("库位信息修改失败!!"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | 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))) { | 
|---|
|  |  |  | 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("更新库位状态失败!!"); | 
|---|
|  |  |  | 
|---|
|  |  |  | .setTaskStatus(TaskStsType.GENERATE_IN.id); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | TaskInParam param = new TaskInParam(); | 
|---|
|  |  |  | param.setSourceStaNo(Integer.parseInt(task.getTargSite())) | 
|---|
|  |  |  | param.setSourceStaNo(task.getTargSite()) | 
|---|
|  |  |  | .setIoType(type) | 
|---|
|  |  |  | .setLocType1(Integer.parseInt(loc.getType())); | 
|---|
|  |  |  | //获取新库位 | 
|---|
|  |  |  | 
|---|
|  |  |  | if (Objects.isNull(locInfo)) { | 
|---|
|  |  |  | throw new CoolException("获取库位失败!!"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | task.setTargLoc(locInfo.getLocNo()); | 
|---|
|  |  |  | task.setTargLoc(locInfo.getLocNo()) | 
|---|
|  |  |  | .setOrgSite(task.getTargSite()); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | if (!this.updateById(task)) { | 
|---|
|  |  |  | throw new CoolException("任务状态更新失败!!"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | //获取因当前任务出库的所有物料信息 | 
|---|
|  |  |  | List<LocItemWorking> tempLocs = locItemWorkingService.list(new LambdaQueryWrapper<LocItemWorking>().eq(LocItemWorking::getTaskId, task.getId())); | 
|---|
|  |  |  | if (tempLocs.isEmpty()) { | 
|---|
|  |  |  | throw new CoolException("数据错误,作业中库存数据丢失!!"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | List<TaskItem> taskItems = taskItemService.list(new LambdaQueryWrapper<TaskItem>().eq(TaskItem::getTaskId, task.getId())); | 
|---|
|  |  |  | for (int i = 0; i < taskItems.size(); i++) { | 
|---|
|  |  |  | TaskItem taskItem = taskItems.get(i); | 
|---|
|  |  |  | LocItemWorking locWorking = locItemWorkingService.getOne(new LambdaQueryWrapper<LocItemWorking>() | 
|---|
|  |  |  | .eq(LocItemWorking::getTaskId, taskItem.getTaskId()) | 
|---|
|  |  |  | .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 (task.getTaskType().equals(TaskType.TASK_TYPE_CHECK_IN.type)) { | 
|---|
|  |  |  | locWorking.setWorkQty(0.0); | 
|---|
|  |  |  | locWorking.setAnfme(taskItem.getAnfme()); | 
|---|
|  |  |  | } else { | 
|---|
|  |  |  | if (locWorking.getAnfme().compareTo(taskItem.getAnfme()) >= 0) { | 
|---|
|  |  |  | locWorking.setWorkQty(0.0); | 
|---|
|  |  |  | locWorking.setAnfme(Math.round((locWorking.getAnfme() - taskItem.getAnfme()) * 10000) / 10000.0); | 
|---|
|  |  |  | } else { | 
|---|
|  |  |  | continue; | 
|---|
|  |  |  | if (taskItems.isEmpty()) { | 
|---|
|  |  |  | throw new CoolException("数据错误:任务明细为空!!"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | tempLocs.forEach(working -> { | 
|---|
|  |  |  | taskItems.forEach(taskItem -> { | 
|---|
|  |  |  | if (taskItem.getMatnrId().equals(working.getMatnrId())) { | 
|---|
|  |  |  | Double minQty = taskItem.getAnfme(); | 
|---|
|  |  |  | if (!task.getTaskType().equals(TaskType.TASK_TYPE_CHECK_IN.type)) { | 
|---|
|  |  |  | minQty = Math.round((working.getAnfme() - taskItem.getAnfme()) * 10000) / 10000.0; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if (minQty.compareTo(0.0) > 0) { | 
|---|
|  |  |  | taskItem.setAnfme(minQty); | 
|---|
|  |  |  | if (!taskItemService.updateById(taskItem)) { | 
|---|
|  |  |  | throw new CoolException("任务明细修改失败!!"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } else { | 
|---|
|  |  |  | if (!taskItemService.removeById(taskItem)) { | 
|---|
|  |  |  | throw new CoolException("任务明细修改失败!!"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if (!locItemWorkingService.updateById(locWorking)) { | 
|---|
|  |  |  | throw new CoolException("临时库存更新失败!!"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | taskItems.get(i).setAnfme(locWorking.getAnfme()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | }); | 
|---|
|  |  |  | }); | 
|---|
|  |  |  | List<Long> matnrIds = taskItems.stream().map(TaskItem::getMatnrId).collect(Collectors.toList()); | 
|---|
|  |  |  | //删除与任务明细重复的库存信息,以任务明细为准 | 
|---|
|  |  |  | List<LocItemWorking> itemWorkings = tempLocs.stream() | 
|---|
|  |  |  | .filter(working -> !matnrIds.contains(working.getMatnrId())) | 
|---|
|  |  |  | .collect(Collectors.toList()); | 
|---|
|  |  |  | itemWorkings.forEach(working -> { | 
|---|
|  |  |  | TaskItem taskItem = taskItems.stream().findFirst().get(); | 
|---|
|  |  |  | taskItem.setMatnrId(working.getMatnrId()) | 
|---|
|  |  |  | .setMaktx(working.getMaktx()) | 
|---|
|  |  |  | .setMatnrId(working.getMatnrId()) | 
|---|
|  |  |  | .setMatnrCode(working.getMatnrCode()) | 
|---|
|  |  |  | .setSpec(working.getSpec()) | 
|---|
|  |  |  | .setAnfme(working.getAnfme()) | 
|---|
|  |  |  | .setBatch(working.getBatch()) | 
|---|
|  |  |  | .setFieldsIndex(working.getFieldsIndex()) | 
|---|
|  |  |  | .setUnit(working.getUnit()) | 
|---|
|  |  |  | .setId(null) | 
|---|
|  |  |  | .setModel(working.getModel()); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | if (!taskItemService.updateBatchById(taskItems)) { | 
|---|
|  |  |  | throw new CoolException("任务明细更新失败!!"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if (!taskItemService.saveOrUpdate(taskItem)) { | 
|---|
|  |  |  | throw new CoolException("临时库存转任务明细失败!!"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | }); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | Loc one = locService.getOne(new LambdaQueryWrapper<Loc>().eq(Loc::getCode, task.getTargLoc())); | 
|---|
|  |  |  | if (Objects.isNull(one)) { | 
|---|
|  |  |  | locItemWorkingService.remove(new LambdaQueryWrapper<LocItemWorking>().eq(LocItemWorking::getTaskId, task.getId())); | 
|---|
|  |  |  | Loc loc1 = locService.getOne(new LambdaQueryWrapper<Loc>().eq(Loc::getCode, task.getTargLoc())); | 
|---|
|  |  |  | if (Objects.isNull(loc1)) { | 
|---|
|  |  |  | throw new CoolException("目标库位不存在!!"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | one.setUseStatus(LocStsType.LOC_STS_TYPE_S.type); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | if (!locService.updateById(one)) { | 
|---|
|  |  |  | List<LocItemWorking> workings = new ArrayList<>(); | 
|---|
|  |  |  | List<TaskItem> items = taskItemService.list(new LambdaQueryWrapper<TaskItem>().eq(TaskItem::getTaskId, task.getId())); | 
|---|
|  |  |  | items.forEach(taskItem -> { | 
|---|
|  |  |  | if (taskItem.getAnfme() > 0) { | 
|---|
|  |  |  | LocItemWorking itemWorking = new LocItemWorking(); | 
|---|
|  |  |  | BeanUtils.copyProperties(taskItem, itemWorking); | 
|---|
|  |  |  | itemWorking.setTaskId(task.getId()) | 
|---|
|  |  |  | .setLocId(loc1.getId()) | 
|---|
|  |  |  | .setLocCode(loc1.getCode()); | 
|---|
|  |  |  | workings.add(itemWorking); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | }); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | if (!locItemWorkingService.saveBatch(workings)) { | 
|---|
|  |  |  | throw new CoolException("临时库存更新失败!!"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | loc1.setUseStatus(LocStsType.LOC_STS_TYPE_S.type); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | if (!locService.updateById(loc1)) { | 
|---|
|  |  |  | throw new CoolException("库位预约入库失败!!"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return task; | 
|---|
|  |  |  | 
|---|
|  |  |  | throw new CoolException("波次明细不存在!!"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | try { | 
|---|
|  |  |  | saveOutStockItem(maps.get(key), null, waveItem, loginUserId); | 
|---|
|  |  |  | saveOutStockItem(maps.get(key), null, waveItem, null, loginUserId); | 
|---|
|  |  |  | } catch (Exception e) { | 
|---|
|  |  |  | throw new CoolException(e.getMessage()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | 
|---|
|  |  |  | throw new CoolException("单据明细不存在!!"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | try { | 
|---|
|  |  |  | saveOutStockItem(maps.get(key), orderItem, null, loginUserId); | 
|---|
|  |  |  | saveOutStockItem(maps.get(key), orderItem,  loginUserId); | 
|---|
|  |  |  | } catch (Exception e) { | 
|---|
|  |  |  | throw new CoolException(e.getMessage()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } else if (task.getResource().equals(TaskResouceType.TASK_RESOUCE_CHECK_TYPE.val)) { | 
|---|
|  |  |  | WkOrderItem orderItem = asnOrderItemService.getById(key); | 
|---|
|  |  |  | if (Objects.isNull(orderItem)) { | 
|---|
|  |  |  | throw new CoolException("单据明细不存在!!"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | try { | 
|---|
|  |  |  | saveOutStockItem(maps.get(key), null, null, orderItem, loginUserId); | 
|---|
|  |  |  | } catch (Exception e) { | 
|---|
|  |  |  | throw new CoolException(e.getMessage()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | 
|---|
|  |  |  | }); | 
|---|
|  |  |  | } else { | 
|---|
|  |  |  | try { | 
|---|
|  |  |  | saveOutStockItem(taskItems, null, null, loginUserId); | 
|---|
|  |  |  | saveOutStockItem(taskItems, null, null, null, loginUserId); | 
|---|
|  |  |  | } catch (Exception e) { | 
|---|
|  |  |  | throw new CoolException(e.getMessage()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | 
|---|
|  |  |  | * @version 1.0 | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | @Transactional(rollbackFor = Exception.class) | 
|---|
|  |  |  | public void saveOutStockItem(List<TaskItem> taskItems, WkOrderItem orderItem, WaveItem waveItem, Long loginUserId) throws Exception { | 
|---|
|  |  |  | public void saveOutStockItem(List<TaskItem> taskItems, WkOrderItem diffItem, Long loginUserId) { | 
|---|
|  |  |  | try { | 
|---|
|  |  |  | saveOutStockItem(taskItems, null, null, diffItem, loginUserId); | 
|---|
|  |  |  | } catch (Exception e) { | 
|---|
|  |  |  | throw new CoolException(e); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * @author Ryan | 
|---|
|  |  |  | * @date 2025/5/20 | 
|---|
|  |  |  | * @description: 出库信息保存至库存明细表 | 
|---|
|  |  |  | * @version 1.0 | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | @Transactional(rollbackFor = Exception.class) | 
|---|
|  |  |  | public void saveOutStockItem(List<TaskItem> taskItems, WkOrderItem orderItem, WaveItem waveItem, WkOrderItem diffItem, Long loginUserId) throws Exception { | 
|---|
|  |  |  | Stock stock = new Stock(); | 
|---|
|  |  |  | String ruleCode = SerialRuleUtils.generateRuleCode(SerialRuleCode.SYS_STOCK_CODE, null); | 
|---|
|  |  |  | if (StringUtils.isBlank(ruleCode)) { | 
|---|
|  |  |  | 
|---|
|  |  |  | .setUpdateBy(loginUserId) | 
|---|
|  |  |  | .setUpdateTime(new Date()) | 
|---|
|  |  |  | .setAnfme(sum); | 
|---|
|  |  |  | if (Objects.isNull(waveItem)) { | 
|---|
|  |  |  | if (!Objects.isNull(waveItem)) { | 
|---|
|  |  |  | //TODO 生成波次时需要将波次号写入单据,通过物料,批次,动态字段等唯一值反查单据信息 | 
|---|
|  |  |  | stock.setSourceId(waveItem.getId()).setType(OrderType.ORDER_OUT.type); | 
|---|
|  |  |  | } else if (!Objects.isNull(orderItem) && StringUtils.isNotBlank(orderItem.getId() + "")) { | 
|---|
|  |  |  | WkOrder wkOrder = asnOrderService.getById(orderItem.getOrderId()); | 
|---|
|  |  |  | Double qty = Math.round((wkOrder.getQty() + sum) * 10000) / 10000.0; | 
|---|
|  |  |  | wkOrder.setExceStatus(AsnExceStatus.OUT_STOCK_STATUS_TASK_WORKING.val) | 
|---|
|  |  |  | .setQty(qty); | 
|---|
|  |  |  | if (!asnOrderService.updateById(wkOrder)) { | 
|---|
|  |  |  | throw new CoolException("出库单更新失败!!"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | stock.setWkType(Short.parseShort(wkOrder.getWkType())) | 
|---|
|  |  |  | .setType(OrderType.ORDER_OUT.type); | 
|---|
|  |  |  | } else if (!Objects.isNull(diffItem)) { | 
|---|
|  |  |  | stock.setWkType(Short.parseShort(OrderWorkType.ORDER_WORK_TYPE_STOCK_CHECK.type)) | 
|---|
|  |  |  | .setType(OrderType.ORDER_CHECK.type); | 
|---|
|  |  |  | } else { | 
|---|
|  |  |  | stock.setWkType(Short.parseShort(OrderWorkType.ORDER_WORK_TYPE_STOCK_OUT.type)) | 
|---|
|  |  |  | .setMemo("库存出库,无单据信息!!") | 
|---|
|  |  |  | .setType(OrderType.ORDER_OUT.type); | 
|---|
|  |  |  | } else if (!Objects.isNull(orderItem) && StringUtils.isNotBlank(orderItem.getId() + "")) { | 
|---|
|  |  |  | WkOrder wkOrder = asnOrderService.getById(orderItem.getOrderId()); | 
|---|
|  |  |  | stock.setWkType(Short.parseShort(wkOrder.getWkType())) | 
|---|
|  |  |  | .setType(OrderType.ORDER_OUT.type); | 
|---|
|  |  |  | } else { | 
|---|
|  |  |  | //TODO 生成波次时需要将波次号写入单据,通过物料,批次,动态字段等唯一值反查单据信息 | 
|---|
|  |  |  | stock.setSourceId(waveItem.getId()).setType(OrderType.ORDER_OUT.type); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if (!stockService.save(stock)) { | 
|---|
|  |  |  | throw new CoolException("库存保存失败!!"); | 
|---|
|  |  |  | 
|---|
|  |  |  | if (!stockItemService.saveBatch(stockItems)) { | 
|---|
|  |  |  | throw new CoolException("库存修改架失败!!"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | 
|---|
|  |  |  | List<LocItem> locItems = new ArrayList<>(); | 
|---|
|  |  |  | items.forEach(taskItem -> { | 
|---|
|  |  |  | Loc loc = locService.getOne(new LambdaQueryWrapper<Loc>().eq(Loc::getCode, task.getTargLoc()), false); | 
|---|
|  |  |  | if (Objects.isNull(loc)) { | 
|---|
|  |  |  | throw new CoolException("库位不存在!!"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | LocItem item = new LocItem(); | 
|---|
|  |  |  | BeanUtils.copyProperties(taskItem, item); | 
|---|
|  |  |  | item.setLocCode(loc.getCode()).setId(null) | 
|---|
|  |  |  | .setLocId(loc.getId()) | 
|---|
|  |  |  | .setUpdateBy(loginUserId) | 
|---|
|  |  |  | .setWkType(taskItem.getWkType()) | 
|---|
|  |  |  | .setType(taskItem.getOrderType()); | 
|---|
|  |  |  | locItems.add(item); | 
|---|
|  |  |  | LocItem locItem = locItemService.getOne(new LambdaQueryWrapper<LocItem>() | 
|---|
|  |  |  | .eq(LocItem::getMatnrId, taskItem.getMatnrId()) | 
|---|
|  |  |  | .eq(LocItem::getLocId, loc.getId()) | 
|---|
|  |  |  | .eq(StringUtils.isNoneBlank(taskItem.getBatch()), LocItem::getBatch, taskItem.getBatch()) | 
|---|
|  |  |  | .eq(StringUtils.isNoneBlank(taskItem.getFieldsIndex()), LocItem::getFieldsIndex, taskItem.getFieldsIndex())); | 
|---|
|  |  |  | if (Objects.isNull(locItem)) { | 
|---|
|  |  |  | BeanUtils.copyProperties(taskItem, item); | 
|---|
|  |  |  | item.setLocCode(loc.getCode()) | 
|---|
|  |  |  | .setId(null) | 
|---|
|  |  |  | .setLocId(loc.getId()) | 
|---|
|  |  |  | .setUpdateBy(loginUserId) | 
|---|
|  |  |  | .setWkType(taskItem.getWkType()) | 
|---|
|  |  |  | .setType(taskItem.getOrderType()); | 
|---|
|  |  |  | locItems.add(item); | 
|---|
|  |  |  | } else { | 
|---|
|  |  |  | locItem.setAnfme(Math.round((locItem.getAnfme() + taskItem.getAnfme()) * 10000) / 10000.0) | 
|---|
|  |  |  | .setUpdateTime(new Date()); | 
|---|
|  |  |  | locItems.add(locItem); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | }); | 
|---|
|  |  |  | if (!locItemService.saveBatch(locItems)) { | 
|---|
|  |  |  | throw new CoolException("库位明细更新失败!!"); | 
|---|
|  |  |  | 
|---|
|  |  |  | * @return | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | @Transactional(rollbackFor = Exception.class) | 
|---|
|  |  |  | public void saveStockItems(List<TaskItem> items, Task task, Long id, String code, Short wkType, String type, Long loginUserId) throws | 
|---|
|  |  |  | Exception { | 
|---|
|  |  |  | public void saveStockItems(List<TaskItem> items, Task task, Long id, String code, Short wkType, String type, Long loginUserId) { | 
|---|
|  |  |  | Stock stock = new Stock(); | 
|---|
|  |  |  | String ruleCode = SerialRuleUtils.generateRuleCode(SerialRuleCode.SYS_STOCK_CODE, null); | 
|---|
|  |  |  | if (StringUtils.isBlank(ruleCode)) { | 
|---|
|  |  |  | 
|---|
|  |  |  | if (!stockItemService.saveBatch(stockItems)) { | 
|---|
|  |  |  | throw new CoolException("库存修改架失败!!"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | WkOrder order = asnOrderService.getOne(new LambdaQueryWrapper<WkOrder>().eq(WkOrder::getCode, code)); | 
|---|
|  |  |  | if (!Objects.isNull(order)) { | 
|---|
|  |  |  | //            Double qty = Math.round((order.getQty() + sum) * 10000) / 10000.0; | 
|---|
|  |  |  | order.setExceStatus(AsnExceStatus.ASN_EXCE_STATUS_TASK_EXCE.val); | 
|---|
|  |  |  | if (!asnOrderService.updateById(order)) { | 
|---|
|  |  |  | throw new CoolException("入库单状态修改失败!!"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|