Authored by tanling

拦截黑名单

package com.yohoufo.dal.order;
import com.yohoufo.dal.order.model.BlackUser;
import org.apache.ibatis.annotations.Param;
import java.util.List;
public interface BlackUserMapper {
int deleteByPrimaryKey(Integer id);
int insert(BlackUser record);
int insertSelective(BlackUser record);
BlackUser selectByPrimaryKey(Integer id);
List<BlackUser> selectByUid(@Param("uid") Integer uid);
int updateByPrimaryKeySelective(BlackUser record);
int updateByPrimaryKey(BlackUser record);
}
\ No newline at end of file
... ...
package com.yohoufo.dal.order.model;
public class BlackUser {
private Integer id;
private Integer uid;
private Integer blackType;
private Integer createTime;
private String reason;
private Integer updateTime;
private Integer status;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getUid() {
return uid;
}
public void setUid(Integer uid) {
this.uid = uid;
}
public Integer getBlackType() {
return blackType;
}
public void setBlackType(Integer blackType) {
this.blackType = blackType;
}
public Integer getCreateTime() {
return createTime;
}
public void setCreateTime(Integer createTime) {
this.createTime = createTime;
}
public String getReason() {
return reason;
}
public void setReason(String reason) {
this.reason = reason == null ? null : reason.trim();
}
public Integer getUpdateTime() {
return updateTime;
}
public void setUpdateTime(Integer updateTime) {
this.updateTime = updateTime;
}
public Integer getStatus() {
return status;
}
public void setStatus(Integer status) {
this.status = status;
}
}
\ 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.yohoufo.dal.order.BlackUserMapper" >
<resultMap id="BaseResultMap" type="com.yohoufo.dal.order.model.BlackUser" >
<id column="id" property="id" jdbcType="INTEGER" />
<result column="uid" property="uid" jdbcType="INTEGER" />
<result column="black_type" property="blackType" jdbcType="INTEGER" />
<result column="create_time" property="createTime" jdbcType="INTEGER" />
<result column="reason" property="reason" jdbcType="VARCHAR" />
<result column="update_time" property="updateTime" jdbcType="INTEGER" />
<result column="status" property="status" jdbcType="INTEGER" />
</resultMap>
<sql id="Base_Column_List" >
id, uid, black_type, create_time, reason, update_time, status
</sql>
<select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
select
<include refid="Base_Column_List" />
from black_user
where id = #{id,jdbcType=INTEGER}
</select>
<select id="selectByUid" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
select
<include refid="Base_Column_List" />
from black_user
where uid = #{uid,jdbcType=INTEGER}
</select>
<delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
delete from black_user
where id = #{id,jdbcType=INTEGER}
</delete>
<insert id="insert" parameterType="com.yohoufo.dal.order.model.BlackUser" >
insert into black_user (id, uid, black_type,
create_time, reason, update_time,
status)
values (#{id,jdbcType=INTEGER}, #{uid,jdbcType=INTEGER}, #{blackType,jdbcType=INTEGER},
#{createTime,jdbcType=INTEGER}, #{reason,jdbcType=VARCHAR}, #{updateTime,jdbcType=INTEGER},
#{status,jdbcType=INTEGER})
</insert>
<insert id="insertSelective" parameterType="com.yohoufo.dal.order.model.BlackUser" >
insert into black_user
<trim prefix="(" suffix=")" suffixOverrides="," >
<if test="id != null" >
id,
</if>
<if test="uid != null" >
uid,
</if>
<if test="blackType != null" >
black_type,
</if>
<if test="createTime != null" >
create_time,
</if>
<if test="reason != null" >
reason,
</if>
<if test="updateTime != null" >
update_time,
</if>
<if test="status != null" >
status,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides="," >
<if test="id != null" >
#{id,jdbcType=INTEGER},
</if>
<if test="uid != null" >
#{uid,jdbcType=INTEGER},
</if>
<if test="blackType != null" >
#{blackType,jdbcType=INTEGER},
</if>
<if test="createTime != null" >
#{createTime,jdbcType=INTEGER},
</if>
<if test="reason != null" >
#{reason,jdbcType=VARCHAR},
</if>
<if test="updateTime != null" >
#{updateTime,jdbcType=INTEGER},
</if>
<if test="status != null" >
#{status,jdbcType=INTEGER},
</if>
</trim>
</insert>
<update id="updateByPrimaryKeySelective" parameterType="com.yohoufo.dal.order.model.BlackUser" >
update black_user
<set >
<if test="uid != null" >
uid = #{uid,jdbcType=INTEGER},
</if>
<if test="blackType != null" >
black_type = #{blackType,jdbcType=INTEGER},
</if>
<if test="createTime != null" >
create_time = #{createTime,jdbcType=INTEGER},
</if>
<if test="reason != null" >
reason = #{reason,jdbcType=VARCHAR},
</if>
<if test="updateTime != null" >
update_time = #{updateTime,jdbcType=INTEGER},
</if>
<if test="status != null" >
status = #{status,jdbcType=INTEGER},
</if>
</set>
where id = #{id,jdbcType=INTEGER}
</update>
<update id="updateByPrimaryKey" parameterType="com.yohoufo.dal.order.model.BlackUser" >
update black_user
set uid = #{uid,jdbcType=INTEGER},
black_type = #{blackType,jdbcType=INTEGER},
create_time = #{createTime,jdbcType=INTEGER},
reason = #{reason,jdbcType=VARCHAR},
update_time = #{updateTime,jdbcType=INTEGER},
status = #{status,jdbcType=INTEGER}
where id = #{id,jdbcType=INTEGER}
</update>
</mapper>
\ No newline at end of file
... ...
package com.yohoufo.order.common;
public enum BlackTypeEnum {
SELL(1, "禁止售出"),
BUY(2, "禁止购入");
int code;
String desc;
BlackTypeEnum(int code, String desc) {
this.code = code;
this.desc = desc;
}
public int getCode() {
return code;
}
@Override
public String toString() {
return "BlackTypeEnum{" +
"code=" + code +
", desc='" + desc + '\'' +
'}';
}
}
... ...
... ... @@ -10,6 +10,8 @@ import com.yohoufo.common.annotation.IgnoreSignature;
import com.yohoufo.common.exception.GatewayException;
import com.yohoufo.common.exception.UfoServiceException;
import com.yohoufo.dal.order.model.SellerOrder;
import com.yohoufo.order.common.BlackTypeEnum;
import com.yohoufo.order.interceptor.BlackUserInterceptor;
import com.yohoufo.order.model.request.OrderListRequest;
import com.yohoufo.order.model.request.OrderRequest;
import com.yohoufo.order.model.response.OrderSubmitResp;
... ... @@ -105,6 +107,9 @@ public class SellerOrderController {
.skupType(skupType)
.build();
logger.info("in ufo.sellerOrder.publishPrd, req {}", req);
BlackUserInterceptor.getBlackUserInteceptor(BlackTypeEnum.SELL.getCode()).before(uid);
OrderSubmitResp resp = sellerOrderService.publishPrd(req);
return new ApiResponse.ApiResponseBuilder().data(resp).code(200).message("发布成功").build();
}
... ... @@ -542,6 +547,9 @@ public class SellerOrderController {
.imageList(imageList)
.build();
logger.info("in ufo.sellerOrder.publishImperfectPrd, req {}", req);
BlackUserInterceptor.getBlackUserInteceptor(BlackTypeEnum.SELL.getCode()).before(uid);
OrderSubmitResp resp = imperfectGoodsService.publish(req);
return new ApiResponse.ApiResponseBuilder().data(resp).code(200).message("发布成功").build();
}
... ...
package com.yohoufo.order.interceptor;
import com.google.common.collect.Maps;
import com.yoho.error.ServiceError;
import com.yoho.error.exception.ServiceException;
import com.yohoufo.dal.order.BlackUserMapper;
import com.yohoufo.dal.order.model.BlackUser;
import com.yohoufo.order.common.BlackTypeEnum;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
public abstract class BlackUserInterceptor implements ApplicationContextAware {
Logger logger = LoggerFactory.getLogger(BlackUserInterceptor.class);
@Autowired
BlackUserMapper blackUserMapper;
public void before(int uid){
if (uid<0){
logger.warn("uid null");
return;
}
List<BlackUser> blackUserList = blackUserMapper.selectByUid(uid);
blackUserList = blackUserList.stream().filter(x->{
if(x.getBlackType() == getBlackType() && x.getStatus() == 1){
return true;
}
return false;
}).collect(Collectors.toList());
// 黑名单向外抛出异常
if (CollectionUtils.isNotEmpty(blackUserList)){
logger.warn("not allow black user buy .uid is {}", uid);
throw getServiceException();
}
}
abstract int getBlackType();
private static final Map<Integer, BlackUserInterceptor> blackUserInteceptorMap = Maps.newHashMap();
public static BlackUserInterceptor getBlackUserInteceptor(Integer code){
return blackUserInteceptorMap.get(code);
}
abstract ServiceException getServiceException();
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
// 重构map中的拦截器key,value
Map<String, BlackUserInterceptor> map = applicationContext.getBeansOfType(BlackUserInterceptor.class);
for (String beanName: map.keySet()){
BlackUserInterceptor interceptor = (BlackUserInterceptor) applicationContext.getBean(beanName);
blackUserInteceptorMap.put(interceptor.getBlackType(), interceptor);
}
}
}
... ...
package com.yohoufo.order.interceptor;
import com.yoho.error.ServiceError;
import com.yoho.error.exception.ServiceException;
import com.yohoufo.order.common.BlackTypeEnum;
import org.springframework.stereotype.Component;
@Component
public class BuyBlackUserInterceptor extends BlackUserInterceptor {
@Override
int getBlackType() {
return BlackTypeEnum.BUY.getCode();
}
@Override
ServiceException getServiceException() {
return new ServiceException(ServiceError.NOT_ALLOW_BLACK_UID_BUY);
}
}
\ No newline at end of file
... ...
package com.yohoufo.order.interceptor;
import com.yoho.error.ServiceError;
import com.yoho.error.exception.ServiceException;
import com.yohoufo.order.common.BlackTypeEnum;
import org.springframework.stereotype.Component;
@Component
public class SellBlackUserInterceptor extends BlackUserInterceptor {
@Override
int getBlackType() {
return BlackTypeEnum.SELL.getCode();
}
@Override
ServiceException getServiceException() {
return new ServiceException(ServiceError.NOT_ALLOW_BLACK_UID_SELL);
}
}
\ No newline at end of file
... ...
... ... @@ -27,10 +27,12 @@ import com.yohoufo.order.charge.model.ChargeGoods;
import com.yohoufo.order.charge.model.ChargeParam;
import com.yohoufo.order.charge.model.ChargeResult;
import com.yohoufo.order.charge.model.CouponMatchResult;
import com.yohoufo.order.common.BlackTypeEnum;
import com.yohoufo.order.event.BuyerCancelEvent;
import com.yohoufo.order.event.ErpBuyerOrderEvent;
import com.yohoufo.order.event.NotPaidNoticeEvent;
import com.yohobuy.ufo.model.order.vo.AddressInfo;
import com.yohoufo.order.interceptor.BlackUserInterceptor;
import com.yohoufo.order.model.bo.CouponBo;
import com.yohoufo.order.model.dto.BuyerOrderSubmitResult;
import com.yohoufo.order.model.dto.OrderBuilder;
... ... @@ -272,6 +274,10 @@ public class ShoppingServiceImpl implements IShoppingService {
logger.warn("submit param is null");
throw new ServiceException(ServiceError.ORDER_REQUEST_PARM_IS_EMPTY);
}
// 黑名单拦截
BlackUserInterceptor.getBlackUserInteceptor(BlackTypeEnum.BUY.getCode()).before(uid);
shoppingRiskWatchDog.checkWaitingPayCnt(uid);
//查询并校验用户地址
Pair<AddressInfo, AddressInfo> userAddressPair = getAndCheckAddressInfo(shoppingRequest);
... ...
... ... @@ -6,7 +6,7 @@
<parent>
<groupId>com.yoho</groupId>
<artifactId>parent</artifactId>
<version>1.5.7-SNAPSHOT</version>
<version>1.5.8-SNAPSHOT</version>
</parent>
<groupId>com.yohoufo.fore</groupId>
... ... @@ -18,7 +18,7 @@
<properties>
<qiniu.version>7.0.5</qiniu.version>
<project-name>yohoufo-fore</project-name>
<model.version>12.0-SNAPSHOT</model.version>
<model.version>13.0-SNAPSHOT</model.version>
<lombok.version>1.16.16</lombok.version>
</properties>
... ...