Merge branch 'master' of 39.106.16.162:/home/git/teaching-backend/teaching-backend

master
yh 3 months ago
commit 77fed5d052
  1. 3
      src/main/java/com/teaching/backend/common/ErrorCode.java
  2. 9
      src/main/java/com/teaching/backend/controller/courses/CoursesController.java
  3. 79
      src/main/java/com/teaching/backend/controller/message/MessagesController.java
  4. 83
      src/main/java/com/teaching/backend/controller/message/UserInboxController.java
  5. 28
      src/main/java/com/teaching/backend/controller/umsAdmin/UmsStudentManageController.java
  6. 16
      src/main/java/com/teaching/backend/mapper/message/MessagesMapper.java
  7. 19
      src/main/java/com/teaching/backend/mapper/message/UserInboxMapper.java
  8. 14
      src/main/java/com/teaching/backend/model/dto/message/SysMessageDTO.java
  9. 2
      src/main/java/com/teaching/backend/model/entity/courses/StudentCourses.java
  10. 56
      src/main/java/com/teaching/backend/model/entity/message/SysMessages.java
  11. 50
      src/main/java/com/teaching/backend/model/entity/message/UserInbox.java
  12. 6
      src/main/java/com/teaching/backend/model/query/CourseQuery.java
  13. 24
      src/main/java/com/teaching/backend/model/query/MessagesQuery.java
  14. 1
      src/main/java/com/teaching/backend/model/query/PageQuery.java
  15. 20
      src/main/java/com/teaching/backend/model/vo/message/CourseUserInfVO.java
  16. 53
      src/main/java/com/teaching/backend/model/vo/message/SysMessageVO.java
  17. 21
      src/main/java/com/teaching/backend/model/vo/message/UserReadMessageTimeVO.java
  18. 3
      src/main/java/com/teaching/backend/service/courses/ICoursesService.java
  19. 171
      src/main/java/com/teaching/backend/service/impl/courses/CoursesServiceImpl.java
  20. 2
      src/main/java/com/teaching/backend/service/impl/courses/StudentCoursesServiceImpl.java
  21. 142
      src/main/java/com/teaching/backend/service/impl/message/MessagesServiceImpl.java
  22. 438
      src/main/java/com/teaching/backend/service/impl/message/UserInboxServiceImpl.java
  23. 134
      src/main/java/com/teaching/backend/service/impl/umsAdmin/UmsStudentManageServiceImpl.java
  24. 2
      src/main/java/com/teaching/backend/service/impl/umsAdmin/UmsUserServiceImpl.java
  25. 27
      src/main/java/com/teaching/backend/service/message/IMessagesService.java
  26. 32
      src/main/java/com/teaching/backend/service/message/IUserInboxService.java
  27. 7
      src/main/java/com/teaching/backend/service/umsAdmin/UmsStudentManageService.java
  28. 12
      src/main/resources/mapper/UserInboxMapper.xml

@ -20,7 +20,8 @@ public enum ErrorCode {
CONTENT_NOT_EXIT(40008, "内容id不存在"),
KNOWS_EXIT(40009, "该项下面存在关联的知识点,请在删除关联的知识点后再来操作!"),
INVALID_ROLE(400010, "角色不存在"),
MESSAGE_NO_READ(400011, "消息未读,禁止删除!"),
MESSAGE_NO_EXIT(400012, "消息不存在!"),
NOT_LOGIN_ERROR(40100, "未登录"),
NO_AUTH_ERROR(40101, "无权限"),
NOT_FOUND_ERROR(40400, "请求数据不存在"),

@ -12,6 +12,7 @@ import com.teaching.backend.model.entity.umsAdmin.UmsStudent;
import com.teaching.backend.model.query.CourseQuery;
import com.teaching.backend.model.vo.courses.CoursesVO;
import com.teaching.backend.model.vo.courses.PersonalCenterStudentListVO;
import com.teaching.backend.model.vo.message.CourseUserInfVO;
import com.teaching.backend.service.courses.ICoursesService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
@ -138,4 +139,12 @@ public class CoursesController {
return CommonResult.success(umsStudentList);
}
@ApiOperation("根据用户id,查询与用户有关的课程列表,并在课程列表显示所有用户信息")
// @ValidateParams({"userId"})
@GetMapping("/page/courseusers")
public BaseResponse<List<CourseUserInfVO>> getUserInfByCourse(@RequestParam Long userId){
List<CourseUserInfVO> courseUsersInfList = coursesService.getUserInfByCourse(userId);
return ResultUtils.success(courseUsersInfList);
}
}

@ -0,0 +1,79 @@
package com.teaching.backend.controller.message;
import com.teaching.backend.common.BaseResponse;
import com.teaching.backend.common.ResultUtils;
import com.teaching.backend.filter.ValidateParams;
import com.teaching.backend.model.dto.courses.PageDTO;
import com.teaching.backend.model.dto.message.SysMessageDTO;
import com.teaching.backend.model.entity.message.SysMessages;
import com.teaching.backend.model.query.CourseQuery;
import com.teaching.backend.model.query.MessagesQuery;
import com.teaching.backend.model.vo.courses.CoursesVO;
import com.teaching.backend.model.vo.message.SysMessageVO;
import com.teaching.backend.service.message.IMessagesService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.time.LocalDateTime;
import java.util.List;
/**
* <p>
* 前端控制器
* </p>
*
* @author zjh
* @since 2024-08-06
*/
@Api(tags = "系统消息管理接口")
@RestController
@RequestMapping("/messages")
public class MessagesController {
@Autowired
private IMessagesService messagesService;
@ApiOperation("发送消息")
@ValidateParams({"senderId","title","content","receiverId"}) // 需要校验的参数
@PostMapping("/addmessage")
public BaseResponse<String> sendMessage(@RequestBody SysMessageDTO sysMessageDTO){
String data = messagesService.sendMessageToUserInbox(sysMessageDTO);
return ResultUtils.success(data);
}
//TODO 功能待完善
@ApiOperation("删除发件箱的消息--批删--逻辑删除")
@ValidateParams({"messageIds"}) // 需要校验的参数
@DeleteMapping ("/deletesendmessage")
public BaseResponse<String> deleteMessages(@RequestParam List<Long> messageIds){
String data = messagesService.deleteSendMessagesByLogical(messageIds);
return ResultUtils.success(data);
}
@ApiOperation("根据id查询消息")
@ValidateParams({"id"}) // 需要校验的参数
@GetMapping ("/getmessage/{id}")
public BaseResponse<SysMessages> getMessageById(@PathVariable("id") Long id){
SysMessages data = messagesService.getById(id);
return ResultUtils.success(data);
}
/**
* 修改消息
* @param sysMessageDTO
* @return
*/
@ApiOperation("修改消息--仅限于未发送成功的消息(草稿功能)")
@PutMapping ("/modifymessage")
public BaseResponse<String> modifyMessage(@RequestBody SysMessageDTO sysMessageDTO){
boolean b = messagesService.updateById(sysMessageDTO);
return ResultUtils.success(b ? "修改成功" : "修改失败");
}
}

@ -0,0 +1,83 @@
package com.teaching.backend.controller.message;
import com.teaching.backend.common.BaseResponse;
import com.teaching.backend.common.ResultUtils;
import com.teaching.backend.filter.ValidateParams;
import com.teaching.backend.model.dto.courses.PageDTO;
import com.teaching.backend.model.query.MessagesQuery;
import com.teaching.backend.model.vo.message.SysMessageVO;
import com.teaching.backend.service.message.IUserInboxService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
/**
* <p>
* 前端控制器
* </p>
*
* @author zjh
* @since 2024-08-06
*/
@Api(tags = "用户信箱管理接口")
@RestController
@RequestMapping("/user-inbox")
public class UserInboxController {
@Autowired
private IUserInboxService userInboxService;
@ApiOperation("用户分页查看个人消息(收件箱)")
@ValidateParams({"userId","isDelete"}) // 需要校验的参数
@GetMapping("/page/receivemessages")
public BaseResponse<PageDTO<SysMessageVO>> getMessages(MessagesQuery messagesQuery){
PageDTO<SysMessageVO> messageList = userInboxService.queryReceiveMessages(messagesQuery);
return ResultUtils.success(messageList);
}
@ApiOperation("用户分页查看个人消息(发件箱)")
@ValidateParams({"userId","isDelete","isSend"})
@GetMapping("/page/sendmessages")
public BaseResponse<PageDTO<SysMessageVO>> getSendMessages(MessagesQuery messagesQuery){
PageDTO<SysMessageVO> messageList = userInboxService.querySendMessages(messagesQuery);
return ResultUtils.success(messageList);
}
//用户查看消息详情,如果是第一次查看,则会更新“已读状态”和“阅读时间”
@ApiOperation("查看消息详情")
@ValidateParams({"userId","messageId"}) // 需要校验的参数
@GetMapping("/getmessagedetails")
public BaseResponse<SysMessageVO> getMessageDetail(MessagesQuery messagesQuery){
SysMessageVO userCheckMessageVO = userInboxService.getMessageDetail(messagesQuery);
return ResultUtils.success(userCheckMessageVO);
}
@ApiOperation("获取已读/未读用户集合")
@ValidateParams({"userId", "messageId"})
@GetMapping("/message/readstatus")
public BaseResponse<SysMessageVO> getMessageReadStatus(@RequestParam Long userId, @RequestParam Long messageId) {
SysMessageVO messageVO = userInboxService.getReadStatus(userId, messageId);
return ResultUtils.success(messageVO);
}
@ApiOperation("一键已读")
@PutMapping ("/readmessagesbatch")
public BaseResponse<String> readMessagesBatch(@RequestParam List<Long> messageIds, @RequestParam Long userId){
String data = userInboxService.readMessagesBatch(messageIds,userId);
return ResultUtils.success(data);
}
@ApiOperation("删除收件箱的消息--批删--逻辑删除")
@ValidateParams({"messageIds","userId"}) // 需要校验的参数
@DeleteMapping ("/deletereceivemessage")
public BaseResponse<String> deleteReceiveMessages(@RequestParam List<Long> messageIds,@RequestParam Long userId){
String data = userInboxService.deleteReceiveMessagesByLogical(messageIds,userId);
return ResultUtils.success(data);
}
}

@ -1,5 +1,6 @@
package com.teaching.backend.controller.umsAdmin;
import com.teaching.backend.common.BaseResponse;
import com.teaching.backend.common.CommonResult;
import com.teaching.backend.model.dto.umsAdmin.UmsStudentPageQueryDTO;
import com.teaching.backend.model.entity.umsAdmin.UmsStudentManage;
@ -9,6 +10,9 @@ import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@RestController
@ -65,8 +69,30 @@ public class UmsStudentManageController {
@PutMapping
@ApiOperation("编辑学生信息")
public CommonResult update(@RequestBody UmsStudentManage umsStudentManage){
log.info("编辑学生信息: {}",umsStudentManage);
umsStudentManageService.updateStudentAndUser(umsStudentManage);
return CommonResult.success("修改成功");
}
/**
* 根据ids批量删除
* @param ids
* @return
*/
@ApiOperation("学生信息批量删除")
@DeleteMapping("/batchDelete")
public CommonResult<String> batchDelete(@RequestParam List<Long> ids){
return umsStudentManageService.batchRemove(ids);
}
/**
* 根据ids初始密码
* @param ids
* @return
*/
@ApiOperation("学生信息批量初始密码")
@PutMapping("/initialPassword")
public CommonResult<String> initialPassword(@RequestParam List<Long> ids){
return umsStudentManageService.initialPassword(ids);
}
}

@ -0,0 +1,16 @@
package com.teaching.backend.mapper.message;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.teaching.backend.model.entity.message.SysMessages;
/**
* <p>
* Mapper 接口
* </p>
*
* @author zjh
* @since 2024-08-06
*/
public interface MessagesMapper extends BaseMapper<SysMessages> {
}

@ -0,0 +1,19 @@
package com.teaching.backend.mapper.message;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.teaching.backend.model.entity.message.UserInbox;
import java.util.List;
/**
* <p>
* Mapper 接口
* </p>
*
* @author zjh
* @since 2024-08-06
*/
public interface UserInboxMapper extends BaseMapper<UserInbox> {
boolean insertBatchInBox(List<UserInbox> userInboxes);
}

@ -0,0 +1,14 @@
package com.teaching.backend.model.dto.message;
import com.teaching.backend.model.entity.message.SysMessages;
import io.swagger.annotations.ApiModel;
import lombok.Data;
@Data
@ApiModel(description = "教师创建课程参数实体")
public class SysMessageDTO extends SysMessages {
/**
* 收件人id集合
*/
private String receiverId;
}

@ -33,7 +33,7 @@ public class StudentCourses implements Serializable {
/**
* 学生的userId
*/
private String student;
private Long student;
/**
* 课程id

@ -0,0 +1,56 @@
package com.teaching.backend.model.entity.message;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import java.time.LocalDateTime;
import java.io.Serializable;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;
/**
* <p>
*
* </p>
*
* @author zjh
* @since 2024-08-06
*/
@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@TableName("sys_messages")
@ApiModel(value="SysMessages对象", description="")
public class SysMessages implements Serializable {
private static final long serialVersionUID = 1L;
@TableId(value = "id", type = IdType.AUTO)
private Long id;
@ApiModelProperty(value = "发件人id")
private Long senderId;
@ApiModelProperty(value = "课程id")
private String courseId;
@ApiModelProperty(value = "消息标题")
private String title;
@ApiModelProperty(value = "消息内容")
private String content;
@ApiModelProperty(value = "发送时间")
private LocalDateTime sendTime;
@ApiModelProperty(value = "是否删除(逻辑删除)")
private Integer isDelete;
@ApiModelProperty(value = "是否发送(未发送的存草稿)")
private Boolean isSend;
}

@ -0,0 +1,50 @@
package com.teaching.backend.model.entity.message;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import java.time.LocalDateTime;
import java.io.Serializable;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;
/**
* <p>
*
* </p>
*
* @author zjh
* @since 2024-08-06
*/
@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@TableName("user_inbox")
@ApiModel(value="UserInbox对象", description="")
public class UserInbox implements Serializable {
private static final long serialVersionUID = 1L;
@TableId(value = "id", type = IdType.AUTO)
private Long id;
@ApiModelProperty(value = "消息id")
private Long messageId;
@ApiModelProperty(value = "接收人id")
private Long receiverId;
@ApiModelProperty(value = "消息查看时间")
private LocalDateTime readTime;
@ApiModelProperty(value = "是否已读(0没有,1有)")
private Boolean isRead;
@ApiModelProperty(value = "是否删除(0没有,1有)")
private Boolean isDelete;
}

@ -25,11 +25,11 @@ public class CourseQuery extends PageQuery {
private String teacher;
@ApiModelProperty("课程类别")
private String category;
private Integer category;
@ApiModelProperty("课程性质")
private String nature;
private Integer nature;
@ApiModelProperty("课程考核类型")
private String assessmenttype;
private Integer assessmenttype;
}

@ -0,0 +1,24 @@
package com.teaching.backend.model.query;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
@EqualsAndHashCode(callSuper = true)
@Data
@ApiModel(description = "系统消息查询条件实体")
public class MessagesQuery extends PageQuery {
@ApiModelProperty(value = "用户的userId",required = true)
private Long userId;
@ApiModelProperty(value = "消息id")
private Long messageId;
@ApiModelProperty(value = "是否发送 false没有 true已发送")
private Boolean isSend;
@ApiModelProperty(value = "是否已读 false没有 true已读 可以作为消息筛选条件 用户不选这个就不用传")
private Boolean isRead;
@ApiModelProperty(value = "是否删除 0没有 1回收站 2回收站已删除(发件人不可见,收件人可见)")
private Integer isDelete;
}

@ -41,4 +41,5 @@ public class PageQuery {
public <T> Page<T> toMpPageDefaultSortByUpdateTime(){
return toMpPage(new OrderItem("update_time", false));
}
}

@ -0,0 +1,20 @@
package com.teaching.backend.model.vo.message;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import java.time.LocalDateTime;
import java.util.List;
@Data
public class CourseUserInfVO {
@ApiModelProperty(value = "课程id")
private String courseId;
@ApiModelProperty(value = "课程name")
private String courseName;
@ApiModelProperty(value = "与课程有关的用户信息集合")
private List<UserReadMessageTimeVO> usersInf;
}

@ -0,0 +1,53 @@
package com.teaching.backend.model.vo.message;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
@Data
public class SysMessageVO {
@ApiModelProperty("id")
private Long id;
@ApiModelProperty("课程id")
private String courseId;
@ApiModelProperty("课程名称")
private String courseName;
@ApiModelProperty(value = "发件人id")
private Long senderId;
@ApiModelProperty(value = "发件人name")
private String senderName;
@ApiModelProperty(value = "消息标题")
private String title;
@ApiModelProperty(value = "消息内容")
private String content;
@ApiModelProperty(value = "发送时间")
private LocalDateTime sendTime;
@ApiModelProperty(value = "收信总人数")
private Long userNum;
@ApiModelProperty(value = "已读用户人数")
private Long readUserNum;
@ApiModelProperty(value = "收件用户信息集合")
private List<UserReadMessageTimeVO> receiveUsers;
@ApiModelProperty(value = "是否已读(0没有,1有)")
private Boolean isRead;
@ApiModelProperty(value = "已读用户集合")
private List<UserReadMessageTimeVO> readUsers;
@ApiModelProperty(value = "未读用户集合")
private List<UserReadMessageTimeVO> noReadUsers;
}

@ -0,0 +1,21 @@
package com.teaching.backend.model.vo.message;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import java.time.LocalDateTime;
import java.util.List;
@Data
public class UserReadMessageTimeVO {
@ApiModelProperty(value = "接收人id")
private Long id;
@ApiModelProperty(value = "接收人name")
private String receiverName;
@ApiModelProperty(value = "消息查看时间")
private LocalDateTime readTime;
}

@ -11,6 +11,7 @@ import com.teaching.backend.model.entity.umsAdmin.UmsUser;
import com.teaching.backend.model.query.CourseQuery;
import com.teaching.backend.model.vo.courses.CoursesVO;
import com.teaching.backend.model.vo.courses.PersonalCenterStudentListVO;
import com.teaching.backend.model.vo.message.CourseUserInfVO;
import com.teaching.backend.model.vo.umsAdmin.UmsStudentVO;
import javax.servlet.http.HttpServletResponse;
@ -51,4 +52,6 @@ public interface ICoursesService extends IService<Courses> {
List<PersonalCenterStudentListVO> queryStudentList(String userId);
LinkedHashSet<UmsStudentManage> queryTeacherByStudentList(String userId);
List<CourseUserInfVO> getUserInfByCourse(Long userId);
}

@ -32,9 +32,12 @@ import com.teaching.backend.model.query.CourseQuery;
import com.teaching.backend.model.vo.courses.CourseObjectivesTreeVO;
import com.teaching.backend.model.vo.courses.CoursesVO;
import com.teaching.backend.model.vo.courses.PersonalCenterStudentListVO;
import com.teaching.backend.model.vo.message.CourseUserInfVO;
import com.teaching.backend.model.vo.message.UserReadMessageTimeVO;
import com.teaching.backend.model.vo.umsAdmin.UmsStudentVO;
import com.teaching.backend.service.courses.ICoursesService;
import com.teaching.backend.utils.CourseCode;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.xwpf.usermodel.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
@ -62,6 +65,7 @@ import static com.teaching.backend.constant.RoleConstants.TEACHER_ROLE;
* @since 2024-05-30
*/
@Service
@Slf4j
public class CoursesServiceImpl extends ServiceImpl<CoursesMapper, Courses> implements ICoursesService {
@Autowired
@ -195,10 +199,10 @@ public class CoursesServiceImpl extends ServiceImpl<CoursesMapper, Courses> impl
Page<Courses> p = lambdaQuery()
.like(courseQuery.getName() != null, Courses::getName, courseQuery.getName())
.apply("FIND_IN_SET({0}, teacher)", courseQuery.getUserId())
.eq(StringUtils.hasText(courseQuery.getCategory()), Courses::getCategory, courseQuery.getCategory())
// .eq(courseQuery.getCategory() != null && !courseQuery.getCategory().isEmpty(), Courses::getCategory, courseQuery.getCategory())
.eq(courseQuery.getNature() != null && !courseQuery.getNature().isEmpty(), Courses::getNature, courseQuery.getNature())
.eq(courseQuery.getAssessmenttype() != null &&!courseQuery.getAssessmenttype().isEmpty(), Courses::getAssessmenttype, courseQuery.getAssessmenttype())
// .eq(StringUtils.hasText(courseQuery.getCategory()), Courses::getCategory, courseQuery.getCategory())
.eq(courseQuery.getCategory() != null, Courses::getCategory, courseQuery.getCategory())
.eq(courseQuery.getNature() != null, Courses::getNature, courseQuery.getNature())
.eq(courseQuery.getAssessmenttype() != null, Courses::getAssessmenttype, courseQuery.getAssessmenttype())
.select(Courses::getId,Courses::getTeacher,Courses::getImg,Courses::getName,Courses::getCredit,Courses::getClasshours)
.page(page);
return PageDTO.of(p,CoursesVO.class);
@ -222,9 +226,9 @@ public class CoursesServiceImpl extends ServiceImpl<CoursesMapper, Courses> impl
Page<Courses> page = courseQuery.toMpPageDefaultSortByCreateTime();
Page<Courses> p = lambdaQuery()
.like(courseQuery.getName() != null && !courseQuery.getName().isEmpty(), Courses::getName, courseQuery.getName())
.eq(courseQuery.getCategory() != null && !courseQuery.getCategory().isEmpty(), Courses::getCategory, courseQuery.getCategory())
.eq(courseQuery.getNature() != null && !courseQuery.getNature().isEmpty(), Courses::getNature, courseQuery.getNature())
.eq(courseQuery.getAssessmenttype() != null &&!courseQuery.getAssessmenttype().isEmpty(), Courses::getAssessmenttype, courseQuery.getAssessmenttype())
.eq(courseQuery.getCategory() != null, Courses::getCategory, courseQuery.getCategory())
.eq(courseQuery.getNature() != null, Courses::getNature, courseQuery.getNature())
.eq(courseQuery.getAssessmenttype() != null, Courses::getAssessmenttype, courseQuery.getAssessmenttype())
.apply(courseQuery.getTeacher() != null && !courseQuery.getTeacher().isEmpty(), "FIND_IN_SET({0}, teacher)", courseQuery.getTeacher())
.select(Courses::getId,Courses::getTeacher,Courses::getImg,Courses::getName,Courses::getCredit,Courses::getClasshours)
.page(page);
@ -382,7 +386,7 @@ public class CoursesServiceImpl extends ServiceImpl<CoursesMapper, Courses> impl
List<StudentCourses> studentUsernames = studentCoursesMapper.selectBatchSomeStudent(courseId);
List<CourseLearningRecord> courseLearningRecordList = new ArrayList<>();
for (StudentCourses studentUsername : studentUsernames) {
CourseLearningRecord courseLearningRecord = courseLearningRecordMapper.selectNewRecord(studentUsername.getStudent());
CourseLearningRecord courseLearningRecord = courseLearningRecordMapper.selectNewRecord(String.valueOf(studentUsername.getStudent()));
courseLearningRecordList.add(courseLearningRecord);
}
courseLearningRecordList = courseLearningRecordList.stream().sorted(Comparator.comparing(CourseLearningRecord::getTimes).reversed()).collect(Collectors.toList());
@ -396,6 +400,8 @@ public class CoursesServiceImpl extends ServiceImpl<CoursesMapper, Courses> impl
throw new BusinessException(400,"只有老师才有此功能");
}
@Override
public List<CoursesVO> getPagePageSize(int page, int pageSize) {
int startIndex = (page-1) * pageSize;
@ -610,6 +616,155 @@ public class CoursesServiceImpl extends ServiceImpl<CoursesMapper, Courses> impl
document.write(outputStream);
}
@Override
public List<CourseUserInfVO> getUserInfByCourse(Long userId) {
UmsUser umsUser = umsUserMapper.selectById(userId);
if (umsUser == null){
throw new BusinessException(ErrorCode.PARAMS_USER_NOTEXISTS);
}
int roleId = Integer.parseInt(umsUser.getRoleId());
List<CourseUserInfVO> courseUserInfVO;
switch (roleId) {
//教师
case 1:
courseUserInfVO = queryCourseUserForTeacher(userId);
break;
//学生
case 2:
courseUserInfVO = queryCourseUserForStudent(userId);
break;
default:
throw new BusinessException(ErrorCode.INVALID_ROLE);
}
return courseUserInfVO;
}
private List<CourseUserInfVO> queryCourseUserForTeacher(Long userId) {
// 查询所有与该教师相关的课程
List<Courses> teacherCoursesInf = coursesMapper.selectList(
new LambdaQueryWrapper<Courses>()
.apply("FIND_IN_SET({0}, teacher)", userId)
.select(Courses::getId, Courses::getTeacher)
);
List<CourseUserInfVO> courseUserInfVOS = new ArrayList<>();
for (Courses courses : teacherCoursesInf) {
CourseUserInfVO courseUserInfVO = new CourseUserInfVO();
List<UserReadMessageTimeVO> userReadMessageTimeVOS = new ArrayList<>();
courseUserInfVO.setCourseId(courses.getId());
// 查询与课程相关的所有学生
List<Long> studentIds = studentCoursesMapper.selectList(
new LambdaQueryWrapper<>(StudentCourses.class)
.eq(StudentCourses::getCourse, courses.getId())
).stream().map(StudentCourses::getStudent).collect(Collectors.toList());
// 为每个学生创建一个新的 UserReadMessageTimeVO 对象
for (Long studentId : studentIds) {
UmsStudent umsStudent = umsStudentMapper.selectOne(
new LambdaQueryWrapper<>(UmsStudent.class)
.eq(UmsStudent::getUserId, studentId)
.select(UmsStudent::getName)
);
if (umsStudent == null) {
log.error("用户不存在: {}",studentId);
throw new BusinessException(ErrorCode.PARAMS_USER_NOTEXISTS);
}
UserReadMessageTimeVO userReadMessageTimeVO = new UserReadMessageTimeVO(); // 在循环中创建新对象
userReadMessageTimeVO.setId(studentId);
userReadMessageTimeVO.setReceiverName(umsStudent.getName());
userReadMessageTimeVOS.add(userReadMessageTimeVO);
}
// 为每个教师创建一个新的 UserReadMessageTimeVO 对象
String[] teacherUserIds = courses.getTeacher().split(",");
for (String teacherId : teacherUserIds) {
UmsTeacher umsTeacher = umsTeacherMapper.selectOne(
new LambdaQueryWrapper<UmsTeacher>()
.eq(UmsTeacher::getUserId, teacherId)
);
if (umsTeacher == null) {
log.error("教师不存在: {}",teacherId);
throw new BusinessException(ErrorCode.PARAMS_USER_NOTEXISTS);
}
UserReadMessageTimeVO userReadMessageTimeVO = new UserReadMessageTimeVO(); // 在循环中创建新对象
userReadMessageTimeVO.setId(umsTeacher.getUserId());
userReadMessageTimeVO.setReceiverName(umsTeacher.getName());
userReadMessageTimeVOS.add(userReadMessageTimeVO);
}
courseUserInfVO.setUsersInf(userReadMessageTimeVOS);
courseUserInfVOS.add(courseUserInfVO);
}
return courseUserInfVOS;
}
private List<CourseUserInfVO> queryCourseUserForStudent(Long userId) {
// 查询学生所参与的所有课程
List<StudentCourses> studentCoursesList = studentCoursesMapper.selectList(
new LambdaQueryWrapper<StudentCourses>()
.eq(StudentCourses::getStudent, userId)
.select(StudentCourses::getCourse)
);
List<CourseUserInfVO> courseUserInfVOS = new ArrayList<>();
for (StudentCourses studentCourse : studentCoursesList) {
CourseUserInfVO courseUserInfVO = new CourseUserInfVO();
List<UserReadMessageTimeVO> userReadMessageTimeVOS = new ArrayList<>();
courseUserInfVO.setCourseId(studentCourse.getCourse());
// 查询与课程相关的所有学生
List<Long> studentIds = studentCoursesMapper.selectList(
new LambdaQueryWrapper<>(StudentCourses.class)
.eq(StudentCourses::getCourse, studentCourse.getCourse())
).stream().map(StudentCourses::getStudent).collect(Collectors.toList());
// 为每个学生创建一个新的 UserReadMessageTimeVO 对象
for (Long studentId : studentIds) {
UmsStudent umsStudent = umsStudentMapper.selectOne(
new LambdaQueryWrapper<>(UmsStudent.class)
.eq(UmsStudent::getUserId, studentId)
.select(UmsStudent::getName)
);
if (umsStudent == null) {
log.error("用户不存在: {}", studentId);
throw new BusinessException(ErrorCode.PARAMS_USER_NOTEXISTS);
}
UserReadMessageTimeVO userReadMessageTimeVO = new UserReadMessageTimeVO(); // 在循环中创建新对象
userReadMessageTimeVO.setId(studentId);
userReadMessageTimeVO.setReceiverName(umsStudent.getName());
userReadMessageTimeVOS.add(userReadMessageTimeVO);
}
// 查询与课程相关的所有教师
Courses course = coursesMapper.selectById(studentCourse.getCourse());
if (course != null) {
String[] teacherUserIds = course.getTeacher().split(",");
for (String teacherId : teacherUserIds) {
UmsTeacher umsTeacher = umsTeacherMapper.selectOne(
new LambdaQueryWrapper<UmsTeacher>()
.eq(UmsTeacher::getUserId, teacherId)
);
if (umsTeacher == null) {
log.error("教师不存在: {}", teacherId);
throw new BusinessException(ErrorCode.PARAMS_USER_NOTEXISTS);
}
UserReadMessageTimeVO userReadMessageTimeVO = new UserReadMessageTimeVO(); // 在循环中创建新对象
userReadMessageTimeVO.setId(umsTeacher.getUserId());
userReadMessageTimeVO.setReceiverName(umsTeacher.getName());
userReadMessageTimeVOS.add(userReadMessageTimeVO);
}
} else {
log.error("课程不存在: {}", studentCourse.getCourse());
throw new BusinessException(ErrorCode.PARAMS_COURSE_NOTEXISTS);
}
courseUserInfVO.setUsersInf(userReadMessageTimeVOS);
courseUserInfVOS.add(courseUserInfVO);
}
return courseUserInfVOS;
}
}

@ -53,7 +53,7 @@ public class StudentCoursesServiceImpl extends ServiceImpl<StudentCoursesMapper,
@Override
public String saveCoursesToStudents(StudentCoursesDTO studentCoursesDTO) {
String studentId = studentCoursesDTO.getStudent();
Long studentId = studentCoursesDTO.getStudent();
if (studentId == null || studentId.equals("")) {
throw new BusinessException(ErrorCode.STUDENT_NOT_EXIT);
}

@ -0,0 +1,142 @@
package com.teaching.backend.service.impl.message;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.teaching.backend.common.ErrorCode;
import com.teaching.backend.exception.BusinessException;
import com.teaching.backend.mapper.message.MessagesMapper;
import com.teaching.backend.mapper.message.UserInboxMapper;
import com.teaching.backend.model.dto.message.SysMessageDTO;
import com.teaching.backend.model.entity.message.SysMessages;
import com.teaching.backend.model.entity.message.UserInbox;
import com.teaching.backend.service.message.IMessagesService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.ThreadLocalRandom;
import java.util.stream.Collectors;
/**
* <p>
* 服务实现类
* </p>
*
* @author zjh
* @since 2024-08-06
*/
@Service
@Slf4j
public class MessagesServiceImpl extends ServiceImpl<MessagesMapper, SysMessages> implements IMessagesService {
@Autowired
private UserInboxMapper userInboxMapper;
@Override
@Transactional
public String sendMessageToUserInbox(SysMessageDTO sysMessageDTO) {
// 1.数据非空校验(已完成)
// 2.消息ID检查,如果存在,说明是草稿箱编辑过的消息,否则生成新的ID
Long messageId = Optional.ofNullable(sysMessageDTO.getId())
.orElseGet(() -> System.currentTimeMillis() + ThreadLocalRandom.current().nextLong(1000));
// 3.解析传过来的receiverId
List<Long> receiverIds = Arrays.stream(sysMessageDTO.getReceiverId().split(","))
.map(Long::valueOf)
.collect(Collectors.toList());
// 4.数据写入收件箱
List<UserInbox> userInboxes = receiverIds.stream()
.map(receiverId -> {
UserInbox userInbox = new UserInbox();
userInbox.setMessageId(messageId);
userInbox.setReceiverId(receiverId);
return userInbox;
})
.collect(Collectors.toList());
// 执行批量插入操作,并判断是否成功
boolean inboxInsertSuccess = userInboxMapper.insertBatchInBox(userInboxes);
if (!inboxInsertSuccess) {
throw new BusinessException(ErrorCode.OPERATION_ERROR, "收件箱数据插入失败!");
}
// 5.更新或保存消息数据
SysMessages sysMessages = new SysMessages();
BeanUtils.copyProperties(sysMessageDTO, sysMessages);
sysMessages.setId(messageId);
sysMessages.setSendTime(LocalDateTime.now());
sysMessages.setIsSend(true);
boolean messageSaveSuccess;
if (sysMessageDTO.getId() != null) {
messageSaveSuccess = updateById(sysMessages);
} else {
messageSaveSuccess = save(sysMessages); // 使用 save 方法插入新的记录
}
if (!messageSaveSuccess) {
throw new BusinessException(ErrorCode.OPERATION_ERROR, "消息保存失败!");
}
return "消息发送成功!";
}
/**
* 发件箱的删除
*
* @param messageIds 消息ID列表
* @return 删除结果
*/
@Override
@Transactional
public String deleteSendMessagesByLogical(List<Long> messageIds) {
for (Long messageId : messageIds) {
// 1.查询消息体
SysMessages message = query().getBaseMapper().selectById(messageId);
if (message == null) {
log.error("消息未找到,消息id:{}", messageId);
continue;
}
// 2.根据消息状态处理删除逻辑
switch (message.getIsDelete()) {
case 0: // 未删除状态
if (Boolean.FALSE.equals(message.getIsSend())) {
// 未发送,物理删除
if (query().getBaseMapper().deleteById(messageId) < 1) {
log.error("删除失败,消息id:{}", messageId);
}
} else {
// 已发送,标记为回收站
updateMessageDeleteStatus(message, 1);
}
break;
case 1: // 回收站状态
// 标记为彻底删除
updateMessageDeleteStatus(message, 2);
break;
default:
log.warn("未知删除状态,消息id:{}", messageId);
break;
}
}
return "删除成功";
}
private void updateMessageDeleteStatus(SysMessages message, int deleteStatus) {
message.setIsDelete(deleteStatus);
if (query().getBaseMapper().updateById(message) < 1) {
log.error("删除失败,消息id:{}", message.getId());
}
}
}

@ -0,0 +1,438 @@
package com.teaching.backend.service.impl.message;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.teaching.backend.common.ErrorCode;
import com.teaching.backend.exception.BusinessException;
import com.teaching.backend.mapper.courses.CoursesMapper;
import com.teaching.backend.mapper.message.UserInboxMapper;
import com.teaching.backend.mapper.umsAdmin.UmsStudentMapper;
import com.teaching.backend.mapper.umsAdmin.UmsTeacherMapper;
import com.teaching.backend.model.dto.courses.PageDTO;
import com.teaching.backend.model.entity.courses.Courses;
import com.teaching.backend.model.entity.message.SysMessages;
import com.teaching.backend.model.entity.message.UserInbox;
import com.teaching.backend.model.entity.umsAdmin.UmsStudent;
import com.teaching.backend.model.entity.umsAdmin.UmsTeacher;
import com.teaching.backend.model.query.MessagesQuery;
import com.teaching.backend.model.vo.message.SysMessageVO;
import com.teaching.backend.model.vo.message.UserReadMessageTimeVO;
import com.teaching.backend.service.message.IMessagesService;
import com.teaching.backend.service.message.IUserInboxService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;
/**
* <p>
* 服务实现类
* </p>
*
* @author zjh
* @since 2024-08-06
*/
@Service
@Slf4j
public class UserInboxServiceImpl extends ServiceImpl<UserInboxMapper, UserInbox> implements IUserInboxService {
@Autowired
private UmsTeacherMapper umsTeacherMapper;
@Autowired
private UmsStudentMapper umsStudentMapper;
@Autowired
private CoursesMapper coursesMapper;
@Autowired
private IMessagesService messagesService;
@Autowired
private UserInboxMapper userInboxMapper;
//TODO 不需要特别指定“已读”或者是“未读”
@Override
public PageDTO<SysMessageVO> queryReceiveMessages(MessagesQuery messagesQuery) {
// 先按用户的id找到其的消息列表 默认看未删除的,(是否读 作为筛选条件后传进去)
LambdaQueryWrapper<UserInbox> userInboxQueryWrapper = new LambdaQueryWrapper<>();
userInboxQueryWrapper
.eq(UserInbox::getReceiverId, messagesQuery.getUserId())
.eq(messagesQuery.getIsRead() != null,UserInbox::getIsRead,messagesQuery.getIsRead())
.eq(UserInbox::getIsDelete, messagesQuery.getIsDelete());
List<UserInbox> userInboxes = userInboxMapper.selectList(userInboxQueryWrapper);
if (userInboxes.isEmpty()){
throw new BusinessException(ErrorCode.MESSAGE_NO_EXIT);
}
Map<Long, Boolean> messageReadStatusMap = userInboxes.stream()
.collect(Collectors.toMap(UserInbox::getMessageId, UserInbox::getIsRead));
List<Long> messageIds = new ArrayList<>(messageReadStatusMap.keySet());
Page<SysMessages> page = messagesQuery.toMpPage("send_time", false);
// 根据条件查询SysMessages表
Page<SysMessages> p = messagesService.page(page,
new LambdaQueryWrapper<SysMessages>()
.in(SysMessages::getId,messageIds)
.select(SysMessages::getId,SysMessages::getCourseId,SysMessages::getTitle,SysMessages::getSenderId,SysMessages::getSendTime)
);
// 将Page<SysMessages>转换为PageDTO<SysMessageVO>
PageDTO<SysMessageVO> messages = PageDTO.of(p, SysMessageVO.class);
// PageDTO<SysMessageVO> messages = PageDTO.of(p, message -> {
// SysMessageVO vo = new SysMessageVO();
// BeanUtil.copyProperties(message, vo, CopyOptions.create().setIgnoreProperties(SysMessageVO::getContent));
// return vo;
// });
// 获取PageDTO中的SysMessageVO列表
List<SysMessageVO> messagesList = messages.getList();
// 填充附加数据
for (SysMessageVO messageVO : messagesList) {
//获取发件人信息
UmsTeacher umsTeacher = umsTeacherMapper.selectOne(
new LambdaQueryWrapper<UmsTeacher>()
.eq(UmsTeacher::getUserId, messageVO.getSenderId()));
//没有这个教师那说明就是学生
if (umsTeacher == null) {
messageVO.setSenderName(umsStudentMapper.selectOne(
new LambdaQueryWrapper<UmsStudent>()
.eq(UmsStudent::getUserId, messageVO.getSenderId())
).getName());
} else {
messageVO.setSenderName(umsTeacher.getName());
}
Courses course = coursesMapper.selectOne(new LambdaQueryWrapper<Courses>()
.eq(Courses::getId, messageVO.getCourseId()));
if (course != null) {
messageVO.setCourseName(course.getName());
}
// 设置isRead字段
Boolean isRead = messageReadStatusMap.get(messageVO.getId());
messageVO.setIsRead(isRead != null ? isRead : false);
}
// 设置消息列表并返回
messages.setList(messagesList);
return messages;
}
@Override
public PageDTO<SysMessageVO> querySendMessages(MessagesQuery messagesQuery) {
// 初始化一个按发送时间降序排序的Page对象
Page<SysMessages> page = messagesQuery.toMpPage("send_time", false);
// 根据条件查询SysMessages表
Page<SysMessages> p = messagesService.page(page,
new LambdaQueryWrapper<SysMessages>()
.eq(SysMessages::getSenderId, messagesQuery.getUserId())
.eq(SysMessages::getIsSend, messagesQuery.getIsSend())
.eq(SysMessages::getIsDelete, messagesQuery.getIsDelete())
.select(SysMessages::getId, SysMessages::getCourseId, SysMessages::getTitle, SysMessages::getSendTime)
);
// 将Page<SysMessages>转换为PageDTO<SysMessageVO>
PageDTO<SysMessageVO> messages = PageDTO.of(p, SysMessageVO.class);
List<SysMessageVO> messagesList = messages.getList();
if (messagesList.isEmpty()) {
throw new BusinessException(ErrorCode.MESSAGE_NO_EXIT);
}
// 批量查询课程名称
Set<String> courseIds = messagesList.stream()
.map(SysMessageVO::getCourseId)
.filter(Objects::nonNull)
.collect(Collectors.toSet());
Map<String, String> courseNames = coursesMapper.selectBatchIds(courseIds).stream()
.collect(Collectors.toMap(Courses::getId, Courses::getName));
// 批量查询所有用户收件箱数据
Set<Long> messageIds = messagesList.stream()
.map(SysMessageVO::getId)
.collect(Collectors.toSet());
List<UserInbox> allUserInboxes = userInboxMapper.selectList(
new LambdaQueryWrapper<UserInbox>()
.in(UserInbox::getMessageId, messageIds)
.select(UserInbox::getMessageId, UserInbox::getReceiverId, UserInbox::getIsRead)
);
// 将UserInbox数据按消息ID进行分组
Map<Long, List<UserInbox>> userInboxesByMessageId = allUserInboxes.stream()
.collect(Collectors.groupingBy(UserInbox::getMessageId));
// 缓存用户名称以避免重复查询
Map<Long, String> userNamesCache = new HashMap<>();
for (SysMessageVO sysMessageVO : messagesList) {
String courseId = sysMessageVO.getCourseId();
if (courseId != null) {
sysMessageVO.setCourseName(courseNames.get(courseId));
}
List<UserInbox> userInboxes = userInboxesByMessageId.get(sysMessageVO.getId());
if (userInboxes != null) {
sysMessageVO.setUserNum((long) userInboxes.size());
sysMessageVO.setReadUserNum(userInboxes.stream().filter(UserInbox::getIsRead).count());
List<UserReadMessageTimeVO> receiveUsers = userInboxes.stream().limit(5)
.map(userInbox -> {
UserReadMessageTimeVO vo = new UserReadMessageTimeVO();
Long receiverId = userInbox.getReceiverId();
vo.setId(receiverId);
// 获取或缓存接收者名称
String receiverName = userNamesCache.computeIfAbsent(receiverId, id -> {
UmsStudent umsStudent = umsStudentMapper.selectOne(
new LambdaQueryWrapper<UmsStudent>().eq(UmsStudent::getUserId, id)
);
if (umsStudent != null) {
return umsStudent.getName();
} else {
UmsTeacher umsTeacher = umsTeacherMapper.selectOne(
new LambdaQueryWrapper<UmsTeacher>().eq(UmsTeacher::getUserId, id)
);
return umsTeacher != null ? umsTeacher.getName() : null;
}
});
vo.setReceiverName(receiverName);
return vo;
}).collect(Collectors.toList());
sysMessageVO.setReceiveUsers(receiveUsers);
}
}
// 更新PageDTO中的消息列表并返回
messages.setList(messagesList);
return messages;
}
@Override
public SysMessageVO getMessageDetail(MessagesQuery messagesQuery) {
SysMessages sysMessage = messagesService.getById(messagesQuery.getMessageId());
SysMessageVO sysMessageVO = new SysMessageVO();
sysMessageVO.setId(messagesQuery.getMessageId());
sysMessageVO.setContent(sysMessage.getContent());
sysMessageVO.setSenderId(messagesQuery.getUserId());
// 获取发件人信息
UmsTeacher umsTeacher = umsTeacherMapper.selectOne(
new LambdaQueryWrapper<UmsTeacher>()
.eq(UmsTeacher::getUserId, sysMessage.getSenderId()));
if (umsTeacher == null) {
sysMessageVO.setSenderName(umsStudentMapper.selectOne(
new LambdaQueryWrapper<UmsStudent>()
.eq(UmsStudent::getUserId, sysMessage.getSenderId())
).getName());
} else {
sysMessageVO.setSenderName(umsTeacher.getName());
}
// 根据当前用户是发件人还是收件人决定查询的内容
if (sysMessage.getSenderId().equals(messagesQuery.getUserId())) {
// 发件人查看详情,查询所有收件人信息
List<UserInbox> userInboxes = userInboxMapper.selectList(
new LambdaQueryWrapper<UserInbox>()
.eq(UserInbox::getMessageId, messagesQuery.getMessageId())
);
List<UserReadMessageTimeVO> receiveUsers = userInboxes.stream().map(userInbox -> {
UserReadMessageTimeVO vo = new UserReadMessageTimeVO();
vo.setId(userInbox.getReceiverId());
UmsStudent umsStudent = umsStudentMapper.selectOne(
new LambdaQueryWrapper<UmsStudent>()
.eq(UmsStudent::getUserId, userInbox.getReceiverId()));
if (umsStudent == null) {
vo.setReceiverName(umsTeacherMapper.selectOne(
new LambdaQueryWrapper<UmsTeacher>()
.eq(UmsTeacher::getUserId, userInbox.getReceiverId())
).getName());
} else {
vo.setReceiverName(umsStudent.getName());
}
vo.setReadTime(userInbox.getReadTime());
return vo;
}).collect(Collectors.toList());
sysMessageVO.setReceiveUsers(receiveUsers);
} else {
// 收件人查看详情,无需再次查询收件人信息,前端可复用列表中已查询到的部分数据
UserInbox userInbox = userInboxMapper.selectOne(
new LambdaQueryWrapper<UserInbox>()
.eq(UserInbox::getMessageId, messagesQuery.getMessageId())
.eq(UserInbox::getReceiverId, messagesQuery.getUserId())
);
if (userInbox != null) {
if (userInbox.getReadTime() == null) {
userInbox.setReadTime(LocalDateTime.now());
}
if (!userInbox.getIsRead()) {
userInbox.setIsRead(true);
}
sysMessageVO.setIsRead(userInbox.getIsRead());
userInboxMapper.updateById(userInbox);
}
}
return sysMessageVO;
}
/**
* 收件箱消息删除逻辑删除
*
* @param messageIds 消息ID列表
* @param userId 用户ID
* @return 删除结果
*/
@Override
@Transactional
public String deleteReceiveMessagesByLogical(List<Long> messageIds, Long userId) {
for (Long messageId : messageIds) {
UserInbox userInbox = getUserInboxByMessageIdAndUserId(messageId, userId);
if (userInbox == null) {
log.error("未找到收件箱消息,消息id:{},用户id:{}", messageId, userId);
continue;
}
if (!userInbox.getIsRead()) {
log.error("此消息未读:{}", messageId);
throw new BusinessException(ErrorCode.MESSAGE_NO_READ);
}
if (!userInbox.getIsDelete()) {
userInbox.setIsDelete(true);
userInboxMapper.updateById(userInbox);
} else {
userInboxMapper.deleteById(userInbox);
}
}
return "删除成功!";
}
/**
* 一键已读
*
* @param messageIds 消息ID列表
* @param userId 用户ID
* @return 操作结果
*/
@Override
@Transactional
public String readMessagesBatch(List<Long> messageIds, Long userId) {
for (Long messageId : messageIds) {
UserInbox userInbox = getUserInboxByMessageIdAndUserId(messageId, userId);
if (userInbox == null) {
log.error("未找到收件箱消息,消息id:{},用户id:{}", messageId, userId);
continue;
}
if (!userInbox.getIsRead()) {
userInbox.setIsRead(true);
userInbox.setReadTime(LocalDateTime.now());
userInboxMapper.updateById(userInbox);
}
}
return "操作成功!";
}
private UserInbox getUserInboxByMessageIdAndUserId(Long messageId, Long userId) {
return userInboxMapper.selectOne(new LambdaQueryWrapper<UserInbox>()
.eq(UserInbox::getMessageId, messageId)
.eq(UserInbox::getReceiverId, userId));
}
/**
*
* @param userId
* @param messageId
* @return
*/
@Override
public SysMessageVO getReadStatus(Long userId, Long messageId) {
// 验证当前用户是否是消息的发件人
SysMessages sysMessages = messagesService.getById(messageId);
if (sysMessages == null || !sysMessages.getSenderId().equals(userId)) {
throw new BusinessException(ErrorCode.NO_AUTH_ERROR, "您没有权限查看此消息的已读/未读用户信息。");
}
SysMessageVO sysMessageVO = new SysMessageVO();
sysMessageVO.setId(messageId);
// 查询 UserInbox 以获取当前消息的收件人列表
List<UserInbox> userInboxes = userInboxMapper.selectList(
new LambdaQueryWrapper<UserInbox>()
.eq(UserInbox::getMessageId, messageId)
.select(UserInbox::getReceiverId, UserInbox::getIsRead, UserInbox::getReadTime)
);
List<Long> receiverIds = userInboxes.stream().map(UserInbox::getReceiverId).collect(Collectors.toList());
sysMessageVO.setUserNum((long) receiverIds.size()); // 设置收信总人数
// 提取已读消息的 UserInbox 并按阅读时间降序排序
List<UserInbox> readUserInboxes = userInboxes.stream()
.filter(UserInbox::getIsRead)
.sorted(Comparator.comparing(UserInbox::getReadTime, Comparator.nullsLast(Comparator.reverseOrder())))
.collect(Collectors.toList());
// 设置已读学生人数
sysMessageVO.setReadUserNum((long) readUserInboxes.size());
// 创建已读用户的 UserReadMessageTimeVO 列表
List<UserReadMessageTimeVO> readStudents = readUserInboxes.stream()
.map(userInbox -> {
UserReadMessageTimeVO vo = new UserReadMessageTimeVO();
vo.setId(userInbox.getReceiverId());
UmsStudent umsStudent = umsStudentMapper.selectOne(
new LambdaQueryWrapper<UmsStudent>()
.eq(UmsStudent::getUserId, userInbox.getReceiverId()));
if (umsStudent == null) {
vo.setReceiverName(umsTeacherMapper.selectOne(
new LambdaQueryWrapper<UmsTeacher>()
.eq(UmsTeacher::getUserId, userInbox.getReceiverId())
).getName());
} else {
vo.setReceiverName(umsStudent.getName());
}
vo.setReadTime(userInbox.getReadTime());
return vo;
})
.collect(Collectors.toList());
// 创建未读用户的 UserReadMessageTimeVO 列表
List<UserReadMessageTimeVO> noReadStudents = receiverIds.stream()
.filter(id -> readUserInboxes.stream().noneMatch(ri -> ri.getReceiverId().equals(id)))
.map(id -> {
UserReadMessageTimeVO vo = new UserReadMessageTimeVO();
vo.setId(id);
UmsStudent umsStudent = umsStudentMapper.selectOne(
new LambdaQueryWrapper<UmsStudent>()
.eq(UmsStudent::getUserId, id));
if (umsStudent == null) {
vo.setReceiverName(umsTeacherMapper.selectOne(
new LambdaQueryWrapper<UmsTeacher>()
.eq(UmsTeacher::getUserId, id)
).getName());
} else {
vo.setReceiverName(umsStudent.getName());
}
return vo;
})
.collect(Collectors.toList());
// 设置已读和未读学生集合
sysMessageVO.setReadUsers(readStudents);
sysMessageVO.setNoReadUsers(noReadStudents);
return sysMessageVO;
}
}

@ -1,6 +1,7 @@
package com.teaching.backend.service.impl.umsAdmin;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.teaching.backend.common.CommonResult;
import com.teaching.backend.exception.BusinessException;
import com.teaching.backend.mapper.umsAdmin.*;
import com.teaching.backend.model.dto.umsAdmin.UmsStudentPageQueryDTO;
@ -10,15 +11,22 @@ import com.teaching.backend.service.umsAdmin.UmsStudentManageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
@Component
@Service
@Slf4j
public class UmsStudentManageServiceImpl extends ServiceImpl<UmsStudentManageMapper, UmsStudentManage> implements UmsStudentManageService {
@ -31,6 +39,15 @@ public class UmsStudentManageServiceImpl extends ServiceImpl<UmsStudentManageMap
private UmsUserMapper umsUserMapper;
@Autowired
private CoursesServiceImpl coursesService;
@Autowired
private PasswordEncoder passwordEncoder;
@Value("${spring.datasource.url}")
private String myUrl;
@Value("${spring.datasource.username}")
private String myUsername;
@Value("${spring.datasource.password}")
private String myPassword;
@Override
@ -91,4 +108,119 @@ public class UmsStudentManageServiceImpl extends ServiceImpl<UmsStudentManageMap
throw new BusinessException(400,"账号状态修改失败");
}
}
@Override
public CommonResult<String> batchRemove(List<Long> ids) {
String url = myUrl; // 数据库URL
String user = myUsername; // 数据库用户名
String password = myPassword; // 数据库密码
Connection conn = null;
PreparedStatement pstmt1 = null;
PreparedStatement pstmt2 = null;
try {
// 建立数据库连接
conn = DriverManager.getConnection(url, user, password);
// 关闭自动提交
conn.setAutoCommit(false);
StringBuilder inClause = new StringBuilder("(");
for (int i = 0; i < ids.size(); i++) {
inClause.append("?");
if (i < ids.size() - 1) {
inClause.append(", ");
}
}
inClause.append(")");
// 准备SQL语句
String sql1 = "DELETE FROM ums_user WHERE id IN " + inClause.toString();
String sql2 = "DELETE FROM ums_student WHERE user_id IN " + inClause.toString();
// 创建PreparedStatement对象
pstmt1 = conn.prepareStatement(sql1);
pstmt2 = conn.prepareStatement(sql2);
// 为IN子句中的每个ID设置参数
for (int i = 0; i < ids.size(); i++) {
pstmt1.setInt(i + 1, Math.toIntExact(ids.get(i)));
pstmt2.setInt(i + 1, Math.toIntExact(ids.get(i)));
}
// 执行DELETE语句
pstmt1.executeUpdate();
pstmt2.executeUpdate();
// 提交事务
conn.commit();
return CommonResult.success("数据删除成功!");
} catch (SQLException e) {
if (conn != null) {
try {
// 发生异常时回滚事务
conn.rollback();
} catch (SQLException ex) {
ex.printStackTrace();
}
}
e.printStackTrace();
return CommonResult.failed("数据删除失败!");
} finally {
// 关闭资源(在实际应用中,应该使用try-with-resources语句或确保在finally块中关闭所有资源)
try {
if (pstmt1 != null) pstmt1.close();
if (pstmt2 != null) pstmt2.close();
if (conn != null) conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
@Override
public CommonResult<String> initialPassword(List<Long> ids) {
String encodePassword = passwordEncoder.encode("123456");
String url = myUrl; // 数据库URL
String user = myUsername; // 数据库用户名
String password = myPassword; // 数据库密码
Connection conn = null;
String sql = "UPDATE ums_user SET password = ? WHERE id = ?";
try{
conn = DriverManager.getConnection(url, user, password);
PreparedStatement pstmt = conn.prepareStatement(sql);
conn.setAutoCommit(false); // 关闭自动提交,以便进行批处理
for (Long userId : ids) {
pstmt.setString(1, encodePassword);
pstmt.setLong(2, userId);
pstmt.addBatch(); // 将SQL语句添加到批处理中
}
pstmt.executeBatch(); // 执行批处理中的所有更新
conn.commit(); // 提交事务
return CommonResult.success("修改成功!");
} catch (SQLException e) {
e.printStackTrace();
try {
if (conn != null) {
conn.rollback(); // 发生异常时回滚事务
}
} catch (SQLException ex) {
ex.printStackTrace();
}
return CommonResult.failed("修改失败!");
}
}
}

@ -105,7 +105,7 @@ public class UmsUserServiceImpl extends ServiceImpl<UmsUserMapper, UmsUser> impl
if(!passwordEncoder.matches(password,userDetails.getPassword())){
throw new BusinessException(400,"密码错误");
}
if(!userDetails.isEnabled()){
if(userDetails.isEnabled()){
throw new BusinessException(400,"帐号已被禁用");
}
UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken(userDetails, null, userDetails.getAuthorities());

@ -0,0 +1,27 @@
package com.teaching.backend.service.message;
import com.baomidou.mybatisplus.extension.service.IService;
import com.teaching.backend.model.dto.courses.PageDTO;
import com.teaching.backend.model.dto.message.SysMessageDTO;
import com.teaching.backend.model.entity.message.SysMessages;
import com.teaching.backend.model.entity.message.UserInbox;
import com.teaching.backend.model.query.MessagesQuery;
import com.teaching.backend.model.vo.message.SysMessageVO;
import java.util.List;
/**
* <p>
* 服务类
* </p>
*
* @author zjh
* @since 2024-08-06
*/
public interface IMessagesService extends IService<SysMessages> {
String sendMessageToUserInbox(SysMessageDTO sysMessageDTO);
String deleteSendMessagesByLogical(List<Long> messageIds);
}

@ -0,0 +1,32 @@
package com.teaching.backend.service.message;
import com.baomidou.mybatisplus.extension.service.IService;
import com.teaching.backend.model.dto.courses.PageDTO;
import com.teaching.backend.model.entity.message.UserInbox;
import com.teaching.backend.model.query.MessagesQuery;
import com.teaching.backend.model.vo.message.SysMessageVO;
import java.util.List;
/**
* <p>
* 服务类
* </p>
*
* @author zjh
* @since 2024-08-06
*/
public interface IUserInboxService extends IService<UserInbox> {
PageDTO<SysMessageVO> queryReceiveMessages(MessagesQuery messagesQuery);
SysMessageVO getMessageDetail(MessagesQuery messagesQuery);
PageDTO<SysMessageVO> querySendMessages(MessagesQuery messagesQuery);
String readMessagesBatch(List<Long> messageIds, Long userId);
String deleteReceiveMessagesByLogical(List<Long> messageIds, Long userId);
SysMessageVO getReadStatus(Long userId, Long messageId);
}

@ -3,9 +3,12 @@ package com.teaching.backend.service.umsAdmin;
import cn.hutool.db.PageResult;
import com.baomidou.mybatisplus.extension.service.IService;
import com.teaching.backend.common.BaseResponse;
import com.teaching.backend.common.CommonResult;
import com.teaching.backend.model.dto.umsAdmin.UmsStudentPageQueryDTO;
import com.teaching.backend.model.entity.umsAdmin.UmsStudentManage;
import java.util.List;
import java.util.Map;
public interface UmsStudentManageService extends IService<UmsStudentManage> {
@ -18,4 +21,8 @@ public interface UmsStudentManageService extends IService<UmsStudentManage> {
void updateStudentAndUser(UmsStudentManage umsStudentManage);
void startOrStop(String status, Long id);
CommonResult<String> batchRemove(List<Long> ids);
CommonResult<String> initialPassword(List<Long> ids);
}

@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.teaching.backend.mapper.message.UserInboxMapper">
<insert id="insertBatchInBox" parameterType="java.util.List">
INSERT INTO user_inbox (message_id, receiver_id)
VALUES
<foreach collection="list" item="item" index="index" separator=",">
(#{item.messageId}, #{item.receiverId})
</foreach>
</insert>
</mapper>
Loading…
Cancel
Save