package com.xzq.mentalhealth.service.impl;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xzq.mentalhealth.common.ErrorCode;
import com.xzq.mentalhealth.exception.BusinessException;
import com.xzq.mentalhealth.mapper.CategoryMapper;
import com.xzq.mentalhealth.mapper.PaperMapper;
import com.xzq.mentalhealth.mapper.QuestionMapper;
import com.xzq.mentalhealth.mapper.QuestionPaperMapper;
import com.xzq.mentalhealth.model.dto.HandPaperDTO;
import com.xzq.mentalhealth.model.dto.PaperDTO;
import com.xzq.mentalhealth.model.entity.Category;
import com.xzq.mentalhealth.model.entity.Paper;
import com.xzq.mentalhealth.model.entity.Question;
import com.xzq.mentalhealth.model.entity.QuestionPaper;
import com.xzq.mentalhealth.model.vo.PaperFrontVO;
import com.xzq.mentalhealth.service.PaperService;
import com.xzq.mentalhealth.service.QuestionPaperService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
/**
* @author 谢志强
* @description 针对表【paper(测评问卷表)】的数据库操作Service实现
* @createDate 2023-01-18 11:55:41
*/
@Service
public class PaperServiceImpl extends ServiceImpl<PaperMapper, Paper>
implements PaperService {
@Resource
PaperMapper paperMapper;
@Resource
QuestionMapper questionMapper;
@Resource
CategoryMapper categoryMapper;
@Resource
QuestionPaperMapper questionPaperMapper;
@Resource
QuestionPaperService questionPaperService;
@Override
public Page<Paper> PaperList(long pageNum, long pageSize, String name) {
Page<Paper> PaperPage = new Page<>(pageNum, pageSize);
QueryWrapper<Paper> paperQueryWrapper = new QueryWrapper<>();
paperQueryWrapper.orderByAsc("id");
if (!"".equals(name)) {
paperQueryWrapper.like("name", name);
}
return paperMapper.selectPage(PaperPage, paperQueryWrapper);
}
@Override
public List<Paper> findAll(String name) {
QueryWrapper<Paper> paperQueryWrapper = new QueryWrapper<>();
if (StrUtil.isNotBlank(name)){
paperQueryWrapper.like("name",name);
}
return paperMapper.selectList(paperQueryWrapper);
}
@Override
public List<Paper> findAllByLimit(String name) {
QueryWrapper<Paper> paperQueryWrapper = new QueryWrapper<>();
if (StrUtil.isNotBlank(name)){
paperQueryWrapper.like("name",name);
}
paperQueryWrapper.last("limit 8");
return paperMapper.selectList(paperQueryWrapper);
}
@Override
public Integer addPaper(Paper paper) {
String paperName = paper.getName();
if (paperName == null){
throw new BusinessException(ErrorCode.SYSTEM_ERROR,"名字不能为空");
}
return paperMapper.insert(paper);
}
@Override
public Integer editPaper(Paper paper) {
return paperMapper.updateById(paper);
}
//创建测评问卷
@Override
public Boolean takePaper(PaperDTO paperDTO) {
//todo 防止重复添加相同的问卷 在创建之前删除
UpdateWrapper<QuestionPaper> questionPaperUpdateWrapper = new UpdateWrapper<>();
questionPaperUpdateWrapper.eq("paperId",paperDTO.getPaperId());
int delete = questionPaperMapper.delete(questionPaperUpdateWrapper);
if (delete <0){
throw new BusinessException(ErrorCode.SYSTEM_ERROR);
}
// 通过题目分类categoryId查询出该类型下的所有题目
QueryWrapper<Question> questionQueryWrapper = new QueryWrapper<>();
questionQueryWrapper.eq("categoryId",paperDTO.getCategoryId());
// 根据categoryId所有题目,再通过type区分题型
List<Question> questions = questionMapper.selectList(questionQueryWrapper);
List<Question> type1List = questions.stream().filter(question -> question.getType()==1).collect(Collectors.toList());//选择题
List<Question> type2List = questions.stream().filter(question -> question.getType()==2).collect(Collectors.toList());//判断题
// 查看题库中是否这么多数量的题型
if (type1List.size() < paperDTO.getType1()){
throw new BusinessException(ErrorCode.PARAMS_ERROR,"题库中选择题不足");
}
if (type2List.size() < paperDTO.getType2()){
throw new BusinessException(ErrorCode.PARAMS_ERROR,"题库中判断题不足");
}
// 开始随机组卷
List<QuestionPaper> questionPapers = getQuestionPaper(type1List.size(), paperDTO.getType1(), type1List, paperDTO.getPaperId());
questionPapers.addAll(getQuestionPaper(type2List.size(), paperDTO.getType2(), type2List, paperDTO.getPaperId()));
//批量插入关联关系表
return questionPaperService.saveBatch(questionPapers);
}
/**
* 手动创建问卷
* @param handPaperDTO
* @return
*/
@Override
public Boolean handPaper(HandPaperDTO handPaperDTO) {
//todo 防止重复添加相同的问卷 在创建之前删除
UpdateWrapper<QuestionPaper> questionPaperUpdateWrapper = new UpdateWrapper<>();
questionPaperUpdateWrapper.eq("paperId",handPaperDTO.getPaperId());
int delete = questionPaperMapper.delete(questionPaperUpdateWrapper);
if (delete <0){
throw new BusinessException(ErrorCode.SYSTEM_ERROR);
}
List<Long> handQuestionIds = handPaperDTO.getHandQuestionIds();
ArrayList<QuestionPaper> list = new ArrayList<>();
for (Long handQuestionId : handQuestionIds) {
QuestionPaper questionPaper = new QuestionPaper();
questionPaper.setQuestionId(handQuestionId);
questionPaper.setPaperId(handPaperDTO.getPaperId());
list.add(questionPaper);
}
return questionPaperService.saveBatch(list);
}
/**
* 前台分页查询
* @param pageNum
* @param pageSize
* @param categoryId
* @return
*/
@Override
public Page<Paper> paperFrontList(long pageNum, long pageSize, String name, long categoryId) {
Page<Paper> paperPage = new Page<>(pageNum, pageSize);
//封装条件
QueryWrapper<Paper> wrapper = new QueryWrapper<>();
if(!StrUtil.isEmpty(name)) {
wrapper.like("name",name);
}
if(categoryId > 0){
wrapper.eq("categoryId",categoryId);
}
return paperMapper.selectPage(paperPage, wrapper);
}
/**
* 返回响应给前端的问卷信息数据
* @param paperId
* @return
*/
@Override
public PaperFrontVO getFrontPaperInfo(long paperId) {
//根据paperId查看paper数据
Paper paper = paperMapper.selectById(paperId);
if(paper == null){
throw new BusinessException(ErrorCode.SYSTEM_ERROR);
}
Long categoryId = paper.getCategoryId();
Category category = categoryMapper.selectById(categoryId);
if (category == null){
throw new BusinessException(ErrorCode.SYSTEM_ERROR);
}
PaperFrontVO paperFrontVO = new PaperFrontVO();
paperFrontVO.setPaperId(paper.getId());
paperFrontVO.setName(paper.getName());
paperFrontVO.setPaperCover(paper.getPaperCover());
paperFrontVO.setCategoryName(category.getName());
paperFrontVO.setDescription(paper.getDescription());
return paperFrontVO;
}
/**
* 查看问卷
* @param paperId
* @return
*/
//todo 现在实后端看�
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
基于Java开发的心理健康咨询系统源码.zip基于Java开发的心理健康咨询系统源码.zip基于Java开发的心理健康咨询系统源码.zip基于Java开发的心理健康咨询系统源码.zip基于Java开发的心理健康咨询系统源码.zip基于Java开发的心理健康咨询系统源码.zip基于Java开发的心理健康咨询系统源码.zip基于Java开发的心理健康咨询系统源码.zip基于Java开发的心理健康咨询系统源码.zip基于Java开发的心理健康咨询系统源码.zip基于Java开发的心理健康咨询系统源码.zip基于Java开发的心理健康咨询系统源码.zip基于Java开发的心理健康咨询系统源码.zip
资源推荐
资源详情
资源评论
收起资源包目录
基于Java开发的心理健康咨询系统源码.zip (145个子文件)
mvnw.cmd 7KB
maven-wrapper.jar 57KB
PaperServiceImpl.java 11KB
CourseServiceImpl.java 9KB
PaperController.java 8KB
TeacherController.java 8KB
CourseController.java 8KB
ConsultationServiceImpl.java 6KB
ConsultationController.java 6KB
PayController.java 6KB
WebSocketServer.java 6KB
FilesServiceImpl.java 5KB
OrderController.java 5KB
MenuController.java 5KB
RoleController.java 5KB
ResultServiceImpl.java 5KB
ResultController.java 4KB
CategoryController.java 4KB
QuestionController.java 4KB
FilesController.java 4KB
OrdersServiceImpl.java 4KB
ChapterServiceImpl.java 4KB
UserServiceImpl.java 4KB
ChapterController.java 4KB
SubjectController.java 3KB
CorsConfig.java 3KB
MenuServiceImpl.java 3KB
QuestionServiceImpl.java 3KB
VideoController.java 2KB
SubjectServiceImpl.java 2KB
CategoryServiceImpl.java 2KB
Consultation.java 2KB
JwtInterceptor.java 2KB
Question.java 2KB
Course.java 2KB
UserController.java 2KB
TokenUtils.java 2KB
User.java 2KB
Knife4jConfiguration.java 2KB
Orders.java 2KB
Menu.java 2KB
Paper.java 1KB
PaperService.java 1KB
Result.java 1KB
Video.java 1KB
ResultUtil.java 1KB
UserVO.java 1KB
QuestionPaper.java 1KB
Category.java 1KB
UserRole.java 1KB
RoleMenu.java 1KB
VideoServiceImpl.java 1KB
Files.java 1KB
Role.java 1KB
Subject.java 1KB
ErrorCode.java 1KB
CourseFrontVO.java 1022B
Chapter.java 996B
GlobalExceptionHandler.java 990B
CourseService.java 984B
BusinessException.java 958B
BaseResponse.java 873B
Dict.java 871B
ConsultationService.java 860B
MenuService.java 853B
MybatisPlusConfig.java 812B
FilesService.java 793B
OrdersService.java 777B
ResultService.java 737B
OrderNoUtil.java 687B
QuestionService.java 657B
CategoryService.java 653B
QuestionPaperServiceImpl.java 648B
RoleMenuServiceImpl.java 594B
UserRoleServiceImpl.java 594B
UserService.java 562B
ChapterService.java 560B
DictServiceImpl.java 555B
WebSocketConfig.java 537B
SubjectService.java 500B
QuestionPaperMapper.java 488B
RoleTeacherVO.java 478B
RoleMenuMapper.java 467B
UserRoleMapper.java 467B
QuestionMapper.java 463B
ConsultationMapper.java 460B
SubjectMapper.java 455B
ChapterMapper.java 455B
ConsultationDTO.java 455B
ResultMapper.java 453B
CategoryMapper.java 448B
OrdersMapper.java 447B
VideoMapper.java 445B
CourseMapper.java 444B
DictMapper.java 440B
FilesMapper.java 439B
MenuMapper.java 437B
UserMapper.java 434B
PaperMapper.java 432B
AlipayConfig.java 430B
共 145 条
- 1
- 2
资源评论
- m0_600353622024-04-08这个资源对我启发很大,受益匪浅,学到了很多,谢谢分享~
manylinux
- 粉丝: 4428
- 资源: 2491
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 纯 Python Java 解析器和工具.zip
- YOLO标记口罩数据集 (YOLO 格式注释)
- uniapp+vue3+云开发全栈开发同城配送鲜花小程序任意商城教程
- 客户需求快速小程序项目开发技巧
- java项目,课程设计-医疗服务系统.zip
- YOLO 注释风力涡轮机表面损坏-以 YOLO 格式注释风力涡轮机表面损伤 一万六千多文件
- 第一个适用于 Java 的 REST API 框架.zip
- Nvidia GeForce GT 1030显卡驱动(Win7)
- TIA PORTAL V17 UPD8- 更新包(最新版本2024.09)-链接地址.txt
- 示例应用程序展示了客户端和服务器上 JavaFX 和 Spring 技术的集成.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功