Compare commits
1 Commits
feature-Da
...
e4f1385411
| Author | SHA1 | Date | |
|---|---|---|---|
| e4f1385411 |
@@ -3,6 +3,8 @@ package com.bao.dating.controller;
|
||||
import com.bao.dating.context.UserContext;
|
||||
import com.bao.dating.service.ContactsService;
|
||||
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
@@ -12,6 +14,7 @@ import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api/contacts")
|
||||
public class ContactsController {
|
||||
@Resource
|
||||
private ContactsService contactsService;
|
||||
@@ -42,4 +45,56 @@ public class ContactsController {
|
||||
result.put("data", friends);
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 拉黑联系人接口
|
||||
* @param userId 当前用户ID
|
||||
* @param contactUserId 被拉黑联系人ID
|
||||
* @return 接口响应
|
||||
*/
|
||||
@PostMapping("/blacklist/{userId}/{contactUserId}")
|
||||
public ResponseEntity<Map<String, Object>> blacklistContact(
|
||||
@PathVariable Long userId,
|
||||
@PathVariable Long contactUserId) {
|
||||
Map<String, Object> result = new HashMap<>();
|
||||
boolean success = contactsService.blacklistContact(userId, contactUserId);
|
||||
if (success) {
|
||||
result.put("code", 200);
|
||||
result.put("msg", "拉黑联系人成功");
|
||||
result.put("data", null);
|
||||
return ResponseEntity.ok(result);
|
||||
} else {
|
||||
result.put("code", 500);
|
||||
result.put("msg", "拉黑联系人失败,联系人不存在或参数错误");
|
||||
result.put("data", null);
|
||||
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(result);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除联系人接口
|
||||
* @param userId 当前用户ID
|
||||
* @param contactUserId 被删除联系人ID
|
||||
* @return 接口响应
|
||||
*/
|
||||
@PostMapping("/delete/{userId}/{contactUserId}")
|
||||
public ResponseEntity<Map<String, Object>> deleteContact(
|
||||
@PathVariable Long userId,
|
||||
@PathVariable Long contactUserId) {
|
||||
Map<String, Object> result = new HashMap<>();
|
||||
boolean success = contactsService.deleteContact(userId, contactUserId);
|
||||
if (success) {
|
||||
result.put("code", 200);
|
||||
result.put("msg", "删除联系人成功");
|
||||
result.put("data", null);
|
||||
return ResponseEntity.ok(result);
|
||||
} else {
|
||||
result.put("code", 500);
|
||||
result.put("msg", "删除联系人失败,联系人不存在或参数错误");
|
||||
result.put("data", null);
|
||||
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(result);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,66 +0,0 @@
|
||||
package com.bao.dating.controller;
|
||||
|
||||
import com.bao.dating.common.Result;
|
||||
import com.bao.dating.common.ResultCode;
|
||||
import com.bao.dating.pojo.vo.DataAnalysisVO;
|
||||
import com.bao.dating.util.DataAnalysisRedisService;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.ArrayList;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/dataAnalysis")
|
||||
public class DataAnalysisController {
|
||||
@Resource
|
||||
private DataAnalysisRedisService dataAnalysisRedisService;
|
||||
|
||||
/**
|
||||
* 统计动态
|
||||
* @param days 统计时常
|
||||
* @return result
|
||||
*/
|
||||
@GetMapping("/postTotal")
|
||||
public Result<DataAnalysisVO> postDataAnalysis(@RequestParam Integer days){
|
||||
if (checkParam(days)){
|
||||
return Result.error(ResultCode.PARAM_ERROR);
|
||||
}
|
||||
Long count = dataAnalysisRedisService.countPostTotalByDays(days);
|
||||
String timePeriod = dataAnalysisRedisService.calculateTimeRanges(LocalDateTime.now(), days);
|
||||
DataAnalysisVO dataAnalysisVO = new DataAnalysisVO();
|
||||
dataAnalysisVO.setTotal(count);
|
||||
dataAnalysisVO.setTimePeriod(timePeriod);
|
||||
return Result.success(ResultCode.SUCCESS,dataAnalysisVO);
|
||||
}
|
||||
|
||||
@GetMapping("/loginUserTotal")
|
||||
public Result<DataAnalysisVO> loginUserDataAnalysis(@RequestParam Integer days){
|
||||
if (checkParam(days)){
|
||||
return Result.error(ResultCode.PARAM_ERROR);
|
||||
}
|
||||
Long count = dataAnalysisRedisService.countLoginUserTotalByDays(days);
|
||||
String timePeriod = dataAnalysisRedisService.calculateTimeRanges(LocalDateTime.now(), days);
|
||||
DataAnalysisVO dataAnalysisVO = new DataAnalysisVO();
|
||||
dataAnalysisVO.setTotal(count);
|
||||
dataAnalysisVO.setTimePeriod(timePeriod);
|
||||
return Result.success(ResultCode.SUCCESS,dataAnalysisVO);
|
||||
}
|
||||
|
||||
/**
|
||||
* 检验参数
|
||||
* @param days 查询天数
|
||||
* @return 参数是否合法
|
||||
*/
|
||||
public Boolean checkParam(Integer days){
|
||||
ArrayList<Integer> day = new ArrayList<>();
|
||||
day.add(7);
|
||||
day.add(15);
|
||||
day.add(30);
|
||||
day.add(365);
|
||||
return days != null && day.contains(days);
|
||||
}
|
||||
}
|
||||
@@ -1,17 +1,17 @@
|
||||
package com.bao.dating.controller;
|
||||
|
||||
|
||||
import com.bao.dating.anno.Log;
|
||||
import com.bao.dating.common.Result;
|
||||
import com.bao.dating.common.ResultCode;
|
||||
import com.bao.dating.pojo.dto.PostRequestDTO;
|
||||
import com.bao.dating.pojo.entity.Post;
|
||||
import com.bao.dating.pojo.vo.PostEditVO;
|
||||
import com.bao.dating.service.PostService;
|
||||
import com.bao.dating.util.DataAnalysisRedisService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
@@ -26,9 +26,6 @@ public class PostController {
|
||||
@Autowired
|
||||
private PostService postService;
|
||||
|
||||
@Resource
|
||||
private DataAnalysisRedisService dataAnalysisRedisService;
|
||||
|
||||
/**
|
||||
* 上传媒体文件接口 like
|
||||
* @param files 媒体文件数组
|
||||
@@ -49,9 +46,6 @@ public class PostController {
|
||||
public Result<Post> createPostJson(@RequestBody PostRequestDTO postDTO) {
|
||||
// 调用 Service 层处理发布动态业务逻辑
|
||||
Post result = postService.createPost(postDTO);
|
||||
//这里将发布动态操作记录到redis中
|
||||
Long postId = result.getPostId();
|
||||
dataAnalysisRedisService.recordPostData(postId);
|
||||
return Result.success(ResultCode.SUCCESS, "动态发布成功,等待审核。", result);
|
||||
}
|
||||
|
||||
|
||||
@@ -9,13 +9,11 @@ import com.bao.dating.pojo.dto.UserLoginDTO;
|
||||
import com.bao.dating.pojo.vo.UserInfoVO;
|
||||
import com.bao.dating.pojo.vo.UserLoginVO;
|
||||
import com.bao.dating.service.UserService;
|
||||
import com.bao.dating.util.DataAnalysisRedisService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.util.List;
|
||||
import javax.annotation.Resource;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.util.Map;
|
||||
|
||||
@@ -31,9 +29,6 @@ public class UserController {
|
||||
@Autowired
|
||||
private UserService userService;
|
||||
|
||||
@Resource
|
||||
private DataAnalysisRedisService dataAnalysisRedisService;
|
||||
|
||||
/**
|
||||
* 登录
|
||||
* @param userLoginDTO 登录参数
|
||||
@@ -41,12 +36,6 @@ public class UserController {
|
||||
@PostMapping("/login")
|
||||
public Result<UserLoginVO> login(@RequestBody UserLoginDTO userLoginDTO) {
|
||||
UserLoginVO userloginVO = userService.userLogin(userLoginDTO);
|
||||
//将用户登录记录到redis中
|
||||
if (userloginVO != null){
|
||||
Long userId = userloginVO.getUserId();
|
||||
dataAnalysisRedisService.recordLoginUserData(userId);
|
||||
}
|
||||
|
||||
return Result.success(ResultCode.SUCCESS, "登录成功", userloginVO);
|
||||
}
|
||||
|
||||
@@ -139,11 +128,6 @@ public class UserController {
|
||||
}
|
||||
//登录
|
||||
UserLoginVO vo = userService.loginByPhone(phone);
|
||||
//将用户登录操作记录到redis中
|
||||
if (vo != null){
|
||||
Long userId = vo.getUserId();
|
||||
dataAnalysisRedisService.recordLoginUserData(userId);
|
||||
}
|
||||
return Result.success(ResultCode.SUCCESS, "登录成功", vo);
|
||||
}
|
||||
|
||||
@@ -216,9 +200,6 @@ public class UserController {
|
||||
if (userLoginVO == null){
|
||||
return Result.error(ResultCode.FAIL,"请先注册用户或添加邮箱");
|
||||
}
|
||||
//将用户登录操作记录到redis中
|
||||
Long userId = userLoginVO.getUserId();
|
||||
dataAnalysisRedisService.recordLoginUserData(userId);
|
||||
return Result.success(ResultCode.SUCCESS,"用户登录成功",userLoginVO);
|
||||
}
|
||||
|
||||
|
||||
@@ -17,4 +17,20 @@ public interface ContactMapper {
|
||||
*/
|
||||
List<Map<String, Object>> selectFriendsByUserId(@Param("userId") Long userId);
|
||||
|
||||
|
||||
/**
|
||||
* 拉黑联系人
|
||||
* @param userId 当前用户ID
|
||||
* @param contactUserId 被拉黑的联系人ID
|
||||
* @return 影响行数
|
||||
*/
|
||||
int blacklistContact(@Param("userId") Long userId, @Param("contactUserId") Long contactUserId);
|
||||
|
||||
/**
|
||||
* 删除联系人(逻辑删除)
|
||||
* @param userId 当前用户ID
|
||||
* @param contactUserId 被删除的联系人ID
|
||||
* @return 影响行数
|
||||
*/
|
||||
int deleteContact(@Param("userId") Long userId, @Param("contactUserId") Long contactUserId);
|
||||
}
|
||||
|
||||
@@ -1,11 +0,0 @@
|
||||
package com.bao.dating.pojo.vo;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class DataAnalysisVO {
|
||||
//所有总数
|
||||
private Long total;
|
||||
//时间段
|
||||
private String timePeriod;
|
||||
}
|
||||
@@ -15,6 +15,20 @@ public interface ContactsService {
|
||||
List<Map<String, Object>> getFriendsByUserId(Long userId);
|
||||
|
||||
|
||||
/**
|
||||
* 拉黑联系人
|
||||
* @param userId 当前用户ID
|
||||
* @param contactUserId 被拉黑联系人ID
|
||||
* @return 操作是否成功
|
||||
*/
|
||||
boolean blacklistContact(Long userId, Long contactUserId);
|
||||
|
||||
/**
|
||||
* 删除联系人(逻辑删除)
|
||||
* @param userId 当前用户ID
|
||||
* @param contactUserId 被删除联系人ID
|
||||
* @return 操作是否成功
|
||||
*/
|
||||
boolean deleteContact(Long userId, Long contactUserId);
|
||||
}
|
||||
|
||||
|
||||
@@ -5,6 +5,7 @@ import com.bao.dating.service.ContactsService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@@ -19,4 +20,34 @@ public class ContactServiceImpl implements ContactsService {
|
||||
// 直接调用Mapper查询,无额外封装
|
||||
return contactMapper.selectFriendsByUserId(userId);
|
||||
}
|
||||
|
||||
/**
|
||||
* 拉黑联系人
|
||||
*/
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public boolean blacklistContact(Long userId, Long contactUserId) {
|
||||
// 参数校验
|
||||
if (userId == null || contactUserId == null || userId.equals(contactUserId)) {
|
||||
return false;
|
||||
}
|
||||
// 执行拉黑操作
|
||||
int affectRows = contactMapper.blacklistContact(userId, contactUserId);
|
||||
return affectRows > 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除联系人
|
||||
*/
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public boolean deleteContact(Long userId, Long contactUserId) {
|
||||
// 参数校验
|
||||
if (userId == null || contactUserId == null || userId.equals(contactUserId)) {
|
||||
return false;
|
||||
}
|
||||
// 执行删除操作
|
||||
int affectRows = contactMapper.deleteContact(userId, contactUserId);
|
||||
return affectRows > 0;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,101 +0,0 @@
|
||||
package com.bao.dating.util;
|
||||
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* 基于redis的数据分析工具 -统计动态总数和登录人数
|
||||
*/
|
||||
@Component
|
||||
public class DataAnalysisRedisService {
|
||||
// Redis Key 定义
|
||||
//动态总数
|
||||
private static final String REDIS_KEY_POST_TOTAL = "data:post:total";
|
||||
//活跃人数
|
||||
private static final String REDIS_KEY_LOGIN_USER_TOTAL = "data:loginUser:total";
|
||||
|
||||
@Resource
|
||||
private RedisTemplate<String, Object> redisTemplate;
|
||||
|
||||
/**
|
||||
* 记录动态向redis中添加数据
|
||||
* @param postId 动态id
|
||||
*/
|
||||
public void recordPostData(Long postId) {
|
||||
// 获取当前毫秒级时间戳(作为Sorted Set的score)
|
||||
long currentTimestamp = System.currentTimeMillis();
|
||||
// 1. 记录动态数据
|
||||
redisTemplate.opsForZSet().add(REDIS_KEY_POST_TOTAL, postId, currentTimestamp);
|
||||
// 可选:设置Key的过期时间(1年+1天,避免数据无限累积)
|
||||
setKeyExpire(REDIS_KEY_POST_TOTAL);
|
||||
}
|
||||
|
||||
/**
|
||||
* 记录动态向redis中添加数据
|
||||
* @param userId 用户id
|
||||
*/
|
||||
public void recordLoginUserData(Long userId) {
|
||||
// 获取当前毫秒级时间戳(作为Sorted Set的score)
|
||||
long currentTimestamp = System.currentTimeMillis();
|
||||
// 1. 记录动态数据
|
||||
redisTemplate.opsForZSet().add(REDIS_KEY_LOGIN_USER_TOTAL, userId, currentTimestamp);
|
||||
// 可选:设置Key的过期时间(1年+1天,避免数据无限累积)
|
||||
setKeyExpire(REDIS_KEY_LOGIN_USER_TOTAL);
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置Redis Key的过期时间(1年+1天,确保1年内的数据都能被查询)
|
||||
*/
|
||||
private void setKeyExpire(String redisKey) {
|
||||
if (Boolean.FALSE.equals(redisTemplate.hasKey(redisKey))) {
|
||||
redisTemplate.expire(redisKey, 366, TimeUnit.DAYS);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 统计指定时间段内发布的动态
|
||||
* @param days 指定时间段
|
||||
* @return 总数
|
||||
*/
|
||||
public Long countPostTotalByDays(int days) {
|
||||
// 1. 计算时间范围:结束时间=当前时间,起始时间=当前时间 - N天的毫秒数
|
||||
long endTimestamp = System.currentTimeMillis();
|
||||
long startTimestamp = endTimestamp - (long) days * 24 * 60 * 60 * 1000;
|
||||
// 2. key min max - 统计指定score范围内的成员数量(高效,直接返回数量)
|
||||
return redisTemplate.opsForZSet().count(REDIS_KEY_POST_TOTAL, startTimestamp, endTimestamp);
|
||||
}
|
||||
|
||||
public Long countLoginUserTotalByDays(int days) {
|
||||
// 1. 计算时间范围:结束时间=当前时间,起始时间=当前时间 - N天的毫秒数
|
||||
long endTimestamp = System.currentTimeMillis();
|
||||
long startTimestamp = endTimestamp - (long) days * 24 * 60 * 60 * 1000;
|
||||
// 2. key min max - 统计指定score范围内的成员数量(高效,直接返回数量)
|
||||
return redisTemplate.opsForZSet().count(REDIS_KEY_LOGIN_USER_TOTAL, startTimestamp, endTimestamp);
|
||||
}
|
||||
|
||||
/**
|
||||
* 核心方法:传入当前时间,返回各时间段的结果
|
||||
* @param currentDateTime 当前时间(LocalDateTime类型,可直接通过LocalDateTime.now()获取)
|
||||
*/
|
||||
public String calculateTimeRanges(LocalDateTime currentDateTime,int days) {
|
||||
// 1. 计算各时间段的起始时间(当前时间往前推对应时长)
|
||||
LocalDateTime sevenDaysAgo = currentDateTime.minusDays(7);
|
||||
LocalDateTime fifteenDaysAgo = currentDateTime.minusDays(15);
|
||||
LocalDateTime thirtyDaysAgo = currentDateTime.minusDays(30);
|
||||
LocalDateTime oneYearAgo = currentDateTime.minusYears(1);
|
||||
if (days == 7)
|
||||
return sevenDaysAgo + " - " + currentDateTime;
|
||||
if (days == 15)
|
||||
return fifteenDaysAgo + " - " + currentDateTime;
|
||||
if (days == 30)
|
||||
return thirtyDaysAgo + " - " + currentDateTime;
|
||||
if (days == 365)
|
||||
return oneYearAgo + " - " + currentDateTime;
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -21,5 +21,29 @@
|
||||
AND c.relation_type != 3 -- 排除黑名单
|
||||
AND c.user_id != c.contact_user_id -- 排除自己
|
||||
</select>
|
||||
|
||||
|
||||
<!-- 拉黑联系人:更新relation_type为3(黑名单)、contact_status为3(已拉黑) -->
|
||||
<update id="blacklistContact">
|
||||
UPDATE contacts
|
||||
SET relation_type = 3,
|
||||
contact_status = 3,
|
||||
updated_at = CURRENT_TIMESTAMP
|
||||
WHERE user_id = #{userId}
|
||||
AND contact_user_id = #{contactUserId}
|
||||
AND contact_status != 2
|
||||
</update>
|
||||
|
||||
<!-- 删除联系人:更新contact_status为2(已删除) -->
|
||||
<update id="deleteContact">
|
||||
UPDATE contacts
|
||||
SET contact_status = 2,
|
||||
updated_at = CURRENT_TIMESTAMP
|
||||
WHERE user_id = #{userId}
|
||||
AND contact_user_id = #{contactUserId}
|
||||
AND contact_status != 2
|
||||
</update>
|
||||
</mapper>
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user