|  |  |  | 
|---|
|  |  |  | import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper; | 
|---|
|  |  |  | import com.vincent.rsf.framework.common.R; | 
|---|
|  |  |  | import com.vincent.rsf.framework.exception.CoolException; | 
|---|
|  |  |  | import com.vincent.rsf.server.manager.entity.AsnOrder; | 
|---|
|  |  |  | import com.vincent.rsf.server.manager.entity.LocItem; | 
|---|
|  |  |  | import com.vincent.rsf.server.manager.entity.WaveItem; | 
|---|
|  |  |  | import com.vincent.rsf.server.manager.enums.AsnExceStatus; | 
|---|
|  |  |  | import com.vincent.rsf.server.manager.enums.WaveExceStatus; | 
|---|
|  |  |  | import com.vincent.rsf.server.manager.enums.*; | 
|---|
|  |  |  | import com.vincent.rsf.server.manager.entity.*; | 
|---|
|  |  |  | import com.vincent.rsf.server.manager.mapper.WaveMapper; | 
|---|
|  |  |  | import com.vincent.rsf.server.manager.entity.Wave; | 
|---|
|  |  |  | import com.vincent.rsf.server.manager.service.*; | 
|---|
|  |  |  | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; | 
|---|
|  |  |  | 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; | 
|---|
|  |  |  | 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.ArrayList; | 
|---|
|  |  |  | import java.util.List; | 
|---|
|  |  |  | import java.util.Map; | 
|---|
|  |  |  | import java.util.Objects; | 
|---|
|  |  |  | import java.util.*; | 
|---|
|  |  |  | import java.util.stream.Collectors; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Slf4j | 
|---|
|  |  |  | @Service("waveService") | 
|---|
|  |  |  | public class WaveServiceImpl extends ServiceImpl<WaveMapper, Wave> implements WaveService { | 
|---|
|  |  |  |  | 
|---|
|  |  |  | 
|---|
|  |  |  | private TaskItemService taskItemService; | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | private LocItemService locItemService; | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | private LocService locService; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * @param | 
|---|
|  |  |  | * @param loginUserId | 
|---|
|  |  |  | * @return | 
|---|
|  |  |  | * @author Ryan | 
|---|
|  |  |  | * @description 波次任务下发 | 
|---|
|  |  |  | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | @Transactional(rollbackFor = Exception.class) | 
|---|
|  |  |  | public R publicTask(Map<String, Object> map) { | 
|---|
|  |  |  | List<Long> ids = (List<Long>) map.get("ids"); | 
|---|
|  |  |  | if (Objects.isNull(ids) || ids.isEmpty()) { | 
|---|
|  |  |  | public R publicTask(Map<String, Object> map, Long loginUserId) { | 
|---|
|  |  |  | List<WaveItem> itemParams = (List<WaveItem>) map.get("waveItem"); | 
|---|
|  |  |  | if (Objects.isNull(itemParams) || itemParams.isEmpty()) { | 
|---|
|  |  |  | throw new CoolException("参数不能为空!!"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | List<Wave> waves = this.list(new LambdaQueryWrapper<Wave>().in(Wave::getId, ids)); | 
|---|
|  |  |  | if (Objects.isNull(waves) || waves.isEmpty()) { | 
|---|
|  |  |  | List<WaveItem> items = JSONArray.parseArray(JSONArray.toJSONString(itemParams), WaveItem.class); | 
|---|
|  |  |  | String waveId = map.get("wave").toString(); | 
|---|
|  |  |  | Wave waves = this.getById(Long.parseLong(waveId)); | 
|---|
|  |  |  | if (Objects.isNull(waves)) { | 
|---|
|  |  |  | throw new CoolException("波次数据不存在!!"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | List<Long> list = waves.stream().map(Wave::getId).collect(Collectors.toList()); | 
|---|
|  |  |  | List<WaveItem> waveItems = waveItemService.list(new LambdaQueryWrapper<WaveItem>().in(WaveItem::getWaveId, list)); | 
|---|
|  |  |  | List<WaveItem> waveItems = waveItemService.list(new LambdaQueryWrapper<WaveItem>().eq(WaveItem::getWaveId, waves.getId())); | 
|---|
|  |  |  | if (waveItems.isEmpty()) { | 
|---|
|  |  |  | throw new CoolException("波次明细不存在!!"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | List<Long> orderIds = waveItems.stream().map(WaveItem::getOrderId).collect(Collectors.toList()); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /**查询每条明细匹配的库位*/ | 
|---|
|  |  |  | /**生成出库任务*/ | 
|---|
|  |  |  | try { | 
|---|
|  |  |  | List<WaveItem> items = getLocs(waveItems); | 
|---|
|  |  |  | generateOutTask(items, loginUserId, waves); | 
|---|
|  |  |  | } catch (Exception e) { | 
|---|
|  |  |  | throw new CoolException("库位获取失败!!!"); | 
|---|
|  |  |  | log.error(e.getMessage()); | 
|---|
|  |  |  | throw new CoolException("出库任务生成失败!!!"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | //TODO 1. 根据波次明细生成出库任务 | 
|---|
|  |  |  | // 2. 根据物料SKU寻找符合物料库位  {1. 根据物料编码,批次,动态字段 查询符合的库位,再根据库位中物料的数量选择最适合的库位 2. 判断当前订单是全拖出库还是拣料入库} | 
|---|
|  |  |  | // 3. 修改主单、波次执行数量 | 
|---|
|  |  |  | // 4. 判断全仓出库或拣料出库 | 
|---|
|  |  |  | List<AsnOrder> orders = asnOrderService.list(new LambdaQueryWrapper<AsnOrder>().in(AsnOrder::getId, orderIds)); | 
|---|
|  |  |  | /**修改原出库单状态*/ | 
|---|
|  |  |  | if (!asnOrderService.update(new LambdaQueryWrapper<AsnOrder>() | 
|---|
|  |  |  | .eq(AsnOrder::getExceStatus, AsnExceStatus.OUT_STOCK_STATUS_TASK_WORKING.val) | 
|---|
|  |  |  | .in(AsnOrder::getId, orders))) { | 
|---|
|  |  |  | 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))) { | 
|---|
|  |  |  | throw new CoolException("出库单据状态修改失败!!"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if (!this.update(new LambdaUpdateWrapper<Wave>().set(Wave::getExceStatus, WaveExceStatus.WAVE_EXCE_STATUS_TASK).in(Wave::getId, ids))) { | 
|---|
|  |  |  | throw new CoolException("波次状态修改失败!!"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return R.ok(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * @param | 
|---|
|  |  |  | * @param loginUserId | 
|---|
|  |  |  | * @param wave | 
|---|
|  |  |  | * @return | 
|---|
|  |  |  | * @author Ryan | 
|---|
|  |  |  | * @description 生成出库任务 | 
|---|
|  |  |  | * @time 2025/4/28 14:01 | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | @Synchronized | 
|---|
|  |  |  | @Transactional(rollbackFor = Exception.class) | 
|---|
|  |  |  | public void generateOutTask(List<WaveItem> itemParams, Long loginUserId, Wave wave) throws Exception { | 
|---|
|  |  |  | List<LocItem> locItemList = new ArrayList<>(); | 
|---|
|  |  |  | for (WaveItem param : itemParams) { | 
|---|
|  |  |  | String locs = param.getStockLocs(); | 
|---|
|  |  |  | List<LocItem> locItems = JSONArray.parseArray(locs, LocItem.class); | 
|---|
|  |  |  | if (locItems.isEmpty()) { | 
|---|
|  |  |  | continue; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | List<Long> list = locItems.stream().map(LocItem::getLocId).collect(Collectors.toList()); | 
|---|
|  |  |  | /**根据供应商批次,物料码, 动态字段查询指定的物料库存信息*/ | 
|---|
|  |  |  | List<LocItem> items = locItemService.list(new LambdaQueryWrapper<LocItem>() | 
|---|
|  |  |  | .eq(LocItem::getSplrBatch, param.getSplrBatch()) | 
|---|
|  |  |  | .in(LocItem::getLocId, list) | 
|---|
|  |  |  | .eq(StringUtils.isNotBlank(param.getFieldsIndex()), LocItem::getFieldsIndex, param.getFieldsIndex()) | 
|---|
|  |  |  | .eq(LocItem::getMatnrCode, param.getMatnrCode())); | 
|---|
|  |  |  | if (items.isEmpty()) { | 
|---|
|  |  |  | throw new CoolException("库存信息有变,请取消当前波次,生新生成新的波次!!"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | /***将有货有的明细信息存放到库位信息中*/ | 
|---|
|  |  |  | for (int i = 0; i < items.size(); i++) { | 
|---|
|  |  |  | items.get(i) | 
|---|
|  |  |  | .setSourceId(param.getWaveId()) | 
|---|
|  |  |  | .setSourceCode(param.getWaveCode()) | 
|---|
|  |  |  | .setSource(param.getId()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | locItemList.addAll(items); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if (locItemList.isEmpty()) { | 
|---|
|  |  |  | throw new CoolException("没有合适库位!!"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /**拆分波次明细库位集,合并相同库位,分解任务明细*/ | 
|---|
|  |  |  | Map<Long, List<LocItem>> listMap = locItemList.stream().collect(Collectors.groupingBy(LocItem::getLocId)); | 
|---|
|  |  |  | /**根据库位汇总信息,生成任务明细**/ | 
|---|
|  |  |  | listMap.keySet().forEach(key -> { | 
|---|
|  |  |  | List<LocItem> locItems = listMap.get(key); | 
|---|
|  |  |  | LocItem item1 = locItems.stream().findFirst().get(); | 
|---|
|  |  |  | WaveItem waveItem = waveItemService.getById(item1.getSource()); | 
|---|
|  |  |  | if (null == waveItem || Objects.isNull(waveItem)) { | 
|---|
|  |  |  | throw new CoolException("数据错误:波次明细不存在!!"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | //TODO 当前任务完成后,通过定时事件判断是全盘出库,还是拣料再入库 | 
|---|
|  |  |  | Loc loc = locService.getById(key); | 
|---|
|  |  |  | Task task = new Task(); | 
|---|
|  |  |  | String ruleCode = SerialRuleUtils.generateRuleCode(SerialRuleCode.SYS_TASK_CODE, null); | 
|---|
|  |  |  | if (StringUtils.isBlank(ruleCode)) { | 
|---|
|  |  |  | throw new CoolException("编码规则错误:请检查「SYS_TASK_CODE」是否设置完成!!"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if (Objects.isNull(loc)) { | 
|---|
|  |  |  | throw new CoolException("库位不存在!!"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | List<TaskItem> items = taskItemService.list(new LambdaQueryWrapper<TaskItem>().in(TaskItem::getSourceId, wave.getId())); | 
|---|
|  |  |  | if (items.isEmpty()) { | 
|---|
|  |  |  | throw new CoolException("波次任务已生成,不能重复生成!!"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | task.setTaskCode(ruleCode) | 
|---|
|  |  |  | .setTaskType(TaskType.TASK_TYPE_OUT.type) | 
|---|
|  |  |  | .setTaskStatus(TaskStsType.GENERATE_OUT.id) | 
|---|
|  |  |  | .setResource(TaskResouceType.TASK_RESOUCE_WAVE_TYPE.val) | 
|---|
|  |  |  | .setBarcode(loc.getBarcode()) | 
|---|
|  |  |  | .setOrgLoc(loc.getCode()) | 
|---|
|  |  |  | .setCreateBy(loginUserId) | 
|---|
|  |  |  | .setUpdateBy(loginUserId) | 
|---|
|  |  |  | .setTargSite(wave.getTargSite()); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | if (!taskService.save(task)) { | 
|---|
|  |  |  | throw new CoolException("任务生成失败!!"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | List<TaskItem> taskItems = new ArrayList<>(); | 
|---|
|  |  |  | /**生成任务明细信息*/ | 
|---|
|  |  |  | for (LocItem item : locItems) { | 
|---|
|  |  |  | TaskItem taskItem = new TaskItem(); | 
|---|
|  |  |  | BeanUtils.copyProperties(item, taskItem); | 
|---|
|  |  |  | taskItem.setTaskId(task.getId()) | 
|---|
|  |  |  | .setAnfme(waveItem.getAnfme()) | 
|---|
|  |  |  | .setId(null) | 
|---|
|  |  |  | .setSourceCode(wave.getCode()) | 
|---|
|  |  |  | .setSourceId(wave.getId()) | 
|---|
|  |  |  | .setSource(item.getSource()); | 
|---|
|  |  |  | taskItems.add(taskItem); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if (!taskItemService.saveBatch(taskItems)) { | 
|---|
|  |  |  | throw new CoolException("任务明细保存失败!!"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /**修改波次执行数量*/ | 
|---|
|  |  |  | taskItems.forEach(item -> { | 
|---|
|  |  |  | boolean update = waveItemService.update(new LambdaUpdateWrapper<WaveItem>() | 
|---|
|  |  |  | .eq(WaveItem::getId, item.getSource()) | 
|---|
|  |  |  | .set(WaveItem::getWorkQty, item.getAnfme())); | 
|---|
|  |  |  | if (!update) { | 
|---|
|  |  |  | throw new CoolException("波次执行数量修改失败!!"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | }); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | List<WaveItem> waveItems = waveItemService.list(new LambdaQueryWrapper<WaveItem>().eq(WaveItem::getWaveId, wave.getId())); | 
|---|
|  |  |  | double sum = waveItems.stream().mapToDouble(WaveItem::getWorkQty).sum(); | 
|---|
|  |  |  | /**波次主单信息修改*/ | 
|---|
|  |  |  | if (!this.update(new LambdaUpdateWrapper<Wave>() | 
|---|
|  |  |  | .eq(Wave::getId, wave.getId()) | 
|---|
|  |  |  | .set(Wave::getWorkQty, sum) | 
|---|
|  |  |  | .set(Wave::getExceStatus, WaveExceStatus.WAVE_EXCE_STATUS_TASK.val))) { | 
|---|
|  |  |  | throw new CoolException("波次主单信息修改失败!!"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | }); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | 
|---|
|  |  |  | * @description 根据物料编码,批次,动态字段 查询符合的库位,再根据库位中物料的数量选择最适合的库位 | 
|---|
|  |  |  | * @time 2025/4/27 09:26 | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | private synchronized List<WaveItem> getLocs(List<WaveItem> waveItems) throws Exception { | 
|---|
|  |  |  | @Synchronized | 
|---|
|  |  |  | private  List<WaveItem> getLocs(List<WaveItem> waveItems) throws Exception { | 
|---|
|  |  |  | //TODO  根据物料编码,批次,动态字段 查询符合的库位,再根据库位中物料的数量选择最适合的库位 | 
|---|
|  |  |  | waveItems.forEach(waveItem -> { | 
|---|
|  |  |  | List<LocItem> locItems = locItemService.list(new QueryWrapper<LocItem>() | 
|---|
|  |  |  | .select("id", "loc_id", "loc_code", "order_id", "SUM(anfme) anfme", "SUM(work_qty) work_qty", "splr_batch", "fields_index", "matnr_code") | 
|---|
|  |  |  | .select("id", "loc_id", "loc_code", "order_id", "SUM(anfme) anfme", "SUM(qty) qty", "SUM(work_qty) work_qty", "splr_batch", "fields_index", "matnr_code") | 
|---|
|  |  |  | .lambda() | 
|---|
|  |  |  | .eq(LocItem::getMatnrCode, waveItem.getMatnrCode()) | 
|---|
|  |  |  | .eq(LocItem::getSplrBatch, waveItem.getSplrBatch()) | 
|---|
|  |  |  | .eq(StringUtils.isNotBlank(waveItem.getFieldsIndex()), LocItem::getFieldsIndex, waveItem.getFieldsIndex()) | 
|---|
|  |  |  | .groupBy(LocItem::getMatnrCode, LocItem::getSplrBatch, LocItem::getFieldsIndex, LocItem::getId)); | 
|---|
|  |  |  | List<Double> doubles1 = locItems.stream().map(LocItem::getAnfme).collect(Collectors.toList()); | 
|---|
|  |  |  | double[] doubles = doubles1.stream().mapToDouble(Double::doubleValue).toArray(); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | Double[] doubles = locItems.stream().map(LocItem::getAnfme).toArray(Double[]::new); | 
|---|
|  |  |  | List<Double> result = OptimalAlgorithmUtil.findBestCombination(doubles, waveItem.getAnfme()); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /**使用回溯算法计算,获取符合出库量的最简组合*/ | 
|---|
|  |  |  | List<Integer> result = OptimalAlgorithmUtil.findCombination(doubles, waveItem.getAnfme()); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | String locs = JSONArray.toJSONString(new ArrayList<>()); | 
|---|
|  |  |  | String locs = "[]"; | 
|---|
|  |  |  | if (Objects.isNull(result) || result.isEmpty()) { | 
|---|
|  |  |  | waveItem.setStockLocs(locs).setStockQty(0.0); | 
|---|
|  |  |  | } else { | 
|---|
|  |  |  | /**过滤集合中最简短的组合*/ | 
|---|
|  |  |  | List<LocItem> locsInfo = result.stream() | 
|---|
|  |  |  | .filter(i -> i >= 0 && i < locItems.size()) | 
|---|
|  |  |  | .map(locItems::get).collect(Collectors.toList()); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | if (!locItems.isEmpty()) { | 
|---|
|  |  |  | List<String> codes = locItems.stream().map(LocItem::getLocCode).collect(Collectors.toList()); | 
|---|
|  |  |  | locs = JSONArray.toJSONString(codes); | 
|---|
|  |  |  | locs = JSONArray.toJSONString(locsInfo); | 
|---|
|  |  |  | Double sumQty = locsInfo.stream().mapToDouble(LocItem::getAnfme).sum(); | 
|---|
|  |  |  | Double surQty = locsInfo.stream().mapToDouble(LocItem::getWorkQty).sum(); | 
|---|
|  |  |  | Double qty = locsInfo.stream().mapToDouble(LocItem::getQty).sum(); | 
|---|
|  |  |  | Double v = sumQty - surQty - qty; | 
|---|
|  |  |  | waveItem.setStockLocs(locs).setStockQty(v); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | waveItem.setStockLocs(locs); | 
|---|
|  |  |  | }); | 
|---|
|  |  |  | return waveItems; | 
|---|
|  |  |  | } | 
|---|