【探花交友】查询通用设置、陌生人问题、黑名单管理

浅浅的花香味﹌ 2024-04-03 10:01 168阅读 0赞

目录

1、通用设置

1.1 需求分析

1.2 查询通用设置

1.2 陌生人问题

1.3 通知设置

1.4 黑名单管理

1、通用设置

1.1 需求分析

1.1.1 需求分析

通用设置,包含探花交友APP基本的软件设置功能。包含:

设置陌生人问题:当平台其他用户想进行在线交流时需要回答陌生人问题。

通用设置:包含一些APP通知设置

黑名单:对于不感兴趣的用户设置黑名单屏蔽骚扰a30ab413f6ad494f825bde929a4f5e42.png

f685adaf554d44a8a6f805f4c4121068.png

1.1.2 数据库表

通用设置

  1. CREATE TABLE `tb_settings` (
  2. `id` bigint(20) NOT NULL AUTO_INCREMENT,
  3. `user_id` bigint(20) DEFAULT NULL,
  4. `like_notification` tinyint(4) DEFAULT '1' COMMENT '推送喜欢通知',
  5. `pinglun_notification` tinyint(4) DEFAULT '1' COMMENT '推送评论通知',
  6. `gonggao_notification` tinyint(4) DEFAULT '1' COMMENT '推送公告通知',
  7. `created` datetime DEFAULT NULL,
  8. `updated` datetime DEFAULT NULL,
  9. PRIMARY KEY (`id`)
  10. ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='设置表';

问题表

  1. CREATE TABLE `tb_question` (
  2. `id` bigint(20) NOT NULL AUTO_INCREMENT,
  3. `user_id` bigint(20) DEFAULT NULL COMMENT '用户id',
  4. `txt` varchar(200) DEFAULT NULL COMMENT '问题内容',
  5. `created` datetime DEFAULT NULL,
  6. `updated` datetime DEFAULT NULL,
  7. PRIMARY KEY (`id`),
  8. KEY `user_id` (`user_id`)
  9. ) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8

黑名单

  1. CREATE TABLE `tb_black_list` (
  2. `id` bigint(20) NOT NULL AUTO_INCREMENT,
  3. `user_id` bigint(20) DEFAULT NULL,
  4. `black_user_id` bigint(20) DEFAULT NULL,
  5. `created` datetime DEFAULT NULL,
  6. `updated` datetime DEFAULT NULL,
  7. PRIMARY KEY (`id`),
  8. KEY `user_id` (`user_id`)
  9. ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='黑名单';

1.1.3 搭建提供者环境

实体类

(1) Settings

  1. @Data
  2. @NoArgsConstructor
  3. @AllArgsConstructor
  4. public class Settings extends BasePojo {
  5. private Long id;
  6. private Long userId;
  7. private Boolean likeNotification;
  8. private Boolean pinglunNotification;
  9. private Boolean gonggaoNotification;
  10. }

(2)Question

  1. @Data
  2. @NoArgsConstructor
  3. @AllArgsConstructor
  4. public class Question extends BasePojo {
  5. private Long id;
  6. private Long userId;
  7. //问题内容
  8. private String txt;
  9. }

(3)BlackList

  1. @Data
  2. @NoArgsConstructor
  3. @AllArgsConstructor
  4. public class BlackList extends BasePojo {
  5. private Long id;
  6. private Long userId;
  7. private Long blackUserId;
  8. }

mapper接口

(1)SettingsMapper

  1. public interface SettingsMapper extends BaseMapper<Settings> {
  2. }

(2)QuestionMapper

  1. public interface QuestionMapper extends BaseMapper<Question> {
  2. }

(3)BlackListMapper

  1. public interface BlackListMapper extends BaseMapper<BlackList> {
  2. }

api接口

(1) SettingApi

  1. package com.tanhua.dubbo.api;
  2. import com.tanhua.domain.db.Settings;
  3. public interface SettingsApi {
  4. }

(2)QuestionApi

  1. package com.tanhua.dubbo.api;
  2. import com.tanhua.domain.db.Question;
  3. public interface QuestionApi {
  4. }

(3)BlackListApi

  1. package com.tanhua.dubbo.api;
  2. import com.baomidou.mybatisplus.core.metadata.IPage;
  3. import com.tanhua.domain.db.UserInfo;
  4. public interface BlackListApi {
  5. }

api服务实现类

(1)SettingServiceImpl

  1. package com.tanhua.dubbo.api;
  2. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  3. import com.tanhua.domain.db.Settings;
  4. import com.tanhua.dubbo.mapper.SettingsMapper;
  5. import org.apache.dubbo.config.annotation.Service;
  6. import org.springframework.beans.factory.annotation.Autowired;
  7. @Service
  8. public class SettingsApiImpl implements SettingsApi {
  9. @Autowired
  10. private SettingsMapper settingsMapper;
  11. }

(2)QuestionServiceImpl

  1. package com.tanhua.dubbo.api;
  2. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  3. import com.tanhua.domain.db.Question;
  4. import com.tanhua.dubbo.mapper.QuestionMapper;
  5. import org.apache.dubbo.config.annotation.Service;
  6. import org.springframework.beans.factory.annotation.Autowired;
  7. @Service
  8. public class QuestionApiImpl implements QuestionApi {
  9. @Autowired
  10. private QuestionMapper questionMapper;
  11. }

(3)BlackListServiceImpl

  1. package com.tanhua.dubbo.api;
  2. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  3. import com.baomidou.mybatisplus.core.metadata.IPage;
  4. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  5. import com.tanhua.domain.db.BlackList;
  6. import com.tanhua.domain.db.UserInfo;
  7. import com.tanhua.dubbo.mapper.BlackListMapper;
  8. import com.tanhua.dubbo.mapper.UserInfoMapper;
  9. import org.apache.dubbo.config.annotation.Service;
  10. import org.springframework.beans.factory.annotation.Autowired;
  11. @Service
  12. public class BlackListApiImpl implements BlackListApi {
  13. @Autowired
  14. private BlackListMapper blackListMapper;
  15. }

1.2 查询通用设置

1.2.1 接口文档

66198f49df7a46dfaaf4af75df35668f.png

1.2.2 代码实现

vo对象

  1. @Data
  2. @NoArgsConstructor
  3. @AllArgsConstructor
  4. public class SettingsVo implements Serializable {
  5. private Long id;
  6. private String strangerQuestion = "";
  7. private String phone;
  8. private Boolean likeNotification = true;
  9. private Boolean pinglunNotification = true;
  10. private Boolean gonggaoNotification = true;
  11. }

SettingsController

tanhua-server工程创建SettingsController完成代码编写

  1. @RestController
  2. @RequestMapping("/users")
  3. public class SettingsController {
  4. @Autowired
  5. private SettingsService settingsService;
  6. /**
  7. * 查询通用设置
  8. */
  9. @GetMapping("/settings")
  10. public ResponseEntity settings() {
  11. SettingsVo vo = settingsService.settings();
  12. return ResponseEntity.ok(vo);
  13. }
  14. }

SettingService

tanhua-server工程创建SettingService完成代码编写

  1. @Service
  2. public class SettingsService {
  3. @DubboReference
  4. private QuestionApi questionApi;
  5. @DubboReference
  6. private SettingsApi settingsApi;
  7. @DubboReference
  8. private BlackListApi blackListApi;
  9. //查询通用设置
  10. public SettingsVo settings() {
  11. SettingsVo vo = new SettingsVo();
  12. //1、获取用户id
  13. Long userId = UserHolder.getUserId();
  14. vo.setId(userId);
  15. //2、获取用户的手机号码
  16. vo.setPhone(UserHolder.getMobile());
  17. //3、获取用户的陌生人问题
  18. Question question = questionApi.findByUserId(userId);
  19. String txt = question == null ? "你喜欢java吗?" : question.getTxt();
  20. vo.setStrangerQuestion(txt);
  21. //4、获取用户的APP通知开关数据
  22. Settings settings = settingsApi.findByUserId(userId);
  23. if(settings != null) {
  24. vo.setGonggaoNotification(settings.getGonggaoNotification());
  25. vo.setPinglunNotification(settings.getPinglunNotification());
  26. vo.setLikeNotification(settings.getLikeNotification());
  27. }
  28. return vo;
  29. }
  30. }

QuestionApi

在tanhua-dubbo中的QuestionApiQuestionApiImpl补充方法

  1. @Override
  2. public Question findByUserId(Long userId) {
  3. QueryWrapper<Question> qw = new QueryWrapper<>();
  4. qw.eq("user_id",userId);
  5. return questionMapper.selectOne(qw);
  6. }

SettingApi

在tanhua-dubbo中的SettingApiSettingApiImpl补充方法

  1. //根据用户id查询
  2. public Settings findByUserId(Long userId) {
  3. QueryWrapper<Settings> qw = new QueryWrapper<>();
  4. qw.eq("user_id",userId);
  5. return settingsMapper.selectOne(qw);
  6. }

1.2 陌生人问题

对数据库表进行操作:如果存在数据,更新数据库。如果不存在数据,保存数据库表数据

1.2.1 接口文档

a2f9be76a66d4cc4971c39e1ae30d009.png

1.2.2 代码实现

SettingsController

  1. /**
  2. * 设置陌生人问题
  3. */
  4. @PostMapping("/questions")
  5. public ResponseEntity questions(@RequestBody Map map) {
  6. //获取参数
  7. String content = (String) map.get("content");
  8. settingsService.saveQuestion(content);
  9. return ResponseEntity.ok(null);
  10. }

SettingsService

  1. //设置陌生人问题
  2. public void saveQuestion(String content) {
  3. //1、获取当前用户id
  4. Long userId = UserHolder.getUserId();
  5. //2、调用api查询当前用户的陌生人问题
  6. Question question = questionApi.findByUserId(userId);
  7. //3、判断问题是否存在
  8. if(question == null) {
  9. //3.1 如果不存在,保存
  10. question = new Question();
  11. question.setUserId(userId);
  12. question.setTxt(content);
  13. questionApi.save(question);
  14. }else {
  15. //3.2 如果存在,更新
  16. question.setTxt(content);
  17. questionApi.update(question);
  18. }
  19. }

QuestionApi

tanhua-dubbo工程中的QuestionApiQuestionApiImpl中添加保存和更新方法

  1. @Override
  2. public void save(Question question) {
  3. questionMapper.insert(question);
  4. }
  5. @Override
  6. public void update(Question question) {
  7. questionMapper.updateById(question);
  8. }

1.3 通知设置

1.3.1 接口文档

通知管理:对通知进行保存或者更新的操作

http://192.168.136.160:3000/project/19/interface/api/280

4c8c1b6734d44b628cf2cabb7a084b07.png

1.3.2 代码实现

SettingsController

  1. /**
  2. * 通知设置
  3. */
  4. @PostMapping("/notifications/setting")
  5. public ResponseEntity notifications(@RequestBody Map map) {
  6. //获取参数
  7. settingsService.saveSettings(map);
  8. return ResponseEntity.ok(null);
  9. }

SettingsService

  1. //通知设置
  2. public void saveSettings(Map map) {
  3. boolean likeNotification = (Boolean) map.get("likeNotification");
  4. boolean pinglunNotification = (Boolean) map.get("pinglunNotification");
  5. boolean gonggaoNotification = (Boolean) map.get("gonggaoNotification");
  6. //1、获取当前用户id
  7. Long userId = UserHolder.getUserId();
  8. //2、根据用户id,查询用户的通知设置
  9. Settings settings = settingsApi.findByUserId(userId);
  10. //3、判断
  11. if(settings == null) {
  12. //保存
  13. settings = new Settings();
  14. settings.setUserId(userId);
  15. settings.setPinglunNotification(pinglunNotification);
  16. settings.setLikeNotification(likeNotification);
  17. settings.setGonggaoNotification(gonggaoNotification);
  18. settingsApi.save(settings);
  19. }else {
  20. settings.setPinglunNotification(pinglunNotification);
  21. settings.setLikeNotification(likeNotification);
  22. settings.setGonggaoNotification(gonggaoNotification);
  23. settingsApi.update(settings);
  24. }
  25. }

SettingsApi

tanhua-dubbo工程中的SettingsApiSettingsApiImpl中添加保存和更新方法

  1. @Override
  2. public void save(Settings settings) {
  3. settingsMapper.insert(settings);
  4. }
  5. @Override
  6. public void update(Settings settings) {
  7. settingsMapper.updateById(settings);
  8. }

1.4 黑名单管理

1.3.1 接口文档

  • 查询黑名单列表

b6b31780c67348909f582b4dc1fd971d.png

0a7545a6ebc945f08cc6c7e4936f4e56.png

  • 移除黑名单

a0290e1d735646a2a7f683510ef80ebf.png

1.3.2 分页查询

vo对象

tanhua-domain工程的配置分页vo对象

  1. package com.tanhua.domain.vo;
  2. import lombok.AllArgsConstructor;
  3. import lombok.Data;
  4. import lombok.NoArgsConstructor;
  5. import java.io.Serializable;
  6. import java.util.Collections;
  7. import java.util.List;
  8. @Data
  9. @AllArgsConstructor
  10. @NoArgsConstructor
  11. public class PageResult implements Serializable {
  12. private Integer counts = 0;//总记录数
  13. private Integer pagesize;//页大小
  14. private Integer pages = 0;//总页数
  15. private Integer page;//当前页码
  16. private List<?> items = Collections.emptyList(); //列表
  17. public PageResult(Integer page,Integer pagesize,
  18. int counts,List list) {
  19. this.page = page;
  20. this.pagesize = pagesize;
  21. this.items = list;
  22. this.counts = counts;
  23. this.pages = counts % pagesize == 0 ? counts / pagesize : counts / pagesize + 1;
  24. }
  25. }

SettingsController

  1. /**
  2. * 分页查询黑名单列表
  3. */
  4. @GetMapping("/blacklist")
  5. public ResponseEntity blacklist(
  6. @RequestParam(defaultValue = "1") int page,
  7. @RequestParam(defaultValue = "10") int size) {
  8. //1、调用service查询
  9. PageResult pr = settingsService.blacklist(page,size);
  10. //2、构造返回
  11. return ResponseEntity.ok(pr);
  12. }
  13. /**
  14. * 取消黑名单
  15. */
  16. @DeleteMapping("/blacklist/{uid}")
  17. public ResponseEntity deleteBlackList(@PathVariable("uid") Long blackUserId) {
  18. settingsService.deleteBlackList(blackUserId);
  19. return ResponseEntity.ok(null);
  20. }

SettingService

  1. //分页查询黑名单列表
  2. public PageResult blacklist(int page, int size) {
  3. //1、获取当前用户的id
  4. Long userId = UserHolder.getUserId();
  5. //2、调用API查询用户的黑名单分页列表 Ipage对象
  6. IPage<UserInfo> iPage = blackListApi.findByUserId(userId,page,size);
  7. //3、对象转化,将查询的Ipage对象的内容封装到PageResult中
  8. PageResult pr = new PageResult(page,size,iPage.getTotal(),iPage.getRecords());
  9. //4、返回
  10. return pr;
  11. }
  12. //取消黑名单
  13. public void deleteBlackList(Long blackUserId) {
  14. //1、获取当前用户id
  15. Long userId = UserHolder.getUserId();
  16. //2、调用api删除
  17. blackListApi.delete(userId,blackUserId);
  18. }

BlackListApi

  1. @Override
  2. public IPage<UserInfo> findByUserId(Long userId, int page, int size) {
  3. //1、构建分页参数对象Page
  4. Page pages = new Page(page,size);
  5. //2、调用方法分页(自定义编写 分页参数Page,sql条件参数)
  6. return userInfoMapper.findBlackList(pages,userId);
  7. }
  8. @Override
  9. public void delete(Long userId, Long blackUserId) {
  10. QueryWrapper<BlackList> qw = new QueryWrapper<>();
  11. qw.eq("user_id",userId);
  12. qw.eq("black_user_id",blackUserId);
  13. blackListMapper.delete(qw);
  14. }

UserInfoMapper

  1. public interface UserInfoMapper extends BaseMapper<UserInfo> {
  2. @Select("select * from tb_user_info where id in (\n" +
  3. " SELECT black_user_id FROM tb_black_list where user_id=#{userId}\n" +
  4. ")")
  5. IPage<UserInfo> findBlackList(@Param("pages") Page pages, @Param("userId") Long userId);
  6. }

MybatisPlusConfig

tanhua-dubbo-db引导类开启mybatis-plus分页插件支持

  1. @Bean
  2. public MybatisPlusInterceptor mybatisPlusInterceptor() {
  3. MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
  4. interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
  5. return interceptor;
  6. }

使用mybatis-plus的分页:

  • 创建分页对象:Page,指定当前页和每页查询条数
  • 基础查询:mapper.selectPage(page,查询条件)
  • 自定义查询:Ipage 方法名称(Page对象,xxx查询条件)

发表评论

表情:
评论列表 (有 0 条评论,168人围观)

还没有评论,来说两句吧...

相关阅读

    相关 交友项目面试分析

    1、项目介绍 探花交友是一个陌生人在线交友平台,在该平台中可以搜索附近的人,查看好友动态,平台提供大数据分析,通过后台推荐系统帮我们快速匹配自己的“意中人”。项目主要分为