diff --git a/src/main/java/com/teaching/backend/common/ErrorCode.java b/src/main/java/com/teaching/backend/common/ErrorCode.java
index 95830fb..4883d57 100644
--- a/src/main/java/com/teaching/backend/common/ErrorCode.java
+++ b/src/main/java/com/teaching/backend/common/ErrorCode.java
@@ -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, "请求数据不存在"),
diff --git a/src/main/java/com/teaching/backend/controller/courses/CoursesController.java b/src/main/java/com/teaching/backend/controller/courses/CoursesController.java
index adba450..c42bb16 100644
--- a/src/main/java/com/teaching/backend/controller/courses/CoursesController.java
+++ b/src/main/java/com/teaching/backend/controller/courses/CoursesController.java
@@ -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> getUserInfByCourse(@RequestParam Long userId){
+ List courseUsersInfList = coursesService.getUserInfByCourse(userId);
+ return ResultUtils.success(courseUsersInfList);
+ }
+
}
diff --git a/src/main/java/com/teaching/backend/controller/message/MessagesController.java b/src/main/java/com/teaching/backend/controller/message/MessagesController.java
new file mode 100644
index 0000000..be95f7f
--- /dev/null
+++ b/src/main/java/com/teaching/backend/controller/message/MessagesController.java
@@ -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;
+
+/**
+ *
+ * 前端控制器
+ *
+ *
+ * @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 sendMessage(@RequestBody SysMessageDTO sysMessageDTO){
+ String data = messagesService.sendMessageToUserInbox(sysMessageDTO);
+ return ResultUtils.success(data);
+ }
+
+ //TODO 功能待完善
+ @ApiOperation("删除发件箱的消息--批删--逻辑删除")
+ @ValidateParams({"messageIds"}) // 需要校验的参数
+ @DeleteMapping ("/deletesendmessage")
+ public BaseResponse deleteMessages(@RequestParam List messageIds){
+ String data = messagesService.deleteSendMessagesByLogical(messageIds);
+ return ResultUtils.success(data);
+ }
+
+
+
+ @ApiOperation("根据id查询消息")
+ @ValidateParams({"id"}) // 需要校验的参数
+ @GetMapping ("/getmessage/{id}")
+ public BaseResponse getMessageById(@PathVariable("id") Long id){
+ SysMessages data = messagesService.getById(id);
+ return ResultUtils.success(data);
+ }
+
+ /**
+ * 修改消息
+ * @param sysMessageDTO
+ * @return
+ */
+ @ApiOperation("修改消息--仅限于未发送成功的消息(草稿功能)")
+ @PutMapping ("/modifymessage")
+ public BaseResponse modifyMessage(@RequestBody SysMessageDTO sysMessageDTO){
+ boolean b = messagesService.updateById(sysMessageDTO);
+ return ResultUtils.success(b ? "修改成功" : "修改失败");
+ }
+
+
+}
diff --git a/src/main/java/com/teaching/backend/controller/message/UserInboxController.java b/src/main/java/com/teaching/backend/controller/message/UserInboxController.java
new file mode 100644
index 0000000..0fb7469
--- /dev/null
+++ b/src/main/java/com/teaching/backend/controller/message/UserInboxController.java
@@ -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;
+
+/**
+ *
+ * 前端控制器
+ *
+ *
+ * @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> getMessages(MessagesQuery messagesQuery){
+ PageDTO messageList = userInboxService.queryReceiveMessages(messagesQuery);
+ return ResultUtils.success(messageList);
+ }
+
+ @ApiOperation("用户分页查看个人消息(发件箱)")
+ @ValidateParams({"userId","isDelete","isSend"})
+ @GetMapping("/page/sendmessages")
+ public BaseResponse> getSendMessages(MessagesQuery messagesQuery){
+ PageDTO messageList = userInboxService.querySendMessages(messagesQuery);
+ return ResultUtils.success(messageList);
+ }
+
+ //用户查看消息详情,如果是第一次查看,则会更新“已读状态”和“阅读时间”
+ @ApiOperation("查看消息详情")
+ @ValidateParams({"userId","messageId"}) // 需要校验的参数
+ @GetMapping("/getmessagedetails")
+ public BaseResponse getMessageDetail(MessagesQuery messagesQuery){
+ SysMessageVO userCheckMessageVO = userInboxService.getMessageDetail(messagesQuery);
+ return ResultUtils.success(userCheckMessageVO);
+ }
+
+ @ApiOperation("获取已读/未读用户集合")
+ @ValidateParams({"userId", "messageId"})
+ @GetMapping("/message/readstatus")
+ public BaseResponse getMessageReadStatus(@RequestParam Long userId, @RequestParam Long messageId) {
+ SysMessageVO messageVO = userInboxService.getReadStatus(userId, messageId);
+ return ResultUtils.success(messageVO);
+ }
+
+
+ @ApiOperation("一键已读")
+ @PutMapping ("/readmessagesbatch")
+ public BaseResponse readMessagesBatch(@RequestParam List messageIds, @RequestParam Long userId){
+ String data = userInboxService.readMessagesBatch(messageIds,userId);
+ return ResultUtils.success(data);
+ }
+
+ @ApiOperation("删除收件箱的消息--批删--逻辑删除")
+ @ValidateParams({"messageIds","userId"}) // 需要校验的参数
+ @DeleteMapping ("/deletereceivemessage")
+ public BaseResponse deleteReceiveMessages(@RequestParam List messageIds,@RequestParam Long userId){
+ String data = userInboxService.deleteReceiveMessagesByLogical(messageIds,userId);
+ return ResultUtils.success(data);
+ }
+
+}
diff --git a/src/main/java/com/teaching/backend/controller/umsAdmin/UmsStudentManageController.java b/src/main/java/com/teaching/backend/controller/umsAdmin/UmsStudentManageController.java
index 6dbf21c..71c47b8 100644
--- a/src/main/java/com/teaching/backend/controller/umsAdmin/UmsStudentManageController.java
+++ b/src/main/java/com/teaching/backend/controller/umsAdmin/UmsStudentManageController.java
@@ -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 batchDelete(@RequestParam List ids){
+ return umsStudentManageService.batchRemove(ids);
+ }
+
+ /**
+ * 根据ids初始密码
+ * @param ids
+ * @return
+ */
+ @ApiOperation("学生信息批量初始密码")
+ @PutMapping("/initialPassword")
+ public CommonResult initialPassword(@RequestParam List ids){
+ return umsStudentManageService.initialPassword(ids);
+ }
+
}
diff --git a/src/main/java/com/teaching/backend/mapper/message/MessagesMapper.java b/src/main/java/com/teaching/backend/mapper/message/MessagesMapper.java
new file mode 100644
index 0000000..7c267c4
--- /dev/null
+++ b/src/main/java/com/teaching/backend/mapper/message/MessagesMapper.java
@@ -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;
+
+/**
+ *
+ * Mapper 接口
+ *
+ *
+ * @author zjh
+ * @since 2024-08-06
+ */
+public interface MessagesMapper extends BaseMapper {
+
+}
diff --git a/src/main/java/com/teaching/backend/mapper/message/UserInboxMapper.java b/src/main/java/com/teaching/backend/mapper/message/UserInboxMapper.java
new file mode 100644
index 0000000..bf18b1f
--- /dev/null
+++ b/src/main/java/com/teaching/backend/mapper/message/UserInboxMapper.java
@@ -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;
+
+/**
+ *
+ * Mapper 接口
+ *
+ *
+ * @author zjh
+ * @since 2024-08-06
+ */
+public interface UserInboxMapper extends BaseMapper {
+
+ boolean insertBatchInBox(List userInboxes);
+}
diff --git a/src/main/java/com/teaching/backend/model/dto/message/SysMessageDTO.java b/src/main/java/com/teaching/backend/model/dto/message/SysMessageDTO.java
new file mode 100644
index 0000000..87a82c5
--- /dev/null
+++ b/src/main/java/com/teaching/backend/model/dto/message/SysMessageDTO.java
@@ -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;
+}
diff --git a/src/main/java/com/teaching/backend/model/entity/courses/StudentCourses.java b/src/main/java/com/teaching/backend/model/entity/courses/StudentCourses.java
index a91a9a0..151e8ea 100644
--- a/src/main/java/com/teaching/backend/model/entity/courses/StudentCourses.java
+++ b/src/main/java/com/teaching/backend/model/entity/courses/StudentCourses.java
@@ -33,7 +33,7 @@ public class StudentCourses implements Serializable {
/**
* 学生的userId
*/
- private String student;
+ private Long student;
/**
* 课程id
diff --git a/src/main/java/com/teaching/backend/model/entity/message/SysMessages.java b/src/main/java/com/teaching/backend/model/entity/message/SysMessages.java
new file mode 100644
index 0000000..1677b01
--- /dev/null
+++ b/src/main/java/com/teaching/backend/model/entity/message/SysMessages.java
@@ -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;
+
+/**
+ *
+ *
+ *
+ *
+ * @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;
+
+
+}
diff --git a/src/main/java/com/teaching/backend/model/entity/message/UserInbox.java b/src/main/java/com/teaching/backend/model/entity/message/UserInbox.java
new file mode 100644
index 0000000..aca75c1
--- /dev/null
+++ b/src/main/java/com/teaching/backend/model/entity/message/UserInbox.java
@@ -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;
+
+/**
+ *
+ *
+ *
+ *
+ * @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;
+
+
+}
diff --git a/src/main/java/com/teaching/backend/model/query/CourseQuery.java b/src/main/java/com/teaching/backend/model/query/CourseQuery.java
index b8d1e73..0e523dc 100644
--- a/src/main/java/com/teaching/backend/model/query/CourseQuery.java
+++ b/src/main/java/com/teaching/backend/model/query/CourseQuery.java
@@ -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;
}
diff --git a/src/main/java/com/teaching/backend/model/query/MessagesQuery.java b/src/main/java/com/teaching/backend/model/query/MessagesQuery.java
new file mode 100644
index 0000000..33172d4
--- /dev/null
+++ b/src/main/java/com/teaching/backend/model/query/MessagesQuery.java
@@ -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;
+
+
+}
diff --git a/src/main/java/com/teaching/backend/model/query/PageQuery.java b/src/main/java/com/teaching/backend/model/query/PageQuery.java
index 7b8e7da..7896a66 100644
--- a/src/main/java/com/teaching/backend/model/query/PageQuery.java
+++ b/src/main/java/com/teaching/backend/model/query/PageQuery.java
@@ -41,4 +41,5 @@ public class PageQuery {
public Page toMpPageDefaultSortByUpdateTime(){
return toMpPage(new OrderItem("update_time", false));
}
+
}
diff --git a/src/main/java/com/teaching/backend/model/vo/message/CourseUserInfVO.java b/src/main/java/com/teaching/backend/model/vo/message/CourseUserInfVO.java
new file mode 100644
index 0000000..364f456
--- /dev/null
+++ b/src/main/java/com/teaching/backend/model/vo/message/CourseUserInfVO.java
@@ -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 usersInf;
+
+}
diff --git a/src/main/java/com/teaching/backend/model/vo/message/SysMessageVO.java b/src/main/java/com/teaching/backend/model/vo/message/SysMessageVO.java
new file mode 100644
index 0000000..f4ddd9c
--- /dev/null
+++ b/src/main/java/com/teaching/backend/model/vo/message/SysMessageVO.java
@@ -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 receiveUsers;
+
+ @ApiModelProperty(value = "是否已读(0没有,1有)")
+ private Boolean isRead;
+
+ @ApiModelProperty(value = "已读用户集合")
+ private List readUsers;
+
+ @ApiModelProperty(value = "未读用户集合")
+ private List noReadUsers;
+
+}
diff --git a/src/main/java/com/teaching/backend/model/vo/message/UserReadMessageTimeVO.java b/src/main/java/com/teaching/backend/model/vo/message/UserReadMessageTimeVO.java
new file mode 100644
index 0000000..413ce06
--- /dev/null
+++ b/src/main/java/com/teaching/backend/model/vo/message/UserReadMessageTimeVO.java
@@ -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;
+
+}
diff --git a/src/main/java/com/teaching/backend/service/courses/ICoursesService.java b/src/main/java/com/teaching/backend/service/courses/ICoursesService.java
index 9f619a2..ac2e4c7 100644
--- a/src/main/java/com/teaching/backend/service/courses/ICoursesService.java
+++ b/src/main/java/com/teaching/backend/service/courses/ICoursesService.java
@@ -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 {
List queryStudentList(String userId);
LinkedHashSet queryTeacherByStudentList(String userId);
+
+ List getUserInfByCourse(Long userId);
}
diff --git a/src/main/java/com/teaching/backend/service/impl/courses/CoursesServiceImpl.java b/src/main/java/com/teaching/backend/service/impl/courses/CoursesServiceImpl.java
index 313528a..da9f0dc 100644
--- a/src/main/java/com/teaching/backend/service/impl/courses/CoursesServiceImpl.java
+++ b/src/main/java/com/teaching/backend/service/impl/courses/CoursesServiceImpl.java
@@ -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 implements ICoursesService {
@Autowired
@@ -195,10 +199,10 @@ public class CoursesServiceImpl extends ServiceImpl impl
Page 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 impl
Page page = courseQuery.toMpPageDefaultSortByCreateTime();
Page 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 impl
List studentUsernames = studentCoursesMapper.selectBatchSomeStudent(courseId);
List 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 impl
throw new BusinessException(400,"只有老师才有此功能");
}
+
+
@Override
public List getPagePageSize(int page, int pageSize) {
int startIndex = (page-1) * pageSize;
@@ -610,6 +616,155 @@ public class CoursesServiceImpl extends ServiceImpl impl
document.write(outputStream);
}
+ @Override
+ public List 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;
+ 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 queryCourseUserForTeacher(Long userId) {
+ // 查询所有与该教师相关的课程
+ List teacherCoursesInf = coursesMapper.selectList(
+ new LambdaQueryWrapper()
+ .apply("FIND_IN_SET({0}, teacher)", userId)
+ .select(Courses::getId, Courses::getTeacher)
+ );
+
+ List courseUserInfVOS = new ArrayList<>();
+ for (Courses courses : teacherCoursesInf) {
+ CourseUserInfVO courseUserInfVO = new CourseUserInfVO();
+ List userReadMessageTimeVOS = new ArrayList<>();
+ courseUserInfVO.setCourseId(courses.getId());
+
+ // 查询与课程相关的所有学生
+ List 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()
+ .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 queryCourseUserForStudent(Long userId) {
+ // 查询学生所参与的所有课程
+ List studentCoursesList = studentCoursesMapper.selectList(
+ new LambdaQueryWrapper()
+ .eq(StudentCourses::getStudent, userId)
+ .select(StudentCourses::getCourse)
+ );
+
+ List courseUserInfVOS = new ArrayList<>();
+ for (StudentCourses studentCourse : studentCoursesList) {
+ CourseUserInfVO courseUserInfVO = new CourseUserInfVO();
+ List userReadMessageTimeVOS = new ArrayList<>();
+ courseUserInfVO.setCourseId(studentCourse.getCourse());
+
+ // 查询与课程相关的所有学生
+ List 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()
+ .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;
+ }
}
diff --git a/src/main/java/com/teaching/backend/service/impl/courses/StudentCoursesServiceImpl.java b/src/main/java/com/teaching/backend/service/impl/courses/StudentCoursesServiceImpl.java
index 4a92433..4caa06f 100644
--- a/src/main/java/com/teaching/backend/service/impl/courses/StudentCoursesServiceImpl.java
+++ b/src/main/java/com/teaching/backend/service/impl/courses/StudentCoursesServiceImpl.java
@@ -53,7 +53,7 @@ public class StudentCoursesServiceImpl extends ServiceImpl
+ * 服务实现类
+ *
+ *
+ * @author zjh
+ * @since 2024-08-06
+ */
+@Service
+@Slf4j
+public class MessagesServiceImpl extends ServiceImpl 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 receiverIds = Arrays.stream(sysMessageDTO.getReceiverId().split(","))
+ .map(Long::valueOf)
+ .collect(Collectors.toList());
+
+ // 4.数据写入收件箱
+ List 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 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());
+ }
+ }
+
+}
diff --git a/src/main/java/com/teaching/backend/service/impl/message/UserInboxServiceImpl.java b/src/main/java/com/teaching/backend/service/impl/message/UserInboxServiceImpl.java
new file mode 100644
index 0000000..3dd07ca
--- /dev/null
+++ b/src/main/java/com/teaching/backend/service/impl/message/UserInboxServiceImpl.java
@@ -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;
+
+/**
+ *
+ * 服务实现类
+ *
+ *
+ * @author zjh
+ * @since 2024-08-06
+ */
+@Service
+@Slf4j
+public class UserInboxServiceImpl extends ServiceImpl 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 queryReceiveMessages(MessagesQuery messagesQuery) {
+ // 先按用户的id找到其的消息列表 默认看未删除的,(是否读 作为筛选条件后传进去)
+ LambdaQueryWrapper userInboxQueryWrapper = new LambdaQueryWrapper<>();
+ userInboxQueryWrapper
+ .eq(UserInbox::getReceiverId, messagesQuery.getUserId())
+ .eq(messagesQuery.getIsRead() != null,UserInbox::getIsRead,messagesQuery.getIsRead())
+ .eq(UserInbox::getIsDelete, messagesQuery.getIsDelete());
+ List userInboxes = userInboxMapper.selectList(userInboxQueryWrapper);
+ if (userInboxes.isEmpty()){
+ throw new BusinessException(ErrorCode.MESSAGE_NO_EXIT);
+ }
+ Map messageReadStatusMap = userInboxes.stream()
+ .collect(Collectors.toMap(UserInbox::getMessageId, UserInbox::getIsRead));
+
+ List messageIds = new ArrayList<>(messageReadStatusMap.keySet());
+
+ Page page = messagesQuery.toMpPage("send_time", false);
+
+ // 根据条件查询SysMessages表
+ Page p = messagesService.page(page,
+ new LambdaQueryWrapper()
+ .in(SysMessages::getId,messageIds)
+ .select(SysMessages::getId,SysMessages::getCourseId,SysMessages::getTitle,SysMessages::getSenderId,SysMessages::getSendTime)
+ );
+
+ // 将Page转换为PageDTO
+
+ PageDTO messages = PageDTO.of(p, SysMessageVO.class);
+// PageDTO messages = PageDTO.of(p, message -> {
+// SysMessageVO vo = new SysMessageVO();
+// BeanUtil.copyProperties(message, vo, CopyOptions.create().setIgnoreProperties(SysMessageVO::getContent));
+// return vo;
+// });
+ // 获取PageDTO中的SysMessageVO列表
+ List messagesList = messages.getList();
+ // 填充附加数据
+ for (SysMessageVO messageVO : messagesList) {
+ //获取发件人信息
+ UmsTeacher umsTeacher = umsTeacherMapper.selectOne(
+ new LambdaQueryWrapper()
+ .eq(UmsTeacher::getUserId, messageVO.getSenderId()));
+ //没有这个教师那说明就是学生
+ if (umsTeacher == null) {
+ messageVO.setSenderName(umsStudentMapper.selectOne(
+ new LambdaQueryWrapper()
+ .eq(UmsStudent::getUserId, messageVO.getSenderId())
+ ).getName());
+ } else {
+ messageVO.setSenderName(umsTeacher.getName());
+ }
+
+ Courses course = coursesMapper.selectOne(new LambdaQueryWrapper()
+ .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 querySendMessages(MessagesQuery messagesQuery) {
+ // 初始化一个按发送时间降序排序的Page对象
+ Page page = messagesQuery.toMpPage("send_time", false);
+
+ // 根据条件查询SysMessages表
+ Page p = messagesService.page(page,
+ new LambdaQueryWrapper()
+ .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转换为PageDTO
+ PageDTO messages = PageDTO.of(p, SysMessageVO.class);
+ List messagesList = messages.getList();
+
+ if (messagesList.isEmpty()) {
+ throw new BusinessException(ErrorCode.MESSAGE_NO_EXIT);
+ }
+
+ // 批量查询课程名称
+ Set courseIds = messagesList.stream()
+ .map(SysMessageVO::getCourseId)
+ .filter(Objects::nonNull)
+ .collect(Collectors.toSet());
+
+ Map courseNames = coursesMapper.selectBatchIds(courseIds).stream()
+ .collect(Collectors.toMap(Courses::getId, Courses::getName));
+
+ // 批量查询所有用户收件箱数据
+ Set messageIds = messagesList.stream()
+ .map(SysMessageVO::getId)
+ .collect(Collectors.toSet());
+
+ List allUserInboxes = userInboxMapper.selectList(
+ new LambdaQueryWrapper()
+ .in(UserInbox::getMessageId, messageIds)
+ .select(UserInbox::getMessageId, UserInbox::getReceiverId, UserInbox::getIsRead)
+ );
+
+ // 将UserInbox数据按消息ID进行分组
+ Map> userInboxesByMessageId = allUserInboxes.stream()
+ .collect(Collectors.groupingBy(UserInbox::getMessageId));
+
+ // 缓存用户名称以避免重复查询
+ Map userNamesCache = new HashMap<>();
+
+ for (SysMessageVO sysMessageVO : messagesList) {
+ String courseId = sysMessageVO.getCourseId();
+ if (courseId != null) {
+ sysMessageVO.setCourseName(courseNames.get(courseId));
+ }
+
+ List userInboxes = userInboxesByMessageId.get(sysMessageVO.getId());
+ if (userInboxes != null) {
+ sysMessageVO.setUserNum((long) userInboxes.size());
+ sysMessageVO.setReadUserNum(userInboxes.stream().filter(UserInbox::getIsRead).count());
+
+ List 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().eq(UmsStudent::getUserId, id)
+ );
+ if (umsStudent != null) {
+ return umsStudent.getName();
+ } else {
+ UmsTeacher umsTeacher = umsTeacherMapper.selectOne(
+ new LambdaQueryWrapper().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()
+ .eq(UmsTeacher::getUserId, sysMessage.getSenderId()));
+ if (umsTeacher == null) {
+ sysMessageVO.setSenderName(umsStudentMapper.selectOne(
+ new LambdaQueryWrapper()
+ .eq(UmsStudent::getUserId, sysMessage.getSenderId())
+ ).getName());
+ } else {
+ sysMessageVO.setSenderName(umsTeacher.getName());
+ }
+
+ // 根据当前用户是发件人还是收件人决定查询的内容
+ if (sysMessage.getSenderId().equals(messagesQuery.getUserId())) {
+ // 发件人查看详情,查询所有收件人信息
+ List userInboxes = userInboxMapper.selectList(
+ new LambdaQueryWrapper()
+ .eq(UserInbox::getMessageId, messagesQuery.getMessageId())
+ );
+
+ List receiveUsers = userInboxes.stream().map(userInbox -> {
+ UserReadMessageTimeVO vo = new UserReadMessageTimeVO();
+ vo.setId(userInbox.getReceiverId());
+ UmsStudent umsStudent = umsStudentMapper.selectOne(
+ new LambdaQueryWrapper()
+ .eq(UmsStudent::getUserId, userInbox.getReceiverId()));
+ if (umsStudent == null) {
+ vo.setReceiverName(umsTeacherMapper.selectOne(
+ new LambdaQueryWrapper()
+ .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()
+ .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 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 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()
+ .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 userInboxes = userInboxMapper.selectList(
+ new LambdaQueryWrapper()
+ .eq(UserInbox::getMessageId, messageId)
+ .select(UserInbox::getReceiverId, UserInbox::getIsRead, UserInbox::getReadTime)
+ );
+
+ List receiverIds = userInboxes.stream().map(UserInbox::getReceiverId).collect(Collectors.toList());
+ sysMessageVO.setUserNum((long) receiverIds.size()); // 设置收信总人数
+
+ // 提取已读消息的 UserInbox 并按阅读时间降序排序
+ List 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 readStudents = readUserInboxes.stream()
+ .map(userInbox -> {
+ UserReadMessageTimeVO vo = new UserReadMessageTimeVO();
+ vo.setId(userInbox.getReceiverId());
+ UmsStudent umsStudent = umsStudentMapper.selectOne(
+ new LambdaQueryWrapper()
+ .eq(UmsStudent::getUserId, userInbox.getReceiverId()));
+ if (umsStudent == null) {
+ vo.setReceiverName(umsTeacherMapper.selectOne(
+ new LambdaQueryWrapper()
+ .eq(UmsTeacher::getUserId, userInbox.getReceiverId())
+ ).getName());
+ } else {
+ vo.setReceiverName(umsStudent.getName());
+ }
+ vo.setReadTime(userInbox.getReadTime());
+ return vo;
+ })
+ .collect(Collectors.toList());
+
+ // 创建未读用户的 UserReadMessageTimeVO 列表
+ List 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()
+ .eq(UmsStudent::getUserId, id));
+ if (umsStudent == null) {
+ vo.setReceiverName(umsTeacherMapper.selectOne(
+ new LambdaQueryWrapper()
+ .eq(UmsTeacher::getUserId, id)
+ ).getName());
+ } else {
+ vo.setReceiverName(umsStudent.getName());
+ }
+ return vo;
+ })
+ .collect(Collectors.toList());
+
+ // 设置已读和未读学生集合
+ sysMessageVO.setReadUsers(readStudents);
+ sysMessageVO.setNoReadUsers(noReadStudents);
+
+ return sysMessageVO;
+ }
+
+}
diff --git a/src/main/java/com/teaching/backend/service/impl/umsAdmin/UmsStudentManageServiceImpl.java b/src/main/java/com/teaching/backend/service/impl/umsAdmin/UmsStudentManageServiceImpl.java
index 5dea081..a81f5f0 100644
--- a/src/main/java/com/teaching/backend/service/impl/umsAdmin/UmsStudentManageServiceImpl.java
+++ b/src/main/java/com/teaching/backend/service/impl/umsAdmin/UmsStudentManageServiceImpl.java
@@ -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 implements UmsStudentManageService {
@@ -31,6 +39,15 @@ public class UmsStudentManageServiceImpl extends ServiceImpl batchRemove(List 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 initialPassword(List 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("修改失败!");
+ }
+
+ }
}
diff --git a/src/main/java/com/teaching/backend/service/impl/umsAdmin/UmsUserServiceImpl.java b/src/main/java/com/teaching/backend/service/impl/umsAdmin/UmsUserServiceImpl.java
index e691525..797c403 100644
--- a/src/main/java/com/teaching/backend/service/impl/umsAdmin/UmsUserServiceImpl.java
+++ b/src/main/java/com/teaching/backend/service/impl/umsAdmin/UmsUserServiceImpl.java
@@ -105,7 +105,7 @@ public class UmsUserServiceImpl extends ServiceImpl 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());
diff --git a/src/main/java/com/teaching/backend/service/message/IMessagesService.java b/src/main/java/com/teaching/backend/service/message/IMessagesService.java
new file mode 100644
index 0000000..687d204
--- /dev/null
+++ b/src/main/java/com/teaching/backend/service/message/IMessagesService.java
@@ -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;
+
+/**
+ *
+ * 服务类
+ *
+ *
+ * @author zjh
+ * @since 2024-08-06
+ */
+public interface IMessagesService extends IService {
+
+ String sendMessageToUserInbox(SysMessageDTO sysMessageDTO);
+
+ String deleteSendMessagesByLogical(List messageIds);
+
+}
diff --git a/src/main/java/com/teaching/backend/service/message/IUserInboxService.java b/src/main/java/com/teaching/backend/service/message/IUserInboxService.java
new file mode 100644
index 0000000..9bc13ba
--- /dev/null
+++ b/src/main/java/com/teaching/backend/service/message/IUserInboxService.java
@@ -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;
+
+/**
+ *
+ * 服务类
+ *
+ *
+ * @author zjh
+ * @since 2024-08-06
+ */
+public interface IUserInboxService extends IService {
+
+ PageDTO queryReceiveMessages(MessagesQuery messagesQuery);
+
+ SysMessageVO getMessageDetail(MessagesQuery messagesQuery);
+
+ PageDTO querySendMessages(MessagesQuery messagesQuery);
+
+ String readMessagesBatch(List messageIds, Long userId);
+
+ String deleteReceiveMessagesByLogical(List messageIds, Long userId);
+
+ SysMessageVO getReadStatus(Long userId, Long messageId);
+}
diff --git a/src/main/java/com/teaching/backend/service/umsAdmin/UmsStudentManageService.java b/src/main/java/com/teaching/backend/service/umsAdmin/UmsStudentManageService.java
index cc741bf..55e346a 100644
--- a/src/main/java/com/teaching/backend/service/umsAdmin/UmsStudentManageService.java
+++ b/src/main/java/com/teaching/backend/service/umsAdmin/UmsStudentManageService.java
@@ -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 {
@@ -18,4 +21,8 @@ public interface UmsStudentManageService extends IService {
void updateStudentAndUser(UmsStudentManage umsStudentManage);
void startOrStop(String status, Long id);
+
+ CommonResult batchRemove(List ids);
+
+ CommonResult initialPassword(List ids);
}
diff --git a/src/main/resources/mapper/UserInboxMapper.xml b/src/main/resources/mapper/UserInboxMapper.xml
new file mode 100644
index 0000000..99c8bec
--- /dev/null
+++ b/src/main/resources/mapper/UserInboxMapper.xml
@@ -0,0 +1,12 @@
+
+
+
+
+
+ INSERT INTO user_inbox (message_id, receiver_id)
+ VALUES
+
+ (#{item.messageId}, #{item.receiverId})
+
+
+