|  |  |  | 
|---|
|  |  |  | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; | 
|---|
|  |  |  | import com.vincent.rsf.server.manager.utils.LocManageUtil; | 
|---|
|  |  |  | import com.vincent.rsf.server.manager.utils.OptimalAlgorithmUtil; | 
|---|
|  |  |  | import com.vincent.rsf.server.system.constant.SerialRuleCode; | 
|---|
|  |  |  | import com.vincent.rsf.server.system.utils.SerialRuleUtils; | 
|---|
|  |  |  | import lombok.Synchronized; | 
|---|
|  |  |  | import lombok.extern.slf4j.Slf4j; | 
|---|
|  |  |  | import org.apache.commons.lang3.StringUtils; | 
|---|
|  |  |  | 
|---|
|  |  |  | private WaveService waveService; | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | private WaveRuleServiceImpl waveRuleService; | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | private WaveOrderRelaServiceImpl waveOrderRelaService; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * @param | 
|---|
|  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  | List<Long> orderIds = waveItems.stream().map(WaveItem::getOrderId).collect(Collectors.toList()); | 
|---|
|  |  |  | /**修改出库单状态*/ | 
|---|
|  |  |  | if (!asnOrderService.update(new LambdaUpdateWrapper<AsnOrder>() | 
|---|
|  |  |  | .set(AsnOrder::getExceStatus, AsnExceStatus.OUT_STOCK_STATUS_TASK_WORKING.val) | 
|---|
|  |  |  | .in(AsnOrder::getId, orderIds))) { | 
|---|
|  |  |  | if (!asnOrderService.update(new LambdaUpdateWrapper<WkOrder>() | 
|---|
|  |  |  | .set(WkOrder::getExceStatus, AsnExceStatus.OUT_STOCK_STATUS_TASK_WORKING.val) | 
|---|
|  |  |  | .in(WkOrder::getId, orderIds))) { | 
|---|
|  |  |  | throw new CoolException("出库单据状态修改失败!!"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return R.ok(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 选择波次明细下发任务 | 
|---|
|  |  |  | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | @Transactional(rollbackFor = Exception.class) | 
|---|
|  |  |  | public R waveToTask(Map<String, Object> map, Long loginUserId) { | 
|---|
|  |  |  | if (Objects.isNull(map.get("waveItem"))) { | 
|---|
|  |  |  | if (Objects.isNull(map.get("ids"))) { | 
|---|
|  |  |  | throw new CoolException("参数不能为空!!"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | String waveId = map.get("wave").toString(); | 
|---|
|  |  |  | Wave waves = this.getById(Long.parseLong(waveId)); | 
|---|
|  |  |  | List<Long> WaveIds = JSONArray.parseArray(JSON.toJSONString(map.get("ids")), Long.class); | 
|---|
|  |  |  | List<Wave> waves = waveService.listByIds(WaveIds); | 
|---|
|  |  |  | if (Objects.isNull(waves)) { | 
|---|
|  |  |  | throw new CoolException("波次数据不存在!!"); | 
|---|
|  |  |  | throw new CoolException("数据错误: 波次不存在!!"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | List<Long> waveItems = JSONArray.parseArray(JSON.toJSONString(map.get("waveItem")), Long.class); | 
|---|
|  |  |  | if (waveItems.isEmpty()) { | 
|---|
|  |  |  | throw new CoolException("波次明细不能为空!!"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | List<WaveItem> items = waveItemService.listByIds(waveItems); | 
|---|
|  |  |  | if (items.isEmpty()) { | 
|---|
|  |  |  | throw new CoolException("波次明细不存在!!"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | if (!waveItemService.update(new LambdaUpdateWrapper<WaveItem>() | 
|---|
|  |  |  | .set(WaveItem::getExceStatus, WaveItemExceStatus.WAVE_EXCE_STATUS_ING.val) | 
|---|
|  |  |  | .in(WaveItem::getId, waveItems))) { | 
|---|
|  |  |  | throw new CoolException("执行状态修改失败!!"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | WaveRule waveRule = waveRuleService.getOne(new LambdaQueryWrapper<WaveRule>() | 
|---|
|  |  |  | .eq(WaveRule::getType, WaveRuleType.First_In_First_Out.type)); | 
|---|
|  |  |  | if (Cools.isEmpty(waveRule)) { | 
|---|
|  |  |  | throw new CoolException("未找到当前策略"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | List<WaveToLocParams> params = new ArrayList<>(); | 
|---|
|  |  |  | for (WaveItem item : items) { | 
|---|
|  |  |  | WaveToLocParams locParams = new WaveToLocParams(); | 
|---|
|  |  |  | BeanUtils.copyProperties(item, locParams); | 
|---|
|  |  |  | locParams.setBatch(item.getSplrBatch()) | 
|---|
|  |  |  | .setItemId(item.getId()) | 
|---|
|  |  |  | .setWaveId(item.getWaveId()); | 
|---|
|  |  |  | params.add(locParams); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | List<OrderOutItemDto> results = LocManageUtil.getOutOrderList(params, waveRule); | 
|---|
|  |  |  | if (results.isEmpty()) { | 
|---|
|  |  |  | Wave wave = waveService.getById(waveId); | 
|---|
|  |  |  | if (Objects.isNull(wave)) { | 
|---|
|  |  |  | throw new CoolException("<UNK>"); | 
|---|
|  |  |  | for (Wave wave : waves) { | 
|---|
|  |  |  | if (wave.getExceStatus() > WaveExceStatus.WAVE_EXCE_STATUS_EXCING.val) { | 
|---|
|  |  |  | throw new CoolException("当前状态无法下发执行!!"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | wave.setUpdateBy(loginUserId).setUpdateTime(new Date()); | 
|---|
|  |  |  | if (wave.getAnfme().compareTo(wave.getWorkQty()) == 0) { | 
|---|
|  |  |  | wave.setExceStatus(WaveExceStatus.WAVE_EXCE_STATUS_TASK.val); | 
|---|
|  |  |  | } else { | 
|---|
|  |  |  | wave.setExceStatus(WaveExceStatus.WAVE_EXCE_STATUS_EXCING.val); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | List<WaveItem> items = waveItemService.list(new LambdaQueryWrapper<WaveItem>().eq(WaveItem::getWaveId, wave.getId())); | 
|---|
|  |  |  | if (items.isEmpty()) { | 
|---|
|  |  |  | throw new CoolException("波次明细不存在!!"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | waveService.updateById(wave); | 
|---|
|  |  |  | return R.ok(); | 
|---|
|  |  |  | List<Long> ids = items.stream().map(WaveItem::getId).collect(Collectors.toList()); | 
|---|
|  |  |  | if (!waveItemService.update(new LambdaUpdateWrapper<WaveItem>() | 
|---|
|  |  |  | .set(WaveItem::getExceStatus, WaveExceStatus.WAVE_EXCE_STATUS_EXCING.val) | 
|---|
|  |  |  | .in(WaveItem::getId, ids))) { | 
|---|
|  |  |  | throw new CoolException("执行状态修改失败!!"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | WaveRule waveRule = waveRuleService.getOne(new LambdaQueryWrapper<WaveRule>() | 
|---|
|  |  |  | .eq(WaveRule::getType, WaveRuleType.First_In_First_Out.type)); | 
|---|
|  |  |  | if (Cools.isEmpty(waveRule)) { | 
|---|
|  |  |  | throw new CoolException("未找到当前策略"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | List<WaveToLocParams> params = new ArrayList<>(); | 
|---|
|  |  |  | for (WaveItem item : items) { | 
|---|
|  |  |  | WaveToLocParams locParams = new WaveToLocParams(); | 
|---|
|  |  |  | BeanUtils.copyProperties(item, locParams); | 
|---|
|  |  |  | locParams.setBatch(item.getSplrBatch()) | 
|---|
|  |  |  | .setItemId(item.getId()) | 
|---|
|  |  |  | .setWaveId(item.getWaveId()); | 
|---|
|  |  |  | params.add(locParams); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | List<OrderOutItemDto> results = LocManageUtil.getOutOrderList(params, waveRule); | 
|---|
|  |  |  | if (results.isEmpty()) { | 
|---|
|  |  |  | wave.setUpdateBy(loginUserId).setUpdateTime(new Date()); | 
|---|
|  |  |  | if (wave.getAnfme().compareTo(wave.getWorkQty()) == 0) { | 
|---|
|  |  |  | wave.setExceStatus(WaveExceStatus.WAVE_EXCE_STATUS_TASK.val); | 
|---|
|  |  |  | } else { | 
|---|
|  |  |  | wave.setExceStatus(WaveExceStatus.WAVE_EXCE_STATUS_EXCING.val); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | wave.setMemo("-->库存不足"); | 
|---|
|  |  |  | waveService.updateById(wave); | 
|---|
|  |  |  | return R.ok(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | try { | 
|---|
|  |  |  | /**生成出库任务*/ | 
|---|
|  |  |  | generateOutTask(results, loginUserId, wave); | 
|---|
|  |  |  | } catch (Exception e) { | 
|---|
|  |  |  | log.error("UNK", e); | 
|---|
|  |  |  | throw new CoolException(e.getMessage()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | List<TaskItem> taskItems = taskItemService.list(new LambdaQueryWrapper<TaskItem>() | 
|---|
|  |  |  | .in(TaskItem::getSource, ids)); | 
|---|
|  |  |  | if (Cools.isEmpty(taskItems)) { | 
|---|
|  |  |  | throw new CoolException("暂无合适库存信息!!"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | for (TaskItem item : taskItems) { | 
|---|
|  |  |  | WaveItem waveItem = waveItemService.getById(item.getSource()); | 
|---|
|  |  |  | Double workQty = Math.round((waveItem.getWorkQty() + item.getAnfme()) * 10000) / 10000.0; | 
|---|
|  |  |  | waveItem.setWorkQty(workQty); | 
|---|
|  |  |  | if (workQty.compareTo(waveItem.getAnfme()) < 0) { | 
|---|
|  |  |  | waveItem.setExceStatus(WaveExceStatus.WAVE_EXCE_STATUS_EXCING.val); | 
|---|
|  |  |  | } else { | 
|---|
|  |  |  | waveItem.setExceStatus(WaveExceStatus.WAVE_EXCE_STATUS_TASK.val); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | if (!waveItemService.update(new LambdaUpdateWrapper<WaveItem>() | 
|---|
|  |  |  | .set(WaveItem::getExceStatus, WaveExceStatus.WAVE_EXCE_STATUS_TASK.val) | 
|---|
|  |  |  | .setSql("work_qty = work_qty + " + item.getAnfme()) | 
|---|
|  |  |  | .set(WaveItem::getUpdateBy, loginUserId) | 
|---|
|  |  |  | .set(WaveItem::getUpdateTime, new Date()) | 
|---|
|  |  |  | .eq(WaveItem::getId, item.getSource()))) { | 
|---|
|  |  |  | throw new CoolException("下发执行异常,请稍候重试!"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if (!waveService.update(new LambdaUpdateWrapper<Wave>() | 
|---|
|  |  |  | .set(Wave::getExceStatus, WaveExceStatus.WAVE_EXCE_STATUS_TASK.val) | 
|---|
|  |  |  | .set(Wave::getWorkQty, taskItems.stream().mapToDouble(TaskItem::getAnfme).sum()) | 
|---|
|  |  |  | .set(Wave::getUpdateBy, loginUserId) | 
|---|
|  |  |  | .set(Wave::getMemo, null) | 
|---|
|  |  |  | .set(Wave::getUpdateTime, new Date()) | 
|---|
|  |  |  | .eq(Wave::getId, wave.getId()))) { | 
|---|
|  |  |  | throw new CoolException("波次状态修改失败!!"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | /**生成出库任务*/ | 
|---|
|  |  |  | try { | 
|---|
|  |  |  | generateOutTask(results, loginUserId, waves); | 
|---|
|  |  |  | } catch (Exception e) { | 
|---|
|  |  |  | log.error("UNK", e); | 
|---|
|  |  |  | throw new CoolException(e.getMessage()); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | return R.ok(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 终止波次下发任务 | 
|---|
|  |  |  | * | 
|---|
|  |  |  | * @param id | 
|---|
|  |  |  | * @return | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | @Transactional(rollbackFor = Exception.class) | 
|---|
|  |  |  | public R stopPubTask(Long id) { | 
|---|
|  |  |  | Wave wave = this.getById(id); | 
|---|
|  |  |  | if (Objects.isNull(wave)) { | 
|---|
|  |  |  | throw new CoolException("波次单不存在!!"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | List<TaskItem> taskItems = taskItemService.list(new LambdaQueryWrapper<TaskItem>() | 
|---|
|  |  |  | .in(TaskItem::getSource, waveItems)); | 
|---|
|  |  |  | if (Cools.isEmpty(taskItems)) { | 
|---|
|  |  |  | throw new CoolException("暂无合适库存信息!!"); | 
|---|
|  |  |  | .eq(TaskItem::getSourceId, wave.getId())); | 
|---|
|  |  |  | if (!taskItems.isEmpty()) { | 
|---|
|  |  |  | throw new CoolException("已生成任务不可终止当前波次!!"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | for (TaskItem item : taskItems) { | 
|---|
|  |  |  | WaveItem waveItem = waveItemService.getById(item.getSource()); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | Double workQty = Math.round((waveItem.getWorkQty() + item.getAnfme()) * 10000) / 10000.0; | 
|---|
|  |  |  | waveItem.setWorkQty(workQty); | 
|---|
|  |  |  | if (workQty.compareTo(waveItem.getAnfme()) < 0) { | 
|---|
|  |  |  | waveItem.setExceStatus(WaveItemExceStatus.WAVE_EXCE_STATUS_ING.val); | 
|---|
|  |  |  | } else { | 
|---|
|  |  |  | waveItem.setExceStatus(WaveItemExceStatus.WAVE_EXCE_STATUS_PUBD.val); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | List<WaveOrderRela> orderRelas = waveOrderRelaService.list(new LambdaQueryWrapper<WaveOrderRela>().eq(WaveOrderRela::getWaveId, id)); | 
|---|
|  |  |  | if (orderRelas.isEmpty()) { | 
|---|
|  |  |  | throw new CoolException("无关联明细!!"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | if (!waveItemService.update(new LambdaUpdateWrapper<WaveItem>() | 
|---|
|  |  |  | .set(WaveItem::getExceStatus, WaveItemExceStatus.WAVE_EXCE_STATUS_PUBD.val) | 
|---|
|  |  |  | .setSql("work_qty = work_qty + " + item.getAnfme()) | 
|---|
|  |  |  | .set(WaveItem::getUpdateBy, loginUserId) | 
|---|
|  |  |  | .set(WaveItem::getUpdateTime, new Date()) | 
|---|
|  |  |  | .eq(WaveItem::getId, item.getSource()))) { | 
|---|
|  |  |  | throw new CoolException("下发执行异常,请稍候重试!"); | 
|---|
|  |  |  | List<Long> orderIds = orderRelas.stream().map(WaveOrderRela::getOrderItemId).collect(Collectors.toList()); | 
|---|
|  |  |  | List<WkOrderItem> orderItems = asnOrderItemService.list(new LambdaQueryWrapper<WkOrderItem>().in(WkOrderItem::getId, orderIds)); | 
|---|
|  |  |  | orderItems.forEach(asnOrderItem -> { | 
|---|
|  |  |  | asnOrderItem.setWorkQty(0.0); | 
|---|
|  |  |  | if (!asnOrderItemService.updateById(asnOrderItem)) { | 
|---|
|  |  |  | throw new CoolException("单据明细更新失败!!"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | }); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | List<Long> orders = orderRelas.stream().map(WaveOrderRela::getOrderId).collect(Collectors.toList()); | 
|---|
|  |  |  | if (!asnOrderService.update(new LambdaUpdateWrapper<WkOrder>() | 
|---|
|  |  |  | .set(WkOrder::getWorkQty, 0.0) | 
|---|
|  |  |  | .set(WkOrder::getExceStatus, AsnExceStatus.OUT_STOCK_STATUS_TASK_INIT.val) | 
|---|
|  |  |  | .in(WkOrder::getId, orders))) { | 
|---|
|  |  |  | throw new CoolException("单据更新失败!!"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if (!waveService.update(new LambdaUpdateWrapper<Wave>() | 
|---|
|  |  |  | .set(Wave::getExceStatus, WaveExceStatus.WAVE_EXCE_STATUS_TASK.val) | 
|---|
|  |  |  | .set(Wave::getWorkQty, taskItems.stream().mapToDouble(TaskItem::getAnfme).sum()) | 
|---|
|  |  |  | .set(Wave::getUpdateBy, loginUserId) | 
|---|
|  |  |  | .set(Wave::getUpdateTime, new Date()) | 
|---|
|  |  |  | .eq(Wave::getId, waveId))) { | 
|---|
|  |  |  | throw new CoolException("波次状态修改失败!!"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | this.update(new LambdaUpdateWrapper<Wave>() | 
|---|
|  |  |  | .eq(Wave::getId, id) | 
|---|
|  |  |  | .set(Wave::getExceStatus, WaveExceStatus.WAVE_EXCE_STATUS_TASK.val)); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | return R.ok(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|