Authored by 胡古飞

fix breakSizePercent

... ... @@ -50,42 +50,42 @@ public class ProductSizesLogicService {
if (CollectionUtils.isEmpty(goodsList)) {
return new ArrayList<>();
}
// 获取goodsId对应得Storage列表
List<Integer> goodsIdList = goodsList.parallelStream().map(Goods::getId).collect(Collectors.toList());
List<Storage> oldStorageList = storageMapper.getByGoodsIds(goodsIdList);
if (CollectionUtils.isEmpty(oldStorageList)) {
return new ArrayList<>();
}
// 过滤状态是上架的且有库存的Goods和Storage
Map<Integer,Set<Integer>> productValidStatusSizeIds = new HashMap<Integer, Set<Integer>>();
Map<Integer,Set<Integer>> productHasStorageSizeIds = new HashMap<Integer, Set<Integer>>();
List<Storage> realEffectiveStorageList = new ArrayList<Storage>();
Map<Integer, Integer> productValidStatusSizeCount = new HashMap<Integer, Integer>();
Map<Integer, Integer> productHasStorageSizeCount = new HashMap<Integer, Integer>();
List<Storage> effectiveStorageList = new ArrayList<Storage>();
for (Storage storage : oldStorageList) {
Integer productId = storage.getProductId();
Integer sizeId = storage.getGoodsDimensionId();
//处理未下架的sku
if(!VALID_STATUS.equals(storage.getStatus())){
// 处理未下架的sku
if (!VALID_STATUS.equals(storage.getStatus())) {
continue;
}
this.addToMap(productId, sizeId, productValidStatusSizeIds);
//处理有库存的sku
if(storage.getStorageNum() == null || storage.getStorageNum().intValue() <= 0){
this.increaseMapCount(productId, productValidStatusSizeCount);
// 处理有库存的sku
if (storage.getStorageNum() == null || storage.getStorageNum().intValue() <= 0) {
continue;
}
this.addToMap(productId, sizeId, productHasStorageSizeIds);
realEffectiveStorageList.add(storage);
this.increaseMapCount(productId, productHasStorageSizeCount);
effectiveStorageList.add(storage);
}
if (CollectionUtils.isEmpty(realEffectiveStorageList)) {
if (CollectionUtils.isEmpty(effectiveStorageList)) {
return new ArrayList<>();
}
// 根据上架的GoodsId筛选Storage数据
Set<Integer> validGoodsIdSet = goodsIdList.stream().collect(Collectors.toSet());
List<Integer> validErpSkuIdList = new ArrayList<>();
for (Storage s : realEffectiveStorageList) {
for (Storage s : effectiveStorageList) {
if (validGoodsIdSet.contains(s.getGoodsId())) {
validErpSkuIdList.add(s.getErpSkuId());
}
... ... @@ -94,13 +94,13 @@ public class ProductSizesLogicService {
if (validErpSkuIdList == null || validErpSkuIdList.isEmpty()) {
return new ArrayList<>();
}
// 从数据库中获取真实库存
List<ProductSizes> productSizesFromDb = productSizesMapper.getProductSizes(validErpSkuIdList);
if (CollectionUtils.isEmpty(productSizesFromDb)) {
return new ArrayList<>();
}
// 构造返回结果对象
List<ProductSizesBO> results = new ArrayList<ProductSizesBO>();
for (ProductSizes productSizes : productSizesFromDb) {
... ... @@ -114,32 +114,33 @@ public class ProductSizesLogicService {
}
List<Size> sizeList = sizeMapper.getByIds(sizeIdSet.stream().collect(Collectors.toList()));
Map<Integer, String> sizeNameMap = sizeList.stream().collect(Collectors.toMap(Size::getId, Size::getSizeName));
//填充其他信息
// 填充其他信息
for (ProductSizesBO productSizesBO : results) {
// 填充尺码名称
productSizesBO.setSizeNames(this.getSizeNames(productSizesBO.getSizeIdSet(), sizeNameMap));
// 填充断码率
Set<Integer> validStatusSizeIds = productValidStatusSizeIds.getOrDefault(productSizesBO.getProductId(), new HashSet<Integer>());
Set<Integer> hasStorageSizeIds = productHasStorageSizeIds.getOrDefault(productSizesBO.getProductId(), new HashSet<Integer>());
productSizesBO.setBreakSizePercent(this.getBreakSizePercent(validStatusSizeIds, hasStorageSizeIds));
Integer validStatusSizeCount = productValidStatusSizeCount.getOrDefault(productSizesBO.getProductId(), 0);
Integer hasStorageSizeCount = productHasStorageSizeCount.getOrDefault(productSizesBO.getProductId(), 0);
productSizesBO.setBreakSizePercent(this.getBreakSizePercent(validStatusSizeCount, hasStorageSizeCount));
}
return results;
}
private<K,V> void addToMap(K key,V value,Map<K,Set<V>> map){
Set<V> values = map.get(key);
if(values==null){
values = new HashSet<V>();
map.put(key, values);
private <K, V> void increaseMapCount(K key, Map<K, Integer> map) {
Integer value = map.get(key);
if (value == null) {
value = 0;
}
values.add(value);
value = value + 1;
map.put(key, value);
}
private String getSizeNames(Set<Integer> sizeIds,Map<Integer, String> sizeNameMap){
private String getSizeNames(Set<Integer> sizeIds, Map<Integer, String> sizeNameMap) {
StringBuilder results = new StringBuilder();
for (Integer sizeId : sizeIds) {
String sizeName = sizeNameMap.get(sizeId);
if(sizeName==null){
if (sizeName == null) {
continue;
}
results.append(",").append(sizeName);
... ... @@ -147,16 +148,13 @@ public class ProductSizesLogicService {
return results.toString().replaceFirst(",", "");
}
private double getBreakSizePercent(Set<Integer> validStatusSizeIds,Set<Integer> hasStorageSizeIds) {
if (validStatusSizeIds == null || validStatusSizeIds.isEmpty()) {
private double getBreakSizePercent(Integer validStatusSizeCount, Integer hasStorageSizeCount) {
if (validStatusSizeCount == null || validStatusSizeCount<=0) {
return 100d;
}
if (hasStorageSizeIds == null || hasStorageSizeIds.isEmpty()) {
if (hasStorageSizeCount == null || hasStorageSizeCount<=0) {
return 100d;
}
int totalSizeCount = validStatusSizeIds.size();
int hasStorageSizeCount = hasStorageSizeIds.size();
return 100d * (totalSizeCount - hasStorageSizeCount) / totalSizeCount;
return 100d * (validStatusSizeCount - hasStorageSizeCount) / validStatusSizeCount;
}
}
... ...