Authored by 胡古飞

fix breakSizePercent

@@ -50,42 +50,42 @@ public class ProductSizesLogicService { @@ -50,42 +50,42 @@ public class ProductSizesLogicService {
50 if (CollectionUtils.isEmpty(goodsList)) { 50 if (CollectionUtils.isEmpty(goodsList)) {
51 return new ArrayList<>(); 51 return new ArrayList<>();
52 } 52 }
53 - 53 +
54 // 获取goodsId对应得Storage列表 54 // 获取goodsId对应得Storage列表
55 List<Integer> goodsIdList = goodsList.parallelStream().map(Goods::getId).collect(Collectors.toList()); 55 List<Integer> goodsIdList = goodsList.parallelStream().map(Goods::getId).collect(Collectors.toList());
56 List<Storage> oldStorageList = storageMapper.getByGoodsIds(goodsIdList); 56 List<Storage> oldStorageList = storageMapper.getByGoodsIds(goodsIdList);
57 if (CollectionUtils.isEmpty(oldStorageList)) { 57 if (CollectionUtils.isEmpty(oldStorageList)) {
58 return new ArrayList<>(); 58 return new ArrayList<>();
59 } 59 }
60 - 60 +
61 // 过滤状态是上架的且有库存的Goods和Storage 61 // 过滤状态是上架的且有库存的Goods和Storage
62 - Map<Integer,Set<Integer>> productValidStatusSizeIds = new HashMap<Integer, Set<Integer>>();  
63 - Map<Integer,Set<Integer>> productHasStorageSizeIds = new HashMap<Integer, Set<Integer>>();  
64 - List<Storage> realEffectiveStorageList = new ArrayList<Storage>(); 62 + Map<Integer, Integer> productValidStatusSizeCount = new HashMap<Integer, Integer>();
  63 + Map<Integer, Integer> productHasStorageSizeCount = new HashMap<Integer, Integer>();
  64 + List<Storage> effectiveStorageList = new ArrayList<Storage>();
65 for (Storage storage : oldStorageList) { 65 for (Storage storage : oldStorageList) {
66 Integer productId = storage.getProductId(); 66 Integer productId = storage.getProductId();
67 Integer sizeId = storage.getGoodsDimensionId(); 67 Integer sizeId = storage.getGoodsDimensionId();
68 - //处理未下架的sku  
69 - if(!VALID_STATUS.equals(storage.getStatus())){ 68 + // 处理未下架的sku
  69 + if (!VALID_STATUS.equals(storage.getStatus())) {
70 continue; 70 continue;
71 } 71 }
72 - this.addToMap(productId, sizeId, productValidStatusSizeIds);  
73 - //处理有库存的sku  
74 - if(storage.getStorageNum() == null || storage.getStorageNum().intValue() <= 0){ 72 + this.increaseMapCount(productId, productValidStatusSizeCount);
  73 + // 处理有库存的sku
  74 + if (storage.getStorageNum() == null || storage.getStorageNum().intValue() <= 0) {
75 continue; 75 continue;
76 } 76 }
77 - this.addToMap(productId, sizeId, productHasStorageSizeIds);  
78 - realEffectiveStorageList.add(storage); 77 + this.increaseMapCount(productId, productHasStorageSizeCount);
  78 + effectiveStorageList.add(storage);
79 } 79 }
80 -  
81 - if (CollectionUtils.isEmpty(realEffectiveStorageList)) { 80 +
  81 + if (CollectionUtils.isEmpty(effectiveStorageList)) {
82 return new ArrayList<>(); 82 return new ArrayList<>();
83 } 83 }
84 84
85 // 根据上架的GoodsId筛选Storage数据 85 // 根据上架的GoodsId筛选Storage数据
86 Set<Integer> validGoodsIdSet = goodsIdList.stream().collect(Collectors.toSet()); 86 Set<Integer> validGoodsIdSet = goodsIdList.stream().collect(Collectors.toSet());
87 List<Integer> validErpSkuIdList = new ArrayList<>(); 87 List<Integer> validErpSkuIdList = new ArrayList<>();
88 - for (Storage s : realEffectiveStorageList) { 88 + for (Storage s : effectiveStorageList) {
89 if (validGoodsIdSet.contains(s.getGoodsId())) { 89 if (validGoodsIdSet.contains(s.getGoodsId())) {
90 validErpSkuIdList.add(s.getErpSkuId()); 90 validErpSkuIdList.add(s.getErpSkuId());
91 } 91 }
@@ -94,13 +94,13 @@ public class ProductSizesLogicService { @@ -94,13 +94,13 @@ public class ProductSizesLogicService {
94 if (validErpSkuIdList == null || validErpSkuIdList.isEmpty()) { 94 if (validErpSkuIdList == null || validErpSkuIdList.isEmpty()) {
95 return new ArrayList<>(); 95 return new ArrayList<>();
96 } 96 }
97 - 97 +
98 // 从数据库中获取真实库存 98 // 从数据库中获取真实库存
99 List<ProductSizes> productSizesFromDb = productSizesMapper.getProductSizes(validErpSkuIdList); 99 List<ProductSizes> productSizesFromDb = productSizesMapper.getProductSizes(validErpSkuIdList);
100 if (CollectionUtils.isEmpty(productSizesFromDb)) { 100 if (CollectionUtils.isEmpty(productSizesFromDb)) {
101 return new ArrayList<>(); 101 return new ArrayList<>();
102 } 102 }
103 - 103 +
104 // 构造返回结果对象 104 // 构造返回结果对象
105 List<ProductSizesBO> results = new ArrayList<ProductSizesBO>(); 105 List<ProductSizesBO> results = new ArrayList<ProductSizesBO>();
106 for (ProductSizes productSizes : productSizesFromDb) { 106 for (ProductSizes productSizes : productSizesFromDb) {
@@ -114,32 +114,33 @@ public class ProductSizesLogicService { @@ -114,32 +114,33 @@ public class ProductSizesLogicService {
114 } 114 }
115 List<Size> sizeList = sizeMapper.getByIds(sizeIdSet.stream().collect(Collectors.toList())); 115 List<Size> sizeList = sizeMapper.getByIds(sizeIdSet.stream().collect(Collectors.toList()));
116 Map<Integer, String> sizeNameMap = sizeList.stream().collect(Collectors.toMap(Size::getId, Size::getSizeName)); 116 Map<Integer, String> sizeNameMap = sizeList.stream().collect(Collectors.toMap(Size::getId, Size::getSizeName));
117 -  
118 - //填充其他信息 117 +
  118 + // 填充其他信息
119 for (ProductSizesBO productSizesBO : results) { 119 for (ProductSizesBO productSizesBO : results) {
120 // 填充尺码名称 120 // 填充尺码名称
121 productSizesBO.setSizeNames(this.getSizeNames(productSizesBO.getSizeIdSet(), sizeNameMap)); 121 productSizesBO.setSizeNames(this.getSizeNames(productSizesBO.getSizeIdSet(), sizeNameMap));
122 // 填充断码率 122 // 填充断码率
123 - Set<Integer> validStatusSizeIds = productValidStatusSizeIds.getOrDefault(productSizesBO.getProductId(), new HashSet<Integer>());  
124 - Set<Integer> hasStorageSizeIds = productHasStorageSizeIds.getOrDefault(productSizesBO.getProductId(), new HashSet<Integer>());  
125 - productSizesBO.setBreakSizePercent(this.getBreakSizePercent(validStatusSizeIds, hasStorageSizeIds)); 123 + Integer validStatusSizeCount = productValidStatusSizeCount.getOrDefault(productSizesBO.getProductId(), 0);
  124 + Integer hasStorageSizeCount = productHasStorageSizeCount.getOrDefault(productSizesBO.getProductId(), 0);
  125 + productSizesBO.setBreakSizePercent(this.getBreakSizePercent(validStatusSizeCount, hasStorageSizeCount));
126 } 126 }
127 return results; 127 return results;
128 } 128 }
129 -  
130 - private<K,V> void addToMap(K key,V value,Map<K,Set<V>> map){  
131 - Set<V> values = map.get(key);  
132 - if(values==null){  
133 - values = new HashSet<V>();  
134 - map.put(key, values); 129 +
  130 + private <K, V> void increaseMapCount(K key, Map<K, Integer> map) {
  131 + Integer value = map.get(key);
  132 + if (value == null) {
  133 + value = 0;
135 } 134 }
136 - values.add(value); 135 + value = value + 1;
  136 + map.put(key, value);
137 } 137 }
138 - private String getSizeNames(Set<Integer> sizeIds,Map<Integer, String> sizeNameMap){ 138 +
  139 + private String getSizeNames(Set<Integer> sizeIds, Map<Integer, String> sizeNameMap) {
139 StringBuilder results = new StringBuilder(); 140 StringBuilder results = new StringBuilder();
140 for (Integer sizeId : sizeIds) { 141 for (Integer sizeId : sizeIds) {
141 String sizeName = sizeNameMap.get(sizeId); 142 String sizeName = sizeNameMap.get(sizeId);
142 - if(sizeName==null){ 143 + if (sizeName == null) {
143 continue; 144 continue;
144 } 145 }
145 results.append(",").append(sizeName); 146 results.append(",").append(sizeName);
@@ -147,16 +148,13 @@ public class ProductSizesLogicService { @@ -147,16 +148,13 @@ public class ProductSizesLogicService {
147 return results.toString().replaceFirst(",", ""); 148 return results.toString().replaceFirst(",", "");
148 } 149 }
149 150
150 - private double getBreakSizePercent(Set<Integer> validStatusSizeIds,Set<Integer> hasStorageSizeIds) {  
151 - if (validStatusSizeIds == null || validStatusSizeIds.isEmpty()) { 151 + private double getBreakSizePercent(Integer validStatusSizeCount, Integer hasStorageSizeCount) {
  152 + if (validStatusSizeCount == null || validStatusSizeCount<=0) {
152 return 100d; 153 return 100d;
153 } 154 }
154 - if (hasStorageSizeIds == null || hasStorageSizeIds.isEmpty()) { 155 + if (hasStorageSizeCount == null || hasStorageSizeCount<=0) {
155 return 100d; 156 return 100d;
156 } 157 }
157 - int totalSizeCount = validStatusSizeIds.size();  
158 - int hasStorageSizeCount = hasStorageSizeIds.size();  
159 - return 100d * (totalSizeCount - hasStorageSizeCount) / totalSizeCount; 158 + return 100d * (validStatusSizeCount - hasStorageSizeCount) / validStatusSizeCount;
160 } 159 }
161 -  
162 } 160 }