Authored by tanling

付费渠道新客券

package com.yoho.unions.common.redis;
import com.alibaba.fastjson.JSONArray;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.yoho.core.redis.YHHashOperations;
import com.yoho.core.redis.YHRedisTemplate;
import com.yoho.unions.common.utils.SerializeUtils;
import com.yoho.unions.helper.CacheKeyHelper;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
... ... @@ -11,15 +17,21 @@ import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
@Component
public class RedisHashCache {
private final static Logger logger = LoggerFactory.getLogger(RedisHashCache.class);
@Resource(name="yhHashOperations")
YHHashOperations<String, String, String> yhHashOperations;
@Resource(name = "yhHashOperations")
private YHHashOperations<String, String, String> hashOperations;
@Resource(name = "yhRedisTemplate")
private YHRedisTemplate<String, String> yHRedisTemplate;
/**
* 获取所有的values
... ... @@ -35,7 +47,7 @@ public class RedisHashCache {
return null;
}
List<String> valueList = yhHashOperations.values(key);
List<String> valueList = hashOperations.values(key);
logger.debug("values hashOperation value list size is {}", valueList == null ? 0 : valueList.size());
if (CollectionUtils.isEmpty(valueList)) {
return null;
... ... @@ -51,4 +63,170 @@ public class RedisHashCache {
return null;
}
/**
* Hash存储数据
*
* @param cacheKey
* @param entries
* @param timeout
* @param unit
* @param <V>
*/
public <V> void add(String cacheKey, Map<String, List<V>> entries, long timeout, TimeUnit unit) {
if (entries == null || entries.size() == 0) {
logger.warn("add params is null .");
return;
}
try {
// 将入口参数中的 Map<String, List<V>> 装换成 Map<String, String>
Map<String, String> valueStrMap = Maps.newHashMap();
for (String key : entries.keySet()) {
List<V> valueObjList = entries.get(key);
valueStrMap.put(key, JSONArray.toJSONString(valueObjList));
}
this.hashOperations.putAll(cacheKey, valueStrMap);
this.yHRedisTemplate.longExpire(cacheKey, timeout, unit);
} catch (Exception e) {
logger.warn("add failed!!! key is: {}", cacheKey);
}
}
/**
* 根据key和hashkey获取数据
*
* @param cacheKey
* @param hashKey
* @param clazz
* @param <T>
* @return
*/
public <T> T get(String cacheKey, String hashKey, Class<T> clazz) {
try {
if (StringUtils.isEmpty(cacheKey) || StringUtils.isEmpty(hashKey)) {
return null;
}
String value = this.hashOperations.get(cacheKey, hashKey);
if (StringUtils.isEmpty(value)) {
return null;
}
return CacheKeyHelper.string2Value(value, clazz);
} catch (Exception e) {
logger.warn("RedisHashCache get failed!!! cacheKey is: {},hashkey {}", cacheKey, hashKey);
}
return null;
}
/**
* 根据key和hashkey获取数据
*
* @param cacheKey
* @param clazz
* @param <T>
* @return
*/
public <T> List<T> multiGet(String cacheKey, Collection<? extends Object> hashKeyList, Class<T> clazz) {
try {
if (StringUtils.isEmpty(cacheKey) || CollectionUtils.isEmpty(hashKeyList)) {
return null;
}
List<String> hkeyList = new ArrayList<>();
for (Object hobj : hashKeyList) {
hkeyList.add(hobj.toString());
}
List<String> valueList = this.hashOperations.multiGet(cacheKey, hkeyList);
if (CollectionUtils.isEmpty(valueList)) {
return null;
}
List<T> resultList = Lists.newArrayList();
for (String result : valueList) {
if (StringUtils.isEmpty(result)) {
continue;
}
resultList.add(CacheKeyHelper.string2Value(result, clazz));
}
return resultList;
} catch (Exception e) {
logger.warn("RedisHashCache get failed!!! cacheKey is: {},hashkey {}", cacheKey, hashKeyList);
}
return null;
}
/**
* 存储hash结构数据
*
* @param cacheKey
* @param hashKey
* @param value
* @param timeout
* @param unit
* @param <T>
*/
public <T> void put(String cacheKey, String hashKey, T value, long timeout, TimeUnit unit) {
try {
if (StringUtils.isEmpty(cacheKey) || StringUtils.isEmpty(hashKey)) {
logger.warn("RedisHashCache put failed!!! cacheKey is: {},hashkey {}", cacheKey, hashKey);
return;
}
String v = CacheKeyHelper.value2String(value);
if (StringUtils.isEmpty(v)) {
logger.warn("RedisHashCache put failed!!! value {}", v);
return;
}
this.hashOperations.put(cacheKey, hashKey, v);
this.yHRedisTemplate.longExpire(cacheKey, timeout, unit);
} catch (Exception e) {
logger.warn("RedisHashCache get failed!!! cacheKey is: {},hashkey {}", cacheKey, hashKey);
}
}
/**
* 批量put
*
* @param obj key的后缀
* @param m 需要设置的值(该map的key是去除hashKey后缀的值)
* @param unit 超时时间单位
*/
public <T> void putAll(String cacheKey, Map<? extends String, ? extends T> map, long timeout, TimeUnit unit) {
logger.debug("Enter putAll hashOperation redis value. obj is {}, m is {}", map);
if (MapUtils.isEmpty(map)) {
return;
}
try {
if (StringUtils.isBlank(cacheKey)) {
return;
}
Map<String,String> newMap = Maps.newHashMap();
for(Map.Entry<? extends String, ? extends T> o : map.entrySet()){
newMap.put(o.getKey(),CacheKeyHelper.value2String(o.getValue()));
}
// 批量set
hashOperations.putAll(cacheKey, newMap);
// 设置超时
yHRedisTemplate.longExpire(cacheKey, timeout, unit);
} catch (Exception e) {
logger.warn("Redis exception. hash redis putAll . obj is {}, map is {}, exception msg is {}", map, e.getMessage());
}
}
/**
* 判断值是否存在
* @param cacheKey
* @return
*/
public Boolean hasKey(String cacheKey) {
logger.debug("Enter hasKey hashOperation redis. cacheKey is {}", cacheKey);
try {
return yHRedisTemplate.hasKey(cacheKey);
} catch (Exception e) {
logger.warn("get haskey method fail!! e {}", e);
}
return false;
}
}
... ...
package com.yoho.unions.vo;
import lombok.Data;
import lombok.ToString;
/**
* Created by yoho on 2017/3/2.
*/
@Data
@ToString
public class ChannelTypeReqVO {
private String udid;
private String mobile;
}
... ...
package com.yoho.unions.vo;
import lombok.Data;
import lombok.ToString;
/**
* Created by yoho on 2017/3/2.
*/
@Data
@ToString
public class ChannelTypeVO {
private String channelTypeName;
}
... ...
package com.yoho.unions.vo;
import com.alibaba.fastjson.annotation.JSONField;
import lombok.Data;
import lombok.ToString;
import java.io.Serializable;
@Data
@ToString
public class UnionReqVO{
private String channel_id;
... ... @@ -23,68 +25,5 @@ public class UnionReqVO{
private String utm_campaign;
public String getChannel_id() {
return channel_id;
}
public void setChannel_id(String channel_id) {
this.channel_id = channel_id;
}
public String getTarget_url() {
return target_url;
}
public void setTarget_url(String target_url) {
this.target_url = target_url;
}
public String getTracking_code() {
return tracking_code;
}
public void setTracking_code(String tracking_code) {
this.tracking_code = tracking_code;
}
public String getU_id() {
return u_id;
}
public void setU_id(String u_id) {
this.u_id = u_id;
}
public String getUnion_type() {
return union_type == null ? "" :union_type;
}
public void setUnion_type(String union_type) {
this.union_type = union_type;
}
public String getUtm_source() {
return utm_source == null ? "" :utm_source ;
}
public void setUtm_source(String utm_source) {
this.utm_source = utm_source;
}
public String getUtm_medium() {
return utm_medium == null ? "" :utm_medium;
}
public void setUtm_medium(String utm_medium) {
this.utm_medium = utm_medium;
}
public String getUtm_campaign() {
return utm_campaign == null ? "" : utm_campaign;
}
public void setUtm_campaign(String utm_campaign) {
this.utm_campaign = utm_campaign;
}
}
... ...
package com.yoho.unions.dal;
import com.yoho.unions.dal.model.MobilePanChannel;
import org.apache.ibatis.annotations.Param;
public interface IMobilePanChannelDAO {
int deleteByPrimaryKey(Integer id);
int insert(MobilePanChannel record);
int insertSelective(MobilePanChannel record);
MobilePanChannel selectByPrimaryKey(Integer id);
MobilePanChannel selectByMobile(@Param("mobile") String mobile);
int updateByPrimaryKeySelective(MobilePanChannel record);
int updateByPrimaryKey(MobilePanChannel record);
}
\ No newline at end of file
... ...
package com.yoho.unions.dal.model;
import lombok.Data;
import lombok.ToString;
@Data
@ToString
public class MobilePanChannel {
private Integer id;
private String mobile;
private Byte channel;
private Integer createTime;
}
\ 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.unions.dal.IMobilePanChannelDAO" >
<resultMap id="BaseResultMap" type="com.yoho.unions.dal.model.MobilePanChannel" >
<id column="id" property="id" jdbcType="INTEGER" />
<result column="mobile" property="mobile" jdbcType="VARCHAR" />
<result column="channel" property="channel" jdbcType="TINYINT" />
<result column="create_time" property="createTime" jdbcType="INTEGER" />
</resultMap>
<sql id="Base_Column_List" >
id, mobile, channel, create_time
</sql>
<select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
select
<include refid="Base_Column_List" />
from mobile_pan_channel
where id = #{id,jdbcType=INTEGER}
</select>
<select id="selectByMobile" resultMap="BaseResultMap" parameterType="java.lang.String" >
select
<include refid="Base_Column_List" />
from mobile_pan_channel
where mobile = #{mobile,jdbcType=VARCHAR}
</select>
<delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
delete from mobile_pan_channel
where id = #{id,jdbcType=INTEGER}
</delete>
<insert id="insert" parameterType="com.yoho.unions.dal.model.MobilePanChannel" >
insert into mobile_pan_channel (id, mobile, channel,
create_time)
values (#{id,jdbcType=INTEGER}, #{mobile,jdbcType=VARCHAR}, #{channel,jdbcType=TINYINT},
#{createTime,jdbcType=INTEGER})
</insert>
<insert id="insertSelective" parameterType="com.yoho.unions.dal.model.MobilePanChannel" >
insert into mobile_pan_channel
<trim prefix="(" suffix=")" suffixOverrides="," >
<if test="id != null" >
id,
</if>
<if test="mobile != null" >
mobile,
</if>
<if test="channel != null" >
channel,
</if>
<if test="createTime != null" >
create_time,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides="," >
<if test="id != null" >
#{id,jdbcType=INTEGER},
</if>
<if test="mobile != null" >
#{mobile,jdbcType=VARCHAR},
</if>
<if test="channel != null" >
#{channel,jdbcType=TINYINT},
</if>
<if test="createTime != null" >
#{createTime,jdbcType=INTEGER},
</if>
</trim>
</insert>
<update id="updateByPrimaryKeySelective" parameterType="com.yoho.unions.dal.model.MobilePanChannel" >
update mobile_pan_channel
<set >
<if test="mobile != null" >
mobile = #{mobile,jdbcType=VARCHAR},
</if>
<if test="channel != null" >
channel = #{channel,jdbcType=TINYINT},
</if>
<if test="createTime != null" >
create_time = #{createTime,jdbcType=INTEGER},
</if>
</set>
where id = #{id,jdbcType=INTEGER}
</update>
<update id="updateByPrimaryKey" parameterType="com.yoho.unions.dal.model.MobilePanChannel" >
update mobile_pan_channel
set mobile = #{mobile,jdbcType=VARCHAR},
channel = #{channel,jdbcType=TINYINT},
create_time = #{createTime,jdbcType=INTEGER}
where id = #{id,jdbcType=INTEGER}
</update>
</mapper>
\ No newline at end of file
... ...
package com.yoho.unions.server.restapi;
import com.yoho.unions.server.service.IPayChannelService;
import com.yoho.unions.vo.ApiResponse;
import com.yoho.unions.vo.ChannelTypeReqVO;
import com.yoho.unions.vo.ChannelTypeVO;
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.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
/**
* Created by yoho on 2017/3/2.
*/
@Controller
@RequestMapping("/PayChannelRest")
public class PayChannelRest {
Logger logger = LoggerFactory.getLogger(PayChannelRest.class);
@Autowired
IPayChannelService payChannelService;
@RequestMapping("/getChannelType")
@ResponseBody
public ApiResponse getChannelType(ChannelTypeReqVO unionReqVO) {
logger.info("Enter PayChannelRest.getChannelType unionReqVO {} ", unionReqVO);
ChannelTypeVO channelTypeBO = payChannelService.getChannelType(unionReqVO);
if (StringUtils.isNotBlank(channelTypeBO.getChannelTypeName())){
return new ApiResponse.ApiResponseBuilder().data(channelTypeBO).build();
}else{
return new ApiResponse.ApiResponseBuilder().code(401).build();
}
}
}
... ...
package com.yoho.unions.server.service;
import com.yoho.unions.vo.ChannelTypeReqVO;
import com.yoho.unions.vo.ChannelTypeVO;
import com.yoho.unions.vo.UnionReqVO;
/**
* Created by yoho on 2017/3/2.
*/
public interface IPayChannelService {
/**
* 判断是否是否付费渠道
* @param unionReqVO
* @return
*/
public ChannelTypeVO getChannelType(ChannelTypeReqVO unionReqVO);
}
... ...
package com.yoho.unions.server.service.impl;
import com.yoho.unions.common.redis.RedisHashCache;
import com.yoho.unions.common.redis.RedisValueCache;
import com.yoho.unions.dal.IMobilePanChannelDAO;
import com.yoho.unions.dal.model.MobilePanChannel;
import com.yoho.unions.server.service.IPayChannelService;
import com.yoho.unions.vo.ChannelTypeReqVO;
import com.yoho.unions.vo.ChannelTypeVO;
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;
import java.util.concurrent.TimeUnit;
/**
* Created by yoho on 2017/3/2.
*/
@Service
public class PayChannelServiceImpl implements IPayChannelService {
static Logger logger = LoggerFactory.getLogger(PayChannelServiceImpl.class);
@Autowired
IMobilePanChannelDAO mobilePanChannelDAO;
@Autowired
RedisHashCache redisHashCache;
@Autowired
RedisValueCache redisValueCache;
private static final String UNION_PAY_CHANNEL_KEY_PRE = "union:pay_channel:";
/**
* 判断是否是否付费渠道
* @param unionReqVO
* @return
*/
public ChannelTypeVO getChannelType(ChannelTypeReqVO unionReqVO){
ChannelTypeVO channelTypeBO = new ChannelTypeVO();
// (1)判断是否是 付费渠道
if (StringUtils.isNotBlank(unionReqVO.getUdid())){
String redisKey = UNION_PAY_CHANNEL_KEY_PRE + unionReqVO.getUdid();
String payChannel= redisValueCache.get(redisKey, String.class);
if(StringUtils.isNotBlank(payChannel)){
channelTypeBO.setChannelTypeName("paychannel");
}
}
// (2)判断是否是 泛渠道
if (StringUtils.isEmpty(channelTypeBO.getChannelTypeName())
&& StringUtils.isNotBlank(unionReqVO.getMobile())){
MobilePanChannel mobilePanChannel = mobilePanChannelDAO.selectByMobile(unionReqVO.getMobile());
if (mobilePanChannel != null){
channelTypeBO.setChannelTypeName("panchannel");
}
}
return channelTypeBO;
}
}
... ...
... ... @@ -51,5 +51,6 @@ datasources:
- com.yoho.unions.dal.IChannelGroupConditionDAO
- com.yoho.unions.dal.IChannelGroupBatchDAO
- com.yoho.unions.dal.IChannelSmsBlackDAO
- com.yoho.unions.dal.IMobilePanChannelDAO
readOnlyInSlave: true
\ No newline at end of file
... ...
... ... @@ -52,5 +52,6 @@ datasources:
- com.yoho.unions.dal.IChannelGroupConditionDAO
- com.yoho.unions.dal.IChannelGroupBatchDAO
- com.yoho.unions.dal.IChannelSmsBlackDAO
- com.yoho.unions.dal.IMobilePanChannelDAO
readOnlyInSlave: true
\ No newline at end of file
... ...