Authored by 胡古飞

Merge branch 'wn_forbidden'

package com.yoho.search.dal;
import com.yoho.search.dal.model.ForbiddenSortBrand;
import java.util.List;
public interface ForbiddenSortBrandMapper {
int deleteByPrimaryKey(Short id);
int insert(ForbiddenSortBrand record);
ForbiddenSortBrand selectByPrimaryKey(Short id);
void insertBatch(List<ForbiddenSortBrand> list);
void deleteAll();
}
\ No newline at end of file
... ...
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.yoho.search.dal.ForbiddenSortBrandMapper">
<resultMap id="BaseResultMap" type="com.yoho.search.dal.model.ForbiddenSortBrand">
<id column="id" property="id" jdbcType="INTEGER"/>
<result column="max_sort_id" property="maxSortId" jdbcType="INTEGER"/>
<result column="middle_sort_id" property="middleSortId" jdbcType="INTEGER"/>
<result column="small_sort_id" property="smallSortId" jdbcType="INTEGER"/>
<result column="brand_id" property="brandId" jdbcType="INTEGER"/>
<result column="create_time" property="createTime" jdbcType="INTEGER"/>
</resultMap>
<sql id="Base_Column_List">
id, max_sort_id, middle_sort_id, small_sort_id, brand_id, create_time
</sql>
<select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Short">
select
<include refid="Base_Column_List"/>
from forbidden_sort_brand
where id = #{id,jdbcType=INTEGER}
</select>
<delete id="deleteByPrimaryKey" parameterType="java.lang.Short">
delete from forbidden_sort_brand
where id = #{id,jdbcType=INTEGER}
</delete>
<delete id="deleteAll">
delete from forbidden_sort_brand
where 1=1
</delete>
<insert id="insert" parameterType="com.yoho.search.dal.model.ForbiddenSortBrand">
insert into forbidden_sort_brand (id, max_sort_id, middle_sort_id, small_sort_id,
brand_id, create_time)
values (#{id,jdbcType=INTEGER}, #{maxSortId,jdbcType=INTEGER}, #{middleSortId,jdbcType=INTEGER}, #{smallSortId,jdbcType=INTEGER},
#{brandId,jdbcType=INTEGER}, #{createTime,jdbcType=INTEGER})
</insert>
<insert id="insertBatch" parameterType="java.util.List" timeout="20000">
insert ignore into forbidden_sort_brand (max_sort_id, middle_sort_id, small_sort_id,brand_id, create_time)
values
<foreach collection="list" item="item" index="index"
separator=",">
(#{item.maxSortId, jdbcType=INTEGER},
#{item.middleSortId, jdbcType=INTEGER},
#{item.smallSortId, jdbcType=INTEGER},
#{item.brandId, jdbcType=INTEGER},
#{item.createTime, jdbcType=INTEGER})
</foreach>
</insert>
</mapper>
\ No newline at end of file
... ...
package com.yoho.search.consumer.service.logic;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import com.yoho.search.base.utils.FileUtils;
import com.yoho.search.consumer.service.base.BrandService;
import com.yoho.search.consumer.service.base.ProductSortService;
import com.yoho.search.dal.ForbiddenSortBrandMapper;
import com.yoho.search.dal.model.Brand;
import com.yoho.search.dal.model.ForbiddenSortBrand;
import com.yoho.search.dal.model.ProductSort;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
... ... @@ -14,165 +14,248 @@ import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.yoho.search.base.utils.FileUtils;
import com.yoho.search.consumer.service.base.BrandService;
import com.yoho.search.consumer.service.base.ProductSortService;
import com.yoho.search.dal.model.Brand;
import com.yoho.search.dal.model.ProductSort;
import java.util.*;
import java.util.stream.Collectors;
@Service
public class ForbidenSortBrandLogicService {
private static final Logger logger = LoggerFactory.getLogger(ForbidenSortBrandLogicService.class);
private static final Integer PARENTID_OF_MAXSORT = Integer.valueOf(0);
private static final String LINE_SEPARATOR = System.lineSeparator();
@Autowired
private ProductSortService productSortService;
@Autowired
private BrandService brandService;
private final String forbiddenSortBrandFileName = "inner/forbidden_brands.txt";
private Map<Integer, Map<Integer, List<Integer>>> forbiddenSortBrandMap;
private void checkAndInit() {
if (forbiddenSortBrandMap != null) {
return;
}
synchronized (this) {
if (forbiddenSortBrandMap != null) {
return;
}
forbiddenSortBrandMap = genForbiddenSortBrandMap();
}
}
public Map<Integer, Map<Integer, List<Integer>>> getForbiddenSortBrandMap() {
checkAndInit();
return forbiddenSortBrandMap;
}
public boolean isForbiddenSortBrand(Integer maxSortId, Integer middleSortId, Integer brandId) {
if (maxSortId == null || middleSortId == null || brandId == null) {
return false;
}
checkAndInit();
Map<Integer, List<Integer>> middleSortMap = forbiddenSortBrandMap.get(maxSortId);
if (middleSortMap == null) {
return false;
}
List<Integer> brandIds = middleSortMap.get(middleSortId);
if (brandIds == null) {
return false;
}
return brandIds.contains(brandId);
}
private Map<Integer, Map<Integer, List<Integer>>> genForbiddenSortBrandMap() {
//1、获取所有的品牌信息
Map<String, Integer> brandNameToIdMap = this.getBrandNameToIdMap();
//2、获取所有的品类信息
List<ProductSort> productSortList = productSortService.getPageLists(0, Integer.MAX_VALUE);
//3、获取大分类Map
Map<String,Integer> maxSortMap = new HashMap<String, Integer>();
productSortList.stream().filter(sort -> PARENTID_OF_MAXSORT.equals(sort.getParentId())).collect(Collectors.toList())
.forEach(sort -> maxSortMap.put(sort.getSortName(), sort.getId()));
//4、构造大分类和中分类的对应关系
Map<Integer,Map<String,Integer>> maxSortToMiddleSort = new HashMap<Integer, Map<String,Integer>>();
for (Integer maxSortId : maxSortMap.values()) {
Map<String,Integer> middleSortMap = new HashMap<String, Integer>();
for (ProductSort productSort : productSortList) {
if(productSort.getParentId().equals(maxSortId)){
middleSortMap.put(productSort.getSortName(), productSort.getId());
}
}
maxSortToMiddleSort.put(maxSortId, middleSortMap);
}
//5、读取文件
String filePath = this.getClass().getResource("/").getPath();
List<String> records = FileUtils.readFile(filePath + forbiddenSortBrandFileName);
if (CollectionUtils.isEmpty(records)) {
return new HashMap<Integer, Map<Integer, List<Integer>>>();
}
//6、构建数据
Map<Integer, Map<Integer, List<Integer>>> resultMap = new LinkedHashMap<>();
StringBuilder transferResult = new StringBuilder(10000);
for (int i = 1; i < records.size(); i++) {
String[] contents = records.get(i).split("\\\t", 3);
if (contents == null || contents.length != 3) {
contents = records.get(i).split(" ", 3);
if (contents == null || contents.length != 3) {
logger.warn("Process line [{}] failed.", i);
continue;
}
}
Integer maxSortId = maxSortMap.get(contents[0]);
if (maxSortId == null) {
logger.warn("Process line [{}] with maxSort [{}] failed.", i, contents[0]);
continue;
}
Integer middleSortId = maxSortToMiddleSort.get(maxSortId).get(contents[1]);
if (middleSortId == null) {
logger.warn("Process line [{}] with middleSort [{}] failed.", i, contents[1]);
continue;
}
Integer brandId = brandNameToIdMap.get(contents[2]);
if (brandId == null) {
logger.warn("Process line [{}] with brand [{}] failed.", i, contents[2]);
continue;
}
Map<Integer, List<Integer>> innerMap = resultMap.get(maxSortId);
if (innerMap == null) {
innerMap = new LinkedHashMap<>(1000);
resultMap.put(maxSortId, innerMap);
}
List<Integer> innerBrandIds = innerMap.get(middleSortId);
if (innerBrandIds == null) {
innerBrandIds = new ArrayList<>(1000);
innerMap.put(middleSortId, innerBrandIds);
}
innerBrandIds.add(brandId);
transferResult.append(maxSortId).append("-").append(middleSortId).append("-").append(brandId).append(LINE_SEPARATOR);
}
// 7、拼接数据
StringBuilder calResult = new StringBuilder(10000);
for (Map.Entry<Integer, Map<Integer, List<Integer>>> maxSortEntry : resultMap.entrySet()) {
for (Map.Entry<Integer, List<Integer>> middleSortEntry : maxSortEntry.getValue().entrySet()) {
String brandIdList = middleSortEntry.getValue().stream().map(String::valueOf).collect(Collectors.joining(","));
calResult.append(maxSortEntry.getKey()).append("|").append(middleSortEntry.getKey()).append("|").append(brandIdList).append(LINE_SEPARATOR);
}
}
logger.info("[ForbiddenBrandsController][forbidden_cal]" + LINE_SEPARATOR + calResult.toString());
FileUtils.writeFile("forbidden_cal.txt", calResult.toString());
FileUtils.writeFile("forbidden_tranfer.txt", transferResult.toString());
// 8、返回结果
return resultMap;
}
private Map<String, Integer> getBrandNameToIdMap() {
Map<String, Integer> brandNameToIdMap = new HashMap<String, Integer>();
List<Brand> brandList = brandService.getBrandPageLists(0, Integer.MAX_VALUE);
brandList.forEach(brand -> {
if (StringUtils.isNotEmpty(brand.getBrandName())) {
brandNameToIdMap.put(brand.getBrandName(), brand.getId());
}
if (StringUtils.isNotEmpty(brand.getBrandNameEn())) {
brandNameToIdMap.put(brand.getBrandNameEn(), brand.getId());
}
if (StringUtils.isNotEmpty(brand.getBrandNameCn())) {
brandNameToIdMap.put(brand.getBrandNameCn(), brand.getId());
}
});
return brandNameToIdMap;
}
private static final Logger logger = LoggerFactory.getLogger(ForbidenSortBrandLogicService.class);
private static final Integer PARENTID_OF_MAXSORT = Integer.valueOf(0);
private static final String LINE_SEPARATOR = System.lineSeparator();
@Autowired
private ProductSortService productSortService;
@Autowired
private BrandService brandService;
@Autowired
private ForbiddenSortBrandMapper forbiddenSortBrandMapper;
private final String forbiddenSortBrandFileName = "inner/forbidden_brands.txt";
private Map<Integer, Map<Integer, Map<Integer, List<Integer>>>> forbiddenSortBrandMap;
private void checkAndInit() {
if (forbiddenSortBrandMap != null) {
return;
}
synchronized (this) {
if (forbiddenSortBrandMap != null) {
return;
}
forbiddenSortBrandMap = genForbiddenSortBrandMap();
//insert to DB
List<ForbiddenSortBrand> forbiddenSortBrands = new ArrayList<>();
forbiddenSortBrandMap.forEach((maxSortId, middleSmallBrandMap) -> {
middleSmallBrandMap.forEach((middleSortId,smallBrandMap)->{
smallBrandMap.forEach((smallSortId, list) -> {
for (Integer brandId : list) {
ForbiddenSortBrand forbiddenSortBrand = new ForbiddenSortBrand();
forbiddenSortBrand.setMaxSortId(maxSortId);
forbiddenSortBrand.setMiddleSortId(middleSortId);
forbiddenSortBrand.setSmallSortId(smallSortId);
forbiddenSortBrand.setBrandId(brandId);
long time = System.currentTimeMillis() / 1000L;
forbiddenSortBrand.setCreateTime((int) time);
forbiddenSortBrands.add(forbiddenSortBrand);
}
});
});
});
forbiddenSortBrandMapper.deleteAll();
long begin = System.currentTimeMillis();
logger.info("deleteAll forbiddenSortBrand success [cost={}]", (System.currentTimeMillis() - begin));
List<List<ForbiddenSortBrand>> result = createList(forbiddenSortBrands, 1000);
logger.info("batch insert forbiddenSortBrand begin");
for (int i = 0; i < result.size(); i++) {
forbiddenSortBrandMapper.insertBatch(result.get(i));
logger.info("batch insert forbiddenSortBrand [page={}]of[{}],[cost={}]", i, result.size(), (System.currentTimeMillis() - begin));
}
logger.info("batch insert forbiddenSortBrand success,[cost={}]", (System.currentTimeMillis() - begin));
}
}
public Map<Integer, Map<Integer, Map<Integer, List<Integer>>>> getForbiddenSortBrandMap() {
checkAndInit();
return forbiddenSortBrandMap;
}
public boolean isForbiddenSortBrand(Integer maxSortId, Integer middleSortId,Integer smallSortId, Integer brandId) {
if (maxSortId == null || middleSortId == null || brandId == null) {
return false;
}
checkAndInit();
Map<Integer, Map<Integer, List<Integer>>> middleSortMap = forbiddenSortBrandMap.get(maxSortId);
if (middleSortMap == null) {
return false;
}
Map<Integer, List<Integer>> smallSortMap = middleSortMap.get(middleSortId);
if (smallSortMap == null) {
return false;
}
List<Integer> brandIds = smallSortMap.get(smallSortId);
if (brandIds == null) {
return false;
}
return brandIds.contains(brandId);
}
private Map<Integer, Map<Integer, Map<Integer, List<Integer>>>> genForbiddenSortBrandMap() {
//1、获取所有的品牌信息
Map<String, Integer> brandNameToIdMap = this.getBrandNameToIdMap();
//2、获取所有的品类信息
List<ProductSort> productSortList = productSortService.getPageLists(0, Integer.MAX_VALUE);
//3、获取大分类Map
Map<String, Integer> maxSortMap = new HashMap<String, Integer>();
productSortList.stream().filter(sort -> PARENTID_OF_MAXSORT.equals(sort.getParentId())).collect(Collectors.toList())
.forEach(sort -> maxSortMap.put(sort.getSortName(), sort.getId()));
//4、构造大分类和中分类的对应关系
Map<Integer, Map<String, Integer>> maxSortToMiddleSort = new HashMap<Integer, Map<String, Integer>>();
Map<Integer, Map<String, Integer>> middleSortToSmallSort = new HashMap<>();
for (Integer maxSortId : maxSortMap.values()) {
Map<String, Integer> middleSortMap = new HashMap<String, Integer>();
for (ProductSort productSort : productSortList) {
if (productSort.getParentId().equals(maxSortId)) {
middleSortMap.put(productSort.getSortName(), productSort.getId());
}
}
maxSortToMiddleSort.put(maxSortId, middleSortMap);
//构建中分类个小分类的对应关系
Map<String, Integer> smallSortMap = new HashMap<String, Integer>();
middleSortMap.forEach((middleSortName, middleSortId) -> {
for (ProductSort productSort : productSortList) {
if (productSort.getParentId().equals(middleSortId)) {
smallSortMap.put(productSort.getSortName(), productSort.getId());
}
}
middleSortToSmallSort.put(middleSortId, smallSortMap);
});
}
//5、读取文件
String filePath = this.getClass().getResource("/").getPath();
List<String> records = FileUtils.readFile(filePath + forbiddenSortBrandFileName);
if (CollectionUtils.isEmpty(records)) {
return new HashMap<Integer, Map<Integer, Map<Integer, List<Integer>>>>();
}
//6、构建数据
Map<Integer, Map<Integer, Map<Integer, List<Integer>>>> resultMap = new LinkedHashMap<>();
StringBuilder transferResult = new StringBuilder(10000);
for (int i = 1; i < records.size(); i++) {
String[] contents = records.get(i).split("\\\t", 4);
if (contents == null || contents.length != 4) {
contents = records.get(i).split(" ", 4);
if (contents == null || contents.length != 4) {
logger.warn("Process line [{}] failed.", i);
continue;
}
}
Integer maxSortId = maxSortMap.get(contents[0]);
if (maxSortId == null) {
logger.warn("Process line [{}] with maxSort [{}] failed.", i, contents[0]);
continue;
}
Integer middleSortId = maxSortToMiddleSort.get(maxSortId).get(contents[1]);
if (middleSortId == null) {
logger.warn("Process line [{}] with middleSort [{}] failed.", i, contents[1]);
continue;
}
Integer smallSortId = 0;
if (contents[2].equals("全部")) {
} else {
smallSortId = middleSortToSmallSort.get(middleSortId).get(contents[2]);
}
if (middleSortId == null) {
logger.warn("Process line [{}] with smallSort [{}] failed.", i, contents[2]);
continue;
}
Integer brandId = brandNameToIdMap.get(contents[3]);
if (brandId == null) {
logger.warn("Process line [{}] with brand [{}] failed.", i, contents[3]);
continue;
}
Map<Integer, Map<Integer, List<Integer>>> middleSmallBrandMap = resultMap.get(maxSortId);
if (middleSmallBrandMap == null) {
middleSmallBrandMap = new LinkedHashMap<>(1000);
resultMap.put(maxSortId, middleSmallBrandMap);
}
Map<Integer, List<Integer>> smallBrandMap = middleSmallBrandMap.get(middleSortId);
if (smallBrandMap == null) {
smallBrandMap = new LinkedHashMap<>(1000);
middleSmallBrandMap.put(middleSortId,smallBrandMap);
resultMap.put(maxSortId, middleSmallBrandMap);
}
List<Integer> innerBrandIds = smallBrandMap.get(smallSortId);
if (innerBrandIds == null) {
innerBrandIds = new ArrayList<>(1000);
smallBrandMap.put(smallSortId,innerBrandIds);
middleSmallBrandMap.put(middleSortId,smallBrandMap);
resultMap.put(maxSortId, middleSmallBrandMap);
}
innerBrandIds.add(brandId);
transferResult.append(maxSortId).append("-").append(middleSortId).append("-").append(smallSortId).append("-").append(brandId).append(LINE_SEPARATOR);
}
// 7、拼接数据
StringBuilder calResult = new StringBuilder(10000);
resultMap.forEach((maxSortId,middleSmallBrand)->{
middleSmallBrand.forEach((middleSortId,smallBrand)->{
smallBrand.forEach((smallSortId,brandIdList)->{
String brandIdListString = brandIdList.stream().map(String::valueOf).collect(Collectors.joining(","));
calResult.append(maxSortId).append("|").append(middleSortId).append("|").append(smallSortId).append("|").append(brandIdListString).append(LINE_SEPARATOR);
});
});
});
logger.info("[ForbiddenBrandsController][forbidden_cal]" + LINE_SEPARATOR + calResult.toString());
FileUtils.writeFile("forbidden_cal.txt", calResult.toString());
FileUtils.writeFile("forbidden_tranfer.txt", transferResult.toString());
// 8、返回结果
return resultMap;
}
private Map<String, Integer> getBrandNameToIdMap() {
Map<String, Integer> brandNameToIdMap = new HashMap<String, Integer>();
List<Brand> brandList = brandService.getBrandPageLists(0, Integer.MAX_VALUE);
brandList.forEach(brand -> {
if (StringUtils.isNotEmpty(brand.getBrandName())) {
brandNameToIdMap.put(brand.getBrandName(), brand.getId());
}
if (StringUtils.isNotEmpty(brand.getBrandNameEn())) {
brandNameToIdMap.put(brand.getBrandNameEn(), brand.getId());
}
if (StringUtils.isNotEmpty(brand.getBrandNameCn())) {
brandNameToIdMap.put(brand.getBrandNameCn(), brand.getId());
}
});
return brandNameToIdMap;
}
private static List<List<ForbiddenSortBrand>> createList(List<ForbiddenSortBrand> target, int size) {
List<List<ForbiddenSortBrand>> listArr = new ArrayList<List<ForbiddenSortBrand>>();
//获取被拆分的数组个数
int arrSize = target.size() % size == 0 ? target.size() / size : target.size() / size + 1;
for (int i = 0; i < arrSize; i++) {
List<ForbiddenSortBrand> sub = new ArrayList<ForbiddenSortBrand>();
//把指定索引数据放入到list中
for (int j = i * size; j <= size * (i + 1) - 1; j++) {
if (j <= target.size() - 1) {
sub.add(target.get(j));
}
}
listArr.add(sub);
}
return listArr;
}
}
... ...
... ... @@ -52,14 +52,13 @@ public class SpecialDealLogicService {
}
private int getIsForbiddenSortBrand(ProductIBO productIBO) {
boolean isForbidenSortBrand = forbidenSortBrandLogicService.isForbiddenSortBrand(productIBO.getMaxSortId(), productIBO.getMiddleSortId(), productIBO.getBrandId());
boolean isForbidenSortBrand = forbidenSortBrandLogicService.isForbiddenSortBrand(productIBO.getMaxSortId(), productIBO.getMiddleSortId(),productIBO.getSmallSortId(), productIBO.getBrandId());
return isForbidenSortBrand ? 1 : 0;
}
/**
* 全量建索引時對PI的某些字段做特殊處理
*
* @param productIndex
*/
public void specialDealProductI(ProductIBO productIBO) {
// 1、处理ageLevel
... ...
This diff could not be displayed because it is too large.