Authored by Gino Zhang

Merge branch 'sales_category' of git.yoho.cn:yoho-search/yoho-search-consumer into sales_category

... ... @@ -8,6 +8,8 @@ import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
... ... @@ -16,43 +18,76 @@ import com.yoho.search.dal.model.SalesCategory;
@Component
public class SalesCategoryLogicService {
private static final Logger logger = LoggerFactory.getLogger(SalesCategoryLogicService.class);
@Autowired
private SalesCategoryService salesCategoryService;
// 一级销售类目和物理小分类之间的关系
private Map<String, Set<Integer>> physicalChannelToSortIds;
private Map<Integer, Set<Integer>> physicalChannelToSortIds;
// 判断是否是有效的有货物理分类
private boolean isLegalYohoCategory(SalesCategory salesCategory) {
if (salesCategory.getState() == null || salesCategory.getState() == 0) {
return false;
}
// BLK的忽视
if (salesCategory.getAppType() == null || salesCategory.getAppType() == 1) {
return false;
}
return true;
}
public void doReload() {
Map<String, Set<Integer>> temps = new ConcurrentHashMap<String, Set<Integer>>();
List<SalesCategory> allSalesCategory = salesCategoryService.selectAll();
for (SalesCategory salesCategory : allSalesCategory) {
if (salesCategory.getState() == null || salesCategory.getState() == 0) {
continue;
}
// 获取一级销售类目
String categoryCode = salesCategory.getCategoryCode();
if (StringUtils.isBlank(categoryCode)) {
continue;
}
String[] categorys = categoryCode.split(",", 2);
if (categorys.length != 2) {
continue;
}
String physicalChannel = categorys[0];
// 获取全部物理分类
String relation_parameter = salesCategory.getRelationParameter();
if (StringUtils.isBlank(relation_parameter)) {
continue;
}
String[] smallSortIds = relation_parameter.split(",");
Set<Integer> results = temps.get(physicalChannel);
if (results == null) {
results = new HashSet<Integer>();
temps.put(physicalChannel, results);
// 1、获取所有的销售分类
List<SalesCategory> salesCategorys = salesCategoryService.selectAll();
// 2、过滤出所有有货的一级销售分类
List<Integer> yohoFirstPhysicalChannels = new ArrayList<Integer>();
for (SalesCategory salesCategory : salesCategorys) {
if (isLegalYohoCategory(salesCategory) && new Integer(0).equals(salesCategory.getParentId())) {
yohoFirstPhysicalChannels.add(salesCategory.getCategoryId());
}
for (String smallSortId : smallSortIds) {
results.add(Integer.valueOf(smallSortId));
}
// 3、获取一级销售分类对应的物理分类
Map<Integer, Set<Integer>> temps = new ConcurrentHashMap<Integer, Set<Integer>>();
for (SalesCategory salesCategory : salesCategorys) {
try {
// 1、判断销售类目是否合法
if (!isLegalYohoCategory(salesCategory)) {
continue;
}
// 2、获取一级销售类目
String categoryCode = salesCategory.getCategoryCode();
if (StringUtils.isBlank(categoryCode)) {
continue;
}
String[] categorys = categoryCode.split(",", 2);
if (categorys.length != 2) {
continue;
}
// 3、判断分割出来的一级销售类目是否合法
Integer physicalChannel = Integer.valueOf(categorys[0]);
if(!yohoFirstPhysicalChannels.contains(physicalChannel)){
continue;
}
// 获取全部物理分类
String relation_parameter = salesCategory.getRelationParameter();
if (StringUtils.isBlank(relation_parameter)) {
continue;
}
String[] smallSortIds = relation_parameter.split(",");
Set<Integer> results = temps.get(physicalChannel);
if (results == null) {
results = new HashSet<Integer>();
temps.put(physicalChannel, results);
}
for (String smallSortId : smallSortIds) {
results.add(Integer.valueOf(smallSortId));
}
} catch (Exception e) {
logger.error(e.getMessage(),e);
}
}
physicalChannelToSortIds = temps;
... ... @@ -76,15 +111,15 @@ public class SalesCategoryLogicService {
* @param smallSortId
* @return
*/
public List<String> getPhysicalChannelsBySmallSortId(Integer smallSortId) {
public List<Integer> getPhysicalChannelsBySmallSortId(Integer smallSortId) {
if (smallSortId == null) {
return new ArrayList<String>();
return new ArrayList<Integer>();
}
if (physicalChannelToSortIds == null || physicalChannelToSortIds.isEmpty()) {
doReload();
}
List<String> channels = new ArrayList<String>();
for (Map.Entry<String, Set<Integer>> entry : physicalChannelToSortIds.entrySet()) {
List<Integer> channels = new ArrayList<Integer>();
for (Map.Entry<Integer, Set<Integer>> entry : physicalChannelToSortIds.entrySet()) {
if (entry.getValue().contains(smallSortId)) {
channels.add(entry.getKey());
}
... ...
... ... @@ -81,7 +81,7 @@ public class SpecialDealLogicService {
productIBO.setIsForbiddenSortBrand(isForbiddenSortBrand);
// 4、处理物理分类对应的销售分类
List<String> physicalChannels = salesCategoryLogicService.getPhysicalChannelsBySmallSortId(productIBO.getSmallSortId());
List<Integer> physicalChannels = salesCategoryLogicService.getPhysicalChannelsBySmallSortId(productIBO.getSmallSortId());
productIBO.setPhysicalChannels(StringUtils.join(physicalChannels, ","));
}
... ...