Authored by gemingdan

Merge branch 'ge_attributeNames' into 0310

package com.yoho.search.dal;
import java.util.List;
import java.util.Set;
import org.apache.ibatis.annotations.Param;
import com.yoho.search.dal.model.ProductAttribute;
public interface ProductAttributeMapper {
int deleteByPrimaryKey(Integer attributeId);
int insert(ProductAttribute record);
int insertSelective(ProductAttribute record);
ProductAttribute selectByPrimaryKey(Integer attributeId);
int updateByPrimaryKeySelective(ProductAttribute record);
int updateByPrimaryKey(ProductAttribute record);
List<ProductAttribute> selectByAttributeIds(@Param(value="ids") Set<Integer> attributeIds);
}
\ No newline at end of file
... ...
package com.yoho.search.dal;
import java.util.List;
import org.apache.ibatis.annotations.Param;
import com.yoho.search.dal.model.ProductAttributePropertyValues;
public interface ProductAttributePropertyValuesMapper {
int deleteByPrimaryKey(Integer id);
int insert(ProductAttributePropertyValues record);
int insertSelective(ProductAttributePropertyValues record);
ProductAttributePropertyValues selectByPrimaryKey(Integer id);
int updateByPrimaryKeySelective(ProductAttributePropertyValues record);
int updateByPrimaryKey(ProductAttributePropertyValues record);
List<ProductAttributePropertyValues> selectBySkns(@Param(value="skns")List<Integer> skns);
}
\ No newline at end of file
... ...
package com.yoho.search.dal.model;
import com.yoho.search.base.utils.EnumUtil;
public class ProductAttribute {
private Integer attributeId;
private String attributeName;
private Integer categoryId;
private Integer saleType;
private String inputType;
private Byte attributeType;
private String isMust;
private String isSearch;
private Short maxValueLen;
private String isColor;
private String isAllowAlias;
private Integer orderBy;
private Byte state;
private String remark;
private String attributeValues;
private Byte belong;
private Integer createTime;
private Integer displayPosition;
public Integer getAttributeId() {
return attributeId;
}
public void setAttributeId(Integer attributeId) {
this.attributeId = attributeId;
}
public String getAttributeName() {
return attributeName;
}
public void setAttributeName(String attributeName) {
this.attributeName = attributeName == null ? null : attributeName.trim();
}
public Integer getCategoryId() {
return categoryId;
}
public void setCategoryId(Integer categoryId) {
this.categoryId = categoryId;
}
public Integer getSaleType() {
return saleType;
}
public void setSaleType(Integer saleType) {
this.saleType = saleType;
}
public String getInputType() {
return EnumUtil.filterEmptyStr(inputType);
}
public void setInputType(String inputType) {
this.inputType = inputType == null ? null : inputType.trim();
}
public Byte getAttributeType() {
return attributeType;
}
public void setAttributeType(Byte attributeType) {
this.attributeType = attributeType;
}
public String getIsMust() {
return EnumUtil.filterEmptyStr(isMust);
}
public void setIsMust(String isMust) {
this.isMust = isMust == null ? null : isMust.trim();
}
public String getIsSearch() {
return EnumUtil.filterEmptyStr(isSearch);
}
public void setIsSearch(String isSearch) {
this.isSearch = isSearch == null ? null : isSearch.trim();
}
public Short getMaxValueLen() {
return maxValueLen;
}
public void setMaxValueLen(Short maxValueLen) {
this.maxValueLen = maxValueLen;
}
public String getIsColor() {
return EnumUtil.filterEmptyStr(isColor);
}
public void setIsColor(String isColor) {
this.isColor = isColor == null ? null : isColor.trim();
}
public String getIsAllowAlias() {
return EnumUtil.filterEmptyStr(isAllowAlias);
}
public void setIsAllowAlias(String isAllowAlias) {
this.isAllowAlias = isAllowAlias == null ? null : isAllowAlias.trim();
}
public Integer getOrderBy() {
return orderBy;
}
public void setOrderBy(Integer orderBy) {
this.orderBy = orderBy;
}
public Byte getState() {
return state;
}
public void setState(Byte state) {
this.state = state;
}
public String getRemark() {
return remark;
}
public void setRemark(String remark) {
this.remark = remark == null ? null : remark.trim();
}
public String getAttributeValues() {
return attributeValues;
}
public void setAttributeValues(String attributeValues) {
this.attributeValues = attributeValues == null ? null : attributeValues.trim();
}
public Byte getBelong() {
return belong;
}
public void setBelong(Byte belong) {
this.belong = belong;
}
public Integer getCreateTime() {
return createTime;
}
public void setCreateTime(Integer createTime) {
this.createTime = createTime;
}
public Integer getDisplayPosition() {
return displayPosition;
}
public void setDisplayPosition(Integer displayPosition) {
this.displayPosition = displayPosition;
}
}
\ No newline at end of file
... ...
package com.yoho.search.dal.model;
public class ProductAttributePropertyValues {
private Integer id;
private Integer productSkn;
private Integer attributeId;
private String attributeValueId;
private Integer displayPosition;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getProductSkn() {
return productSkn;
}
public void setProductSkn(Integer productSkn) {
this.productSkn = productSkn;
}
public Integer getAttributeId() {
return attributeId;
}
public void setAttributeId(Integer attributeId) {
this.attributeId = attributeId;
}
public String getAttributeValueId() {
return attributeValueId;
}
public void setAttributeValueId(String attributeValueId) {
this.attributeValueId = attributeValueId == null ? null : attributeValueId.trim();
}
public Integer getDisplayPosition() {
return displayPosition;
}
public void setDisplayPosition(Integer displayPosition) {
this.displayPosition = displayPosition;
}
}
\ 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.ProductAttributeMapper" >
<resultMap id="BaseResultMap" type="com.yoho.search.dal.model.ProductAttribute" >
<id column="attribute_id" property="attributeId" jdbcType="INTEGER" />
<result column="attribute_name" property="attributeName" jdbcType="VARCHAR" />
<result column="category_id" property="categoryId" jdbcType="INTEGER" />
<result column="sale_type" property="saleType" jdbcType="INTEGER" />
<result column="input_type" property="inputType" jdbcType="CHAR" />
<result column="attribute_type" property="attributeType" jdbcType="TINYINT" />
<result column="is_must" property="isMust" jdbcType="CHAR" />
<result column="is_search" property="isSearch" jdbcType="CHAR" />
<result column="max_value_len" property="maxValueLen" jdbcType="SMALLINT" />
<result column="is_color" property="isColor" jdbcType="CHAR" />
<result column="is_allow_alias" property="isAllowAlias" jdbcType="CHAR" />
<result column="order_by" property="orderBy" jdbcType="INTEGER" />
<result column="state" property="state" jdbcType="TINYINT" />
<result column="remark" property="remark" jdbcType="VARCHAR" />
<result column="attribute_values" property="attributeValues" jdbcType="VARCHAR" />
<result column="belong" property="belong" jdbcType="TINYINT" />
<result column="create_time" property="createTime" jdbcType="INTEGER" />
<result column="display_position" property="displayPosition" jdbcType="INTEGER" />
</resultMap>
<sql id="Base_Column_List" >
attribute_id, attribute_name, category_id, sale_type, input_type, attribute_type,
is_must, is_search, max_value_len, is_color, is_allow_alias, order_by, state, remark,
attribute_values, belong, create_time, display_position
</sql>
<select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
select
<include refid="Base_Column_List" />
from product_attribute
where attribute_id = #{attributeId,jdbcType=INTEGER}
</select>
<select id="selectByAttributeIds" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from product_attribute
where attribute_id in
<foreach item="item" index="index" collection="ids"
open="(" separator="," close=")">
#{item}
</foreach>
</select>
<delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
delete from product_attribute
where attribute_id = #{attributeId,jdbcType=INTEGER}
</delete>
<insert id="insert" parameterType="com.yoho.search.dal.model.ProductAttribute" >
insert into product_attribute (attribute_id, attribute_name, category_id,
sale_type, input_type, attribute_type,
is_must, is_search, max_value_len,
is_color, is_allow_alias, order_by,
state, remark, attribute_values,
belong, create_time, display_position
)
values (#{attributeId,jdbcType=INTEGER}, #{attributeName,jdbcType=VARCHAR}, #{categoryId,jdbcType=INTEGER},
#{saleType,jdbcType=INTEGER}, #{inputType,jdbcType=CHAR}, #{attributeType,jdbcType=TINYINT},
#{isMust,jdbcType=CHAR}, #{isSearch,jdbcType=CHAR}, #{maxValueLen,jdbcType=SMALLINT},
#{isColor,jdbcType=CHAR}, #{isAllowAlias,jdbcType=CHAR}, #{orderBy,jdbcType=INTEGER},
#{state,jdbcType=TINYINT}, #{remark,jdbcType=VARCHAR}, #{attributeValues,jdbcType=VARCHAR},
#{belong,jdbcType=TINYINT}, #{createTime,jdbcType=INTEGER}, #{displayPosition,jdbcType=INTEGER}
)
</insert>
<insert id="insertSelective" parameterType="com.yoho.search.dal.model.ProductAttribute" >
insert into product_attribute
<trim prefix="(" suffix=")" suffixOverrides="," >
<if test="attributeId != null" >
attribute_id,
</if>
<if test="attributeName != null" >
attribute_name,
</if>
<if test="categoryId != null" >
category_id,
</if>
<if test="saleType != null" >
sale_type,
</if>
<if test="inputType != null" >
input_type,
</if>
<if test="attributeType != null" >
attribute_type,
</if>
<if test="isMust != null" >
is_must,
</if>
<if test="isSearch != null" >
is_search,
</if>
<if test="maxValueLen != null" >
max_value_len,
</if>
<if test="isColor != null" >
is_color,
</if>
<if test="isAllowAlias != null" >
is_allow_alias,
</if>
<if test="orderBy != null" >
order_by,
</if>
<if test="state != null" >
state,
</if>
<if test="remark != null" >
remark,
</if>
<if test="attributeValues != null" >
attribute_values,
</if>
<if test="belong != null" >
belong,
</if>
<if test="createTime != null" >
create_time,
</if>
<if test="displayPosition != null" >
display_position,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides="," >
<if test="attributeId != null" >
#{attributeId,jdbcType=INTEGER},
</if>
<if test="attributeName != null" >
#{attributeName,jdbcType=VARCHAR},
</if>
<if test="categoryId != null" >
#{categoryId,jdbcType=INTEGER},
</if>
<if test="saleType != null" >
#{saleType,jdbcType=INTEGER},
</if>
<if test="inputType != null" >
#{inputType,jdbcType=CHAR},
</if>
<if test="attributeType != null" >
#{attributeType,jdbcType=TINYINT},
</if>
<if test="isMust != null" >
#{isMust,jdbcType=CHAR},
</if>
<if test="isSearch != null" >
#{isSearch,jdbcType=CHAR},
</if>
<if test="maxValueLen != null" >
#{maxValueLen,jdbcType=SMALLINT},
</if>
<if test="isColor != null" >
#{isColor,jdbcType=CHAR},
</if>
<if test="isAllowAlias != null" >
#{isAllowAlias,jdbcType=CHAR},
</if>
<if test="orderBy != null" >
#{orderBy,jdbcType=INTEGER},
</if>
<if test="state != null" >
#{state,jdbcType=TINYINT},
</if>
<if test="remark != null" >
#{remark,jdbcType=VARCHAR},
</if>
<if test="attributeValues != null" >
#{attributeValues,jdbcType=VARCHAR},
</if>
<if test="belong != null" >
#{belong,jdbcType=TINYINT},
</if>
<if test="createTime != null" >
#{createTime,jdbcType=INTEGER},
</if>
<if test="displayPosition != null" >
#{displayPosition,jdbcType=INTEGER},
</if>
</trim>
</insert>
<update id="updateByPrimaryKeySelective" parameterType="com.yoho.search.dal.model.ProductAttribute" >
update product_attribute
<set >
<if test="attributeName != null" >
attribute_name = #{attributeName,jdbcType=VARCHAR},
</if>
<if test="categoryId != null" >
category_id = #{categoryId,jdbcType=INTEGER},
</if>
<if test="saleType != null" >
sale_type = #{saleType,jdbcType=INTEGER},
</if>
<if test="inputType != null" >
input_type = #{inputType,jdbcType=CHAR},
</if>
<if test="attributeType != null" >
attribute_type = #{attributeType,jdbcType=TINYINT},
</if>
<if test="isMust != null" >
is_must = #{isMust,jdbcType=CHAR},
</if>
<if test="isSearch != null" >
is_search = #{isSearch,jdbcType=CHAR},
</if>
<if test="maxValueLen != null" >
max_value_len = #{maxValueLen,jdbcType=SMALLINT},
</if>
<if test="isColor != null" >
is_color = #{isColor,jdbcType=CHAR},
</if>
<if test="isAllowAlias != null" >
is_allow_alias = #{isAllowAlias,jdbcType=CHAR},
</if>
<if test="orderBy != null" >
order_by = #{orderBy,jdbcType=INTEGER},
</if>
<if test="state != null" >
state = #{state,jdbcType=TINYINT},
</if>
<if test="remark != null" >
remark = #{remark,jdbcType=VARCHAR},
</if>
<if test="attributeValues != null" >
attribute_values = #{attributeValues,jdbcType=VARCHAR},
</if>
<if test="belong != null" >
belong = #{belong,jdbcType=TINYINT},
</if>
<if test="createTime != null" >
create_time = #{createTime,jdbcType=INTEGER},
</if>
<if test="displayPosition != null" >
display_position = #{displayPosition,jdbcType=INTEGER},
</if>
</set>
where attribute_id = #{attributeId,jdbcType=INTEGER}
</update>
<update id="updateByPrimaryKey" parameterType="com.yoho.search.dal.model.ProductAttribute" >
update product_attribute
set attribute_name = #{attributeName,jdbcType=VARCHAR},
category_id = #{categoryId,jdbcType=INTEGER},
sale_type = #{saleType,jdbcType=INTEGER},
input_type = #{inputType,jdbcType=CHAR},
attribute_type = #{attributeType,jdbcType=TINYINT},
is_must = #{isMust,jdbcType=CHAR},
is_search = #{isSearch,jdbcType=CHAR},
max_value_len = #{maxValueLen,jdbcType=SMALLINT},
is_color = #{isColor,jdbcType=CHAR},
is_allow_alias = #{isAllowAlias,jdbcType=CHAR},
order_by = #{orderBy,jdbcType=INTEGER},
state = #{state,jdbcType=TINYINT},
remark = #{remark,jdbcType=VARCHAR},
attribute_values = #{attributeValues,jdbcType=VARCHAR},
belong = #{belong,jdbcType=TINYINT},
create_time = #{createTime,jdbcType=INTEGER},
display_position = #{displayPosition,jdbcType=INTEGER}
where attribute_id = #{attributeId,jdbcType=INTEGER}
</update>
</mapper>
\ 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.ProductAttributePropertyValuesMapper" >
<resultMap id="BaseResultMap" type="com.yoho.search.dal.model.ProductAttributePropertyValues" >
<id column="id" property="id" jdbcType="INTEGER" />
<result column="product_skn" property="productSkn" jdbcType="INTEGER" />
<result column="attribute_id" property="attributeId" jdbcType="INTEGER" />
<result column="attribute_value_id" property="attributeValueId" jdbcType="VARCHAR" />
<result column="display_position" property="displayPosition" jdbcType="INTEGER" />
</resultMap>
<sql id="Base_Column_List" >
id, product_skn, attribute_id, attribute_value_id, display_position
</sql>
<select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
select
<include refid="Base_Column_List" />
from product_attribute_property_values
where id = #{id,jdbcType=INTEGER}
</select>
<select id="selectBySkns" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from product_attribute_property_values
where product_skn in
<foreach item="item" index="index" collection="skns"
open="(" separator="," close=")">
#{item}
</foreach>
</select>
<delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
delete from product_attribute_property_values
where id = #{id,jdbcType=INTEGER}
</delete>
<insert id="insert" parameterType="com.yoho.search.dal.model.ProductAttributePropertyValues" >
insert into product_attribute_property_values (id, product_skn, attribute_id,
attribute_value_id, display_position)
values (#{id,jdbcType=INTEGER}, #{productSkn,jdbcType=INTEGER}, #{attributeId,jdbcType=INTEGER},
#{attributeValueId,jdbcType=VARCHAR}, #{displayPosition,jdbcType=INTEGER})
</insert>
<insert id="insertSelective" parameterType="com.yoho.search.dal.model.ProductAttributePropertyValues" >
insert into product_attribute_property_values
<trim prefix="(" suffix=")" suffixOverrides="," >
<if test="id != null" >
id,
</if>
<if test="productSkn != null" >
product_skn,
</if>
<if test="attributeId != null" >
attribute_id,
</if>
<if test="attributeValueId != null" >
attribute_value_id,
</if>
<if test="displayPosition != null" >
display_position,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides="," >
<if test="id != null" >
#{id,jdbcType=INTEGER},
</if>
<if test="productSkn != null" >
#{productSkn,jdbcType=INTEGER},
</if>
<if test="attributeId != null" >
#{attributeId,jdbcType=INTEGER},
</if>
<if test="attributeValueId != null" >
#{attributeValueId,jdbcType=VARCHAR},
</if>
<if test="displayPosition != null" >
#{displayPosition,jdbcType=INTEGER},
</if>
</trim>
</insert>
<update id="updateByPrimaryKeySelective" parameterType="com.yoho.search.dal.model.ProductAttributePropertyValues" >
update product_attribute_property_values
<set >
<if test="productSkn != null" >
product_skn = #{productSkn,jdbcType=INTEGER},
</if>
<if test="attributeId != null" >
attribute_id = #{attributeId,jdbcType=INTEGER},
</if>
<if test="attributeValueId != null" >
attribute_value_id = #{attributeValueId,jdbcType=VARCHAR},
</if>
<if test="displayPosition != null" >
display_position = #{displayPosition,jdbcType=INTEGER},
</if>
</set>
where id = #{id,jdbcType=INTEGER}
</update>
<update id="updateByPrimaryKey" parameterType="com.yoho.search.dal.model.ProductAttributePropertyValues" >
update product_attribute_property_values
set product_skn = #{productSkn,jdbcType=INTEGER},
attribute_id = #{attributeId,jdbcType=INTEGER},
attribute_value_id = #{attributeValueId,jdbcType=VARCHAR},
display_position = #{displayPosition,jdbcType=INTEGER}
where id = #{id,jdbcType=INTEGER}
</update>
</mapper>
\ No newline at end of file
... ...
package com.yoho.search.consumer.index.increment;
import java.util.Map;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.core.ChannelAwareMessageListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.alibaba.fastjson.JSONObject;
import com.rabbitmq.client.Channel;
import com.yoho.error.event.SearchEvent;
import com.yoho.search.base.utils.ConvertUtils;
import com.yoho.search.base.utils.EventReportEnum;
import com.yoho.search.base.utils.ISearchConstants;
import com.yoho.search.consumer.service.base.ProductAttributeService;
import com.yoho.search.core.es.utils.IgnoreSomeException;
import com.yoho.search.dal.model.ProductAttribute;
@Component
public class ProductAttributeMqListener extends AbstractMqListener implements ChannelAwareMessageListener{
private static final Logger logger = LoggerFactory.getLogger(ProductAttributePropertyValuesMqListener.class);
@Autowired
private ProductAttributeService productAttributeService;
@Override
public void onMessage(Message message, Channel channel) throws Exception {
try {
String messageStr = new String(message.getBody(), "UTF-8");
logger.info("[model=ProductAttributeMqListener] [message={}]", messageStr);
// 如果在重建索引等待
this.waitingRebuildingIndex();
JSONObject json = JSONObject.parseObject(messageStr);
String tableName = ISearchConstants.TABLE_NAME_TBL_PRODUCT_ATTRIBUTE;
String action = json.getString("action");
if (ISearchConstants.ACTION_DELETE.equals(action)) {
deleteData(json.getString("data"), tableName);
} else if (ISearchConstants.ACTION_UPDATE.equals(action)) {
updateData(json.getObject("data", Map.class), tableName);
} else {
updateData(json.getObject("data", Map.class), tableName);
}
} catch (Exception e) {
publisher.publishEvent(new SearchEvent(EventReportEnum.PRODUCTATTRIBUTEMQLISTENER_ONMESSAGE.getEventName(), EventReportEnum.PRODUCTATTRIBUTEMQLISTENER_ONMESSAGE.getFunctionName(),
EventReportEnum.PRODUCTATTRIBUTEMQLISTENER_ONMESSAGE.getMoudleName(), "exception", IgnoreSomeException.filterSomeException(e), null));
Thread.sleep(1000);
throw e;
}
}
@SuppressWarnings({ "rawtypes", "unchecked" })
public void updateData(final Map data, final String tableName) throws Exception {
long begin = System.currentTimeMillis();
ProductAttribute productAttribute = new ProductAttribute();
productAttribute = (ProductAttribute) ConvertUtils.toJavaBean(productAttribute, data);
if (productAttribute == null || productAttribute.getAttributeId() == null) {
return;
}
// 更新数据库
productAttributeService.saveOrUpdateProductAttribute(productAttribute);
logger.info("[func=updateData][step=success][tableName={}][attributeId={}][cost={}ms]", tableName, productAttribute.getAttributeId(), (System.currentTimeMillis() - begin));
}
public void deleteData(final String id, final String tableName) throws Exception {
if (StringUtils.isBlank(id)) {
return;
}
long begin = System.currentTimeMillis();
// 删除数据
productAttributeService.deleteProductAttributeById(Integer.valueOf(id));
logger.info("[func=deleteData][step=success][tableName={}][attributeId={}][cost={}ms]", tableName, id, (System.currentTimeMillis() - begin));
}
}
... ...
package com.yoho.search.consumer.index.increment;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.core.ChannelAwareMessageListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.alibaba.fastjson.JSONObject;
import com.rabbitmq.client.Channel;
import com.yoho.error.event.SearchEvent;
import com.yoho.search.base.utils.ConvertUtils;
import com.yoho.search.base.utils.EventReportEnum;
import com.yoho.search.base.utils.ISearchConstants;
import com.yoho.search.consumer.index.common.IYohoIndexService;
import com.yoho.search.consumer.service.base.ProductAttributeService;
import com.yoho.search.consumer.service.base.ProductService;
import com.yoho.search.core.es.utils.IgnoreSomeException;
import com.yoho.search.dal.model.Product;
import com.yoho.search.dal.model.ProductAttributePropertyValues;
@Component
public class ProductAttributePropertyValuesMqListener extends AbstractMqListener implements ChannelAwareMessageListener{
private static final Logger logger = LoggerFactory.getLogger(ProductAttributePropertyValuesMqListener.class);
@Autowired
private ProductAttributeService productAttributeService;
@Autowired
private ProductService productService;
@Autowired
private IYohoIndexService indexService;
@Override
public void onMessage(Message message, Channel channel) throws Exception {
try {
String messageStr = new String(message.getBody(), "UTF-8");
logger.info("[model=ProductAttributePropertyValuesMqListener] [message={}]", messageStr);
// 如果在重建索引等待
this.waitingRebuildingIndex();
JSONObject json = JSONObject.parseObject(messageStr);
String indexName = ISearchConstants.INDEX_NAME_PRODUCT_INDEX;
String action = json.getString("action");
if (ISearchConstants.ACTION_DELETE.equals(action)) {
deleteData(json.getString("data"), indexName);
} else if (ISearchConstants.ACTION_UPDATE.equals(action)) {
updateData(json.getObject("data", Map.class), indexName);
} else {
updateData(json.getObject("data", Map.class), indexName);
}
} catch (Exception e) {
publisher.publishEvent(new SearchEvent(EventReportEnum.PRODUCTATTRIBUTEPROPERTYVALUESMQLISTENER_ONMESSAGE.getEventName(), EventReportEnum.PRODUCTATTRIBUTEPROPERTYVALUESMQLISTENER_ONMESSAGE.getFunctionName(),
EventReportEnum.PRODUCTATTRIBUTEPROPERTYVALUESMQLISTENER_ONMESSAGE.getMoudleName(), "exception", IgnoreSomeException.filterSomeException(e), null));
Thread.sleep(1000);
throw e;
}
}
@SuppressWarnings({ "rawtypes", "unchecked" })
public void updateData(final Map data, final String indexName) throws Exception {
long begin = System.currentTimeMillis();
ProductAttributePropertyValues productAttributePropertyValues = new ProductAttributePropertyValues();
productAttributePropertyValues = (ProductAttributePropertyValues) ConvertUtils.toJavaBean(productAttributePropertyValues, data);
if (productAttributePropertyValues == null || productAttributePropertyValues.getProductSkn() == null) {
return;
}
// 更新数据库
productAttributeService.saveOrUpdate(productAttributePropertyValues);
// 获取product数据
Product product = productService.getBySkn(productAttributePropertyValues.getProductSkn());
if (product == null) {
logger.info("[func=updateData][indexName={}] can not find this productId(skn= {}) in table product", indexName, productAttributePropertyValues.getProductSkn());
return;
}
// 更新es数据
Map<String, Object> esData = new HashMap<String, Object>();
String attributeNames = getAttributeNames(productAttributePropertyValues.getProductSkn());
esData.put("attributeNames", attributeNames);
indexService.updateIndexData(indexName, product.getId().toString(), esData);
logger.info("[func=updateData][step=success][indexName={}][skn={}][cost={}ms]", indexName, productAttributePropertyValues.getProductSkn(), (System.currentTimeMillis() - begin));
}
public void deleteData(final String id, final String indexName) throws Exception {
if (StringUtils.isBlank(id)) {
return;
}
long begin = System.currentTimeMillis();
ProductAttributePropertyValues productAttributePropertyValues = productAttributeService.selectById(Integer.valueOf(id));
// 删除数据
productAttributeService.deleteById(Integer.valueOf(id));
// 获取product数据
Product product = productService.getBySkn(productAttributePropertyValues.getProductSkn());
if (product == null) {
logger.info("[func=deleteData][indexName={}] can not find this productId({}) in table product", indexName, id);
return;
}
// 更新ES数据
Map<String, Object> data = new HashMap<String, Object>();
String attributeNames = getAttributeNames(productAttributePropertyValues.getProductSkn());
data.put("attributeNames", attributeNames);
indexService.updateIndexData(indexName, product.getId().toString(), data);
logger.info("[func=deleteData][step=success][indexName={}][productId={}][cost={}ms]", indexName, product.getId(), (System.currentTimeMillis() - begin));
}
public String getAttributeNames(Integer skn){
ArrayList<Integer> skns = new ArrayList<Integer>();
skns.add(skn);
Map<Integer, String> attributeMap = productAttributeService.selectBySkns(skns);
if(attributeMap.get(skn)!=null){
return attributeMap.get(skn);
}
return "";
}
}
... ...
... ... @@ -936,6 +936,10 @@
"type": "string",
"analyzer": "comma_spliter"
},
"attributeNames": {
"type": "string",
"analyzer": "comma_spliter"
},
"sknDefaultImg": {
"type": "string",
"index": "not_analyzed"
... ...
package com.yoho.search.consumer.service.base;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yoho.search.dal.ProductAttributeMapper;
import com.yoho.search.dal.ProductAttributePropertyValuesMapper;
import com.yoho.search.dal.model.ProductAttribute;
import com.yoho.search.dal.model.ProductAttributePropertyValues;
/**
* 从product_attribute_property_values和product_attribute获取skn的AttributeNames
* @author gemingdan
*
*/
@Service
public class ProductAttributeService {
@Autowired
private ProductAttributeMapper productAttributeMapper;
@Autowired
private ProductAttributePropertyValuesMapper productAttributePropertyValuesMapper;
public Map<Integer,String> selectBySkns(List<Integer> skns){
Map<Integer,String> result= new HashMap<Integer,String>();
List<ProductAttributePropertyValues> attributeList =productAttributePropertyValuesMapper.selectBySkns(skns);
if(attributeList==null){
return result;
}
Set<Integer> attributeIds=new HashSet<Integer>();
attributeList.forEach(a->{
attributeIds.add(a.getAttributeId());
});
if(attributeIds.size()==0){
return result;
}
List<ProductAttribute> valueList=productAttributeMapper.selectByAttributeIds(attributeIds);
if(valueList==null||valueList.size()==0){
return result;
}
Map<Integer, ProductAttribute> ProductAttributeMap = valueList.stream().parallel().collect(Collectors.toMap(ProductAttribute::getAttributeId, (p) -> p));
Map<Integer,Set<String>> resultSet= new HashMap<Integer,Set<String>>();
attributeList.forEach(a->{
if(ProductAttributeMap.get(a.getAttributeId())==null||a.getAttributeValueId()==null){
return;
}
ProductAttribute pa= ProductAttributeMap.get(a.getAttributeId());
String[] attributeValueIds = a.getAttributeValueId().split(",");
if(pa.getAttributeValues()==null||pa.getAttributeValues().indexOf("[")<0){
return;
}
JSONArray values = JSONArray.parseArray(pa.getAttributeValues());
Set<String> attributeNameSet=new HashSet<String>();
values.forEach(v->{
JSONObject vobj=(JSONObject)v;
for(int i=0;i<attributeValueIds.length;i++){
Integer id=(Integer)vobj.get("id");
if(id!=null&&attributeValueIds[i].endsWith(id.toString())){
attributeNameSet.add((String)vobj.get("name"));
}
}
});
if(attributeNameSet.size()==0){
return;
}
if(resultSet.get(a.getProductSkn())!=null){
attributeNameSet.addAll(resultSet.get(a.getProductSkn()));
}
resultSet.put(a.getProductSkn(),attributeNameSet);
});
resultSet.forEach((skn,set)->{
result.put(skn,StringUtils.join(set.toArray(), ",") );
});
return result;
}
public void saveOrUpdate(ProductAttributePropertyValues productAttributePropertyValues) {
if(productAttributePropertyValues==null||productAttributePropertyValues.getId()==null){
return;
}
if(productAttributePropertyValuesMapper.selectByPrimaryKey(productAttributePropertyValues.getId())==null){
//插入
productAttributePropertyValuesMapper.insert(productAttributePropertyValues);
}else{
//更新
productAttributePropertyValuesMapper.updateByPrimaryKey(productAttributePropertyValues);
}
}
public void deleteById(Integer id) {
productAttributePropertyValuesMapper.deleteByPrimaryKey(id);
}
public ProductAttributePropertyValues selectById(Integer id) {
return productAttributePropertyValuesMapper.selectByPrimaryKey(id);
}
public void saveOrUpdateProductAttribute(ProductAttribute productAttribute) {
if(productAttribute==null||productAttribute.getAttributeId()==null){
return;
}
if(productAttributeMapper.selectByPrimaryKey(productAttribute.getAttributeId())==null){
//插入
productAttributeMapper.insert(productAttribute);
}else{
//更新
productAttributeMapper.updateByPrimaryKey(productAttribute);
}
}
public void deleteProductAttributeById(Integer id) {
productAttributeMapper.deleteByPrimaryKey(id);
}
}
... ...
... ... @@ -194,6 +194,7 @@ public class ProductIndexService {
map.put("phrase", productIndexBO.getPhrase());
map.put("isPhraseExist", productIndexBO.getIsPhraseExist());
map.put("pattern", productIndexBO.getPattern());
map.put("attributeNames", productIndexBO.getAttributeNames());
return map;
}
... ...
... ... @@ -104,6 +104,9 @@ public class ProductIndexBO extends ProductIBO implements Serializable {
// from `erp_product`.`product_ext`
private String sknDefaultImg;
//from erp_product.product_attribute yh_shops.product_attribute_property_values
private String attributeNames;
// get
public BigDecimal getSpecialPrice() {
return specialPrice;
... ... @@ -482,4 +485,12 @@ public class ProductIndexBO extends ProductIBO implements Serializable {
this.sknDefaultImg = sknDefaultImg;
}
public String getAttributeNames() {
return attributeNames;
}
public void setAttributeNames(String attributeNames) {
this.attributeNames = attributeNames;
}
}
\ No newline at end of file
... ...
package com.yoho.search.consumer.service.logic.productIndex.viewBuilder;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.yoho.search.consumer.service.base.ProductAttributeService;
import com.yoho.search.consumer.service.bo.ProductIndexBO;
/**
* attributeNames
* from erp_product.product_attribute yh_shops.product_attribute_property_values
* @author gemingdan
*
*/
@Component
public class ProductAttributeNamesBuilder implements ViewBuilder {
@Autowired
private ProductAttributeService productAttributeService;
@Override
public void build(List<ProductIndexBO> productIndexBOs, List<Integer> ids,List<Integer> skns) {
Map<Integer, String> attributeNamesMap = productAttributeService.selectBySkns(skns);
for(ProductIndexBO b:productIndexBOs){
if(b.getProductSkn()!=null&&attributeNamesMap.get(b.getProductSkn())!=null){
b.setAttributeNames(attributeNamesMap.get(b.getProductSkn()));
}
if(b.getAttributeNames()==null){
b.setAttributeNames("");
}
}
}
}
... ...
... ... @@ -77,6 +77,8 @@
<rabbit:queue durable="true" exclusive="false" name="data_update_channel_tblproductskc" />
<rabbit:queue durable="true" exclusive="false" name="data_update_channel_tblproductsku" />
<rabbit:queue durable="true" exclusive="false" name="data_update_channel_productext" />
<rabbit:queue durable="true" exclusive="false" name="data_update_channel_productattributepropertyvalues" />
<rabbit:queue durable="true" exclusive="false" name="data_update_channel_productattribute" />
<rabbit:template exchange="${search.mq.exchange}" id="amqpTemplate"
connection-factory="connectionFactory" message-converter="jsonMessageConverter" />
... ... @@ -156,6 +158,8 @@
<rabbit:listener queue-names="data_update_channel_tblproductskc" ref="tblProductSkcMqListener" />
<rabbit:listener queue-names="data_update_channel_tblproductsku" ref="tblProductSkuMqListener" />
<rabbit:listener queue-names="data_update_channel_productext" ref="productExtMqListener" />
<rabbit:listener queue-names="data_update_channel_productattributepropertyvalues" ref="productAttributePropertyValuesMqListener" />
<rabbit:listener queue-names="data_update_channel_productattribute" ref="productAttributeMqListener" />
</rabbit:listener-container>
</beans>
\ No newline at end of file
... ...