This commit is contained in:
zrh050423 2024-12-21 01:44:29 +08:00
parent 7760061688
commit 4884b8391e
6 changed files with 88 additions and 10 deletions

View File

@ -4,6 +4,7 @@ import edu.zrh.healthsystem.entity.TalkInfo;
import edu.zrh.healthsystem.model.Talk;
import edu.zrh.healthsystem.model.TalkResponse;
import edu.zrh.healthsystem.service.TalkService;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
@ -27,10 +28,9 @@ public class TalkController {
public String callApi() {
return talkService.callApi("http://10.10.10.44:11434/");
}
@PostMapping("/talk/main")
public Map<String, Object> callApiChat(@RequestBody Talk talk) {
TalkResponse result = talkService.callApiChat(talk);
public Map<String, Object> callApiChat(@RequestBody Talk talk, HttpServletRequest httpServletRequest) {
TalkResponse result = talkService.callApiChat(talk,httpServletRequest);
if (result != null) {
return Map.of("status", "success", "data", result);
} else {

View File

@ -25,5 +25,10 @@ public class TalkInfo {
@Column(name="talk_user")
private String talkUser;
@ManyToOne
@JoinColumn(name = "talk_user", referencedColumnName = "user_name", insertable = false, updatable = false)
private UserMainInfo userMainInfo;
// 关联到 UserMainInfo
}

View File

@ -34,4 +34,9 @@ public class UserLoginInfo {
*/
@Column(name = "permission")
private String permission;
@ManyToOne
@JoinColumn(name = "id", referencedColumnName = "user_id", insertable = false, updatable = false)
private UserMainInfo userMainInfo;
// 关联到 UserMainInfo
}

View File

@ -0,0 +1,45 @@
package edu.zrh.healthsystem.entity;
import jakarta.persistence.*;
import lombok.Data;
/**
* @author han
*/
@Entity
@Data
@Table(name = "user_main")
public class UserMainInfo {
/**
* ID
*/
@Id
@Column(name = "user_id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
/**
* 名字
*/
@Column(name = "user_name")
private String username;
/**
* 年龄
*/
@Column(name = "user_age")
private Integer age;
/**
* 性别
*/
@Column(name = "user_gender")
private String gender;
/**
* 邮箱
*/
@Column(name = "user_email")
private String email;
}

View File

@ -0,0 +1,8 @@
package edu.zrh.healthsystem.repository;
import edu.zrh.healthsystem.entity.MedicalRecordInfo;
import edu.zrh.healthsystem.entity.UserMainInfo;
import org.springframework.data.jpa.repository.JpaRepository;
public interface UserMainInfoRepository extends JpaRepository<UserMainInfo, Integer> {
}

View File

@ -1,10 +1,15 @@
package edu.zrh.healthsystem.service;
import edu.zrh.healthsystem.controller.TalkController;
import edu.zrh.healthsystem.entity.TalkInfo;
import edu.zrh.healthsystem.entity.UserMainInfo;
import edu.zrh.healthsystem.model.Talk;
import edu.zrh.healthsystem.model.TalkResponse;
import edu.zrh.healthsystem.repository.TalkInfoRepository;
import edu.zrh.healthsystem.repository.UserMainInfoRepository;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.client.RestTemplate;
import java.time.LocalDateTime;
@ -19,24 +24,34 @@ import java.util.Map;
public class TalkService {
private final RestTemplate restTemplate;
private final TalkInfoRepository talkInfoRepository;
public TalkService(RestTemplate restTemplate, TalkInfoRepository talkInfoRepository) {
private final UserMainInfoRepository userMainInfoRepository;
public TalkService(RestTemplate restTemplate, TalkInfoRepository talkInfoRepository, UserMainInfoRepository userMainInfoRepository) {
this.restTemplate = restTemplate;
this.talkInfoRepository = talkInfoRepository;
this.userMainInfoRepository = userMainInfoRepository;
}
public String callApi(String url) {
return restTemplate.getForObject(url, String.class);
}
public TalkResponse callApiChat(Talk request) {
public TalkResponse callApiChat(Talk request, HttpServletRequest httpServletRequest) {
TalkResponse response = restTemplate.postForObject("http://10.10.10.44:11434/api/generate", request, TalkResponse.class);
String userId = (String) httpServletRequest.getAttribute("user");
UserMainInfo userMainInfo = userMainInfoRepository.findById(Integer.parseInt(userId)).orElse(null);
if (userMainInfo != null) {
TalkInfo talkInfo = new TalkInfo();
talkInfo.setTalkPrompt(request.getPrompt());
if (response != null) {
talkInfo.setTalkResponse(response.getResponse());
}
talkInfo.setTalkTime(LocalDateTime.now().format(DateTimeFormatter.ISO_LOCAL_DATE_TIME));
TalkInfo talkInfo = new TalkInfo();
talkInfo.setTalkPrompt(request.getPrompt());
talkInfo.setTalkResponse(response.getResponse());
talkInfo.setTalkTime(LocalDateTime.now().format(DateTimeFormatter.ISO_LOCAL_DATE_TIME));
talkInfo.setTalkUser(userMainInfo.getUsername());
talkInfoRepository.save(talkInfo);
// 保存聊天记录
talkInfoRepository.save(talkInfo);
}
return response;
}
public List<TalkInfo> getAllRecord() {