Authored by tanling

拦截黑名单

  1 +package com.yohoufo.dal.order;
  2 +
  3 +
  4 +import com.yohoufo.dal.order.model.BlackUser;
  5 +import org.apache.ibatis.annotations.Param;
  6 +
  7 +import java.util.List;
  8 +
  9 +public interface BlackUserMapper {
  10 + int deleteByPrimaryKey(Integer id);
  11 +
  12 + int insert(BlackUser record);
  13 +
  14 + int insertSelective(BlackUser record);
  15 +
  16 + BlackUser selectByPrimaryKey(Integer id);
  17 +
  18 + List<BlackUser> selectByUid(@Param("uid") Integer uid);
  19 +
  20 + int updateByPrimaryKeySelective(BlackUser record);
  21 +
  22 + int updateByPrimaryKey(BlackUser record);
  23 +}
  1 +package com.yohoufo.dal.order.model;
  2 +
  3 +public class BlackUser {
  4 + private Integer id;
  5 +
  6 + private Integer uid;
  7 +
  8 + private Integer blackType;
  9 +
  10 + private Integer createTime;
  11 +
  12 + private String reason;
  13 +
  14 + private Integer updateTime;
  15 +
  16 + private Integer status;
  17 +
  18 + public Integer getId() {
  19 + return id;
  20 + }
  21 +
  22 + public void setId(Integer id) {
  23 + this.id = id;
  24 + }
  25 +
  26 + public Integer getUid() {
  27 + return uid;
  28 + }
  29 +
  30 + public void setUid(Integer uid) {
  31 + this.uid = uid;
  32 + }
  33 +
  34 + public Integer getBlackType() {
  35 + return blackType;
  36 + }
  37 +
  38 + public void setBlackType(Integer blackType) {
  39 + this.blackType = blackType;
  40 + }
  41 +
  42 + public Integer getCreateTime() {
  43 + return createTime;
  44 + }
  45 +
  46 + public void setCreateTime(Integer createTime) {
  47 + this.createTime = createTime;
  48 + }
  49 +
  50 + public String getReason() {
  51 + return reason;
  52 + }
  53 +
  54 + public void setReason(String reason) {
  55 + this.reason = reason == null ? null : reason.trim();
  56 + }
  57 +
  58 + public Integer getUpdateTime() {
  59 + return updateTime;
  60 + }
  61 +
  62 + public void setUpdateTime(Integer updateTime) {
  63 + this.updateTime = updateTime;
  64 + }
  65 +
  66 + public Integer getStatus() {
  67 + return status;
  68 + }
  69 +
  70 + public void setStatus(Integer status) {
  71 + this.status = status;
  72 + }
  73 +}
  1 +<?xml version="1.0" encoding="UTF-8" ?>
  2 +<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
  3 +<mapper namespace="com.yohoufo.dal.order.BlackUserMapper" >
  4 + <resultMap id="BaseResultMap" type="com.yohoufo.dal.order.model.BlackUser" >
  5 + <id column="id" property="id" jdbcType="INTEGER" />
  6 + <result column="uid" property="uid" jdbcType="INTEGER" />
  7 + <result column="black_type" property="blackType" jdbcType="INTEGER" />
  8 + <result column="create_time" property="createTime" jdbcType="INTEGER" />
  9 + <result column="reason" property="reason" jdbcType="VARCHAR" />
  10 + <result column="update_time" property="updateTime" jdbcType="INTEGER" />
  11 + <result column="status" property="status" jdbcType="INTEGER" />
  12 + </resultMap>
  13 + <sql id="Base_Column_List" >
  14 + id, uid, black_type, create_time, reason, update_time, status
  15 + </sql>
  16 + <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
  17 + select
  18 + <include refid="Base_Column_List" />
  19 + from black_user
  20 + where id = #{id,jdbcType=INTEGER}
  21 + </select>
  22 +
  23 + <select id="selectByUid" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
  24 + select
  25 + <include refid="Base_Column_List" />
  26 + from black_user
  27 + where uid = #{uid,jdbcType=INTEGER}
  28 + </select>
  29 +
  30 +
  31 + <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
  32 + delete from black_user
  33 + where id = #{id,jdbcType=INTEGER}
  34 + </delete>
  35 + <insert id="insert" parameterType="com.yohoufo.dal.order.model.BlackUser" >
  36 + insert into black_user (id, uid, black_type,
  37 + create_time, reason, update_time,
  38 + status)
  39 + values (#{id,jdbcType=INTEGER}, #{uid,jdbcType=INTEGER}, #{blackType,jdbcType=INTEGER},
  40 + #{createTime,jdbcType=INTEGER}, #{reason,jdbcType=VARCHAR}, #{updateTime,jdbcType=INTEGER},
  41 + #{status,jdbcType=INTEGER})
  42 + </insert>
  43 + <insert id="insertSelective" parameterType="com.yohoufo.dal.order.model.BlackUser" >
  44 + insert into black_user
  45 + <trim prefix="(" suffix=")" suffixOverrides="," >
  46 + <if test="id != null" >
  47 + id,
  48 + </if>
  49 + <if test="uid != null" >
  50 + uid,
  51 + </if>
  52 + <if test="blackType != null" >
  53 + black_type,
  54 + </if>
  55 + <if test="createTime != null" >
  56 + create_time,
  57 + </if>
  58 + <if test="reason != null" >
  59 + reason,
  60 + </if>
  61 + <if test="updateTime != null" >
  62 + update_time,
  63 + </if>
  64 + <if test="status != null" >
  65 + status,
  66 + </if>
  67 + </trim>
  68 + <trim prefix="values (" suffix=")" suffixOverrides="," >
  69 + <if test="id != null" >
  70 + #{id,jdbcType=INTEGER},
  71 + </if>
  72 + <if test="uid != null" >
  73 + #{uid,jdbcType=INTEGER},
  74 + </if>
  75 + <if test="blackType != null" >
  76 + #{blackType,jdbcType=INTEGER},
  77 + </if>
  78 + <if test="createTime != null" >
  79 + #{createTime,jdbcType=INTEGER},
  80 + </if>
  81 + <if test="reason != null" >
  82 + #{reason,jdbcType=VARCHAR},
  83 + </if>
  84 + <if test="updateTime != null" >
  85 + #{updateTime,jdbcType=INTEGER},
  86 + </if>
  87 + <if test="status != null" >
  88 + #{status,jdbcType=INTEGER},
  89 + </if>
  90 + </trim>
  91 + </insert>
  92 + <update id="updateByPrimaryKeySelective" parameterType="com.yohoufo.dal.order.model.BlackUser" >
  93 + update black_user
  94 + <set >
  95 + <if test="uid != null" >
  96 + uid = #{uid,jdbcType=INTEGER},
  97 + </if>
  98 + <if test="blackType != null" >
  99 + black_type = #{blackType,jdbcType=INTEGER},
  100 + </if>
  101 + <if test="createTime != null" >
  102 + create_time = #{createTime,jdbcType=INTEGER},
  103 + </if>
  104 + <if test="reason != null" >
  105 + reason = #{reason,jdbcType=VARCHAR},
  106 + </if>
  107 + <if test="updateTime != null" >
  108 + update_time = #{updateTime,jdbcType=INTEGER},
  109 + </if>
  110 + <if test="status != null" >
  111 + status = #{status,jdbcType=INTEGER},
  112 + </if>
  113 + </set>
  114 + where id = #{id,jdbcType=INTEGER}
  115 + </update>
  116 + <update id="updateByPrimaryKey" parameterType="com.yohoufo.dal.order.model.BlackUser" >
  117 + update black_user
  118 + set uid = #{uid,jdbcType=INTEGER},
  119 + black_type = #{blackType,jdbcType=INTEGER},
  120 + create_time = #{createTime,jdbcType=INTEGER},
  121 + reason = #{reason,jdbcType=VARCHAR},
  122 + update_time = #{updateTime,jdbcType=INTEGER},
  123 + status = #{status,jdbcType=INTEGER}
  124 + where id = #{id,jdbcType=INTEGER}
  125 + </update>
  126 +</mapper>
  1 +package com.yohoufo.order.common;
  2 +
  3 +public enum BlackTypeEnum {
  4 +
  5 + SELL(1, "禁止售出"),
  6 + BUY(2, "禁止购入");
  7 +
  8 + int code;
  9 +
  10 + String desc;
  11 +
  12 + BlackTypeEnum(int code, String desc) {
  13 + this.code = code;
  14 + this.desc = desc;
  15 + }
  16 +
  17 + public int getCode() {
  18 + return code;
  19 + }
  20 +
  21 + @Override
  22 + public String toString() {
  23 + return "BlackTypeEnum{" +
  24 + "code=" + code +
  25 + ", desc='" + desc + '\'' +
  26 + '}';
  27 + }
  28 +}
@@ -10,6 +10,8 @@ import com.yohoufo.common.annotation.IgnoreSignature; @@ -10,6 +10,8 @@ import com.yohoufo.common.annotation.IgnoreSignature;
10 import com.yohoufo.common.exception.GatewayException; 10 import com.yohoufo.common.exception.GatewayException;
11 import com.yohoufo.common.exception.UfoServiceException; 11 import com.yohoufo.common.exception.UfoServiceException;
12 import com.yohoufo.dal.order.model.SellerOrder; 12 import com.yohoufo.dal.order.model.SellerOrder;
  13 +import com.yohoufo.order.common.BlackTypeEnum;
  14 +import com.yohoufo.order.interceptor.BlackUserInterceptor;
13 import com.yohoufo.order.model.request.OrderListRequest; 15 import com.yohoufo.order.model.request.OrderListRequest;
14 import com.yohoufo.order.model.request.OrderRequest; 16 import com.yohoufo.order.model.request.OrderRequest;
15 import com.yohoufo.order.model.response.OrderSubmitResp; 17 import com.yohoufo.order.model.response.OrderSubmitResp;
@@ -105,6 +107,9 @@ public class SellerOrderController { @@ -105,6 +107,9 @@ public class SellerOrderController {
105 .skupType(skupType) 107 .skupType(skupType)
106 .build(); 108 .build();
107 logger.info("in ufo.sellerOrder.publishPrd, req {}", req); 109 logger.info("in ufo.sellerOrder.publishPrd, req {}", req);
  110 +
  111 + BlackUserInterceptor.getBlackUserInteceptor(BlackTypeEnum.SELL.getCode()).before(uid);
  112 +
108 OrderSubmitResp resp = sellerOrderService.publishPrd(req); 113 OrderSubmitResp resp = sellerOrderService.publishPrd(req);
109 return new ApiResponse.ApiResponseBuilder().data(resp).code(200).message("发布成功").build(); 114 return new ApiResponse.ApiResponseBuilder().data(resp).code(200).message("发布成功").build();
110 } 115 }
@@ -542,6 +547,9 @@ public class SellerOrderController { @@ -542,6 +547,9 @@ public class SellerOrderController {
542 .imageList(imageList) 547 .imageList(imageList)
543 .build(); 548 .build();
544 logger.info("in ufo.sellerOrder.publishImperfectPrd, req {}", req); 549 logger.info("in ufo.sellerOrder.publishImperfectPrd, req {}", req);
  550 +
  551 + BlackUserInterceptor.getBlackUserInteceptor(BlackTypeEnum.SELL.getCode()).before(uid);
  552 +
545 OrderSubmitResp resp = imperfectGoodsService.publish(req); 553 OrderSubmitResp resp = imperfectGoodsService.publish(req);
546 return new ApiResponse.ApiResponseBuilder().data(resp).code(200).message("发布成功").build(); 554 return new ApiResponse.ApiResponseBuilder().data(resp).code(200).message("发布成功").build();
547 } 555 }
  1 +package com.yohoufo.order.interceptor;
  2 +
  3 +import com.google.common.collect.Maps;
  4 +import com.yoho.error.ServiceError;
  5 +import com.yoho.error.exception.ServiceException;
  6 +import com.yohoufo.dal.order.BlackUserMapper;
  7 +import com.yohoufo.dal.order.model.BlackUser;
  8 +import com.yohoufo.order.common.BlackTypeEnum;
  9 +import org.apache.commons.collections.CollectionUtils;
  10 +import org.slf4j.Logger;
  11 +import org.slf4j.LoggerFactory;
  12 +import org.springframework.beans.BeansException;
  13 +import org.springframework.beans.factory.BeanNameAware;
  14 +import org.springframework.beans.factory.annotation.Autowired;
  15 +import org.springframework.context.ApplicationContext;
  16 +import org.springframework.context.ApplicationContextAware;
  17 +import org.springframework.stereotype.Component;
  18 +
  19 +import java.util.List;
  20 +import java.util.Map;
  21 +import java.util.stream.Collectors;
  22 +
  23 +public abstract class BlackUserInterceptor implements ApplicationContextAware {
  24 +
  25 + Logger logger = LoggerFactory.getLogger(BlackUserInterceptor.class);
  26 +
  27 + @Autowired
  28 + BlackUserMapper blackUserMapper;
  29 +
  30 + public void before(int uid){
  31 +
  32 + if (uid<0){
  33 + logger.warn("uid null");
  34 + return;
  35 + }
  36 +
  37 + List<BlackUser> blackUserList = blackUserMapper.selectByUid(uid);
  38 + blackUserList = blackUserList.stream().filter(x->{
  39 + if(x.getBlackType() == getBlackType() && x.getStatus() == 1){
  40 + return true;
  41 + }
  42 + return false;
  43 + }).collect(Collectors.toList());
  44 +
  45 + // 黑名单向外抛出异常
  46 + if (CollectionUtils.isNotEmpty(blackUserList)){
  47 + logger.warn("not allow black user buy .uid is {}", uid);
  48 + throw getServiceException();
  49 + }
  50 + }
  51 +
  52 + abstract int getBlackType();
  53 +
  54 +
  55 + private static final Map<Integer, BlackUserInterceptor> blackUserInteceptorMap = Maps.newHashMap();
  56 +
  57 + public static BlackUserInterceptor getBlackUserInteceptor(Integer code){
  58 + return blackUserInteceptorMap.get(code);
  59 + }
  60 +
  61 + abstract ServiceException getServiceException();
  62 +
  63 +
  64 + @Override
  65 + public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
  66 + // 重构map中的拦截器key,value
  67 + Map<String, BlackUserInterceptor> map = applicationContext.getBeansOfType(BlackUserInterceptor.class);
  68 + for (String beanName: map.keySet()){
  69 + BlackUserInterceptor interceptor = (BlackUserInterceptor) applicationContext.getBean(beanName);
  70 + blackUserInteceptorMap.put(interceptor.getBlackType(), interceptor);
  71 + }
  72 +
  73 + }
  74 +
  75 +}
  76 +
  77 +
  78 +
  79 +
  1 +package com.yohoufo.order.interceptor;
  2 +
  3 +import com.yoho.error.ServiceError;
  4 +import com.yoho.error.exception.ServiceException;
  5 +import com.yohoufo.order.common.BlackTypeEnum;
  6 +import org.springframework.stereotype.Component;
  7 +
  8 +@Component
  9 +public class BuyBlackUserInterceptor extends BlackUserInterceptor {
  10 +
  11 + @Override
  12 + int getBlackType() {
  13 + return BlackTypeEnum.BUY.getCode();
  14 + }
  15 +
  16 + @Override
  17 + ServiceException getServiceException() {
  18 + return new ServiceException(ServiceError.NOT_ALLOW_BLACK_UID_BUY);
  19 + }
  20 +
  21 +}
  1 +package com.yohoufo.order.interceptor;
  2 +
  3 +import com.yoho.error.ServiceError;
  4 +import com.yoho.error.exception.ServiceException;
  5 +import com.yohoufo.order.common.BlackTypeEnum;
  6 +import org.springframework.stereotype.Component;
  7 +
  8 +@Component
  9 +public class SellBlackUserInterceptor extends BlackUserInterceptor {
  10 +
  11 + @Override
  12 + int getBlackType() {
  13 + return BlackTypeEnum.SELL.getCode();
  14 + }
  15 +
  16 + @Override
  17 + ServiceException getServiceException() {
  18 + return new ServiceException(ServiceError.NOT_ALLOW_BLACK_UID_SELL);
  19 + }
  20 +
  21 +
  22 +}
@@ -27,10 +27,12 @@ import com.yohoufo.order.charge.model.ChargeGoods; @@ -27,10 +27,12 @@ import com.yohoufo.order.charge.model.ChargeGoods;
27 import com.yohoufo.order.charge.model.ChargeParam; 27 import com.yohoufo.order.charge.model.ChargeParam;
28 import com.yohoufo.order.charge.model.ChargeResult; 28 import com.yohoufo.order.charge.model.ChargeResult;
29 import com.yohoufo.order.charge.model.CouponMatchResult; 29 import com.yohoufo.order.charge.model.CouponMatchResult;
  30 +import com.yohoufo.order.common.BlackTypeEnum;
30 import com.yohoufo.order.event.BuyerCancelEvent; 31 import com.yohoufo.order.event.BuyerCancelEvent;
31 import com.yohoufo.order.event.ErpBuyerOrderEvent; 32 import com.yohoufo.order.event.ErpBuyerOrderEvent;
32 import com.yohoufo.order.event.NotPaidNoticeEvent; 33 import com.yohoufo.order.event.NotPaidNoticeEvent;
33 import com.yohobuy.ufo.model.order.vo.AddressInfo; 34 import com.yohobuy.ufo.model.order.vo.AddressInfo;
  35 +import com.yohoufo.order.interceptor.BlackUserInterceptor;
34 import com.yohoufo.order.model.bo.CouponBo; 36 import com.yohoufo.order.model.bo.CouponBo;
35 import com.yohoufo.order.model.dto.BuyerOrderSubmitResult; 37 import com.yohoufo.order.model.dto.BuyerOrderSubmitResult;
36 import com.yohoufo.order.model.dto.OrderBuilder; 38 import com.yohoufo.order.model.dto.OrderBuilder;
@@ -272,6 +274,10 @@ public class ShoppingServiceImpl implements IShoppingService { @@ -272,6 +274,10 @@ public class ShoppingServiceImpl implements IShoppingService {
272 logger.warn("submit param is null"); 274 logger.warn("submit param is null");
273 throw new ServiceException(ServiceError.ORDER_REQUEST_PARM_IS_EMPTY); 275 throw new ServiceException(ServiceError.ORDER_REQUEST_PARM_IS_EMPTY);
274 } 276 }
  277 +
  278 + // 黑名单拦截
  279 + BlackUserInterceptor.getBlackUserInteceptor(BlackTypeEnum.BUY.getCode()).before(uid);
  280 +
275 shoppingRiskWatchDog.checkWaitingPayCnt(uid); 281 shoppingRiskWatchDog.checkWaitingPayCnt(uid);
276 //查询并校验用户地址 282 //查询并校验用户地址
277 Pair<AddressInfo, AddressInfo> userAddressPair = getAndCheckAddressInfo(shoppingRequest); 283 Pair<AddressInfo, AddressInfo> userAddressPair = getAndCheckAddressInfo(shoppingRequest);
@@ -6,7 +6,7 @@ @@ -6,7 +6,7 @@
6 <parent> 6 <parent>
7 <groupId>com.yoho</groupId> 7 <groupId>com.yoho</groupId>
8 <artifactId>parent</artifactId> 8 <artifactId>parent</artifactId>
9 - <version>1.5.7-SNAPSHOT</version> 9 + <version>1.5.8-SNAPSHOT</version>
10 </parent> 10 </parent>
11 11
12 <groupId>com.yohoufo.fore</groupId> 12 <groupId>com.yohoufo.fore</groupId>
@@ -18,7 +18,7 @@ @@ -18,7 +18,7 @@
18 <properties> 18 <properties>
19 <qiniu.version>7.0.5</qiniu.version> 19 <qiniu.version>7.0.5</qiniu.version>
20 <project-name>yohoufo-fore</project-name> 20 <project-name>yohoufo-fore</project-name>
21 - <model.version>12.0-SNAPSHOT</model.version> 21 + <model.version>13.0-SNAPSHOT</model.version>
22 <lombok.version>1.16.16</lombok.version> 22 <lombok.version>1.16.16</lombok.version>
23 </properties> 23 </properties>
24 24