Authored by WN\wangnan

Merge branch 'master' into wn_fix

package com.yoho.search.dal;
import com.yoho.search.dal.model.ScoreSknRule;
import org.apache.ibatis.annotations.Param;
import java.util.List;
public interface ScoreSknRuleMapper {
int deleteByPrimaryKey(Integer id);
int insert(ScoreSknRule record);
int insertSelective(ScoreSknRule record);
ScoreSknRule selectByPrimaryKey(Integer id);
ScoreSknRule selectBySkn(Integer skn);
List<ScoreSknRule> selectAll();
List<Integer> selectAllSkn();
List<Integer> selectBySkns(@Param(value="skns")List<Integer> skns);
int updateByPrimaryKeySelective(ScoreSknRule record);
int updateByPrimaryKey(ScoreSknRule record);
}
\ 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.ScoreSknRuleMapper" >
<resultMap id="BaseResultMap" type="com.yoho.search.dal.model.ScoreSknRule" >
<id column="id" property="id" jdbcType="INTEGER" />
<result column="skn" property="skn" jdbcType="INTEGER" />
<result column="update_time" property="updateTime" jdbcType="INTEGER" />
<result column="create_time" property="createTime" jdbcType="INTEGER" />
</resultMap>
<sql id="Base_Column_List" >
id, skn, update_time, create_time
</sql>
<select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
select
<include refid="Base_Column_List" />
from score_skn_rule
where id = #{id,jdbcType=INTEGER}
</select>
<select id="selectBySkn" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
select
<include refid="Base_Column_List" />
from score_skn_rule
where skn = #{skn,jdbcType=INTEGER}
</select>
<select id="selectAll" resultMap="BaseResultMap" >
select
<include refid="Base_Column_List" />
from score_skn_rule
</select>
<select id="selectAllSkn" resultType="java.lang.Integer" >
select skn
from score_skn_rule
</select>
<select id="selectBySkns" resultType="java.lang.Integer">
select skn
from score_skn_rule
where skn in
<foreach item="item" collection="skns" open="(" separator="," close=")">
#{item}
</foreach>
</select>
<delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
delete from score_skn_rule
where id = #{id,jdbcType=INTEGER}
</delete>
<insert id="insert" parameterType="com.yoho.search.dal.model.ScoreSknRule" >
insert into score_skn_rule (id, skn, update_time,
create_time)
values (#{id,jdbcType=INTEGER}, #{skn,jdbcType=INTEGER}, #{updateTime,jdbcType=INTEGER},
#{createTime,jdbcType=INTEGER})
</insert>
<insert id="insertSelective" parameterType="com.yoho.search.dal.model.ScoreSknRule" >
insert into score_skn_rule
<trim prefix="(" suffix=")" suffixOverrides="," >
<if test="id != null" >
id,
</if>
<if test="skn != null" >
skn,
</if>
<if test="updateTime != null" >
update_time,
</if>
<if test="createTime != null" >
create_time,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides="," >
<if test="id != null" >
#{id,jdbcType=INTEGER},
</if>
<if test="skn != null" >
#{skn,jdbcType=INTEGER},
</if>
<if test="updateTime != null" >
#{updateTime,jdbcType=INTEGER},
</if>
<if test="createTime != null" >
#{createTime,jdbcType=INTEGER},
</if>
</trim>
</insert>
<update id="updateByPrimaryKeySelective" parameterType="com.yoho.search.dal.model.ScoreSknRule" >
update score_skn_rule
<set >
<if test="skn != null" >
skn = #{skn,jdbcType=INTEGER},
</if>
<if test="updateTime != null" >
update_time = #{updateTime,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.ScoreSknRule" >
update score_skn_rule
set skn = #{skn,jdbcType=INTEGER},
update_time = #{updateTime,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.index.increment.productIndex;
import com.alibaba.fastjson.JSONObject;
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.index.increment.AbstractIndexMqListener;
import com.yoho.search.consumer.service.base.ProductService;
import com.yoho.search.consumer.service.base.ScoreSknRuleService;
import com.yoho.search.dal.model.Product;
import com.yoho.search.dal.model.ScoreSknRule;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.HashMap;
import java.util.Map;
/**
* Created by gemingdan on 2017/6/19.
*/
@Component
public class ScoreSknRuleMqListener extends AbstractIndexMqListener {
private static final Logger logger = LoggerFactory.getLogger(ScoreSknRuleMqListener.class);
@Autowired
private ScoreSknRuleService scoreSknRuleService;
@Autowired
private ProductService productService;
@Autowired
private IYohoIndexService indexService;
@Override
public String getIndexName() {
return ISearchConstants.INDEX_NAME_PRODUCT_INDEX;
}
@Override
protected EventReportEnum getEventReportEnum() {
return EventReportEnum.SCORESKNRULEMQLISTENER_ONMESSAGE;
}
@Override
protected void deleteData(String id) throws Exception {
int intId=Integer.valueOf(id);
ScoreSknRule scoreSknRule = scoreSknRuleService.selectById(intId);
if (scoreSknRule ==null) {
return;
}
int result=scoreSknRuleService.deleteById(intId);
if (result > 0) {
// 获取product数据
Product product = productService.getBySkn(Integer.valueOf(scoreSknRule.getSkn()));
if (product == null) {
logger.warn("[func=deleteData][indexName={}] can not find this productId(skn= {}) in table product", this.getIndexName(), scoreSknRule.getSkn());
return;
}
// 更新ES数据
Map<String, Object> esData = new HashMap<String, Object>();
esData.put("toAddScore", "N");
indexService.updateIndexData(this.getIndexName(), product.getId().toString(), esData);
}
}
@Override
protected void updateData(JSONObject data) throws Exception {
ScoreSknRule scoreSknRule = ConvertUtils.toJavaObject(ScoreSknRule.class, data);
if (scoreSknRule == null || scoreSknRule.getId() == null || scoreSknRule.getSkn() == null) {
return;
}
int result = scoreSknRuleService.insertOrUpdate(scoreSknRule);
if (result > 0) {
// 获取product数据
Product product = productService.getBySkn(scoreSknRule.getSkn());
if (product == null) {
logger.warn("[func=updateData][indexName={}] can not find this productId(skn= {}) in table product", this.getIndexName(), scoreSknRule.getSkn());
return;
}
// 更新es数据
Map<String, Object> esData = new HashMap<String, Object>();
esData.put("toAddScore", "Y");
indexService.updateIndexData(this.getIndexName(), product.getId().toString(), esData);
}
}
}
... ...
... ... @@ -884,6 +884,14 @@
"format": "doc_values"
}
},
"toAddScore": {
"type": "string",
"index": "not_analyzed",
"doc_values": true,
"fielddata": {
"format": "doc_values"
}
},
"goodsList": {
"properties": {
"color_code": {
... ...
... ... @@ -8,6 +8,8 @@ import java.util.Set;
import java.util.stream.Collectors;
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.Service;
... ... @@ -20,80 +22,97 @@ import com.yoho.search.dal.model.ProductAttributePropertyValues;
/**
* 从product_attribute_property_values和product_attribute获取skn的AttributeNames
*
* @author gemingdan
*
*/
@Service
public class ProductAttributeService {
private static final Logger logger = LoggerFactory.getLogger(ProductAttributeService.class);
@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;
public Map<Integer, String> selectBySkns(List<Integer> skns) {
if (skns == null || skns.isEmpty()) {
return new HashMap<Integer, String>();
}
Set<Integer> attributeIds=new HashSet<Integer>();
attributeList.forEach(a->{
attributeIds.add(a.getAttributeId());
});
if(attributeIds.size()==0){
return result;
List<ProductAttributePropertyValues> attributePropertyValues = productAttributePropertyValuesMapper.selectBySkns(skns);
if (attributePropertyValues == null || attributePropertyValues.isEmpty()) {
return new HashMap<Integer, String>();
}
List<ProductAttribute> valueList=productAttributeMapper.selectByAttributeIds(attributeIds);
if(valueList==null||valueList.size()==0){
return result;
Set<Integer> attributeIds = new HashSet<Integer>();
for (ProductAttributePropertyValues attributePropertyValue : attributePropertyValues) {
attributeIds.add(attributePropertyValue.getAttributeId());
}
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"));
List<ProductAttribute> productAttributes = productAttributeMapper.selectByAttributeIds(attributeIds);
if (productAttributes == null || productAttributes.isEmpty()) {
return new HashMap<Integer, String>();
}
Map<Integer, ProductAttribute> productAttributeMap = productAttributes.stream().parallel().collect(Collectors.toMap(ProductAttribute::getAttributeId, (p) -> p));
Map<Integer, Set<String>> sknToNameSetMap = new HashMap<Integer, Set<String>>();
for (ProductAttributePropertyValues a : attributePropertyValues) {
try {
// 1、判断ProductAttribute是否存在,以及对应数据的合法性
if (productAttributeMap.get(a.getAttributeId()) == null || a.getAttributeValueId() == null) {
continue;
}
ProductAttribute productAttribute = productAttributeMap.get(a.getAttributeId());
if (productAttribute.getAttributeValues() == null || productAttribute.getAttributeValues().indexOf("[") < 0) {
continue;
}
JSONArray attributeValues = JSONArray.parseArray(productAttribute.getAttributeValues());
// 2、判断attributeValueIds的合法性
String[] attributeValueIds = a.getAttributeValueId().split(",");
// 3、循环对比attribute_value_id和attribute_values
Set<String> attributeNameSet = new HashSet<String>();
for (int i = 0; i < attributeValues.size(); i++) {
JSONObject attributeValue = attributeValues.getJSONObject(i);
String idString = attributeValue.getString("id");
if (StringUtils.isBlank(idString)) {
continue;
}
for (String attributeValueId : attributeValueIds) {
if (attributeValueId.equals(idString)) {
attributeNameSet.add(attributeValue.getString("name"));
}
}
}
});
if(attributeNameSet.size()==0){
return;
}
if(resultSet.get(a.getProductSkn())!=null){
attributeNameSet.addAll(resultSet.get(a.getProductSkn()));
if (attributeNameSet.size() == 0) {
continue;
}
if (sknToNameSetMap.get(a.getProductSkn()) != null) {
attributeNameSet.addAll(sknToNameSetMap.get(a.getProductSkn()));
}
sknToNameSetMap.put(a.getProductSkn(), attributeNameSet);
} catch (Exception e) {
logger.error(e.getMessage());
}
resultSet.put(a.getProductSkn(),attributeNameSet);
});
resultSet.forEach((skn,set)->{
result.put(skn,StringUtils.join(set.toArray(), ",") );
});
}
// 构造结果
Map<Integer, String> result = new HashMap<Integer, String>();
for (Map.Entry<Integer, Set<String>> entry : sknToNameSetMap.entrySet()) {
result.put(entry.getKey(), StringUtils.join(entry.getValue().toArray(), ","));
}
return result;
}
public void saveOrUpdate(ProductAttributePropertyValues productAttributePropertyValues) {
if(productAttributePropertyValues==null||productAttributePropertyValues.getId()==null){
if (productAttributePropertyValues == null || productAttributePropertyValues.getId() == null) {
return;
}
if(productAttributePropertyValuesMapper.selectByPrimaryKey(productAttributePropertyValues.getId())==null){
//插入
productAttributePropertyValuesMapper.insert(productAttributePropertyValues);
}else{
//更新
if (productAttributePropertyValuesMapper.selectByPrimaryKey(productAttributePropertyValues.getId()) == null) {
// 插入
productAttributePropertyValuesMapper.insertSelective(productAttributePropertyValues);
} else {
// 更新
productAttributePropertyValuesMapper.updateByPrimaryKey(productAttributePropertyValues);
}
}
... ... @@ -107,22 +126,21 @@ public class ProductAttributeService {
}
public void saveOrUpdateProductAttribute(ProductAttribute productAttribute) {
if(productAttribute==null||productAttribute.getAttributeId()==null){
if (productAttribute == null || productAttribute.getAttributeId() == null) {
return;
}
if(productAttributeMapper.selectByPrimaryKey(productAttribute.getAttributeId())==null){
//插入
if (productAttributeMapper.selectByPrimaryKey(productAttribute.getAttributeId()) == null) {
// 插入
productAttributeMapper.insert(productAttribute);
}else{
//更新
} else {
// 更新
productAttributeMapper.updateByPrimaryKey(productAttribute);
}
}
public void deleteProductAttributeById(Integer id) {
productAttributeMapper.deleteByPrimaryKey(id);
}
}
... ...
... ... @@ -215,6 +215,7 @@ public class ProductIndexService {
map.put(ProductIndexEsField.couponLimitStatus, productIndexBO.getCouponLimitStatus());
map.put(ProductIndexEsField.isFobbiden, productIndexBO.getIsFobbiden());
map.put(ProductIndexEsField.storeShowStatus, productIndexBO.getStoreShowStatus());
map.put(ProductIndexEsField.toAddScore, productIndexBO.getToAddScore());
return map;
}
... ...
package com.yoho.search.consumer.service.base;
import com.yoho.search.dal.ScoreSknRuleMapper;
import com.yoho.search.dal.model.ScoreSknRule;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* Created by gemingdan on 2017/6/19.
*/
@Service
public class ScoreSknRuleService {
@Autowired
private ScoreSknRuleMapper scoreSknRuleMapper;
public int deleteById(Integer id){
return scoreSknRuleMapper.deleteByPrimaryKey(id);
}
public int insertOrUpdate(ScoreSknRule record){
ScoreSknRule scoreSknRule = scoreSknRuleMapper.selectBySkn(record.getSkn());
if (scoreSknRule == null) {
return scoreSknRuleMapper.insert(record);
}
scoreSknRule.setUpdateTime(record.getUpdateTime());
scoreSknRule.setCreateTime(record.getCreateTime());
scoreSknRuleMapper.updateByPrimaryKey(scoreSknRule);
return 0;
}
public ScoreSknRule selectById(Integer id){
return scoreSknRuleMapper.selectByPrimaryKey(id);
}
public String getAllSkns() {
StringBuilder result = new StringBuilder();
List<Integer> scoreSknRules = scoreSknRuleMapper.selectAllSkn();
for (Integer skn: scoreSknRules) {
result.append(",").append(skn);
}
if (result.length() < 1) {
return null;
}
return result.substring(1);
}
}
... ...
... ... @@ -130,6 +130,9 @@ public class ProductIndexBO extends ProductIBO implements Serializable {
// from product_price
private Integer lastReducePriceTime;
//from score_skn_rule
private String toAddScore;
public Integer getLastReducePriceTime() {
return lastReducePriceTime;
}
... ... @@ -625,4 +628,12 @@ public class ProductIndexBO extends ProductIBO implements Serializable {
public void setPromotionTitles(String promotionTitles) {
this.promotionTitles = promotionTitles;
}
public String getToAddScore() {
return toAddScore;
}
public void setToAddScore(String toAddScore) {
this.toAddScore = toAddScore;
}
}
\ No newline at end of file
... ...
package com.yoho.search.consumer.service.logic;
import com.yoho.search.consumer.service.base.ScoreSknRuleService;
import org.apache.curator.framework.CuratorFramework;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
/**
* Created by gemingdan on 2017/6/19.
* 暂时无用
*/
//@Component
public class ScoreSknRuleLogicService implements InitializingBean {
private static final Logger logger = LoggerFactory.getLogger(ScoreSknRuleLogicService.class);
private static final String CONFIG_ROOT_PATH = "/yh/config";
private static final String KEY = "search.function.score.skn.rule";
@Autowired
private ScoreSknRuleService scoreSknRuleService;
@Resource(name = "curatorFramework")
private CuratorFramework client;
@Override
public void afterPropertiesSet() throws Exception {
updateZkScoreSkn();
}
public void updateZkScoreSkn() {
String skns = scoreSknRuleService.getAllSkns();
if (skns == null) {
return;
}
publishToZk(skns);
}
private synchronized void publishToZk(String value) {
try {
if (client.checkExists().forPath(CONFIG_ROOT_PATH) == null) {
client.create().creatingParentContainersIfNeeded().forPath(CONFIG_ROOT_PATH);
}
String path = CONFIG_ROOT_PATH + "/" + KEY;
if (this.client.checkExists().forPath(path) == null) {
this.client.create().forPath(path, value.getBytes("UTF-8"));
} else {
this.client.setData().forPath(path, value.getBytes("UTF-8"));
}
} catch (Exception e) {
logger.error("publish function score rule " + KEY + " to zk failed!", e);
}
}
}
... ...
package com.yoho.search.consumer.service.logic.productIndex.viewBuilder;
import com.yoho.search.consumer.service.bo.ProductIndexBO;
import com.yoho.search.dal.ScoreSknRuleMapper;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
/**
* Created by gemingdan on 2017/6/21.
*/
@Component
public class ScoreSknRuleBuilder implements ViewBuilder {
@Autowired
private ScoreSknRuleMapper scoreSknRuleMapper;
@Override
public void build(List<ProductIndexBO> productIndexBOs, List<Integer> ids, List<Integer> sknList) {
List<Integer> productSknList = scoreSknRuleMapper.selectBySkns(sknList);
if(CollectionUtils.isEmpty(productSknList)){
return;
}
Set<Integer> sknSet = productSknList.stream().parallel().collect(Collectors.toSet());
productIndexBOs.stream().forEach(productIndexBO -> {
if (sknSet.contains(productIndexBO.getProductSkn())) {
productIndexBO.setToAddScore("Y");
} else {
productIndexBO.setToAddScore("N");
}
});
}
}
... ...
... ... @@ -82,6 +82,7 @@
<rabbit:queue durable="true" exclusive="false" name="data_update_channel_promotioninfo" />
<rabbit:queue durable="true" exclusive="false" name="data_update_channel_promotionparams" />
<rabbit:queue durable="true" exclusive="false" name="data_update_channel_promotionproductflags" />
<rabbit:queue durable="true" exclusive="false" name="data_update_channel_scoresknrule" />
<rabbit:template exchange="${search.mq.exchange}" id="amqpTemplate"
connection-factory="connectionFactory" message-converter="jsonMessageConverter" />
... ... @@ -166,6 +167,7 @@
<rabbit:listener queue-names="data_update_channel_promotioninfo" ref="promotionInfoMqListener" />
<rabbit:listener queue-names="data_update_channel_promotionparams" ref="promotionParamsMqListener" />
<rabbit:listener queue-names="data_update_channel_promotionproductflags" ref="promotionProductFlagsMqListener" />
<rabbit:listener queue-names="data_update_channel_scoresknrule" ref="scoreSknRuleMqListener" />
</rabbit:listener-container>
</beans>
\ No newline at end of file
... ...