AdvisorServer/src/main/java/com/syzb/common/service/CommentBlackService.java

281 lines
13 KiB
Java
Raw Normal View History

2025-02-20 14:14:51 +08:00
package com.syzb.common.service;
2025-01-27 21:47:33 +08:00
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;
2025-02-21 20:05:47 +08:00
import com.google.common.collect.Table;
import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.map.IMap;
2025-02-20 14:14:51 +08:00
import com.syzb.common.constant.CommentBlackScope;
import com.syzb.common.constant.CommentBlackStatus;
import com.syzb.common.constant.CommentBlackType;
import com.syzb.common.entity.CommentBlack;
import com.syzb.common.handler.BizException;
import com.syzb.common.mapper.CommentBlackMapper;
import com.syzb.common.query.AddCommentBlackQuery;
import com.syzb.common.query.BaseProductQuery;
import com.syzb.common.query.CommentBlackQuery;
import com.syzb.common.query.RemoveCommentBlackQuery;
import com.syzb.common.result.Pager;
import com.syzb.common.result.ResponseStatus;
import com.syzb.common.util.logger.LoggerUtil;
import com.syzb.common.vo.BackendUserVO;
import com.syzb.common.vo.CommentBlackVO;
import com.syzb.common.vo.MergeProductInfoVO;
import com.syzb.rbac.entity.Dept;
import com.syzb.rbac.entity.UserDept;
import com.syzb.rbac.service.DeptService;
import com.syzb.rbac.service.UserService;
2025-01-27 21:47:33 +08:00
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;
2025-02-20 14:14:51 +08:00
import static com.syzb.common.config.cache.CacheKey.COMMENT_BLACK;
import static com.syzb.common.config.cache.CacheKey.CommentBlackKey.ALL_BLACK_COMMENT;
import static com.syzb.common.config.cache.CacheKey.CommentBlackKey.ALL_BLACK_USER;
2025-01-27 21:47:33 +08:00
@Service
public class CommentBlackService {
@Resource
private CommentBlackMapper commentBlackMapper;
@Resource
private UserService userService;
@Resource
private MergeProductService mergeProductService;
@Resource
private HazelcastInstance hazelcastInstance;
@Resource
private CacheService cacheService;
@Resource
private DeptService deptService;
private static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
@Transactional(rollbackFor = Exception.class)
2025-02-10 14:47:05 +08:00
public Integer addCommentBlack(BackendUserVO backendUserVO, AddCommentBlackQuery query) {
String userPhone = query.getUserPhone();
2025-01-27 21:47:33 +08:00
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);
}
2025-02-20 11:09:27 +08:00
CommentBlack commentBlack = query.toPO();
2025-01-27 21:47:33 +08:00
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);
}
2025-02-10 14:47:05 +08:00
this.clearCache(ALL_BLACK_USER, ALL_BLACK_COMMENT);
2025-01-27 21:47:33 +08:00
return commentBlack.getId();
}
@Transactional(rollbackFor = Exception.class)
2025-02-10 14:47:05 +08:00
public void removeCommentBlack(BackendUserVO backendUserVO, RemoveCommentBlackQuery query) {
2025-01-27 21:47:33 +08:00
LocalDateTime now = LocalDateTime.now();
QueryWrapper<CommentBlack> wrapper = Wrappers.query();
2025-02-10 14:47:05 +08:00
wrapper.eq("phone", query.getUserPhone())
.eq("product_id", query.getProductId())
.eq("product_type", query.getProductType())
2025-01-27 21:47:33 +08:00
.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);
}
}
2025-02-10 14:47:05 +08:00
this.clearCache(ALL_BLACK_USER, ALL_BLACK_COMMENT);
2025-01-27 21:47:33 +08:00
}
2025-02-10 14:47:05 +08:00
public Pager<CommentBlackVO> queryCommentBlackList(BackendUserVO backendUserVO, CommentBlackQuery query) {
2025-01-27 21:47:33 +08:00
LocalDateTime startTime = null;
LocalDateTime endTime = null;
LocalDateTime startOpTime = null;
LocalDateTime endOpTime = null;
2025-02-10 14:47:05 +08:00
if (StrUtil.isNotEmpty(query.getStartTime())) {
startTime = LocalDateTime.parse(query.getStartTime(), formatter);
2025-01-27 21:47:33 +08:00
}
2025-02-10 14:47:05 +08:00
if (StrUtil.isNotEmpty(query.getEndTime())) {
endTime = LocalDateTime.parse(query.getEndTime(), formatter);
2025-01-27 21:47:33 +08:00
}
2025-02-10 14:47:05 +08:00
if (StrUtil.isNotEmpty(query.getStartOpTime())) {
startOpTime = LocalDateTime.parse(query.getStartOpTime(), formatter);
2025-01-27 21:47:33 +08:00
}
2025-02-10 14:47:05 +08:00
if (StrUtil.isNotEmpty(query.getEndOpTime())) {
endOpTime = LocalDateTime.parse(query.getEndOpTime(), formatter);
2025-01-27 21:47:33 +08:00
}
QueryWrapper<CommentBlack> wrapper = Wrappers.query();
2025-02-10 14:47:05 +08:00
wrapper.like(StrUtil.isNotEmpty(query.getUserName()), "user_name", query.getUserName())
.eq(StrUtil.isNotEmpty(query.getPhone()), "phone", query.getPhone())
2025-02-12 22:22:38 +08:00
.eq(query.getStatus() != null, "status", query.getStatus())
2025-02-10 14:47:05 +08:00
.eq(query.getType() != null, "type", query.getType())
.eq(query.getProductType() != null, "product_type", query.getProductType())
.eq(query.getProductType() != null && query.getProductId() != null, "product_id", query.getProductId())
.like(StrUtil.isNotBlank(query.getContent()), "content", query.getContent())
.like(StrUtil.isNotEmpty(query.getReason()), "reason", query.getReason())
.eq(query.getOperatorId() != null, "operator_id", query.getOperatorId())
2025-01-27 21:47:33 +08:00
.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");
2025-02-10 14:47:05 +08:00
Page<CommentBlack> page = commentBlackMapper.selectPage(query.toPage(), wrapper);
2025-01-27 21:47:33 +08:00
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();
2025-02-20 11:09:27 +08:00
List<CommentBlackVO> voList = list.stream().map(CommentBlackVO::new).collect(Collectors.toList());
2025-01-27 21:47:33 +08:00
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());
}
}
}
return new Pager<>(voList, page.getTotal());
}
/**
* 校验是否禁言
*/
2025-02-10 14:47:05 +08:00
public boolean checkIsBlack(String phone, Integer productId, Integer productType) {
2025-01-27 21:47:33 +08:00
// 判断是否禁言用户
Set<String> blackUsers = getAllBlackUser();
2025-02-10 14:47:05 +08:00
if (!blackUsers.contains(phone)) {
return false;
}
List<CommentBlack> blackComments = getAllBlackComment();
if (CollectionUtils.isEmpty(blackComments)) {
return false;
2025-01-27 21:47:33 +08:00
}
2025-02-10 14:47:05 +08:00
for (CommentBlack commentBlack : blackComments) {
if (commentBlack.getPhone().equals(phone)) {
if (CommentBlackScope.PRODUCT.value.equals(commentBlack.getScope())) {
if (commentBlack.getProductId().equals(productId) && commentBlack.getProductType().equals(productType)) {
return true;
}
} else if (CommentBlackScope.PRODUCT_TYPE.value.equals(commentBlack.getScope())) {
if (commentBlack.getProductType().equals(productType)) {
return true;
}
} else if (CommentBlackScope.GLOBAL.value.equals(commentBlack.getScope())) {
return true;
}
}
}
return false;
}
private Set<String> getAllBlackUser() {
return cacheService.get(COMMENT_BLACK, ALL_BLACK_USER, () ->
getAllBlackComment().stream().map(CommentBlack::getPhone).collect(Collectors.toSet()));
}
private List<CommentBlack> getAllBlackComment() {
LocalDateTime now = LocalDateTime.now();
return cacheService.get(COMMENT_BLACK, ALL_BLACK_COMMENT, () -> {
QueryWrapper<CommentBlack> wrapper = Wrappers.<CommentBlack>query()
.in("status", Arrays.asList(CommentBlackStatus.EFFECT.value, CommentBlackStatus.EXPIRED.value))
.lt("start_time", now)
.gt("end_time", now);
List<CommentBlack> commentBlackList = commentBlackMapper.selectList(wrapper);
LoggerUtil.info("db当前黑名单用户" + JSONObject.toJSONString(commentBlackList));
return commentBlackList;
});
2025-01-27 21:47:33 +08:00
}
2025-02-23 12:35:57 +08:00
public Set<String> getBlackUserIds(Integer productId, Integer productType) {
Set<String> blackUsers = new HashSet<>();
List<CommentBlack> blackComments = getAllBlackComment();
for (CommentBlack commentBlack : blackComments) {
if (CommentBlackScope.PRODUCT.value.equals(commentBlack.getScope())) {
if (commentBlack.getProductId().equals(productId) && commentBlack.getProductType().equals(productType)) {
blackUsers.add(commentBlack.getPhone());
}
} else if (CommentBlackScope.PRODUCT_TYPE.value.equals(commentBlack.getScope())) {
if (commentBlack.getProductType().equals(productType)) {
blackUsers.add(commentBlack.getPhone());
}
} else if (CommentBlackScope.GLOBAL.value.equals(commentBlack.getScope())) {
blackUsers.add(commentBlack.getPhone());
}
}
return blackUsers;
}
private void clearCache(String... cacheKeys) {
IMap<String, Object> cacheMap = hazelcastInstance.getMap(COMMENT_BLACK);
for (String key : cacheKeys) {
cacheMap.remove(key);
}
}
2025-01-27 21:47:33 +08:00
}