|
|
|
@ -175,7 +175,7 @@ public class AddCoodeService {
|
|
|
|
|
|
|
|
|
|
for (String code : processedCodes) {
|
|
|
|
|
UdiEntity udiEntity = FilterUdiUtils.getUdi(code);
|
|
|
|
|
if (udiEntity != null) {
|
|
|
|
|
if (udiEntity != null && StrUtil.isNotEmpty(udiEntity.getUdi())) {
|
|
|
|
|
udiEntityMap.put(code, udiEntity);
|
|
|
|
|
nameCodes.put(udiEntity.getUdi(), code);
|
|
|
|
|
} else {
|
|
|
|
@ -189,6 +189,7 @@ public class AddCoodeService {
|
|
|
|
|
|
|
|
|
|
if (udiEntityMap.isEmpty()) {
|
|
|
|
|
addCodeResult.setVailCodeResultResponses(vailCodeResultResponses);
|
|
|
|
|
addCodeResult.setOrderId(addOrderCodeRequest.getBillNo());
|
|
|
|
|
return ResultVOUtils.success(addCodeResult);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -819,60 +820,72 @@ public class AddCoodeService {
|
|
|
|
|
return Collections.emptyList();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 获取当前订单的所有明细码
|
|
|
|
|
List<IoOrderDetailCodeEntity> ioOrderDetailCodeEntities = orderDetailCodeDao.selectList(new QueryWrapper<IoOrderDetailCodeEntity>().select("id", "count", "reCount", "bindRlFk", "batchNo", "price").eq("orderIdFk", orderEntity.getBillNo()));
|
|
|
|
|
|
|
|
|
|
// 按照关联ID和批次号分组码实体
|
|
|
|
|
Map<String, List<IoCodeTempEntity>> groupedTempEntities = codeTempEntities.stream().collect(Collectors.groupingBy(entity -> entity.getRelId() + ":" + StrUtil.trimToEmpty(entity.getBatchNo())));
|
|
|
|
|
Map<String, List<IoCodeTempEntity>> groupedTempEntities = codeTempEntities.stream()
|
|
|
|
|
.collect(Collectors.groupingBy(entity -> entity.getRelId() + ":" + StrUtil.trimToEmpty(entity.getBatchNo())));
|
|
|
|
|
|
|
|
|
|
// 使用synchronized块来保护关键部分
|
|
|
|
|
synchronized (orderEntity.getBillNo().intern()) {
|
|
|
|
|
// 获取当前订单的所有明细码
|
|
|
|
|
// 处理每个分组
|
|
|
|
|
for (Map.Entry<String, List<IoCodeTempEntity>> entry : groupedTempEntities.entrySet()) {
|
|
|
|
|
List<IoCodeTempEntity> group = entry.getValue();
|
|
|
|
|
if (CollUtil.isEmpty(group)) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 处理每个分组
|
|
|
|
|
for (Map.Entry<String, List<IoCodeTempEntity>> entry : groupedTempEntities.entrySet()) {
|
|
|
|
|
List<IoCodeTempEntity> group = entry.getValue();
|
|
|
|
|
if (CollUtil.isEmpty(group)) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
IoCodeTempEntity representative = group.get(0);
|
|
|
|
|
UdiRelevanceResponse udiRelevanceResponse = udiRelevanceService.selectSupGroupById(representative.getRelId(), representative.getSupId());
|
|
|
|
|
|
|
|
|
|
// 计算该组的总数量
|
|
|
|
|
int totalCount = group.stream().mapToInt(IoCodeTempEntity::getMyCount).sum();
|
|
|
|
|
int totalReCount = group.stream().mapToInt(IoCodeTempEntity::getMyReCount).sum();
|
|
|
|
|
// 如果没有现有明细码或未找到匹配的明细码,则创建新的
|
|
|
|
|
boolean isUpdate = false;
|
|
|
|
|
IoOrderDetailCodeEntity resultDetailEntity = null;
|
|
|
|
|
if (CollUtil.isNotEmpty(ioOrderDetailCodeEntities)) {
|
|
|
|
|
for (IoOrderDetailCodeEntity orderDetailCodeEntity : ioOrderDetailCodeEntities) {
|
|
|
|
|
if (orderDetailCodeEntity.getBindRlFk().longValue() == representative.getRelId().longValue() && StrUtil.trimToEmpty(orderDetailCodeEntity.getBatchNo()).equals(StrUtil.trimToEmpty(representative.getBatchNo()))) {
|
|
|
|
|
// 更新现有明细码的数量
|
|
|
|
|
orderDetailCodeEntity.setCount(orderDetailCodeEntity.getCount() + totalCount);
|
|
|
|
|
orderDetailCodeEntity.setReCount(orderDetailCodeEntity.getReCount() + totalReCount);
|
|
|
|
|
orderDetailCodeEntity.setUpdateTime(new Date());
|
|
|
|
|
orderDetailCodeDao.updateCount(orderDetailCodeEntity);
|
|
|
|
|
isUpdate = true;
|
|
|
|
|
resultDetailEntity = orderDetailCodeEntity;
|
|
|
|
|
break;
|
|
|
|
|
IoCodeTempEntity representative = group.get(0);
|
|
|
|
|
UdiRelevanceResponse udiRelevanceResponse = udiRelevanceService.selectSupGroupById(
|
|
|
|
|
representative.getRelId(),
|
|
|
|
|
representative.getSupId());
|
|
|
|
|
|
|
|
|
|
// 计算该组的总数量
|
|
|
|
|
int totalCount = group.stream().mapToInt(IoCodeTempEntity::getMyCount).sum();
|
|
|
|
|
int totalReCount = group.stream().mapToInt(IoCodeTempEntity::getMyReCount).sum();
|
|
|
|
|
|
|
|
|
|
// 在数据库层面使用悲观锁或乐观锁查询现有记录
|
|
|
|
|
IoOrderDetailCodeEntity existingEntity = orderDetailCodeDao.selectOne(
|
|
|
|
|
new QueryWrapper<IoOrderDetailCodeEntity>()
|
|
|
|
|
.eq("orderIdFk", orderEntity.getBillNo())
|
|
|
|
|
.eq("bindRlFk", representative.getRelId())
|
|
|
|
|
.eq(StrUtil.isNotEmpty(representative.getBatchNo()), "batchNo", representative.getBatchNo())
|
|
|
|
|
.last("limit 1 FOR UPDATE")); // 添加行级锁
|
|
|
|
|
|
|
|
|
|
IoOrderDetailCodeEntity resultDetailEntity;
|
|
|
|
|
if (existingEntity != null) {
|
|
|
|
|
// 更新现有明细码的数量
|
|
|
|
|
existingEntity.setCount(existingEntity.getCount() + totalCount);
|
|
|
|
|
existingEntity.setReCount(existingEntity.getReCount() + totalReCount);
|
|
|
|
|
existingEntity.setUpdateTime(new Date());
|
|
|
|
|
orderDetailCodeDao.updateCount(existingEntity);
|
|
|
|
|
resultDetailEntity = existingEntity;
|
|
|
|
|
} else {
|
|
|
|
|
// 创建新记录
|
|
|
|
|
resultDetailEntity = buildEntity(orderEntity, representative, udiRelevanceResponse, totalCount, totalReCount);
|
|
|
|
|
orderDetailCodeDao.insert(resultDetailEntity);
|
|
|
|
|
}
|
|
|
|
|
// 批量更新价格信息
|
|
|
|
|
List<IoCodeTempEntity> updateBatch = new ArrayList<>();
|
|
|
|
|
for (IoCodeTempEntity entity : group) {
|
|
|
|
|
if (entity.getPrice() == null && udiRelevanceResponse != null && udiRelevanceResponse.getPrice() != null) {
|
|
|
|
|
entity.setPrice(udiRelevanceResponse.getPrice());
|
|
|
|
|
}
|
|
|
|
|
entity.setBizId(resultDetailEntity.getId());
|
|
|
|
|
updateBatch.add(entity);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 如果没有更新现有记录,则创建新记录
|
|
|
|
|
if (!isUpdate) {
|
|
|
|
|
resultDetailEntity = buildEntity(orderEntity, representative, udiRelevanceResponse, totalCount, totalReCount);
|
|
|
|
|
orderDetailCodeDao.insert(resultDetailEntity);
|
|
|
|
|
}
|
|
|
|
|
// 更新价格信息
|
|
|
|
|
for (IoCodeTempEntity entity : group) {
|
|
|
|
|
if (entity.getPrice() == null && udiRelevanceResponse != null && udiRelevanceResponse.getPrice() != null) {
|
|
|
|
|
entity.setPrice(udiRelevanceResponse.getPrice());
|
|
|
|
|
// 批量更新以提高性能
|
|
|
|
|
if (!updateBatch.isEmpty()) {
|
|
|
|
|
codeTempService.batchUpdate(updateBatch);
|
|
|
|
|
}
|
|
|
|
|
entity.setBizId(resultDetailEntity.getId());
|
|
|
|
|
codeTempService.updateById(entity);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 返回最新的明细码列表
|
|
|
|
|
return orderDetailCodeDao.selectList(
|
|
|
|
|
new QueryWrapper<IoOrderDetailCodeEntity>()
|
|
|
|
|
.select("id", "count", "reCount", "bindRlFk", "batchNo", "price")
|
|
|
|
|
.eq("orderIdFk", orderEntity.getBillNo()));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 返回最新的明细码列表
|
|
|
|
|
return orderDetailCodeDao.selectList(new QueryWrapper<IoOrderDetailCodeEntity>().select("id", "count", "reCount", "bindRlFk", "batchNo", "price").eq("orderIdFk", orderEntity.getBillNo()));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Integer getMaxGroupNumber() {
|
|
|
|
|