| | |
| | | package com.vincent.rsf.server.manager.service.impl; |
| | | |
| | | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
| | | import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper; |
| | | import com.baomidou.mybatisplus.core.toolkit.StringUtils; |
| | | import com.vincent.rsf.framework.exception.CoolException; |
| | | import com.vincent.rsf.server.manager.controller.params.ReviseLogItemParams; |
| | | import com.vincent.rsf.server.manager.entity.*; |
| | | import com.vincent.rsf.server.manager.mapper.ReviseLogItemMapper; |
| | | import com.vincent.rsf.server.manager.entity.ReviseLogItem; |
| | | import com.vincent.rsf.server.manager.service.ReviseLogItemService; |
| | | import com.vincent.rsf.server.manager.service.*; |
| | | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
| | | 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.List; |
| | | import java.util.Objects; |
| | | import java.util.stream.Collectors; |
| | | |
| | | @Service("reviseLogItemService") |
| | | public class ReviseLogItemServiceImpl extends ServiceImpl<ReviseLogItemMapper, ReviseLogItem> implements ReviseLogItemService { |
| | | |
| | | @Autowired |
| | | private ReviseLogService reviseLogService; |
| | | @Autowired |
| | | private LocItemService locItemService; |
| | | @Autowired |
| | | private LocService locService; |
| | | @Autowired |
| | | private ReviseLogItemService reviseLogItemService; |
| | | @Autowired |
| | | private LocReviseService locReviseService; |
| | | |
| | | |
| | | /** |
| | | * 保存库存调整历史单明细 |
| | | * |
| | | * @param reviseLogItem |
| | | * @return |
| | | */ |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public List<ReviseLogItem> itemSave(ReviseLogItemParams reviseLogItem, Long userId) { |
| | | ReviseLog reviseLog = reviseLogService.getById(reviseLogItem.getReviseLogId()); |
| | | if (Objects.isNull(reviseLog)) { |
| | | throw new RuntimeException("库存历史单据不存在!!"); |
| | | } |
| | | Loc loc = locService.getOne(new LambdaQueryWrapper<Loc>() |
| | | .eq(Loc::getCode, reviseLog.getLocCode())); |
| | | if (Objects.isNull(loc)) { |
| | | throw new RuntimeException("库位库存不存在!!"); |
| | | } |
| | | List<ReviseLogItem> items = reviseLogItem.getItems(); |
| | | |
| | | this.remove(new LambdaQueryWrapper<ReviseLogItem>() |
| | | .eq(ReviseLogItem::getLocId, loc.getId())); |
| | | |
| | | items.forEach(item -> { |
| | | ReviseLogItem logItem = new ReviseLogItem(); |
| | | BeanUtils.copyProperties(item, logItem); |
| | | logItem.setReviseLogId(reviseLogItem.getReviseLogId()) |
| | | .setUpdateBy(userId) |
| | | .setCreateBy(userId) |
| | | .setLocId(loc.getId()) |
| | | .setLocCode(loc.getCode()); |
| | | if (Objects.isNull(item.getAnfme())) { |
| | | logItem.setAnfme(0.0); |
| | | } |
| | | if (!this.saveOrUpdate(logItem)) { |
| | | throw new RuntimeException("库存明细调整失败"); |
| | | } |
| | | |
| | | 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()) |
| | | .setLocCode(loc.getCode()) |
| | | .setAnfme(logItem.getReviseQty()) |
| | | .setUpdateBy(userId) |
| | | .setId(null) |
| | | .setCreateBy(userId); |
| | | if (!locItemService.save(locDetl)) { |
| | | throw new CoolException("库存明细保存失败!!"); |
| | | } |
| | | } else { |
| | | one.setAnfme(logItem.getReviseQty()); |
| | | if (!locItemService.updateById(one)) { |
| | | throw new RuntimeException("库存明细修改失败!!"); |
| | | } |
| | | } |
| | | }); |
| | | |
| | | List<ReviseLog> list = reviseLogService.list(new LambdaQueryWrapper<ReviseLog>().eq(ReviseLog::getReviseId, reviseLog.getReviseId())); |
| | | if (Objects.isNull(list)) { |
| | | throw new RuntimeException("数据错误:库存调整单不存在!!"); |
| | | } |
| | | List<Long> reviseIds = list.stream().map(ReviseLog::getId).collect(Collectors.toList()); |
| | | List<ReviseLogItem> logItems = reviseLogItemService.list(new LambdaQueryWrapper<ReviseLogItem>() |
| | | .in(ReviseLogItem::getReviseLogId, reviseIds)); |
| | | |
| | | Double anfems = logItems.stream().mapToDouble(ReviseLogItem::getAnfme).sum(); |
| | | Double reviseQty = logItems.stream().mapToDouble(ReviseLogItem::getReviseQty).sum(); |
| | | |
| | | if (!locReviseService.update(new LambdaUpdateWrapper<LocRevise>() |
| | | .eq(LocRevise::getId, reviseLog.getReviseId()) |
| | | .set(LocRevise::getAnfme, anfems) |
| | | .set(LocRevise::getReviseQty, reviseQty))) { |
| | | throw new RuntimeException("库存调整单修改失败!!"); |
| | | } |
| | | return items; |
| | | } |
| | | } |