Authored by 周少峰

'd'

package com.yohoufo.product.service.impl;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;
import java.util.Map.Entry;
import java.util.Objects;
import java.util.UUID;
import java.util.function.BinaryOperator;
import java.util.function.Function;
import java.util.stream.Collectors;
... ... @@ -2094,28 +2085,17 @@ public class ProductServiceImpl implements ProductService {
@Override
public List<StorageCheckResp> getStorageByCodeAndColorName(List<ProductRequestBo> reqList) {
List<StorageCheckResp> result = new ArrayList<>();
LOGGER.info("method ProductServiceImpl.getStorageByCodeAndColorName in, params is {}", reqList);
// 返回结果
List<StorageCheckResp> result = new ArrayList<>();
// 请求参数中的货号
List<String> reqProductCode = new ArrayList<>();
// 系统中有的货号
List<String> resultProductCode = new ArrayList<>();
// 系统中货号对应的productIds
List<Integer> productIds = new ArrayList<>();
HashMap<Integer, String> productIdAndCode = new HashMap<>();
// 请求参数中的尺码名称
List<String> reqSizeName = new ArrayList<>();
// 系统中的尺码名称
List<String> resultSizeName = new ArrayList<>();
HashMap<Integer, String> sizeIdAndName = new HashMap<>();
List<Integer> resultSizeIds = new ArrayList<>();
if (CollectionUtils.isEmpty(reqList)) {
LOGGER.error("getStorageByCodeAndColorName 参数错误, reqList = {}", reqList);
throw new ServiceException(400, "参数错误");
}
... ... @@ -2123,72 +2103,46 @@ public class ProductServiceImpl implements ProductService {
reqList.stream().forEach(item -> {
reqProductCode.add(item.getProductCode());
reqSizeName.add(item.getSizeName());
result.add(OrikaUtils.map(item, StorageCheckResp.class));
});
LOGGER.info("int reqProductCode lengh= {}", resultProductCode.size());
// 2. 查询 productCode 对应的product_id, 根据product_id 查询storage
List<Product> products = productMapper.selectByCodes(reqProductCode);
// 货号都不存在
if (CollectionUtils.isEmpty(products)) {
for(StorageCheckResp item : result) {
setStorageCheckRespStatusInfo(item, StorageCheckEnum.PRODUCT_NOT_FOUND.getType());
}
return result;
}
products.stream().forEach(item -> {
productIds.add(item.getId());
resultProductCode.add(item.getProductCode());
productIdAndCode.put(item.getId(), item.getProductCode());
// 默认货号不存在
StorageCheckResp resultItem = OrikaUtils.map(item, StorageCheckResp.class);
setStorageCheckRespStatusInfo(resultItem, StorageCheckEnum.PRODUCT_NOT_FOUND.getType());
result.add(resultItem);
});
LOGGER.info("int resultProductCode lengh= {}", resultProductCode.size());
// 3.查询系统中的库存
List<Storage> storages = storageMapper.selectByProductIds(productIds);
// 2. 查询 productCode/product_id对应关系
List<Integer> productIds = new ArrayList<>();
Map<String, Integer> productCodeIdMap = productMapper.selectByCodes(reqProductCode).stream().map(item -> {productIds.add(item.getId()); return item;}).collect(Collectors.toMap(Product::getProductCode, Product::getId));
// 4. 查询 sizeName 对应的size_id
List<Size> sizes = sizeMapper.selectByNames(reqSizeName);
sizes.stream().forEach(item->{
resultSizeName.add(item.getSizeName());
resultSizeIds.add(item.getId());
sizeIdAndName.put(item.getId(), item.getSizeName());
});
// 3. 查询 sizeId/sizeName对应关系
Set<String> sizeNamesSet = new HashSet<>();
Map<Integer, String> sizeIdNameMap = sizeMapper.selectByNames(reqSizeName).stream().map(item -> {
sizeNamesSet.add(item.getSizeName());
return item;}).collect(Collectors.toMap(Size::getId, Size::getSizeName));
// 5. 标记检测情况
// 5.1 不存在的货号
reqProductCode.removeAll(resultProductCode);
// 5.2 不存在的尺码名称
reqSizeName.removeAll(resultSizeName);
loop: for(StorageCheckResp item : result) {
// 不存在的货号
if (reqProductCode.contains(item.getProductCode())) {
setStorageCheckRespStatusInfo(item, StorageCheckEnum.PRODUCT_NOT_FOUND.getType());
LOGGER.info("货号不存在 = {}", item);
continue;
}
// 4.查询所有productCode 的库存
Map<Integer, List<Storage>> storagesMap = storageMapper.selectByProductIds(productIds).stream().collect(Collectors.groupingBy(Storage::getProductId));
// 不存在的尺码
if (reqSizeName.contains(item.getSizeName())) {
LOGGER.info("尺码名称不存在 = {}", item);
// 5.标记查询结果
result.stream().map(item -> {
// 尺码不存在
if (!sizeNamesSet.contains(item.getSizeName())) {
setStorageCheckRespStatusInfo(item, StorageCheckEnum.SIZE_NOT_FOUND.getType());
continue;
return item;
}
for(Storage storage : storages) {
// 货号和尺码匹配
if (item.getProductCode().equals(productIdAndCode.get(storage.getProductId())) && item.getSizeName().equals(sizeIdAndName.get(storage.getSizeId()))) {
setStorageCheckRespStatusInfo(item, StorageCheckEnum.PRODUCT_SIZE_MATCH.getType());
item.setStorageId(storage.getId());
continue loop;
}
// 获取productCode 库存信息
List<Storage> storages = storagesMap.get(productCodeIdMap.get(item.getProductCode()));
if (null == storages) {
return item;
}
// sizeName 匹配
if(storages.stream().anyMatch(storage -> sizeIdNameMap.get(storage.getSizeId()).equals(item.getSizeName()))) {
setStorageCheckRespStatusInfo(item, StorageCheckEnum.PRODUCT_SIZE_MATCH.getType());
return item;
}
return item;
// 货号和尺码不匹配
setStorageCheckRespStatusInfo(item, StorageCheckEnum.PRODUCT_SIZE_NOT_MATCH.getType());
}
}).collect(Collectors.toList());
return result;
}
... ...