新增加Dify相关接口WeChatMentalAlertController用于接收信息调用企业微信发送消息给铺导员,新增加对有心理问题学生的数据库表存储student_mental_rating,新增加对student_mental_rating表的所有查询和学号精准查询
This commit is contained in:
@@ -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));
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user