From 496b76d6cd2994a6d957f5ab953ce0922183cf91 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=9F=A5=E6=97=A0=E6=B6=AF?= <2637171921@qq.com> Date: Thu, 14 Aug 2025 15:02:03 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E5=8A=A0Dify=E7=9B=B8?= =?UTF-8?q?=E5=85=B3=E6=8E=A5=E5=8F=A3WeChatMentalAlertController=E7=94=A8?= =?UTF-8?q?=E4=BA=8E=E6=8E=A5=E6=94=B6=E4=BF=A1=E6=81=AF=E8=B0=83=E7=94=A8?= =?UTF-8?q?=E4=BC=81=E4=B8=9A=E5=BE=AE=E4=BF=A1=E5=8F=91=E9=80=81=E6=B6=88?= =?UTF-8?q?=E6=81=AF=E7=BB=99=E9=93=BA=E5=AF=BC=E5=91=98=EF=BC=8C=E6=96=B0?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=AF=B9=E6=9C=89=E5=BF=83=E7=90=86=E9=97=AE?= =?UTF-8?q?=E9=A2=98=E5=AD=A6=E7=94=9F=E7=9A=84=E6=95=B0=E6=8D=AE=E5=BA=93?= =?UTF-8?q?=E8=A1=A8=E5=AD=98=E5=82=A8student=5Fmental=5Frating=EF=BC=8C?= =?UTF-8?q?=E6=96=B0=E5=A2=9E=E5=8A=A0=E5=AF=B9student=5Fmental=5Frating?= =?UTF-8?q?=E8=A1=A8=E7=9A=84=E6=89=80=E6=9C=89=E6=9F=A5=E8=AF=A2=E5=92=8C?= =?UTF-8?q?=E5=AD=A6=E5=8F=B7=E7=B2=BE=E5=87=86=E6=9F=A5=E8=AF=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../common/WeChatMentalAlertController.java | 129 ++++++++++++++++++ .../system/domain/StudentMentalRating.java | 49 +++++++ .../mapper/StudentMentalRatingMapper.java | 32 +++++ .../system/StudentMentalRatingMapper.xml | 46 +++++++ 4 files changed, 256 insertions(+) create mode 100644 srs-admin/src/main/java/com/srs/web/controller/common/WeChatMentalAlertController.java create mode 100644 srs-system/src/main/java/com/srs/system/domain/StudentMentalRating.java create mode 100644 srs-system/src/main/java/com/srs/system/mapper/StudentMentalRatingMapper.java create mode 100644 srs-system/src/main/resources/mapper/system/StudentMentalRatingMapper.xml diff --git a/srs-admin/src/main/java/com/srs/web/controller/common/WeChatMentalAlertController.java b/srs-admin/src/main/java/com/srs/web/controller/common/WeChatMentalAlertController.java new file mode 100644 index 0000000..9aa2976 --- /dev/null +++ b/srs-admin/src/main/java/com/srs/web/controller/common/WeChatMentalAlertController.java @@ -0,0 +1,129 @@ +package com.srs.web.controller.common; + +import com.srs.common.core.controller.BaseController; +import com.srs.common.core.domain.AjaxResult; +import com.srs.common.core.domain.entity.SysUser; +import com.srs.common.core.domain.model.LoginUser; +import com.srs.common.utils.WeChatUtil; +import com.srs.framework.web.service.TokenService; +import com.srs.system.domain.StudentMentalRating; +import com.srs.system.mapper.StudentMentalRatingMapper; +import com.srs.system.mapper.SysUserMapper; +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.util.StringUtils; +import org.springframework.web.bind.annotation.*; + +import javax.servlet.http.HttpServletRequest; + +/** + * Dify心理问题发送企业微信 知无涯 + */ +@Slf4j +@RestController +@RequestMapping("/api/wechat") +public class WeChatMentalAlertController extends BaseController { + + @Autowired + private SysUserMapper sysUserMapper; + + @Autowired + private WeChatUtil weChatUtil; + + @Autowired + private TokenService tokenService; + @Autowired + private StudentMentalRatingMapper studentMentalRatingMapper; + + /** + * 处理心理预警通知请求 + */ + @PostMapping("/mentalAlert") + public AjaxResult handleMentalAlert(@RequestBody MentalAlertRequest request, + HttpServletRequest httpRequest) { + + // 校验 token 是否有效 + LoginUser loginUser = tokenService.getLoginUser(httpRequest); + if (loginUser == null) { + return AjaxResult.error("Token无效或已过期"); + } + + // 查询辅导员信息 + SysUser teacher = sysUserMapper.selectTeacherByStuNo(request.getUserId()); + if (teacher == null || !StringUtils.hasText(teacher.getUserName())) { + log.error("辅导员信息不完整,学号: {}", request.getUserId()); + return AjaxResult.error("未分配辅导员或信息不完整"); + } + /* 保存学生心理问题评级 */ + StudentMentalRating record = new StudentMentalRating(); + record.setStudentId(request.getUserId()); + record.setRating(request.getRating()); + studentMentalRatingMapper.insert(record); + + // 构建并发送消息 + try { + String content = buildContent(request, teacher); + weChatUtil.sendTextMessage(teacher.getUserName(), content); + return AjaxResult.success("消息已发送至辅导员"); + } catch (Exception e) { + log.error("发送企业微信失败", e); + return AjaxResult.error("发送失败: " + e.getMessage()); + } + } + + /** + * 构建企业微信消息内容 + */ + private String buildContent(MentalAlertRequest request, SysUser teacher) { + String teacherName = StringUtils.hasText(teacher.getNickName()) + ? teacher.getNickName() + : teacher.getUserName(); + return String.format( + "【心理预警通知】\n" + + "辅导员:%s(%s)\n" + + "学生姓名:%s\n" + + "学号:%s\n" + + "问题描述:%s\n" + + "AI建议:%s\n" + + "心理问题评级:%s", + teacherName, + teacher.getUserName(), + request.getUserName(), + request.getUserId(), + request.getStudentQuestion(), + request.getAiAnswer(), + request.getRating() + ); + } + + + @Data + @NoArgsConstructor + @AllArgsConstructor + public static class MentalAlertRequest { + private String userId; // 学生学号 + private String userName; // 学生姓名 + private String studentQuestion; // 学生提问内容 + private String aiAnswer; // AI回复内容 + private String rating; // 心理问题评级 + } + + /** + * 获取全部学生心理评级记录 + */ + @GetMapping("/rating/all") + public AjaxResult allRatings() { + return AjaxResult.success(studentMentalRatingMapper.selectAll()); + } + + /** + * 根据学号获取全部记录 + */ + @GetMapping("/rating/{stuNo}") + public AjaxResult listByStuNo(@PathVariable String stuNo) { + return AjaxResult.success(studentMentalRatingMapper.selectByStuNo(stuNo)); + } +} \ No newline at end of file diff --git a/srs-system/src/main/java/com/srs/system/domain/StudentMentalRating.java b/srs-system/src/main/java/com/srs/system/domain/StudentMentalRating.java new file mode 100644 index 0000000..b26f57d --- /dev/null +++ b/srs-system/src/main/java/com/srs/system/domain/StudentMentalRating.java @@ -0,0 +1,49 @@ +package com.srs.system.domain; + +import com.fasterxml.jackson.annotation.JsonFormat; +import com.srs.common.core.domain.BaseEntity; + +import java.util.Date; + +/** + * 学生心理问题评级表 知无涯 + */ +public class StudentMentalRating extends BaseEntity { + + private static final long serialVersionUID = 1L; + + private Long id; + private String studentId; + private String rating; + + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getStudentId() { + return studentId; + } + + public void setStudentId(String studentId) { + this.studentId = studentId; + } + + public String getRating() { + return rating; + } + + public void setRating(String rating) { + this.rating = rating; + } + + @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8") + private Date createdTime; + + @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8") + private Date updatedTime; +} \ No newline at end of file diff --git a/srs-system/src/main/java/com/srs/system/mapper/StudentMentalRatingMapper.java b/srs-system/src/main/java/com/srs/system/mapper/StudentMentalRatingMapper.java new file mode 100644 index 0000000..bcc0f32 --- /dev/null +++ b/srs-system/src/main/java/com/srs/system/mapper/StudentMentalRatingMapper.java @@ -0,0 +1,32 @@ +package com.srs.system.mapper; + +import com.srs.system.domain.StudentMentalRating; +import org.apache.ibatis.annotations.Param; + +import java.util.List; + +public interface StudentMentalRatingMapper { + + /** + * 按学号查询 知无涯 + */ + StudentMentalRating selectByStudentId(String studentId); + + /** + * 插入 + */ + int insert(StudentMentalRating record); + + /** + * 按学号更新评级 + */ + int updateRatingByStudentId(StudentMentalRating record); + + /** 全部记录 */ + List selectAll(); + + /** 单学号全部记录 */ + List selectByStuNo(@Param("stuNo") String stuNo); + +} + diff --git a/srs-system/src/main/resources/mapper/system/StudentMentalRatingMapper.xml b/srs-system/src/main/resources/mapper/system/StudentMentalRatingMapper.xml new file mode 100644 index 0000000..88cd1e5 --- /dev/null +++ b/srs-system/src/main/resources/mapper/system/StudentMentalRatingMapper.xml @@ -0,0 +1,46 @@ + + + + + + + + INSERT INTO student_mental_rating(student_id, rating) + VALUES (#{studentId}, #{rating}) + + + + UPDATE student_mental_rating + SET rating = #{rating} + WHERE student_id = #{studentId} + + + + + + + + \ No newline at end of file