Authored by gemingdan

skn加分

package com.yoho.search.dal;
import com.yoho.search.dal.model.ScoreSknRule;
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();
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 id = #{id,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>
<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.database;
import com.alibaba.fastjson.JSONObject;
import com.yoho.search.base.utils.ConvertUtils;
import com.yoho.search.base.utils.EventReportEnum;
import com.yoho.search.consumer.index.increment.AbstractMqListener;
import com.yoho.search.consumer.service.base.ScoreSknRuleService;
import com.yoho.search.consumer.service.logic.ScoreSknRuleLogicService;
import com.yoho.search.dal.model.ScoreSknRule;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
/**
* Created by gemingdan on 2017/6/19.
*/
@Component
public class ScoreSknRuleMqListener extends AbstractMqListener {
@Autowired
private ScoreSknRuleService scoreSknRuleService;
@Autowired
private ScoreSknRuleLogicService scoreSknRuleLogicService;
@Override
protected EventReportEnum getEventReportEnum() {
return EventReportEnum.SCORESKNRULEMQLISTENER_ONMESSAGE;
}
@Override
protected void deleteData(String id) throws Exception {
int intId=Integer.valueOf(id);
if (scoreSknRuleService.selectById(intId)==null) {
return;
}
int result=scoreSknRuleService.deleteById(intId);
if (result > 0) {
scoreSknRuleLogicService.updateZkScoreSkn();
}
}
@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.insert(scoreSknRule);
if (result > 0) {
scoreSknRuleLogicService.updateZkScoreSkn();
}
}
}
... ...
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 insert(ScoreSknRule record){
if (scoreSknRuleMapper.selectBySkn(record.getSkn()) != null) {
return 1;
}
return scoreSknRuleMapper.insert(record);
}
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);
}
}
... ...
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);
}
}
}
... ...
... ... @@ -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
... ...