Authored by wangnan

forbidenSortBrand 插表逻辑

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);
int insertSelective(ForbiddenSortBrand record);
ForbiddenSortBrand selectByPrimaryKey(Short id);
int updateByPrimaryKeySelective(ForbiddenSortBrand record);
int updateByPrimaryKey(ForbiddenSortBrand record);
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="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, 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,
brand_id, create_time)
values (#{id,jdbcType=INTEGER}, #{maxSortId,jdbcType=INTEGER}, #{middleSortId,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, brand_id, create_time)
values
<foreach collection="list" item="item" index="index"
separator=",">
(#{item.maxSortId, jdbcType=INTEGER},
#{item.middleSortId, jdbcType=INTEGER},
#{item.brandId, jdbcType=INTEGER},
#{item.createTime, jdbcType=INTEGER})
</foreach>
</insert>
<insert id="insertSelective" parameterType="com.yoho.search.dal.model.ForbiddenSortBrand">
insert into forbidden_sort_brand
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="id != null">
id,
</if>
<if test="maxSortId != null">
max_sort_id,
</if>
<if test="middleSortId != null">
middle_sort_id,
</if>
<if test="brandId != null">
brand_id,
</if>
<if test="createTime != null">
create_time,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="id != null">
#{id,jdbcType=INTEGER},
</if>
<if test="maxSortId != null">
#{maxSortId,jdbcType=INTEGER},
</if>
<if test="middleSortId != null">
#{middleSortId,jdbcType=INTEGER},
</if>
<if test="brandId != null">
#{brandId,jdbcType=INTEGER},
</if>
<if test="createTime != null">
#{createTime,jdbcType=INTEGER},
</if>
</trim>
</insert>
<update id="updateByPrimaryKeySelective" parameterType="com.yoho.search.dal.model.ForbiddenSortBrand">
update forbidden_sort_brand
<set>
<if test="maxSortId != null">
max_sort_id = #{maxSortId,jdbcType=INTEGER},
</if>
<if test="middleSortId != null">
middle_sort_id = #{middleSortId,jdbcType=INTEGER},
</if>
<if test="brandId != null">
brand_id = #{brandId,jdbcType=INTEGER},
</if>
<if test="createTime != null">
create_time = #{createTime,jdbcType=INTEGER},
</if>
</set>
where id = #{id,jdbcType=INTEGER}
</update>
<update id="updateByPrimaryKey" parameterType="com.yoho.search.dal.model.ForbiddenSortBrand">
update forbidden_sort_brand
set max_sort_id = #{maxSortId,jdbcType=INTEGER},
middle_sort_id = #{middleSortId,jdbcType=INTEGER},
brand_id = #{brandId,jdbcType=INTEGER},
create_time = #{createTime,jdbcType=INTEGER}
where id = #{id,jdbcType=INTEGER}
</update>
</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,11 +14,8 @@ 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 {
... ... @@ -32,6 +29,8 @@ public class ForbidenSortBrandLogicService {
private ProductSortService productSortService;
@Autowired
private BrandService brandService;
@Autowired
private ForbiddenSortBrandMapper forbiddenSortBrandMapper;
private final String forbiddenSortBrandFileName = "inner/forbidden_brands.txt";
... ... @@ -46,6 +45,31 @@ public class ForbidenSortBrandLogicService {
return;
}
forbiddenSortBrandMap = genForbiddenSortBrandMap();
//insert to DB
List<ForbiddenSortBrand> forbiddenSortBrands = new ArrayList<>();
forbiddenSortBrandMap.forEach((maxSortId, map) -> {
map.forEach((middleSortId, list) -> {
for (Integer brandId : list) {
ForbiddenSortBrand forbiddenSortBrand = new ForbiddenSortBrand();
forbiddenSortBrand.setMaxSortId(maxSortId);
forbiddenSortBrand.setMiddleSortId(middleSortId);
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));
}
}
... ... @@ -78,16 +102,16 @@ public class ForbidenSortBrandLogicService {
List<ProductSort> productSortList = productSortService.getPageLists(0, Integer.MAX_VALUE);
//3、获取大分类Map
Map<String,Integer> maxSortMap = new HashMap<String, Integer>();
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>> maxSortToMiddleSort = new HashMap<Integer, Map<String, Integer>>();
for (Integer maxSortId : maxSortMap.values()) {
Map<String,Integer> middleSortMap = new HashMap<String, Integer>();
Map<String, Integer> middleSortMap = new HashMap<String, Integer>();
for (ProductSort productSort : productSortList) {
if(productSort.getParentId().equals(maxSortId)){
if (productSort.getParentId().equals(maxSortId)) {
middleSortMap.put(productSort.getSortName(), productSort.getId());
}
}
... ... @@ -175,4 +199,20 @@ public class ForbidenSortBrandLogicService {
return brandNameToIdMap;
}
public 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;
}
}
... ...