Authored by LUOXC

黑名单限制

package com.yoho.order.dal;
import com.yoho.order.model.BlackUser;
import org.apache.ibatis.annotations.Param;
import java.util.List;
public interface BlackUserMapper {
int insert(@Param("records") List<BlackUser> records);
int deleteByUid(@Param("uid") Integer uid);
List<BlackUser> selectByUid(@Param("uid") Integer uid);
int deleteByUidsAndBlackType(@Param("uids") List<Integer> uids, @Param("blackType") Integer blackType);
}
\ No newline at end of file
... ...
package com.yoho.order.model;
import lombok.Data;
import java.util.Arrays;
import java.util.List;
@Data
public class BlackUser {
/**
* 创建买家订单
*/
public static final Integer BLACK_TYPE_CREATE_BUYER_ORDER = 2;
/**
* 创建卖家订单
*/
public static final Integer BLACK_TYPE_CREATE_SELLER_ORDER = 1;
public static final List<Integer> ALL_BLACK_TYPE = Arrays.asList(BLACK_TYPE_CREATE_SELLER_ORDER, BLACK_TYPE_CREATE_BUYER_ORDER);
/**
* 有效的
*/
public static Integer STATUS_ENABLE = 1;
private Integer id;
private Integer uid;
private Integer blackType;
private Integer createTime;
private String reason;
private Integer updateTime;
private Integer 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.yoho.order.dal.BlackUserMapper" >
<resultMap id="BaseResultMap" type="com.yoho.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="selectByUid" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from black_user
where uid = #{uid,jdbcType=INTEGER}
</select>
<delete id="deleteByUid">
delete from black_user
where uid = #{uid,jdbcType=INTEGER}
</delete>
<delete id="deleteByUidsAndBlackType">
delete from black_user
where uid in
<foreach collection="uids" item="uid" open="(" close=")" separator=",">
#{uid,jdbcType=INTEGER}
</foreach>
and blackType = #{blackType,jdbcType=INTEGER}
</delete>
<insert id="insert">
insert into black_user (uid, black_type, create_time, reason, update_time, status)
values
<foreach collection="records" item="record" separator=",">
(#{record.uid,jdbcType=INTEGER}, #{record.blackType,jdbcType=INTEGER}, #{record.createTime,jdbcType=INTEGER},
#{record.reason,jdbcType=VARCHAR}, #{record.updateTime,jdbcType=INTEGER}, #{record.status,jdbcType=INTEGER})
</foreach>
</insert>
</mapper>
\ No newline at end of file
... ...
package com.yoho.ufo.order.controller;
import com.google.common.base.Splitter;
import com.yoho.order.dal.BlackUserMapper;
import com.yoho.order.model.BlackUser;
import com.yoho.ufo.service.model.ApiResponse;
import com.yoho.ufo.util.DateUtil;
import lombok.val;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Objects;
import java.util.stream.Collectors;
@RestController
@RequestMapping(value = "/blackUser")
public class BlackUserController {
@Autowired
private BlackUserMapper blackUserMapper;
@RequestMapping(value = "/create")
public ApiResponse create(String uids, Integer blackType, String reason) {
if (StringUtils.isBlank(uids)) {
return new ApiResponse.ApiResponseBuilder().code(400).message("用户账户无效").build();
}
if (StringUtils.isBlank(reason)) {
return new ApiResponse.ApiResponseBuilder().code(400).message("请输入拉黑原因").build();
}
if (!BlackUser.ALL_BLACK_TYPE.contains(blackType)) {
return new ApiResponse.ApiResponseBuilder().code(400).message("无效的类型").build();
}
val blackUids = Splitter.on(",").splitToList(uids).stream()
.filter(StringUtils::isNoneBlank)
.map(Integer::valueOf)
.distinct()
.limit(100)
.collect(Collectors.toList());
if (CollectionUtils.isEmpty(blackUids)) {
return new ApiResponse.ApiResponseBuilder().code(400).message("没有可加的用户").build();
}
blackUserMapper.deleteByUidsAndBlackType(blackUids, blackType);
val blackUsers = blackUids.stream()
.map(uid -> {
BlackUser blackUser = new BlackUser();
blackUser.setUid(uid);
blackUser.setBlackType(blackType);
blackUser.setReason(reason);
blackUser.setCreateTime(DateUtil.getCurrentTimeSeconds());
blackUser.setStatus(BlackUser.STATUS_ENABLE);
blackUser.setUpdateTime(blackUser.getCreateTime());
return blackUser;
})
.collect(Collectors.toList());
blackUserMapper.insert(blackUsers);
return new ApiResponse.ApiResponseBuilder().build();
}
@RequestMapping(value = "/remove")
public ApiResponse remove(Integer uid) {
if (Objects.isNull(uid)) {
return new ApiResponse.ApiResponseBuilder().code(400).message("用户账户无效").build();
}
blackUserMapper.deleteByUid(uid);
return new ApiResponse.ApiResponseBuilder().build();
}
@RequestMapping(value = "/check")
public ApiResponse check(Integer uid) {
if (Objects.isNull(uid)) {
return new ApiResponse.ApiResponseBuilder().code(400).message("用户账户无效").build();
}
String message = blackUserMapper.selectByUid(uid).stream()
.filter(e -> e.getStatus().equals(BlackUser.STATUS_ENABLE))
.map(BlackUser::getBlackType)
.map(blackType -> {
if (BlackUser.BLACK_TYPE_CREATE_BUYER_ORDER.equals(blackType)) {
return "限制购买";
}
if (BlackUser.BLACK_TYPE_CREATE_SELLER_ORDER.equals(blackType)) {
return "限制出售";
}
return null;
})
.filter(Objects::nonNull)
.reduce("", (a, b) -> a + "," + b);
if (StringUtils.isBlank(message)) {
return new ApiResponse.ApiResponseBuilder().code(200).message("无限制").build();
} else {
return new ApiResponse.ApiResponseBuilder().message(message).build();
}
}
}
... ...