diff --git a/dl-module-repair/src/main/java/cn/iocoder/yudao/module/tickets/service/impl/DlTwItemServiceImpl.java b/dl-module-repair/src/main/java/cn/iocoder/yudao/module/tickets/service/impl/DlTwItemServiceImpl.java index ca30ae54..2fcf23b8 100644 --- a/dl-module-repair/src/main/java/cn/iocoder/yudao/module/tickets/service/impl/DlTwItemServiceImpl.java +++ b/dl-module-repair/src/main/java/cn/iocoder/yudao/module/tickets/service/impl/DlTwItemServiceImpl.java @@ -14,6 +14,7 @@ import cn.iocoder.yudao.module.tickets.vo.AddTwiVO; import cn.iocoder.yudao.module.tickets.vo.AppWaresGroupVO; import cn.iocoder.yudao.module.tickets.vo.DlTwItemReqVO; import cn.iocoder.yudao.module.tickets.vo.DlTwItemRespVO; +import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import org.apache.commons.lang3.StringUtils; import org.springframework.context.annotation.Lazy; @@ -104,19 +105,61 @@ public class DlTwItemServiceImpl extends ServiceImpl if (CollectionUtil.isEmpty(addTwiVO.getItems())){ throw exception0(500, "请选择配件"); } - List twItems = addTwiVO.getItems().stream().map(item -> { - DlTwItem twItem = new DlTwItem(); - twItem.setTwId(addTwiVO.getId()); - twItem.setWaresId(item.getId()); - twItem.setWaresName(item.getName()); - twItem.setWaresCount(item.getCount()); - twItem.setWaresAlreadyCount(0); - twItem.setWaresStatus(""); - twItem.setRemark(item.getRemark()); - return twItem; - }).collect(Collectors.toList()); - // 新增 - baseMapper.insert(twItems); + // 查出该配件申请单已有的所有配件,这里只取没有审核过(通过、驳回的不要,同一个配件如果已经审核过了又添加,还是算新的) + List oldData = baseMapper.selectList(new LambdaQueryWrapper().and(i -> { + i.in(DlTwItem::getTwId, addTwiVO.getId()) + .eq(DlTwItem::getWaresStatus, ""); + })); + if (CollectionUtil.isNotEmpty(oldData)){ + // 取配件ID + List ids = oldData.stream().map(DlTwItem::getWaresId).collect(Collectors.toList()); + // 过滤添加的 + List add = addTwiVO.getItems().stream().filter(item -> !ids.contains(item.getId())).collect(Collectors.toList()); + // 过滤存在的 + List update = addTwiVO.getItems().stream().filter(item -> ids.contains(item.getId())).collect(Collectors.toList()); + // 添加 + if (CollectionUtil.isNotEmpty(add)){ + List twItems = add.stream().map(item -> { + DlTwItem twItem = new DlTwItem(); + twItem.setTwId(addTwiVO.getId()); + twItem.setWaresId(item.getId()); + twItem.setWaresName(item.getName()); + twItem.setWaresCount(item.getCount()); + twItem.setWaresAlreadyCount(0); + twItem.setWaresStatus(""); + twItem.setRemark(item.getRemark()); + return twItem; + }).collect(Collectors.toList()); + // 新增 + baseMapper.insert(twItems); + } + // 更新 + if (CollectionUtil.isNotEmpty(update)){ + List twItems = update.stream().map(item -> { + DlTwItem twItem = oldData.stream().filter(i -> i.getWaresId().equals(item.getId())).findFirst().orElse(null); + // 只更新数量,其他不变 + if (twItem != null) { + twItem.setWaresCount(twItem.getWaresCount() + item.getCount()); + } + return twItem; + }).collect(Collectors.toList()); + baseMapper.updateById(twItems); + } + }else { + List twItems = addTwiVO.getItems().stream().map(item -> { + DlTwItem twItem = new DlTwItem(); + twItem.setTwId(addTwiVO.getId()); + twItem.setWaresId(item.getId()); + twItem.setWaresName(item.getName()); + twItem.setWaresCount(item.getCount()); + twItem.setWaresAlreadyCount(0); + twItem.setWaresStatus(""); + twItem.setRemark(item.getRemark()); + return twItem; + }).collect(Collectors.toList()); + // 新增 + baseMapper.insert(twItems); + } } /**