|  |  |  | 
|---|
|  |  |  | import com.vincent.rsf.server.manager.entity.*; | 
|---|
|  |  |  | import com.vincent.rsf.server.manager.enums.CommonExceStatus; | 
|---|
|  |  |  | import com.vincent.rsf.server.manager.enums.OrderType; | 
|---|
|  |  |  | import com.vincent.rsf.server.manager.enums.OrderWorkType; | 
|---|
|  |  |  | import com.vincent.rsf.server.manager.mapper.ReviseLogMapper; | 
|---|
|  |  |  | import com.vincent.rsf.server.manager.service.*; | 
|---|
|  |  |  | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; | 
|---|
|  |  |  | 
|---|
|  |  |  | import java.util.List; | 
|---|
|  |  |  | import java.util.Map; | 
|---|
|  |  |  | import java.util.Objects; | 
|---|
|  |  |  | import java.util.Set; | 
|---|
|  |  |  | import java.util.stream.Collectors; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Service("reviseLogService") | 
|---|
|  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | private LocService locService; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | private ReviseLogItemService reviseLogItemService; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 库存调整单明细添加 | 
|---|
|  |  |  | * | 
|---|
|  |  |  | * @param revise | 
|---|
|  |  |  | * @param loginUserId | 
|---|
|  |  |  | * @return | 
|---|
|  |  |  | 
|---|
|  |  |  | .setReviseId(locRevise.getId()) | 
|---|
|  |  |  | .setCreateBy(loginUserId) | 
|---|
|  |  |  | .setUpdateBy(loginUserId) | 
|---|
|  |  |  | .setReviseCode(locRevise.getCode()) ; | 
|---|
|  |  |  | .setReviseCode(locRevise.getCode()); | 
|---|
|  |  |  | if (!reviseLogService.save(reviseLog)) { | 
|---|
|  |  |  | throw new CoolException("调整单明细保存失败!!"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | List<LocItem> locItems = locItemService.list(new LambdaQueryWrapper<LocItem>() | 
|---|
|  |  |  | .eq(LocItem::getLocCode, item.getLocCode())); | 
|---|
|  |  |  | if (!locItems.isEmpty()) { | 
|---|
|  |  |  | locItems.forEach(ote -> { | 
|---|
|  |  |  | ReviseLogItem logItem = new ReviseLogItem(); | 
|---|
|  |  |  | BeanUtils.copyProperties(ote, logItem); | 
|---|
|  |  |  | logItem.setReviseLogId(reviseLog.getId()); | 
|---|
|  |  |  | if (!reviseLogItemService.save(logItem)) { | 
|---|
|  |  |  | throw new CoolException("明细保存失败!!"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | }); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | Double sum = locItems.stream().mapToDouble(LocItem::getAnfme).sum(); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | locRevise.setAnfme(Math.round((sum + locRevise.getAnfme()) * 10000) / 10000.0); | 
|---|
|  |  |  | }); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | locRevise.setExceStatus(CommonExceStatus.COMMON_EXCE_STATUS_UN_EXCE.val); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | if (!locReviseService.updateById(locRevise)) { | 
|---|
|  |  |  | 
|---|
|  |  |  | if (!revise.getExceStatus().equals(CommonExceStatus.COMMON_EXCE_STATUS_EXCE_ING.val)) { | 
|---|
|  |  |  | throw new CoolException("单据状态未执行或已完成,无法执行完成操作!!"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | ReviseLog logs = reviseLogService.getOne(new LambdaQueryWrapper<ReviseLog>().eq(ReviseLog::getReviseId, revise.getId())); | 
|---|
|  |  |  | if (Objects.isNull(logs)) { | 
|---|
|  |  |  | List<ReviseLog> logs = reviseLogService.list(new LambdaQueryWrapper<ReviseLog>().eq(ReviseLog::getReviseId, revise.getId())); | 
|---|
|  |  |  | if (logs.isEmpty()) { | 
|---|
|  |  |  | throw new CoolException("库存日志不存在!!"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | List<ReviseLogItem> logItems = reviseLogItemService.list(new LambdaQueryWrapper<ReviseLogItem>().eq(ReviseLogItem::getReviseLogId, logs.getId())); | 
|---|
|  |  |  | Set<Long> longs = logs.stream().map(ReviseLog::getId).collect(Collectors.toSet()); | 
|---|
|  |  |  | List<ReviseLogItem> logItems = reviseLogItemService.list(new LambdaQueryWrapper<ReviseLogItem>().in(ReviseLogItem::getReviseLogId, longs)); | 
|---|
|  |  |  | if (logItems.isEmpty()) { | 
|---|
|  |  |  | throw new CoolException("调整明细为空!!"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | Map<Long, List<ReviseLogItem>> listMap = logItems.stream().collect(Collectors.groupingBy(ReviseLogItem::getLocId)); | 
|---|
|  |  |  | listMap.keySet().forEach(items -> { | 
|---|
|  |  |  | Loc loc = locService.getById(items); | 
|---|
|  |  |  | if (Objects.isNull(loc)) { | 
|---|
|  |  |  | throw new CoolException("库位不存在!!"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | List<LocItem> list = locItemService.list(new LambdaQueryWrapper<LocItem>().eq(LocItem::getLocId, loc.getId())); | 
|---|
|  |  |  | if (!list.isEmpty()) { | 
|---|
|  |  |  | throw new CoolException("库位:" + loc.getCode() + ", 调整信息为空!!"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | locItemService.remove(new LambdaQueryWrapper<LocItem>().eq(LocItem::getLocId, loc.getId())); | 
|---|
|  |  |  | listMap.get(items).forEach(logItem -> { | 
|---|
|  |  |  | LocItem one = locItemService.getOne(new LambdaQueryWrapper<LocItem>() | 
|---|
|  |  |  | .eq(LocItem::getMatnrId, logItem.getMatnrId()) | 
|---|
|  |  |  | .eq(StringUtils.isNotBlank(logItem.getBatch()), LocItem::getBatch, logItem.getBatch()) | 
|---|
|  |  |  | .eq(StringUtils.isNotBlank(logItem.getFieldsIndex()), LocItem::getFieldsIndex, logItem.getFieldsIndex()) | 
|---|
|  |  |  | .eq(LocItem::getLocCode, logItem.getLocCode())); | 
|---|
|  |  |  | if (Objects.isNull(one)) { | 
|---|
|  |  |  | LocItem locDetl = new LocItem(); | 
|---|
|  |  |  | BeanUtils.copyProperties(logItem, locDetl); | 
|---|
|  |  |  | locDetl.setLocId(loc.getId()) | 
|---|
|  |  |  | .setType(OrderType.ORDER_REVISE.type) | 
|---|
|  |  |  | .setLocCode(loc.getCode()) | 
|---|
|  |  |  | .setAnfme(logItem.getReviseQty()) | 
|---|
|  |  |  | .setUpdateBy(loginUserId) | 
|---|
|  |  |  | .setId(null) | 
|---|
|  |  |  | .setCreateBy(loginUserId); | 
|---|
|  |  |  | if (!locItemService.save(locDetl)) { | 
|---|
|  |  |  | throw new CoolException("库存明细保存失败!!"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } else { | 
|---|
|  |  |  | one.setAnfme(logItem.getReviseQty()); | 
|---|
|  |  |  | if (!locItemService.updateById(one)) { | 
|---|
|  |  |  | throw new CoolException("库存明细修改失败!!"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | LocItem locDetl = new LocItem(); | 
|---|
|  |  |  | BeanUtils.copyProperties(logItem, locDetl); | 
|---|
|  |  |  | locDetl.setLocId(loc.getId()) | 
|---|
|  |  |  | .setType(OrderType.ORDER_REVISE.type) | 
|---|
|  |  |  | .setWkType(Short.parseShort(OrderWorkType.ORDER_WORK_TYPE_STOCK_REVISE.type)) | 
|---|
|  |  |  | .setLocCode(loc.getCode()) | 
|---|
|  |  |  | .setAnfme(logItem.getReviseQty()) | 
|---|
|  |  |  | .setUpdateBy(loginUserId) | 
|---|
|  |  |  | .setId(null) | 
|---|
|  |  |  | .setCreateBy(loginUserId); | 
|---|
|  |  |  | if (!locItemService.save(locDetl)) { | 
|---|
|  |  |  | throw new CoolException("库存明细保存失败!!"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | }); | 
|---|
|  |  |  | }); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | revise.setExceStatus(CommonExceStatus.COMMON_EXCE_STATUS_TASK_DONE.val); | 
|---|
|  |  |  | if (!locReviseService.updateById(revise)) { | 
|---|
|  |  |  | throw new CoolException("调整单修改失败!!"); | 
|---|