268 lines
		
	
	
		
			13 KiB
		
	
	
	
		
			Java
		
	
	
	
	
	
			
		
		
	
	
			268 lines
		
	
	
		
			13 KiB
		
	
	
	
		
			Java
		
	
	
	
	
	
| package com.upchina.common.service;
 | ||
| 
 | ||
| import cn.hutool.core.collection.CollUtil;
 | ||
| import cn.hutool.core.util.StrUtil;
 | ||
| import com.alibaba.fastjson.JSONObject;
 | ||
| import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 | ||
| import com.baomidou.mybatisplus.core.toolkit.Wrappers;
 | ||
| import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 | ||
| import com.google.common.collect.Table;
 | ||
| import com.hazelcast.core.HazelcastInstance;
 | ||
| import com.hazelcast.map.IMap;
 | ||
| import com.upchina.common.constant.CommentBlackStatus;
 | ||
| import com.upchina.common.constant.CommentBlackType;
 | ||
| import com.upchina.common.entity.CommentBlack;
 | ||
| import com.upchina.common.handler.BizException;
 | ||
| import com.upchina.common.mapper.CommentBlackMapper;
 | ||
| import com.upchina.common.query.AddCommentBlackQuery;
 | ||
| import com.upchina.common.query.BaseProductQuery;
 | ||
| import com.upchina.common.query.CommentBlackQuery;
 | ||
| import com.upchina.common.result.Pager;
 | ||
| import com.upchina.common.result.ResponseStatus;
 | ||
| import com.upchina.common.util.HideUtils;
 | ||
| import com.upchina.common.util.LoggerUtil;
 | ||
| import com.upchina.common.vo.BackendUserVO;
 | ||
| import com.upchina.common.vo.CommentBlackVO;
 | ||
| import com.upchina.common.vo.MergeProductInfoVO;
 | ||
| import com.upchina.rbac.entity.Dept;
 | ||
| import com.upchina.rbac.entity.UserDept;
 | ||
| import com.upchina.rbac.service.AuthService;
 | ||
| import com.upchina.rbac.service.DeptService;
 | ||
| import com.upchina.rbac.service.UserService;
 | ||
| import ma.glasnost.orika.MapperFacade;
 | ||
| import org.springframework.beans.factory.annotation.Value;
 | ||
| import org.springframework.stereotype.Service;
 | ||
| import org.springframework.transaction.annotation.Transactional;
 | ||
| import org.springframework.util.CollectionUtils;
 | ||
| 
 | ||
| import javax.annotation.Resource;
 | ||
| import java.time.LocalDateTime;
 | ||
| import java.time.format.DateTimeFormatter;
 | ||
| import java.util.*;
 | ||
| import java.util.stream.Collectors;
 | ||
| 
 | ||
| import static com.upchina.common.config.cache.CacheKey.COMMENT_BLACK;
 | ||
| import static com.upchina.common.config.cache.CacheKey.CommentBlackKey.ALL_BLACK_USER;
 | ||
| 
 | ||
| @Service
 | ||
| public class CommentBlackService {
 | ||
| 
 | ||
|     @Value("${rsa.priKey}")
 | ||
|     private String ypPriKey;
 | ||
| 
 | ||
|     @Value("${rsa.pubKey}")
 | ||
|     private String ypPubKey;
 | ||
| 
 | ||
|     @Resource
 | ||
|     private MapperFacade mapperFacade;
 | ||
| 
 | ||
|     @Resource
 | ||
|     private CommentBlackMapper commentBlackMapper;
 | ||
| 
 | ||
|     @Resource
 | ||
|     private UserService userService;
 | ||
| 
 | ||
|     @Resource
 | ||
|     private MergeProductService mergeProductService;
 | ||
| 
 | ||
|     @Resource
 | ||
|     private HazelcastInstance hazelcastInstance;
 | ||
| 
 | ||
|     @Resource
 | ||
|     private CacheService cacheService;
 | ||
| 
 | ||
|     @Resource
 | ||
|     private AuthService authService;
 | ||
| 
 | ||
|     @Resource
 | ||
|     private DeptService deptService;
 | ||
| 
 | ||
|     private static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
 | ||
| 
 | ||
|     public Set<String> getAllBlackUser() {
 | ||
|         LocalDateTime now = LocalDateTime.now();
 | ||
|         List<CommentBlack> list = cacheService.get(COMMENT_BLACK, ALL_BLACK_USER, () -> {
 | ||
|             QueryWrapper<CommentBlack> wrapper = Wrappers.query();
 | ||
|             wrapper.in("status", Arrays.asList(CommentBlackStatus.EFFECT.value, CommentBlackStatus.EXPIRED.value));
 | ||
|             List<CommentBlack> commentBlackList = commentBlackMapper.selectList(wrapper);
 | ||
|             LoggerUtil.info("db当前黑名单用户:" + JSONObject.toJSONString(commentBlackList));
 | ||
|             if (CollectionUtils.isEmpty(commentBlackList)) {
 | ||
|                 return null;
 | ||
|             }
 | ||
|             return commentBlackList;
 | ||
|         });
 | ||
|         if (CollUtil.isEmpty(list)) {
 | ||
|             return null;
 | ||
|         }
 | ||
|         Set<String> set = list.stream()
 | ||
|                 .filter(black -> now.isAfter(black.getStartTime()) && now.isBefore(black.getEndTime()))
 | ||
|                 .map(CommentBlack::getPhone).collect(Collectors.toSet());
 | ||
|         LoggerUtil.info("当前黑名单用户:" + JSONObject.toJSONString(set));
 | ||
|         return set;
 | ||
|     }
 | ||
| 
 | ||
|     @Transactional(rollbackFor = Exception.class)
 | ||
|     public Integer addCommentBlack(BackendUserVO backendUserVO, AddCommentBlackQuery addCommentBlackQuery) {
 | ||
| //        String userPhone = RsaUtil.priKeyDecryption(ypPriKey, addCommentBlackQuery.getUserPhone());
 | ||
|         String userPhone = addCommentBlackQuery.getUserPhone();
 | ||
|         LocalDateTime now = LocalDateTime.now();
 | ||
|         //禁言开始时间-1s
 | ||
|         now = now.minusSeconds(1L);
 | ||
|         QueryWrapper<CommentBlack> wrapper = Wrappers.query();
 | ||
|         wrapper.eq("phone", userPhone)
 | ||
|                 .in("status", Arrays.asList(CommentBlackStatus.EFFECT.value, CommentBlackStatus.EXPIRED.value))
 | ||
|                 .lt("start_time", now)
 | ||
|                 .ge("end_time", now);
 | ||
|         long validSize = commentBlackMapper.selectCount(wrapper);
 | ||
|         if (validSize > 0) {
 | ||
|             throw new BizException(ResponseStatus.REPETITIVE_ERROR);
 | ||
|         }
 | ||
|         CommentBlack commentBlack = mapperFacade.map(addCommentBlackQuery, CommentBlack.class);
 | ||
|         commentBlack.setUserName(addCommentBlackQuery.getUserName());
 | ||
|         commentBlack.setPhone(userPhone);
 | ||
|         commentBlack.setStartTime(now);
 | ||
|         commentBlack.setStatus(CommentBlackStatus.EFFECT.value);
 | ||
|         commentBlack.setOperatorId(backendUserVO.getUserId());
 | ||
|         commentBlack.setUpdateTime(now);
 | ||
|         commentBlack.setAdvisorId(backendUserVO.getAdvisorId());
 | ||
|         if (CommentBlackType.DAY.value.equals(commentBlack.getType())) {
 | ||
|             commentBlack.setEndTime(now.plusDays(1));
 | ||
|         } else if (CommentBlackType.MONTH.value.equals(commentBlack.getType())) {
 | ||
|             commentBlack.setEndTime(now.plusMonths(1));
 | ||
|         } else {
 | ||
|             commentBlack.setEndTime(now.plusYears(100));
 | ||
|         }
 | ||
|         int count = commentBlackMapper.insert(commentBlack);
 | ||
|         if (count < 1) {
 | ||
|             throw new BizException(ResponseStatus.DB_SAVE_ERROR);
 | ||
|         }
 | ||
|         this.clearCache(Collections.singletonList(ALL_BLACK_USER));
 | ||
|         return commentBlack.getId();
 | ||
|     }
 | ||
| 
 | ||
|     @Transactional(rollbackFor = Exception.class)
 | ||
|     public void removeCommentBlack(String phone) {
 | ||
|         LocalDateTime now = LocalDateTime.now();
 | ||
|         QueryWrapper<CommentBlack> wrapper = Wrappers.query();
 | ||
|         wrapper.eq("phone", phone)
 | ||
|                 .in("status", Arrays.asList(CommentBlackStatus.EFFECT.value, CommentBlackStatus.EXPIRED.value))
 | ||
|                 .lt("start_time", now)
 | ||
|                 .ge("end_time", now);
 | ||
|         List<CommentBlack> commentBlackList = commentBlackMapper.selectList(wrapper);
 | ||
|         if (CollectionUtils.isEmpty(commentBlackList)) {
 | ||
|             throw new BizException(ResponseStatus.REMOVE_BLACK_USER_ERROR);
 | ||
|         }
 | ||
|         for (CommentBlack commentBlack : commentBlackList) {
 | ||
|             CommentBlack updateObj = new CommentBlack();
 | ||
|             updateObj.setId(commentBlack.getId());
 | ||
|             updateObj.setStatus(CommentBlackStatus.REMOVED.value);
 | ||
|             updateObj.setUpdateTime(now);
 | ||
|             updateObj.setEndTime(now);
 | ||
|             int count = commentBlackMapper.updateById(updateObj);
 | ||
|             if (count < 1) {
 | ||
|                 throw new BizException(ResponseStatus.DB_SAVE_ERROR);
 | ||
|             }
 | ||
|         }
 | ||
|         this.clearCache(Collections.singletonList(ALL_BLACK_USER));
 | ||
|     }
 | ||
| 
 | ||
|     public Pager<CommentBlackVO> queryCommentBlackList(BackendUserVO backendUserVO, CommentBlackQuery commentBlackQuery) {
 | ||
|         LocalDateTime startTime = null;
 | ||
|         LocalDateTime endTime = null;
 | ||
|         LocalDateTime startOpTime = null;
 | ||
|         LocalDateTime endOpTime = null;
 | ||
|         if (StrUtil.isNotEmpty(commentBlackQuery.getStartTime())) {
 | ||
|             startTime = LocalDateTime.parse(commentBlackQuery.getStartTime(), formatter);
 | ||
|         }
 | ||
|         if (StrUtil.isNotEmpty(commentBlackQuery.getEndTime())) {
 | ||
|             endTime = LocalDateTime.parse(commentBlackQuery.getEndTime(), formatter);
 | ||
|         }
 | ||
|         if (StrUtil.isNotEmpty(commentBlackQuery.getStartOpTime())) {
 | ||
|             startOpTime = LocalDateTime.parse(commentBlackQuery.getStartOpTime(), formatter);
 | ||
|         }
 | ||
|         if (StrUtil.isNotEmpty(commentBlackQuery.getEndOpTime())) {
 | ||
|             endOpTime = LocalDateTime.parse(commentBlackQuery.getEndOpTime(), formatter);
 | ||
|         }
 | ||
|         /*Set<Integer> advisorIdSet = authService.getAccessibleAdviserSet(null, backendUserVO, UserType.fromValue(commentBlackQuery.getUserType()));
 | ||
|         if (advisorIdSet != null && advisorIdSet.isEmpty()) {
 | ||
|             return Pager.emptyPager();
 | ||
|         }*/
 | ||
|         QueryWrapper<CommentBlack> wrapper = Wrappers.query();
 | ||
|         wrapper.like(StrUtil.isNotEmpty(commentBlackQuery.getUserName()), "user_name", commentBlackQuery.getUserName())
 | ||
|                 .eq(StrUtil.isNotEmpty(commentBlackQuery.getPhone()), "phone", commentBlackQuery.getPhone())
 | ||
|                 .eq(commentBlackQuery.getType() != null, "type", commentBlackQuery.getType())
 | ||
|                 //.in(!CollectionUtils.isEmpty(advisorIdSet), "advisor_id", advisorIdSet)
 | ||
|                 .eq(commentBlackQuery.getProductType() != null, "product_type", commentBlackQuery.getProductType())
 | ||
|                 .eq(commentBlackQuery.getProductType() != null && commentBlackQuery.getProductId() != null, "product_id", commentBlackQuery.getProductId())
 | ||
|                 .like(StrUtil.isNotBlank(commentBlackQuery.getContent()), "content", commentBlackQuery.getContent())
 | ||
|                 .like(StrUtil.isNotEmpty(commentBlackQuery.getReason()), "reason", commentBlackQuery.getReason())
 | ||
|                 .eq(commentBlackQuery.getOperatorId() != null, "operator_id", commentBlackQuery.getOperatorId())
 | ||
|                 .ge(startTime != null, "start_time", startTime)
 | ||
|                 .lt(endTime != null, "start_time", endTime)
 | ||
|                 .ge(startOpTime != null, "end_time", startOpTime)
 | ||
|                 .lt(endOpTime != null, "end_time", endOpTime);
 | ||
|         wrapper.orderByDesc("start_time");
 | ||
|         Page<CommentBlack> page = commentBlackMapper.selectPage(commentBlackQuery.toPage(), wrapper);
 | ||
|         List<CommentBlack> list = page.getRecords();
 | ||
|         if (CollectionUtils.isEmpty(list)) {
 | ||
|             return Pager.emptyPager();
 | ||
|         }
 | ||
|         List<BaseProductQuery> baseProductQueryList = list.stream().map(commentBlack -> {
 | ||
|             if (commentBlack.getProductId() != null && commentBlack.getProductType() != null) {
 | ||
|                 return new BaseProductQuery(commentBlack.getProductId(), commentBlack.getProductType());
 | ||
|             }
 | ||
|             return null;
 | ||
|         }).filter(Objects::nonNull).collect(Collectors.toList());
 | ||
|         Table<Integer, Integer, MergeProductInfoVO> productTable = mergeProductService.queryMergeProductInfo(baseProductQueryList);
 | ||
|         Map<Integer, UserDept> userMap = userService.getUserMap();
 | ||
|         Map<String, Dept> deptMap = deptService.getDeptMap();
 | ||
|         List<CommentBlackVO> voList = list.stream().map(commentBlack -> mapperFacade.map(commentBlack, CommentBlackVO.class)).collect(Collectors.toList());
 | ||
|         for (CommentBlackVO commentBlackVO : voList) {
 | ||
|             // 查询产品信息
 | ||
|             if (commentBlackVO.getProductId() != null && commentBlackVO.getProductType() != null) {
 | ||
|                 MergeProductInfoVO mergeProductInfoVO = productTable.get(commentBlackVO.getProductType(), commentBlackVO.getProductId());
 | ||
|                 if (mergeProductInfoVO != null) {
 | ||
|                     commentBlackVO.setProductName(mergeProductInfoVO.getProductName());
 | ||
|                 }
 | ||
|             }
 | ||
|             LocalDateTime now = LocalDateTime.now();
 | ||
|             if (CommentBlackStatus.EFFECT.value.equals(commentBlackVO.getStatus()) && now.isAfter(commentBlackVO.getEndTime())) {
 | ||
|                 commentBlackVO.setStatus(CommentBlackStatus.EXPIRED.value);
 | ||
|             }
 | ||
|             UserDept user = userMap.get(commentBlackVO.getOperatorId());
 | ||
|             if (user != null) {
 | ||
|                 commentBlackVO.setOperatorName(user.getName());
 | ||
|             }
 | ||
|             if (StrUtil.isNotEmpty(commentBlackVO.getUserOrgNo())) {
 | ||
|                 Dept dept = deptMap.get(commentBlackVO.getUserOrgNo());
 | ||
|                 if (dept != null) {
 | ||
|                     commentBlackVO.setUserOrgName(dept.getName());
 | ||
|                 }
 | ||
|             }
 | ||
|             commentBlackVO.setPhone(HideUtils.hidePhoneNo(commentBlackVO.getPhone()));
 | ||
|             commentBlackVO.setHidePhone(commentBlackVO.getPhone());
 | ||
|             commentBlackVO.setUserName(HideUtils.maskLastName(commentBlackVO.getUserName()));
 | ||
|         }
 | ||
|         return new Pager<>(voList, page.getTotal());
 | ||
|     }
 | ||
| 
 | ||
|     private void clearCache(List<String> cacheKeys) {
 | ||
|         IMap<String, Object> cacheMap = hazelcastInstance.getMap(COMMENT_BLACK);
 | ||
|         for (String key : cacheKeys) {
 | ||
|             cacheMap.remove(key);
 | ||
|         }
 | ||
|     }
 | ||
| 
 | ||
|     /**
 | ||
|      * 校验是否禁言
 | ||
|      */
 | ||
|     public void check(String phone) {
 | ||
|         // 判断是否禁言用户
 | ||
|         Set<String> blackUsers = getAllBlackUser();
 | ||
|         if (CollUtil.isNotEmpty(blackUsers) && blackUsers.contains(phone)) {
 | ||
|             throw new BizException("禁言用户,禁止发言");
 | ||
|         }
 | ||
|     }
 | ||
| 
 | ||
| }
 | 
