|
|
package com.yoho.search.consumer.service.logicService.ufo;
|
|
|
|
|
|
import com.yoho.search.consumer.service.bo.UfoProductIndexBO;
|
|
|
import com.yoho.search.dal.UfoStorageMapper;
|
|
|
import com.yoho.search.dal.UfoStoragePriceMapper;
|
|
|
import com.yoho.search.dal.model.UfoStorage;
|
|
|
import com.yoho.search.dal.model.UfoStoragePrice;
|
|
|
import org.apache.commons.collections.CollectionUtils;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
import java.math.BigDecimal;
|
|
|
import java.util.*;
|
|
|
import java.util.Comparator;
|
|
|
import java.util.HashMap;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
/**
|
...
|
...
|
@@ -19,36 +24,55 @@ import java.util.stream.Collectors; |
|
|
public class UfoStoragePriceFieldBuilder implements UfoIndexFieldBuilder {
|
|
|
|
|
|
@Autowired
|
|
|
private UfoStorageMapper ufoStorageMapper;
|
|
|
@Autowired
|
|
|
private UfoStoragePriceMapper ufoStoragePriceMapper;
|
|
|
|
|
|
@Override
|
|
|
public void build(List<UfoProductIndexBO> ufoProductIndexBOList, List<Integer> idList) {
|
|
|
List<UfoStorage> ufoStorageList = ufoStorageMapper.selectByProductIdList(idList);
|
|
|
Map<Integer, List<UfoStorage>> ufoStorageMap = new HashMap<>();
|
|
|
if (CollectionUtils.isNotEmpty(ufoStorageList)) {
|
|
|
ufoStorageMap = ufoStorageList.stream().collect(Collectors.groupingBy(UfoStorage::getProductId));
|
|
|
}
|
|
|
List<UfoStoragePrice> ufoStoragePriceList = ufoStoragePriceMapper.selectByProductIdList(idList);
|
|
|
Map<Integer, List<UfoStoragePrice>> ufoStoragePriceMap = new HashMap<>(ufoStoragePriceList.size());
|
|
|
Map<Integer, List<UfoStoragePrice>> ufoStoragePriceMap = new HashMap<>();
|
|
|
if (CollectionUtils.isNotEmpty(ufoStoragePriceList)) {
|
|
|
for (UfoStoragePrice ufoStoragePrice : ufoStoragePriceList) {
|
|
|
if (ufoStoragePriceMap.containsKey(ufoStoragePrice.getProductId())) {
|
|
|
List<UfoStoragePrice> storagePriceList = ufoStoragePriceMap.get(ufoStoragePrice.getProductId());
|
|
|
storagePriceList.add(ufoStoragePrice);
|
|
|
} else {
|
|
|
List<UfoStoragePrice> storagePriceList = new ArrayList<>();
|
|
|
storagePriceList.add(ufoStoragePrice);
|
|
|
ufoStoragePriceMap.put(ufoStoragePrice.getProductId(), storagePriceList);
|
|
|
}
|
|
|
}
|
|
|
ufoStoragePriceMap = ufoStoragePriceList.stream().collect(Collectors.groupingBy(UfoStoragePrice::getProductId));
|
|
|
}
|
|
|
ufoProductIndexBOList.stream().forEach(p -> {
|
|
|
List<UfoStoragePrice> storagePriceList = ufoStoragePriceMap.get(p.getId());
|
|
|
p.setPrice(-1d);
|
|
|
if (CollectionUtils.isNotEmpty(storagePriceList)) {
|
|
|
storagePriceList = storagePriceList.stream().filter(s -> s.getStatus() == 1).sorted(Comparator.comparing(UfoStoragePrice::getPrice)).collect(Collectors.toList());
|
|
|
if (CollectionUtils.isNotEmpty(storagePriceList)) {
|
|
|
BigDecimal price = storagePriceList.get(0).getPrice();
|
|
|
if (price != null) {
|
|
|
p.setPrice(price.doubleValue());
|
|
|
}
|
|
|
}
|
|
|
Map<Integer, List<UfoStoragePrice>> finalUfoStoragePriceMap = ufoStoragePriceMap;
|
|
|
Map<Integer, List<UfoStorage>> finalUfoStorageMap = ufoStorageMap;
|
|
|
for (UfoProductIndexBO ufoProductIndexBO : ufoProductIndexBOList) {
|
|
|
List<UfoStoragePrice> storagePriceList = finalUfoStoragePriceMap.get(ufoProductIndexBO.getId());
|
|
|
List<UfoStorage> storageList = finalUfoStorageMap.get(ufoProductIndexBO.getId());
|
|
|
BigDecimal suggestLowPrice = new BigDecimal(0);
|
|
|
BigDecimal suggestHighPrice = new BigDecimal(Integer.MAX_VALUE);
|
|
|
if (CollectionUtils.isNotEmpty(storageList)) {
|
|
|
UfoStorage ufoStorage = storageList.get(0);
|
|
|
suggestLowPrice = ufoStorage.getSuggestLowPrice() == null ? suggestLowPrice : ufoStorage.getSuggestLowPrice();
|
|
|
suggestHighPrice = ufoStorage.getSuggestHighPrice() == null ? suggestHighPrice : ufoStorage.getSuggestHighPrice();
|
|
|
}
|
|
|
ufoProductIndexBO.setPrice(-1d);
|
|
|
if (CollectionUtils.isEmpty(storagePriceList)) {
|
|
|
continue;
|
|
|
}
|
|
|
});
|
|
|
BigDecimal finalSuggestLowPrice = suggestLowPrice;
|
|
|
BigDecimal finalSuggestHighPrice = suggestHighPrice;
|
|
|
storagePriceList = storagePriceList.stream()
|
|
|
.filter(s -> s.getStatus() == 1)
|
|
|
.filter(s -> s.getPrice().compareTo(finalSuggestLowPrice) >= 0)
|
|
|
.filter(s -> s.getPrice().compareTo(finalSuggestHighPrice) <= 0)
|
|
|
.sorted(Comparator.comparing(UfoStoragePrice::getPrice))
|
|
|
.collect(Collectors.toList());
|
|
|
if (CollectionUtils.isEmpty(storagePriceList)) {
|
|
|
continue;
|
|
|
}
|
|
|
BigDecimal price = storagePriceList.get(0).getPrice();
|
|
|
if (price == null) {
|
|
|
continue;
|
|
|
}
|
|
|
ufoProductIndexBO.setPrice(price.doubleValue());
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
...
|
...
|
|